use of org.elasticsearch.cluster.routing.ShardRouting in project elasticsearch by elastic.
the class ClusterAllocationExplainActionTests method testFindPrimaryShardToExplain.
public void testFindPrimaryShardToExplain() {
ClusterState clusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), randomFrom(ShardRoutingState.values()));
ClusterAllocationExplainRequest request = new ClusterAllocationExplainRequest("idx", 0, true, null);
ShardRouting shard = findShardToExplain(request, routingAllocation(clusterState));
assertEquals(clusterState.getRoutingTable().index("idx").shard(0).primaryShard(), shard);
}
use of org.elasticsearch.cluster.routing.ShardRouting in project elasticsearch by elastic.
the class ClusterAllocationExplainActionTests method testFindAnyUnassignedShardToExplain.
public void testFindAnyUnassignedShardToExplain() {
// find unassigned primary
ClusterState clusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), ShardRoutingState.UNASSIGNED);
ClusterAllocationExplainRequest request = new ClusterAllocationExplainRequest();
ShardRouting shard = findShardToExplain(request, routingAllocation(clusterState));
assertEquals(clusterState.getRoutingTable().index("idx").shard(0).primaryShard(), shard);
// find unassigned replica
clusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), ShardRoutingState.STARTED, ShardRoutingState.UNASSIGNED);
request = new ClusterAllocationExplainRequest();
shard = findShardToExplain(request, routingAllocation(clusterState));
assertEquals(clusterState.getRoutingTable().index("idx").shard(0).replicaShards().get(0), shard);
// no unassigned shard to explain
final ClusterState allStartedClusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), ShardRoutingState.STARTED, ShardRoutingState.STARTED);
final ClusterAllocationExplainRequest anyUnassignedShardsRequest = new ClusterAllocationExplainRequest();
expectThrows(IllegalStateException.class, () -> findShardToExplain(anyUnassignedShardsRequest, routingAllocation(allStartedClusterState)));
}
use of org.elasticsearch.cluster.routing.ShardRouting in project elasticsearch by elastic.
the class ClusterAllocationExplainActionTests method testFindAnyReplicaToExplain.
public void testFindAnyReplicaToExplain() {
// prefer unassigned replicas to started replicas
ClusterState clusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), ShardRoutingState.STARTED, ShardRoutingState.STARTED, ShardRoutingState.UNASSIGNED);
ClusterAllocationExplainRequest request = new ClusterAllocationExplainRequest("idx", 0, false, null);
ShardRouting shard = findShardToExplain(request, routingAllocation(clusterState));
assertEquals(clusterState.getRoutingTable().index("idx").shard(0).replicaShards().stream().filter(ShardRouting::unassigned).findFirst().get(), shard);
// prefer started replicas to initializing/relocating replicas
clusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), ShardRoutingState.STARTED, randomFrom(ShardRoutingState.RELOCATING, ShardRoutingState.INITIALIZING), ShardRoutingState.STARTED);
request = new ClusterAllocationExplainRequest("idx", 0, false, null);
shard = findShardToExplain(request, routingAllocation(clusterState));
assertEquals(clusterState.getRoutingTable().index("idx").shard(0).replicaShards().stream().filter(ShardRouting::started).findFirst().get(), shard);
}
use of org.elasticsearch.cluster.routing.ShardRouting in project elasticsearch by elastic.
the class ClusterAllocationExplainActionTests method testFindShardAssignedToNode.
public void testFindShardAssignedToNode() {
// find shard with given node
final boolean primary = randomBoolean();
ShardRoutingState[] replicaStates = new ShardRoutingState[0];
if (primary == false) {
replicaStates = new ShardRoutingState[] { ShardRoutingState.STARTED };
}
ClusterState clusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), ShardRoutingState.STARTED, replicaStates);
ShardRouting shardToExplain = primary ? clusterState.getRoutingTable().index("idx").shard(0).primaryShard() : clusterState.getRoutingTable().index("idx").shard(0).replicaShards().get(0);
ClusterAllocationExplainRequest request = new ClusterAllocationExplainRequest("idx", 0, primary, shardToExplain.currentNodeId());
RoutingAllocation allocation = routingAllocation(clusterState);
ShardRouting foundShard = findShardToExplain(request, allocation);
assertEquals(shardToExplain, foundShard);
// shard is not assigned to given node
String explainNode = null;
for (RoutingNode routingNode : clusterState.getRoutingNodes()) {
if (routingNode.nodeId().equals(shardToExplain.currentNodeId()) == false) {
explainNode = routingNode.nodeId();
break;
}
}
final ClusterAllocationExplainRequest failingRequest = new ClusterAllocationExplainRequest("idx", 0, primary, explainNode);
expectThrows(IllegalStateException.class, () -> findShardToExplain(failingRequest, allocation));
}
use of org.elasticsearch.cluster.routing.ShardRouting in project elasticsearch by elastic.
the class TransportReplicationActionTests method testReplicaProxy.
public void testReplicaProxy() throws InterruptedException, ExecutionException {
ReplicationOperation.Replicas proxy = action.newReplicasProxy();
final String index = "test";
final ShardId shardId = new ShardId(index, "_na_", 0);
ClusterState state = stateWithActivePrimary(index, true, 1 + randomInt(3), randomInt(2));
logger.info("using state: {}", state);
setState(clusterService, state);
// check that at unknown node fails
PlainActionFuture<ReplicaResponse> listener = new PlainActionFuture<>();
proxy.performOn(TestShardRouting.newShardRouting(shardId, "NOT THERE", false, randomFrom(ShardRoutingState.values())), new Request(), listener);
assertTrue(listener.isDone());
assertListenerThrows("non existent node should throw a NoNodeAvailableException", listener, NoNodeAvailableException.class);
final IndexShardRoutingTable shardRoutings = state.routingTable().shardRoutingTable(shardId);
final ShardRouting replica = randomFrom(shardRoutings.replicaShards().stream().filter(ShardRouting::assignedToNode).collect(Collectors.toList()));
listener = new PlainActionFuture<>();
proxy.performOn(replica, new Request(), listener);
assertFalse(listener.isDone());
CapturingTransport.CapturedRequest[] captures = transport.getCapturedRequestsAndClear();
assertThat(captures, arrayWithSize(1));
if (randomBoolean()) {
final TransportReplicationAction.ReplicaResponse response = new TransportReplicationAction.ReplicaResponse(randomAsciiOfLength(10), randomLong());
transport.handleResponse(captures[0].requestId, response);
assertTrue(listener.isDone());
assertThat(listener.get(), equalTo(response));
} else if (randomBoolean()) {
transport.handleRemoteError(captures[0].requestId, new ElasticsearchException("simulated"));
assertTrue(listener.isDone());
assertListenerThrows("listener should reflect remote error", listener, ElasticsearchException.class);
} else {
transport.handleError(captures[0].requestId, new TransportException("simulated"));
assertTrue(listener.isDone());
assertListenerThrows("listener should reflect remote error", listener, TransportException.class);
}
AtomicReference<Object> failure = new AtomicReference<>();
AtomicReference<Object> ignoredFailure = new AtomicReference<>();
AtomicBoolean success = new AtomicBoolean();
proxy.failShardIfNeeded(replica, randomIntBetween(1, 10), "test", new ElasticsearchException("simulated"), () -> success.set(true), failure::set, ignoredFailure::set);
CapturingTransport.CapturedRequest[] shardFailedRequests = transport.getCapturedRequestsAndClear();
// A replication action doesn't not fail the request
assertEquals(0, shardFailedRequests.length);
}
Aggregations