Search in sources :

Example 1 with StartedShardEntry

use of org.opensearch.cluster.action.shard.ShardStateAction.StartedShardEntry in project OpenSearch by opensearch-project.

the class ShardStartedClusterStateTaskExecutorTests method testNonExistentIndexMarkedAsSuccessful.

public void testNonExistentIndexMarkedAsSuccessful() throws Exception {
    final ClusterState clusterState = stateWithNoShard();
    final StartedShardEntry entry = new StartedShardEntry(new ShardId("test", "_na", 0), "aId", randomNonNegativeLong(), "test");
    final ClusterStateTaskExecutor.ClusterTasksResult result = executeTasks(clusterState, singletonList(entry));
    assertSame(clusterState, result.resultingState);
    assertThat(result.executionResults.size(), equalTo(1));
    assertThat(result.executionResults.containsKey(entry), is(true));
    assertThat(((ClusterStateTaskExecutor.TaskResult) result.executionResults.get(entry)).isSuccess(), is(true));
}
Also used : ShardId(org.opensearch.index.shard.ShardId) ClusterState(org.opensearch.cluster.ClusterState) StartedShardEntry(org.opensearch.cluster.action.shard.ShardStateAction.StartedShardEntry) ClusterStateTaskExecutor(org.opensearch.cluster.ClusterStateTaskExecutor)

Example 2 with StartedShardEntry

use of org.opensearch.cluster.action.shard.ShardStateAction.StartedShardEntry in project OpenSearch by opensearch-project.

the class ShardStartedClusterStateTaskExecutorTests method testStartedShards.

public void testStartedShards() throws Exception {
    final String indexName = "test";
    final ClusterState clusterState = state(indexName, randomBoolean(), ShardRoutingState.INITIALIZING, ShardRoutingState.INITIALIZING);
    final IndexMetadata indexMetadata = clusterState.metadata().index(indexName);
    final ShardId shardId = new ShardId(indexMetadata.getIndex(), 0);
    final long primaryTerm = indexMetadata.primaryTerm(shardId.id());
    final ShardRouting primaryShard = clusterState.routingTable().shardRoutingTable(shardId).primaryShard();
    final String primaryAllocationId = primaryShard.allocationId().getId();
    final List<StartedShardEntry> tasks = new ArrayList<>();
    tasks.add(new StartedShardEntry(shardId, primaryAllocationId, primaryTerm, "test"));
    if (randomBoolean()) {
        final ShardRouting replicaShard = clusterState.routingTable().shardRoutingTable(shardId).replicaShards().iterator().next();
        final String replicaAllocationId = replicaShard.allocationId().getId();
        tasks.add(new StartedShardEntry(shardId, replicaAllocationId, primaryTerm, "test"));
    }
    final ClusterStateTaskExecutor.ClusterTasksResult result = executeTasks(clusterState, tasks);
    assertNotSame(clusterState, result.resultingState);
    assertThat(result.executionResults.size(), equalTo(tasks.size()));
    tasks.forEach(task -> {
        assertThat(result.executionResults.containsKey(task), is(true));
        assertThat(((ClusterStateTaskExecutor.TaskResult) result.executionResults.get(task)).isSuccess(), is(true));
        final IndexShardRoutingTable shardRoutingTable = result.resultingState.routingTable().shardRoutingTable(task.shardId);
        assertThat(shardRoutingTable.getByAllocationId(task.allocationId).state(), is(ShardRoutingState.STARTED));
    });
}
Also used : ShardId(org.opensearch.index.shard.ShardId) ClusterState(org.opensearch.cluster.ClusterState) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) StartedShardEntry(org.opensearch.cluster.action.shard.ShardStateAction.StartedShardEntry) ClusterStateTaskExecutor(org.opensearch.cluster.ClusterStateTaskExecutor) ArrayList(java.util.ArrayList) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ShardRouting(org.opensearch.cluster.routing.ShardRouting)

