-
Notifications
You must be signed in to change notification settings - Fork 24.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add check when trying to reroute a shard to a non-data discovery node #28886
Changes from 3 commits
2eef691
99d0c6d
0226fb7
9ae73e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,12 +21,15 @@ | |
|
||
import org.apache.logging.log4j.Logger; | ||
import org.elasticsearch.Version; | ||
import org.elasticsearch.cluster.ClusterInfo; | ||
import org.elasticsearch.cluster.ClusterName; | ||
import org.elasticsearch.cluster.ClusterState; | ||
import org.elasticsearch.cluster.ESAllocationTestCase; | ||
import org.elasticsearch.cluster.metadata.IndexMetaData; | ||
import org.elasticsearch.cluster.metadata.MetaData; | ||
import org.elasticsearch.cluster.node.DiscoveryNode; | ||
import org.elasticsearch.cluster.node.DiscoveryNodes; | ||
import org.elasticsearch.cluster.routing.RoutingNodes; | ||
import org.elasticsearch.cluster.routing.RoutingTable; | ||
import org.elasticsearch.cluster.routing.ShardRoutingState; | ||
import org.elasticsearch.cluster.routing.allocation.command.AbstractAllocateAllocationCommand; | ||
|
@@ -36,6 +39,7 @@ | |
import org.elasticsearch.cluster.routing.allocation.command.AllocationCommands; | ||
import org.elasticsearch.cluster.routing.allocation.command.CancelAllocationCommand; | ||
import org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand; | ||
import org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders; | ||
import org.elasticsearch.cluster.routing.allocation.decider.EnableAllocationDecider; | ||
import org.elasticsearch.common.io.stream.BytesStreamOutput; | ||
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput; | ||
|
@@ -47,12 +51,16 @@ | |
import org.elasticsearch.common.xcontent.NamedXContentRegistry; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
import org.elasticsearch.common.xcontent.json.JsonXContent; | ||
import org.elasticsearch.index.Index; | ||
import org.elasticsearch.index.IndexNotFoundException; | ||
import org.elasticsearch.index.shard.ShardId; | ||
import org.elasticsearch.index.shard.ShardNotFoundException; | ||
|
||
import java.util.Collections; | ||
import java.util.EnumSet; | ||
import java.util.HashSet; | ||
|
||
import static java.util.Collections.emptyMap; | ||
import static java.util.Collections.singleton; | ||
import static org.elasticsearch.cluster.routing.ShardRoutingState.INITIALIZING; | ||
import static org.elasticsearch.cluster.routing.ShardRoutingState.RELOCATING; | ||
|
@@ -520,4 +528,70 @@ public void testXContent() throws Exception { | |
protected NamedXContentRegistry xContentRegistry() { | ||
return new NamedXContentRegistry(NetworkModule.getNamedXContents()); | ||
} | ||
|
||
public void testMoveShardToNonDataNode() { | ||
AllocationService allocation = createAllocationService(Settings.builder().put("cluster.routing.allocation.node_concurrent_recoveries", 10).build()); | ||
|
||
logger.info("creating an index with 1 shard, no replica"); | ||
MetaData metaData = MetaData.builder() | ||
.put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(0)) | ||
.build(); | ||
RoutingTable routingTable = RoutingTable.builder() | ||
.addAsNew(metaData.index("test")) | ||
.build(); | ||
ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); | ||
|
||
logger.info("--> adding two nodes"); | ||
|
||
clusterState = ClusterState.builder(clusterState).nodes( | ||
DiscoveryNodes.builder() | ||
.add(new DiscoveryNode("node1", "node1", "node1", "test1", "test1", buildNewFakeTransportAddress(), emptyMap(), | ||
MASTER_DATA_ROLES, Version.CURRENT)) | ||
.add(new DiscoveryNode("node2", "node2", "node2", "test2", "test2", buildNewFakeTransportAddress(), emptyMap(), | ||
new HashSet<>(randomSubsetOf(EnumSet.of(DiscoveryNode.Role.MASTER, DiscoveryNode.Role.INGEST))), Version.CURRENT))).build(); | ||
|
||
logger.info("start primary shard"); | ||
clusterState = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); | ||
|
||
Index index = clusterState.getMetaData().index("test").getIndex(); | ||
MoveAllocationCommand command = new MoveAllocationCommand(index.getName(), 0, "node1", "node2"); | ||
RoutingAllocation routingAllocation = new RoutingAllocation(new AllocationDeciders(Settings.EMPTY, Collections.emptyList()), | ||
new RoutingNodes(clusterState, false), clusterState, ClusterInfo.EMPTY, System.nanoTime()); | ||
logger.info("--> executing move allocation command to non-data node"); | ||
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> command.execute(routingAllocation, false)); | ||
assertEquals("[move_allocation] can't move 0 from node1 to node2: node2 is not a data node.", e.getMessage()); | ||
} | ||
|
||
public void testMoveShardFromNonDataNode() { | ||
AllocationService allocation = createAllocationService(Settings.builder().put("cluster.routing.allocation.node_concurrent_recoveries", 10).build()); | ||
|
||
logger.info("creating an index with 1 shard, no replica"); | ||
MetaData metaData = MetaData.builder() | ||
.put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(0)) | ||
.build(); | ||
RoutingTable routingTable = RoutingTable.builder() | ||
.addAsNew(metaData.index("test")) | ||
.build(); | ||
ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); | ||
|
||
logger.info("--> adding two nodes"); | ||
|
||
clusterState = ClusterState.builder(clusterState).nodes( | ||
DiscoveryNodes.builder() | ||
.add(new DiscoveryNode("node1", "node1", "node1", "test1", "test1", buildNewFakeTransportAddress(), emptyMap(), | ||
MASTER_DATA_ROLES, Version.CURRENT)) | ||
.add(new DiscoveryNode("node2", "node2", "node2", "test2", "test2", buildNewFakeTransportAddress(), emptyMap(), | ||
new HashSet<>(randomSubsetOf(EnumSet.of(DiscoveryNode.Role.MASTER, DiscoveryNode.Role.INGEST))), Version.CURRENT))).build(); | ||
|
||
logger.info("start primary shard"); | ||
clusterState = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); | ||
|
||
Index index = clusterState.getMetaData().index("test").getIndex(); | ||
MoveAllocationCommand command = new MoveAllocationCommand(index.getName(), 0, "node2", "node1"); | ||
RoutingAllocation routingAllocation = new RoutingAllocation(new AllocationDeciders(Settings.EMPTY, Collections.emptyList()), | ||
new RoutingNodes(clusterState, false), clusterState, ClusterInfo.EMPTY, System.nanoTime()); | ||
logger.info("--> executing move allocation command from non-data node"); | ||
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> command.execute(routingAllocation, false)); | ||
assertEquals("[move_allocation] can't move 0 from node2 to node1: node2 is not a data node.", e.getMessage()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Excellent, thanks. I'm glad you did that, because it shows that this message isn't what I was expecting. The shard number on its own is likely to cause confusion, and also it's useful to have the full Something like this'd be great: |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you wrap these lines to <120 characters? We're trying to cut down on overlong lines.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure thing!