Search in sources :

Example 1 with ResyncReplicationRequest

use of org.opensearch.action.resync.ResyncReplicationRequest in project OpenSearch by opensearch-project.

the class PrimaryReplicaSyncerTests method testSyncerSendsOffCorrectDocuments.

public void testSyncerSendsOffCorrectDocuments() throws Exception {
    IndexShard shard = newStartedShard(true);
    TaskManager taskManager = new TaskManager(Settings.EMPTY, threadPool, Collections.emptySet());
    AtomicBoolean syncActionCalled = new AtomicBoolean();
    List<ResyncReplicationRequest> resyncRequests = new ArrayList<>();
    PrimaryReplicaSyncer.SyncAction syncAction = (request, parentTask, allocationId, primaryTerm, listener) -> {
        logger.info("Sending off {} operations", request.getOperations().length);
        syncActionCalled.set(true);
        resyncRequests.add(request);
        assertThat(parentTask, instanceOf(PrimaryReplicaSyncer.ResyncTask.class));
        listener.onResponse(new ResyncReplicationResponse());
    };
    PrimaryReplicaSyncer syncer = new PrimaryReplicaSyncer(taskManager, syncAction);
    syncer.setChunkSize(new ByteSizeValue(randomIntBetween(1, 10)));
    int numDocs = randomInt(10);
    for (int i = 0; i < numDocs; i++) {
        // Index doc but not advance local checkpoint.
        shard.applyIndexOperationOnPrimary(Versions.MATCH_ANY, VersionType.INTERNAL, new SourceToParse(shard.shardId().getIndexName(), "_doc", Integer.toString(i), new BytesArray("{}"), XContentType.JSON), SequenceNumbers.UNASSIGNED_SEQ_NO, 0, IndexRequest.UNSET_AUTO_GENERATED_TIMESTAMP, true);
    }
    long globalCheckPoint = numDocs > 0 ? randomIntBetween(0, numDocs - 1) : 0;
    boolean syncNeeded = numDocs > 0;
    String allocationId = shard.routingEntry().allocationId().getId();
    shard.updateShardState(shard.routingEntry(), shard.getPendingPrimaryTerm(), null, 1000L, Collections.singleton(allocationId), new IndexShardRoutingTable.Builder(shard.shardId()).addShard(shard.routingEntry()).build());
    shard.updateLocalCheckpointForShard(allocationId, globalCheckPoint);
    assertEquals(globalCheckPoint, shard.getLastKnownGlobalCheckpoint());
    logger.info("Total ops: {}, global checkpoint: {}", numDocs, globalCheckPoint);
    PlainActionFuture<PrimaryReplicaSyncer.ResyncTask> fut = new PlainActionFuture<>();
    syncer.resync(shard, fut);
    PrimaryReplicaSyncer.ResyncTask resyncTask = fut.get();
    if (syncNeeded) {
        assertTrue("Sync action was not called", syncActionCalled.get());
        ResyncReplicationRequest resyncRequest = resyncRequests.remove(0);
        assertThat(resyncRequest.getTrimAboveSeqNo(), equalTo(numDocs - 1L));
        assertThat("trimAboveSeqNo has to be specified in request #0 only", resyncRequests.stream().mapToLong(ResyncReplicationRequest::getTrimAboveSeqNo).filter(seqNo -> seqNo != SequenceNumbers.UNASSIGNED_SEQ_NO).findFirst().isPresent(), is(false));
        assertThat(resyncRequest.getMaxSeenAutoIdTimestampOnPrimary(), equalTo(shard.getMaxSeenAutoIdTimestamp()));
    }
    if (syncNeeded && globalCheckPoint < numDocs - 1) {
        assertThat(resyncTask.getSkippedOperations(), equalTo(0));
        assertThat(resyncTask.getResyncedOperations(), equalTo(Math.toIntExact(numDocs - 1 - globalCheckPoint)));
        if (shard.indexSettings.isSoftDeleteEnabled()) {
            assertThat(resyncTask.getTotalOperations(), equalTo(Math.toIntExact(numDocs - 1 - globalCheckPoint)));
        } else {
            assertThat(resyncTask.getTotalOperations(), equalTo(numDocs));
        }
    } else {
        assertThat(resyncTask.getSkippedOperations(), equalTo(0));
        assertThat(resyncTask.getResyncedOperations(), equalTo(0));
        assertThat(resyncTask.getTotalOperations(), equalTo(0));
    }
    closeShards(shard);
}
Also used : ResyncReplicationResponse(org.opensearch.action.resync.ResyncReplicationResponse) SequenceNumbers(org.opensearch.index.seqno.SequenceNumbers) IsInstanceOf.instanceOf(org.hamcrest.core.IsInstanceOf.instanceOf) ToXContent(org.opensearch.common.xcontent.ToXContent) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ByteSizeValue(org.opensearch.common.unit.ByteSizeValue) ByteBufferStreamInput(org.opensearch.common.io.stream.ByteBufferStreamInput) ByteBuffer(java.nio.ByteBuffer) Strings(org.opensearch.common.Strings) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) ArrayList(java.util.ArrayList) SourceToParse(org.opensearch.index.mapper.SourceToParse) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) NetworkModule(org.opensearch.common.network.NetworkModule) XContentFactory(org.opensearch.common.xcontent.XContentFactory) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) StreamInput(org.opensearch.common.io.stream.StreamInput) Versions(org.opensearch.common.lucene.uid.Versions) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) Task(org.opensearch.tasks.Task) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) TaskManager(org.opensearch.tasks.TaskManager) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) VersionType(org.opensearch.index.VersionType) List(java.util.List) BytesArray(org.opensearch.common.bytes.BytesArray) Matchers.equalTo(org.hamcrest.Matchers.equalTo) XContentType(org.opensearch.common.xcontent.XContentType) Matchers.is(org.hamcrest.Matchers.is) ResyncReplicationRequest(org.opensearch.action.resync.ResyncReplicationRequest) IndexRequest(org.opensearch.action.index.IndexRequest) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) BytesArray(org.opensearch.common.bytes.BytesArray) ArrayList(java.util.ArrayList) ByteSizeValue(org.opensearch.common.unit.ByteSizeValue) SourceToParse(org.opensearch.index.mapper.SourceToParse) Matchers.containsString(org.hamcrest.Matchers.containsString) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TaskManager(org.opensearch.tasks.TaskManager) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) ResyncReplicationRequest(org.opensearch.action.resync.ResyncReplicationRequest) ResyncReplicationResponse(org.opensearch.action.resync.ResyncReplicationResponse)