Example 3 with StartedShardEntry

use of org.opensearch.cluster.action.shard.ShardStateAction.StartedShardEntry in project OpenSearch by opensearch-project.

the class ShardStartedClusterStateTaskExecutorTests method testPrimaryTermsMismatch.

public void testPrimaryTermsMismatch() throws Exception {
    final String indexName = "test";
    final int shard = 0;
    final int primaryTerm = 2 + randomInt(200);
    ClusterState clusterState = state(indexName, randomBoolean(), ShardRoutingState.INITIALIZING, ShardRoutingState.INITIALIZING);
    clusterState = ClusterState.builder(clusterState).metadata(Metadata.builder(clusterState.metadata()).put(IndexMetadata.builder(clusterState.metadata().index(indexName)).primaryTerm(shard, primaryTerm).build(), true).build()).build();
    final ShardId shardId = new ShardId(clusterState.metadata().index(indexName).getIndex(), shard);
    final String primaryAllocationId = clusterState.routingTable().shardRoutingTable(shardId).primaryShard().allocationId().getId();
    {
        final StartedShardEntry task = new StartedShardEntry(shardId, primaryAllocationId, primaryTerm - 1, "primary terms does not match on primary");
        final ClusterStateTaskExecutor.ClusterTasksResult result = executeTasks(clusterState, singletonList(task));
        assertSame(clusterState, result.resultingState);
        assertThat(result.executionResults.size(), equalTo(1));
        assertThat(result.executionResults.containsKey(task), is(true));
        assertThat(((ClusterStateTaskExecutor.TaskResult) result.executionResults.get(task)).isSuccess(), is(true));
        IndexShardRoutingTable shardRoutingTable = result.resultingState.routingTable().shardRoutingTable(task.shardId);
        assertThat(shardRoutingTable.getByAllocationId(task.allocationId).state(), is(ShardRoutingState.INITIALIZING));
        assertSame(clusterState, result.resultingState);
    }
    {
        final StartedShardEntry task = new StartedShardEntry(shardId, primaryAllocationId, primaryTerm, "primary terms match on primary");
        final ClusterStateTaskExecutor.ClusterTasksResult result = executeTasks(clusterState, singletonList(task));
        assertNotSame(clusterState, result.resultingState);
        assertThat(result.executionResults.size(), equalTo(1));
        assertThat(result.executionResults.containsKey(task), is(true));
        assertThat(((ClusterStateTaskExecutor.TaskResult) result.executionResults.get(task)).isSuccess(), is(true));
        IndexShardRoutingTable shardRoutingTable = result.resultingState.routingTable().shardRoutingTable(task.shardId);
        assertThat(shardRoutingTable.getByAllocationId(task.allocationId).state(), is(ShardRoutingState.STARTED));
        assertNotSame(clusterState, result.resultingState);
        clusterState = result.resultingState;
    }
    {
        final long replicaPrimaryTerm = randomBoolean() ? primaryTerm : primaryTerm - 1;
        final String replicaAllocationId = clusterState.routingTable().shardRoutingTable(shardId).replicaShards().iterator().next().allocationId().getId();
        final StartedShardEntry task = new StartedShardEntry(shardId, replicaAllocationId, replicaPrimaryTerm, "test on replica");
        final ClusterStateTaskExecutor.ClusterTasksResult result = executeTasks(clusterState, singletonList(task));
        assertNotSame(clusterState, result.resultingState);
        assertThat(result.executionResults.size(), equalTo(1));
        assertThat(result.executionResults.containsKey(task), is(true));
        assertThat(((ClusterStateTaskExecutor.TaskResult) result.executionResults.get(task)).isSuccess(), is(true));
        IndexShardRoutingTable shardRoutingTable = result.resultingState.routingTable().shardRoutingTable(task.shardId);
        assertThat(shardRoutingTable.getByAllocationId(task.allocationId).state(), is(ShardRoutingState.STARTED));
        assertNotSame(clusterState, result.resultingState);
    }
}
Also used : ShardId(org.opensearch.index.shard.ShardId) ClusterState(org.opensearch.cluster.ClusterState) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) StartedShardEntry(org.opensearch.cluster.action.shard.ShardStateAction.StartedShardEntry)

