Skip to content

Commit

Permalink
Randomize AllocationDecider order in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
s1monw committed Dec 17, 2013
1 parent 79ab05c commit a4f97be
Show file tree
Hide file tree
Showing 42 changed files with 284 additions and 250 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@
import org.elasticsearch.cluster.routing.allocation.decider.Decision;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.node.settings.NodeSettingsService;

import java.util.ArrayList;
import java.util.Iterator;
Expand All @@ -57,16 +55,6 @@ public class AllocationService extends AbstractComponent {
private final ClusterInfoService clusterInfoService;
private final ShardsAllocators shardsAllocators;

public AllocationService() {
this(ImmutableSettings.Builder.EMPTY_SETTINGS);
}

public AllocationService(Settings settings) {
this(settings,
new AllocationDeciders(settings, new NodeSettingsService(ImmutableSettings.Builder.EMPTY_SETTINGS)),
new ShardsAllocators(settings), ClusterInfoService.EMPTY);
}

@Inject
public AllocationService(Settings settings, AllocationDeciders allocationDeciders, ShardsAllocators shardsAllocators, ClusterInfoService clusterInfoService) {
super(settings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@

package org.elasticsearch.cluster.routing.allocation.decider;

import com.google.common.collect.ImmutableSet;
import org.elasticsearch.cluster.routing.RoutingNode;
import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.cluster.routing.allocation.RoutingAllocation;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.node.settings.NodeSettingsService;

import java.util.Set;

Expand All @@ -37,59 +35,14 @@ public class AllocationDeciders extends AllocationDecider {

private final AllocationDecider[] allocations;

/**
* Create a new {@link AllocationDeciders} instance. The different deciders
* should be added in order, as looping over them will stop when the first
* return a {@link Decision#THROTTLE} or {@link Decision#NO}. For performance
* reasons, those more likely to return either of these, and those with
* cheap execution should be executed first.
*
* Performance characteristics:
* {@link ConcurrentRebalanceAllocationDecider} numerical comparison of a counter in {@link org.elasticsearch.cluster.routing.RoutingNodes},
* constant performance, likely to be triggered.
* {@link DisableAllocationDecider} lookup of setting. Constant performance, not as
* likely to be triggered.
* {@link ClusterRebalanceAllocationDecider} checks for unassigned primaries, inactive primaries and
* a rebalance already happening in replica set.
* {@link DiskThresholdDecider} one numerical comparison per node in cluster.
* {@link SnapshotInProgressAllocationDecider} status lookup, unlikely.
* {@link FilterAllocationDecider} checks all allocation include/exclude filters in the cluster against the
* node's attributes.
* {@link RebalanceOnlyWhenActiveAllocationDecider} checks if all shards are active.
* {@link ReplicaAfterPrimaryActiveAllocationDecider} finds primary in replica set, checks whether it
* is started.
* {@link ShardsLimitAllocationDecider} loops over shards allocated on a node, filters out non-relocating
* shards of the same index to do a count comparison.
* {@link AwarenessAllocationDecider} loops over all shards in cluster.
* {@link SameShardAllocationDecider} loops over shards on node.
* {@link ThrottlingAllocationDecider} checks primaries initializing (looping over shards on node) for a primary
* to be allocated, for replicas loops over all shards on node.
*
* @param settings settings to use
* @param nodeSettingsService per-node settings to use
*/
public AllocationDeciders(Settings settings, NodeSettingsService nodeSettingsService) {
this(settings, ImmutableSet.<AllocationDecider>builder()
.add(new ConcurrentRebalanceAllocationDecider(settings, nodeSettingsService))
.add(new DisableAllocationDecider(settings, nodeSettingsService))
.add(new ClusterRebalanceAllocationDecider(settings))
.add(new DiskThresholdDecider(settings, nodeSettingsService))
.add(new SnapshotInProgressAllocationDecider(settings))
.add(new FilterAllocationDecider(settings, nodeSettingsService))
.add(new RebalanceOnlyWhenActiveAllocationDecider(settings))
.add(new ReplicaAfterPrimaryActiveAllocationDecider(settings))
.add(new ShardsLimitAllocationDecider(settings))
.add(new AwarenessAllocationDecider(settings, nodeSettingsService))
.add(new SameShardAllocationDecider(settings))
.add(new ThrottlingAllocationDecider(settings, nodeSettingsService))
.build()
);
public AllocationDeciders(Settings settings, AllocationDecider[] allocations) {
super(settings);
this.allocations = allocations;
}

@Inject
public AllocationDeciders(Settings settings, Set<AllocationDecider> allocations) {
super(settings);
this.allocations = allocations.toArray(new AllocationDecider[allocations.size()]);
this(settings, allocations.toArray(new AllocationDecider[allocations.size()]));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.cluster.routing.allocation.decider;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.common.inject.multibindings.Multibinder;
Expand Down Expand Up @@ -51,22 +52,27 @@ public AllocationDecidersModule add(Class<? extends AllocationDecider> allocatio
@Override
protected void configure() {
Multibinder<AllocationDecider> allocationMultibinder = Multibinder.newSetBinder(binder(), AllocationDecider.class);
allocationMultibinder.addBinding().to(SameShardAllocationDecider.class);
allocationMultibinder.addBinding().to(FilterAllocationDecider.class);
allocationMultibinder.addBinding().to(ReplicaAfterPrimaryActiveAllocationDecider.class);
allocationMultibinder.addBinding().to(ThrottlingAllocationDecider.class);
allocationMultibinder.addBinding().to(RebalanceOnlyWhenActiveAllocationDecider.class);
allocationMultibinder.addBinding().to(ClusterRebalanceAllocationDecider.class);
allocationMultibinder.addBinding().to(ConcurrentRebalanceAllocationDecider.class);
allocationMultibinder.addBinding().to(DisableAllocationDecider.class);
allocationMultibinder.addBinding().to(AwarenessAllocationDecider.class);
allocationMultibinder.addBinding().to(ShardsLimitAllocationDecider.class);
allocationMultibinder.addBinding().to(DiskThresholdDecider.class);
allocationMultibinder.addBinding().to(SnapshotInProgressAllocationDecider.class);
for (Class<? extends AllocationDecider> deciderClass : DEFAULT_ALLOCATION_DECIDERS) {
allocationMultibinder.addBinding().to(deciderClass);
}
for (Class<? extends AllocationDecider> allocation : allocations) {
allocationMultibinder.addBinding().to(allocation);
}

bind(AllocationDeciders.class).asEagerSingleton();
}

public static final ImmutableSet<Class<? extends AllocationDecider>> DEFAULT_ALLOCATION_DECIDERS = ImmutableSet.<Class<? extends AllocationDecider>>builder().
add(SameShardAllocationDecider.class).
add(FilterAllocationDecider.class).
add(ReplicaAfterPrimaryActiveAllocationDecider.class).
add(ThrottlingAllocationDecider.class).
add(RebalanceOnlyWhenActiveAllocationDecider.class).
add(ClusterRebalanceAllocationDecider.class).
add(ConcurrentRebalanceAllocationDecider.class).
add(DisableAllocationDecider.class).
add(AwarenessAllocationDecider.class).
add(ShardsLimitAllocationDecider.class).
add(DiskThresholdDecider.class).
add(SnapshotInProgressAllocationDecider.class).build();
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public DiskThresholdDecider(Settings settings) {
}

@Inject
protected DiskThresholdDecider(Settings settings, NodeSettingsService nodeSettingsService) {
public DiskThresholdDecider(Settings settings, NodeSettingsService nodeSettingsService) {
super(settings);
String lowWatermark = settings.get(CLUSTER_ROUTING_ALLOCATION_LOW_DISK_WATERMARK, "0.7");
String highWatermark = settings.get(CLUSTER_ROUTING_ALLOCATION_HIGH_DISK_WATERMARK, "0.85");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@
import org.elasticsearch.cluster.routing.allocation.RoutingAllocation;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.test.ElasticsearchAllocationTestCase;

import java.util.Random;

import static org.elasticsearch.cluster.routing.ShardRoutingState.INITIALIZING;
import static org.elasticsearch.cluster.routing.allocation.RoutingAllocationTests.newNode;
import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;

public class ClusterAllocationRerouteBenchmark {

Expand All @@ -45,8 +48,7 @@ public static void main(String[] args) {
final int numReplicas = 2;
final int numberOfNodes = 30;
final int numberOfTags = 2;

AllocationService strategy = new AllocationService(settingsBuilder().build());
AllocationService strategy = ElasticsearchAllocationTestCase.createAllocationService(ImmutableSettings.EMPTY, new Random(1));

MetaData.Builder mb = MetaData.builder();
for (int i = 1; i <= numIndices; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.test.ElasticsearchTestCase;
import org.elasticsearch.test.ElasticsearchAllocationTestCase;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import org.junit.Test;
Expand All @@ -23,14 +23,14 @@
import static org.elasticsearch.cluster.routing.allocation.RoutingAllocationTests.newNode;
import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;

public class AddIncrementallyTests extends ElasticsearchTestCase {
public class AddIncrementallyTests extends ElasticsearchAllocationTestCase {
private final ESLogger logger = Loggers.getLogger(AddIncrementallyTests.class);

@Test
public void testAddNodesAndIndices() {
ImmutableSettings.Builder settings = settingsBuilder();
settings.put("cluster.routing.allocation.allow_rebalance", ClusterRebalanceAllocationDecider.ClusterRebalanceType.ALWAYS.toString());
AllocationService service = new AllocationService(settings.build());
AllocationService service = createAllocationService(settings.build());

ClusterState clusterState = initCluster(service, 1, 3, 3, 1);
assertThat(clusterState.routingNodes().node("node0").shardsWithState(STARTED).size(), Matchers.equalTo(9));
Expand Down Expand Up @@ -75,7 +75,7 @@ public void testMinimalRelocations() {
ImmutableSettings.Builder settings = settingsBuilder();
settings.put("cluster.routing.allocation.allow_rebalance", ClusterRebalanceAllocationDecider.ClusterRebalanceType.ALWAYS.toString())
.put("cluster.routing.allocation.node_concurrent_recoveries", 2);
AllocationService service = new AllocationService(settings.build());
AllocationService service = createAllocationService(settings.build());

ClusterState clusterState = initCluster(service, 1, 3, 3, 1);
assertThat(clusterState.routingNodes().node("node0").shardsWithState(STARTED).size(), Matchers.equalTo(9));
Expand Down Expand Up @@ -146,7 +146,7 @@ public void testMinimalRelocationsNoLimit() {
settings.put("cluster.routing.allocation.allow_rebalance", ClusterRebalanceAllocationDecider.ClusterRebalanceType.ALWAYS.toString())
.put("cluster.routing.allocation.node_concurrent_recoveries", 100)
.put("cluster.routing.allocation.node_initial_primaries_recoveries", 100);
AllocationService service = new AllocationService(settings.build());
AllocationService service = createAllocationService(settings.build());

ClusterState clusterState = initCluster(service, 1, 3, 3, 1);
assertThat(clusterState.routingNodes().node("node0").shardsWithState(STARTED).size(), Matchers.equalTo(9));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.elasticsearch.cluster.routing.RoutingTable;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.test.ElasticsearchTestCase;
import org.elasticsearch.test.ElasticsearchAllocationTestCase;
import org.junit.Test;

import static org.elasticsearch.cluster.routing.ShardRoutingState.INITIALIZING;
Expand All @@ -36,13 +36,13 @@

/**
*/
public class AllocatePostApiFlagTests extends ElasticsearchTestCase {
public class AllocatePostApiFlagTests extends ElasticsearchAllocationTestCase {

private final ESLogger logger = Loggers.getLogger(AllocatePostApiFlagTests.class);

@Test
public void simpleFlagTests() {
AllocationService allocation = new AllocationService(settingsBuilder().put("cluster.routing.allocation.concurrent_recoveries", 10).build());
AllocationService allocation = createAllocationService(settingsBuilder().put("cluster.routing.allocation.concurrent_recoveries", 10).build());

logger.info("creating an index with 1 shard, no replica");
MetaData metaData = MetaData.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.test.ElasticsearchTestCase;
import org.elasticsearch.test.ElasticsearchAllocationTestCase;
import org.junit.Test;

import static org.elasticsearch.cluster.routing.ShardRoutingState.INITIALIZING;
Expand All @@ -51,13 +51,13 @@

/**
*/
public class AllocationCommandsTests extends ElasticsearchTestCase {
public class AllocationCommandsTests extends ElasticsearchAllocationTestCase {

private final ESLogger logger = Loggers.getLogger(AllocationCommandsTests.class);

@Test
public void moveShardCommand() {
AllocationService allocation = new AllocationService(settingsBuilder().put("cluster.routing.allocation.concurrent_recoveries", 10).build());
AllocationService allocation = createAllocationService(settingsBuilder().put("cluster.routing.allocation.concurrent_recoveries", 10).build());

logger.info("creating an index with 1 shard, no replica");
MetaData metaData = MetaData.builder()
Expand Down Expand Up @@ -101,7 +101,7 @@ public void moveShardCommand() {

@Test
public void allocateCommand() {
AllocationService allocation = new AllocationService(settingsBuilder()
AllocationService allocation = createAllocationService(settingsBuilder()
.put(DisableAllocationDecider.CLUSTER_ROUTING_ALLOCATION_DISABLE_NEW_ALLOCATION, true)
.put(DisableAllocationDecider.CLUSTER_ROUTING_ALLOCATION_DISABLE_ALLOCATION, true)
.build());
Expand Down Expand Up @@ -182,7 +182,7 @@ public void allocateCommand() {

@Test
public void cancelCommand() {
AllocationService allocation = new AllocationService(settingsBuilder()
AllocationService allocation = createAllocationService(settingsBuilder()
.put(DisableAllocationDecider.CLUSTER_ROUTING_ALLOCATION_DISABLE_NEW_ALLOCATION, true)
.put(DisableAllocationDecider.CLUSTER_ROUTING_ALLOCATION_DISABLE_ALLOCATION, true)
.build());
Expand Down

0 comments on commit a4f97be

Please sign in to comment.