Example 2 with ResyncReplicationRequest

use of org.opensearch.action.resync.ResyncReplicationRequest in project OpenSearch by opensearch-project.

the class OpenSearchIndexLevelReplicationTestCase method executeResyncOnPrimary.

private TransportWriteAction.WritePrimaryResult<ResyncReplicationRequest, ResyncReplicationResponse> executeResyncOnPrimary(IndexShard primary, ResyncReplicationRequest request) {
    final TransportWriteAction.WritePrimaryResult<ResyncReplicationRequest, ResyncReplicationResponse> result = new TransportWriteAction.WritePrimaryResult<>(TransportResyncReplicationAction.performOnPrimary(request), new ResyncReplicationResponse(), null, null, primary, logger);
    TransportWriteActionTestHelper.performPostWriteActions(primary, request, result.location, logger);
    return result;
}
Also used : ResyncReplicationRequest(org.opensearch.action.resync.ResyncReplicationRequest) TransportWriteAction(org.opensearch.action.support.replication.TransportWriteAction) ResyncReplicationResponse(org.opensearch.action.resync.ResyncReplicationResponse)

Aggregations

ResyncReplicationRequest (org.opensearch.action.resync.ResyncReplicationRequest)2 ResyncReplicationResponse (org.opensearch.action.resync.ResyncReplicationResponse)2 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Matchers.equalTo (org.hamcrest.Matchers.equalTo)1 Matchers.is (org.hamcrest.Matchers.is)1 IsInstanceOf.instanceOf (org.hamcrest.core.IsInstanceOf.instanceOf)1 IndexRequest (org.opensearch.action.index.IndexRequest)1 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)1 TransportWriteAction (org.opensearch.action.support.replication.TransportWriteAction)1 IndexShardRoutingTable (org.opensearch.cluster.routing.IndexShardRoutingTable)1 Strings (org.opensearch.common.Strings)1 BytesArray (org.opensearch.common.bytes.BytesArray)1