Example 4 with StartedShardEntry

use of org.opensearch.cluster.action.shard.ShardStateAction.StartedShardEntry in project OpenSearch by opensearch-project.

the class ShardStartedClusterStateTaskExecutorTests method testNonInitializingShardAreMarkedAsSuccessful.

public void testNonInitializingShardAreMarkedAsSuccessful() throws Exception {
    final String indexName = "test";
    final ClusterState clusterState = stateWithAssignedPrimariesAndReplicas(new String[] { indexName }, randomIntBetween(2, 10), 1);
    final IndexMetadata indexMetadata = clusterState.metadata().index(indexName);
    final List<StartedShardEntry> tasks = IntStream.range(0, randomIntBetween(1, indexMetadata.getNumberOfShards())).mapToObj(i -> {
        final ShardId shardId = new ShardId(indexMetadata.getIndex(), i);
        final IndexShardRoutingTable shardRoutingTable = clusterState.routingTable().shardRoutingTable(shardId);
        final String allocationId;
        if (randomBoolean()) {
            allocationId = shardRoutingTable.primaryShard().allocationId().getId();
        } else {
            allocationId = shardRoutingTable.replicaShards().iterator().next().allocationId().getId();
        }
        final long primaryTerm = indexMetadata.primaryTerm(shardId.id());
        return new StartedShardEntry(shardId, allocationId, primaryTerm, "test");
    }).collect(Collectors.toList());
    final ClusterStateTaskExecutor.ClusterTasksResult result = executeTasks(clusterState, tasks);
    assertSame(clusterState, result.resultingState);
    assertThat(result.executionResults.size(), equalTo(tasks.size()));
    tasks.forEach(task -> {
        assertThat(result.executionResults.containsKey(task), is(true));
        assertThat(((ClusterStateTaskExecutor.TaskResult) result.executionResults.get(task)).isSuccess(), is(true));
    });
}
Also used : IntStream(java.util.stream.IntStream) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AllocationService(org.opensearch.cluster.routing.allocation.AllocationService) Priority(org.opensearch.common.Priority) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) ClusterState(org.opensearch.cluster.ClusterState) OpenSearchAllocationTestCase(org.opensearch.cluster.OpenSearchAllocationTestCase) ShardRoutingState(org.opensearch.cluster.routing.ShardRoutingState) ActionListener(org.opensearch.action.ActionListener) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) ClusterStateTaskExecutor(org.opensearch.cluster.ClusterStateTaskExecutor) Settings(org.opensearch.common.settings.Settings) ClusterStateCreationUtils.stateWithActivePrimary(org.opensearch.action.support.replication.ClusterStateCreationUtils.stateWithActivePrimary) ClusterStateCreationUtils.stateWithAssignedPrimariesAndReplicas(org.opensearch.action.support.replication.ClusterStateCreationUtils.stateWithAssignedPrimariesAndReplicas) ClusterStateCreationUtils.state(org.opensearch.action.support.replication.ClusterStateCreationUtils.state) Collectors(java.util.stream.Collectors) CLUSTER_ROUTING_ALLOCATION_NODE_CONCURRENT_RECOVERIES_SETTING(org.opensearch.cluster.routing.allocation.decider.ThrottlingAllocationDecider.CLUSTER_ROUTING_ALLOCATION_NODE_CONCURRENT_RECOVERIES_SETTING) ShardRouting(org.opensearch.cluster.routing.ShardRouting) ShardId(org.opensearch.index.shard.ShardId) ClusterStateCreationUtils.stateWithNoShard(org.opensearch.action.support.replication.ClusterStateCreationUtils.stateWithNoShard) List(java.util.List) Stream(java.util.stream.Stream) Matchers.equalTo(org.hamcrest.Matchers.equalTo) StartedShardEntry(org.opensearch.cluster.action.shard.ShardStateAction.StartedShardEntry) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) ShardId(org.opensearch.index.shard.ShardId) ClusterState(org.opensearch.cluster.ClusterState) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) StartedShardEntry(org.opensearch.cluster.action.shard.ShardStateAction.StartedShardEntry) ClusterStateTaskExecutor(org.opensearch.cluster.ClusterStateTaskExecutor) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata)

