Skip to content

Return seq_no and primary_term for noop update #44603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC

private static class BulkRestBuilderListener extends RestBuilderListener<BulkRequest> {
private final BulkItemResponse ITEM_RESPONSE = new BulkItemResponse(1, DocWriteRequest.OpType.UPDATE,
new UpdateResponse(new ShardId("mock", "", 1), "mock_type", "1", 1L, DocWriteResponse.Result.CREATED));
new UpdateResponse(new ShardId("mock", "", 1), "mock_type", "1", 0L, 1L, 1L, DocWriteResponse.Result.CREATED));

private final RestRequest request;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

public class TransportNoopBulkAction extends HandledTransportAction<BulkRequest, BulkResponse> {
private static final BulkItemResponse ITEM_RESPONSE = new BulkItemResponse(1, DocWriteRequest.OpType.UPDATE,
new UpdateResponse(new ShardId("mock", "", 1), "mock_type", "1", 1L, DocWriteResponse.Result.CREATED));
new UpdateResponse(new ShardId("mock", "", 1), "mock_type", "1", 0L, 1L, 1L, DocWriteResponse.Result.CREATED));

@Inject
public TransportNoopBulkAction(TransportService transportService, ActionFilters actionFilters) {
Expand Down
2 changes: 2 additions & 0 deletions docs/reference/docs/update.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ the request was ignored.
"_type": "_doc",
"_id": "1",
"_version": 7,
"_primary_term": 1,
"_seq_no": 6,
"result": "noop"
}
--------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -885,8 +885,8 @@ void doExecute(ActionType<Response> action, Request request, ActionListener<Resp
true);
} else if (item instanceof UpdateRequest) {
UpdateRequest update = (UpdateRequest) item;
response = new UpdateResponse(shardId, update.type(), update.id(),
randomIntBetween(0, Integer.MAX_VALUE), Result.CREATED);
response = new UpdateResponse(shardId, update.type(), update.id(), randomNonNegativeLong(),
randomIntBetween(1, Integer.MAX_VALUE), randomIntBetween(0, Integer.MAX_VALUE), Result.CREATED);
} else if (item instanceof DeleteRequest) {
DeleteRequest delete = (DeleteRequest) item;
response =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
"Noop":
- skip:
version: " - 7.9.99"
reason: "Noop does not return seq_no and primary_term until 8.0"
- do:
index:
index: test_1
type: test
id: 1
body: { foo: bar }

- match: { _seq_no: 0 }
- match: { _version: 1 }
- match: { _primary_term: 1 }
- match: { result: created }

- do:
update:
index: test_1
type: test
id: 1
body:
doc: { foo: bar }

- match: { _seq_no: 0 }
- match: { _version: 1 }
- match: { _primary_term: 1 }
- match: { result: noop }

- do:
update:
index: test_1
type: test
id: 1
body:
doc: { foo: bar }
detect_noop: false

- match: { _seq_no: 1 }
- match: { _primary_term: 1 }
- match: { _version: 2 }
- match: { result: updated }
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,8 @@ public abstract static class Builder {
protected Result result = null;
protected boolean forcedRefresh;
protected ShardInfo shardInfo = null;
protected Long seqNo = UNASSIGNED_SEQ_NO;
protected Long primaryTerm = UNASSIGNED_PRIMARY_TERM;
protected long seqNo = UNASSIGNED_SEQ_NO;
protected long primaryTerm = UNASSIGNED_PRIMARY_TERM;

public ShardId getShardId() {
return shardId;
Expand Down Expand Up @@ -411,11 +411,11 @@ public void setShardInfo(ShardInfo shardInfo) {
this.shardInfo = shardInfo;
}

public void setSeqNo(Long seqNo) {
public void setSeqNo(long seqNo) {
this.seqNo = seqNo;
}

public void setPrimaryTerm(Long primaryTerm) {
public void setPrimaryTerm(long primaryTerm) {
this.primaryTerm = primaryTerm;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import org.elasticsearch.index.IndexNotFoundException;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.VersionType;
import org.elasticsearch.index.seqno.SequenceNumbers;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.indices.IndexClosedException;
import org.elasticsearch.ingest.IngestService;
Expand Down Expand Up @@ -681,7 +682,8 @@ void markCurrentItemAsDropped() {
new BulkItemResponse(currentSlot, indexRequest.opType(),
new UpdateResponse(
new ShardId(indexRequest.index(), IndexMetaData.INDEX_UUID_NA_VALUE, 0),
indexRequest.type(), indexRequest.id(), indexRequest.version(), DocWriteResponse.Result.NOOP
indexRequest.type(), indexRequest.id(), SequenceNumbers.UNASSIGNED_SEQ_NO, SequenceNumbers.UNASSIGNED_PRIMARY_TERM,
indexRequest.version(), DocWriteResponse.Result.NOOP
)
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ Result prepareUpsert(ShardId shardId, UpdateRequest request, final GetResult get
break;
case NONE:
UpdateResponse update = new UpdateResponse(shardId, getResult.getType(), getResult.getId(),
getResult.getVersion(), DocWriteResponse.Result.NOOP);
getResult.getSeqNo(), getResult.getPrimaryTerm(), getResult.getVersion(), DocWriteResponse.Result.NOOP);
update.setGetResult(getResult);
return new Result(update, DocWriteResponse.Result.NOOP, upsertResult.v2(), XContentType.JSON);
default:
Expand Down Expand Up @@ -194,7 +194,7 @@ Result prepareUpdateIndexRequest(ShardId shardId, UpdateRequest request, GetResu
// where users repopulating multi-fields or adding synonyms, etc.
if (detectNoop && noop) {
UpdateResponse update = new UpdateResponse(shardId, getResult.getType(), getResult.getId(),
getResult.getVersion(), DocWriteResponse.Result.NOOP);
getResult.getSeqNo(), getResult.getPrimaryTerm(), getResult.getVersion(), DocWriteResponse.Result.NOOP);
update.setGetResult(extractGetResult(request, request.index(), getResult.getSeqNo(), getResult.getPrimaryTerm(),
getResult.getVersion(), updatedSourceAsMap, updateSourceContentType, getResult.internalSourceRef()));
return new Result(update, DocWriteResponse.Result.NOOP, updatedSourceAsMap, updateSourceContentType);
Expand Down Expand Up @@ -257,7 +257,7 @@ Result prepareUpdateScriptRequest(ShardId shardId, UpdateRequest request, GetRes
default:
// If it was neither an INDEX or DELETE operation, treat it as a noop
UpdateResponse update = new UpdateResponse(shardId, getResult.getType(), getResult.getId(),
getResult.getVersion(), DocWriteResponse.Result.NOOP);
getResult.getSeqNo(), getResult.getPrimaryTerm(), getResult.getVersion(), DocWriteResponse.Result.NOOP);
update.setGetResult(extractGetResult(request, request.index(), getResult.getSeqNo(), getResult.getPrimaryTerm(),
getResult.getVersion(), updatedSourceAsMap, updateSourceContentType, getResult.internalSourceRef()));
return new Result(update, DocWriteResponse.Result.NOOP, updatedSourceAsMap, updateSourceContentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.get.GetResult;
import org.elasticsearch.index.seqno.SequenceNumbers;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.rest.RestStatus;

Expand All @@ -50,8 +49,8 @@ public UpdateResponse(StreamInput in) throws IOException {
* Constructor to be used when a update didn't translate in a write.
* For example: update script with operation set to none
*/
public UpdateResponse(ShardId shardId, String type, String id, long version, Result result) {
this(new ShardInfo(0, 0), shardId, type, id, SequenceNumbers.UNASSIGNED_SEQ_NO, 0, version, result);
public UpdateResponse(ShardId shardId, String type, String id, long seqNo, long primaryTerm, long version, Result result) {
this(new ShardInfo(0, 0), shardId, type, id, seqNo, primaryTerm, version, result);
}

public UpdateResponse(
Expand Down Expand Up @@ -152,10 +151,10 @@ public void setGetResult(GetResult getResult) {
@Override
public UpdateResponse build() {
UpdateResponse update;
if (shardInfo != null && seqNo != null) {
if (shardInfo != null) {
update = new UpdateResponse(shardInfo, shardId, type, id, seqNo, primaryTerm, version, result);
} else {
update = new UpdateResponse(shardId, type, id, version, result);
update = new UpdateResponse(shardId, type, id, seqNo, primaryTerm, version, result);
}
if (getResult != null) {
update.setGetResult(new GetResult(update.getIndex(), update.getType(), update.getId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import org.elasticsearch.index.engine.VersionConflictEngineException;
import org.elasticsearch.index.mapper.Mapping;
import org.elasticsearch.index.mapper.MetadataFieldMapper;
import org.elasticsearch.index.seqno.SequenceNumbers;
import org.elasticsearch.index.shard.IndexShard;
import org.elasticsearch.index.shard.IndexShardTestCase;
import org.elasticsearch.index.shard.ShardId;
Expand Down Expand Up @@ -438,8 +437,7 @@ public void testNoopUpdateRequest() throws Exception {
.doc(Requests.INDEX_CONTENT_TYPE, "field", "value");
BulkItemRequest primaryRequest = new BulkItemRequest(0, writeRequest);

DocWriteResponse noopUpdateResponse = new UpdateResponse(shardId, "_doc", "id", 0,
DocWriteResponse.Result.NOOP);
DocWriteResponse noopUpdateResponse = new UpdateResponse(shardId, "_doc", "id", 0, 2, 1, DocWriteResponse.Result.NOOP);

IndexShard shard = mock(IndexShard.class);

Expand Down Expand Up @@ -471,7 +469,7 @@ public void testNoopUpdateRequest() throws Exception {
equalTo(DocWriteResponse.Result.NOOP));
assertThat(bulkShardRequest.items().length, equalTo(1));
assertEquals(primaryRequest, bulkShardRequest.items()[0]); // check that bulk item was not mutated
assertThat(primaryResponse.getResponse().getSeqNo(), equalTo(SequenceNumbers.UNASSIGNED_SEQ_NO));
assertThat(primaryResponse.getResponse().getSeqNo(), equalTo(0L));
}

public void testUpdateRequestWithFailure() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.get.GetResult;
import org.elasticsearch.index.get.GetResultTests;
import org.elasticsearch.index.seqno.SequenceNumbers;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.RandomObjects;
Expand All @@ -54,7 +55,7 @@ public class UpdateResponseTests extends ESTestCase {

public void testToXContent() throws IOException {
{
UpdateResponse updateResponse = new UpdateResponse(new ShardId("index", "index_uuid", 0), "type", "id", 0, NOT_FOUND);
UpdateResponse updateResponse = new UpdateResponse(new ShardId("index", "index_uuid", 0), "type", "id", -2, 0, 0, NOT_FOUND);
String output = Strings.toString(updateResponse);
assertEquals("{\"_index\":\"index\",\"_type\":\"type\",\"_id\":\"id\",\"_version\":0,\"result\":\"not_found\"," +
"\"_shards\":{\"total\":0,\"successful\":0,\"failed\":0}}", output);
Expand Down Expand Up @@ -162,21 +163,21 @@ public static Tuple<UpdateResponse, UpdateResponse> randomUpdateResponse(XConten

// We also want small number values (randomNonNegativeLong() tend to generate high numbers)
// in order to catch some conversion error that happen between int/long after parsing.
Long seqNo = randomFrom(randomNonNegativeLong(), (long) randomIntBetween(0, 10_000), null);
long primaryTerm = seqNo == null ? 0 : randomIntBetween(1, 16);
long seqNo = randomFrom(randomNonNegativeLong(), (long) randomIntBetween(0, 10_000), SequenceNumbers.UNASSIGNED_SEQ_NO);
long primaryTerm = seqNo == SequenceNumbers.UNASSIGNED_SEQ_NO ? SequenceNumbers.UNASSIGNED_PRIMARY_TERM : randomIntBetween(1, 16);

ShardId actualShardId = new ShardId(index, indexUUid, shardId);
ShardId expectedShardId = new ShardId(index, INDEX_UUID_NA_VALUE, -1);

UpdateResponse actual, expected;
if (seqNo != null) {
if (seqNo != SequenceNumbers.UNASSIGNED_SEQ_NO) {
Tuple<ReplicationResponse.ShardInfo, ReplicationResponse.ShardInfo> shardInfos = RandomObjects.randomShardInfo(random());

actual = new UpdateResponse(shardInfos.v1(), actualShardId, type, id, seqNo, primaryTerm, version, result);
expected = new UpdateResponse(shardInfos.v2(), expectedShardId, type, id, seqNo, primaryTerm, version, result);
} else {
actual = new UpdateResponse(actualShardId, type, id, version, result);
expected = new UpdateResponse(expectedShardId, type, id, version, result);
actual = new UpdateResponse(actualShardId, type, id, seqNo, primaryTerm, version, result);
expected = new UpdateResponse(expectedShardId, type, id, seqNo, primaryTerm, version, result);
}

if (actualGetResult.isExists()) {
Expand Down
Loading