Skip to content

Commit 5f76f39

Browse files
authored
Rename seq# powered optimistic concurrency control parameters to ifSeqNo/ifPrimaryTerm (#36757)
This PR renames the parameters previously introduce to the following: ### URL Parameters ``` PUT twitter/_doc/1?if_seq_no=501&if_primary_term=1 { "user" : "kimchy", "post_date" : "2009-11-15T14:12:12", "message" : "trying out Elasticsearch" } DELETE twitter/_doc/1?if_seq_no=501&if_primary_term=1 ``` ### Bulk API ``` POST _bulk { "index" : { "_index" : "test", "_type" : "_doc", "_id" : "1", "if_seq_no": 501, "if_primary_term": 1 } } { "field1" : "value1" } { "delete" : { "_index" : "test", "_type" : "_doc", "_id" : "2", "if_seq_no": 501, "if_primary_term": 1 } } ``` ### Java API ``` IndexRequest.ifSeqNo(long seqNo) IndexRequest.ifPrimaryTerm(long primaryTerm) DeleteRequest.ifSeqNo(long seqNo) DeleteRequest.ifPrimaryTerm(long primaryTerm) ``` Relates #36148 Relates #10708
1 parent af57575 commit 5f76f39

File tree

16 files changed

+244
-156
lines changed

16 files changed

