microsoft / spring-data-cosmosdb

Access data with Azure Cosmos DB
MIT License
94 stars 64 forks source link

Optimistic Lock implementation based on _etag field #396

Closed dsibilio closed 5 years ago

dsibilio commented 5 years ago

Fixes #378

Here you can find a basic implementation of Optimistic Locking via CosmosDB if-match precondition based on the _etag field of an entity.

Behaviour

Upserts that result in updates with not matching _etags will fail. Deletes with not matching _etags will fail, but - as of the current implementation - deletes by Id and collection deletion do not apply any concurrency control.

How-To

The _etag field needs to be a String field, explicitly declared and marked with the org.springframework.data.annotation.Version annotation at field level, inside the entity that needs concurrency control features such as optimistic locking.

E.g.

@Document(collection = "testCollection")
class VersionedVolunteer {
    String id;
    String name;
    @Version
    String _etag;
}

Notes

kushagraThapar commented 5 years ago

/azp run

azure-pipelines[bot] commented 5 years ago
Pull request contains merge conflicts.
kushagraThapar commented 5 years ago

@dsibilio can you please resolve the merge conflicts for this PR. I will run additional tests to make sure we are passing everything.

dsibilio commented 5 years ago

@dsibilio can you please resolve the merge conflicts for this PR. I will run additional tests to make sure we are passing everything.

@kushagraThapar conflicts have been merged, ty for the reminder ;-)

kushagraThapar commented 5 years ago

@dsibilio Can you please resolve conflicts again for this PR, we would like to get it in today if possible. We would like to include this in our 2.1.8 release.

Thanks!

dsibilio commented 5 years ago

@dsibilio Can you please resolve conflicts again for this PR, we would like to get it in today if possible. We would like to include this in our 2.1.8 release.

Thanks!

I've merged the conflicts but couldn't run the tests. I'm having trouble with my internet connection, so if any issues occur feel free to make any amends to my branch - edits should be allowed by any spring-data-cosmosdb mantainer.

kushagraThapar commented 5 years ago

@dsibilio Can you please resolve conflicts again for this PR, we would like to get it in today if possible. We would like to include this in our 2.1.8 release. Thanks!

I've merged the conflicts but couldn't run the tests. I'm having trouble with my internet connection, so if any issues occur feel free to make any amends to my branch - edits should be allowed by any spring-data-cosmosdb mantainer.

Thanks for fixing the conflicts, I will run the CI for this branch.

kushagraThapar commented 5 years ago

/azp run

azure-pipelines[bot] commented 5 years ago
No pipelines are associated with this pull request.
dsibilio commented 5 years ago

@kushagraThapar Fixed codacy issues after handling conflitcs with master, I cannot run the azure pipeline by myself due to insufficient permission. When you try and run the azp we can check if all tests are ok :)

azure-pipelines[bot] commented 5 years ago
Commenter does not have sufficient privileges for PR 396 in repo microsoft/spring-data-cosmosdb
kushagraThapar commented 5 years ago

/azp run

azure-pipelines[bot] commented 5 years ago
No pipelines are associated with this pull request.
dsibilio commented 5 years ago

/azp run

Fixed a JUnit that failed due to checking wrong illegal arguments (those are downstack, checked by another test anyways). You can try again :+1:

kushagraThapar commented 5 years ago

/azp run

azure-pipelines[bot] commented 5 years ago
No pipelines are associated with this pull request.
kushagraThapar commented 5 years ago

/azp run Emulator-spring-data-cosmosdb-tests

azure-pipelines[bot] commented 5 years ago
No pipelines are associated with this pull request.
kushagraThapar commented 5 years ago

@dsibilio, the emulator test keeps failing, I ran it twice to make sure. Can you please fix it ?

dsibilio commented 5 years ago

@dsibilio, the emulator test keeps failing, I ran it twice to make sure. Can you please fix it ?

It seems the "precondition is not met" message doesn't match with the emulator's "pre-condition is not met" message. Since integration tests run, I assume the message is delivered differently if run against a real CosmosDb instance and a CosmosDb Emulator instance (?) I fixed the JUnit test assertion to just check for the "is not met" part of the error message, if you run it again we can verify further errors if any :+1:

kushagraThapar commented 5 years ago

/azp run

azure-pipelines[bot] commented 5 years ago
No pipelines are associated with this pull request.