Search in sources :

Example 16 with IndexShard

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

the class RecoveryDuringReplicationTests method testRecoveryAfterPrimaryPromotion.

@TestLogging("org.elasticsearch.index.shard:TRACE,org.elasticsearch.indices.recovery:TRACE")
public void testRecoveryAfterPrimaryPromotion() throws Exception {
    try (ReplicationGroup shards = createGroup(2)) {
        shards.startAll();
        int totalDocs = shards.indexDocs(randomInt(10));
        int committedDocs = 0;
        if (randomBoolean()) {
            shards.flush();
            committedDocs = totalDocs;
        }
        // we need some indexing to happen to transfer local checkpoint information to the primary
        // so it can update the global checkpoint and communicate to replicas
        boolean expectSeqNoRecovery = totalDocs > 0;
        final IndexShard oldPrimary = shards.getPrimary();
        final IndexShard newPrimary = shards.getReplicas().get(0);
        final IndexShard replica = shards.getReplicas().get(1);
        if (randomBoolean()) {
            // simulate docs that were inflight when primary failed, these will be rolled back
            final int rollbackDocs = randomIntBetween(1, 5);
            logger.info("--> indexing {} rollback docs", rollbackDocs);
            for (int i = 0; i < rollbackDocs; i++) {
                final IndexRequest indexRequest = new IndexRequest(index.getName(), "type", "rollback_" + i).source("{}", XContentType.JSON);
                final IndexResponse primaryResponse = indexOnPrimary(indexRequest, oldPrimary);
                indexOnReplica(primaryResponse, indexRequest, replica);
            }
            if (randomBoolean()) {
                oldPrimary.flush(new FlushRequest(index.getName()));
                expectSeqNoRecovery = false;
            }
        }
        shards.promoteReplicaToPrimary(newPrimary);
        // index some more
        totalDocs += shards.indexDocs(randomIntBetween(0, 5));
        oldPrimary.close("demoted", false);
        oldPrimary.store().close();
        IndexShard newReplica = shards.addReplicaWithExistingPath(oldPrimary.shardPath(), oldPrimary.routingEntry().currentNodeId());
        shards.recoverReplica(newReplica);
        if (expectSeqNoRecovery) {
            assertThat(newReplica.recoveryState().getIndex().fileDetails(), empty());
            assertThat(newReplica.recoveryState().getTranslog().recoveredOperations(), equalTo(totalDocs - committedDocs));
        } else {
            assertThat(newReplica.recoveryState().getIndex().fileDetails(), not(empty()));
            assertThat(newReplica.recoveryState().getTranslog().recoveredOperations(), equalTo(totalDocs - committedDocs));
        }
        shards.removeReplica(replica);
        replica.close("resync", false);
        replica.store().close();
        newReplica = shards.addReplicaWithExistingPath(replica.shardPath(), replica.routingEntry().currentNodeId());
        shards.recoverReplica(newReplica);
        shards.assertAllEqual(totalDocs);
    }
}
Also used : IndexResponse(org.elasticsearch.action.index.IndexResponse) FlushRequest(org.elasticsearch.action.admin.indices.flush.FlushRequest) IndexShard(org.elasticsearch.index.shard.IndexShard) IndexRequest(org.elasticsearch.action.index.IndexRequest) TestLogging(org.elasticsearch.test.junit.annotations.TestLogging)

Example 17 with IndexShard

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

the class RecoveryDuringReplicationTests method testIndexingDuringFileRecovery.

public void testIndexingDuringFileRecovery() throws Exception {
    try (ReplicationGroup shards = createGroup(randomInt(1))) {
        shards.startAll();
        int docs = shards.indexDocs(randomInt(50));
        shards.flush();
        IndexShard replica = shards.addReplica();
        final CountDownLatch recoveryBlocked = new CountDownLatch(1);
        final CountDownLatch releaseRecovery = new CountDownLatch(1);
        final RecoveryState.Stage blockOnStage = randomFrom(BlockingTarget.SUPPORTED_STAGES);
        final Future<Void> recoveryFuture = shards.asyncRecoverReplica(replica, (indexShard, node) -> new BlockingTarget(blockOnStage, recoveryBlocked, releaseRecovery, indexShard, node, recoveryListener, logger));
        recoveryBlocked.await();
        docs += shards.indexDocs(randomInt(20));
        releaseRecovery.countDown();
        recoveryFuture.get();
        shards.assertAllEqual(docs);
    }
}
Also used : IndexShard(org.elasticsearch.index.shard.IndexShard) CountDownLatch(java.util.concurrent.CountDownLatch) RecoveryState(org.elasticsearch.indices.recovery.RecoveryState)

Example 18 with IndexShard

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

the class CorruptedFileIT method testCorruptFileAndRecover.

/**
     * Tests that we can actually recover from a corruption on the primary given that we have replica shards around.
     */