Example 5 with StartedShardEntry

use of org.opensearch.cluster.action.shard.ShardStateAction.StartedShardEntry in project OpenSearch by opensearch-project.

the class ShardStartedClusterStateTaskExecutorTests method testNonExistentShardsAreMarkedAsSuccessful.

public void testNonExistentShardsAreMarkedAsSuccessful() throws Exception {
    final String indexName = "test";
    final ClusterState clusterState = stateWithActivePrimary(indexName, true, randomInt(2), randomInt(2));
    final IndexMetadata indexMetadata = clusterState.metadata().index(indexName);
    final List<StartedShardEntry> tasks = Stream.concat(// Existent shard id but different allocation id
    IntStream.range(0, randomIntBetween(1, 5)).mapToObj(i -> new StartedShardEntry(new ShardId(indexMetadata.getIndex(), 0), String.valueOf(i), 0L, "allocation id")), // Non existent shard id
    IntStream.range(1, randomIntBetween(2, 5)).mapToObj(i -> new StartedShardEntry(new ShardId(indexMetadata.getIndex(), i), String.valueOf(i), 0L, "shard id"))).collect(Collectors.toList());
    final ClusterStateTaskExecutor.ClusterTasksResult result = executeTasks(clusterState, tasks);
    assertSame(clusterState, result.resultingState);
    assertThat(result.executionResults.size(), equalTo(tasks.size()));
    tasks.forEach(task -> {
        assertThat(result.executionResults.containsKey(task), is(true));
        assertThat(((ClusterStateTaskExecutor.TaskResult) result.executionResults.get(task)).isSuccess(), is(true));
    });
}
Also used : ShardId(org.opensearch.index.shard.ShardId) ClusterState(org.opensearch.cluster.ClusterState) StartedShardEntry(org.opensearch.cluster.action.shard.ShardStateAction.StartedShardEntry) ClusterStateTaskExecutor(org.opensearch.cluster.ClusterStateTaskExecutor) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata)

Aggregations

StartedShardEntry (org.opensearch.cluster.action.shard.ShardStateAction.StartedShardEntry)7 ShardId (org.opensearch.index.shard.ShardId)7 ClusterState (org.opensearch.cluster.ClusterState)6 ClusterStateTaskExecutor (org.opensearch.cluster.ClusterStateTaskExecutor)5 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)4 IndexShardRoutingTable (org.opensearch.cluster.routing.IndexShardRoutingTable)4 ArrayList (java.util.ArrayList)3 ShardRouting (org.opensearch.cluster.routing.ShardRouting)3 Collections (java.util.Collections)2 Collections.singletonList (java.util.Collections.singletonList)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 IntStream (java.util.stream.IntStream)2 Stream (java.util.stream.Stream)2 Matchers.equalTo (org.hamcrest.Matchers.equalTo)2 Matchers.is (org.hamcrest.Matchers.is)2 Matchers.notNullValue (org.hamcrest.Matchers.notNullValue)2 ActionListener (org.opensearch.action.ActionListener)2 ClusterStateCreationUtils.state (org.opensearch.action.support.replication.ClusterStateCreationUtils.state)2 ClusterStateCreationUtils.stateWithActivePrimary (org.opensearch.action.support.replication.ClusterStateCreationUtils.stateWithActivePrimary)2