|
42 | 42 | import org.elasticsearch.index.VersionType;
|
43 | 43 | import org.elasticsearch.index.mapper.SourceToParse;
|
44 | 44 | import org.elasticsearch.index.seqno.SequenceNumbers;
|
| 45 | +import org.elasticsearch.index.translog.TestTranslog; |
| 46 | +import org.elasticsearch.index.translog.Translog; |
45 | 47 | import org.elasticsearch.tasks.Task;
|
46 | 48 | import org.elasticsearch.tasks.TaskManager;
|
47 | 49 |
|
48 | 50 | import java.io.IOException;
|
49 | 51 | import java.nio.ByteBuffer;
|
50 | 52 | import java.util.ArrayList;
|
| 53 | +import java.util.Arrays; |
51 | 54 | import java.util.Collections;
|
52 | 55 | import java.util.List;
|
53 | 56 | import java.util.concurrent.CountDownLatch;
|
54 | 57 | import java.util.concurrent.atomic.AtomicBoolean;
|
| 58 | +import java.util.stream.Collectors; |
55 | 59 |
|
56 | 60 | import static org.hamcrest.Matchers.containsString;
|
57 | 61 | import static org.hamcrest.Matchers.equalTo;
|
58 | 62 | import static org.hamcrest.Matchers.is;
|
59 | 63 | import static org.hamcrest.core.IsInstanceOf.instanceOf;
|
| 64 | +import static org.mockito.Matchers.anyLong; |
| 65 | +import static org.mockito.Matchers.anyString; |
| 66 | +import static org.mockito.Mockito.doReturn; |
| 67 | +import static org.mockito.Mockito.spy; |
| 68 | +import static org.mockito.Mockito.when; |
60 | 69 |
|
61 | 70 | public class PrimaryReplicaSyncerTests extends IndexShardTestCase {
|
62 | 71 |
|
@@ -186,6 +195,31 @@ public void onResponse(PrimaryReplicaSyncer.ResyncTask result) {
|
186 | 195 | }
|
187 | 196 | }
|
188 | 197 |
|
| 198 | + public void testDoNotSendOperationsWithoutSequenceNumber() throws Exception { |
| 199 | + IndexShard shard = spy(newStartedShard(true)); |
| 200 | + when(shard.getGlobalCheckpoint()).thenReturn(SequenceNumbers.UNASSIGNED_SEQ_NO); |
| 201 | + int numOps = between(0, 20); |
| 202 | + List<Translog.Operation> operations = new ArrayList<>(); |
| 203 | + for (int i = 0; i < numOps; i++) { |
| 204 | + operations.add(new Translog.Index( |
| 205 | + "_doc", Integer.toString(i), randomBoolean() ? SequenceNumbers.UNASSIGNED_SEQ_NO : i, primaryTerm, new byte[]{1})); |
| 206 | + } |
| 207 | + doReturn(TestTranslog.newSnapshotFromOperations(operations)).when(shard).getHistoryOperations(anyString(), anyLong()); |
| 208 | + TaskManager taskManager = new TaskManager(Settings.EMPTY, threadPool, Collections.emptySet()); |
| 209 | + List<Translog.Operation> sentOperations = new ArrayList<>(); |
| 210 | + PrimaryReplicaSyncer.SyncAction syncAction = (request, parentTask, allocationId, primaryTerm, listener) -> { |
| 211 | + sentOperations.addAll(Arrays.asList(request.getOperations())); |
| 212 | + listener.onResponse(new ResyncReplicationResponse()); |
| 213 | + }; |
| 214 | + PrimaryReplicaSyncer syncer = new PrimaryReplicaSyncer(taskManager, syncAction); |
| 215 | + syncer.setChunkSize(new ByteSizeValue(randomIntBetween(1, 10))); |
| 216 | + PlainActionFuture<PrimaryReplicaSyncer.ResyncTask> fut = new PlainActionFuture<>(); |
| 217 | + syncer.resync(shard, fut); |
| 218 | + fut.actionGet(); |
| 219 | + assertThat(sentOperations, equalTo(operations.stream().filter(op -> op.seqNo() >= 0).collect(Collectors.toList()))); |
| 220 | + closeShards(shard); |
| 221 | + } |
| 222 | + |
189 | 223 | public void testStatusSerialization() throws IOException {
|
190 | 224 | PrimaryReplicaSyncer.ResyncTask.Status status = new PrimaryReplicaSyncer.ResyncTask.Status(randomAlphaOfLength(10),
|
191 | 225 | randomIntBetween(0, 1000), randomIntBetween(0, 1000), randomIntBetween(0, 1000));
|
|
0 commit comments