Search in sources :

Example 1 with IndexShardState

use of org.opensearch.index.shard.IndexShardState in project OpenSearch by opensearch-project.

the class IndicesServiceTests method testStatsByShardDoesNotDieFromExpectedExceptions.

public void testStatsByShardDoesNotDieFromExpectedExceptions() {
    final int shardCount = randomIntBetween(2, 5);
    final int failedShardId = randomIntBetween(0, shardCount - 1);
    final Index index = new Index("test-index", "abc123");
    // the shard that is going to fail
    final ShardId shardId = new ShardId(index, failedShardId);
    final List<IndexShard> shards = new ArrayList<>(shardCount);
    final List<IndexShardStats> shardStats = new ArrayList<>(shardCount - 1);
    final IndexShardState state = randomFrom(IndexShardState.values());
    final String message = "TEST - expected";
    final RuntimeException expectedException = randomFrom(new IllegalIndexShardStateException(shardId, state, message), new AlreadyClosedException(message));
    // this allows us to control the indices that exist
    final IndicesService mockIndicesService = mock(IndicesService.class);
    final IndexService indexService = mock(IndexService.class);
    // generate fake shards and their responses
    for (int i = 0; i < shardCount; ++i) {
        final IndexShard shard = mock(IndexShard.class);
        shards.add(shard);
        if (failedShardId != i) {
            final IndexShardStats successfulShardStats = mock(IndexShardStats.class);
            shardStats.add(successfulShardStats);
            when(mockIndicesService.indexShardStats(mockIndicesService, shard, CommonStatsFlags.ALL)).thenReturn(successfulShardStats);
        } else {
            when(mockIndicesService.indexShardStats(mockIndicesService, shard, CommonStatsFlags.ALL)).thenThrow(expectedException);
        }
    }
    when(mockIndicesService.iterator()).thenReturn(Collections.singleton(indexService).iterator());
    when(indexService.iterator()).thenReturn(shards.iterator());
    when(indexService.index()).thenReturn(index);
    // real one, which has a logger defined
    final IndicesService indicesService = getIndicesService();
    final Map<Index, List<IndexShardStats>> indexStats = indicesService.statsByShard(mockIndicesService, CommonStatsFlags.ALL);
    assertThat(indexStats.isEmpty(), equalTo(false));
    assertThat("index not defined", indexStats.containsKey(index), equalTo(true));
    assertThat("unexpected shard stats", indexStats.get(index), equalTo(shardStats));
}
Also used : IndexService(org.opensearch.index.IndexService) IndexShard(org.opensearch.index.shard.IndexShard) ArrayList(java.util.ArrayList) Index(org.opensearch.index.Index) IndexShardStats(org.opensearch.action.admin.indices.stats.IndexShardStats) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) IllegalIndexShardStateException(org.opensearch.index.shard.IllegalIndexShardStateException) ShardId(org.opensearch.index.shard.ShardId) List(java.util.List) ArrayList(java.util.ArrayList) IndexShardState(org.opensearch.index.shard.IndexShardState)

Example 2 with IndexShardState

use of org.opensearch.index.shard.IndexShardState in project OpenSearch by opensearch-project.

the class IndicesClusterStateService method updateShard.

