Skip to content

Support optimistic locking for full update scenario using seq_no + primary_term [DATAES-799] #1371

@spring-projects-issues

Description

@spring-projects-issues

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:

  1. Repository.findById() is called
  2. The returned entity is edited by a user
  3. 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:

  1. Add @SequenceNumber and @PrimaryTerm annotations in org.springframework.data.elasticsearch.annotations; they will be used to mark Long entity fields
  2. 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
  3. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues in core supportin: mappingMapping and conversion infrastructuretype: enhancementA general enhancement

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions