|
|
@@ -20,17 +20,21 @@ package org.elasticsearch.index.shard;
|
|
|
|
|
|
import org.elasticsearch.Version;
|
|
|
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
|
|
-import org.elasticsearch.common.lucene.uid.Versions;
|
|
|
import org.elasticsearch.common.settings.Settings;
|
|
|
import org.elasticsearch.common.xcontent.XContentType;
|
|
|
import org.elasticsearch.index.VersionType;
|
|
|
import org.elasticsearch.index.engine.Engine;
|
|
|
+import org.elasticsearch.index.engine.VersionConflictEngineException;
|
|
|
import org.elasticsearch.index.get.GetResult;
|
|
|
import org.elasticsearch.index.mapper.RoutingFieldMapper;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
|
+import static org.elasticsearch.common.lucene.uid.Versions.MATCH_ANY;
|
|
|
+import static org.elasticsearch.index.seqno.SequenceNumbers.UNASSIGNED_PRIMARY_TERM;
|
|
|
+import static org.elasticsearch.index.seqno.SequenceNumbers.UNASSIGNED_SEQ_NO;
|
|
|
+
|
|
|
public class ShardGetServiceTests extends IndexShardTestCase {
|
|
|
|
|
|
public void testGetForUpdate() throws IOException {
|
|
|
@@ -47,7 +51,8 @@ public class ShardGetServiceTests extends IndexShardTestCase {
|
|
|
recoverShardFromStore(primary);
|
|
|
Engine.IndexResult test = indexDoc(primary, "test", "0", "{\"foo\" : \"bar\"}");
|
|
|
assertTrue(primary.getEngine().refreshNeeded());
|
|
|
- GetResult testGet = primary.getService().getForUpdate("test", "0", test.getVersion(), VersionType.INTERNAL);
|
|
|
+ GetResult testGet = primary.getService().getForUpdate(
|
|
|
+ "test", "0", test.getVersion(), VersionType.INTERNAL, UNASSIGNED_SEQ_NO, UNASSIGNED_PRIMARY_TERM);
|
|
|
assertFalse(testGet.getFields().containsKey(RoutingFieldMapper.NAME));
|
|
|
assertEquals(new String(testGet.source(), StandardCharsets.UTF_8), "{\"foo\" : \"bar\"}");
|
|
|
try (Engine.Searcher searcher = primary.getEngine().acquireSearcher("test", Engine.SearcherScope.INTERNAL)) {
|
|
|
@@ -56,7 +61,8 @@ public class ShardGetServiceTests extends IndexShardTestCase {
|
|
|
|
|
|
Engine.IndexResult test1 = indexDoc(primary, "test", "1", "{\"foo\" : \"baz\"}", XContentType.JSON, "foobar");
|
|
|
assertTrue(primary.getEngine().refreshNeeded());
|
|
|
- GetResult testGet1 = primary.getService().getForUpdate("test", "1", test1.getVersion(), VersionType.INTERNAL);
|
|
|
+ GetResult testGet1 = primary.getService().getForUpdate(
|
|
|
+ "test", "1", test1.getVersion(), VersionType.INTERNAL, UNASSIGNED_SEQ_NO, UNASSIGNED_PRIMARY_TERM);
|
|
|
assertEquals(new String(testGet1.source(), StandardCharsets.UTF_8), "{\"foo\" : \"baz\"}");
|
|
|
assertTrue(testGet1.getFields().containsKey(RoutingFieldMapper.NAME));
|
|
|
assertEquals("foobar", testGet1.getFields().get(RoutingFieldMapper.NAME).getValue());
|
|
|
@@ -69,13 +75,22 @@ public class ShardGetServiceTests extends IndexShardTestCase {
|
|
|
}
|
|
|
|
|
|
// now again from the reader
|
|
|
- test1 = indexDoc(primary, "test", "1", "{\"foo\" : \"baz\"}", XContentType.JSON, "foobar");
|
|
|
+ Engine.IndexResult test2 = indexDoc(primary, "test", "1", "{\"foo\" : \"baz\"}", XContentType.JSON, "foobar");
|
|
|
assertTrue(primary.getEngine().refreshNeeded());
|
|
|
- testGet1 = primary.getService().getForUpdate("test", "1", test1.getVersion(), VersionType.INTERNAL);
|
|
|
+ testGet1 = primary.getService().getForUpdate("test", "1", test2.getVersion(), VersionType.INTERNAL,
|
|
|
+ UNASSIGNED_SEQ_NO, UNASSIGNED_PRIMARY_TERM);
|
|
|
assertEquals(new String(testGet1.source(), StandardCharsets.UTF_8), "{\"foo\" : \"baz\"}");
|
|
|
assertTrue(testGet1.getFields().containsKey(RoutingFieldMapper.NAME));
|
|
|
assertEquals("foobar", testGet1.getFields().get(RoutingFieldMapper.NAME).getValue());
|
|
|
|
|
|
+ final long primaryTerm = primary.operationPrimaryTerm;
|
|
|
+ testGet1 = primary.getService().getForUpdate("test", "1", MATCH_ANY, VersionType.INTERNAL, test2.getSeqNo(), primaryTerm);
|
|
|
+ assertEquals(new String(testGet1.source(), StandardCharsets.UTF_8), "{\"foo\" : \"baz\"}");
|
|
|
+
|
|
|
+ expectThrows(VersionConflictEngineException.class, () ->
|
|
|
+ primary.getService().getForUpdate("test", "1", MATCH_ANY, VersionType.INTERNAL, test2.getSeqNo() + 1, primaryTerm));
|
|
|
+ expectThrows(VersionConflictEngineException.class, () ->
|
|
|
+ primary.getService().getForUpdate("test", "1", MATCH_ANY, VersionType.INTERNAL, test2.getSeqNo(), primaryTerm + 1));
|
|
|
closeShards(primary);
|
|
|
}
|
|
|
|
|
|
@@ -93,13 +108,16 @@ public class ShardGetServiceTests extends IndexShardTestCase {
|
|
|
Engine.IndexResult indexResult = indexDoc(shard, "some_type", "0", "{\"foo\" : \"bar\"}");
|
|
|
assertTrue(indexResult.isCreated());
|
|
|
|
|
|
- GetResult getResult = shard.getService().getForUpdate("some_type", "0", Versions.MATCH_ANY, VersionType.INTERNAL);
|
|
|
+ GetResult getResult = shard.getService().getForUpdate(
|
|
|
+ "some_type", "0", MATCH_ANY, VersionType.INTERNAL, UNASSIGNED_SEQ_NO, UNASSIGNED_PRIMARY_TERM);
|
|
|
assertTrue(getResult.isExists());
|
|
|
|
|
|
- getResult = shard.getService().getForUpdate("some_other_type", "0", Versions.MATCH_ANY, VersionType.INTERNAL);
|
|
|
+ getResult = shard.getService().getForUpdate(
|
|
|
+ "some_other_type", "0", MATCH_ANY, VersionType.INTERNAL, UNASSIGNED_SEQ_NO, UNASSIGNED_PRIMARY_TERM);
|
|
|
assertFalse(getResult.isExists());
|
|
|
|
|
|
- getResult = shard.getService().getForUpdate("_doc", "0", Versions.MATCH_ANY, VersionType.INTERNAL);
|
|
|
+ getResult = shard.getService().getForUpdate(
|
|
|
+ "_doc", "0", MATCH_ANY, VersionType.INTERNAL, UNASSIGNED_SEQ_NO, UNASSIGNED_PRIMARY_TERM);
|
|
|
assertTrue(getResult.isExists());
|
|
|
|
|
|
closeShards(shard);
|