private void updateShard(DiscoveryNodes nodes, ShardRouting shardRouting, Shard shard, RoutingTable routingTable, ClusterState clusterState) {
    final ShardRouting currentRoutingEntry = shard.routingEntry();
    assert currentRoutingEntry.isSameAllocation(shardRouting) : "local shard has a different allocation id but wasn't cleaned by removeShards. " + "cluster state: " + shardRouting + " local: " + currentRoutingEntry;
    final long primaryTerm;
    try {
        final IndexMetadata indexMetadata = clusterState.metadata().index(shard.shardId().getIndex());
        primaryTerm = indexMetadata.primaryTerm(shard.shardId().id());
        final Set<String> inSyncIds = indexMetadata.inSyncAllocationIds(shard.shardId().id());
        final IndexShardRoutingTable indexShardRoutingTable = routingTable.shardRoutingTable(shardRouting.shardId());
        shard.updateShardState(shardRouting, primaryTerm, primaryReplicaSyncer::resync, clusterState.version(), inSyncIds, indexShardRoutingTable);
    } catch (Exception e) {
        failAndRemoveShard(shardRouting, true, "failed updating shard routing entry", e, clusterState);
        return;
    }
    final IndexShardState state = shard.state();
    if (shardRouting.initializing() && (state == IndexShardState.STARTED || state == IndexShardState.POST_RECOVERY)) {
        // we managed to tell the master we started), mark us as started
        if (logger.isTraceEnabled()) {
            logger.trace("{} master marked shard as initializing, but shard has state [{}], resending shard started to {}", shardRouting.shardId(), state, nodes.getMasterNode());
        }
        if (nodes.getMasterNode() != null) {
            shardStateAction.shardStarted(shardRouting, primaryTerm, "master " + nodes.getMasterNode() + " marked shard as initializing, but shard state is [" + state + "], mark shard as started", SHARD_STATE_ACTION_LISTENER, clusterState);
        }
    }
}
Also used : IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) ShardRouting(org.opensearch.cluster.routing.ShardRouting) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) IndexShardState(org.opensearch.index.shard.IndexShardState) ResourceAlreadyExistsException(org.opensearch.ResourceAlreadyExistsException) RecoveryFailedException(org.opensearch.indices.recovery.RecoveryFailedException) ShardLockObtainFailedException(org.opensearch.env.ShardLockObtainFailedException) IOException(java.io.IOException) ShardNotFoundException(org.opensearch.index.shard.ShardNotFoundException) IndexShardRelocatedException(org.opensearch.index.shard.IndexShardRelocatedException)

Example 3 with IndexShardState

use of org.opensearch.index.shard.IndexShardState in project OpenSearch by opensearch-project.

the class SnapshotShardsService method snapshot.

/**
 * Creates shard snapshot
 *
 * @param snapshot       snapshot
 * @param snapshotStatus snapshot status
 */
private void snapshot(final ShardId shardId, final Snapshot snapshot, final IndexId indexId, final Map<String, Object> userMetadata, final IndexShardSnapshotStatus snapshotStatus, Version version, ActionListener<String> listener) {
    try {
        final IndexShard indexShard = indicesService.indexServiceSafe(shardId.getIndex()).getShardOrNull(shardId.id());
        if (indexShard.routingEntry().primary() == false) {
            throw new IndexShardSnapshotFailedException(shardId, "snapshot should be performed only on primary");
        }
        if (indexShard.routingEntry().relocating()) {
            // do not snapshot when in the process of relocation of primaries so we won't get conflicts
            throw new IndexShardSnapshotFailedException(shardId, "cannot snapshot while relocating");
        }
        final IndexShardState indexShardState = indexShard.state();
        if (indexShardState == IndexShardState.CREATED || indexShardState == IndexShardState.RECOVERING) {
            // shard has just been created, or still recovering
            throw new IndexShardSnapshotFailedException(shardId, "shard didn't fully recover yet");
        }
        final Repository repository = repositoriesService.repository(snapshot.getRepository());
        Engine.IndexCommitRef snapshotRef = null;
        try {
            // we flush first to make sure we get the latest writes snapshotted
            snapshotRef = indexShard.acquireLastIndexCommit(true);
            final IndexCommit snapshotIndexCommit = snapshotRef.getIndexCommit();
            repository.snapshotShard(indexShard.store(), indexShard.mapperService(), snapshot.getSnapshotId(), indexId, snapshotRef.getIndexCommit(), getShardStateId(indexShard, snapshotIndexCommit), snapshotStatus, version, userMetadata, ActionListener.runBefore(listener, snapshotRef::close));
        } catch (Exception e) {
            IOUtils.close(snapshotRef);
            throw e;
        }
    } catch (Exception e) {
        listener.onFailure(e);
    }
}
Also used : Repository(org.opensearch.repositories.Repository) IndexShard(org.opensearch.index.shard.IndexShard) IndexShardSnapshotFailedException(org.opensearch.index.snapshots.IndexShardSnapshotFailedException) IndexShardState(org.opensearch.index.shard.IndexShardState) Engine(org.opensearch.index.engine.Engine) IndexCommit(org.apache.lucene.index.IndexCommit) IOException(java.io.IOException) IndexShardSnapshotFailedException(org.opensearch.index.snapshots.IndexShardSnapshotFailedException) TransportException(org.opensearch.transport.TransportException)