+244
-156
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@
4343
"type" : "time",
4444
"description" : "Explicit operation timeout"
4545
},
46-
"if_seq_no_match" : {
46+
"if_seq_no" : {
4747
"type" : "number",
4848
"description" : "only perform the delete operation if the last operation that has changed the document has the specified sequence number"
4949
},
50-
"if_primary_term_match" : {
50+
"if_primary_term" : {
5151
"type" : "number",
5252
"description" : "only perform the delete operation if the last operation that has changed the document has the specified primary term"
5353
},

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@
5757
"options" : ["internal", "external", "external_gte", "force"],
5858
"description" : "Specific version type"
5959
},
60-
"if_seq_no_match" : {
60+
"if_seq_no" : {
6161
"type" : "number",
6262
"description" : "only perform the index operation if the last operation that has changed the document has the specified sequence number"
6363
},
64-
"if_primary_term_match" : {
64+
"if_primary_term" : {
6565
"type" : "number",
6666
"description" : "only perform the index operation if the last operation that has changed the document has the specified primary term"
6767
},

rest-api-spec/src/main/resources/rest-api-spec/test/index/30_cas.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,25 @@
2626
index:
2727
index: test_1
2828
id: 1
29-
if_seq_no_match: 10000
30-
if_primary_term_match: $primary_term
29+
if_seq_no: 10000
30+
if_primary_term: $primary_term
3131
body: { foo: bar2 }
3232

3333
- do:
3434
catch: conflict
3535
index:
3636
index: test_1
3737
id: 1
38-
if_seq_no_match: $seqno
39-
if_primary_term_match: 1000
38+
if_seq_no: $seqno
39+
if_primary_term: 1000
4040
body: { foo: bar2 }
4141

4242
- do:
4343
index:
4444
index: test_1
4545
id: 1
46-
if_seq_no_match: $seqno
47-
if_primary_term_match: $primary_term
46+
if_seq_no: $seqno
47+
if_primary_term: $primary_term
4848
body: { foo: bar2 }
4949

5050
- match: { _version: 2 }

server/src/main/java/org/elasticsearch/action/bulk/BulkRequest.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public class BulkRequest extends ActionRequest implements CompositeIndicesReques
7878
private static final ParseField RETRY_ON_CONFLICT = new ParseField("retry_on_conflict");
7979
private static final ParseField PIPELINE = new ParseField("pipeline");
8080
private static final ParseField SOURCE = new ParseField("_source");
81-
private static final ParseField IF_SEQ_NO_MATCH = new ParseField("if_seq_no_match");
82-
private static final ParseField IF_PRIMARY_TERM_MATCH = new ParseField("if_primary_term_match");
81+
private static final ParseField IF_SEQ_NO = new ParseField("if_seq_no");
82+
private static final ParseField IF_PRIMARY_TERM = new ParseField("if_primary_term");
8383

8484
/**
8585
* Requests that are part of this request. It is only possible to add things that are both {@link ActionRequest}s and
@@ -350,8 +350,8 @@ public BulkRequest add(BytesReference data, @Nullable String defaultIndex, @Null
350350
String opType = null;
351351
long version = Versions.MATCH_ANY;
352352
VersionType versionType = VersionType.INTERNAL;
353-
long ifSeqNoMatch = SequenceNumbers.UNASSIGNED_SEQ_NO;
354-
long ifPrimaryTermMatch = 0;
353+
long ifSeqNo = SequenceNumbers.UNASSIGNED_SEQ_NO;
354+
long ifPrimaryTerm = 0;
355355
int retryOnConflict = 0;
356356
String pipeline = valueOrDefault(defaultPipeline, globalPipeline);
357357

@@ -382,10 +382,10 @@ public BulkRequest add(BytesReference data, @Nullable String defaultIndex, @Null
382382
version = parser.longValue();
383383
} else if (VERSION_TYPE.match(currentFieldName, parser.getDeprecationHandler())) {
384384
versionType = VersionType.fromString(parser.text());
385-
} else if (IF_SEQ_NO_MATCH.match(currentFieldName, parser.getDeprecationHandler())) {
386-
ifSeqNoMatch = parser.longValue();
387-
} else if (IF_PRIMARY_TERM_MATCH.match(currentFieldName, parser.getDeprecationHandler())) {
388-
ifPrimaryTermMatch = parser.longValue();
385+
} else if (IF_SEQ_NO.match(currentFieldName, parser.getDeprecationHandler())) {
386+
ifSeqNo = parser.longValue();
387+
} else if (IF_PRIMARY_TERM.match(currentFieldName, parser.getDeprecationHandler())) {
388+
ifPrimaryTerm = parser.longValue();
389389
} else if (RETRY_ON_CONFLICT.match(currentFieldName, parser.getDeprecationHandler())) {
390390
retryOnConflict = parser.intValue();
391391
} else if (PIPELINE.match(currentFieldName, parser.getDeprecationHandler())) {
@@ -414,7 +414,7 @@ public BulkRequest add(BytesReference data, @Nullable String defaultIndex, @Null
414414

415415
if ("delete".equals(action)) {
416416
add(new DeleteRequest(index, type, id).routing(routing)
417-
.version(version).versionType(versionType).setIfMatch(ifSeqNoMatch, ifPrimaryTermMatch), payload);
417+
.version(version).versionType(versionType).setIfSeqNo(ifSeqNo).setIfPrimaryTerm(ifPrimaryTerm), payload);
418418
} else {
419419
nextMarker = findNextMarker(marker, from, data, length);
420420
if (nextMarker == -1) {
@@ -427,16 +427,17 @@ public BulkRequest add(BytesReference data, @Nullable String defaultIndex, @Null
427427
if ("index".equals(action)) {
428428
if (opType == null) {
429429
internalAdd(new IndexRequest(index, type, id).routing(routing).version(version).versionType(versionType)
430-
.setPipeline(pipeline).ifMatch(ifSeqNoMatch, ifPrimaryTermMatch)
430+
.setPipeline(pipeline).setIfSeqNo(ifSeqNo).setIfPrimaryTerm(ifPrimaryTerm)
431431
.source(sliceTrimmingCarriageReturn(data, from, nextMarker,xContentType), xContentType), payload);
432432
} else {
433433
internalAdd(new IndexRequest(index, type, id).routing(routing).version(version).versionType(versionType)
434-
.create("create".equals(opType)).setPipeline(pipeline).ifMatch(ifSeqNoMatch, ifPrimaryTermMatch)
434+
.create("create".equals(opType)).setPipeline(pipeline)
435+
.setIfSeqNo(ifSeqNo).setIfPrimaryTerm(ifPrimaryTerm)
435436
.source(sliceTrimmingCarriageReturn(data, from, nextMarker, xContentType), xContentType), payload);
436437
}
437438
} else if ("create".equals(action)) {
438439
internalAdd(new IndexRequest(index, type, id).routing(routing).version(version).versionType(versionType)
439-
.create(true).setPipeline(pipeline).ifMatch(ifSeqNoMatch, ifPrimaryTermMatch)
440+
.create(true).setPipeline(pipeline).setIfSeqNo(ifSeqNo).setIfPrimaryTerm(ifPrimaryTerm)
440441
.source(sliceTrimmingCarriageReturn(data, from, nextMarker, xContentType), xContentType), payload);
441442
} else if ("update".equals(action)) {
442443
UpdateRequest updateRequest = new UpdateRequest(index, type, id).routing(routing).retryOnConflict(retryOnConflict)

server/src/main/java/org/elasticsearch/action/bulk/TransportShardBulkAction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ private static void executeIndexRequestOnPrimary(BulkPrimaryExecutionContext con
462462
executeOnPrimaryWhileHandlingMappingUpdates(context,
463463
() ->
464464
primary.applyIndexOperationOnPrimary(request.version(), request.versionType(), sourceToParse,
465-
request.ifSeqNoMatch(), request.ifPrimaryTermMatch(), request.getAutoGeneratedTimestamp(), request.isRetry()),
465+
request.ifSeqNo(), request.ifPrimaryTerm(), request.getAutoGeneratedTimestamp(), request.isRetry()),
466466
e -> primary.getFailedIndexResult(e, request.version()),
467467
context::markOperationAsExecuted,
468468
mapping -> mappingUpdater.updateMappings(mapping, primary.shardId(), request.type()));
@@ -474,7 +474,7 @@ private static void executeDeleteRequestOnPrimary(BulkPrimaryExecutionContext co
474474
final IndexShard primary = context.getPrimary();
475475
executeOnPrimaryWhileHandlingMappingUpdates(context,
476476
() -> primary.applyDeleteOperationOnPrimary(request.version(), request.type(), request.id(), request.versionType(),
477-
request.ifSeqNoMatch(), request.ifPrimaryTermMatch()),
477+
request.ifSeqNo(), request.ifPrimaryTerm()),
478478
e -> primary.getFailedDeleteResult(e, request.version()),
479479
context::markOperationAsExecuted,
480480
mapping -> mappingUpdater.updateMappings(mapping, primary.shardId(), request.type()));

server/src/main/java/org/elasticsearch/action/delete/DeleteRequest.java

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ public class DeleteRequest extends ReplicatedWriteRequest<DeleteRequest>
5858
private String routing;
5959
private long version = Versions.MATCH_ANY;
6060
private VersionType versionType = VersionType.INTERNAL;
61-
private long ifSeqNoMatch = SequenceNumbers.UNASSIGNED_SEQ_NO;
62-
private long ifPrimaryTermMatch = 0;
61+
private long ifSeqNo = SequenceNumbers.UNASSIGNED_SEQ_NO;
62+
private long ifPrimaryTerm = 0;
6363

6464
public DeleteRequest() {
6565
}
@@ -116,11 +116,20 @@ public ActionRequestValidationException validate() {
116116
validationException = addValidationError("version type [force] may no longer be used", validationException);
117117
}
118118

119-
if (ifSeqNoMatch != SequenceNumbers.UNASSIGNED_SEQ_NO && (
119+
if (ifSeqNo != SequenceNumbers.UNASSIGNED_SEQ_NO && (
120120
versionType != VersionType.INTERNAL || version != Versions.MATCH_ANY
121121
)) {
122122
validationException = addValidationError("compare and write operations can not use versioning", validationException);
123123
}
124+
125+
if (ifPrimaryTerm == 0 && ifSeqNo != SequenceNumbers.UNASSIGNED_SEQ_NO) {
126+
validationException = addValidationError("ifSeqNo is set, but primary term is [0]", validationException);
127+
}
128+
if (ifPrimaryTerm != 0 && ifSeqNo == SequenceNumbers.UNASSIGNED_SEQ_NO) {
129+
validationException =
130+
addValidationError("ifSeqNo is unassigned, but primary term is [" + ifPrimaryTerm + "]", validationException);
131+
}
132+
124133
return validationException;
125134
}
126135

@@ -203,29 +212,52 @@ public DeleteRequest versionType(VersionType versionType) {
203212
return this;
204213
}
205214

206-
public long ifSeqNoMatch() {
207-
return ifSeqNoMatch;
215+
/**
216+
* If set, only perform this delete request if the document was last modification was assigned this sequence number.
217+
* If the document last modification was assigned a different sequence number a
218+
* {@link org.elasticsearch.index.engine.VersionConflictEngineException} will be thrown.
219+
*/
220+
public long ifSeqNo() {
221+
return ifSeqNo;
208222
}
209223

210-
public long ifPrimaryTermMatch() {
211-
return ifPrimaryTermMatch;
224+
/**
225+
* If set, only perform this delete request if the document was last modification was assigned this primary term.
226+
*
227+
* If the document last modification was assigned a different term a
228+
* {@link org.elasticsearch.index.engine.VersionConflictEngineException} will be thrown.
229+
*/
230+
public long ifPrimaryTerm() {
231+
return ifPrimaryTerm;
212232
}
213233

214-
public DeleteRequest setIfMatch(long seqNo, long term) {
215-
if (term == 0 && seqNo != SequenceNumbers.UNASSIGNED_SEQ_NO) {
216-
throw new IllegalArgumentException("seqNo is set, but primary term is [0]");
217-
}
218-
if (term != 0 && seqNo == SequenceNumbers.UNASSIGNED_SEQ_NO) {
219-
throw new IllegalArgumentException("seqNo is unassigned, but primary term is [" + term + "]");
220-
}
234+
/**
235+
* only perform this delete request if the document was last modification was assigned the given
236+
* sequence number. Must be used in combination with {@link #setIfPrimaryTerm(long)}
237+
*
238+
* If the document last modification was assigned a different sequence number a
239+
* {@link org.elasticsearch.index.engine.VersionConflictEngineException} will be thrown.
240+
*/
241+
public DeleteRequest setIfSeqNo(long seqNo) {
221242
if (seqNo < 0 && seqNo != SequenceNumbers.UNASSIGNED_SEQ_NO) {
222243
throw new IllegalArgumentException("sequence numbers must be non negative. got [" + seqNo + "].");
223244
}
245+
ifSeqNo = seqNo;
246+
return this;
247+
}
248+
249+
/**
250+
* only perform this delete request if the document was last modification was assigned the given
251+
* primary term. Must be used in combination with {@link #setIfSeqNo(long)}
252+
*
253+
* If the document last modification was assigned a different primary term a
254+
* {@link org.elasticsearch.index.engine.VersionConflictEngineException} will be thrown.
255+
*/
256+
public DeleteRequest setIfPrimaryTerm(long term) {
224257
if (term < 0) {
225258
throw new IllegalArgumentException("primary term must be non negative. got [" + term + "]");
226259
}
227-
ifSeqNoMatch = seqNo;
228-
ifPrimaryTermMatch = term;
260+
ifPrimaryTerm = term;
229261
return this;
230262
}
231263

@@ -251,11 +283,11 @@ public void readFrom(StreamInput in) throws IOException {
251283
version = in.readLong();
252284
versionType = VersionType.fromValue(in.readByte());
253285
if (in.getVersion().onOrAfter(Version.V_7_0_0)) {
254-
ifSeqNoMatch = in.readZLong();
255-
ifPrimaryTermMatch = in.readVLong();
286+
ifSeqNo = in.readZLong();
287+
ifPrimaryTerm = in.readVLong();
256288
} else {
257-
ifSeqNoMatch = SequenceNumbers.UNASSIGNED_SEQ_NO;
258-
ifPrimaryTermMatch = 0;
289+
ifSeqNo = SequenceNumbers.UNASSIGNED_SEQ_NO;
290+
ifPrimaryTerm = 0;
259291
}
260292
}
261293

@@ -271,10 +303,10 @@ public void writeTo(StreamOutput out) throws IOException {
271303
out.writeLong(version);
272304
out.writeByte(versionType.getValue());
273305
if (out.getVersion().onOrAfter(Version.V_7_0_0)) {
274-
out.writeZLong(ifSeqNoMatch);
275-
out.writeVLong(ifPrimaryTermMatch);
276-
} else if (ifSeqNoMatch != SequenceNumbers.UNASSIGNED_SEQ_NO || ifPrimaryTermMatch != 0) {
277-
assert false : "setIfMatch [" + ifSeqNoMatch + "], currentDocTem [" + ifPrimaryTermMatch + "]";
306+
out.writeZLong(ifSeqNo);
307+
out.writeVLong(ifPrimaryTerm);
308+
} else if (ifSeqNo != SequenceNumbers.UNASSIGNED_SEQ_NO || ifPrimaryTerm != 0) {
309+
assert false : "setIfMatch [" + ifSeqNo + "], currentDocTem [" + ifPrimaryTerm + "]";
278310
throw new IllegalStateException(
279311
"sequence number based compare and write is not supported until all nodes are on version 7.0 or higher. " +
280312
"Stream version [" + out.getVersion() + "]");

server/src/main/java/org/elasticsearch/action/delete/DeleteRequestBuilder.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,26 @@ public DeleteRequestBuilder setVersionType(VersionType versionType) {
8282
}
8383

8484
/**
85-
* only performs this delete request if the document was last modification was assigned the given
86-
* sequence number and primary term
85+
* only perform this delete request if the document was last modification was assigned the given
86+
* sequence number. Must be used in combination with {@link #setIfPrimaryTerm(long)}
87+
*
88+
* If the document last modification was assigned a different sequence number a
89+
* {@link org.elasticsearch.index.engine.VersionConflictEngineException} will be thrown.
8790
*/
88-
public DeleteRequestBuilder setIfMatch(long seqNo, long term) {
89-
request.setIfMatch(seqNo, term);
91+
public DeleteRequestBuilder setIfSeqNo(long seqNo) {
92+
request.setIfSeqNo(seqNo);
93+
return this;
94+
}
95+
96+
/**
97+
* only perform this delete request if the document was last modification was assigned the given
98+
* primary term. Must be used in combination with {@link #setIfSeqNo(long)}
99+
*
100+
* If the document last modification was assigned a different term a
101+
* {@link org.elasticsearch.index.engine.VersionConflictEngineException} will be thrown.
102+
*/
103+
public DeleteRequestBuilder setIfPrimaryTerm(long term) {
104+
request.setIfPrimaryTerm(term);
90105
return this;
91106
}
92107

0 commit comments

Comments
 (0)