Wrong order of documents updated in ES and database

Hello,

I collected more information and I was able to replicate the issue by debugging the code:

Set breakpoints:
a) ElasticsearchBatchingWorkOrchestrator (method “protected void doSubmit(ElasticsearchBatchedWork<?> work, OperationSubmitter operationSubmitter)”) - line 111: executors.get( work.getQueuingKey() ).submit( work, operationSubmitter );

b) BatchingExecutor (method “public CompletableFuture<?> work()”) - line 170: workBuffer.clear();

Steps to reproduce:

  1. “Thread 1” commit database transaction - data : id = 1, status = ‘OPEN’, version = 1
  2. “Thread 1” execution will stop on breakpoint a)
  • The record is committed in database so it’s visible from other threads
  1. “Thread 2” commit database transaction - data : id = 1, status = ‘DELETED’, version = 2
  • The database now contains those values “id = 1, status = ‘DELETED’, version = 2”
  1. “Thread 2” execution will stop on breakpoint a)
  2. “Thread 2” continue with the code execution
  3. “Thread 1” continue with the code execution
  4. Execution will stop at breakpoint b)
    workQueue contains 2 records:
    [0] - id = 1, status = ‘DELETED’, version = 2
    [1] - id = 1, status = ‘OPEN’, version = 1

The records in the workedQueue are in wrong order

  1. continue with the execution
  2. execution will stop at breakpoint b)
    workQueue contains 0 records

So now I have “id = 1, status = ‘DELETED’, version = 2” in database but in ES there are wrong data “id = 1, status = ‘OPEN’, version = 1”.

Some values from Object “work” in ElasticsearchBatchingWorkOrchestrator.doSubmit:
work.refreshStrategy = DocumentRefreshStrategy.NONE
work.resultAssessor = ElasticsearchRequestSuccessAssessor[ignoredErrorStatuses=[ ], ignoredErrorTypes=[ ], ignoreShardFailures=true]

This is just one possibility to reproduce the issue, but in general the issue occurs when workQueue contains records in wrong order.

Am I missing some settings on entity or in hibernate search that would validate version based on an attribute in entity? Some kind of optimistic locking?

Thank you.