Andrzej Bialecki created SOLR-18720:
---------------------------------------

             Summary: CrossDC Consumer: concurrent processing threads apply 
updates out of order
                 Key: SOLR-18720
                 URL: https://issues.apache.org/jira/browse/SOLR-18720
             Project: Solr
          Issue Type: Bug
          Components: module - crossDC
            Reporter: Andrzej Bialecki
            Assignee: Andrzej Bialecki


{{KafkaCrossDcConsumer.pollAndProcessRequests()}} reads the records of a 
partition in offset order and groups them into batches. A new batch starts when 
any of these is true:
 * the record's params differ from the previous record's,
 * {{CollapseUpdates.NONE}} is set (a new batch for every record),
 * {{CollapseUpdates.PARTIAL}} (the default) is set and the record holds a 
delete, or the current batch already holds one,
 * the batch has reached {{{}maxCollapseRecords{}}}.

Each batch goes to {{{}sendBatch(){}}}. That method submits the batch to a 
{{ThreadPoolExecutor}} with {{solr.crossdc.consumerProcessingThreads}} threads 
(default 5) and moves on to the next batch without waiting, so consecutive 
batches of the same collection run at the same time and can reach the target 
Solr in any order. Two other mechanisms look like they might prevent this, but 
neither does:
 * {{PartitionManager}} orders only the offset commits. It commits up to the 
first unfinished work unit, but it does not control the order in which batches 
run.
 * The bounded work queue ({{{}BlockingQueue{}}}, capacity 10) only blocks the 
submitter when the queue is full.

SOLR-18077 keys records by collection, so all records of a collection land in 
one partition in the right order. The consumer has never kept that order when 
it applies the records. The splits at deletes exist because order matters 
there, but the split batches can still run in any order.

Consequences on the target:
 * a document that the source added and then deleted by id survives
 * a document added after a Delete-By-Query is deleted
 * an overwrite is lost. This can happen without any deletes: adds of the same 
id that land in different batches (because their params differ or 
{{maxCollapseRecords}} was reached) can arrive in either order. Atomic and 
in-place updates are affected the same way.
 * a mirrored commit (an UPDATE with {{{}commit=true{}}}) gets its own batch 
because its params differ. It can run before the batch in front of it, so the 
target does not show those changes until the next commit.

h3. Steps to reproduce
 # Set up a source SolrCloud cluster with 
{{MirroringUpdateRequestProcessorFactory}} in the default update chain and 
{{{}solr.crossdc.mirrorCommits=true{}}}. Set up a target cluster with the stock 
consumer and default settings. We used 2 source nodes, a collection with 2 
shards and {{{}replicationFactor=1{}}}, and {{{}solr.crossdc.expandDbq=none{}}}.
 # Send one JSON update to a source node (see below).
 # Wait until the consumer has caught up, then compare {{id:json1_*}} on source 
and target.

{code:json}
{
  "add": {"doc": {"id": "json1_a1", "grp_s": "a"}},
  "add": {"doc": {"id": "json1_a2", "grp_s": "a"}},
  "add": {"doc": {"id": "json1_b1", "grp_s": "b"}},
  "add": {"doc": {"id": "json1_b2", "grp_s": "b"}},
  "add": {"doc": {"id": "json1_c1", "grp_s": "c"}},
  "delete": {"id": "json1_a1"},
  "delete": {"query": "id:json1_* AND grp_s:b"},
  "add": {"doc": {"id": "json1_b1", "grp_s": "b"}},
  "add": {"doc": {"id": "json1_b3", "grp_s": "b"}},
  "add": {"doc": {"id": "json1_d1", "grp_s": "d"}},
  "delete": {"id": "json1_d1"}
}
{code}
h3. Actual result
||cluster||documents||
|source|{{json1_a2 json1_b1 json1_b3 json1_c1}}|
|target|{{json1_a1 json1_a2 json1_b3 json1_c1 json1_d1}}|

{{json1_a1}} and {{json1_d1}} survived their delete-by-id. {{{}json1_b1{}}}, 
re-added after the Delete-By-Query, is missing.

The records were in the right order in the topic, both in the source Kafka and 
in the target Kafka (MirrorMaker2 sat in between). All of them were in one 
partition at consecutive offsets:
{noformat}
51407 add json1_b1      51411 add json1_b2              51415 add json1_b3
51408 add json1_c1      51412 delete json1_a1           51416 add json1_d1
51409 add json1_a1      51413 DBQ grp_s:b               51417 delete json1_d1
51410 add json1_a2      51414 add json1_b1              51418 commit
{noformat}
The consumer splits these records into six batches and submits all six without 
waiting:
||batch||offsets||content||reason for split||
|1|51407–51411|add b1, c1, a1, a2, b2| |
|2|51412|delete a1|holds a delete|
|3|51413|DBQ {{grp_s:b}}|holds a delete|
|4|51414–51416|add b1, b3, d1|previous batch holds a delete|
|5|51417|delete d1|holds a delete|
|6|51418|commit|different params|

The target state matches batch 2 running before batch 1, batch 5 before batch 
4, and batch 3 after batch 4.

The outcome depends on timing, and a short request sometimes comes out right. 
We ran three scenarios: the one above, a JSON request that overwrites, deletes 
and re-adds documents from an earlier request, and the same commands sent as an 
XML request. With the defaults, all three failed on every run.

With {{solr.crossdc.consumerProcessingThreads=1}} and nothing else changed, all 
three pass. One worker reading a FIFO queue runs the batches in the order they 
were submitted.
h3. Expected result

The target ends up with the same documents as the source. Requests for one 
collection are applied in offset order.
h3. Suggested fix

Keep the parallelism across collections, but run the batches of one collection 
one at a time. Keep the last {{Future}} per collection, and chain each new 
batch of that collection to run only after the previous one completes. Offset 
commits stay correct as they are, because {{PartitionManager}} only commits 
over completed work units.

Serializing per partition instead is simpler, but on a single-partition topic 
it is the same as running with one thread. Waiting only around batches with 
deletes is not enough, because add-only batches also reorder.

Until that is in place, set the default of {{consumerProcessingThreads}} to 1. 
Also state in {{cross-dc-replication.adoc}} that values above 1 do not preserve 
the order of updates within a collection.
h3. Test plan
 * Unit test in {{{}KafkaCrossDcConsumerTest{}}}: use a mocked 
{{messageProcessor}} that delays add batches, feed it the record sequence 
above, and check that handling order matches offset order for each collection.
 * Integration test in {{{}SolrAndKafkaIntegrationTest{}}}: send the JSON 
request above with the default thread count and compare source and target.

h3. Related
 * {{processResult()}} handles {{FAILED_RESUBMIT}} by sending the request back 
to the end of the main topic ({{{}KafkaCrossDcConsumer.java:611-627{}}}), 
behind later requests for the same collection. A retried add can therefore 
overwrite a later delete. {{FAILED_NO_RETRY}} sends the request to the DLQ, and 
later requests are applied without it. These deserve their own ticket.
 * ADMIN and CONFIGSET requests are submitted right away 
({{{}KafkaCrossDcConsumer.java:511{}}}), before the pending update batch is 
flushed. They can overtake earlier updates even with one thread. This matters 
little in practice, because collection admin requests usually have no 
collection key and go to a different partition anyway.

🤖 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