Search in sources :

Example 66 with IndexShard

use of org.elasticsearch.index.shard.IndexShard in project crate by crate.

the class RecoverySourceHandlerTests method testSendFileChunksConcurrently.

@Test
public void testSendFileChunksConcurrently() throws Exception {
    final IndexShard shard = mock(IndexShard.class);
    when(shard.state()).thenReturn(IndexShardState.STARTED);
    final List<FileChunkResponse> unrepliedChunks = new CopyOnWriteArrayList<>();
    final AtomicInteger sentChunks = new AtomicInteger();
    final TestRecoveryTargetHandler recoveryTarget = new TestRecoveryTargetHandler() {

        final AtomicLong chunkNumberGenerator = new AtomicLong();

        @Override
        public void writeFileChunk(StoreFileMetadata md, long position, BytesReference content, boolean lastChunk, int totalTranslogOps, ActionListener<Void> listener) {
            final long chunkNumber = chunkNumberGenerator.getAndIncrement();
            logger.info("--> write chunk name={} seq={}, position={}", md.name(), chunkNumber, position);
            unrepliedChunks.add(new FileChunkResponse(chunkNumber, listener));
            sentChunks.incrementAndGet();
        }
    };
    final int maxConcurrentChunks = between(1, 8);
    final int chunkSize = between(1, 32);
    final RecoverySourceHandler handler = new RecoverySourceHandler(shard, recoveryTarget, threadPool, getStartRecoveryRequest(), chunkSize, maxConcurrentChunks, between(1, 10));
    Store store = newStore(createTempDir(), false);
    List<StoreFileMetadata> files = generateFiles(store, between(1, 10), () -> between(1, chunkSize * 20));
    int totalChunks = files.stream().mapToInt(md -> ((int) md.length() + chunkSize - 1) / chunkSize).sum();
    PlainActionFuture<Void> sendFilesFuture = new PlainActionFuture<>();
    handler.sendFiles(store, files.toArray(new StoreFileMetadata[0]), () -> 0, sendFilesFuture);
    assertBusy(() -> {
        assertThat(sentChunks.get(), equalTo(Math.min(totalChunks, maxConcurrentChunks)));
        assertThat(unrepliedChunks, hasSize(sentChunks.get()));
    });
    List<FileChunkResponse> ackedChunks = new ArrayList<>();
    while (sentChunks.get() < totalChunks || unrepliedChunks.isEmpty() == false) {
        List<FileChunkResponse> chunksToAck = randomSubsetOf(between(1, unrepliedChunks.size()), unrepliedChunks);
        unrepliedChunks.removeAll(chunksToAck);
        ackedChunks.addAll(chunksToAck);
        ackedChunks.sort(Comparator.comparing(c -> c.chunkNumber));
        int checkpoint = -1;
        for (int i = 0; i < ackedChunks.size(); i++) {
            if (i != ackedChunks.get(i).chunkNumber) {
                break;
            } else {
                checkpoint = i;
            }
        }
        int chunksToSend = Math.min(// limited by the remaining chunks
        totalChunks - sentChunks.get(), // limited by the buffering chunks
        maxConcurrentChunks - (sentChunks.get() - 1 - checkpoint));
        int expectedSentChunks = sentChunks.get() + chunksToSend;
        int expectedUnrepliedChunks = unrepliedChunks.size() + chunksToSend;
        chunksToAck.forEach(c -> c.listener.onResponse(null));
        assertBusy(() -> {
            assertThat(sentChunks.get(), equalTo(expectedSentChunks));
            assertThat(unrepliedChunks, hasSize(expectedUnrepliedChunks));
        });
    }
    sendFilesFuture.actionGet();
    store.close();
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) ShardId(org.elasticsearch.index.shard.ShardId) Versions(org.elasticsearch.common.lucene.uid.Versions) IndexSettingsModule(org.elasticsearch.test.IndexSettingsModule) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Term(org.apache.lucene.index.Term) ParseContext(org.elasticsearch.index.mapper.ParseContext) SeqNoStats(org.elasticsearch.index.seqno.SeqNoStats) Document(org.apache.lucene.document.Document) Mockito.doAnswer(org.mockito.Mockito.doAnswer) IOContext(org.apache.lucene.store.IOContext) Path(java.nio.file.Path) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) UUIDs(org.elasticsearch.common.UUIDs) Set(java.util.Set) StandardCharsets(java.nio.charset.StandardCharsets) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) SeqNoFieldMapper(org.elasticsearch.index.mapper.SeqNoFieldMapper) CountDownLatch(java.util.concurrent.CountDownLatch) ReplicationTracker(org.elasticsearch.index.seqno.ReplicationTracker) IndexOutputOutputStream(org.elasticsearch.common.lucene.store.IndexOutputOutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Mockito.mock(org.mockito.Mockito.mock) IndexCommit(org.apache.lucene.index.IndexCommit) IndexShardRelocatedException(org.elasticsearch.index.shard.IndexShardRelocatedException) ArrayList(java.util.ArrayList) BytesArray(org.elasticsearch.common.bytes.BytesArray) RetentionLease(org.elasticsearch.index.seqno.RetentionLease) RetentionLeases(org.elasticsearch.index.seqno.RetentionLeases) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Store(org.elasticsearch.index.store.Store) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ESTestCase(org.elasticsearch.test.ESTestCase) Before(org.junit.Before) FileSystemUtils(org.elasticsearch.common.io.FileSystemUtils) SequenceNumbers(org.elasticsearch.index.seqno.SequenceNumbers) SetOnce(org.apache.lucene.util.SetOnce) Executor(java.util.concurrent.Executor) IdFieldMapper(org.elasticsearch.index.mapper.IdFieldMapper) IOUtils(io.crate.common.io.IOUtils) IndexShard(org.elasticsearch.index.shard.IndexShard) Test(org.junit.Test) IOException(java.io.IOException) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) AtomicLong(java.util.concurrent.atomic.AtomicLong) CRC32(java.util.zip.CRC32) TextField(org.apache.lucene.document.TextField) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) ActionListener(org.elasticsearch.action.ActionListener) Randomness(org.elasticsearch.common.Randomness) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CancellableThreads(org.elasticsearch.common.util.CancellableThreads) ConcurrentCollections(org.elasticsearch.common.util.concurrent.ConcurrentCollections) RecoveryEngineException(org.elasticsearch.index.engine.RecoveryEngineException) VersionType(org.elasticsearch.index.VersionType) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) StoreFileMetadata(org.elasticsearch.index.store.StoreFileMetadata) Settings(org.elasticsearch.common.settings.Settings) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Directory(org.apache.lucene.store.Directory) After(org.junit.After) ThreadPool(org.elasticsearch.threadpool.ThreadPool) StepListener(org.elasticsearch.action.StepListener) Releasable(org.elasticsearch.common.lease.Releasable) ArgumentMatchers.anyObject(org.mockito.ArgumentMatchers.anyObject) DirectoryReader(org.apache.lucene.index.DirectoryReader) UNASSIGNED_SEQ_NO(org.elasticsearch.index.seqno.SequenceNumbers.UNASSIGNED_SEQ_NO) BytesReference(org.elasticsearch.common.bytes.BytesReference) Engine(org.elasticsearch.index.engine.Engine) List(java.util.List) Version(org.elasticsearch.Version) Matchers.equalTo(org.hamcrest.Matchers.equalTo) TimeValue(io.crate.common.unit.TimeValue) IndexReader(org.apache.lucene.index.IndexReader) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) StringField(org.apache.lucene.document.StringField) PRIMARY(org.elasticsearch.index.engine.Engine.Operation.Origin.PRIMARY) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) HashSet(java.util.HashSet) BaseDirectoryWrapper(org.apache.lucene.store.BaseDirectoryWrapper) IndexSettings(org.elasticsearch.index.IndexSettings) LatchedActionListener(org.elasticsearch.action.LatchedActionListener) IntSupplier(java.util.function.IntSupplier) OutputStream(java.io.OutputStream) Collections.emptyMap(java.util.Collections.emptyMap) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) Numbers(org.elasticsearch.common.Numbers) IndexShardState(org.elasticsearch.index.shard.IndexShardState) CorruptionUtils(org.elasticsearch.test.CorruptionUtils) Uid(org.elasticsearch.index.mapper.Uid) Iterator(java.util.Iterator) Collections.emptySet(java.util.Collections.emptySet) Mockito.when(org.mockito.Mockito.when) VersionUtils(org.elasticsearch.test.VersionUtils) TimeUnit(java.util.concurrent.TimeUnit) ExceptionsHelper(org.elasticsearch.ExceptionsHelper) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) Field(org.apache.lucene.document.Field) Translog(org.elasticsearch.index.translog.Translog) DummyShardLock(org.elasticsearch.test.DummyShardLock) Comparator(java.util.Comparator) Collections(java.util.Collections) IndexShard(org.elasticsearch.index.shard.IndexShard) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Store(org.elasticsearch.index.store.Store) StoreFileMetadata(org.elasticsearch.index.store.StoreFileMetadata) AtomicLong(java.util.concurrent.atomic.AtomicLong) ActionListener(org.elasticsearch.action.ActionListener) LatchedActionListener(org.elasticsearch.action.LatchedActionListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Example 67 with IndexShard

use of org.elasticsearch.index.shard.IndexShard in project crate by crate.

the class RecoverySourceHandlerTests method testThrowExceptionOnPrimaryRelocatedBeforePhase1Started.

@Test
public void testThrowExceptionOnPrimaryRelocatedBeforePhase1Started() throws IOException {
    final RecoverySettings recoverySettings = new RecoverySettings(Settings.EMPTY, service);
    final StartRecoveryRequest request = getStartRecoveryRequest();
    final IndexShard shard = mock(IndexShard.class);
    when(shard.seqNoStats()).thenReturn(mock(SeqNoStats.class));
    when(shard.isRelocatedPrimary()).thenReturn(true);
    when(shard.acquireSafeIndexCommit()).thenReturn(mock(Engine.IndexCommitRef.class));
    doAnswer(invocation -> {
        ((ActionListener<Releasable>) invocation.getArguments()[0]).onResponse(() -> {
        });
        return null;
    }).when(shard).acquirePrimaryOperationPermit(any(), anyString(), anyObject());
    final IndexMetadata.Builder indexMetadata = IndexMetadata.builder("test").settings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, between(0, 5)).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, between(1, 5)).put(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), randomBoolean()).put(IndexMetadata.SETTING_VERSION_CREATED, VersionUtils.randomVersion(random())).put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID(random())));
    if (randomBoolean()) {
        indexMetadata.state(IndexMetadata.State.CLOSE);
    }
    when(shard.indexSettings()).thenReturn(new IndexSettings(indexMetadata.build(), Settings.EMPTY));
    final AtomicBoolean phase1Called = new AtomicBoolean();
    final AtomicBoolean prepareTargetForTranslogCalled = new AtomicBoolean();
    final AtomicBoolean phase2Called = new AtomicBoolean();
    final RecoverySourceHandler handler = new RecoverySourceHandler(shard, mock(RecoveryTargetHandler.class), threadPool, request, Math.toIntExact(recoverySettings.getChunkSize().getBytes()), between(1, 8), between(1, 8)) {

        @Override
        void phase1(IndexCommit snapshot, long startingSeqNo, IntSupplier translogOps, ActionListener<SendFileResult> listener) {
            phase1Called.set(true);
            super.phase1(snapshot, startingSeqNo, translogOps, listener);
        }

        @Override
        void prepareTargetForTranslog(int totalTranslogOps, ActionListener<TimeValue> listener) {
            prepareTargetForTranslogCalled.set(true);
            super.prepareTargetForTranslog(totalTranslogOps, listener);
        }

        @Override
        void phase2(long startingSeqNo, long endingSeqNo, Translog.Snapshot snapshot, long maxSeenAutoIdTimestamp, long maxSeqNoOfUpdatesOrDeletes, RetentionLeases retentionLeases, long mappingVersion, ActionListener<SendSnapshotResult> listener) throws IOException {
            phase2Called.set(true);
            super.phase2(startingSeqNo, endingSeqNo, snapshot, maxSeenAutoIdTimestamp, maxSeqNoOfUpdatesOrDeletes, retentionLeases, mappingVersion, listener);
        }
    };
    PlainActionFuture<RecoveryResponse> future = new PlainActionFuture<>();
    expectThrows(IndexShardRelocatedException.class, () -> {
        handler.recoverToTarget(future);
        future.actionGet();
    });
    assertFalse(phase1Called.get());
    assertFalse(prepareTargetForTranslogCalled.get());
    assertFalse(phase2Called.get());
}
Also used : IntSupplier(java.util.function.IntSupplier) IndexShard(org.elasticsearch.index.shard.IndexShard) IndexSettings(org.elasticsearch.index.IndexSettings) IndexCommit(org.apache.lucene.index.IndexCommit) RetentionLeases(org.elasticsearch.index.seqno.RetentionLeases) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SeqNoStats(org.elasticsearch.index.seqno.SeqNoStats) ActionListener(org.elasticsearch.action.ActionListener) LatchedActionListener(org.elasticsearch.action.LatchedActionListener) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Test(org.junit.Test)