public void testCorruptFileAndRecover() throws ExecutionException, InterruptedException, IOException {
    int numDocs = scaledRandomIntBetween(100, 1000);
    // have enough space for 3 copies
    internalCluster().ensureAtLeastNumDataNodes(3);
    if (cluster().numDataNodes() == 3) {
        logger.info("--> cluster has [3] data nodes, corrupted primary will be overwritten");
    }
    assertThat(cluster().numDataNodes(), greaterThanOrEqualTo(3));
    assertAcked(prepareCreate("test").setSettings(Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, "1").put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, "1").put(MergePolicyConfig.INDEX_MERGE_ENABLED, false).put(MockFSIndexStore.INDEX_CHECK_INDEX_ON_CLOSE_SETTING.getKey(), // no checkindex - we corrupt shards on purpose
    false).put(IndexSettings.INDEX_TRANSLOG_FLUSH_THRESHOLD_SIZE_SETTING.getKey(), // no translog based flush - it might change the .liv / segments.N files
    new ByteSizeValue(1, ByteSizeUnit.PB))));
    ensureGreen();
    disableAllocation("test");
    IndexRequestBuilder[] builders = new IndexRequestBuilder[numDocs];
    for (int i = 0; i < builders.length; i++) {
        builders[i] = client().prepareIndex("test", "type").setSource("field", "value");
    }
    indexRandom(true, builders);
    ensureGreen();
    assertAllSuccessful(client().admin().indices().prepareFlush().setForce(true).execute().actionGet());
    // we have to flush at least once here since we don't corrupt the translog
    SearchResponse countResponse = client().prepareSearch().setSize(0).get();
    assertHitCount(countResponse, numDocs);
    final int numShards = numShards("test");
    ShardRouting corruptedShardRouting = corruptRandomPrimaryFile();
    logger.info("--> {} corrupted", corruptedShardRouting);
    enableAllocation("test");
    /*
         * we corrupted the primary shard - now lets make sure we never recover from it successfully
         */
    Settings build = Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, "2").build();
    client().admin().indices().prepareUpdateSettings("test").setSettings(build).get();
    ClusterHealthResponse health = client().admin().cluster().health(Requests.clusterHealthRequest("test").waitForGreenStatus().timeout(// sometimes due to cluster rebalacing and random settings default timeout is just not enough.
    "5m").waitForNoRelocatingShards(true)).actionGet();
    if (health.isTimedOut()) {
        logger.info("cluster state:\n{}\n{}", client().admin().cluster().prepareState().get().getState(), client().admin().cluster().preparePendingClusterTasks().get());
        assertThat("timed out waiting for green state", health.isTimedOut(), equalTo(false));
    }
    assertThat(health.getStatus(), equalTo(ClusterHealthStatus.GREEN));
    final int numIterations = scaledRandomIntBetween(5, 20);
    for (int i = 0; i < numIterations; i++) {
        SearchResponse response = client().prepareSearch().setSize(numDocs).get();
        assertHitCount(response, numDocs);
    }
    /*
         * now hook into the IndicesService and register a close listener to
         * run the checkindex. if the corruption is still there we will catch it.
         */
    // primary + 2 replicas
    final CountDownLatch latch = new CountDownLatch(numShards * 3);
    final CopyOnWriteArrayList<Exception> exception = new CopyOnWriteArrayList<>();
    final IndexEventListener listener = new IndexEventListener() {

        @Override
        public void afterIndexShardClosed(ShardId sid, @Nullable IndexShard indexShard, Settings indexSettings) {
            if (indexShard != null) {
                Store store = indexShard.store();
                store.incRef();
                try {
                    if (!Lucene.indexExists(store.directory()) && indexShard.state() == IndexShardState.STARTED) {
                        return;
                    }
                    try (CheckIndex checkIndex = new CheckIndex(store.directory())) {
                        BytesStreamOutput os = new BytesStreamOutput();
                        PrintStream out = new PrintStream(os, false, StandardCharsets.UTF_8.name());
                        checkIndex.setInfoStream(out);
                        out.flush();
                        CheckIndex.Status status = checkIndex.checkIndex();
                        if (!status.clean) {
                            logger.warn("check index [failure]\n{}", os.bytes().utf8ToString());
                            throw new IOException("index check failure");
                        }
                    }
                } catch (Exception e) {
                    exception.add(e);
                } finally {
                    store.decRef();
                    latch.countDown();
                }
            }
        }
    };
    for (MockIndexEventListener.TestEventListener eventListener : internalCluster().getDataNodeInstances(MockIndexEventListener.TestEventListener.class)) {
        eventListener.setNewDelegate(listener);
    }
    try {
        client().admin().indices().prepareDelete("test").get();
        latch.await();
        assertThat(exception, empty());
    } finally {
        for (MockIndexEventListener.TestEventListener eventListener : internalCluster().getDataNodeInstances(MockIndexEventListener.TestEventListener.class)) {
            eventListener.setNewDelegate(null);
        }
    }
}
Also used : MockIndexEventListener(org.elasticsearch.test.MockIndexEventListener) PrintStream(java.io.PrintStream) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) IndexShard(org.elasticsearch.index.shard.IndexShard) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) MockFSIndexStore(org.elasticsearch.test.store.MockFSIndexStore) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) TransportException(org.elasticsearch.transport.TransportException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) SearchResponse(org.elasticsearch.action.search.SearchResponse) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ShardId(org.elasticsearch.index.shard.ShardId) MockIndexEventListener(org.elasticsearch.test.MockIndexEventListener) IndexEventListener(org.elasticsearch.index.shard.IndexEventListener) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings) Nullable(org.elasticsearch.common.Nullable) CheckIndex(org.apache.lucene.index.CheckIndex) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 19 with IndexShard

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

