Throw an ElasticsearchIllegalArgumentException when allocating on a non-data node.
Today, it would fail with a NullPointerException. Close #4833
This commit is contained in:
parent
510fbf12c3
commit
92a026b3b9
|
@ -21,6 +21,7 @@ package org.elasticsearch.cluster.routing.allocation.command;
|
|||
|
||||
import org.elasticsearch.ElasticsearchException;
|
||||
import org.elasticsearch.ElasticsearchIllegalArgumentException;
|
||||
import org.elasticsearch.ElasticsearchIllegalStateException;
|
||||
import org.elasticsearch.ElasticsearchParseException;
|
||||
import org.elasticsearch.cluster.node.DiscoveryNode;
|
||||
import org.elasticsearch.cluster.routing.MutableShardRouting;
|
||||
|
@ -183,6 +184,14 @@ public class AllocateAllocationCommand implements AllocationCommand {
|
|||
}
|
||||
|
||||
RoutingNode routingNode = allocation.routingNodes().node(discoNode.id());
|
||||
if (routingNode == null) {
|
||||
if (!discoNode.dataNode()) {
|
||||
throw new ElasticsearchIllegalArgumentException("Allocation can only be done on data nodes, not [" + node + "]");
|
||||
} else {
|
||||
throw new ElasticsearchIllegalStateException("Could not find [" + node + "] among the routing nodes");
|
||||
}
|
||||
}
|
||||
|
||||
Decision decision = allocation.deciders().canAllocate(shardRouting, routingNode, allocation);
|
||||
if (decision.type() == Decision.Type.NO) {
|
||||
throw new ElasticsearchIllegalArgumentException("[allocate] allocation of " + shardId + " on node " + discoNode + " is not allowed, reason: " + decision);
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
|
||||
package org.elasticsearch.cluster.routing.allocation;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import org.elasticsearch.ElasticsearchIllegalArgumentException;
|
||||
import org.elasticsearch.cluster.ClusterState;
|
||||
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||
|
@ -117,6 +118,7 @@ public class AllocationCommandsTests extends ElasticsearchAllocationTestCase {
|
|||
.put(newNode("node1"))
|
||||
.put(newNode("node2"))
|
||||
.put(newNode("node3"))
|
||||
.put(newNode("node4", ImmutableMap.of("data", Boolean.FALSE.toString())))
|
||||
).build();
|
||||
RoutingAllocation.Result rerouteResult = allocation.reroute(clusterState);
|
||||
clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build();
|
||||
|
@ -129,6 +131,13 @@ public class AllocationCommandsTests extends ElasticsearchAllocationTestCase {
|
|||
} catch (ElasticsearchIllegalArgumentException e) {
|
||||
}
|
||||
|
||||
logger.info("--> allocating to non-data node, should fail");
|
||||
try {
|
||||
rerouteResult = allocation.reroute(clusterState, new AllocationCommands(new AllocateAllocationCommand(new ShardId("test", 0), "node4", true)));
|
||||
fail();
|
||||
} catch (ElasticsearchIllegalArgumentException e) {
|
||||
}
|
||||
|
||||
logger.info("--> allocating with primary flag set to true");
|
||||
rerouteResult = allocation.reroute(clusterState, new AllocationCommands(new AllocateAllocationCommand(new ShardId("test", 0), "node1", true)));
|
||||
assertThat(rerouteResult.changed(), equalTo(true));
|
||||
|
|
Loading…
Reference in New Issue