Example 68 with IndexShard

use of org.elasticsearch.index.shard.IndexShard in project crate by crate.

the class RecoverySourceHandlerTests method testCancelRecoveryDuringPhase1.

@Test
public void testCancelRecoveryDuringPhase1() throws Exception {
    Store store = newStore(createTempDir("source"), false);
    IndexShard shard = mock(IndexShard.class);
    when(shard.store()).thenReturn(store);
    Directory dir = store.directory();
    RandomIndexWriter writer = new RandomIndexWriter(random(), dir, newIndexWriterConfig());
    int numDocs = randomIntBetween(10, 100);
    for (int i = 0; i < numDocs; i++) {
        Document document = new Document();
        document.add(new StringField("id", Integer.toString(i), Field.Store.YES));
        document.add(newField("field", randomUnicodeOfCodepointLengthBetween(1, 10), TextField.TYPE_STORED));
        writer.addDocument(document);
    }
    writer.commit();
    writer.close();
    AtomicBoolean wasCancelled = new AtomicBoolean();
    SetOnce<Runnable> cancelRecovery = new SetOnce<>();
    final TestRecoveryTargetHandler recoveryTarget = new TestRecoveryTargetHandler() {

        @Override
        public void receiveFileInfo(List<String> phase1FileNames, List<Long> phase1FileSizes, List<String> phase1ExistingFileNames, List<Long> phase1ExistingFileSizes, int totalTranslogOps, ActionListener<Void> listener) {
            recoveryExecutor.execute(() -> listener.onResponse(null));
            if (randomBoolean()) {
                wasCancelled.set(true);
                cancelRecovery.get().run();
            }
        }

        @Override
        public void writeFileChunk(StoreFileMetadata md, long position, BytesReference content, boolean lastChunk, int totalTranslogOps, ActionListener<Void> listener) {
            recoveryExecutor.execute(() -> listener.onResponse(null));
            if (rarely()) {
                wasCancelled.set(true);
                cancelRecovery.get().run();
            }
        }

        @Override
        public void cleanFiles(int totalTranslogOps, long globalCheckpoint, Store.MetadataSnapshot sourceMetadata, ActionListener<Void> listener) {
            recoveryExecutor.execute(() -> listener.onResponse(null));
            if (randomBoolean()) {
                wasCancelled.set(true);
                cancelRecovery.get().run();
            }
        }
    };
    StartRecoveryRequest startRecoveryRequest = getStartRecoveryRequest();
    final RecoverySourceHandler handler = new RecoverySourceHandler(shard, recoveryTarget, threadPool, startRecoveryRequest, between(1, 16), between(1, 4), between(1, 4)) {

        @Override
        void createRetentionLease(long startingSeqNo, ActionListener<RetentionLease> listener) {
            final String leaseId = ReplicationTracker.getPeerRecoveryRetentionLeaseId(startRecoveryRequest.targetNode().getId());
            listener.onResponse(new RetentionLease(leaseId, startingSeqNo, threadPool.absoluteTimeInMillis(), ReplicationTracker.PEER_RECOVERY_RETENTION_LEASE_SOURCE));
        }
    };
    cancelRecovery.set(() -> handler.cancel("test"));
    final StepListener<RecoverySourceHandler.SendFileResult> phase1Listener = new StepListener<>();
    try {
        final CountDownLatch latch = new CountDownLatch(1);
        handler.phase1(DirectoryReader.listCommits(dir).get(0), 0, () -> 0, new LatchedActionListener<>(phase1Listener, latch));
        latch.await();
        phase1Listener.result();
    } catch (Exception e) {
        assertTrue(wasCancelled.get());
        assertNotNull(ExceptionsHelper.unwrap(e, CancellableThreads.ExecutionCancelledException.class));
    }
    store.close();
}
Also used : Store(org.elasticsearch.index.store.Store) StoreFileMetadata(org.elasticsearch.index.store.StoreFileMetadata) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Document(org.apache.lucene.document.Document) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Directory(org.apache.lucene.store.Directory) BytesReference(org.elasticsearch.common.bytes.BytesReference) CancellableThreads(org.elasticsearch.common.util.CancellableThreads) SetOnce(org.apache.lucene.util.SetOnce) IndexShard(org.elasticsearch.index.shard.IndexShard) CountDownLatch(java.util.concurrent.CountDownLatch) IndexShardRelocatedException(org.elasticsearch.index.shard.IndexShardRelocatedException) IOException(java.io.IOException) RecoveryEngineException(org.elasticsearch.index.engine.RecoveryEngineException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ActionListener(org.elasticsearch.action.ActionListener) LatchedActionListener(org.elasticsearch.action.LatchedActionListener) RetentionLease(org.elasticsearch.index.seqno.RetentionLease) StringField(org.apache.lucene.document.StringField) StepListener(org.elasticsearch.action.StepListener) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Test(org.junit.Test)

Example 69 with IndexShard

use of org.elasticsearch.index.shard.IndexShard in project crate by crate.

the class IndexRecoveryIT method testUsesFileBasedRecoveryIfRetentionLeaseMissing.

@Test
public void testUsesFileBasedRecoveryIfRetentionLeaseMissing() throws Exception {
    internalCluster().ensureAtLeastNumDataNodes(2);
    String indexName = "test";
    execute("CREATE TABLE doc.test (num INT)" + " CLUSTERED INTO 1 SHARDS" + " WITH (" + "  number_of_replicas = 1," + "  \"unassigned.node_left.delayed_timeout\"='12h'," + "  \"soft_deletes.enabled\"=true" + " )");
    int numDocs = randomIntBetween(1, 100);
    var args = new Object[numDocs][];
    for (int i = 0; i < numDocs; i++) {
        args[i] = new Object[] { i };
    }
    execute("INSERT INTO doc.test (num) VALUES (?)", args);
    ensureGreen(indexName);
    final ShardId shardId = new ShardId(resolveIndex(indexName), 0);
    final DiscoveryNodes discoveryNodes = clusterService().state().nodes();
    final IndexShardRoutingTable indexShardRoutingTable = clusterService().state().routingTable().shardRoutingTable(shardId);
    final IndexShard primary = internalCluster().getInstance(IndicesService.class, discoveryNodes.get(indexShardRoutingTable.primaryShard().currentNodeId()).getName()).getShardOrNull(shardId);
    final ShardRouting replicaShardRouting = indexShardRoutingTable.replicaShards().get(0);
    internalCluster().restartNode(discoveryNodes.get(replicaShardRouting.currentNodeId()).getName(), new InternalTestCluster.RestartCallback() {

        @Override
        public Settings onNodeStopped(String nodeName) throws Exception {
            assertFalse(client().admin().cluster().prepareHealth().setWaitForNodes(Integer.toString(discoveryNodes.getSize() - 1)).setWaitForEvents(Priority.LANGUID).get().isTimedOut());
            final PlainActionFuture<ReplicationResponse> future = new PlainActionFuture<>();
            primary.removeRetentionLease(ReplicationTracker.getPeerRecoveryRetentionLeaseId(replicaShardRouting), future);
            future.get();
            return super.onNodeStopped(nodeName);
        }
    });
    ensureGreen(indexName);
    // noinspection OptionalGetWithoutIsPresent because it fails the test if absent
    final var recoveryState = client().execute(RecoveryAction.INSTANCE, new RecoveryRequest()).get().shardRecoveryStates().get(indexName).stream().filter(rs -> rs.getPrimary() == false).findFirst().get();
    assertThat(recoveryState.getIndex().totalFileCount(), greaterThan(0));
}
Also used : IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) IndexShard(org.elasticsearch.index.shard.IndexShard) IndicesService(org.elasticsearch.indices.IndicesService) InternalTestCluster(org.elasticsearch.test.InternalTestCluster) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) IOException(java.io.IOException) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) CircuitBreakingException(org.elasticsearch.common.breaker.CircuitBreakingException) ShardId(org.elasticsearch.index.shard.ShardId) RecoveryRequest(org.elasticsearch.action.admin.indices.recovery.RecoveryRequest) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings) Test(org.junit.Test)

