use of org.opensearch.cluster.routing.ShardRouting in project OpenSearch by opensearch-project.
the class ShardFailedClusterStateTaskExecutorTests method createExistingShards.
private List<ShardStateAction.FailedShardEntry> createExistingShards(ClusterState currentState, String reason) {
List<ShardRouting> shards = new ArrayList<>();
GroupShardsIterator<ShardIterator> shardGroups = currentState.routingTable().allAssignedShardsGrouped(new String[] { INDEX }, true);
for (ShardIterator shardIt : shardGroups) {
for (ShardRouting shard : shardIt) {
shards.add(shard);
}
}
List<ShardRouting> failures = randomSubsetOf(randomIntBetween(1, 1 + shards.size() / 4), shards.toArray(new ShardRouting[0]));
String indexUUID = metadata.index(INDEX).getIndexUUID();
int numberOfTasks = randomIntBetween(failures.size(), 2 * failures.size());
List<ShardRouting> shardsToFail = new ArrayList<>(numberOfTasks);
for (int i = 0; i < numberOfTasks; i++) {
shardsToFail.add(randomFrom(failures));
}
return toTasks(currentState, shardsToFail, indexUUID, reason);
}
use of org.opensearch.cluster.routing.ShardRouting 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));
});
}
use of org.opensearch.cluster.routing.ShardRouting in project OpenSearch by opensearch-project.
the class ShardStateActionTests method testNoClusterManager.
public void testNoClusterManager() throws InterruptedException {
final String index = "test";
setState(clusterService, ClusterStateCreationUtils.stateWithActivePrimary(index, true, randomInt(5)));
DiscoveryNodes.Builder noClusterManagerBuilder = DiscoveryNodes.builder(clusterService.state().nodes());
noClusterManagerBuilder.masterNodeId(null);
setState(clusterService, ClusterState.builder(clusterService.state()).nodes(noClusterManagerBuilder));
CountDownLatch latch = new CountDownLatch(1);
AtomicInteger retries = new AtomicInteger();
AtomicBoolean success = new AtomicBoolean();
setUpClusterManagerRetryVerification(1, retries, latch, requestId -> {
});
ShardRouting failedShard = getRandomShardRouting(index);
shardStateAction.localShardFailed(failedShard, "test", getSimulatedFailure(), new ActionListener<Void>() {
@Override
public void onResponse(Void aVoid) {
success.set(true);
latch.countDown();
}
@Override
public void onFailure(Exception e) {
success.set(false);
latch.countDown();
assert false;
}
});
latch.await();
assertThat(retries.get(), equalTo(1));
assertTrue(success.get());
}
use of org.opensearch.cluster.routing.ShardRouting in project OpenSearch by opensearch-project.
the class ShardStateActionTests method testClusterManagerChannelException.
public void testClusterManagerChannelException() throws InterruptedException {
final String index = "test";
setState(clusterService, ClusterStateCreationUtils.stateWithActivePrimary(index, true, randomInt(5)));
CountDownLatch latch = new CountDownLatch(1);
AtomicInteger retries = new AtomicInteger();
AtomicBoolean success = new AtomicBoolean();
AtomicReference<Throwable> throwable = new AtomicReference<>();
LongConsumer retryLoop = requestId -> {
if (randomBoolean()) {
transport.handleRemoteError(requestId, randomFrom(new NotMasterException("simulated"), new FailedToCommitClusterStateException("simulated")));
} else {
if (randomBoolean()) {
transport.handleLocalError(requestId, new NodeNotConnectedException(null, "simulated"));
} else {
transport.handleError(requestId, new NodeDisconnectedException(null, ShardStateAction.SHARD_FAILED_ACTION_NAME));
}
}
};
final int numberOfRetries = randomIntBetween(1, 256);
setUpClusterManagerRetryVerification(numberOfRetries, retries, latch, retryLoop);
ShardRouting failedShard = getRandomShardRouting(index);
shardStateAction.localShardFailed(failedShard, "test", getSimulatedFailure(), new ActionListener<Void>() {
@Override
public void onResponse(Void aVoid) {
success.set(true);
latch.countDown();
}
@Override
public void onFailure(Exception e) {
success.set(false);
throwable.set(e);
latch.countDown();
assert false;
}
});
final CapturingTransport.CapturedRequest[] capturedRequests = transport.getCapturedRequestsAndClear();
assertThat(capturedRequests.length, equalTo(1));
assertFalse(success.get());
assertThat(retries.get(), equalTo(0));
retryLoop.accept(capturedRequests[0].requestId);
latch.await();
assertNull(throwable.get());
assertThat(retries.get(), equalTo(numberOfRetries));
assertTrue(success.get());
}
use of org.opensearch.cluster.routing.ShardRouting in project OpenSearch by opensearch-project.
the class ShardStateActionTests method getRandomShardRouting.
private ShardRouting getRandomShardRouting(String index) {
IndexRoutingTable indexRoutingTable = clusterService.state().routingTable().index(index);
ShardsIterator shardsIterator = indexRoutingTable.randomAllActiveShardsIt();
ShardRouting shardRouting = shardsIterator.nextOrNull();
assert shardRouting != null;
return shardRouting;
}
Aggregations