Skip to content
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

Use Sequence number powered OCC for processing updates #37308

Merged
merged 1 commit into from
Jan 11, 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 @@ -163,19 +163,6 @@ Result prepareUpsert(ShardId shardId, UpdateRequest request, final GetResult get
return new Result(indexRequest, DocWriteResponse.Result.CREATED, null, null);
}

/**
* Calculate the version to use for the update request, using either the existing version if internal versioning is used, or the get
* result document's version if the version type is "FORCE".
*/
static long calculateUpdateVersion(UpdateRequest request, GetResult getResult) {
if (request.versionType() != VersionType.INTERNAL) {
assert request.versionType() == VersionType.FORCE;
return request.version(); // remember, match_any is excluded by the conflict test
} else {
return getResult.getVersion();
}
}

/**
* Calculate a routing value to be used, either the included index request's routing, or retrieved document's routing when defined.
*/
Expand All @@ -195,7 +182,6 @@ static String calculateRouting(GetResult getResult, @Nullable IndexRequest updat
* containing a new {@code IndexRequest} to be executed on the primary and replicas.
*/
Result prepareUpdateIndexRequest(ShardId shardId, UpdateRequest request, GetResult getResult, boolean detectNoop) {
final long updateVersion = calculateUpdateVersion(request, getResult);
final IndexRequest currentRequest = request.doc();
final String routing = calculateRouting(getResult, currentRequest);
final Tuple<XContentType, Map<String, Object>> sourceAndContent = XContentHelper.convertToMap(getResult.internalSourceRef(), true);
Expand All @@ -215,7 +201,8 @@ Result prepareUpdateIndexRequest(ShardId shardId, UpdateRequest request, GetResu
} else {
final IndexRequest finalIndexRequest = Requests.indexRequest(request.index())
.type(request.type()).id(request.id()).routing(routing)
.source(updatedSourceAsMap, updateSourceContentType).version(updateVersion).versionType(request.versionType())
.source(updatedSourceAsMap, updateSourceContentType)
.setIfSeqNo(getResult.getSeqNo()).setIfPrimaryTerm(getResult.getPrimaryTerm())
.waitForActiveShards(request.waitForActiveShards()).timeout(request.timeout())
.setRefreshPolicy(request.getRefreshPolicy());
return new Result(finalIndexRequest, DocWriteResponse.Result.UPDATED, updatedSourceAsMap, updateSourceContentType);
Expand All @@ -228,7 +215,6 @@ Result prepareUpdateIndexRequest(ShardId shardId, UpdateRequest request, GetResu
* primary and replicas.
*/
Result prepareUpdateScriptRequest(ShardId shardId, UpdateRequest request, GetResult getResult, LongSupplier nowInMillis) {
final long updateVersion = calculateUpdateVersion(request, getResult);
final IndexRequest currentRequest = request.doc();
final String routing = calculateRouting(getResult, currentRequest);
final Tuple<XContentType, Map<String, Object>> sourceAndContent = XContentHelper.convertToMap(getResult.internalSourceRef(), true);
Expand Down Expand Up @@ -256,14 +242,16 @@ Result prepareUpdateScriptRequest(ShardId shardId, UpdateRequest request, GetRes
case INDEX:
final IndexRequest indexRequest = Requests.indexRequest(request.index())
.type(request.type()).id(request.id()).routing(routing)
.source(updatedSourceAsMap, updateSourceContentType).version(updateVersion).versionType(request.versionType())
.source(updatedSourceAsMap, updateSourceContentType)
.setIfSeqNo(getResult.getSeqNo()).setIfPrimaryTerm(getResult.getPrimaryTerm())
.waitForActiveShards(request.waitForActiveShards()).timeout(request.timeout())
.setRefreshPolicy(request.getRefreshPolicy());
return new Result(indexRequest, DocWriteResponse.Result.UPDATED, updatedSourceAsMap, updateSourceContentType);
case DELETE:
DeleteRequest deleteRequest = Requests.deleteRequest(request.index())
.type(request.type()).id(request.id()).routing(routing)
.version(updateVersion).versionType(request.versionType()).waitForActiveShards(request.waitForActiveShards())
.setIfSeqNo(getResult.getSeqNo()).setIfPrimaryTerm(getResult.getPrimaryTerm())
.waitForActiveShards(request.waitForActiveShards())
.timeout(request.timeout()).setRefreshPolicy(request.getRefreshPolicy());
return new Result(deleteRequest, DocWriteResponse.Result.DELETED, updatedSourceAsMap, updateSourceContentType);
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.env.Environment;
import org.elasticsearch.index.VersionType;
import org.elasticsearch.index.get.GetResult;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.script.MockScriptEngine;
Expand Down Expand Up @@ -561,24 +560,6 @@ public void testRoutingExtraction() throws Exception {
assertThat(UpdateHelper.calculateRouting(getResult, indexRequest), equalTo("routing1"));
}

@SuppressWarnings("deprecated") // VersionType.FORCE is deprecated
public void testCalculateUpdateVersion() throws Exception {
long randomVersion = randomIntBetween(0, 100);
GetResult getResult = new GetResult("test", "type", "1", 0, 1, randomVersion, true, new BytesArray("{}"), null);

UpdateRequest request = new UpdateRequest("test", "type1", "1");
long version = UpdateHelper.calculateUpdateVersion(request, getResult);

// Use the get result's version
assertThat(version, equalTo(randomVersion));

request = new UpdateRequest("test", "type1", "1").versionType(VersionType.FORCE).version(1337);
version = UpdateHelper.calculateUpdateVersion(request, getResult);

// Use the forced update request version
assertThat(version, equalTo(1337L));
}

public void testNoopDetection() throws Exception {
ShardId shardId = new ShardId("test", "", 0);
GetResult getResult = new GetResult("test", "type", "1", 0, 1, 0, true,
Expand Down