-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Description
Roman Puchkovskiy opened DATAES-799 and commented
Elasticsearch currently recommends using seq_no + primary_term combination to implement optimistic locking: https://www.elastic.co/guide/en/elasticsearch/reference/current/optimistic-concurrency-control.html
Consider the following scenario:
- Repository.findById() is called
- The returned entity is edited by a user
- Repository.save() is called to save the entity
To make optimistic locking work in this scenario, on step 1 the returned entity must return seq_no + primary_term, and on step 3 they must be passed in the index request. But currently, if I'm not mistaken, the returned seq_no + primary_term are not mapped to the entity instance when the entity is materialized, and no way exists to pass them back to save() methods.
I suggest the following:
- Add
@SequenceNumber
and@PrimaryTerm
annotations in org.springframework.data.elasticsearch.annotations; they will be used to mark Long entity fields - When materializing an entity from GetResult, SearchHit, or any other possible DTO coming from 'getting' Elasticsearch response, fill fields mapped with these new annotations with seq_no and primary_term values from the response
- When creating an IndexRequest during indexing/saving, call setIfSeqNo() and setIfPrimaryTerm() on the constructed request with values from the annotated fields
An alternative to have 2 separate fields and 2 annotations is to invent a special value object combining them both, like SequencePosition, encapsulating both seq_no and primary_term, and add just one annotation, @SequenceNumber
, to annotate a field of type SequencePosition.
Does this make sense?
Affects: 4.0 RC2 (Neumann)
Issue Links:
- DATAES-876 Add seqno and primary term to entity on initial save
Referenced from: pull request #441