Skip to content

Commit 7872365

Browse files
bleskesdavidkyle
authored andcommitted
Expose Sequence Number based Optimistic Concurrency Control in the rest layer (#36721)
Relates #36148 Relates #10708
1 parent 557a5be commit 7872365

File tree

7 files changed

+76
-2
lines changed

7 files changed

+76
-2
lines changed

rest-api-spec/src/main/resources/rest-api-spec/api/delete.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@
4343
"type" : "time",
4444
"description" : "Explicit operation timeout"
4545
},
46+
"if_seq_no_match" : {
47+
"type" : "number",
48+
"description" : "only perform the delete operation if the last operation that has changed the document has the specified sequence number"
49+
},
50+
"if_primary_term_match" : {
51+
"type" : "number",
52+
"description" : "only perform the delete operation if the last operation that has changed the document has the specified primary term"
53+
},
4654
"version" : {
4755
"type" : "number",
4856
"description" : "Explicit version number for concurrency control"

rest-api-spec/src/main/resources/rest-api-spec/api/index.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@
5757
"options" : ["internal", "external", "external_gte", "force"],
5858
"description" : "Specific version type"
5959
},
60+
"if_seq_no_match" : {
61+
"type" : "number",
62+
"description" : "only perform the index operation if the last operation that has changed the document has the specified sequence number"
63+
},
64+
"if_primary_term_match" : {
65+
"type" : "number",
66+
"description" : "only perform the index operation if the last operation that has changed the document has the specified primary term"
67+
},
6068
"pipeline" : {
6169
"type" : "string",
6270
"description" : "The pipeline id to preprocess incoming documents with"
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
"Compare And Swap Sequence Numbers":
3+
4+
- skip:
5+
version: " - 6.99.99"
6+
reason: cas ops are introduced in 7.0.0
7+
8+
- do:
9+
index:
10+
index: test_1
11+
id: 1
12+
body: { foo: bar }
13+
- match: { _version: 1}
14+
- set: { _seq_no: seqno }
15+
- set: { _primary_term: primary_term }
16+
17+
- do:
18+
get:
19+
index: test_1
20+
id: 1
21+
- match: { _seq_no: $seqno }
22+
- match: { _primary_term: $primary_term }
23+
24+
- do:
25+
catch: conflict
26+
index:
27+
index: test_1
28+
id: 1
29+
if_seq_no_match: 10000
30+
if_primary_term_match: $primary_term
31+
body: { foo: bar2 }
32+
33+
- do:
34+
catch: conflict
35+
index:
36+
index: test_1
37+
id: 1
38+
if_seq_no_match: $seqno
39+
if_primary_term_match: 1000
40+
body: { foo: bar2 }
41+
42+
- do:
43+
index:
44+
index: test_1
45+
id: 1
46+
if_seq_no_match: $seqno
47+
if_primary_term_match: $primary_term
48+
body: { foo: bar2 }
49+
50+
- match: { _version: 2 }

server/src/main/java/org/elasticsearch/action/get/GetResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public long getVersion() {
9191
}
9292

9393
/**
94-
* The sequence number assigned to the last operation to have changed this document, if found.
94+
* The sequence number assigned to the last operation that has changed this document, if found.
9595
*/
9696
public long getSeqNo() {
9797
return getResult.getSeqNo();

server/src/main/java/org/elasticsearch/index/get/GetResult.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public long getVersion() {
131131
}
132132

133133
/**
134-
* The sequence number assigned to the last operation to have changed this document, if found.
134+
* The sequence number assigned to the last operation that has changed this document, if found.
135135
*/
136136
public long getSeqNo() {
137137
return seqNo;

server/src/main/java/org/elasticsearch/rest/action/document/RestDeleteAction.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
6666
deleteRequest.setRefreshPolicy(request.param("refresh"));
6767
deleteRequest.version(RestActions.parseVersion(request));
6868
deleteRequest.versionType(VersionType.fromString(request.param("version_type"), deleteRequest.versionType()));
69+
deleteRequest.setIfMatch(
70+
request.paramAsLong("if_seq_no_match", deleteRequest.ifSeqNoMatch()),
71+
request.paramAsLong("if_primary_term_match", deleteRequest.ifPrimaryTermMatch())
72+
);
6973

7074
String waitForActiveShards = request.param("wait_for_active_shards");
7175
if (waitForActiveShards != null) {

server/src/main/java/org/elasticsearch/rest/action/document/RestIndexAction.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
9393
indexRequest.setRefreshPolicy(request.param("refresh"));
9494
indexRequest.version(RestActions.parseVersion(request));
9595
indexRequest.versionType(VersionType.fromString(request.param("version_type"), indexRequest.versionType()));
96+
indexRequest.ifMatch(
97+
request.paramAsLong("if_seq_no_match", indexRequest.ifSeqNoMatch()),
98+
request.paramAsLong("if_primary_term_match", indexRequest.ifPrimaryTermMatch())
99+
);
96100
String sOpType = request.param("op_type");
97101
String waitForActiveShards = request.param("wait_for_active_shards");
98102
if (waitForActiveShards != null) {

0 commit comments

Comments
 (0)