Skip to content

Commit a8a7e6e

Browse files
committed
Expose Sequence Number based Optimistic Concurrency Control in the rest layer (#36721)
Relates #36148 Relates #10708
1 parent 9439dbc commit a8a7e6e

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
@@ -44,6 +44,14 @@
4444
"type" : "time",
4545
"description" : "Explicit operation timeout"
4646
},
47+
"if_seq_no_match" : {
48+
"type" : "number",
49+
"description" : "only perform the delete operation if the last operation that has changed the document has the specified sequence number"
50+
},
51+
"if_primary_term_match" : {
52+
"type" : "number",
53+
"description" : "only perform the delete operation if the last operation that has changed the document has the specified primary term"
54+
},
4755
"version" : {
4856
"type" : "number",
4957
"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
@@ -58,6 +58,14 @@
5858
"options" : ["internal", "external", "external_gte", "force"],
5959
"description" : "Specific version type"
6060
},
61+
"if_seq_no_match" : {
62+
"type" : "number",
63+
"description" : "only perform the index operation if the last operation that has changed the document has the specified sequence number"
64+
},
65+
"if_primary_term_match" : {
66+
"type" : "number",
67+
"description" : "only perform the index operation if the last operation that has changed the document has the specified primary term"
68+
},
6169
"pipeline" : {
6270
"type" : "string",
6371
"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.5.99"
6+
reason: cas ops are introduced in 6.6.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
@@ -54,6 +54,10 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
5454
deleteRequest.setRefreshPolicy(request.param("refresh"));
5555
deleteRequest.version(RestActions.parseVersion(request));
5656
deleteRequest.versionType(VersionType.fromString(request.param("version_type"), deleteRequest.versionType()));
57+
deleteRequest.setIfMatch(
58+
request.paramAsLong("if_seq_no_match", deleteRequest.ifSeqNoMatch()),
59+
request.paramAsLong("if_primary_term_match", deleteRequest.ifPrimaryTermMatch())
60+
);
5761

5862
String waitForActiveShards = request.param("wait_for_active_shards");
5963
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
@@ -79,6 +79,10 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
7979
indexRequest.setRefreshPolicy(request.param("refresh"));
8080
indexRequest.version(RestActions.parseVersion(request));
8181
indexRequest.versionType(VersionType.fromString(request.param("version_type"), indexRequest.versionType()));
82+
indexRequest.ifMatch(
83+
request.paramAsLong("if_seq_no_match", indexRequest.ifSeqNoMatch()),
84+
request.paramAsLong("if_primary_term_match", indexRequest.ifPrimaryTermMatch())
85+
);
8286
String sOpType = request.param("op_type");
8387
String waitForActiveShards = request.param("wait_for_active_shards");
8488
if (waitForActiveShards != null) {

0 commit comments

Comments
 (0)