Example 4 with IndexShardState

use of org.opensearch.index.shard.IndexShardState in project OpenSearch by opensearch-project.

the class RelocationIT method testRelocationWhileRefreshing.

public void testRelocationWhileRefreshing() throws Exception {
    int numberOfRelocations = scaledRandomIntBetween(1, rarely() ? 10 : 4);
    int numberOfReplicas = randomBoolean() ? 0 : 1;
    int numberOfNodes = numberOfReplicas == 0 ? 2 : 3;
    logger.info("testRelocationWhileIndexingRandom(numRelocations={}, numberOfReplicas={}, numberOfNodes={})", numberOfRelocations, numberOfReplicas, numberOfNodes);
    String[] nodes = new String[numberOfNodes];
    logger.info("--> starting [node_0] ...");
    nodes[0] = internalCluster().startNode();
    logger.info("--> creating test index ...");
    prepareCreate("test", Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", numberOfReplicas).put("index.refresh_interval", -1)).get();
    for (int i = 1; i < numberOfNodes; i++) {
        logger.info("--> starting [node_{}] ...", i);
        nodes[i] = internalCluster().startNode();
        if (i != numberOfNodes - 1) {
            ClusterHealthResponse healthResponse = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForNodes(Integer.toString(i + 1)).setWaitForGreenStatus().execute().actionGet();
            assertThat(healthResponse.isTimedOut(), equalTo(false));
        }
    }
    final Semaphore postRecoveryShards = new Semaphore(0);
    final IndexEventListener listener = new IndexEventListener() {

        @Override
        public void indexShardStateChanged(IndexShard indexShard, @Nullable IndexShardState previousState, IndexShardState currentState, @Nullable String reason) {
            if (currentState == IndexShardState.POST_RECOVERY) {
                postRecoveryShards.release();
            }
        }
    };
    for (MockIndexEventListener.TestEventListener eventListener : internalCluster().getInstances(MockIndexEventListener.TestEventListener.class)) {
        eventListener.setNewDelegate(listener);
    }
    logger.info("--> starting relocations...");
    // if we have replicas shift those
    int nodeShiftBased = numberOfReplicas;
    for (int i = 0; i < numberOfRelocations; i++) {
        int fromNode = (i % 2);
        int toNode = fromNode == 0 ? 1 : 0;
        fromNode += nodeShiftBased;
        toNode += nodeShiftBased;
        List<IndexRequestBuilder> builders1 = new ArrayList<>();
        for (int numDocs = randomIntBetween(10, 30); numDocs > 0; numDocs--) {
            builders1.add(client().prepareIndex("test").setSource("{}", XContentType.JSON));
        }
        List<IndexRequestBuilder> builders2 = new ArrayList<>();
        for (int numDocs = randomIntBetween(10, 30); numDocs > 0; numDocs--) {
            builders2.add(client().prepareIndex("test").setSource("{}", XContentType.JSON));
        }
        logger.info("--> START relocate the shard from {} to {}", nodes[fromNode], nodes[toNode]);
        client().admin().cluster().prepareReroute().add(new MoveAllocationCommand("test", 0, nodes[fromNode], nodes[toNode])).get();
        logger.debug("--> index [{}] documents", builders1.size());
        indexRandom(false, true, builders1);
        // wait for shard to reach post recovery
        postRecoveryShards.acquire(1);
        logger.debug("--> index [{}] documents", builders2.size());
        indexRandom(true, true, builders2);
        // verify cluster was finished.
        assertFalse(client().admin().cluster().prepareHealth().setWaitForNoRelocatingShards(true).setWaitForEvents(Priority.LANGUID).setTimeout("30s").get().isTimedOut());
        logger.info("--> DONE relocate the shard from {} to {}", fromNode, toNode);
        logger.debug("--> verifying all searches return the same number of docs");
        long expectedCount = -1;
        for (Client client : clients()) {
            SearchResponse response = client.prepareSearch("test").setPreference("_local").setSize(0).get();
            assertNoFailures(response);
            if (expectedCount < 0) {
                expectedCount = response.getHits().getTotalHits().value;
            } else {
                assertEquals(expectedCount, response.getHits().getTotalHits().value);
            }
        }
    }
}
Also used : MockIndexEventListener(org.opensearch.test.MockIndexEventListener) ClusterHealthResponse(org.opensearch.action.admin.cluster.health.ClusterHealthResponse) IndexShard(org.opensearch.index.shard.IndexShard) ArrayList(java.util.ArrayList) MoveAllocationCommand(org.opensearch.cluster.routing.allocation.command.MoveAllocationCommand) Semaphore(java.util.concurrent.Semaphore) SearchResponse(org.opensearch.action.search.SearchResponse) IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) MockIndexEventListener(org.opensearch.test.MockIndexEventListener) IndexEventListener(org.opensearch.index.shard.IndexEventListener) Client(org.opensearch.client.Client) IndexShardState(org.opensearch.index.shard.IndexShardState) Nullable(org.opensearch.common.Nullable)

