Andrzej Bialecki created SOLR-18721:
---------------------------------------

             Summary: CrossDC Producer: a Delete-By-Query can be written to 
Kafka out of order with adds and deletes around it
                 Key: SOLR-18721
                 URL: https://issues.apache.org/jira/browse/SOLR-18721
             Project: Solr
          Issue Type: Bug
          Components: module - crossDC
            Reporter: Andrzej Bialecki
            Assignee: Andrzej Bialecki


Each mirrored command is sent to Kafka from a different core depending on its 
type:
 * adds and Delete-By-Id: from the shard leader of the document 
({{{}MirroringUpdateProcessor.java:169-186{}}}, {{{}254-279{}}})
 * Delete-By-Query: from the core that received the request, either as-is 
({{{}expandDbq=none{}}}, {{{}283-301{}}}) or as pages of Delete-By-Id 
({{{}expandDbq=expand{}}}, the default, {{{}199-248{}}})

Each core has its own {{KafkaProducer}} . {{producer.send()}} is asynchronous 
and the records wait up to {{linger.ms}} (default 30) in the buffer. Kafka 
keeps the order of one producer's records, not across producers. So whenever a 
collection has more than one shard leader, even on one node, a Delete-By-Query 
can land in the topic before adds that came before it, or after adds that came 
after it. On the target this deletes documents it shouldn't, or keeps documents 
it should delete. The consumer can't fix this because the topic is already 
wrong. It also contradicts the ref guide's claim of strict per-collection 
ordering ({{{}cross-dc-replication.adoc{}}}, Limitations).
h3. Steps to reproduce
 # Source: 2 nodes, a collection with 2 shards, {{{}replicationFactor=1{}}}, 
{{{}expandDbq=none{}}}. Target: {{{}consumerProcessingThreads=1{}}}, to rule 
out SOLR-18720.
 # Send one JSON request of about 930 commands to one node (generator below).
 # Compare {{id:gen_*}} on source and target, then replay the topic in offset 
order.

{code:python}
for i in range(1, 801):
    cmds.append(f'"add":{{"doc":{{"id":"gen_{i}","grp_s":"g{i % 20}"}}}}')
    if i % 7 == 0:
        cmds.append(f'"delete":{{"id":"gen_{i - 3}"}}')
    if i % 50 == 0:
        cmds.append(f'"delete":{{"query":"id:gen_* AND grp_s:g{(i // 50) % 
20}"}}')
body = "{" + ",".join(cmds) + "}"
{code}
h3. Actual result

The target is missing {{{}gen_122 gen_306 gen_469 gen_469 gen_734{}}}. All 930 
records are in the topic exactly once, and replaying them in offset order gives 
exactly the target's documents. So the topic order is wrong: 5 of the 16 
Delete-By-Query records come after 1 to 32 adds that the request sent after 
them, all of them for the shard led by the other node. For example, {{gen_122}} 
(group {{{}g2{}}}) was added after the Delete-By-Query of {{{}g2{}}}, but comes 
before it in the topic.

Not tested yet: a single node leading both shards, and 
{{{}expandDbq=expand{}}}. Reading the code, both should fail the same way.
h3. Expected result

Within one collection, the topic order follows the order in which the source 
applied the commands.
h3. Suggested fix

Before forwarding a Delete-By-Query, {{SolrCmdDistributor.distribDelete()}} 
calls {{{}blockAndDoRetries(){}}}, so the earlier adds have already been 
processed by their leaders. Their records just haven't left the producer buffer 
yet. Two waits fix this:
 # In {{finish()}} (currently a no-op), wait for the {{send()}} futures of this 
request. A leader then finishes the forwarded adds before the Delete-By-Query 
is sent.
 # On the receiving core, wait right after mirroring a Delete-By-Query, or 
after each expanded Delete-By-Id page.

The expand path skips {{{}blockAndDoRetries(){}}}, so it also has to drain the 
forwarded adds before it searches for matching ids. Per-producer ordering needs 
idempotence, which is the default in kafka-clients 4.x, so it should not be 
overridable through additional properties. The cost is one broker round trip 
per update request, which could be made configurable. Fix 1 also covers 
commits, which are mirrored from the first shard's leader.
h3. Related
 * SOLR-18720: consumer-side reordering when {{consumerProcessingThreads}} > 1.

🤖 Created with assistance from [Claude Code|https://claude.com/claude-code]



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to