Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,13 @@ public static ShardId selectSplitShard(int shardId, IndexMetaData sourceIndexMet
+ "] must be less that the number of target shards [" + numTargetShards + "]");
}
int routingFactor = getRoutingFactor(numSourceShards, numTargetShards);
// now we verify that the numRoutingShards is valid in the source index
int routingNumShards = sourceIndexMetadata.getRoutingNumShards();
int factor = routingNumShards / numTargetShards;
if (factor * numTargetShards != routingNumShards) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the division then multiplication again looks like you actually want to check that routingNumShards % numTargetShards != 0?

throw new IllegalStateException("the number of routingShards ["
+ routingNumShards + "] must be a multiple of the target shards [" + numTargetShards + "]");
}
// this is just an additional assertion that ensures we are a factor of the routing num shards.
assert getRoutingFactor(numTargetShards, sourceIndexMetadata.getRoutingNumShards()) >= 0;
return new ShardId(sourceIndexMetadata.getIndex(), shardId/routingFactor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,16 @@ public void testSelectShrinkShards() {
}

public void testSelectResizeShards() {
int numTargetShards = randomFrom(4, 6, 8, 12);

IndexMetaData split = IndexMetaData.builder("foo")
.settings(Settings.builder()
.put("index.version.created", 1)
.put("index.number_of_shards", 2)
.put("index.number_of_replicas", 0)
.build())
.creationDate(randomLong())
.setRoutingNumShards(numTargetShards * 2)
.build();

IndexMetaData shrink = IndexMetaData.builder("foo")
Expand All @@ -135,7 +138,6 @@ public void testSelectResizeShards() {
.build())
.creationDate(randomLong())
.build();
int numTargetShards = randomFrom(4, 6, 8, 12);
int shard = randomIntBetween(0, numTargetShards-1);
assertEquals(Collections.singleton(IndexMetaData.selectSplitShard(shard, split, numTargetShards)),
IndexMetaData.selectRecoverFromShards(shard, split, numTargetShards));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@
public class MetaDataCreateIndexServiceTests extends ESTestCase {

private ClusterState createClusterState(String name, int numShards, int numReplicas, Settings settings) {
int numRoutingShards = settings.getAsInt(IndexMetaData.INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING.getKey(), numShards);
MetaData.Builder metaBuilder = MetaData.builder();
IndexMetaData indexMetaData = IndexMetaData.builder(name).settings(settings(Version.CURRENT)
.put(settings))
.numberOfShards(numShards).numberOfReplicas(numReplicas).build();
.numberOfShards(numShards).numberOfReplicas(numReplicas)
.setRoutingNumShards(numRoutingShards).build();
metaBuilder.put(indexMetaData, false);
MetaData metaData = metaBuilder.build();
RoutingTable.Builder routingTableBuilder = RoutingTable.builder();
Expand Down Expand Up @@ -204,10 +206,13 @@ public void testValidateSplitIndex() {
}
).getMessage());


int targetShards;
do {
targetShards = randomIntBetween(numShards+1, 100);
} while (isSplitable(numShards, targetShards) == false);
ClusterState clusterState = ClusterState.builder(createClusterState("source", numShards, 0,
Settings.builder().put("index.blocks.write", true).build())).nodes(DiscoveryNodes.builder().add(newNode("node1")))
.build();
Settings.builder().put("index.blocks.write", true).put("index.number_of_routing_shards", targetShards).build()))
.nodes(DiscoveryNodes.builder().add(newNode("node1"))).build();
AllocationService service = new AllocationService(Settings.builder().build(), new AllocationDeciders(Settings.EMPTY,
Collections.singleton(new MaxRetryAllocationDecider(Settings.EMPTY))),
new TestGatewayAllocator(), new BalancedShardsAllocator(Settings.EMPTY), EmptyClusterInfoService.INSTANCE);
Expand All @@ -218,10 +223,7 @@ public void testValidateSplitIndex() {
routingTable = service.applyStartedShards(clusterState,
routingTable.index("source").shardsWithState(ShardRoutingState.INITIALIZING)).routingTable();
clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
int targetShards;
do {
targetShards = randomIntBetween(numShards+1, 100);
} while (isSplitable(numShards, targetShards) == false);

MetaDataCreateIndexService.validateSplitIndex(clusterState, "source", Collections.emptySet(), "target",
Settings.builder().put("index.number_of_shards", targetShards).build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,32 @@
- match: { _id: "3" }
- match: { _source: { foo: "hello world 3" } }

# try to do an illegal split with number_of_routing_shards set
- do:
catch: /illegal_argument_exception/
indices.split:
index: "source"
target: "target"
wait_for_active_shards: 1
master_timeout: 10s
body:
settings:
index.number_of_replicas: 0
index.number_of_shards: 2
index.number_of_routing_shards: 4

# try to do an illegal split with illegal number_of_shards
- do:
catch: /illegal_state_exception/
indices.split:
index: "source"
target: "target"
wait_for_active_shards: 1
master_timeout: 10s
body:
settings:
index.number_of_replicas: 0
index.number_of_shards: 3