|
8 | 8 |
|
9 | 9 | package org.opensearch.index.shard; |
10 | 10 |
|
| 11 | +import org.opensearch.action.delete.DeleteRequest; |
| 12 | +import org.opensearch.action.index.IndexRequest; |
11 | 13 | import org.opensearch.cluster.metadata.IndexMetadata; |
12 | 14 | import org.opensearch.common.settings.Settings; |
13 | 15 | import org.opensearch.common.unit.TimeValue; |
| 16 | +import org.opensearch.common.xcontent.XContentType; |
14 | 17 | import org.opensearch.index.IndexSettings; |
| 18 | +import org.opensearch.index.engine.DocIdSeqNoAndSource; |
15 | 19 | import org.opensearch.index.engine.NRTReplicationEngineFactory; |
| 20 | +import org.opensearch.index.mapper.MapperService; |
16 | 21 | import org.opensearch.index.replication.OpenSearchIndexLevelReplicationTestCase; |
17 | 22 | import org.opensearch.indices.replication.checkpoint.SegmentReplicationCheckpointPublisher; |
18 | 23 | import org.opensearch.indices.replication.checkpoint.ReplicationCheckpoint; |
19 | 24 | import org.opensearch.indices.replication.common.ReplicationType; |
20 | 25 |
|
21 | 26 | import java.io.IOException; |
| 27 | +import java.util.List; |
22 | 28 |
|
23 | 29 | import static org.mockito.ArgumentMatchers.any; |
24 | 30 | import static org.mockito.Mockito.verify; |
@@ -52,6 +58,54 @@ public void testReplicationCheckpointNotNullForSegReb() throws IOException { |
52 | 58 | closeShards(indexShard); |
53 | 59 | } |
54 | 60 |
|
| 61 | + public void testSegmentReplication_Index_Update_Delete() throws Exception { |
| 62 | + String mappings = "{ \"" + MapperService.SINGLE_MAPPING_NAME + "\": { \"properties\": { \"foo\": { \"type\": \"keyword\"} }}}"; |
| 63 | + try (ReplicationGroup shards = createGroup(2, settings, mappings, new NRTReplicationEngineFactory())) { |
| 64 | + shards.startAll(); |
| 65 | + final IndexShard primaryShard = shards.getPrimary(); |
| 66 | + |
| 67 | + final int numDocs = randomIntBetween(100, 200); |
| 68 | + for (int i = 0; i < numDocs; i++) { |
| 69 | + shards.index(new IndexRequest(index.getName()).id(String.valueOf(i)).source("{\"foo\": \"bar\"}", XContentType.JSON)); |
| 70 | + } |
| 71 | + |
| 72 | + primaryShard.refresh("Test"); |
| 73 | + replicateSegments(primaryShard, shards.getReplicas()); |
| 74 | + |
| 75 | + shards.assertAllEqual(numDocs); |
| 76 | + |
| 77 | + for (int i = 0; i < numDocs; i++) { |
| 78 | + // randomly update docs. |
| 79 | + if (randomBoolean()) { |
| 80 | + shards.index( |
| 81 | + new IndexRequest(index.getName()).id(String.valueOf(i)).source("{ \"foo\" : \"baz\" }", XContentType.JSON) |
| 82 | + ); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + primaryShard.refresh("Test"); |
| 87 | + replicateSegments(primaryShard, shards.getReplicas()); |
| 88 | + shards.assertAllEqual(numDocs); |
| 89 | + |
| 90 | + final List<DocIdSeqNoAndSource> docs = getDocIdAndSeqNos(primaryShard); |
| 91 | + for (IndexShard shard : shards.getReplicas()) { |
| 92 | + assertEquals(getDocIdAndSeqNos(shard), docs); |
| 93 | + } |
| 94 | + for (int i = 0; i < numDocs; i++) { |
| 95 | + // randomly delete. |
| 96 | + if (randomBoolean()) { |
| 97 | + shards.delete(new DeleteRequest(index.getName()).id(String.valueOf(i))); |
| 98 | + } |
| 99 | + } |
| 100 | + primaryShard.refresh("Test"); |
| 101 | + replicateSegments(primaryShard, shards.getReplicas()); |
| 102 | + final List<DocIdSeqNoAndSource> docsAfterDelete = getDocIdAndSeqNos(primaryShard); |
| 103 | + for (IndexShard shard : shards.getReplicas()) { |
| 104 | + assertEquals(getDocIdAndSeqNos(shard), docsAfterDelete); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
55 | 109 | public void testIgnoreShardIdle() throws Exception { |
56 | 110 | try (ReplicationGroup shards = createGroup(1, settings, new NRTReplicationEngineFactory())) { |
57 | 111 | shards.startAll(); |
|
0 commit comments