Example 5 with IndexShardState

use of org.opensearch.index.shard.IndexShardState in project OpenSearch by opensearch-project.

the class ExceptionSerializationTests method testIllegalIndexShardStateException.

public void testIllegalIndexShardStateException() throws IOException {
    ShardId id = new ShardId("foo", "_na_", 1);
    IndexShardState state = randomFrom(IndexShardState.values());
    IllegalIndexShardStateException ex = serialize(new IllegalIndexShardStateException(id, state, "come back later buddy"));
    assertEquals(id, ex.getShardId());
    assertEquals("CurrentState[" + state.name() + "] come back later buddy", ex.getMessage());
    assertEquals(state, ex.currentState());
}
Also used : ShardId(org.opensearch.index.shard.ShardId) IllegalIndexShardStateException(org.opensearch.index.shard.IllegalIndexShardStateException) IndexShardState(org.opensearch.index.shard.IndexShardState)

Aggregations

IndexShardState (org.opensearch.index.shard.IndexShardState)5 IndexShard (org.opensearch.index.shard.IndexShard)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 IllegalIndexShardStateException (org.opensearch.index.shard.IllegalIndexShardStateException)2 ShardId (org.opensearch.index.shard.ShardId)2 List (java.util.List)1 Semaphore (java.util.concurrent.Semaphore)1 IndexCommit (org.apache.lucene.index.IndexCommit)1 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Matchers.hasToString (org.hamcrest.Matchers.hasToString)1 ResourceAlreadyExistsException (org.opensearch.ResourceAlreadyExistsException)1 ClusterHealthResponse (org.opensearch.action.admin.cluster.health.ClusterHealthResponse)1 IndexShardStats (org.opensearch.action.admin.indices.stats.IndexShardStats)1 IndexRequestBuilder (org.opensearch.action.index.IndexRequestBuilder)1 SearchResponse (org.opensearch.action.search.SearchResponse)1 Client (org.opensearch.client.Client)1 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)1 IndexShardRoutingTable (org.opensearch.cluster.routing.IndexShardRoutingTable)1