Example 70 with IndexShard

use of org.elasticsearch.index.shard.IndexShard in project crate by crate.

the class PeerRecoveryTargetServiceTests method testResetStartRequestIfTranslogIsCorrupted.

@Test
public void testResetStartRequestIfTranslogIsCorrupted() throws Exception {
    DiscoveryNode pNode = new DiscoveryNode("foo", buildNewFakeTransportAddress(), Collections.emptyMap(), Collections.emptySet(), Version.CURRENT);
    DiscoveryNode rNode = new DiscoveryNode("foo", buildNewFakeTransportAddress(), Collections.emptyMap(), Collections.emptySet(), Version.CURRENT);
    IndexShard shard = newStartedShard(false);
    final SeqNoStats seqNoStats = populateRandomData(shard);
    shard.close("test", false);
    if (randomBoolean()) {
        shard.store().associateIndexWithNewTranslog(UUIDs.randomBase64UUID());
    } else if (randomBoolean()) {
        Translog.createEmptyTranslog(shard.shardPath().resolveTranslog(), seqNoStats.getGlobalCheckpoint(), shard.shardId(), shard.getOperationPrimaryTerm());
    } else {
        IOUtils.rm(shard.shardPath().resolveTranslog());
    }
    shard = reinitShard(shard, ShardRoutingHelper.initWithSameId(shard.routingEntry(), RecoverySource.PeerRecoverySource.INSTANCE));
    shard.markAsRecovering("peer recovery", new RecoveryState(shard.routingEntry(), pNode, rNode));
    shard.prepareForIndexRecovery();
    RecoveryTarget recoveryTarget = new RecoveryTarget(shard, null, null);
    StartRecoveryRequest request = PeerRecoveryTargetService.getStartRecoveryRequest(logger, rNode, recoveryTarget, randomNonNegativeLong());
    assertThat(request.startingSeqNo(), equalTo(UNASSIGNED_SEQ_NO));
    assertThat(request.metadataSnapshot(), sameInstance(Store.MetadataSnapshot.EMPTY));
    recoveryTarget.decRef();
    closeShards(shard);
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) SeqNoStats(org.elasticsearch.index.seqno.SeqNoStats) IndexShard(org.elasticsearch.index.shard.IndexShard) Test(org.junit.Test)

Aggregations

IndexShard (org.elasticsearch.index.shard.IndexShard)173 IndexService (org.elasticsearch.index.IndexService)74 ShardId (org.elasticsearch.index.shard.ShardId)49 IndicesService (org.elasticsearch.indices.IndicesService)47 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)36 Test (org.junit.Test)35 IOException (java.io.IOException)29 Engine (org.elasticsearch.index.engine.Engine)26 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)21 ElasticsearchException (org.elasticsearch.ElasticsearchException)19 CountDownLatch (java.util.concurrent.CountDownLatch)18 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)18 Settings (org.elasticsearch.common.settings.Settings)18 ArrayList (java.util.ArrayList)16 Translog (org.elasticsearch.index.translog.Translog)16 HashMap (java.util.HashMap)15 Index (org.elasticsearch.index.Index)15 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)13 PlainActionFuture (org.elasticsearch.action.support.PlainActionFuture)12 List (java.util.List)11