use of org.elasticsearch.cluster.routing.allocation.AllocationDecision in project elasticsearch by elastic.
the class ClusterAllocationExplainIT method testUnassignedReplicaDelayedAllocation.
public void testUnassignedReplicaDelayedAllocation() throws Exception {
logger.info("--> starting 3 nodes");
internalCluster().startNodes(3);
logger.info("--> creating an index with 1 primary, 1 replica");
createIndexAndIndexData(1, 1);
logger.info("--> stopping the node with the replica");
internalCluster().stopRandomNode(InternalTestCluster.nameFilter(replicaNode().getName()));
ensureStableCluster(2);
assertBusy(() -> assertEquals(AllocationDecision.ALLOCATION_DELAYED, client().admin().cluster().prepareAllocationExplain().setIndex("idx").setShard(0).setPrimary(false).get().getExplanation().getShardAllocationDecision().getAllocateDecision().getAllocationDecision()));
logger.info("--> observing delayed allocation...");
boolean includeYesDecisions = randomBoolean();
boolean includeDiskInfo = randomBoolean();
ClusterAllocationExplanation explanation = runExplain(false, includeYesDecisions, includeDiskInfo);
ShardId shardId = explanation.getShard();
boolean isPrimary = explanation.isPrimary();
ShardRoutingState shardRoutingState = explanation.getShardState();
DiscoveryNode currentNode = explanation.getCurrentNode();
UnassignedInfo unassignedInfo = explanation.getUnassignedInfo();
ClusterInfo clusterInfo = explanation.getClusterInfo();
AllocateUnassignedDecision allocateDecision = explanation.getShardAllocationDecision().getAllocateDecision();
MoveDecision moveDecision = explanation.getShardAllocationDecision().getMoveDecision();
// verify shard info
assertEquals("idx", shardId.getIndexName());
assertEquals(0, shardId.getId());
assertFalse(isPrimary);
// verify current node info
assertNotEquals(ShardRoutingState.STARTED, shardRoutingState);
assertNull(currentNode);
// verify unassigned info
assertNotNull(unassignedInfo);
assertEquals(Reason.NODE_LEFT, unassignedInfo.getReason());
assertEquals(AllocationStatus.NO_ATTEMPT, unassignedInfo.getLastAllocationStatus());
// verify cluster info
verifyClusterInfo(clusterInfo, includeDiskInfo, 2);
// verify decision objects
assertTrue(allocateDecision.isDecisionTaken());
assertFalse(moveDecision.isDecisionTaken());
assertEquals(AllocationDecision.ALLOCATION_DELAYED, allocateDecision.getAllocationDecision());
assertThat(allocateDecision.getExplanation(), startsWith("cannot allocate because the cluster is still waiting"));
assertThat(allocateDecision.getExplanation(), containsString("despite being allowed to allocate the shard to at least one other node"));
assertNull(allocateDecision.getAllocationId());
assertNull(allocateDecision.getTargetNode());
assertEquals(60000L, allocateDecision.getConfiguredDelayInMillis());
assertThat(allocateDecision.getRemainingDelayInMillis(), greaterThan(0L));
assertEquals(2, allocateDecision.getNodeDecisions().size());
String primaryNodeName = primaryNodeName();
for (NodeAllocationResult result : allocateDecision.getNodeDecisions()) {
assertNotNull(result.getNode());
boolean nodeHoldingPrimary = result.getNode().getName().equals(primaryNodeName);
if (nodeHoldingPrimary) {
// shouldn't be able to allocate to the same node as the primary, the same shard decider should say no
assertEquals(AllocationDecision.NO, result.getNodeDecision());
assertThat(result.getShardStoreInfo().getMatchingBytes(), greaterThan(0L));
} else {
assertEquals(AllocationDecision.YES, result.getNodeDecision());
assertNull(result.getShardStoreInfo());
}
if (includeYesDecisions) {
assertThat(result.getCanAllocateDecision().getDecisions().size(), greaterThan(1));
} else {
// if we are not including YES decisions, then the node holding the primary should have 1 NO decision,
// the other node should have zero NO decisions
assertEquals(nodeHoldingPrimary ? 1 : 0, result.getCanAllocateDecision().getDecisions().size());
}
for (Decision d : result.getCanAllocateDecision().getDecisions()) {
if (d.label().equals("same_shard") && nodeHoldingPrimary) {
assertEquals(Decision.Type.NO, d.type());
assertThat(d.getExplanation(), startsWith("the shard cannot be allocated to the same node on which a copy of the shard already exists"));
} else {
assertEquals(Decision.Type.YES, d.type());
assertNotNull(d.getExplanation());
}
}
}
// verify JSON output
try (XContentParser parser = getParser(explanation)) {
verifyShardInfo(parser, false, includeDiskInfo, ShardRoutingState.UNASSIGNED);
parser.nextToken();
assertEquals("can_allocate", parser.currentName());
parser.nextToken();
assertEquals(AllocationDecision.ALLOCATION_DELAYED.toString(), parser.text());
parser.nextToken();
assertEquals("allocate_explanation", parser.currentName());
parser.nextToken();
assertThat(parser.text(), startsWith("cannot allocate because the cluster is still waiting"));
parser.nextToken();
assertEquals("configured_delay_in_millis", parser.currentName());
parser.nextToken();
assertEquals(60000L, parser.longValue());
parser.nextToken();
assertEquals("remaining_delay_in_millis", parser.currentName());
parser.nextToken();
assertThat(parser.longValue(), greaterThan(0L));
Map<String, AllocationDecision> nodes = new HashMap<>();
nodes.put(primaryNodeName, AllocationDecision.NO);
String[] currentNodes = internalCluster().getNodeNames();
nodes.put(currentNodes[0].equals(primaryNodeName) ? currentNodes[1] : currentNodes[0], AllocationDecision.YES);
verifyNodeDecisions(parser, nodes, includeYesDecisions, true);
assertEquals(Token.END_OBJECT, parser.nextToken());
}
}
use of org.elasticsearch.cluster.routing.allocation.AllocationDecision in project elasticsearch by elastic.
the class ClusterAllocationExplainIT method testUnassignedReplicaWithPriorCopy.
public void testUnassignedReplicaWithPriorCopy() throws Exception {
logger.info("--> starting 3 nodes");
List<String> nodes = internalCluster().startNodes(3);
logger.info("--> creating an index with 1 primary and 1 replica");
createIndexAndIndexData(1, 1);
String primaryNodeName = primaryNodeName();
nodes.remove(primaryNodeName);
logger.info("--> shutting down all nodes except the one that holds the primary");
internalCluster().stopRandomNode(InternalTestCluster.nameFilter(nodes.get(0)));
internalCluster().stopRandomNode(InternalTestCluster.nameFilter(nodes.get(1)));
ensureStableCluster(1);
logger.info("--> setting allocation filtering to only allow allocation on the currently running node");
client().admin().indices().prepareUpdateSettings("idx").setSettings(Settings.builder().put("index.routing.allocation.include._name", primaryNodeName)).get();
logger.info("--> restarting the stopped nodes");
internalCluster().startNode(Settings.builder().put("node.name", nodes.get(0)).build());
internalCluster().startNode(Settings.builder().put("node.name", nodes.get(1)).build());
ensureStableCluster(3);
boolean includeYesDecisions = randomBoolean();
boolean includeDiskInfo = randomBoolean();
ClusterAllocationExplanation explanation = runExplain(false, includeYesDecisions, includeDiskInfo);
ShardId shardId = explanation.getShard();
boolean isPrimary = explanation.isPrimary();
ShardRoutingState shardRoutingState = explanation.getShardState();
DiscoveryNode currentNode = explanation.getCurrentNode();
UnassignedInfo unassignedInfo = explanation.getUnassignedInfo();
ClusterInfo clusterInfo = explanation.getClusterInfo();
AllocateUnassignedDecision allocateDecision = explanation.getShardAllocationDecision().getAllocateDecision();
MoveDecision moveDecision = explanation.getShardAllocationDecision().getMoveDecision();
// verify shard info
assertEquals("idx", shardId.getIndexName());
assertEquals(0, shardId.getId());
assertFalse(isPrimary);
// verify current node info
assertNotEquals(ShardRoutingState.STARTED, shardRoutingState);
assertNull(currentNode);
// verify unassigned info
assertNotNull(unassignedInfo);
assertEquals(Reason.NODE_LEFT, unassignedInfo.getReason());
assertEquals(AllocationStatus.NO_ATTEMPT, unassignedInfo.getLastAllocationStatus());
// verify cluster info
verifyClusterInfo(clusterInfo, includeDiskInfo, 3);
// verify decision objects
assertTrue(allocateDecision.isDecisionTaken());
assertFalse(moveDecision.isDecisionTaken());
AllocationDecision decisionToAllocate = allocateDecision.getAllocationDecision();
assertTrue(decisionToAllocate == AllocationDecision.AWAITING_INFO || decisionToAllocate == AllocationDecision.NO);
if (decisionToAllocate == AllocationDecision.AWAITING_INFO) {
assertEquals("cannot allocate because information about existing shard data is still being retrieved from some of the nodes", allocateDecision.getExplanation());
} else {
assertEquals("cannot allocate because allocation is not permitted to any of the nodes", allocateDecision.getExplanation());
}
assertNull(allocateDecision.getAllocationId());
assertNull(allocateDecision.getTargetNode());
assertEquals(0L, allocateDecision.getConfiguredDelayInMillis());
assertEquals(0L, allocateDecision.getRemainingDelayInMillis());
assertEquals(3, allocateDecision.getNodeDecisions().size());
for (NodeAllocationResult result : allocateDecision.getNodeDecisions()) {
assertNotNull(result.getNode());
boolean nodeHoldingPrimary = result.getNode().getName().equals(primaryNodeName);
assertEquals(AllocationDecision.NO, result.getNodeDecision());
if (includeYesDecisions) {
assertThat(result.getCanAllocateDecision().getDecisions().size(), greaterThan(1));
} else {
assertEquals(1, result.getCanAllocateDecision().getDecisions().size());
}
for (Decision d : result.getCanAllocateDecision().getDecisions()) {
if (d.label().equals("same_shard") && nodeHoldingPrimary) {
assertEquals(Decision.Type.NO, d.type());
assertThat(d.getExplanation(), startsWith("the shard cannot be allocated to the same node on which a copy of the shard already exists"));
} else if (d.label().equals("filter") && nodeHoldingPrimary == false) {
assertEquals(Decision.Type.NO, d.type());
assertEquals("node does not match index setting [index.routing.allocation.include] " + "filters [_name:\"" + primaryNodeName + "\"]", d.getExplanation());
} else {
assertEquals(Decision.Type.YES, d.type());
assertNotNull(d.getExplanation());
}
}
}
// verify JSON output
try (XContentParser parser = getParser(explanation)) {
verifyShardInfo(parser, false, includeDiskInfo, ShardRoutingState.UNASSIGNED);
parser.nextToken();
assertEquals("can_allocate", parser.currentName());
parser.nextToken();
String allocationDecision = parser.text();
assertTrue(allocationDecision.equals(AllocationDecision.NO.toString()) || allocationDecision.equals(AllocationDecision.AWAITING_INFO.toString()));
parser.nextToken();
assertEquals("allocate_explanation", parser.currentName());
parser.nextToken();
if (allocationDecision.equals("awaiting_info")) {
assertEquals("cannot allocate because information about existing shard data is still being retrieved " + "from some of the nodes", parser.text());
} else {
assertEquals("cannot allocate because allocation is not permitted to any of the nodes", parser.text());
}
Map<String, AllocationDecision> nodeDecisions = new HashMap<>();
for (String nodeName : internalCluster().getNodeNames()) {
nodeDecisions.put(nodeName, AllocationDecision.NO);
}
verifyNodeDecisions(parser, nodeDecisions, includeYesDecisions, true);
assertEquals(Token.END_OBJECT, parser.nextToken());
}
}
use of org.elasticsearch.cluster.routing.allocation.AllocationDecision in project elasticsearch by elastic.
the class ClusterAllocationExplainIT method verifyNodeDecisions.
private void verifyNodeDecisions(XContentParser parser, Map<String, AllocationDecision> expectedNodeDecisions, boolean includeYesDecisions, boolean reuseStore) throws IOException {
parser.nextToken();
assertEquals("node_allocation_decisions", parser.currentName());
assertEquals(Token.START_ARRAY, parser.nextToken());
boolean encounteredNo = false;
final int numNodes = expectedNodeDecisions.size();
for (int i = 0; i < numNodes; i++) {
String nodeName = verifyNodeDecisionPrologue(parser);
AllocationDecision allocationDecision = expectedNodeDecisions.get(nodeName);
assertEquals(allocationDecision.toString(), parser.text());
if (allocationDecision != AllocationDecision.YES) {
encounteredNo = true;
} else {
assertFalse("encountered a YES node decision after a NO node decision - sort order is wrong", encounteredNo);
}
parser.nextToken();
if ("store".equals(parser.currentName())) {
assertTrue("store info should not be present", reuseStore);
assertEquals(Token.START_OBJECT, parser.nextToken());
parser.nextToken();
assertEquals("matching_size_in_bytes", parser.currentName());
parser.nextToken();
assertThat(parser.longValue(), greaterThan(0L));
assertEquals(Token.END_OBJECT, parser.nextToken());
parser.nextToken();
}
if (reuseStore == false) {
assertEquals("weight_ranking", parser.currentName());
parser.nextToken();
assertThat(parser.intValue(), greaterThan(0));
parser.nextToken();
}
if (allocationDecision == AllocationDecision.NO || allocationDecision == AllocationDecision.THROTTLED || includeYesDecisions) {
assertEquals("deciders", parser.currentName());
boolean atLeastOneMatchingDecisionFound = verifyDeciders(parser, allocationDecision);
parser.nextToken();
if (allocationDecision == AllocationDecision.NO || allocationDecision == AllocationDecision.THROTTLED) {
assertTrue("decision was " + allocationDecision + " but found no node's with that decision", atLeastOneMatchingDecisionFound);
}
}
assertEquals(Token.END_OBJECT, parser.currentToken());
}
assertEquals(Token.END_ARRAY, parser.nextToken());
}
use of org.elasticsearch.cluster.routing.allocation.AllocationDecision in project elasticsearch by elastic.
the class ClusterAllocationExplainIT method testAllocationFilteringOnIndexCreation.
public void testAllocationFilteringOnIndexCreation() throws Exception {
logger.info("--> starting 2 nodes");
internalCluster().startNodes(2);
logger.info("--> creating an index with 1 primary, 0 replicas, with allocation filtering so the primary can't be assigned");
createIndexAndIndexData(1, 0, Settings.builder().put("index.routing.allocation.include._name", "non_existent_node").build(), ActiveShardCount.NONE);
boolean includeYesDecisions = randomBoolean();
boolean includeDiskInfo = randomBoolean();
ClusterAllocationExplanation explanation = runExplain(true, includeYesDecisions, includeDiskInfo);
ShardId shardId = explanation.getShard();
boolean isPrimary = explanation.isPrimary();
ShardRoutingState shardRoutingState = explanation.getShardState();
DiscoveryNode currentNode = explanation.getCurrentNode();
UnassignedInfo unassignedInfo = explanation.getUnassignedInfo();
ClusterInfo clusterInfo = explanation.getClusterInfo();
AllocateUnassignedDecision allocateDecision = explanation.getShardAllocationDecision().getAllocateDecision();
MoveDecision moveDecision = explanation.getShardAllocationDecision().getMoveDecision();
// verify shard info
assertEquals("idx", shardId.getIndexName());
assertEquals(0, shardId.getId());
assertTrue(isPrimary);
// verify current node info
assertNotEquals(ShardRoutingState.STARTED, shardRoutingState);
assertNull(currentNode);
// verify unassigned info
assertNotNull(unassignedInfo);
assertEquals(Reason.INDEX_CREATED, unassignedInfo.getReason());
assertEquals(AllocationStatus.DECIDERS_NO, unassignedInfo.getLastAllocationStatus());
// verify cluster info
verifyClusterInfo(clusterInfo, includeDiskInfo, 2);
// verify decision objects
assertTrue(allocateDecision.isDecisionTaken());
assertFalse(moveDecision.isDecisionTaken());
assertEquals(AllocationDecision.NO, allocateDecision.getAllocationDecision());
assertEquals("cannot allocate because allocation is not permitted to any of the nodes", allocateDecision.getExplanation());
assertNull(allocateDecision.getAllocationId());
assertNull(allocateDecision.getTargetNode());
assertEquals(0L, allocateDecision.getConfiguredDelayInMillis());
assertEquals(0L, allocateDecision.getRemainingDelayInMillis());
assertEquals(2, allocateDecision.getNodeDecisions().size());
for (NodeAllocationResult result : allocateDecision.getNodeDecisions()) {
assertNotNull(result.getNode());
assertEquals(AllocationDecision.NO, result.getNodeDecision());
if (includeYesDecisions) {
assertThat(result.getCanAllocateDecision().getDecisions().size(), greaterThan(1));
} else {
assertEquals(1, result.getCanAllocateDecision().getDecisions().size());
}
for (Decision d : result.getCanAllocateDecision().getDecisions()) {
if (d.label().equals("filter")) {
assertEquals(Decision.Type.NO, d.type());
assertEquals("node does not match index setting [index.routing.allocation.include] filters " + "[_name:\"non_existent_node\"]", d.getExplanation());
}
}
}
// verify JSON output
try (XContentParser parser = getParser(explanation)) {
verifyShardInfo(parser, true, includeDiskInfo, ShardRoutingState.UNASSIGNED);
parser.nextToken();
assertEquals("can_allocate", parser.currentName());
parser.nextToken();
String allocationDecision = parser.text();
assertTrue(allocationDecision.equals(AllocationDecision.NO.toString()) || allocationDecision.equals(AllocationDecision.AWAITING_INFO.toString()));
parser.nextToken();
assertEquals("allocate_explanation", parser.currentName());
parser.nextToken();
if (allocationDecision.equals("awaiting_info")) {
assertEquals("cannot allocate because information about existing shard data is still being retrieved " + "from some of the nodes", parser.text());
} else {
assertEquals("cannot allocate because allocation is not permitted to any of the nodes", parser.text());
}
Map<String, AllocationDecision> nodeDecisions = new HashMap<>();
for (String nodeName : internalCluster().getNodeNames()) {
nodeDecisions.put(nodeName, AllocationDecision.NO);
}
verifyNodeDecisions(parser, nodeDecisions, includeYesDecisions, false);
assertEquals(Token.END_OBJECT, parser.nextToken());
}
}
use of org.elasticsearch.cluster.routing.allocation.AllocationDecision in project elasticsearch by elastic.
the class ClusterAllocationExplainIT method allNodeDecisions.
private Map<String, AllocationDecision> allNodeDecisions(AllocationDecision allocationDecision, boolean removePrimary) {
Map<String, AllocationDecision> nodeDecisions = new HashMap<>();
Set<String> allNodes = Sets.newHashSet(internalCluster().getNodeNames());
allNodes.remove(removePrimary ? primaryNodeName() : replicaNode().getName());
for (String nodeName : allNodes) {
nodeDecisions.put(nodeName, allocationDecision);
}
return nodeDecisions;
}
Aggregations