-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Short-circuit rebalancing when disabled #40942
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
Merged
DaveCTurner
merged 7 commits into
elastic:master
from
DaveCTurner:2019-04-08-short-circuit-allocation-when-disabled
Apr 8, 2019
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f90200a
Short-circuit rebalancing when disabled
DaveCTurner 149ff95
Moar static
DaveCTurner 7eb4190
EVEN MOAR STATIC
DaveCTurner 84e14c3
No need to check each index if we're going to answer YES anyway
DaveCTurner 003faab
Unnecessary 'this.'
DaveCTurner 7491b9b
Merge branch 'master' into 2019-04-08-short-circuit-allocation-when-d…
DaveCTurner 0c5c04b
Skip rebalancing if disabled and explicitly disabled on some indices too
DaveCTurner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
233 changes: 233 additions & 0 deletions
233
...g/elasticsearch/cluster/routing/allocation/decider/EnableAllocationShortCircuitTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,233 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.elasticsearch.cluster.routing.allocation.decider; | ||
|
||
import org.elasticsearch.Version; | ||
import org.elasticsearch.cluster.ClusterModule; | ||
import org.elasticsearch.cluster.ClusterName; | ||
import org.elasticsearch.cluster.ClusterState; | ||
import org.elasticsearch.cluster.ESAllocationTestCase; | ||
import org.elasticsearch.cluster.EmptyClusterInfoService; | ||
import org.elasticsearch.cluster.metadata.IndexMetaData; | ||
import org.elasticsearch.cluster.metadata.MetaData; | ||
import org.elasticsearch.cluster.node.DiscoveryNodes; | ||
import org.elasticsearch.cluster.routing.RoutingNode; | ||
import org.elasticsearch.cluster.routing.RoutingTable; | ||
import org.elasticsearch.cluster.routing.ShardRouting; | ||
import org.elasticsearch.cluster.routing.ShardRoutingState; | ||
import org.elasticsearch.cluster.routing.allocation.AllocationService; | ||
import org.elasticsearch.cluster.routing.allocation.RoutingAllocation; | ||
import org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator; | ||
import org.elasticsearch.common.settings.ClusterSettings; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.plugins.ClusterPlugin; | ||
import org.elasticsearch.test.gateway.TestGatewayAllocator; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import static org.elasticsearch.cluster.routing.allocation.decider.EnableAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ENABLE_SETTING; | ||
import static org.elasticsearch.cluster.routing.allocation.decider.EnableAllocationDecider.CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING; | ||
import static org.elasticsearch.cluster.routing.allocation.decider.EnableAllocationDecider.INDEX_ROUTING_REBALANCE_ENABLE_SETTING; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.greaterThan; | ||
|
||
public class EnableAllocationShortCircuitTests extends ESAllocationTestCase { | ||
|
||
private static ClusterState createClusterStateWithAllShardsAssigned() { | ||
AllocationService allocationService = createAllocationService(Settings.EMPTY); | ||
|
||
final int numberOfNodes = randomIntBetween(1, 5); | ||
final DiscoveryNodes.Builder discoveryNodesBuilder = DiscoveryNodes.builder(); | ||
for (int i = 0; i < numberOfNodes; i++) { | ||
discoveryNodesBuilder.add(newNode("node" + i)); | ||
} | ||
|
||
final MetaData.Builder metadataBuilder = MetaData.builder(); | ||
final RoutingTable.Builder routingTableBuilder = RoutingTable.builder(); | ||
for (int i = randomIntBetween(1, 10); i >= 0; i--) { | ||
final IndexMetaData indexMetaData = IndexMetaData.builder("test" + i).settings(settings(Version.CURRENT)) | ||
.numberOfShards(1).numberOfReplicas(randomIntBetween(0, numberOfNodes - 1)).build(); | ||
metadataBuilder.put(indexMetaData, true); | ||
routingTableBuilder.addAsNew(indexMetaData); | ||
} | ||
|
||
ClusterState clusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.get(Settings.EMPTY)) | ||
.nodes(discoveryNodesBuilder).metaData(metadataBuilder).routingTable(routingTableBuilder.build()).build(); | ||
|
||
while (clusterState.getRoutingNodes().hasUnassignedShards() | ||
|| clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.INITIALIZING).isEmpty() == false) { | ||
clusterState = allocationService.applyStartedShards(clusterState, | ||
clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.INITIALIZING)); | ||
clusterState = allocationService.reroute(clusterState, "reroute"); | ||
} | ||
|
||
return clusterState; | ||
} | ||
|
||
public void testRebalancingAttemptedIfPermitted() { | ||
ClusterState clusterState = createClusterStateWithAllShardsAssigned(); | ||
|
||
final RebalanceShortCircuitPlugin plugin = new RebalanceShortCircuitPlugin(); | ||
AllocationService allocationService = createAllocationService(Settings.builder() | ||
.put(CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), | ||
randomFrom(EnableAllocationDecider.Allocation.ALL, | ||
EnableAllocationDecider.Allocation.NEW_PRIMARIES, | ||
EnableAllocationDecider.Allocation.PRIMARIES).name()), | ||
plugin); | ||
allocationService.reroute(clusterState, "reroute").routingTable(); | ||
assertThat(plugin.rebalanceAttempts, greaterThan(0)); | ||
} | ||
|
||
public void testRebalancingSkippedIfDisabled() { | ||
ClusterState clusterState = createClusterStateWithAllShardsAssigned(); | ||
|
||
final RebalanceShortCircuitPlugin plugin = new RebalanceShortCircuitPlugin(); | ||
AllocationService allocationService = createAllocationService(Settings.builder() | ||
.put(CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), EnableAllocationDecider.Allocation.NONE.name()), | ||
plugin); | ||
allocationService.reroute(clusterState, "reroute").routingTable(); | ||
assertThat(plugin.rebalanceAttempts, equalTo(0)); | ||
} | ||
|
||
public void testRebalancingSkippedIfDisabledIncludingOnSpecificIndices() { | ||
ClusterState clusterState = createClusterStateWithAllShardsAssigned(); | ||
final IndexMetaData indexMetaData = randomFrom(clusterState.metaData().indices().values().toArray(IndexMetaData.class)); | ||
clusterState = ClusterState.builder(clusterState).metaData(MetaData.builder(clusterState.metaData()) | ||
.put(IndexMetaData.builder(indexMetaData).settings(Settings.builder().put(indexMetaData.getSettings()) | ||
.put(INDEX_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), EnableAllocationDecider.Rebalance.NONE.name()))).build()).build(); | ||
|
||
final RebalanceShortCircuitPlugin plugin = new RebalanceShortCircuitPlugin(); | ||
AllocationService allocationService = createAllocationService(Settings.builder() | ||
.put(CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), EnableAllocationDecider.Rebalance.NONE.name()), | ||
plugin); | ||
allocationService.reroute(clusterState, "reroute").routingTable(); | ||
assertThat(plugin.rebalanceAttempts, equalTo(0)); | ||
} | ||
|
||
public void testRebalancingAttemptedIfDisabledButOverridenOnSpecificIndices() { | ||
ClusterState clusterState = createClusterStateWithAllShardsAssigned(); | ||
final IndexMetaData indexMetaData = randomFrom(clusterState.metaData().indices().values().toArray(IndexMetaData.class)); | ||
clusterState = ClusterState.builder(clusterState).metaData(MetaData.builder(clusterState.metaData()) | ||
.put(IndexMetaData.builder(indexMetaData).settings(Settings.builder().put(indexMetaData.getSettings()) | ||
.put(INDEX_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), | ||
randomFrom(EnableAllocationDecider.Allocation.ALL, | ||
EnableAllocationDecider.Allocation.NEW_PRIMARIES, | ||
EnableAllocationDecider.Allocation.PRIMARIES).name()))).build()).build(); | ||
|
||
final RebalanceShortCircuitPlugin plugin = new RebalanceShortCircuitPlugin(); | ||
AllocationService allocationService = createAllocationService(Settings.builder() | ||
.put(CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), EnableAllocationDecider.Rebalance.NONE.name()), | ||
plugin); | ||
allocationService.reroute(clusterState, "reroute").routingTable(); | ||
assertThat(plugin.rebalanceAttempts, greaterThan(0)); | ||
} | ||
|
||
public void testAllocationSkippedIfDisabled() { | ||
final AllocateShortCircuitPlugin plugin = new AllocateShortCircuitPlugin(); | ||
AllocationService allocationService = createAllocationService(Settings.builder() | ||
.put(CLUSTER_ROUTING_ALLOCATION_ENABLE_SETTING.getKey(), EnableAllocationDecider.Allocation.NONE.name()), | ||
plugin); | ||
|
||
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(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)) | ||
.metaData(metaData).routingTable(routingTable).nodes(DiscoveryNodes.builder().add(newNode("node1"))).build(); | ||
|
||
allocationService.reroute(clusterState, "reroute").routingTable(); | ||
assertThat(plugin.canAllocateAttempts, equalTo(0)); | ||
} | ||
|
||
private static AllocationService createAllocationService(Settings.Builder settings, ClusterPlugin plugin) { | ||
final ClusterSettings emptyClusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS); | ||
List<AllocationDecider> deciders = new ArrayList<>(ClusterModule.createAllocationDeciders(settings.build(), emptyClusterSettings, | ||
Collections.singletonList(plugin))); | ||
return new MockAllocationService( | ||
new AllocationDeciders(deciders), | ||
new TestGatewayAllocator(), new BalancedShardsAllocator(Settings.EMPTY), EmptyClusterInfoService.INSTANCE); | ||
} | ||
|
||
private static class RebalanceShortCircuitPlugin implements ClusterPlugin { | ||
int rebalanceAttempts; | ||
|
||
@Override | ||
public Collection<AllocationDecider> createAllocationDeciders(Settings settings, ClusterSettings clusterSettings) { | ||
return Collections.singletonList(new RebalanceShortCircuitAllocationDecider()); | ||
} | ||
|
||
private class RebalanceShortCircuitAllocationDecider extends AllocationDecider { | ||
|
||
@Override | ||
public Decision canRebalance(ShardRouting shardRouting, RoutingAllocation allocation) { | ||
rebalanceAttempts++; | ||
return super.canRebalance(shardRouting, allocation); | ||
} | ||
|
||
@Override | ||
public Decision canRebalance(RoutingAllocation allocation) { | ||
rebalanceAttempts++; | ||
return super.canRebalance(allocation); | ||
} | ||
} | ||
} | ||
|
||
private static class AllocateShortCircuitPlugin implements ClusterPlugin { | ||
int canAllocateAttempts; | ||
|
||
@Override | ||
public Collection<AllocationDecider> createAllocationDeciders(Settings settings, ClusterSettings clusterSettings) { | ||
return Collections.singletonList(new AllocateShortCircuitAllocationDecider()); | ||
} | ||
|
||
private class AllocateShortCircuitAllocationDecider extends AllocationDecider { | ||
|
||
@Override | ||
public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) { | ||
canAllocateAttempts++; | ||
return super.canAllocate(shardRouting, node, allocation); | ||
} | ||
|
||
@Override | ||
public Decision canAllocate(ShardRouting shardRouting, RoutingAllocation allocation) { | ||
canAllocateAttempts++; | ||
return super.canAllocate(shardRouting, allocation); | ||
} | ||
|
||
@Override | ||
public Decision canAllocate(IndexMetaData indexMetaData, RoutingNode node, RoutingAllocation allocation) { | ||
canAllocateAttempts++; | ||
return super.canAllocate(indexMetaData, node, allocation); | ||
} | ||
|
||
@Override | ||
public Decision canAllocate(RoutingNode node, RoutingAllocation allocation) { | ||
canAllocateAttempts++; | ||
return super.canAllocate(node, allocation); | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think it makes sense to also add some indices in this test case?
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.
Sort of, although then we have to allocate their shards or else the
ClusterRebalanceAllocationDecider
steps in and prevents rebalancing. I expanded the tests to do this.