the class GlobalCheckpointSyncActionTests method testTranslogSyncAfterGlobalCheckpointSync.

public void testTranslogSyncAfterGlobalCheckpointSync() throws Exception {
    final IndicesService indicesService = mock(IndicesService.class);
    final Index index = new Index("index", "uuid");
    final IndexService indexService = mock(IndexService.class);
    when(indicesService.indexServiceSafe(index)).thenReturn(indexService);
    final int id = randomIntBetween(0, 4);
    final IndexShard indexShard = mock(IndexShard.class);
    when(indexService.getShard(id)).thenReturn(indexShard);
    final Translog translog = mock(Translog.class);
    when(indexShard.getTranslog()).thenReturn(translog);
    final GlobalCheckpointSyncAction action = new GlobalCheckpointSyncAction(Settings.EMPTY, transportService, clusterService, indicesService, threadPool, shardStateAction, new ActionFilters(Collections.emptySet()), new IndexNameExpressionResolver(Settings.EMPTY));
    final ShardId shardId = new ShardId(index, id);
    final GlobalCheckpointSyncAction.PrimaryRequest primaryRequest = new GlobalCheckpointSyncAction.PrimaryRequest(shardId);
    if (randomBoolean()) {
        action.shardOperationOnPrimary(primaryRequest, indexShard);
    } else {
        action.shardOperationOnReplica(new GlobalCheckpointSyncAction.ReplicaRequest(primaryRequest, randomNonNegativeLong()), indexShard);
    }
    verify(translog).sync();
}
Also used : IndexService(org.elasticsearch.index.IndexService) IndexShard(org.elasticsearch.index.shard.IndexShard) IndicesService(org.elasticsearch.indices.IndicesService) Index(org.elasticsearch.index.Index) ActionFilters(org.elasticsearch.action.support.ActionFilters) Translog(org.elasticsearch.index.translog.Translog) ShardId(org.elasticsearch.index.shard.ShardId) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver)

Example 20 with IndexShard

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

the class TermVectorsServiceTests method testTook.

public void testTook() throws Exception {
    XContentBuilder mapping = jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("field").field("type", "text").field("term_vector", "with_positions_offsets_payloads").endObject().endObject().endObject().endObject();
    createIndex("test", Settings.EMPTY, "type1", mapping);
    ensureGreen();
    client().prepareIndex("test", "type1", "0").setSource("field", "foo bar").setRefreshPolicy(IMMEDIATE).get();
    IndicesService indicesService = getInstanceFromNode(IndicesService.class);
    IndexService test = indicesService.indexService(resolveIndex("test"));
    IndexShard shard = test.getShardOrNull(0);
    assertThat(shard, notNullValue());
    List<Long> longs = Stream.of(abs(randomLong()), abs(randomLong())).sorted().collect(toList());
    TermVectorsRequest request = new TermVectorsRequest("test", "type1", "0");
    TermVectorsResponse response = TermVectorsService.getTermVectors(shard, request, longs.iterator()::next);
    assertThat(response, notNullValue());
    assertThat(response.getTookInMillis(), equalTo(TimeUnit.NANOSECONDS.toMillis(longs.get(1) - longs.get(0))));
}
Also used : TermVectorsRequest(org.elasticsearch.action.termvectors.TermVectorsRequest) TermVectorsResponse(org.elasticsearch.action.termvectors.TermVectorsResponse) IndexService(org.elasticsearch.index.IndexService) IndexShard(org.elasticsearch.index.shard.IndexShard) IndicesService(org.elasticsearch.indices.IndicesService) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

IndexShard (org.elasticsearch.index.shard.IndexShard)94 IndexService (org.elasticsearch.index.IndexService)51 IndicesService (org.elasticsearch.indices.IndicesService)20 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)19 ShardId (org.elasticsearch.index.shard.ShardId)19 IOException (java.io.IOException)16 Engine (org.elasticsearch.index.engine.Engine)16 ElasticsearchException (org.elasticsearch.ElasticsearchException)13 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)13 Settings (org.elasticsearch.common.settings.Settings)11 Translog (org.elasticsearch.index.translog.Translog)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 HashMap (java.util.HashMap)8 IndexRequest (org.elasticsearch.action.index.IndexRequest)8 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)8 ClusterState (org.elasticsearch.cluster.ClusterState)7 Releasable (org.elasticsearch.common.lease.Releasable)7 ClusterService (org.elasticsearch.cluster.service.ClusterService)6 Path (java.nio.file.Path)5 ArrayList (java.util.ArrayList)5