From ab404d90edff6b83913361a5bffc59ed6ef2f3fa Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Thu, 18 Aug 2016 22:16:20 -0700 Subject: [PATCH 01/15] Plugins: Switch custom ShardsAllocators to pull based model This change moves custom ShardsAllocators from registration on ClusterModule, to implementing getShardsAllocators() in ClusterPlugin. It also removes the legacy alias "even_shard" for the balanced allocator which was removed in 2.0. --- .../elasticsearch/cluster/ClusterModule.java | 40 +++++++++++------- .../elasticsearch/plugins/ClusterPlugin.java | 17 ++++++++ .../cluster/ClusterModuleTests.java | 41 +++++++++++-------- 3 files changed, 68 insertions(+), 30 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/cluster/ClusterModule.java b/core/src/main/java/org/elasticsearch/cluster/ClusterModule.java index 34cf7df6a2e..ff94af47dfa 100644 --- a/core/src/main/java/org/elasticsearch/cluster/ClusterModule.java +++ b/core/src/main/java/org/elasticsearch/cluster/ClusterModule.java @@ -69,25 +69,26 @@ import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.function.Function; +import java.util.function.Supplier; /** * Configures classes and services that affect the entire cluster. */ public class ClusterModule extends AbstractModule { - public static final String EVEN_SHARD_COUNT_ALLOCATOR = "even_shard"; public static final String BALANCED_ALLOCATOR = "balanced"; // default public static final Setting SHARDS_ALLOCATOR_TYPE_SETTING = new Setting<>("cluster.routing.allocation.type", BALANCED_ALLOCATOR, Function.identity(), Property.NodeScope); private final Settings settings; - private final ExtensionPoint.SelectedType shardsAllocators = new ExtensionPoint.SelectedType<>("shards_allocator", ShardsAllocator.class); private final ExtensionPoint.ClassSet indexTemplateFilters = new ExtensionPoint.ClassSet<>("index_template_filter", IndexTemplateFilter.class); private final ClusterService clusterService; private final IndexNameExpressionResolver indexNameExpressionResolver; // pkg private for tests final Collection allocationDeciders; + final ShardsAllocator shardsAllocator; // pkg private so tests can mock Class clusterInfoServiceImpl = InternalClusterInfoService.class; @@ -95,16 +96,11 @@ public class ClusterModule extends AbstractModule { public ClusterModule(Settings settings, ClusterService clusterService, List clusterPlugins) { this.settings = settings; this.allocationDeciders = createAllocationDeciders(settings, clusterService.getClusterSettings(), clusterPlugins); - registerShardsAllocator(ClusterModule.BALANCED_ALLOCATOR, BalancedShardsAllocator.class); - registerShardsAllocator(ClusterModule.EVEN_SHARD_COUNT_ALLOCATOR, BalancedShardsAllocator.class); + this.shardsAllocator = createShardsAllocator(settings, clusterService.getClusterSettings(), clusterPlugins); this.clusterService = clusterService; indexNameExpressionResolver = new IndexNameExpressionResolver(settings); } - public void registerShardsAllocator(String name, Class clazz) { - shardsAllocators.registerExtension(name, clazz); - } - public void registerIndexTemplateFilter(Class indexTemplateFilter) { indexTemplateFilters.registerExtension(indexTemplateFilter); } @@ -148,14 +144,29 @@ public class ClusterModule extends AbstractModule { } } + private static ShardsAllocator createShardsAllocator(Settings settings, ClusterSettings clusterSettings, + List clusterPlugins) { + Map> allocators = new HashMap<>(); + allocators.put(BALANCED_ALLOCATOR, () -> new BalancedShardsAllocator(settings, clusterSettings)); + + for (ClusterPlugin plugin : clusterPlugins) { + plugin.getShardsAllocators(settings, clusterSettings).forEach((k, v) -> { + if (allocators.put(k, v) != null) { + throw new IllegalArgumentException("ShardsAllocator [" + k + "] already defined"); + } + }); + } + String allocatorName = SHARDS_ALLOCATOR_TYPE_SETTING.get(settings); + Supplier allocatorSupplier = allocators.get(allocatorName); + if (allocatorSupplier == null) { + throw new IllegalArgumentException("Unknown ShardsAllocator [" + allocatorName + "]"); + } + return Objects.requireNonNull(allocatorSupplier.get(), + "ShardsAllocator factory for [" + allocatorName + "] returned null"); + } + @Override protected void configure() { - // bind ShardsAllocator - String shardsAllocatorType = shardsAllocators.bindType(binder(), settings, ClusterModule.SHARDS_ALLOCATOR_TYPE_SETTING.getKey(), ClusterModule.BALANCED_ALLOCATOR); - if (shardsAllocatorType.equals(ClusterModule.EVEN_SHARD_COUNT_ALLOCATOR)) { - final ESLogger logger = Loggers.getLogger(getClass(), settings); - logger.warn("{} allocator has been removed in 2.0 using {} instead", ClusterModule.EVEN_SHARD_COUNT_ALLOCATOR, ClusterModule.BALANCED_ALLOCATOR); - } indexTemplateFilters.bind(binder()); bind(ClusterInfoService.class).to(clusterInfoServiceImpl).asEagerSingleton(); @@ -178,5 +189,6 @@ public class ClusterModule extends AbstractModule { bind(MappingUpdatedAction.class).asEagerSingleton(); bind(TaskResultsService.class).asEagerSingleton(); bind(AllocationDeciders.class).toInstance(new AllocationDeciders(settings, allocationDeciders)); + bind(ShardsAllocator.class).toInstance(shardsAllocator); } } diff --git a/core/src/main/java/org/elasticsearch/plugins/ClusterPlugin.java b/core/src/main/java/org/elasticsearch/plugins/ClusterPlugin.java index f2bce818e15..7de805b7045 100644 --- a/core/src/main/java/org/elasticsearch/plugins/ClusterPlugin.java +++ b/core/src/main/java/org/elasticsearch/plugins/ClusterPlugin.java @@ -21,7 +21,10 @@ package org.elasticsearch.plugins; import java.util.Collection; import java.util.Collections; +import java.util.Map; +import java.util.function.Supplier; +import org.elasticsearch.cluster.routing.allocation.allocator.ShardsAllocator; import org.elasticsearch.cluster.routing.allocation.decider.AllocationDecider; import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.Settings; @@ -41,4 +44,18 @@ public interface ClusterPlugin { default Collection createAllocationDeciders(Settings settings, ClusterSettings clusterSettings) { return Collections.emptyList(); } + + /** + * Return {@link ShardsAllocator} implementations added by this plugin. + * + * The key of the returned {@link Map} is the name of the allocator, and the value + * is a function to construct the allocator. + * + * @param settings Settings for the node + * @param clusterSettings Settings for the cluster + * @return A map of allocator implementations + */ + default Map> getShardsAllocators(Settings settings, ClusterSettings clusterSettings) { + return Collections.emptyMap(); + } } diff --git a/core/src/test/java/org/elasticsearch/cluster/ClusterModuleTests.java b/core/src/test/java/org/elasticsearch/cluster/ClusterModuleTests.java index 6f2ede1c49f..603e0e436e5 100644 --- a/core/src/test/java/org/elasticsearch/cluster/ClusterModuleTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/ClusterModuleTests.java @@ -44,6 +44,8 @@ import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.function.Supplier; + public class ClusterModuleTests extends ModuleTestCase { private ClusterService clusterService = new ClusterService(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), null); @@ -126,33 +128,40 @@ public class ClusterModuleTests extends ModuleTestCase { assertTrue(module.allocationDeciders.stream().anyMatch(d -> d.getClass().equals(FakeAllocationDecider.class))); } + private ClusterModule newClusterModuleWithShardsAllocator(Settings settings, String name, Supplier supplier) { + return new ClusterModule(settings, clusterService, Collections.singletonList( + new ClusterPlugin() { + @Override + public Map> getShardsAllocators(Settings settings, ClusterSettings clusterSettings) { + return Collections.singletonMap(name, supplier); + } + } + )); + } + public void testRegisterShardsAllocator() { Settings settings = Settings.builder().put(ClusterModule.SHARDS_ALLOCATOR_TYPE_SETTING.getKey(), "custom").build(); - ClusterModule module = new ClusterModule(settings, clusterService, Collections.emptyList()); - module.registerShardsAllocator("custom", FakeShardsAllocator.class); - assertBinding(module, ShardsAllocator.class, FakeShardsAllocator.class); + ClusterModule module = newClusterModuleWithShardsAllocator(settings, "custom", FakeShardsAllocator::new); + assertEquals(FakeShardsAllocator.class, module.shardsAllocator.getClass()); } public void testRegisterShardsAllocatorAlreadyRegistered() { - ClusterModule module = new ClusterModule(Settings.EMPTY, clusterService, Collections.emptyList()); - try { - module.registerShardsAllocator(ClusterModule.BALANCED_ALLOCATOR, FakeShardsAllocator.class); - } catch (IllegalArgumentException e) { - assertEquals(e.getMessage(), "Can't register the same [shards_allocator] more than once for [balanced]"); - } + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> + newClusterModuleWithShardsAllocator(Settings.EMPTY, ClusterModule.BALANCED_ALLOCATOR, FakeShardsAllocator::new)); + assertEquals("ShardsAllocator [" + ClusterModule.BALANCED_ALLOCATOR + "] already defined", e.getMessage()); } public void testUnknownShardsAllocator() { Settings settings = Settings.builder().put(ClusterModule.SHARDS_ALLOCATOR_TYPE_SETTING.getKey(), "dne").build(); - ClusterModule module = new ClusterModule(settings, clusterService, Collections.emptyList()); - assertBindingFailure(module, "Unknown [shards_allocator]"); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> + new ClusterModule(settings, clusterService, Collections.emptyList())); + assertEquals("Unknown ShardsAllocator [dne]", e.getMessage()); } - public void testEvenShardsAllocatorBackcompat() { - Settings settings = Settings.builder() - .put(ClusterModule.SHARDS_ALLOCATOR_TYPE_SETTING.getKey(), ClusterModule.EVEN_SHARD_COUNT_ALLOCATOR).build(); - ClusterModule module = new ClusterModule(settings, clusterService, Collections.emptyList()); - assertBinding(module, ShardsAllocator.class, BalancedShardsAllocator.class); + public void testShardsAllocatorFactoryNull() { + Settings settings = Settings.builder().put(ClusterModule.SHARDS_ALLOCATOR_TYPE_SETTING.getKey(), "bad").build(); + NullPointerException e = expectThrows(NullPointerException.class, () -> + newClusterModuleWithShardsAllocator(settings, "bad", () -> null)); } public void testRegisterIndexTemplateFilterDuplicate() { From 207d3a60e76c64b2e4a3553e999425879a8a997c Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Thu, 18 Aug 2016 23:06:14 -0700 Subject: [PATCH 02/15] Fix staging url for official plugins This was incorrectly setup in #19996, without the version in the staging build id. --- .../java/org/elasticsearch/plugins/InstallPluginCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/org/elasticsearch/plugins/InstallPluginCommand.java b/core/src/main/java/org/elasticsearch/plugins/InstallPluginCommand.java index 25dec47862b..6579257b5a7 100644 --- a/core/src/main/java/org/elasticsearch/plugins/InstallPluginCommand.java +++ b/core/src/main/java/org/elasticsearch/plugins/InstallPluginCommand.java @@ -212,7 +212,7 @@ class InstallPluginCommand extends SettingCommand { final String stagingHash = System.getProperty(PROPERTY_STAGING_ID); if (stagingHash != null) { url = String.format(Locale.ROOT, - "https://staging.elastic.co/%1$s/download/elasticsearch-plugins/%2$s/%2$s-%3$s.zip", + "https://staging.elastic.co/%3$s-%1$s/download/elasticsearch-plugins/%2$s/%2$s-%3$s.zip", stagingHash, pluginId, version); } else { url = String.format(Locale.ROOT, From a4ea7e72234b480f89e8c59bb12061be11172db1 Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Thu, 18 Aug 2016 12:36:53 +0200 Subject: [PATCH 03/15] Switch indices.exists_type from `{index}/{type}` to `{index}/_mapping/{type}`. #20055 This will help remove types as we will need `{index}/{id}` to tell whether a document exists. Relates #15613 --- .../rest/action/admin/indices/RestTypesExistsAction.java | 4 +++- docs/reference/indices/indices-exists.asciidoc | 4 +++- docs/reference/indices/types-exists.asciidoc | 4 +++- docs/reference/migration/migrate_5_0/rest.asciidoc | 7 +++++++ .../resources/rest-api-spec/api/indices.exists_type.json | 4 ++-- 5 files changed, 18 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/rest/action/admin/indices/RestTypesExistsAction.java b/core/src/main/java/org/elasticsearch/rest/action/admin/indices/RestTypesExistsAction.java index 791fc0eee39..3877715395c 100644 --- a/core/src/main/java/org/elasticsearch/rest/action/admin/indices/RestTypesExistsAction.java +++ b/core/src/main/java/org/elasticsearch/rest/action/admin/indices/RestTypesExistsAction.java @@ -46,7 +46,9 @@ public class RestTypesExistsAction extends BaseRestHandler { @Inject public RestTypesExistsAction(Settings settings, RestController controller) { super(settings); - controller.registerHandler(HEAD, "/{index}/{type}", this); + controller.registerWithDeprecatedHandler( + HEAD, "/{index}/_mapping/{type}", this, + HEAD, "/{index}/{type}", deprecationLogger); } @Override diff --git a/docs/reference/indices/indices-exists.asciidoc b/docs/reference/indices/indices-exists.asciidoc index 1ffb1074a5a..184c649c279 100644 --- a/docs/reference/indices/indices-exists.asciidoc +++ b/docs/reference/indices/indices-exists.asciidoc @@ -5,8 +5,10 @@ Used to check if the index (indices) exists or not. For example: [source,js] -------------------------------------------------- -curl -XHEAD -i 'http://localhost:9200/twitter' +HEAD twitter -------------------------------------------------- +// CONSOLE +// TEST[setup:twitter] The HTTP status code indicates if the index exists or not. A `404` means it does not exist, and `200` means it does. diff --git a/docs/reference/indices/types-exists.asciidoc b/docs/reference/indices/types-exists.asciidoc index 739e0e9bf37..29a9be29e05 100644 --- a/docs/reference/indices/types-exists.asciidoc +++ b/docs/reference/indices/types-exists.asciidoc @@ -5,8 +5,10 @@ Used to check if a type/types exists in an index/indices. [source,js] -------------------------------------------------- -curl -XHEAD -i 'http://localhost:9200/twitter/tweet' +HEAD twitter/_mapping/tweet -------------------------------------------------- +// CONSOLE +// TEST[setup:twitter] The HTTP status code indicates if the type exists or not. A `404` means it does not exist, and `200` means it does. diff --git a/docs/reference/migration/migrate_5_0/rest.asciidoc b/docs/reference/migration/migrate_5_0/rest.asciidoc index 36f4071ce15..218bfb8f236 100644 --- a/docs/reference/migration/migrate_5_0/rest.asciidoc +++ b/docs/reference/migration/migrate_5_0/rest.asciidoc @@ -20,6 +20,13 @@ The `GET` HTTP verb for `/_forcemerge` is no longer supported, please use the It used to be possible to create an index by either calling `PUT index_name` or `POST index_name`. Only the former is now supported. +==== `HEAD {index}/{type}` replaced with `HEAD {index}/_mapping/{type}` + +The endpoint for checking whether a type exists has been changed from +`{index}/{type}` to `{index}/_mapping/{type}` in order to prepare for the +removal of types when `HEAD {index}/{id}` will be used to check whether a +document exists in an index. The old endpoint will keep working until 6.0. + ==== Removed `mem` section from `/_cluster/stats` response The `mem` section contained only one value, the total memory available diff --git a/rest-api-spec/src/main/resources/rest-api-spec/api/indices.exists_type.json b/rest-api-spec/src/main/resources/rest-api-spec/api/indices.exists_type.json index 37d9d979fa5..d793199bc27 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/api/indices.exists_type.json +++ b/rest-api-spec/src/main/resources/rest-api-spec/api/indices.exists_type.json @@ -3,8 +3,8 @@ "documentation": "http://www.elastic.co/guide/en/elasticsearch/reference/master/indices-types-exists.html", "methods": ["HEAD"], "url": { - "path": "/{index}/{type}", - "paths": ["/{index}/{type}"], + "path": "/{index}/_mapping/{type}", + "paths": ["/{index}/_mapping/{type}"], "parts": { "index": { "type" : "list", From 59636a084476c8d5065856cd0e464e902107490b Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Fri, 19 Aug 2016 00:27:37 -0700 Subject: [PATCH 04/15] Internal: Deguice IndicesService Almost all the dependencies of indices service are already created outside of guice. This change deguices MetaStateService, and then IndicesService. --- .../java/org/elasticsearch/gateway/GatewayModule.java | 1 - .../org/elasticsearch/gateway/MetaStateService.java | 1 - .../java/org/elasticsearch/indices/IndicesModule.java | 1 - .../org/elasticsearch/indices/IndicesService.java | 1 - core/src/main/java/org/elasticsearch/node/Node.java | 11 ++++++++++- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/gateway/GatewayModule.java b/core/src/main/java/org/elasticsearch/gateway/GatewayModule.java index f736c070272..8169062a70b 100644 --- a/core/src/main/java/org/elasticsearch/gateway/GatewayModule.java +++ b/core/src/main/java/org/elasticsearch/gateway/GatewayModule.java @@ -30,7 +30,6 @@ public class GatewayModule extends AbstractModule { @Override protected void configure() { - bind(MetaStateService.class).asEagerSingleton(); bind(DanglingIndicesState.class).asEagerSingleton(); bind(GatewayService.class).asEagerSingleton(); bind(TransportNodesListGatewayMetaState.class).asEagerSingleton(); diff --git a/core/src/main/java/org/elasticsearch/gateway/MetaStateService.java b/core/src/main/java/org/elasticsearch/gateway/MetaStateService.java index 9a36df43672..b5ec8466c2c 100644 --- a/core/src/main/java/org/elasticsearch/gateway/MetaStateService.java +++ b/core/src/main/java/org/elasticsearch/gateway/MetaStateService.java @@ -40,7 +40,6 @@ public class MetaStateService extends AbstractComponent { private final NodeEnvironment nodeEnv; - @Inject public MetaStateService(Settings settings, NodeEnvironment nodeEnv) { super(settings); this.nodeEnv = nodeEnv; diff --git a/core/src/main/java/org/elasticsearch/indices/IndicesModule.java b/core/src/main/java/org/elasticsearch/indices/IndicesModule.java index d45725ff4ef..f4a457acc7b 100644 --- a/core/src/main/java/org/elasticsearch/indices/IndicesModule.java +++ b/core/src/main/java/org/elasticsearch/indices/IndicesModule.java @@ -173,7 +173,6 @@ public class IndicesModule extends AbstractModule { protected void configure() { bindMapperExtension(); - bind(IndicesService.class).asEagerSingleton(); bind(RecoverySettings.class).asEagerSingleton(); bind(RecoveryTargetService.class).asEagerSingleton(); bind(RecoverySource.class).asEagerSingleton(); diff --git a/core/src/main/java/org/elasticsearch/indices/IndicesService.java b/core/src/main/java/org/elasticsearch/indices/IndicesService.java index 7519494c39c..0f6ae137e1f 100644 --- a/core/src/main/java/org/elasticsearch/indices/IndicesService.java +++ b/core/src/main/java/org/elasticsearch/indices/IndicesService.java @@ -168,7 +168,6 @@ public class IndicesService extends AbstractLifecycleComponent threadPool.schedule(this.cleanInterval, ThreadPool.Names.SAME, this.cacheCleaner); } - @Inject public IndicesService(Settings settings, PluginsService pluginsService, NodeEnvironment nodeEnv, ClusterSettings clusterSettings, AnalysisRegistry analysisRegistry, IndicesQueriesRegistry indicesQueriesRegistry, IndexNameExpressionResolver indexNameExpressionResolver, diff --git a/core/src/main/java/org/elasticsearch/node/Node.java b/core/src/main/java/org/elasticsearch/node/Node.java index 2558b8506c2..523e6faefb1 100644 --- a/core/src/main/java/org/elasticsearch/node/Node.java +++ b/core/src/main/java/org/elasticsearch/node/Node.java @@ -74,6 +74,7 @@ import org.elasticsearch.env.NodeEnvironment; import org.elasticsearch.gateway.GatewayAllocator; import org.elasticsearch.gateway.GatewayModule; import org.elasticsearch.gateway.GatewayService; +import org.elasticsearch.gateway.MetaStateService; import org.elasticsearch.http.HttpServer; import org.elasticsearch.http.HttpServerTransport; import org.elasticsearch.index.analysis.AnalysisRegistry; @@ -349,6 +350,11 @@ public class Node implements Closeable { .flatMap(p -> p.getNamedWriteables().stream())) .flatMap(Function.identity()).collect(Collectors.toList()); final NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(namedWriteables); + final MetaStateService metaStateService = new MetaStateService(settings, nodeEnvironment); + final IndicesService indicesService = new IndicesService(settings, pluginsService, nodeEnvironment, + settingsModule.getClusterSettings(), analysisModule.getAnalysisRegistry(), searchModule.getQueryParserRegistry(), + clusterModule.getIndexNameExpressionResolver(), indicesModule.getMapperRegistry(), namedWriteableRegistry, + threadPool, settingsModule.getIndexScopedSettings(), circuitBreakerService, metaStateService); client = new NodeClient(settings, threadPool); Collection pluginComponents = pluginsService.filterPlugins(Plugin.class).stream() .flatMap(p -> p.createComponents(client, clusterService, threadPool, resourceWatcherService, @@ -374,6 +380,9 @@ public class Node implements Closeable { b.bind(AnalysisRegistry.class).toInstance(analysisModule.getAnalysisRegistry()); b.bind(IngestService.class).toInstance(ingestService); b.bind(NamedWriteableRegistry.class).toInstance(namedWriteableRegistry); + b.bind(MetaDataUpgrader.class).toInstance(metaDataUpgrader); + b.bind(MetaStateService.class).toInstance(metaStateService); + b.bind(IndicesService.class).toInstance(indicesService); Class searchServiceImpl = pickSearchServiceImplementation(); if (searchServiceImpl == SearchService.class) { b.bind(SearchService.class).asEagerSingleton(); @@ -381,7 +390,7 @@ public class Node implements Closeable { b.bind(SearchService.class).to(searchServiceImpl).asEagerSingleton(); } pluginComponents.stream().forEach(p -> b.bind((Class) p.getClass()).toInstance(p)); - b.bind(MetaDataUpgrader.class).toInstance(metaDataUpgrader); + } ); injector = modules.createInjector(); From a74f77b632ca8561d53163f400918136f9b11986 Mon Sep 17 00:00:00 2001 From: Yannick Welsch Date: Fri, 19 Aug 2016 10:04:42 +0200 Subject: [PATCH 05/15] Check that all active shards have their allocation id in the in-sync set --- .../org/elasticsearch/cluster/routing/IndexRoutingTable.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/core/src/main/java/org/elasticsearch/cluster/routing/IndexRoutingTable.java b/core/src/main/java/org/elasticsearch/cluster/routing/IndexRoutingTable.java index 0fe49369177..0c9c5219df6 100644 --- a/core/src/main/java/org/elasticsearch/cluster/routing/IndexRoutingTable.java +++ b/core/src/main/java/org/elasticsearch/cluster/routing/IndexRoutingTable.java @@ -128,6 +128,11 @@ public class IndexRoutingTable extends AbstractDiffable imple throw new IllegalStateException("shard routing has an index [" + shardRouting.index() + "] that is different " + "from the routing table"); } + if (shardRouting.active() && + indexMetaData.activeAllocationIds(shardRouting.id()).contains(shardRouting.allocationId().getId()) == false) { + throw new IllegalStateException("active shard routing " + shardRouting + " has no corresponding entry in the " + + "in-sync allocation set " + indexMetaData.activeAllocationIds(shardRouting.id())); + } } } return true; From 807da191505beaebf82566e7e23fb8d9a7128bb9 Mon Sep 17 00:00:00 2001 From: Tanguy Leroux Date: Fri, 19 Aug 2016 10:46:21 +0200 Subject: [PATCH 06/15] [Doc] Update plugins intro about categories of plugins core/community --- docs/plugins/index.asciidoc | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/docs/plugins/index.asciidoc b/docs/plugins/index.asciidoc index 725c200ee95..8a572d5ee3f 100644 --- a/docs/plugins/index.asciidoc +++ b/docs/plugins/index.asciidoc @@ -11,14 +11,28 @@ manner. They range from adding custom mapping types, custom analyzers, native scripts, custom discovery and more. Plugins contain JAR files, but may also contain scripts and config files, and -must be installed on every node in the cluster. After installation, each +must be installed on every node in the cluster. After installation, each node must be restarted before the plugin becomes visible. +This documentation distinguishes two categories of plugins: + +Core Plugins:: This category identifies plugins that are part of Elasticsearch +project. Delivered at the same time as Elasticsearch, their version number always +matches the version number of Elasticsearch itself. These plugins are maintained +by the Elastic team with the appreciated help of amazing community members (for +open source plugins). Issues and bug reports can be reported on the +https://github.com/elastic/elasticsearch[Github project page]. + +Community contributed:: This category identifies plugins that are external to +the Elasticsearch project. They are provided by individual developers or private +companies and have their own licenses as well as their own versioning system. +Issues and bug reports can usually be reported on the community plugin's web site. + +For advice on writing your own plugin, see <>. + IMPORTANT: Site plugins -- plugins containing HTML, CSS and Javascript -- are no longer supported. -For advice on writing your own plugin, see <>. - include::plugin-script.asciidoc[] include::api.asciidoc[] From b586465a4c15fadba3a01ff68990cd507fbc63d0 Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Fri, 19 Aug 2016 15:55:24 +0200 Subject: [PATCH 07/15] Make generics explicit to please ECJ. --- .../test/java/org/elasticsearch/cluster/ClusterModuleTests.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/test/java/org/elasticsearch/cluster/ClusterModuleTests.java b/core/src/test/java/org/elasticsearch/cluster/ClusterModuleTests.java index 6f2ede1c49f..9a7dc16a354 100644 --- a/core/src/test/java/org/elasticsearch/cluster/ClusterModuleTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/ClusterModuleTests.java @@ -105,7 +105,7 @@ public class ClusterModuleTests extends ModuleTestCase { public void testRegisterAllocationDeciderDuplicate() { IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new ClusterModule(Settings.EMPTY, clusterService, - Collections.singletonList(new ClusterPlugin() { + Collections.singletonList(new ClusterPlugin() { @Override public Collection createAllocationDeciders(Settings settings, ClusterSettings clusterSettings) { return Collections.singletonList(new EnableAllocationDecider(settings, clusterSettings)); From 771668f3809d3b1cc10a9412de70f71c86eb1c52 Mon Sep 17 00:00:00 2001 From: Yannick Welsch Date: Thu, 18 Aug 2016 22:07:28 +0200 Subject: [PATCH 08/15] Use routingResult method to update cluster state after reroute This ensures that the routing table as well as the metadata (with the primary terms and in-sync allocation ids) is updated. --- .../cluster/metadata/IndexMetaData.java | 4 + ...rdFailedClusterStateTaskExecutorTests.java | 5 +- .../cluster/routing/RoutingTableTests.java | 21 +- .../allocation/AddIncrementallyTests.java | 180 ++--- .../allocation/AllocationCommandsTests.java | 52 +- .../allocation/AllocationPriorityTests.java | 20 +- .../allocation/AwarenessAllocationTests.java | 326 +++++---- .../allocation/BalanceConfigurationTests.java | 76 +-- .../BalanceUnbalancedClusterTests.java | 16 +- .../allocation/CatAllocationTestCase.java | 30 +- .../ClusterRebalanceRoutingTests.java | 646 +++++++++--------- .../ConcurrentRebalanceRoutingTests.java | 92 ++- .../allocation/DeadNodesAllocationTests.java | 32 +- ...ReplicaAsPrimaryDuringRelocationTests.java | 45 +- .../ExpectedShardSizeAllocationTests.java | 20 +- .../allocation/FailedNodeRoutingTests.java | 28 +- .../allocation/FailedShardsRoutingTests.java | 303 ++++---- .../allocation/FilterRoutingTests.java | 93 ++- .../routing/allocation/IndexBalanceTests.java | 503 ++++++-------- .../NodeVersionAllocationDeciderTests.java | 180 +++-- ...alPrimariesToRelocatingPrimariesTests.java | 24 +- .../PreferPrimaryAllocationTests.java | 28 +- ...yNotRelocatedWhileBeingRecoveredTests.java | 30 +- .../RandomAllocationDeciderTests.java | 25 +- .../allocation/RebalanceAfterActiveTests.java | 94 ++- .../RoutingNodesIntegrityTests.java | 176 ++--- .../allocation/SameShardRoutingTests.java | 13 +- .../ShardsLimitAllocationTests.java | 70 +- .../SingleShardNoReplicasRoutingTests.java | 291 ++++---- .../SingleShardOneReplicaRoutingTests.java | 145 ++-- .../allocation/StartedShardsRoutingTests.java | 6 +- .../TenShardsOneReplicaRoutingTests.java | 125 ++-- .../allocation/ThrottlingAllocationTests.java | 237 ++++--- .../UpdateNumberOfReplicasTests.java | 153 ++--- .../decider/DiskThresholdDeciderTests.java | 121 ++-- .../decider/EnableAllocationTests.java | 76 +-- .../structure/RoutingIteratorTests.java | 49 +- .../cluster/ESAllocationTestCase.java | 16 +- 38 files changed, 2039 insertions(+), 2312 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java b/core/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java index 4f51fa9818f..4172750905e 100644 --- a/core/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java +++ b/core/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java @@ -859,6 +859,10 @@ public class IndexMetaData implements Diffable, FromXContentBuild return this; } + public Set getActiveAllocationIds(int shardId) { + return activeAllocationIds.get(shardId); + } + public long version() { return this.version; } diff --git a/core/src/test/java/org/elasticsearch/cluster/action/shard/ShardFailedClusterStateTaskExecutorTests.java b/core/src/test/java/org/elasticsearch/cluster/action/shard/ShardFailedClusterStateTaskExecutorTests.java index 0973983d4ea..877dd2551cd 100644 --- a/core/src/test/java/org/elasticsearch/cluster/action/shard/ShardFailedClusterStateTaskExecutorTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/action/shard/ShardFailedClusterStateTaskExecutorTests.java @@ -161,9 +161,8 @@ public class ShardFailedClusterStateTaskExecutorTests extends ESAllocationTestCa allocationService.reroute(stateAfterAddingNode, reason).routingTable(); ClusterState stateAfterReroute = ClusterState.builder(stateAfterAddingNode).routingTable(afterReroute).build(); RoutingNodes routingNodes = stateAfterReroute.getRoutingNodes(); - RoutingTable afterStart = - allocationService.applyStartedShards(stateAfterReroute, routingNodes.shardsWithState(ShardRoutingState.INITIALIZING)).routingTable(); - return ClusterState.builder(stateAfterReroute).routingTable(afterStart).build(); + RoutingAllocation.Result afterStart = allocationService.applyStartedShards(stateAfterReroute, routingNodes.shardsWithState(ShardRoutingState.INITIALIZING)); + return ClusterState.builder(stateAfterReroute).routingResult(afterStart).build(); } private List createExistingShards(ClusterState currentState, String reason) { diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/RoutingTableTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/RoutingTableTests.java index bc22fbfa314..a60796447ef 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/RoutingTableTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/RoutingTableTests.java @@ -32,6 +32,9 @@ import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.cluster.ESAllocationTestCase; import org.junit.Before; +import java.util.Set; +import java.util.stream.Collectors; + import static org.elasticsearch.cluster.routing.ShardRoutingState.INITIALIZING; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; @@ -89,14 +92,14 @@ public class RoutingTableTests extends ESAllocationTestCase { RoutingAllocation.Result rerouteResult = ALLOCATION_SERVICE.reroute(clusterState, "reroute"); this.testRoutingTable = rerouteResult.routingTable(); assertThat(rerouteResult.changed(), is(true)); - this.clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + this.clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); } private void startInitializingShards(String index) { this.clusterState = ClusterState.builder(clusterState).routingTable(this.testRoutingTable).build(); logger.info("start primary shards for index {}", index); RoutingAllocation.Result rerouteResult = ALLOCATION_SERVICE.applyStartedShards(this.clusterState, this.clusterState.getRoutingNodes().shardsWithState(index, INITIALIZING)); - this.clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + this.clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); this.testRoutingTable = rerouteResult.routingTable(); } @@ -296,10 +299,11 @@ public class RoutingTableTests extends ESAllocationTestCase { .numberOfShards(numShards) .numberOfReplicas(numReplicas) .build(); - MetaData metaData = MetaData.builder().put(indexMetaData, true).build(); final RoutingTableGenerator routingTableGenerator = new RoutingTableGenerator(); final RoutingTableGenerator.ShardCounter counter = new RoutingTableGenerator.ShardCounter(); final IndexRoutingTable indexRoutingTable = routingTableGenerator.genIndexRoutingTable(indexMetaData, counter); + indexMetaData = updateActiveAllocations(indexRoutingTable, indexMetaData); + MetaData metaData = MetaData.builder().put(indexMetaData, true).build(); // test no validation errors assertTrue(indexRoutingTable.validate(metaData)); // test wrong number of shards causes validation errors @@ -327,4 +331,15 @@ public class RoutingTableTests extends ESAllocationTestCase { final MetaData metaData4 = MetaData.builder().put(indexMetaData, true).build(); expectThrows(IllegalStateException.class, () -> indexRoutingTable.validate(metaData4)); } + + private IndexMetaData updateActiveAllocations(IndexRoutingTable indexRoutingTable, IndexMetaData indexMetaData) { + IndexMetaData.Builder imdBuilder = IndexMetaData.builder(indexMetaData); + for (IndexShardRoutingTable shardTable : indexRoutingTable) { + for (ShardRouting shardRouting : shardTable) { + Set activeAllocations = shardTable.activeShards().stream().map(shr -> shr.allocationId().getId()).collect(Collectors.toSet()); + imdBuilder.putActiveAllocationIds(shardRouting.id(), activeAllocations); + } + } + return imdBuilder.build(); + } } diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/AddIncrementallyTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/AddIncrementallyTests.java index a4fe348a9e8..7971b2773b6 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/AddIncrementallyTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/AddIncrementallyTests.java @@ -111,50 +111,44 @@ public class AddIncrementallyTests extends ESAllocationTestCase { nodes.add(newNode("node2")); clusterState = ClusterState.builder(clusterState).nodes(nodes.build()).build(); - RoutingTable routingTable = service.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = service.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); RoutingNodes routingNodes = clusterState.getRoutingNodes(); assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(INITIALIZING).size(), Matchers.equalTo(2)); assertThat(clusterState.getRoutingNodes().node("node0").shardsWithState(INITIALIZING).size(), Matchers.equalTo(0)); assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(INITIALIZING).size(), Matchers.equalTo(0)); - RoutingTable prev = routingTable; - logger.error(clusterState.prettyPrint()); - routingTable = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - assertThat(prev, Matchers.not(Matchers.sameInstance(routingTable))); + assertTrue(routingResult.changed()); assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(STARTED).size(), Matchers.equalTo(2)); assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(INITIALIZING).size(), Matchers.equalTo(2)); assertThat(clusterState.getRoutingNodes().node("node0").shardsWithState(INITIALIZING).size(), Matchers.equalTo(0)); assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(INITIALIZING).size(), Matchers.equalTo(0)); - assertThat(prev, Matchers.not(Matchers.sameInstance(routingTable))); - prev = routingTable; - routingTable = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(STARTED).size(), Matchers.equalTo(4)); assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(INITIALIZING).size(), Matchers.equalTo(2)); assertThat(clusterState.getRoutingNodes().node("node0").shardsWithState(INITIALIZING).size(), Matchers.equalTo(0)); assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(INITIALIZING).size(), Matchers.equalTo(0)); - assertThat(prev, Matchers.not(Matchers.sameInstance(routingTable))); + assertTrue(routingResult.changed()); - prev = routingTable; - routingTable = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(STARTED).size(), Matchers.equalTo(6)); assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(INITIALIZING).size(), Matchers.equalTo(0)); assertThat(clusterState.getRoutingNodes().node("node0").shardsWithState(INITIALIZING).size(), Matchers.equalTo(0)); assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(INITIALIZING).size(), Matchers.equalTo(0)); - assertThat(prev, Matchers.not(Matchers.sameInstance(routingTable))); + assertTrue(routingResult.changed()); - prev = routingTable; - routingTable = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - assertThat(prev, Matchers.sameInstance(routingTable)); + routingResult = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); + assertFalse(routingResult.changed()); assertNumIndexShardsPerNode(clusterState, Matchers.equalTo(2)); logger.debug("ClusterState: {}", clusterState.getRoutingNodes().prettyPrint()); } @@ -181,50 +175,44 @@ public class AddIncrementallyTests extends ESAllocationTestCase { nodes.add(newNode("node2")); clusterState = ClusterState.builder(clusterState).nodes(nodes.build()).build(); - RoutingTable routingTable = service.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = service.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); RoutingNodes routingNodes = clusterState.getRoutingNodes(); assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(INITIALIZING).size(), Matchers.equalTo(2)); assertThat(clusterState.getRoutingNodes().node("node0").shardsWithState(INITIALIZING).size(), Matchers.equalTo(0)); assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(INITIALIZING).size(), Matchers.equalTo(0)); - RoutingTable prev = routingTable; - routingTable = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - assertThat(prev, Matchers.not(Matchers.sameInstance(routingTable))); + assertTrue(routingResult.changed()); assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(STARTED).size(), Matchers.equalTo(2)); assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(INITIALIZING).size(), Matchers.equalTo(2)); assertThat(clusterState.getRoutingNodes().node("node0").shardsWithState(INITIALIZING).size(), Matchers.equalTo(0)); assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(INITIALIZING).size(), Matchers.equalTo(0)); - assertThat(prev, Matchers.not(Matchers.sameInstance(routingTable))); - prev = routingTable; - routingTable = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(STARTED).size(), Matchers.equalTo(4)); assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(INITIALIZING).size(), Matchers.equalTo(2)); assertThat(clusterState.getRoutingNodes().node("node0").shardsWithState(INITIALIZING).size(), Matchers.equalTo(0)); assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(INITIALIZING).size(), Matchers.equalTo(0)); - assertThat(prev, Matchers.not(Matchers.sameInstance(routingTable))); + assertTrue(routingResult.changed()); - prev = routingTable; - routingTable = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(STARTED).size(), Matchers.equalTo(6)); assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(INITIALIZING).size(), Matchers.equalTo(0)); assertThat(clusterState.getRoutingNodes().node("node0").shardsWithState(INITIALIZING).size(), Matchers.equalTo(0)); assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(INITIALIZING).size(), Matchers.equalTo(0)); - assertThat(prev, Matchers.not(Matchers.sameInstance(routingTable))); + assertTrue(routingResult.changed()); - prev = routingTable; - routingTable = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); - assertThat(prev, Matchers.sameInstance(routingTable)); + routingResult = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); + assertFalse(routingResult.changed()); assertNumIndexShardsPerNode(clusterState, Matchers.equalTo(2)); logger.debug("ClusterState: {}", clusterState.getRoutingNodes().prettyPrint()); } @@ -262,24 +250,11 @@ public class AddIncrementallyTests extends ESAllocationTestCase { clusterState = ClusterState.builder(clusterState).nodes(nodes.build()).build(); - RoutingTable routingTable = service.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - RoutingNodes routingNodes = clusterState.getRoutingNodes(); + RoutingAllocation.Result routingResult = service.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); // move initializing to started - - RoutingTable prev = routingTable; - while (true) { - logger.debug("ClusterState: {}", clusterState.getRoutingNodes().prettyPrint()); - routingTable = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); - if (routingTable == prev) - break; - prev = routingTable; - } - - return clusterState; + return applyStartedShardsUntilNoChange(clusterState, service); } private ClusterState initCluster(AllocationService service, int numberOfNodes, int numberOfIndices, int numberOfShards, @@ -299,43 +274,30 @@ public class AddIncrementallyTests extends ESAllocationTestCase { routingTableBuilder.addAsNew(cursor.value); } - RoutingTable routingTable = routingTableBuilder.build(); + RoutingTable initialRoutingTable = routingTableBuilder.build(); logger.info("start {} nodes", numberOfNodes); DiscoveryNodes.Builder nodes = DiscoveryNodes.builder(); for (int i = 0; i < numberOfNodes; i++) { nodes.add(newNode("node" + i)); } - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).nodes(nodes).metaData(metaData).routingTable(routingTable).build(); - routingTable = service.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - RoutingNodes routingNodes = clusterState.getRoutingNodes(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).nodes(nodes).metaData(metaData).routingTable(initialRoutingTable).build(); + RoutingAllocation.Result routingResult = service.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("restart all the primary shards, replicas will start initializing"); - routingNodes = clusterState.getRoutingNodes(); - routingTable = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); + RoutingNodes routingNodes = clusterState.getRoutingNodes(); + routingResult = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("start the replica shards"); routingNodes = clusterState.getRoutingNodes(); - routingTable = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); logger.info("complete rebalancing"); - RoutingTable prev = routingTable; - while (true) { - logger.debug("ClusterState: {}", clusterState.getRoutingNodes().prettyPrint()); - routingTable = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); - if (routingTable == prev) - break; - prev = routingTable; - } - - return clusterState; + return applyStartedShardsUntilNoChange(clusterState, service); } private ClusterState addIndex(ClusterState clusterState, AllocationService service, int indexOrdinal, int numberOfShards, @@ -350,37 +312,22 @@ public class AddIncrementallyTests extends ESAllocationTestCase { routingTableBuilder.addAsNew(imd); MetaData metaData = metaDataBuilder.build(); - RoutingTable routingTable = routingTableBuilder.build(); - clusterState = ClusterState.builder(clusterState).metaData(metaData).routingTable(routingTable).build(); - routingTable = service.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - RoutingNodes routingNodes = clusterState.getRoutingNodes(); + clusterState = ClusterState.builder(clusterState).metaData(metaData).routingTable(routingTableBuilder.build()).build(); + RoutingAllocation.Result routingResult = service.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("restart all the primary shards, replicas will start initializing"); - routingNodes = clusterState.getRoutingNodes(); - routingTable = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); + RoutingNodes routingNodes = clusterState.getRoutingNodes(); + routingResult = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("start the replica shards"); routingNodes = clusterState.getRoutingNodes(); - routingTable = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); + routingResult = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("complete rebalancing"); - RoutingTable prev = routingTable; - while (true) { - logger.debug("ClusterState: {}", clusterState.getRoutingNodes().prettyPrint()); - routingTable = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); - if (routingTable == prev) - break; - prev = routingTable; - } - - return clusterState; + return applyStartedShardsUntilNoChange(clusterState, service); } private ClusterState removeNodes(ClusterState clusterState, AllocationService service, int numNodes) { @@ -399,34 +346,23 @@ public class AddIncrementallyTests extends ESAllocationTestCase { clusterState = ClusterState.builder(clusterState).nodes(nodes.build()).build(); clusterState = ClusterState.builder(clusterState) .routingResult(service.deassociateDeadNodes(clusterState, true, "reroute")).build(); - RoutingNodes routingNodes = clusterState.getRoutingNodes(); logger.info("start all the primary shards, replicas will start initializing"); - RoutingTable routingTable = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); + RoutingNodes routingNodes = clusterState.getRoutingNodes(); + RoutingAllocation.Result routingResult = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("start the replica shards"); - routingTable = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); routingNodes = clusterState.getRoutingNodes(); + routingResult = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("rebalancing"); - routingTable = service.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); + routingResult = service.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("complete rebalancing"); - RoutingTable prev = routingTable; - while (true) { - logger.debug("ClusterState: {}", clusterState.getRoutingNodes().prettyPrint()); - routingTable = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); - if (routingTable == prev) - break; - prev = routingTable; - } + clusterState = applyStartedShardsUntilNoChange(clusterState, service); return clusterState; } diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/AllocationCommandsTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/AllocationCommandsTests.java index d62c7b4ada9..33afad14387 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/AllocationCommandsTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/AllocationCommandsTests.java @@ -81,11 +81,11 @@ public class AllocationCommandsTests extends ESAllocationTestCase { logger.info("adding two nodes and performing rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build(); RoutingAllocation.Result rerouteResult = allocation.reroute(clusterState, "reroute"); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); logger.info("start primary shard"); rerouteResult = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); logger.info("move the shard"); String existingNodeId = clusterState.routingTable().index("test").shard(0).primaryShard().currentNodeId(); @@ -97,13 +97,13 @@ public class AllocationCommandsTests extends ESAllocationTestCase { } rerouteResult = allocation.reroute(clusterState, new AllocationCommands(new MoveAllocationCommand("test", 0, existingNodeId, toNodeId)), false, false); assertThat(rerouteResult.changed(), equalTo(true)); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().node(existingNodeId).iterator().next().state(), equalTo(ShardRoutingState.RELOCATING)); assertThat(clusterState.getRoutingNodes().node(toNodeId).iterator().next().state(), equalTo(ShardRoutingState.INITIALIZING)); logger.info("finish moving the shard"); rerouteResult = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().node(existingNodeId).isEmpty(), equalTo(true)); assertThat(clusterState.getRoutingNodes().node(toNodeId).iterator().next().state(), equalTo(ShardRoutingState.STARTED)); @@ -144,7 +144,7 @@ public class AllocationCommandsTests extends ESAllocationTestCase { .add(newNode("node4", singleton(DiscoveryNode.Role.MASTER))) ).build(); RoutingAllocation.Result rerouteResult = allocation.reroute(clusterState, "reroute"); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(0)); logger.info("--> allocating to non-existent node, should fail"); @@ -198,14 +198,14 @@ public class AllocationCommandsTests extends ESAllocationTestCase { logger.info("--> allocating empty primary with acceptDataLoss flag set to true"); rerouteResult = allocation.reroute(clusterState, new AllocationCommands(new AllocateEmptyPrimaryAllocationCommand("test", 0, "node1", true)), false, false); assertThat(rerouteResult.changed(), equalTo(true)); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(INITIALIZING).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(0)); logger.info("--> start the primary shard"); rerouteResult = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(0)); @@ -220,7 +220,7 @@ public class AllocationCommandsTests extends ESAllocationTestCase { logger.info("--> allocate the replica shard on on the second node"); rerouteResult = allocation.reroute(clusterState, new AllocationCommands(new AllocateReplicaAllocationCommand("test", 0, "node2")), false, false); assertThat(rerouteResult.changed(), equalTo(true)); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(1)); @@ -229,7 +229,7 @@ public class AllocationCommandsTests extends ESAllocationTestCase { logger.info("--> start the replica shard"); rerouteResult = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(1)); @@ -265,13 +265,13 @@ public class AllocationCommandsTests extends ESAllocationTestCase { .add(newNode("node3")) ).build(); RoutingAllocation.Result rerouteResult = allocation.reroute(clusterState, "reroute"); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(0)); logger.info("--> allocating empty primary shard with accept_data_loss flag set to true"); rerouteResult = allocation.reroute(clusterState, new AllocationCommands(new AllocateEmptyPrimaryAllocationCommand("test", 0, "node1", true)), false, false); assertThat(rerouteResult.changed(), equalTo(true)); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(INITIALIZING).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(0)); @@ -285,7 +285,7 @@ public class AllocationCommandsTests extends ESAllocationTestCase { logger.info("--> start the primary shard"); rerouteResult = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(0)); @@ -300,7 +300,7 @@ public class AllocationCommandsTests extends ESAllocationTestCase { logger.info("--> allocate the replica shard on on the second node"); rerouteResult = allocation.reroute(clusterState, new AllocationCommands(new AllocateReplicaAllocationCommand("test", 0, "node2")), false, false); assertThat(rerouteResult.changed(), equalTo(true)); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(1)); @@ -309,7 +309,7 @@ public class AllocationCommandsTests extends ESAllocationTestCase { logger.info("--> cancel the relocation allocation"); rerouteResult = allocation.reroute(clusterState, new AllocationCommands(new CancelAllocationCommand("test", 0, "node2", false)), false, false); assertThat(rerouteResult.changed(), equalTo(true)); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(0)); @@ -318,7 +318,7 @@ public class AllocationCommandsTests extends ESAllocationTestCase { logger.info("--> allocate the replica shard on on the second node"); rerouteResult = allocation.reroute(clusterState, new AllocationCommands(new AllocateReplicaAllocationCommand("test", 0, "node2")), false, false); assertThat(rerouteResult.changed(), equalTo(true)); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(1)); @@ -333,7 +333,7 @@ public class AllocationCommandsTests extends ESAllocationTestCase { logger.info("--> start the replica shard"); rerouteResult = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(1)); @@ -342,7 +342,7 @@ public class AllocationCommandsTests extends ESAllocationTestCase { logger.info("--> cancel allocation of the replica shard"); rerouteResult = allocation.reroute(clusterState, new AllocationCommands(new CancelAllocationCommand("test", 0, "node2", false)), false, false); assertThat(rerouteResult.changed(), equalTo(true)); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(0)); @@ -350,7 +350,7 @@ public class AllocationCommandsTests extends ESAllocationTestCase { logger.info("--> allocate the replica shard on on the second node"); rerouteResult = allocation.reroute(clusterState, new AllocationCommands(new AllocateReplicaAllocationCommand("test", 0, "node2")), false, false); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(rerouteResult.changed(), equalTo(true)); assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1)); @@ -358,7 +358,7 @@ public class AllocationCommandsTests extends ESAllocationTestCase { assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(INITIALIZING).size(), equalTo(1)); logger.info("--> start the replica shard"); rerouteResult = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(1)); @@ -366,7 +366,7 @@ public class AllocationCommandsTests extends ESAllocationTestCase { logger.info("--> move the replica shard"); rerouteResult = allocation.reroute(clusterState, new AllocationCommands(new MoveAllocationCommand("test", 0, "node2", "node3")), false, false); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(1)); @@ -377,7 +377,7 @@ public class AllocationCommandsTests extends ESAllocationTestCase { if (randomBoolean()) { logger.info("--> cancel the primary allocation (with allow_primary set to true)"); rerouteResult = allocation.reroute(clusterState, new AllocationCommands(new CancelAllocationCommand("test", 0, "node1", true)), false, false); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(rerouteResult.changed(), equalTo(true)); assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(0)); assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(STARTED).iterator().next().primary(), equalTo(true)); @@ -385,7 +385,7 @@ public class AllocationCommandsTests extends ESAllocationTestCase { } else { logger.info("--> cancel the move of the replica shard"); rerouteResult = allocation.reroute(clusterState, new AllocationCommands(new CancelAllocationCommand("test", 0, "node3", false)), false, false); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(1)); @@ -393,7 +393,7 @@ public class AllocationCommandsTests extends ESAllocationTestCase { logger.info("--> move the replica shard again"); rerouteResult = allocation.reroute(clusterState, new AllocationCommands(new MoveAllocationCommand("test", 0, "node2", "node3")), false, false); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(1)); @@ -403,7 +403,7 @@ public class AllocationCommandsTests extends ESAllocationTestCase { logger.info("--> cancel the source replica shard"); rerouteResult = allocation.reroute(clusterState, new AllocationCommands(new CancelAllocationCommand("test", 0, "node2", false)), false, false); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(0)); @@ -413,7 +413,7 @@ public class AllocationCommandsTests extends ESAllocationTestCase { logger.info("--> start the former target replica shard"); rerouteResult = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(0)); @@ -421,7 +421,7 @@ public class AllocationCommandsTests extends ESAllocationTestCase { logger.info("--> cancel the primary allocation (with allow_primary set to true)"); rerouteResult = allocation.reroute(clusterState, new AllocationCommands(new CancelAllocationCommand("test", 0, "node1", true)), false, false); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(rerouteResult.changed(), equalTo(true)); assertThat(clusterState.getRoutingNodes().node("node3").shardsWithState(STARTED).iterator().next().primary(), equalTo(true)); assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(0)); diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/AllocationPriorityTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/AllocationPriorityTests.java index 7b84bc875d4..7e93fa040d2 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/AllocationPriorityTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/AllocationPriorityTests.java @@ -61,36 +61,36 @@ public class AllocationPriorityTests extends ESAllocationTestCase { .put(IndexMetaData.builder("first").settings(settings(Version.CURRENT).put(IndexMetaData.SETTING_PRIORITY, priorityFirst)).numberOfShards(2).numberOfReplicas(1)) .put(IndexMetaData.builder("second").settings(settings(Version.CURRENT).put(IndexMetaData.SETTING_PRIORITY, prioritySecond)).numberOfShards(2).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = RoutingTable.builder() .addAsNew(metaData.index("first")) .addAsNew(metaData.index("second")) .build(); - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build(); RoutingAllocation.Result rerouteResult = allocation.reroute(clusterState, "reroute"); clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); - routingTable = allocation.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = allocation.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertEquals(2, clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size()); assertEquals(highPriorityName, clusterState.getRoutingNodes().shardsWithState(INITIALIZING).get(0).getIndexName()); assertEquals(highPriorityName, clusterState.getRoutingNodes().shardsWithState(INITIALIZING).get(1).getIndexName()); - routingTable = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertEquals(2, clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size()); assertEquals(lowPriorityName, clusterState.getRoutingNodes().shardsWithState(INITIALIZING).get(0).getIndexName()); assertEquals(lowPriorityName, clusterState.getRoutingNodes().shardsWithState(INITIALIZING).get(1).getIndexName()); - routingTable = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertEquals(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).toString(),2, clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size()); assertEquals(highPriorityName, clusterState.getRoutingNodes().shardsWithState(INITIALIZING).get(0).getIndexName()); assertEquals(highPriorityName, clusterState.getRoutingNodes().shardsWithState(INITIALIZING).get(1).getIndexName()); - routingTable = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertEquals(2, clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size()); assertEquals(lowPriorityName, clusterState.getRoutingNodes().shardsWithState(INITIALIZING).get(0).getIndexName()); assertEquals(lowPriorityName, clusterState.getRoutingNodes().shardsWithState(INITIALIZING).get(1).getIndexName()); diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/AwarenessAllocationTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/AwarenessAllocationTests.java index 44a4cdb2d43..bf71fa766ed 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/AwarenessAllocationTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/AwarenessAllocationTests.java @@ -64,28 +64,26 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() - .addAsNew(metaData.index("test")) - .build(); + RoutingTable initialRoutingTable = 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(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("--> adding two nodes on same rack and do rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder() .add(newNode("node1", singletonMap("rack_id", "1"))) .add(newNode("node2", singletonMap("rack_id", "1"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(1)); logger.info("--> start the shards (primaries)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("--> start the shards (replicas)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(2)); @@ -93,16 +91,16 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node3", singletonMap("rack_id", "2"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.RELOCATING).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.RELOCATING).get(0).relocatingNodeId(), equalTo("node3")); logger.info("--> complete relocation"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(2)); @@ -113,9 +111,9 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node4", singletonMap("rack_id", "3"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - assertThat(routingTable, sameInstance(clusterState.routingTable())); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + assertFalse(routingResult.changed()); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(2)); } @@ -132,11 +130,9 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() - .addAsNew(metaData.index("test")) - .build(); + RoutingTable initialRoutingTable = 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(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("--> adding two nodes on same rack and do rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder() @@ -144,17 +140,17 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { .add(newNode("node2", singletonMap("rack_id", "1"))) .add(newNode("node3", singletonMap("rack_id", "1"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(1)); logger.info("--> start the shards (primaries)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("--> start the shards (replicas)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(2)); @@ -162,16 +158,16 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node4", singletonMap("rack_id", "2"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.RELOCATING).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.RELOCATING).get(0).relocatingNodeId(), equalTo("node4")); logger.info("--> complete relocation"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(2)); @@ -182,9 +178,9 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node5", singletonMap("rack_id", "3"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - assertThat(routingTable, sameInstance(clusterState.routingTable())); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + assertFalse(routingResult.changed()); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(2)); } @@ -206,19 +202,19 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(5).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = 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(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("--> adding two nodes on same rack and do rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder() .add(newNode("node1", singletonMap("rack_id", "1"))) .add(newNode("node2", singletonMap("rack_id", "1"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("Initializing shards: {}", clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); logger.info("Started shards: {}", clusterState.getRoutingNodes().shardsWithState(STARTED)); @@ -228,12 +224,12 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(5)); logger.info("--> start the shards (primaries)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("--> start the shards (replicas)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(10)); @@ -241,8 +237,8 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node3", singletonMap("rack_id", "2"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(5)); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.RELOCATING).size(), equalTo(5)); @@ -250,12 +246,12 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.RELOCATING).get(0).relocatingNodeId(), equalTo("node3")); logger.info("--> complete initializing"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("--> run it again, since we still might have relocation"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(10)); @@ -266,13 +262,13 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node4", singletonMap("rack_id", "3"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(RELOCATING).size(), greaterThan(0)); logger.info("--> complete relocation"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(10)); @@ -296,29 +292,29 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test2").settings(settings(Version.CURRENT)).numberOfShards(5).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = RoutingTable.builder() .addAsNew(metaData.index("test1")) .addAsNew(metaData.index("test2")) .build(); - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("--> adding two nodes on same rack and do rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder() .add(newNode("node1", singletonMap("rack_id", "1"))) .add(newNode("node2", singletonMap("rack_id", "1"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(10)); logger.info("--> start the shards (primaries)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("--> start the shards (replicas)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(20)); @@ -326,8 +322,8 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node3", singletonMap("rack_id", "2"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(10)); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.RELOCATING).size(), equalTo(10)); @@ -337,11 +333,11 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { logger.info("--> complete initializing"); for (int i = 0; i < 2; i++) { logger.info("--> complete initializing round: [{}]", i); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); } - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(20)); assertThat(clusterState.getRoutingNodes().node("node3").size(), equalTo(10)); @@ -355,15 +351,15 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node4", singletonMap("rack_id", "3"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(RELOCATING).size(), greaterThan(0)); logger.info("--> complete relocation"); for (int i = 0; i < 2; i++) { logger.info("--> complete initializing round: [{}]", i); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); } assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(20)); assertThat(clusterState.getRoutingNodes().node("node3").size(), equalTo(5)); @@ -388,28 +384,28 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(2)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = 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(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("--> adding two nodes on same rack and do rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder() .add(newNode("node1", singletonMap("rack_id", "1"))) .add(newNode("node2", singletonMap("rack_id", "1"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(1)); logger.info("--> start the shards (primaries)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("--> start the shards (replicas)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(2)); @@ -417,16 +413,16 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node3", singletonMap("rack_id", "2"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(2)); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.INITIALIZING).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.INITIALIZING).get(0).currentNodeId(), equalTo("node3")); logger.info("--> complete relocation"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(3)); @@ -437,15 +433,15 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node4", singletonMap("rack_id", "3"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(2)); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.RELOCATING).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.RELOCATING).get(0).relocatingNodeId(), equalTo("node4")); logger.info("--> complete relocation"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(3)); @@ -466,11 +462,11 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(3)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = 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(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("--> adding two nodes on same rack and do rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder() @@ -479,17 +475,17 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { .add(newNode("node3", singletonMap("rack_id", "1"))) .add(newNode("node4", singletonMap("rack_id", "1"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(1)); logger.info("--> start the shards (primaries)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("--> start the shards (replicas)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(4)); @@ -497,16 +493,16 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node5", singletonMap("rack_id", "2"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(3)); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.RELOCATING).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.RELOCATING).get(0).relocatingNodeId(), equalTo("node5")); logger.info("--> complete relocation"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(4)); @@ -517,15 +513,15 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node6", singletonMap("rack_id", "3"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(3)); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.RELOCATING).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.RELOCATING).get(0).relocatingNodeId(), equalTo("node6")); logger.info("--> complete relocation"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(4)); @@ -547,24 +543,24 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = 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(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("--> adding two nodes on same rack and do rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder() .add(newNode("node1", singletonMap("rack_id", "1"))) .add(newNode("node2", singletonMap("rack_id", "1"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(1)); logger.info("--> start the shards (primaries)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("--> replica will not start because we have only one rack value"); assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(1)); @@ -574,16 +570,16 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node3", singletonMap("rack_id", "2"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.INITIALIZING).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.INITIALIZING).get(0).currentNodeId(), equalTo("node3")); logger.info("--> complete relocation"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(2)); @@ -594,9 +590,9 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node4", singletonMap("rack_id", "3"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - assertThat(routingTable, sameInstance(clusterState.routingTable())); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + assertFalse(routingResult.changed()); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(2)); } @@ -614,11 +610,11 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = 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(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("--> adding two nodes on same rack and do rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder() @@ -626,13 +622,13 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { .add(newNode("node2", singletonMap("rack_id", "1"))) .add(newNode("node3", singletonMap("rack_id", "1"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(1)); logger.info("--> start the shards (primaries)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("--> replica will not start because we have only one rack value"); assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(1)); @@ -642,16 +638,16 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node4", singletonMap("rack_id", "2"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.INITIALIZING).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.INITIALIZING).get(0).currentNodeId(), equalTo("node4")); logger.info("--> complete relocation"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(2)); @@ -662,9 +658,9 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node5", singletonMap("rack_id", "3"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - assertThat(routingTable, sameInstance(clusterState.routingTable())); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + assertFalse(routingResult.changed()); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(2)); } @@ -688,25 +684,25 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test2").settings(settings(Version.CURRENT)).numberOfShards(5).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = RoutingTable.builder() .addAsNew(metaData.index("test1")) .addAsNew(metaData.index("test2")) .build(); - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("--> adding two nodes on same rack and do rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder() .add(newNode("node1", singletonMap("rack_id", "1"))) .add(newNode("node2", singletonMap("rack_id", "1"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(10)); logger.info("--> start the shards (primaries)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(10)); @@ -714,20 +710,20 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node3", singletonMap("rack_id", "2"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(10)); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.INITIALIZING).size(), equalTo(10)); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.INITIALIZING).get(0).currentNodeId(), equalTo("node3")); logger.info("--> complete initializing"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("--> run it again, since we still might have relocation"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(20)); @@ -738,13 +734,13 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node4", singletonMap("rack_id", "3"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(RELOCATING).size(), greaterThan(0)); logger.info("--> complete relocation"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(20)); @@ -768,30 +764,30 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(5).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = 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(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("--> adding two nodes in different zones and do rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder() .add(newNode("A-0", singletonMap("zone", "a"))) .add(newNode("B-0", singletonMap("zone", "b"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(0)); assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(5)); logger.info("--> start the shards (primaries)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(5)); assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(5)); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("--> all replicas are allocated and started since we have on node in each zone"); assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(10)); assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(0)); @@ -800,15 +796,15 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("A-1", singletonMap("zone", "a"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(8)); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.INITIALIZING).size(), equalTo(2)); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.INITIALIZING).get(0).currentNodeId(), equalTo("A-1")); logger.info("--> starting initializing shards on the new node"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(10)); assertThat(clusterState.getRoutingNodes().node("A-1").size(), equalTo(2)); @@ -829,11 +825,11 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(4)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = 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(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("--> adding 5 nodes in different zones and do rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder() @@ -844,14 +840,14 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { .add(newNode("A-4", singletonMap("zone", "a"))) .add(newNode("B-0", singletonMap("zone", "b"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(0)); assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(1)); logger.info("--> start the shard (primary)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(3)); assertThat(clusterState.getRoutingNodes().shardsWithState(UNASSIGNED).size(), equalTo(1)); // Unassigned shard is expected. @@ -859,7 +855,7 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { // Cancel all initializing shards and move started primary to another node. AllocationCommands commands = new AllocationCommands(); String primaryNode = null; - for (ShardRouting routing : routingTable.allShards()) { + for (ShardRouting routing : clusterState.routingTable().allShards()) { if (routing.primary()) { primaryNode = routing.currentNodeId(); } else if (routing.initializing()) { @@ -868,8 +864,8 @@ public class AwarenessAllocationTests extends ESAllocationTestCase { } commands.add(new MoveAllocationCommand("test", 0, primaryNode, "A-4")); - routingTable = strategy.reroute(clusterState, commands, false, false).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, commands, false, false); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(0)); assertThat(clusterState.getRoutingNodes().shardsWithState(RELOCATING).size(), equalTo(1)); diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/BalanceConfigurationTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/BalanceConfigurationTests.java index 07c0d47960e..f45567d280a 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/BalanceConfigurationTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/BalanceConfigurationTests.java @@ -81,7 +81,6 @@ public class BalanceConfigurationTests extends ESAllocationTestCase { clusterState = removeNodes(clusterState, strategy); assertIndexBalance(clusterState.getRoutingTable(), clusterState.getRoutingNodes(), (numberOfNodes + 1) - (numberOfNodes + 1) / 2, numberOfIndices, numberOfReplicas, numberOfShards, balanceTreshold); - } public void testReplicaBalance() { @@ -124,7 +123,7 @@ public class BalanceConfigurationTests extends ESAllocationTestCase { routingTableBuilder.addAsNew(cursor.value); } - RoutingTable routingTable = routingTableBuilder.build(); + RoutingTable initialRoutingTable = routingTableBuilder.build(); logger.info("start " + numberOfNodes + " nodes"); @@ -132,35 +131,22 @@ public class BalanceConfigurationTests extends ESAllocationTestCase { for (int i = 0; i < numberOfNodes; i++) { nodes.add(newNode("node" + i)); } - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).nodes(nodes).metaData(metaData).routingTable(routingTable).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - RoutingNodes routingNodes = clusterState.getRoutingNodes(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).nodes(nodes).metaData(metaData).routingTable(initialRoutingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("restart all the primary shards, replicas will start initializing"); - routingNodes = clusterState.getRoutingNodes(); - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); + RoutingNodes routingNodes = clusterState.getRoutingNodes(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("start the replica shards"); routingNodes = clusterState.getRoutingNodes(); - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("complete rebalancing"); - RoutingTable prev = routingTable; - while (true) { - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); - if (routingTable == prev) - break; - prev = routingTable; - } - - return clusterState; + return applyStartedShardsUntilNoChange(clusterState, strategy); } private ClusterState addNode(ClusterState clusterState, AllocationService strategy) { @@ -171,21 +157,9 @@ public class BalanceConfigurationTests extends ESAllocationTestCase { RoutingTable routingTable = strategy.reroute(clusterState, "reroute").routingTable(); clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - RoutingNodes routingNodes = clusterState.getRoutingNodes(); // move initializing to started - - RoutingTable prev = routingTable; - while (true) { - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); - if (routingTable == prev) - break; - prev = routingTable; - } - - return clusterState; + return applyStartedShardsUntilNoChange(clusterState, strategy); } private ClusterState removeNodes(ClusterState clusterState, AllocationService strategy) { @@ -204,35 +178,23 @@ public class BalanceConfigurationTests extends ESAllocationTestCase { strategy.deassociateDeadNodes(clusterState, randomBoolean(), "removed nodes") ).build(); } - RoutingNodes routingNodes = clusterState.getRoutingNodes(); logger.info("start all the primary shards, replicas will start initializing"); - RoutingTable routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); + RoutingNodes routingNodes = clusterState.getRoutingNodes(); + RoutingAllocation.Result routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("start the replica shards"); - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); routingNodes = clusterState.getRoutingNodes(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("rebalancing"); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("complete rebalancing"); - RoutingTable prev = routingTable; - while (true) { - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); - if (routingTable == prev) - break; - prev = routingTable; - } - - return clusterState; + return applyStartedShardsUntilNoChange(clusterState, strategy); } diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/BalanceUnbalancedClusterTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/BalanceUnbalancedClusterTests.java index bcefe46884e..3890be6a4ba 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/BalanceUnbalancedClusterTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/BalanceUnbalancedClusterTests.java @@ -60,22 +60,22 @@ public class BalanceUnbalancedClusterTests extends CatAllocationTestCase { .put(IndexMetaData.builder(index).settings(settings(Version.CURRENT)).numberOfShards(5).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder(state.routingTable()) + RoutingTable initialRoutingTable = RoutingTable.builder(state.routingTable()) .addAsNew(metaData.index(index)) .build(); - ClusterState clusterState = ClusterState.builder(state).metaData(metaData).routingTable(routingTable).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + ClusterState clusterState = ClusterState.builder(state).metaData(metaData).routingTable(initialRoutingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); while (true) { - if (routingTable.shardsWithState(INITIALIZING).isEmpty()) { + if (clusterState.routingTable().shardsWithState(INITIALIZING).isEmpty()) { break; } - routingTable = strategy.applyStartedShards(clusterState, routingTable.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.routingTable().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); } Map counts = new HashMap<>(); - for (IndexShardRoutingTable table : routingTable.index(index)) { + for (IndexShardRoutingTable table : clusterState.routingTable().index(index)) { for (ShardRouting r : table) { String s = r.currentNodeId(); Integer count = counts.get(s); diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/CatAllocationTestCase.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/CatAllocationTestCase.java index 2eb43e15c03..6a08634e9fa 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/CatAllocationTestCase.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/CatAllocationTestCase.java @@ -96,8 +96,21 @@ public abstract class CatAllocationTestCase extends ESAllocationTestCase { MetaData.Builder builder = MetaData.builder(); RoutingTable.Builder routingTableBuilder = RoutingTable.builder(); for(Idx idx : indices.values()) { - IndexMetaData idxMeta = IndexMetaData.builder(idx.name).settings(settings(Version.CURRENT)) - .numberOfShards(idx.numShards()).numberOfReplicas(idx.numReplicas()).build(); + IndexMetaData.Builder idxMetaBuilder = IndexMetaData.builder(idx.name).settings(settings(Version.CURRENT)) + .numberOfShards(idx.numShards()).numberOfReplicas(idx.numReplicas()); + for (ShardRouting shardRouting : idx.routing) { + if (shardRouting.active()) { + Set allocationIds = idxMetaBuilder.getActiveAllocationIds(shardRouting.id()); + if (allocationIds == null) { + allocationIds = new HashSet<>(); + } else { + allocationIds = new HashSet<>(allocationIds); + } + allocationIds.add(shardRouting.allocationId().getId()); + idxMetaBuilder.putActiveAllocationIds(shardRouting.id(), allocationIds); + } + } + IndexMetaData idxMeta = idxMetaBuilder.build(); builder.put(idxMeta, false); IndexRoutingTable.Builder tableBuilder = new IndexRoutingTable.Builder(idxMeta.getIndex()).initializeAsRecovery(idxMeta); Map shardIdToRouting = new HashMap<>(); @@ -107,7 +120,6 @@ public abstract class CatAllocationTestCase extends ESAllocationTestCase { refData = new IndexShardRoutingTable.Builder(shardIdToRouting.get(r.getId())).addShard(r).build(); } shardIdToRouting.put(r.getId(), refData); - } for (IndexShardRoutingTable t: shardIdToRouting.values()) { tableBuilder.addIndexShard(t); @@ -139,20 +151,18 @@ public abstract class CatAllocationTestCase extends ESAllocationTestCase { private ClusterState rebalance(ClusterState clusterState) { RoutingTable routingTable;AllocationService strategy = createAllocationService(Settings.builder() .build()); - RoutingAllocation.Result reroute = strategy.reroute(clusterState, "reroute"); - routingTable = reroute.routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingTable = clusterState.routingTable(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); int numRelocations = 0; while (true) { - List initializing = routingTable.shardsWithState(INITIALIZING); + List initializing = clusterState.routingTable().shardsWithState(INITIALIZING); if (initializing.isEmpty()) { break; } logger.debug("Initializing shards: {}", initializing); numRelocations += initializing.size(); - routingTable = strategy.applyStartedShards(clusterState, initializing).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, initializing); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); } logger.debug("--> num relocations to get balance: {}", numRelocations); return clusterState; diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/ClusterRebalanceRoutingTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/ClusterRebalanceRoutingTests.java index 3c2d4fbc5fe..9346bebd172 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/ClusterRebalanceRoutingTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/ClusterRebalanceRoutingTests.java @@ -56,76 +56,72 @@ public class ClusterRebalanceRoutingTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test2").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = RoutingTable.builder() .addAsNew(metaData.index("test1")) .addAsNew(metaData.index("test2")) .build(); - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("start two nodes"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build(); - RoutingTable prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test1").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } - for (int i = 0; i < routingTable.index("test2").shards().size(); i++) { - assertThat(routingTable.index("test2").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + for (int i = 0; i < clusterState.routingTable().index("test2").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test2").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } logger.info("start all the primary shards for test1, replicas will start initializing"); RoutingNodes routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test1", INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test1", INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).shards().size(), equalTo(2)); -// assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test1").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(2)); +// assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); } - for (int i = 0; i < routingTable.index("test2").shards().size(); i++) { - assertThat(routingTable.index("test2").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + for (int i = 0; i < clusterState.routingTable().index("test2").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test2").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } logger.info("start the test1 replica shards"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test1", INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test1", INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test1").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); } - for (int i = 0; i < routingTable.index("test2").shards().size(); i++) { - assertThat(routingTable.index("test2").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + for (int i = 0; i < clusterState.routingTable().index("test2").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test2").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } logger.info("now, start 1 more node, check that rebalancing will happen (for test1) because we set it to always"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node3"))) .build(); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); assertThat(routingNodes.node("node3").size(), equalTo(1)); @@ -142,95 +138,90 @@ public class ClusterRebalanceRoutingTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test2").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = RoutingTable.builder() .addAsNew(metaData.index("test1")) .addAsNew(metaData.index("test2")) .build(); - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("start two nodes"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build(); - RoutingTable prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test1").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } - for (int i = 0; i < routingTable.index("test2").shards().size(); i++) { - assertThat(routingTable.index("test2").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + for (int i = 0; i < clusterState.routingTable().index("test2").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test2").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } logger.info("start all the primary shards for test1, replicas will start initializing"); RoutingNodes routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test1", INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test1", INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test1").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); } - for (int i = 0; i < routingTable.index("test2").shards().size(); i++) { - assertThat(routingTable.index("test2").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + for (int i = 0; i < clusterState.routingTable().index("test2").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test2").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } logger.info("start the test1 replica shards"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test1", INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test1", INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test1").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); } - for (int i = 0; i < routingTable.index("test2").shards().size(); i++) { - assertThat(routingTable.index("test2").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + for (int i = 0; i < clusterState.routingTable().index("test2").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test2").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } logger.info("start all the primary shards for test2, replicas will start initializing"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test2", INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test2", INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test1").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); } - for (int i = 0; i < routingTable.index("test2").shards().size(); i++) { - assertThat(routingTable.index("test2").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test2").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test2").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); + for (int i = 0; i < clusterState.routingTable().index("test2").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test2").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test2").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test2").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); } logger.info("now, start 1 more node, check that rebalancing happen (for test1) because we set it to primaries_active"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node3"))) .build(); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); assertThat(routingNodes.node("node3").size(), equalTo(1)); @@ -246,76 +237,72 @@ public class ClusterRebalanceRoutingTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test2").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = RoutingTable.builder() .addAsNew(metaData.index("test1")) .addAsNew(metaData.index("test2")) .build(); - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("start two nodes"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build(); - RoutingTable prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test1").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } - for (int i = 0; i < routingTable.index("test2").shards().size(); i++) { - assertThat(routingTable.index("test2").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + for (int i = 0; i < clusterState.routingTable().index("test2").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test2").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } logger.info("start all the primary shards for test1, replicas will start initializing"); RoutingNodes routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test1", INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test1", INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test1").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); } - for (int i = 0; i < routingTable.index("test2").shards().size(); i++) { - assertThat(routingTable.index("test2").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + for (int i = 0; i < clusterState.routingTable().index("test2").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test2").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } logger.info("start the test1 replica shards"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test1", INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test1", INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test1").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); } - for (int i = 0; i < routingTable.index("test2").shards().size(); i++) { - assertThat(routingTable.index("test2").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + for (int i = 0; i < clusterState.routingTable().index("test2").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test2").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } logger.info("now, start 1 more node, check that rebalancing will not happen (for test1) because we set it to primaries_active"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node3"))) .build(); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); assertThat(routingNodes.node("node3").isEmpty(), equalTo(true)); @@ -330,114 +317,108 @@ public class ClusterRebalanceRoutingTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test2").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = RoutingTable.builder() .addAsNew(metaData.index("test1")) .addAsNew(metaData.index("test2")) .build(); - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("start two nodes"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build(); - RoutingTable prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test1").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } - for (int i = 0; i < routingTable.index("test2").shards().size(); i++) { - assertThat(routingTable.index("test2").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + for (int i = 0; i < clusterState.routingTable().index("test2").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test2").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } logger.info("start all the primary shards for test1, replicas will start initializing"); RoutingNodes routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test1", INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test1", INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test1").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); } - for (int i = 0; i < routingTable.index("test2").shards().size(); i++) { - assertThat(routingTable.index("test2").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + for (int i = 0; i < clusterState.routingTable().index("test2").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test2").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } logger.info("start the test1 replica shards"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test1", INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test1", INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test1").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); } - for (int i = 0; i < routingTable.index("test2").shards().size(); i++) { - assertThat(routingTable.index("test2").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + for (int i = 0; i < clusterState.routingTable().index("test2").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test2").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } logger.info("start all the primary shards for test2, replicas will start initializing"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test2", INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test2", INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test1").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); } - for (int i = 0; i < routingTable.index("test2").shards().size(); i++) { - assertThat(routingTable.index("test2").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test2").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test2").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); + for (int i = 0; i < clusterState.routingTable().index("test2").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test2").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test2").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test2").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); } logger.info("start the test2 replica shards"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test2", INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test2", INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test1").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); } - for (int i = 0; i < routingTable.index("test2").shards().size(); i++) { - assertThat(routingTable.index("test2").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test2").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test2").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); + for (int i = 0; i < clusterState.routingTable().index("test2").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test2").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test2").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test2").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); } logger.info("now, start 1 more node, check that rebalancing happen (for test1) because we set it to all_active"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node3"))) .build(); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); assertThat(routingNodes.node("node3").size(), equalTo(1)); @@ -453,76 +434,72 @@ public class ClusterRebalanceRoutingTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test2").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = RoutingTable.builder() .addAsNew(metaData.index("test1")) .addAsNew(metaData.index("test2")) .build(); - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("start two nodes"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build(); - RoutingTable prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test1").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } - for (int i = 0; i < routingTable.index("test2").shards().size(); i++) { - assertThat(routingTable.index("test2").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + for (int i = 0; i < clusterState.routingTable().index("test2").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test2").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } logger.info("start all the primary shards for test1, replicas will start initializing"); RoutingNodes routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test1", INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test1", INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test1").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); } - for (int i = 0; i < routingTable.index("test2").shards().size(); i++) { - assertThat(routingTable.index("test2").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + for (int i = 0; i < clusterState.routingTable().index("test2").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test2").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } logger.info("start the test1 replica shards"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test1", INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test1", INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test1").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); } - for (int i = 0; i < routingTable.index("test2").shards().size(); i++) { - assertThat(routingTable.index("test2").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + for (int i = 0; i < clusterState.routingTable().index("test2").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test2").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } logger.info("now, start 1 more node, check that rebalancing will not happen (for test1) because we set it to all_active"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node3"))) .build(); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); assertThat(routingNodes.node("node3").isEmpty(), equalTo(true)); @@ -537,95 +514,90 @@ public class ClusterRebalanceRoutingTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test2").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = RoutingTable.builder() .addAsNew(metaData.index("test1")) .addAsNew(metaData.index("test2")) .build(); - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("start two nodes"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build(); - RoutingTable prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test1").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } - for (int i = 0; i < routingTable.index("test2").shards().size(); i++) { - assertThat(routingTable.index("test2").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + for (int i = 0; i < clusterState.routingTable().index("test2").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test2").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } logger.info("start all the primary shards for test1, replicas will start initializing"); RoutingNodes routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test1", INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test1", INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test1").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); } - for (int i = 0; i < routingTable.index("test2").shards().size(); i++) { - assertThat(routingTable.index("test2").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + for (int i = 0; i < clusterState.routingTable().index("test2").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test2").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } logger.info("start the test1 replica shards"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test1", INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test1", INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test1").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); } - for (int i = 0; i < routingTable.index("test2").shards().size(); i++) { - assertThat(routingTable.index("test2").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + for (int i = 0; i < clusterState.routingTable().index("test2").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test2").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test2").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test2").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } logger.info("start all the primary shards for test2, replicas will start initializing"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test2", INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test2", INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test1").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); } - for (int i = 0; i < routingTable.index("test2").shards().size(); i++) { - assertThat(routingTable.index("test2").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test2").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test2").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); + for (int i = 0; i < clusterState.routingTable().index("test2").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test2").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test2").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test2").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); } logger.info("now, start 1 more node, check that rebalancing will not happen (for test1) because we set it to all_active"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node3"))) .build(); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); assertThat(routingNodes.node("node3").isEmpty(), equalTo(true)); @@ -657,31 +629,31 @@ public class ClusterRebalanceRoutingTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test1").settings(settings(Version.CURRENT)).numberOfShards(2).numberOfReplicas(0)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = RoutingTable.builder() .addAsNew(metaData.index("test")) .addAsNew(metaData.index("test1")) .build(); - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("start two nodes"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1"))).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(INITIALIZING)); } logger.debug("start all the primary shards for test"); RoutingNodes routingNodes = clusterState.getRoutingNodes(); - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test", INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test", INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(STARTED)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED)); } logger.debug("now, start 1 more node, check that rebalancing will not happen since we unassigned shards"); @@ -691,44 +663,45 @@ public class ClusterRebalanceRoutingTests extends ESAllocationTestCase { logger.debug("reroute and check that nothing has changed"); RoutingAllocation.Result reroute = strategy.reroute(clusterState, "reroute"); assertFalse(reroute.changed()); - routingTable = reroute.routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = reroute; + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(STARTED)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED)); } - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).shards().size(), equalTo(1)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(UNASSIGNED)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(UNASSIGNED)); } logger.debug("now set allocateTest1 to true and reroute we should see the [test1] index initializing"); allocateTest1.set(true); reroute = strategy.reroute(clusterState, "reroute"); assertTrue(reroute.changed()); - routingTable = reroute.routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).shards().size(), equalTo(1)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + routingResult = reroute; + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(INITIALIZING)); } logger.debug("now start initializing shards and expect exactly one rebalance from node1 to node 2 since index [test] is all on node1"); routingNodes = clusterState.getRoutingNodes(); - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test1", INITIALIZING)).routingTable(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test1", INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).shards().size(), equalTo(1)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); } int numStarted = 0; int numRelocating = 0; - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(1)); - if (routingTable.index("test").shard(i).primaryShard().state() == STARTED) { + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(1)); + if (clusterState.routingTable().index("test").shard(i).primaryShard().state() == STARTED) { numStarted++; - } else if (routingTable.index("test").shard(i).primaryShard().state() == RELOCATING) { + } else if (clusterState.routingTable().index("test").shard(i).primaryShard().state() == RELOCATING) { numRelocating++; } } @@ -756,31 +729,31 @@ public class ClusterRebalanceRoutingTests extends ESAllocationTestCase { .build(); // we use a second index here (test1) that never gets assigned otherwise allocateUnassigned is never called if we don't have unassigned shards. - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = RoutingTable.builder() .addAsNew(metaData.index("test")) .addAsNew(metaData.index("test1")) .build(); - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("start two nodes"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1"))).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(INITIALIZING)); } logger.debug("start all the primary shards for test"); RoutingNodes routingNodes = clusterState.getRoutingNodes(); - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test", INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test", INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(STARTED)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED)); } logger.debug("now, start 1 more node, check that rebalancing will not happen since we have shard sync going on"); @@ -788,41 +761,38 @@ public class ClusterRebalanceRoutingTests extends ESAllocationTestCase { .add(newNode("node2"))) .build(); logger.debug("reroute and check that nothing has changed"); - RoutingAllocation.Result reroute = strategy.reroute(clusterState, "reroute"); - assertFalse(reroute.changed()); - routingTable = reroute.routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + assertFalse(routingResult.changed()); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(STARTED)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED)); } - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).shards().size(), equalTo(1)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(UNASSIGNED)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(UNASSIGNED)); } logger.debug("now set hasFetches to true and reroute we should now see exactly one relocating shard"); hasFetches.set(false); - reroute = strategy.reroute(clusterState, "reroute"); - assertTrue(reroute.changed()); - routingTable = reroute.routingTable(); + routingResult = strategy.reroute(clusterState, "reroute"); + assertTrue(routingResult.changed()); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); int numStarted = 0; int numRelocating = 0; - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(1)); - if (routingTable.index("test").shard(i).primaryShard().state() == STARTED) { + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(1)); + if (clusterState.routingTable().index("test").shard(i).primaryShard().state() == STARTED) { numStarted++; - } else if (routingTable.index("test").shard(i).primaryShard().state() == RELOCATING) { + } else if (clusterState.routingTable().index("test").shard(i).primaryShard().state() == RELOCATING) { numRelocating++; } } - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).shards().size(), equalTo(1)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(UNASSIGNED)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(UNASSIGNED)); } assertEquals(numStarted, 1); assertEquals(numRelocating, 1); - } } diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/ConcurrentRebalanceRoutingTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/ConcurrentRebalanceRoutingTests.java index 05242836270..36565e1f85e 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/ConcurrentRebalanceRoutingTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/ConcurrentRebalanceRoutingTests.java @@ -53,96 +53,90 @@ public class ConcurrentRebalanceRoutingTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(5).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = 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(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); - assertThat(routingTable.index("test").shards().size(), equalTo(5)); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).shards().get(0).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(i).shards().get(1).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(i).shards().get(0).currentNodeId(), nullValue()); - assertThat(routingTable.index("test").shard(i).shards().get(1).currentNodeId(), nullValue()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(5)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(0).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(1).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(0).currentNodeId(), nullValue()); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(1).currentNodeId(), nullValue()); } logger.info("start two nodes and fully start the shards"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } logger.info("start all the primary shards, replicas will start initializing"); RoutingNodes routingNodes = clusterState.getRoutingNodes(); - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); } logger.info("now, start 8 more nodes, and check that no rebalancing/relocation have happened"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node3")).add(newNode("node4")).add(newNode("node5")).add(newNode("node6")).add(newNode("node7")).add(newNode("node8")).add(newNode("node9")).add(newNode("node10"))) .build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); } logger.info("start the replica shards, rebalancing should start, but, only 3 should be rebalancing"); routingNodes = clusterState.getRoutingNodes(); - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); // we only allow one relocation at a time - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(7)); - assertThat(routingTable.shardsWithState(RELOCATING).size(), equalTo(3)); + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(7)); + assertThat(clusterState.routingTable().shardsWithState(RELOCATING).size(), equalTo(3)); logger.info("finalize this session relocation, 3 more should relocate now"); routingNodes = clusterState.getRoutingNodes(); - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); // we only allow one relocation at a time - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(7)); - assertThat(routingTable.shardsWithState(RELOCATING).size(), equalTo(3)); + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(7)); + assertThat(clusterState.routingTable().shardsWithState(RELOCATING).size(), equalTo(3)); logger.info("finalize this session relocation, 2 more should relocate now"); routingNodes = clusterState.getRoutingNodes(); - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); // we only allow one relocation at a time - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(8)); - assertThat(routingTable.shardsWithState(RELOCATING).size(), equalTo(2)); + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(8)); + assertThat(clusterState.routingTable().shardsWithState(RELOCATING).size(), equalTo(2)); logger.info("finalize this session relocation, no more relocation"); routingNodes = clusterState.getRoutingNodes(); - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); // we only allow one relocation at a time - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(10)); - assertThat(routingTable.shardsWithState(RELOCATING).size(), equalTo(0)); + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(10)); + assertThat(clusterState.routingTable().shardsWithState(RELOCATING).size(), equalTo(0)); } } diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/DeadNodesAllocationTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/DeadNodesAllocationTests.java index f3c1d4d5c6f..abc69cbf91c 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/DeadNodesAllocationTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/DeadNodesAllocationTests.java @@ -65,14 +65,14 @@ public class DeadNodesAllocationTests extends ESAllocationTestCase { ).build(); RoutingAllocation.Result rerouteResult = allocation.reroute(clusterState, "reroute"); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); // starting primaries rerouteResult = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); // starting replicas rerouteResult = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); logger.info("--> verifying all is allocated"); assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1)); @@ -88,7 +88,7 @@ public class DeadNodesAllocationTests extends ESAllocationTestCase { ).build(); rerouteResult = allocation.deassociateDeadNodes(clusterState, true, "reroute"); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().node(nodeIdRemaining).iterator().next().primary(), equalTo(true)); assertThat(clusterState.getRoutingNodes().node(nodeIdRemaining).iterator().next().state(), equalTo(STARTED)); @@ -116,14 +116,14 @@ public class DeadNodesAllocationTests extends ESAllocationTestCase { ).build(); RoutingAllocation.Result rerouteResult = allocation.reroute(clusterState, "reroute"); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); // starting primaries rerouteResult = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); // starting replicas rerouteResult = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); logger.info("--> verifying all is allocated"); assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1)); @@ -136,7 +136,7 @@ public class DeadNodesAllocationTests extends ESAllocationTestCase { .add(newNode("node3")) ).build(); rerouteResult = allocation.reroute(clusterState, "reroute"); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node1").iterator().next().state(), equalTo(STARTED)); @@ -152,7 +152,7 @@ public class DeadNodesAllocationTests extends ESAllocationTestCase { new MoveAllocationCommand("test", 0, clusterState.routingTable().index("test").shard(0).primaryShard().currentNodeId(), "node3")), false, false); assertThat(rerouteResult.changed(), equalTo(true)); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().node(origPrimaryNodeId).iterator().next().state(), equalTo(RELOCATING)); assertThat(clusterState.getRoutingNodes().node("node3").iterator().next().state(), equalTo(INITIALIZING)); @@ -162,7 +162,7 @@ public class DeadNodesAllocationTests extends ESAllocationTestCase { .add(newNode(origReplicaNodeId)) ).build(); rerouteResult = allocation.deassociateDeadNodes(clusterState, true, "reroute"); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().node(origPrimaryNodeId).iterator().next().state(), equalTo(STARTED)); assertThat(clusterState.getRoutingNodes().node(origReplicaNodeId).iterator().next().state(), equalTo(STARTED)); @@ -190,14 +190,14 @@ public class DeadNodesAllocationTests extends ESAllocationTestCase { ).build(); RoutingAllocation.Result rerouteResult = allocation.reroute(clusterState, "reroute"); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); // starting primaries rerouteResult = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); // starting replicas rerouteResult = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); logger.info("--> verifying all is allocated"); assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1)); @@ -210,7 +210,7 @@ public class DeadNodesAllocationTests extends ESAllocationTestCase { .add(newNode("node3")) ).build(); rerouteResult = allocation.reroute(clusterState, "reroute"); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node1").iterator().next().state(), equalTo(STARTED)); @@ -226,7 +226,7 @@ public class DeadNodesAllocationTests extends ESAllocationTestCase { new MoveAllocationCommand("test",0 , clusterState.routingTable().index("test").shard(0).primaryShard().currentNodeId(), "node3")), false, false); assertThat(rerouteResult.changed(), equalTo(true)); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().node(origPrimaryNodeId).iterator().next().state(), equalTo(RELOCATING)); assertThat(clusterState.getRoutingNodes().node("node3").iterator().next().state(), equalTo(INITIALIZING)); @@ -236,7 +236,7 @@ public class DeadNodesAllocationTests extends ESAllocationTestCase { .add(newNode(origReplicaNodeId)) ).build(); rerouteResult = allocation.deassociateDeadNodes(clusterState, true, "reroute"); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().node(origReplicaNodeId).iterator().next().state(), equalTo(STARTED)); assertThat(clusterState.getRoutingNodes().node("node3").iterator().next().state(), equalTo(INITIALIZING)); diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/ElectReplicaAsPrimaryDuringRelocationTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/ElectReplicaAsPrimaryDuringRelocationTests.java index 3d7a0147363..d840e5ef418 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/ElectReplicaAsPrimaryDuringRelocationTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/ElectReplicaAsPrimaryDuringRelocationTests.java @@ -51,61 +51,56 @@ public class ElectReplicaAsPrimaryDuringRelocationTests extends ESAllocationTest .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(2).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = 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(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("Adding two nodes and performing rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build(); - RoutingTable prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("Start the primary shards"); RoutingNodes routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("Start the replica shards"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(2)); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(2)); assertThat(routingNodes.node("node1").numberOfShardsWithState(STARTED), equalTo(2)); assertThat(routingNodes.node("node2").numberOfShardsWithState(STARTED), equalTo(2)); logger.info("Start another node and perform rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node3"))).build(); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("find the replica shard that gets relocated"); IndexShardRoutingTable indexShardRoutingTable = null; - if (routingTable.index("test").shard(0).replicaShards().get(0).relocating()) { - indexShardRoutingTable = routingTable.index("test").shard(0); - } else if (routingTable.index("test").shard(1).replicaShards().get(0).relocating()) { - indexShardRoutingTable = routingTable.index("test").shard(1); + if (clusterState.routingTable().index("test").shard(0).replicaShards().get(0).relocating()) { + indexShardRoutingTable = clusterState.routingTable().index("test").shard(0); + } else if (clusterState.routingTable().index("test").shard(1).replicaShards().get(0).relocating()) { + indexShardRoutingTable = clusterState.routingTable().index("test").shard(1); } // we might have primary relocating, and the test is only for replicas, so only test in the case of replica allocation if (indexShardRoutingTable != null) { logger.info("kill the node [{}] of the primary shard for the relocating replica", indexShardRoutingTable.primaryShard().currentNodeId()); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).remove(indexShardRoutingTable.primaryShard().currentNodeId())).build(); - prevRoutingTable = routingTable; - routingTable = strategy.deassociateDeadNodes(clusterState, true, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.deassociateDeadNodes(clusterState, true, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("make sure all the primary shards are active"); - assertThat(routingTable.index("test").shard(0).primaryShard().active(), equalTo(true)); - assertThat(routingTable.index("test").shard(1).primaryShard().active(), equalTo(true)); + assertThat(clusterState.routingTable().index("test").shard(0).primaryShard().active(), equalTo(true)); + assertThat(clusterState.routingTable().index("test").shard(1).primaryShard().active(), equalTo(true)); } } } diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/ExpectedShardSizeAllocationTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/ExpectedShardSizeAllocationTests.java index 6aaf08b759e..15f08a9723c 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/ExpectedShardSizeAllocationTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/ExpectedShardSizeAllocationTests.java @@ -81,23 +81,23 @@ public class ExpectedShardSizeAllocationTests extends ESAllocationTestCase { ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); logger.info("Adding one node and performing rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1"))).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertEquals(1, clusterState.getRoutingNodes().node("node1").numberOfShardsWithState(ShardRoutingState.INITIALIZING)); assertEquals(byteSize, clusterState.getRoutingTable().shardsWithState(ShardRoutingState.INITIALIZING).get(0).getExpectedShardSize()); logger.info("Start the primary shard"); RoutingNodes routingNodes = clusterState.getRoutingNodes(); - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertEquals(1, clusterState.getRoutingNodes().node("node1").numberOfShardsWithState(ShardRoutingState.STARTED)); assertEquals(1, clusterState.getRoutingNodes().unassigned().size()); logger.info("Add another one node and reroute"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node2"))).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertEquals(1, clusterState.getRoutingNodes().node("node2").numberOfShardsWithState(ShardRoutingState.INITIALIZING)); assertEquals(byteSize, clusterState.getRoutingTable().shardsWithState(ShardRoutingState.INITIALIZING).get(0).getExpectedShardSize()); @@ -135,11 +135,11 @@ public class ExpectedShardSizeAllocationTests extends ESAllocationTestCase { logger.info("adding two nodes and performing rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build(); RoutingAllocation.Result rerouteResult = allocation.reroute(clusterState, "reroute"); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); logger.info("start primary shard"); rerouteResult = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); logger.info("move the shard"); String existingNodeId = clusterState.routingTable().index("test").shard(0).primaryShard().currentNodeId(); @@ -151,7 +151,7 @@ public class ExpectedShardSizeAllocationTests extends ESAllocationTestCase { } rerouteResult = allocation.reroute(clusterState, new AllocationCommands(new MoveAllocationCommand("test", 0, existingNodeId, toNodeId)), false, false); assertThat(rerouteResult.changed(), equalTo(true)); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertEquals(clusterState.getRoutingNodes().node(existingNodeId).iterator().next().state(), ShardRoutingState.RELOCATING); assertEquals(clusterState.getRoutingNodes().node(toNodeId).iterator().next().state(),ShardRoutingState.INITIALIZING); @@ -160,7 +160,7 @@ public class ExpectedShardSizeAllocationTests extends ESAllocationTestCase { logger.info("finish moving the shard"); rerouteResult = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().node(existingNodeId).isEmpty(), equalTo(true)); assertThat(clusterState.getRoutingNodes().node(toNodeId).iterator().next().state(), equalTo(ShardRoutingState.STARTED)); diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/FailedNodeRoutingTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/FailedNodeRoutingTests.java index 62b2551896c..811c57e4200 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/FailedNodeRoutingTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/FailedNodeRoutingTests.java @@ -49,31 +49,28 @@ public class FailedNodeRoutingTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test2").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = RoutingTable.builder() .addAsNew(metaData.index("test1")) .addAsNew(metaData.index("test2")) .build(); - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("start 4 nodes"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2")).add(newNode("node3")).add(newNode("node4"))).build(); - RoutingTable prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("start all the primary shards, replicas will start initializing"); RoutingNodes routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); logger.info("start the replica shards"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); assertThat(routingNodes.node("node1").numberOfShardsWithState(STARTED), equalTo(1)); @@ -85,13 +82,12 @@ public class FailedNodeRoutingTests extends ESAllocationTestCase { logger.info("remove 2 nodes where primaries are allocated, reroute"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) - .remove(routingTable.index("test1").shard(0).primaryShard().currentNodeId()) - .remove(routingTable.index("test2").shard(0).primaryShard().currentNodeId()) + .remove(clusterState.routingTable().index("test1").shard(0).primaryShard().currentNodeId()) + .remove(clusterState.routingTable().index("test2").shard(0).primaryShard().currentNodeId()) ) .build(); - prevRoutingTable = routingTable; - routingTable = strategy.deassociateDeadNodes(clusterState, true, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.deassociateDeadNodes(clusterState, true, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); for (RoutingNode routingNode : routingNodes) { diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/FailedShardsRoutingTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/FailedShardsRoutingTests.java index b41142e17ab..8c7043a7439 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/FailedShardsRoutingTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/FailedShardsRoutingTests.java @@ -78,14 +78,14 @@ public class FailedShardsRoutingTests extends ESAllocationTestCase { ).build(); RoutingAllocation.Result rerouteResult = allocation.reroute(clusterState, "reroute"); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); // starting primaries rerouteResult = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); // starting replicas rerouteResult = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); logger.info("--> verifying all is allocated"); assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1)); @@ -98,7 +98,7 @@ public class FailedShardsRoutingTests extends ESAllocationTestCase { .add(newNode("node3")) ).build(); rerouteResult = allocation.reroute(clusterState, "reroute"); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node1").iterator().next().state(), equalTo(STARTED)); @@ -114,13 +114,13 @@ public class FailedShardsRoutingTests extends ESAllocationTestCase { new MoveAllocationCommand("test", 0, clusterState.routingTable().index("test").shard(0).primaryShard().currentNodeId(), "node3")), false, false); assertThat(rerouteResult.changed(), equalTo(true)); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().node(origPrimaryNodeId).iterator().next().state(), equalTo(RELOCATING)); assertThat(clusterState.getRoutingNodes().node("node3").iterator().next().state(), equalTo(INITIALIZING)); logger.info("--> fail primary shard recovering instance on node3 being initialized"); rerouteResult = allocation.applyFailedShard(clusterState, clusterState.getRoutingNodes().node("node3").iterator().next()); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); assertThat(clusterState.getRoutingNodes().node(origPrimaryNodeId).iterator().next().state(), equalTo(STARTED)); assertThat(clusterState.getRoutingNodes().node("node3").size(), equalTo(0)); @@ -136,7 +136,7 @@ public class FailedShardsRoutingTests extends ESAllocationTestCase { logger.info("--> fail primary shard recovering instance on node1 being relocated"); rerouteResult = allocation.applyFailedShard(clusterState, clusterState.getRoutingNodes().node(origPrimaryNodeId).iterator().next()); - clusterState = ClusterState.builder(clusterState).routingTable(rerouteResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(rerouteResult).build(); // check promotion of replica to primary assertThat(clusterState.getRoutingNodes().node(origReplicaNodeId).iterator().next().state(), equalTo(STARTED)); @@ -156,70 +156,66 @@ public class FailedShardsRoutingTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = 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(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("Adding two nodes and performing rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build(); - RoutingTable prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("Start the shards (primaries)"); RoutingNodes routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(1)); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(i).primaryShard().currentNodeId(), anyOf(equalTo("node1"), equalTo("node2"))); - assertThat(routingTable.index("test").shard(i).replicaShards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).currentNodeId(), anyOf(equalTo("node2"), equalTo("node1"))); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(1)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().currentNodeId(), anyOf(equalTo("node1"), equalTo("node2"))); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).currentNodeId(), anyOf(equalTo("node2"), equalTo("node1"))); } logger.info("Start the shards (backups)"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(1)); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(i).primaryShard().currentNodeId(), anyOf(equalTo("node1"), equalTo("node2"))); - assertThat(routingTable.index("test").shard(i).replicaShards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).currentNodeId(), anyOf(equalTo("node2"), equalTo("node1"))); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(1)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().currentNodeId(), anyOf(equalTo("node1"), equalTo("node2"))); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).currentNodeId(), anyOf(equalTo("node2"), equalTo("node1"))); } logger.info("fail the primary shard, will have no place to be rerouted to (single node), so stays unassigned"); - ShardRouting shardToFail = routingTable.index("test").shard(0).primaryShard(); - prevRoutingTable = routingTable; - routingTable = strategy.applyFailedShard(clusterState, shardToFail).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + ShardRouting shardToFail = clusterState.routingTable().index("test").shard(0).primaryShard(); + routingResult = strategy.applyFailedShard(clusterState, shardToFail); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shard(0).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(0).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(0).primaryShard().currentNodeId(), not(equalTo(shardToFail.currentNodeId()))); - assertThat(routingTable.index("test").shard(0).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(0).primaryShard().currentNodeId(), anyOf(equalTo("node1"), equalTo("node2"))); - assertThat(routingTable.index("test").shard(0).replicaShards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shard(0).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(0).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(0).primaryShard().currentNodeId(), not(equalTo(shardToFail.currentNodeId()))); + assertThat(clusterState.routingTable().index("test").shard(0).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(0).primaryShard().currentNodeId(), anyOf(equalTo("node1"), equalTo("node2"))); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } public void testFirstAllocationFailureSingleNode() { @@ -234,44 +230,42 @@ public class FailedShardsRoutingTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = 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(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("Adding single node and performing rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1"))).build(); - RoutingTable prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(1)); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test").shard(i).primaryShard().currentNodeId(), equalTo("node1")); - assertThat(routingTable.index("test").shard(i).replicaShards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(1)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().currentNodeId(), equalTo("node1")); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } logger.info("fail the first shard, will have no place to be rerouted to (single node), so stays unassigned"); - prevRoutingTable = routingTable; ShardRouting firstShard = clusterState.getRoutingNodes().node("node1").iterator().next(); - routingTable = strategy.applyFailedShard(clusterState, firstShard).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyFailedShard(clusterState, firstShard); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(1)); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(i).primaryShard().currentNodeId(), nullValue()); - assertThat(routingTable.index("test").shard(i).replicaShards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(1)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().currentNodeId(), nullValue()); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } } @@ -287,11 +281,11 @@ public class FailedShardsRoutingTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(numberOfReplicas)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = 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(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("Adding {} nodes and performing rerouting", numberOfReplicas + 1); DiscoveryNodes.Builder nodeBuilder = DiscoveryNodes.builder(); @@ -302,12 +296,12 @@ public class FailedShardsRoutingTests extends ESAllocationTestCase { while (!clusterState.routingTable().shardsWithState(UNASSIGNED).isEmpty()) { // start all initializing clusterState = ClusterState.builder(clusterState) - .routingTable(strategy - .applyStartedShards(clusterState, clusterState.routingTable().shardsWithState(INITIALIZING)).routingTable() + .routingResult(strategy + .applyStartedShards(clusterState, clusterState.routingTable().shardsWithState(INITIALIZING)) ) .build(); // and assign more unassigned - clusterState = ClusterState.builder(clusterState).routingTable(strategy.reroute(clusterState, "reroute").routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(strategy.reroute(clusterState, "reroute")).build(); } int shardsToFail = randomIntBetween(1, numberOfReplicas); @@ -326,9 +320,9 @@ public class FailedShardsRoutingTests extends ESAllocationTestCase { } } - routingTable = strategy.applyFailedShards(clusterState, failedShards).routingTable(); + RoutingAllocation.Result routingResult = strategy.applyFailedShards(clusterState, failedShards); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); for (FailedRerouteAllocation.FailedShard failedShard : failedShards) { if (routingNodes.getByAllocationId(failedShard.routingEntry.shardId(), failedShard.routingEntry.allocationId().getId()) != null) { @@ -355,48 +349,46 @@ public class FailedShardsRoutingTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = 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(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("Adding two nodes and performing rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build(); - RoutingTable prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - final String nodeHoldingPrimary = routingTable.index("test").shard(0).primaryShard().currentNodeId(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); + final String nodeHoldingPrimary = clusterState.routingTable().index("test").shard(0).primaryShard().currentNodeId(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(1)); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test").shard(i).primaryShard().currentNodeId(), equalTo(nodeHoldingPrimary)); - assertThat(routingTable.index("test").shard(i).replicaShards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(1)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().currentNodeId(), equalTo(nodeHoldingPrimary)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } logger.info("fail the first shard, will start INITIALIZING on the second node"); - prevRoutingTable = routingTable; final ShardRouting firstShard = clusterState.getRoutingNodes().node(nodeHoldingPrimary).iterator().next(); - routingTable = strategy.applyFailedShard(clusterState, firstShard).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); + routingResult = strategy.applyFailedShard(clusterState, firstShard); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); + assertTrue(routingResult.changed()); - final String nodeHoldingPrimary2 = routingTable.index("test").shard(0).primaryShard().currentNodeId(); + final String nodeHoldingPrimary2 = clusterState.routingTable().index("test").shard(0).primaryShard().currentNodeId(); assertThat(nodeHoldingPrimary2, not(equalTo(nodeHoldingPrimary))); - assertThat(routingTable.index("test").shards().size(), equalTo(1)); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test").shard(i).primaryShard().currentNodeId(), not(equalTo(nodeHoldingPrimary))); - assertThat(routingTable.index("test").shard(i).replicaShards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(1)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().currentNodeId(), not(equalTo(nodeHoldingPrimary))); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } } @@ -412,63 +404,59 @@ public class FailedShardsRoutingTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(2).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = 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(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("Adding two nodes and performing rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build(); - RoutingTable prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("Start the shards (primaries)"); RoutingNodes routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(2)); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(i).primaryShard().currentNodeId(), anyOf(equalTo("node1"), equalTo("node2"))); - assertThat(routingTable.index("test").shard(i).replicaShards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).currentNodeId(), anyOf(equalTo("node2"), equalTo("node1"))); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(2)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().currentNodeId(), anyOf(equalTo("node1"), equalTo("node2"))); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).currentNodeId(), anyOf(equalTo("node2"), equalTo("node1"))); } logger.info("Start the shards (backups)"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(2)); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(i).primaryShard().currentNodeId(), anyOf(equalTo("node1"), equalTo("node2"))); - assertThat(routingTable.index("test").shard(i).replicaShards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).currentNodeId(), anyOf(equalTo("node2"), equalTo("node1"))); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(2)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().currentNodeId(), anyOf(equalTo("node1"), equalTo("node2"))); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).currentNodeId(), anyOf(equalTo("node2"), equalTo("node1"))); } logger.info("Adding third node and reroute"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node3"))).build(); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(2)); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(2)); assertThat(routingNodes.node("node1").numberOfShardsWithState(STARTED, RELOCATING), equalTo(2)); assertThat(routingNodes.node("node1").numberOfShardsWithState(STARTED), lessThan(3)); assertThat(routingNodes.node("node2").numberOfShardsWithState(STARTED, RELOCATING), equalTo(2)); @@ -479,13 +467,12 @@ public class FailedShardsRoutingTests extends ESAllocationTestCase { logger.info("Fail the shards on node 3"); ShardRouting shardToFail = routingNodes.node("node3").iterator().next(); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyFailedShard(clusterState, shardToFail).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyFailedShard(clusterState, shardToFail); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(2)); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(2)); assertThat(routingNodes.node("node1").numberOfShardsWithState(STARTED, RELOCATING), equalTo(2)); assertThat(routingNodes.node("node1").numberOfShardsWithState(STARTED), lessThan(3)); assertThat(routingNodes.node("node2").numberOfShardsWithState(STARTED, RELOCATING), equalTo(2)); @@ -523,7 +510,7 @@ public class FailedShardsRoutingTests extends ESAllocationTestCase { ShardRouting primaryShardToFail = clusterState.routingTable().index("test").shard(0).primaryShard(); RoutingAllocation.Result routingResult = allocation.applyFailedShard(clusterState, primaryShardToFail); assertThat(routingResult.changed(), equalTo(true)); - clusterState = ClusterState.builder(clusterState).routingTable(routingResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); // the primary gets allocated on another node, replicas are unassigned assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().shardsWithState(UNASSIGNED).size(), equalTo(2)); @@ -532,7 +519,7 @@ public class FailedShardsRoutingTests extends ESAllocationTestCase { assertThat(newPrimaryShard, not(equalTo(primaryShardToFail))); // start the primary shard - clusterState = ClusterState.builder(clusterState).routingTable(allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING))).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(2)); } @@ -553,16 +540,16 @@ public class FailedShardsRoutingTests extends ESAllocationTestCase { // add 4 nodes clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2")).add(newNode("node3")).add(newNode("node4"))).build(); - clusterState = ClusterState.builder(clusterState).routingTable(allocation.reroute(clusterState, "reroute").routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(allocation.reroute(clusterState, "reroute")).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().shardsWithState(UNASSIGNED).size(), equalTo(2)); // start primary shards - clusterState = ClusterState.builder(clusterState).routingTable(allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING))).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(2)); // start another replica shard, while keep one initializing - clusterState = ClusterState.builder(clusterState).routingTable(allocation.applyStartedShards(clusterState, Collections.singletonList(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).get(0))).routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(allocation.applyStartedShards(clusterState, Collections.singletonList(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).get(0)))).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(2)); assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(1)); @@ -570,7 +557,7 @@ public class FailedShardsRoutingTests extends ESAllocationTestCase { ShardRouting primaryShardToFail = clusterState.routingTable().index("test").shard(0).primaryShard(); RoutingAllocation.Result routingResult = allocation.applyFailedShard(clusterState, primaryShardToFail); assertThat(routingResult.changed(), equalTo(true)); - clusterState = ClusterState.builder(clusterState).routingTable(routingResult.routingTable()).build(); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(1)); assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(2)); diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/FilterRoutingTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/FilterRoutingTests.java index 9e5a40659cf..16b918373a1 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/FilterRoutingTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/FilterRoutingTests.java @@ -58,11 +58,11 @@ public class FilterRoutingTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(2).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = 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(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("--> adding four nodes and performing rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder() @@ -71,17 +71,17 @@ public class FilterRoutingTests extends ESAllocationTestCase { .add(newNode("node3", singletonMap("tag1", "value3"))) .add(newNode("node4", singletonMap("tag1", "value4"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(2)); logger.info("--> start the shards (primaries)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("--> start the shards (replicas)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("--> make sure shards are only allocated on tag1 with value1 and value2"); List startedShards = clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED); @@ -97,7 +97,7 @@ public class FilterRoutingTests extends ESAllocationTestCase { logger.info("Building initial routing table"); - MetaData metaData = MetaData.builder() + MetaData initialMetaData = MetaData.builder() .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT) .put("index.number_of_shards", 2) .put("index.number_of_replicas", 1) @@ -106,11 +106,11 @@ public class FilterRoutingTests extends ESAllocationTestCase { .build())) .build(); - RoutingTable routingTable = RoutingTable.builder() - .addAsNew(metaData.index("test")) + RoutingTable initialRoutingTable = RoutingTable.builder() + .addAsNew(initialMetaData.index("test")) .build(); - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(initialMetaData).routingTable(initialRoutingTable).build(); logger.info("--> adding two nodes and performing rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder() @@ -119,17 +119,17 @@ public class FilterRoutingTests extends ESAllocationTestCase { .add(newNode("node3", singletonMap("tag1", "value3"))) .add(newNode("node4", singletonMap("tag1", "value4"))) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(2)); logger.info("--> start the shards (primaries)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("--> start the shards (replicas)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("--> make sure shards are only allocated on tag1 with value1 and value2"); List startedShards = clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED); @@ -140,24 +140,23 @@ public class FilterRoutingTests extends ESAllocationTestCase { logger.info("--> switch between value2 and value4, shards should be relocating"); - metaData = MetaData.builder() - .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT) - .put("index.number_of_shards", 2) - .put("index.number_of_replicas", 1) - .put("index.routing.allocation.include.tag1", "value1,value4") - .put("index.routing.allocation.exclude.tag1", "value2,value3") - .build())) - .build(); - clusterState = ClusterState.builder(clusterState).metaData(metaData).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + IndexMetaData existingMetaData = clusterState.metaData().index("test"); + MetaData updatedMetaData = MetaData.builder() + .put(IndexMetaData.builder(existingMetaData).settings(Settings.builder().put(existingMetaData.getSettings()) + .put("index.routing.allocation.include.tag1", "value1,value4") + .put("index.routing.allocation.exclude.tag1", "value2,value3") + .build())) + .build(); + clusterState = ClusterState.builder(clusterState).metaData(updatedMetaData).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size(), equalTo(2)); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.RELOCATING).size(), equalTo(2)); assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.INITIALIZING).size(), equalTo(2)); logger.info("--> finish relocation"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); startedShards = clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED); assertThat(startedShards.size(), equalTo(4)); @@ -175,25 +174,25 @@ public class FilterRoutingTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test2").settings(settings(Version.CURRENT)).numberOfShards(2).numberOfReplicas(0)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = RoutingTable.builder() .addAsNew(metaData.index("test1")) .addAsNew(metaData.index("test2")) .build(); - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("--> adding two nodes and performing rerouting"); DiscoveryNode node1 = newNode("node1", singletonMap("tag1", "value1")); DiscoveryNode node2 = newNode("node2", singletonMap("tag1", "value2")); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(node1).add(node2)).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().node(node1.getId()).numberOfShardsWithState(INITIALIZING), equalTo(2)); assertThat(clusterState.getRoutingNodes().node(node2.getId()).numberOfShardsWithState(INITIALIZING), equalTo(2)); logger.info("--> start the shards (only primaries)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("--> make sure all shards are started"); assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(4)); @@ -205,29 +204,29 @@ public class FilterRoutingTests extends ESAllocationTestCase { .build()); logger.info("--> move shards from node1 to node2"); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("--> check that concurrent recoveries only allows 1 shard to move"); assertThat(clusterState.getRoutingNodes().node(node1.getId()).numberOfShardsWithState(STARTED), equalTo(1)); assertThat(clusterState.getRoutingNodes().node(node2.getId()).numberOfShardsWithState(INITIALIZING), equalTo(1)); assertThat(clusterState.getRoutingNodes().node(node2.getId()).numberOfShardsWithState(STARTED), equalTo(2)); logger.info("--> start the shards (only primaries)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("--> move second shard from node1 to node2"); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().node(node2.getId()).numberOfShardsWithState(INITIALIZING), equalTo(1)); assertThat(clusterState.getRoutingNodes().node(node2.getId()).numberOfShardsWithState(STARTED), equalTo(3)); logger.info("--> start the shards (only primaries)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().node(node2.getId()).numberOfShardsWithState(STARTED), equalTo(4)); } } diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/IndexBalanceTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/IndexBalanceTests.java index f1e0fcabfd6..311a1825c1d 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/IndexBalanceTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/IndexBalanceTests.java @@ -56,109 +56,98 @@ public class IndexBalanceTests extends ESAllocationTestCase { MetaData metaData = MetaData.builder().put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(3).numberOfReplicas(1)) .put(IndexMetaData.builder("test1").settings(settings(Version.CURRENT)).numberOfShards(3).numberOfReplicas(1)).build(); - RoutingTable routingTable = RoutingTable.builder().addAsNew(metaData.index("test")).addAsNew(metaData.index("test1")).build(); + RoutingTable initialRoutingTable = RoutingTable.builder().addAsNew(metaData.index("test")).addAsNew(metaData.index("test1")).build(); - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); - assertThat(routingTable.index("test").shards().size(), equalTo(3)); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).shards().get(0).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(i).shards().get(1).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(i).shards().get(0).currentNodeId(), nullValue()); - assertThat(routingTable.index("test").shard(i).shards().get(1).currentNodeId(), nullValue()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(3)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(0).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(1).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(0).currentNodeId(), nullValue()); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(1).currentNodeId(), nullValue()); } - assertThat(routingTable.index("test1").shards().size(), equalTo(3)); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).shards().get(0).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test1").shard(i).shards().get(1).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test1").shard(i).shards().get(0).currentNodeId(), nullValue()); - assertThat(routingTable.index("test1").shard(i).shards().get(1).currentNodeId(), nullValue()); + assertThat(clusterState.routingTable().index("test1").shards().size(), equalTo(3)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).shards().get(0).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test1").shard(i).shards().get(1).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test1").shard(i).shards().get(0).currentNodeId(), nullValue()); + assertThat(clusterState.routingTable().index("test1").shard(i).shards().get(1).currentNodeId(), nullValue()); } logger.info("Adding three node and performing rerouting"); clusterState = ClusterState.builder(clusterState) .nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2")).add(newNode("node3"))).build(); - RoutingTable prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(3)); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test").shard(i).replicaShards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).currentNodeId(), nullValue()); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(3)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).currentNodeId(), nullValue()); } logger.info("Another round of rebalancing"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes())).build(); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable == routingTable, equalTo(true)); + assertFalse(routingResult.changed()); RoutingNodes routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(3)); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(i).replicaShards().size(), equalTo(1)); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(3)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().size(), equalTo(1)); // backup shards are initializing as well, we make sure that they // recover from primary *started* shards in the // IndicesClusterStateService - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); } logger.info("Reroute, nothing should change"); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - assertThat(prevRoutingTable == routingTable, equalTo(true)); + routingResult = strategy.reroute(clusterState, "reroute"); + assertFalse(routingResult.changed()); logger.info("Start the more shards"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(3)); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(i).replicaShards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(3)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); } - - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); - - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test1").shards().size(), equalTo(3)); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test1").shard(i).replicaShards().size(), equalTo(1)); - assertThat(routingTable.index("test1").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test1").shards().size(), equalTo(3)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); } assertThat(routingNodes.node("node1").numberOfShardsWithState(STARTED), equalTo(4)); @@ -186,142 +175,128 @@ public class IndexBalanceTests extends ESAllocationTestCase { MetaData metaData = MetaData.builder().put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(3).numberOfReplicas(1)) .put(IndexMetaData.builder("test1").settings(settings(Version.CURRENT)).numberOfShards(3).numberOfReplicas(1)).build(); - RoutingTable routingTable = RoutingTable.builder().addAsNew(metaData.index("test")).addAsNew(metaData.index("test1")).build(); + RoutingTable initialRoutingTable = RoutingTable.builder().addAsNew(metaData.index("test")).addAsNew(metaData.index("test1")).build(); - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); - assertThat(routingTable.index("test").shards().size(), equalTo(3)); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).shards().get(0).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(i).shards().get(1).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(i).shards().get(0).currentNodeId(), nullValue()); - assertThat(routingTable.index("test").shard(i).shards().get(1).currentNodeId(), nullValue()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(3)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(0).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(1).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(0).currentNodeId(), nullValue()); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(1).currentNodeId(), nullValue()); } - assertThat(routingTable.index("test1").shards().size(), equalTo(3)); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).shards().get(0).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test1").shard(i).shards().get(1).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test1").shard(i).shards().get(0).currentNodeId(), nullValue()); - assertThat(routingTable.index("test1").shard(i).shards().get(1).currentNodeId(), nullValue()); + assertThat(clusterState.routingTable().index("test1").shards().size(), equalTo(3)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).shards().get(0).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test1").shard(i).shards().get(1).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test1").shard(i).shards().get(0).currentNodeId(), nullValue()); + assertThat(clusterState.routingTable().index("test1").shard(i).shards().get(1).currentNodeId(), nullValue()); } logger.info("Adding one node and performing rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1"))).build(); - RoutingTable prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(3)); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test").shard(i).replicaShards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).currentNodeId(), nullValue()); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(3)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).currentNodeId(), nullValue()); } logger.info("Add another node and perform rerouting, nothing will happen since primary not started"); clusterState = ClusterState.builder(clusterState) .nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node2"))).build(); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable == routingTable, equalTo(true)); + assertFalse(routingResult.changed()); logger.info("Start the primary shard"); RoutingNodes routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(3)); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(i).replicaShards().size(), equalTo(1)); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(3)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().size(), equalTo(1)); // backup shards are initializing as well, we make sure that they // recover from primary *started* shards in the // IndicesClusterStateService - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); } logger.info("Reroute, nothing should change"); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - assertThat(prevRoutingTable == routingTable, equalTo(true)); + routingResult = strategy.reroute(clusterState, "reroute"); + assertFalse(routingResult.changed()); logger.info("Start the backup shard"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(3)); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(i).replicaShards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(3)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); } - - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); - - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test1").shards().size(), equalTo(3)); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test1").shard(i).replicaShards().size(), equalTo(1)); - assertThat(routingTable.index("test1").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test1").shards().size(), equalTo(3)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); } logger.info("Add another node and perform rerouting, nothing will happen since primary not started"); clusterState = ClusterState.builder(clusterState) .nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node3"))).build(); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); + assertTrue(routingResult.changed()); logger.info("Reroute, nothing should change"); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - assertThat(prevRoutingTable == routingTable, equalTo(true)); + routingResult = strategy.reroute(clusterState, "reroute"); + assertFalse(routingResult.changed()); logger.info("Start the backup shard"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(3)); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(3)); - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test1").shards().size(), equalTo(3)); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test1").shards().size(), equalTo(3)); assertThat(routingNodes.node("node1").numberOfShardsWithState(STARTED), equalTo(4)); assertThat(routingNodes.node("node2").numberOfShardsWithState(STARTED), equalTo(4)); @@ -347,91 +322,79 @@ public class IndexBalanceTests extends ESAllocationTestCase { MetaData metaData = MetaData.builder().put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(3).numberOfReplicas(1)).build(); - RoutingTable routingTable = RoutingTable.builder().addAsNew(metaData.index("test")).build(); + RoutingTable initialRoutingTable = 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(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); - assertThat(routingTable.index("test").shards().size(), equalTo(3)); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).shards().get(0).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(i).shards().get(1).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(i).shards().get(0).currentNodeId(), nullValue()); - assertThat(routingTable.index("test").shard(i).shards().get(1).currentNodeId(), nullValue()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(3)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(0).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(1).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(0).currentNodeId(), nullValue()); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(1).currentNodeId(), nullValue()); } logger.info("Adding three node and performing rerouting"); clusterState = ClusterState.builder(clusterState) .nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2")).add(newNode("node3"))).build(); - RoutingTable prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(3)); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test").shard(i).replicaShards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).currentNodeId(), nullValue()); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(3)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).currentNodeId(), nullValue()); } logger.info("Another round of rebalancing"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes())).build(); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable == routingTable, equalTo(true)); + assertFalse(routingResult.changed()); RoutingNodes routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(3)); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(i).replicaShards().size(), equalTo(1)); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(3)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().size(), equalTo(1)); // backup shards are initializing as well, we make sure that they // recover from primary *started* shards in the // IndicesClusterStateService - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); } logger.info("Reroute, nothing should change"); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - assertThat(prevRoutingTable == routingTable, equalTo(true)); + routingResult = strategy.reroute(clusterState, "reroute"); + assertFalse(routingResult.changed()); logger.info("Start the more shards"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(3)); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(i).replicaShards().size(), equalTo(1)); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(3)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().size(), equalTo(1)); } - - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); - - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingNodes.node("node1").numberOfShardsWithState(STARTED), equalTo(2)); assertThat(routingNodes.node("node2").numberOfShardsWithState(STARTED), equalTo(2)); assertThat(routingNodes.node("node3").numberOfShardsWithState(STARTED), equalTo(2)); @@ -442,88 +405,75 @@ public class IndexBalanceTests extends ESAllocationTestCase { logger.info("Add new index 3 shards 1 replica"); - prevRoutingTable = routingTable; - metaData = MetaData.builder(metaData) - .put(IndexMetaData.builder("test1").settings(settings(Version.CURRENT) - .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 3) - .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1) - )) - .build(); - routingTable = RoutingTable.builder(routingTable) - .addAsNew(metaData.index("test1")) - .build(); - clusterState = ClusterState.builder(clusterState).metaData(metaData).routingTable(routingTable).build(); + MetaData updatedMetaData = MetaData.builder(clusterState.metaData()) + .put(IndexMetaData.builder("test1").settings(settings(Version.CURRENT) + .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 3) + .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1) + )) + .build(); + RoutingTable updatedRoutingTable = RoutingTable.builder(clusterState.routingTable()) + .addAsNew(updatedMetaData.index("test1")) + .build(); + clusterState = ClusterState.builder(clusterState).metaData(updatedMetaData).routingTable(updatedRoutingTable).build(); - assertThat(routingTable.index("test1").shards().size(), equalTo(3)); + assertThat(clusterState.routingTable().index("test1").shards().size(), equalTo(3)); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test1").shards().size(), equalTo(3)); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test1").shard(i).replicaShards().size(), equalTo(1)); - assertThat(routingTable.index("test1").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test1").shard(i).replicaShards().get(0).currentNodeId(), nullValue()); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test1").shards().size(), equalTo(3)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().get(0).currentNodeId(), nullValue()); } logger.info("Another round of rebalancing"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes())).build(); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable == routingTable, equalTo(true)); + assertFalse(routingResult.changed()); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test1").shards().size(), equalTo(3)); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test1").shard(i).replicaShards().size(), equalTo(1)); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test1").shards().size(), equalTo(3)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().size(), equalTo(1)); // backup shards are initializing as well, we make sure that they // recover from primary *started* shards in the // IndicesClusterStateService - assertThat(routingTable.index("test1").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); } logger.info("Reroute, nothing should change"); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - assertThat(prevRoutingTable == routingTable, equalTo(true)); + routingResult = strategy.reroute(clusterState, "reroute"); + assertFalse(routingResult.changed()); logger.info("Start the more shards"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test1").shards().size(), equalTo(3)); - for (int i = 0; i < routingTable.index("test1").shards().size(); i++) { - assertThat(routingTable.index("test1").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test1").shard(i).replicaShards().size(), equalTo(1)); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test1").shards().size(), equalTo(3)); + for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test1").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test1").shard(i).replicaShards().size(), equalTo(1)); } - - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); - - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingNodes.node("node1").numberOfShardsWithState(STARTED), equalTo(4)); assertThat(routingNodes.node("node2").numberOfShardsWithState(STARTED), equalTo(4)); assertThat(routingNodes.node("node3").numberOfShardsWithState(STARTED), equalTo(4)); @@ -531,6 +481,5 @@ public class IndexBalanceTests extends ESAllocationTestCase { assertThat(routingNodes.node("node1").shardsWithState("test1", STARTED).size(), equalTo(2)); assertThat(routingNodes.node("node2").shardsWithState("test1", STARTED).size(), equalTo(2)); assertThat(routingNodes.node("node3").shardsWithState("test1", STARTED).size(), equalTo(2)); - } } diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/NodeVersionAllocationDeciderTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/NodeVersionAllocationDeciderTests.java index d1934123e53..43db8ad9be7 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/NodeVersionAllocationDeciderTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/NodeVersionAllocationDeciderTests.java @@ -27,6 +27,7 @@ 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.AllocationId; import org.elasticsearch.cluster.routing.IndexRoutingTable; import org.elasticsearch.cluster.routing.IndexShardRoutingTable; import org.elasticsearch.cluster.routing.RestoreSource; @@ -47,6 +48,7 @@ import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.LocalTransportAddress; +import org.elasticsearch.common.util.set.Sets; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.snapshots.Snapshot; import org.elasticsearch.snapshots.SnapshotId; @@ -89,104 +91,96 @@ public class NodeVersionAllocationDeciderTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(5).numberOfReplicas(2)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = 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(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); - assertThat(routingTable.index("test").shards().size(), equalTo(5)); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(3)); - assertThat(routingTable.index("test").shard(i).shards().get(0).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(i).shards().get(1).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(i).shards().get(2).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(i).shards().get(0).currentNodeId(), nullValue()); - assertThat(routingTable.index("test").shard(i).shards().get(1).currentNodeId(), nullValue()); - assertThat(routingTable.index("test").shard(i).shards().get(2).currentNodeId(), nullValue()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(5)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(3)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(0).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(1).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(2).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(0).currentNodeId(), nullValue()); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(1).currentNodeId(), nullValue()); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(2).currentNodeId(), nullValue()); } logger.info("start two nodes and fully start the shards"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build(); - RoutingTable prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(3)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test").shard(i).replicaShardsWithState(UNASSIGNED).size(), equalTo(2)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(3)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShardsWithState(UNASSIGNED).size(), equalTo(2)); } logger.info("start all the primary shards, replicas will start initializing"); RoutingNodes routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(3)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(i).replicaShardsWithState(INITIALIZING).size(), equalTo(1)); - assertThat(routingTable.index("test").shard(i).replicaShardsWithState(UNASSIGNED).size(), equalTo(1)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(3)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShardsWithState(INITIALIZING).size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShardsWithState(UNASSIGNED).size(), equalTo(1)); } routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(3)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(i).replicaShardsWithState(STARTED).size(), equalTo(1)); - assertThat(routingTable.index("test").shard(i).replicaShardsWithState(UNASSIGNED).size(), equalTo(1)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(3)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShardsWithState(STARTED).size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShardsWithState(UNASSIGNED).size(), equalTo(1)); } clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node3", VersionUtils.getPreviousVersion()))) .build(); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(3)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(i).replicaShardsWithState(STARTED).size(), equalTo(1)); - assertThat(routingTable.index("test").shard(i).replicaShardsWithState(UNASSIGNED).size(), equalTo(1)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(3)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShardsWithState(STARTED).size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShardsWithState(UNASSIGNED).size(), equalTo(1)); } clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node4"))) .build(); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(3)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(i).replicaShardsWithState(STARTED).size(), equalTo(1)); - assertThat(routingTable.index("test").shard(i).replicaShardsWithState(INITIALIZING).size(), equalTo(1)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(3)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShardsWithState(STARTED).size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShardsWithState(INITIALIZING).size(), equalTo(1)); } routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(3)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(i).replicaShardsWithState(STARTED).size(), equalTo(2)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(3)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShardsWithState(STARTED).size(), equalTo(2)); } } @@ -258,15 +252,15 @@ public class NodeVersionAllocationDeciderTests extends ESAllocationTestCase { ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); - assertThat(routingTable.index("test").shards().size(), equalTo(5)); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(3)); - assertThat(routingTable.index("test").shard(i).shards().get(0).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(i).shards().get(1).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(i).shards().get(2).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(i).shards().get(0).currentNodeId(), nullValue()); - assertThat(routingTable.index("test").shard(i).shards().get(1).currentNodeId(), nullValue()); - assertThat(routingTable.index("test").shard(i).shards().get(2).currentNodeId(), nullValue()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(5)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(3)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(0).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(1).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(2).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(0).currentNodeId(), nullValue()); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(1).currentNodeId(), nullValue()); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(2).currentNodeId(), nullValue()); } clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder() .add(newNode("old0", VersionUtils.getPreviousVersion())) @@ -294,15 +288,14 @@ public class NodeVersionAllocationDeciderTests extends ESAllocationTestCase { .add(newNode("new0"))).build(); clusterState = stabilize(clusterState, service); - routingTable = clusterState.routingTable(); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(3)); - assertThat(routingTable.index("test").shard(i).shards().get(0).state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(i).shards().get(1).state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(i).shards().get(2).state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(i).shards().get(0).currentNodeId(), notNullValue()); - assertThat(routingTable.index("test").shard(i).shards().get(1).currentNodeId(), notNullValue()); - assertThat(routingTable.index("test").shard(i).shards().get(2).currentNodeId(), notNullValue()); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(3)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(0).state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(1).state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(2).state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(0).currentNodeId(), notNullValue()); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(1).currentNodeId(), notNullValue()); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(2).currentNodeId(), notNullValue()); } } @@ -315,21 +308,25 @@ public class NodeVersionAllocationDeciderTests extends ESAllocationTestCase { MASTER_DATA_ROLES, VersionUtils.getPreviousVersion()); final DiscoveryNode oldNode2 = new DiscoveryNode("oldNode2", LocalTransportAddress.buildUnique(), emptyMap(), MASTER_DATA_ROLES, VersionUtils.getPreviousVersion()); + AllocationId allocationId1P = AllocationId.newInitializing(); + AllocationId allocationId1R = AllocationId.newInitializing(); + AllocationId allocationId2P = AllocationId.newInitializing(); + AllocationId allocationId2R = AllocationId.newInitializing(); MetaData metaData = MetaData.builder() - .put(IndexMetaData.builder(shard1.getIndexName()).settings(settings(Version.CURRENT).put(Settings.EMPTY)).numberOfShards(1).numberOfReplicas(1)) - .put(IndexMetaData.builder(shard2.getIndexName()).settings(settings(Version.CURRENT).put(Settings.EMPTY)).numberOfShards(1).numberOfReplicas(1)) + .put(IndexMetaData.builder(shard1.getIndexName()).settings(settings(Version.CURRENT).put(Settings.EMPTY)).numberOfShards(1).numberOfReplicas(1).putActiveAllocationIds(0, Sets.newHashSet(allocationId1P.getId(), allocationId1R.getId()))) + .put(IndexMetaData.builder(shard2.getIndexName()).settings(settings(Version.CURRENT).put(Settings.EMPTY)).numberOfShards(1).numberOfReplicas(1).putActiveAllocationIds(0, Sets.newHashSet(allocationId2P.getId(), allocationId2R.getId()))) .build(); RoutingTable routingTable = RoutingTable.builder() .add(IndexRoutingTable.builder(shard1.getIndex()) .addIndexShard(new IndexShardRoutingTable.Builder(shard1) - .addShard(TestShardRouting.newShardRouting(shard1.getIndexName(), shard1.getId(), newNode.getId(), true, ShardRoutingState.STARTED)) - .addShard(TestShardRouting.newShardRouting(shard1.getIndexName(), shard1.getId(), oldNode1.getId(), false, ShardRoutingState.STARTED)) + .addShard(TestShardRouting.newShardRouting(shard1.getIndexName(), shard1.getId(), newNode.getId(), null, true, ShardRoutingState.STARTED, allocationId1P)) + .addShard(TestShardRouting.newShardRouting(shard1.getIndexName(), shard1.getId(), oldNode1.getId(), null, false, ShardRoutingState.STARTED, allocationId1R)) .build()) ) .add(IndexRoutingTable.builder(shard2.getIndex()) .addIndexShard(new IndexShardRoutingTable.Builder(shard2) - .addShard(TestShardRouting.newShardRouting(shard2.getIndexName(), shard2.getId(), newNode.getId(), true, ShardRoutingState.STARTED)) - .addShard(TestShardRouting.newShardRouting(shard2.getIndexName(), shard2.getId(), oldNode1.getId(), false, ShardRoutingState.STARTED)) + .addShard(TestShardRouting.newShardRouting(shard2.getIndexName(), shard2.getId(), newNode.getId(), null, true, ShardRoutingState.STARTED, allocationId2P)) + .addShard(TestShardRouting.newShardRouting(shard2.getIndexName(), shard2.getId(), oldNode1.getId(), null, false, ShardRoutingState.STARTED, allocationId2R)) .build()) ) .build(); @@ -385,27 +382,24 @@ public class NodeVersionAllocationDeciderTests extends ESAllocationTestCase { private ClusterState stabilize(ClusterState clusterState, AllocationService service) { logger.trace("RoutingNodes: {}", clusterState.getRoutingNodes().prettyPrint()); - RoutingTable routingTable = service.deassociateDeadNodes(clusterState, true, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = service.deassociateDeadNodes(clusterState, true, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); RoutingNodes routingNodes = clusterState.getRoutingNodes(); assertRecoveryNodeVersions(routingNodes); logger.info("complete rebalancing"); - RoutingTable prev = routingTable; - boolean stable = false; for (int i = 0; i < 1000; i++) { // at most 200 iters - this should be enough for all tests logger.trace("RoutingNodes: {}", clusterState.getRoutingNodes().prettyPrint()); - routingTable = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - if (stable = (routingTable == prev)) { + if (routingResult.changed() == false) { break; } assertRecoveryNodeVersions(routingNodes); - prev = routingTable; } - logger.info("stabilized success [{}]", stable); - assertThat(stable, is(true)); + logger.info("stabilized success [{}]", routingResult.changed() == false); + assertFalse(routingResult.changed()); return clusterState; } diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/PreferLocalPrimariesToRelocatingPrimariesTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/PreferLocalPrimariesToRelocatingPrimariesTests.java index 2118bb6de85..838cd5777ef 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/PreferLocalPrimariesToRelocatingPrimariesTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/PreferLocalPrimariesToRelocatingPrimariesTests.java @@ -57,43 +57,43 @@ public class PreferLocalPrimariesToRelocatingPrimariesTests extends ESAllocation .put(IndexMetaData.builder("test2").settings(settings(Version.CURRENT)).numberOfShards(numberOfShards).numberOfReplicas(0)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = RoutingTable.builder() .addAsNew(metaData.index("test1")) .addAsNew(metaData.index("test2")) .build(); - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("adding two nodes and performing rerouting till all are allocated"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder() .add(newNode("node1", singletonMap("tag1", "value1"))) .add(newNode("node2", singletonMap("tag1", "value2")))).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); while (!clusterState.getRoutingNodes().shardsWithState(INITIALIZING).isEmpty()) { - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); } logger.info("remove one of the nodes and apply filter to move everything from another node"); metaData = MetaData.builder() - .put(IndexMetaData.builder("test1").settings(settings(Version.CURRENT) + .put(IndexMetaData.builder(clusterState.metaData().index("test1")).settings(settings(Version.CURRENT) .put("index.number_of_shards", numberOfShards) .put("index.number_of_replicas", 0) .put("index.routing.allocation.exclude.tag1", "value2") .build())) - .put(IndexMetaData.builder("test2").settings(settings(Version.CURRENT) + .put(IndexMetaData.builder(clusterState.metaData().index("test2")).settings(settings(Version.CURRENT) .put("index.number_of_shards", numberOfShards) .put("index.number_of_replicas", 0) .put("index.routing.allocation.exclude.tag1", "value2") .build())) .build(); clusterState = ClusterState.builder(clusterState).metaData(metaData).nodes(DiscoveryNodes.builder(clusterState.nodes()).remove("node1")).build(); - routingTable = strategy.deassociateDeadNodes(clusterState, true, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.deassociateDeadNodes(clusterState, true, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("[{}] primaries should be still started but [{}] other primaries should be unassigned", numberOfShards, numberOfShards); assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(numberOfShards)); @@ -103,8 +103,8 @@ public class PreferLocalPrimariesToRelocatingPrimariesTests extends ESAllocation logger.info("start node back up"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node1", singletonMap("tag1", "value1")))).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); while (clusterState.getRoutingNodes().shardsWithState(STARTED).size() < totalNumberOfShards) { int localInitializations = 0; diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/PreferPrimaryAllocationTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/PreferPrimaryAllocationTests.java index 70b7740c308..17e40119525 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/PreferPrimaryAllocationTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/PreferPrimaryAllocationTests.java @@ -54,30 +54,30 @@ public class PreferPrimaryAllocationTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test2").settings(settings(Version.CURRENT)).numberOfShards(10).numberOfReplicas(0)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = RoutingTable.builder() .addAsNew(metaData.index("test1")) .addAsNew(metaData.index("test2")) .build(); - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("adding two nodes and performing rerouting till all are allocated"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); while (!clusterState.getRoutingNodes().shardsWithState(INITIALIZING).isEmpty()) { - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); } logger.info("increasing the number of replicas to 1, and perform a reroute (to get the replicas allocation going)"); - routingTable = RoutingTable.builder(routingTable).updateNumberOfReplicas(1).build(); + RoutingTable updatedRoutingTable = RoutingTable.builder(clusterState.routingTable()).updateNumberOfReplicas(1).build(); metaData = MetaData.builder(clusterState.metaData()).updateNumberOfReplicas(1).build(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).metaData(metaData).build(); + clusterState = ClusterState.builder(clusterState).routingTable(updatedRoutingTable).metaData(metaData).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("2 replicas should be initializing now for the existing indices (we throttle to 1)"); assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(2)); @@ -87,15 +87,15 @@ public class PreferPrimaryAllocationTests extends ESAllocationTestCase { .put(IndexMetaData.builder("new_index").settings(settings(Version.CURRENT)).numberOfShards(4).numberOfReplicas(0)) .build(); - routingTable = RoutingTable.builder(clusterState.routingTable()) + updatedRoutingTable = RoutingTable.builder(clusterState.routingTable()) .addAsNew(metaData.index("new_index")) .build(); - clusterState = ClusterState.builder(clusterState).metaData(metaData).routingTable(routingTable).build(); + clusterState = ClusterState.builder(clusterState).metaData(metaData).routingTable(updatedRoutingTable).build(); logger.info("reroute, verify that primaries for the new index primary shards are allocated"); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.routingTable().index("new_index").shardsWithState(INITIALIZING).size(), equalTo(2)); } diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/PrimaryNotRelocatedWhileBeingRecoveredTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/PrimaryNotRelocatedWhileBeingRecoveredTests.java index b984fcb8715..a2dab942e3e 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/PrimaryNotRelocatedWhileBeingRecoveredTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/PrimaryNotRelocatedWhileBeingRecoveredTests.java @@ -54,38 +54,38 @@ public class PrimaryNotRelocatedWhileBeingRecoveredTests extends ESAllocationTes .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(5).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = 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(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("Adding two nodes and performing rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1"))).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("Start the primary shard (on node1)"); RoutingNodes routingNodes = clusterState.getRoutingNodes(); - routingTable = strategy.applyStartedShards(clusterState, routingNodes.node("node1").shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.node("node1").shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(5)); + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(5)); logger.info("start another node, replica will start recovering form primary"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node2"))).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(5)); - assertThat(routingTable.shardsWithState(INITIALIZING).size(), equalTo(5)); + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(5)); + assertThat(clusterState.routingTable().shardsWithState(INITIALIZING).size(), equalTo(5)); logger.info("start another node, make sure the primary is not relocated"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node3"))).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(5)); - assertThat(routingTable.shardsWithState(INITIALIZING).size(), equalTo(5)); + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(5)); + assertThat(clusterState.routingTable().shardsWithState(INITIALIZING).size(), equalTo(5)); } } diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/RandomAllocationDeciderTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/RandomAllocationDeciderTests.java index 68a7a073c83..ea618bb8b6d 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/RandomAllocationDeciderTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/RandomAllocationDeciderTests.java @@ -77,12 +77,13 @@ public class RandomAllocationDeciderTests extends ESAllocationTestCase { routingTableBuilder.addAsNew(metaData.index("INDEX_" + i)); } - RoutingTable routingTable = routingTableBuilder.build(); - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); + RoutingTable initialRoutingTable = routingTableBuilder.build(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); int numIters = scaledRandomIntBetween(5, 15); int nodeIdCounter = 0; int atMostNodes = scaledRandomIntBetween(Math.max(1, maxNumReplicas), 15); final boolean frequentNodes = randomBoolean(); + RoutingAllocation.Result routingResult; for (int i = 0; i < numIters; i++) { logger.info("Start iteration [{}]", i); ClusterState.Builder stateBuilder = ClusterState.builder(clusterState); @@ -108,15 +109,14 @@ public class RandomAllocationDeciderTests extends ESAllocationTestCase { stateBuilder.nodes(newNodesBuilder.build()); clusterState = stateBuilder.build(); if (nodesRemoved) { - routingTable = strategy.deassociateDeadNodes(clusterState, true, "reroute").routingTable(); + routingResult = strategy.deassociateDeadNodes(clusterState, true, "reroute"); } else { - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); + routingResult = strategy.reroute(clusterState, "reroute"); } - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); if (clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size() > 0) { - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)) - .routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); } } logger.info("Fill up nodes such that every shard can be allocated"); @@ -137,12 +137,11 @@ public class RandomAllocationDeciderTests extends ESAllocationTestCase { int iterations = 0; do { iterations++; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); if (clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size() > 0) { - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)) - .routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); } } while (clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.INITIALIZING).size() != 0 || diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/RebalanceAfterActiveTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/RebalanceAfterActiveTests.java index 4fe121fa586..f1c4d99d87f 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/RebalanceAfterActiveTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/RebalanceAfterActiveTests.java @@ -83,77 +83,73 @@ public class RebalanceAfterActiveTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(5).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = 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(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); - assertThat(routingTable.index("test").shards().size(), equalTo(5)); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).shards().get(0).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(i).shards().get(1).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(i).shards().get(0).currentNodeId(), nullValue()); - assertThat(routingTable.index("test").shard(i).shards().get(1).currentNodeId(), nullValue()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(5)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(0).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(1).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(0).currentNodeId(), nullValue()); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(1).currentNodeId(), nullValue()); } logger.info("start two nodes and fully start the shards"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build(); - RoutingTable prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); } logger.info("start all the primary shards, replicas will start initializing"); RoutingNodes routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); - assertEquals(routingTable.index("test").shard(i).replicaShards().get(0).getExpectedShardSize(), sizes[i]); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); + assertEquals(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).getExpectedShardSize(), sizes[i]); } logger.info("now, start 8 more nodes, and check that no rebalancing/relocation have happened"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node3")).add(newNode("node4")).add(newNode("node5")).add(newNode("node6")).add(newNode("node7")).add(newNode("node8")).add(newNode("node9")).add(newNode("node10"))) .build(); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); - assertEquals(routingTable.index("test").shard(i).replicaShards().get(0).getExpectedShardSize(), sizes[i]); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); + assertEquals(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).getExpectedShardSize(), sizes[i]); } logger.info("start the replica shards, rebalancing should start"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); // we only allow one relocation at a time - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(5)); - assertThat(routingTable.shardsWithState(RELOCATING).size(), equalTo(5)); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(5)); + assertThat(clusterState.routingTable().shardsWithState(RELOCATING).size(), equalTo(5)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { int num = 0; - for (ShardRouting routing : routingTable.index("test").shard(i).shards()) { + for (ShardRouting routing : clusterState.routingTable().index("test").shard(i).shards()) { if (routing.state() == RELOCATING || routing.state() == INITIALIZING) { assertEquals(routing.getExpectedShardSize(), sizes[i]); num++; @@ -164,16 +160,15 @@ public class RebalanceAfterActiveTests extends ESAllocationTestCase { logger.info("complete relocation, other half of relocation should happen"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); // we now only relocate 3, since 2 remain where they are! - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(7)); - assertThat(routingTable.shardsWithState(RELOCATING).size(), equalTo(3)); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - for (ShardRouting routing : routingTable.index("test").shard(i).shards()) { + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(7)); + assertThat(clusterState.routingTable().shardsWithState(RELOCATING).size(), equalTo(3)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + for (ShardRouting routing : clusterState.routingTable().index("test").shard(i).shards()) { if (routing.state() == RELOCATING || routing.state() == INITIALIZING) { assertEquals(routing.getExpectedShardSize(), sizes[i]); } @@ -183,12 +178,11 @@ public class RebalanceAfterActiveTests extends ESAllocationTestCase { logger.info("complete relocation, that's it!"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(10)); + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(10)); // make sure we have an even relocation for (RoutingNode routingNode : routingNodes) { assertThat(routingNode.size(), equalTo(1)); diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/RoutingNodesIntegrityTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/RoutingNodesIntegrityTests.java index 3335de93d9f..dbcd035ad79 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/RoutingNodesIntegrityTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/RoutingNodesIntegrityTests.java @@ -56,15 +56,14 @@ public class RoutingNodesIntegrityTests extends ESAllocationTestCase { MetaData metaData = MetaData.builder().put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(3).numberOfReplicas(1)) .put(IndexMetaData.builder("test1").settings(settings(Version.CURRENT)).numberOfShards(3).numberOfReplicas(1)).build(); - RoutingTable routingTable = RoutingTable.builder().addAsNew(metaData.index("test")).addAsNew(metaData.index("test1")).build(); + RoutingTable initialRoutingTable = RoutingTable.builder().addAsNew(metaData.index("test")).addAsNew(metaData.index("test1")).build(); - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); - RoutingNodes routingNodes = clusterState.getRoutingNodes(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("Adding three node and performing rerouting"); clusterState = ClusterState.builder(clusterState) .nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2")).add(newNode("node3"))).build(); - routingNodes = clusterState.getRoutingNodes(); + RoutingNodes routingNodes = clusterState.getRoutingNodes(); assertThat(assertShardStats(routingNodes), equalTo(true)); // all shards are unassigned. so no inactive shards or primaries. @@ -72,9 +71,8 @@ public class RoutingNodesIntegrityTests extends ESAllocationTestCase { assertThat(routingNodes.hasInactivePrimaries(), equalTo(false)); assertThat(routingNodes.hasUnassignedPrimaries(), equalTo(true)); - RoutingTable prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); assertThat(assertShardStats(routingNodes), equalTo(true)); @@ -84,25 +82,21 @@ public class RoutingNodesIntegrityTests extends ESAllocationTestCase { logger.info("Another round of rebalancing"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes())).build(); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("Reroute, nothing should change"); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); + routingResult = strategy.reroute(clusterState, "reroute"); + assertFalse(routingResult.changed()); logger.info("Start the more shards"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); assertThat(assertShardStats(routingNodes), equalTo(true)); @@ -110,9 +104,8 @@ public class RoutingNodesIntegrityTests extends ESAllocationTestCase { assertThat(routingNodes.hasInactivePrimaries(), equalTo(false)); assertThat(routingNodes.hasUnassignedPrimaries(), equalTo(false)); - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - routingNodes = clusterState.getRoutingNodes(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); } @@ -128,72 +121,65 @@ public class RoutingNodesIntegrityTests extends ESAllocationTestCase { MetaData metaData = MetaData.builder().put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(3).numberOfReplicas(1)) .put(IndexMetaData.builder("test1").settings(settings(Version.CURRENT)).numberOfShards(3).numberOfReplicas(1)).build(); - RoutingTable routingTable = RoutingTable.builder().addAsNew(metaData.index("test")).addAsNew(metaData.index("test1")).build(); + RoutingTable initialRoutingTable = RoutingTable.builder().addAsNew(metaData.index("test")).addAsNew(metaData.index("test1")).build(); - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("Adding one node and performing rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1"))).build(); - RoutingTable prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("Add another node and perform rerouting, nothing will happen since primary not started"); clusterState = ClusterState.builder(clusterState) .nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node2"))).build(); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("Start the primary shard"); RoutingNodes routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("Reroute, nothing should change"); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); + routingResult = strategy.reroute(clusterState, "reroute"); logger.info("Start the backup shard"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); logger.info("Add another node and perform rerouting, nothing will happen since primary not started"); clusterState = ClusterState.builder(clusterState) .nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node3"))).build(); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("Reroute, nothing should change"); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); + routingResult = strategy.reroute(clusterState, "reroute"); + assertFalse(routingResult.changed()); logger.info("Start the backup shard"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(3)); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(3)); - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test1").shards().size(), equalTo(3)); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test1").shards().size(), equalTo(3)); assertThat(routingNodes.node("node1").numberOfShardsWithState(STARTED), equalTo(4)); assertThat(routingNodes.node("node2").numberOfShardsWithState(STARTED), equalTo(4)); @@ -220,9 +206,9 @@ public class RoutingNodesIntegrityTests extends ESAllocationTestCase { MetaData metaData = MetaData.builder().put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(3).numberOfReplicas(1)).build(); - RoutingTable routingTable = RoutingTable.builder().addAsNew(metaData.index("test")).build(); + RoutingTable initialRoutingTable = 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(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("Adding three node and performing rerouting"); clusterState = ClusterState.builder(clusterState) @@ -234,9 +220,8 @@ public class RoutingNodesIntegrityTests extends ESAllocationTestCase { assertThat(routingNodes.hasInactivePrimaries(), equalTo(false)); assertThat(routingNodes.hasUnassignedPrimaries(), equalTo(true)); - RoutingTable prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); assertThat(assertShardStats(routingNodes), equalTo(true)); @@ -246,20 +231,18 @@ public class RoutingNodesIntegrityTests extends ESAllocationTestCase { logger.info("Another round of rebalancing"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes())).build(); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable == routingTable, equalTo(true)); + assertFalse(routingResult.changed()); routingNodes = clusterState.getRoutingNodes(); assertThat(routingNodes.node("node1").numberOfShardsWithState(INITIALIZING), equalTo(1)); assertThat(routingNodes.node("node2").numberOfShardsWithState(INITIALIZING), equalTo(1)); assertThat(routingNodes.node("node3").numberOfShardsWithState(INITIALIZING), equalTo(1)); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); assertThat(assertShardStats(routingNodes), equalTo(true)); @@ -271,15 +254,13 @@ public class RoutingNodesIntegrityTests extends ESAllocationTestCase { assertThat(routingNodes.node("node3").numberOfShardsWithState(STARTED), equalTo(1)); logger.info("Reroute, nothing should change"); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - assertThat(prevRoutingTable == routingTable, equalTo(true)); + routingResult = strategy.reroute(clusterState, "reroute"); + assertFalse(routingResult.changed()); logger.info("Start the more shards"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); assertThat(assertShardStats(routingNodes), equalTo(true)); @@ -297,17 +278,16 @@ public class RoutingNodesIntegrityTests extends ESAllocationTestCase { logger.info("Add new index 3 shards 1 replica"); - prevRoutingTable = routingTable; - metaData = MetaData.builder(metaData) + metaData = MetaData.builder(clusterState.metaData()) .put(IndexMetaData.builder("test1").settings(settings(Version.CURRENT) .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 3) .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1) )) .build(); - routingTable = RoutingTable.builder(routingTable) - .addAsNew(metaData.index("test1")) - .build(); - clusterState = ClusterState.builder(clusterState).metaData(metaData).routingTable(routingTable).build(); + RoutingTable updatedRoutingTable = RoutingTable.builder(clusterState.routingTable()) + .addAsNew(metaData.index("test1")) + .build(); + clusterState = ClusterState.builder(clusterState).metaData(metaData).routingTable(updatedRoutingTable).build(); routingNodes = clusterState.getRoutingNodes(); assertThat(assertShardStats(routingNodes), equalTo(true)); @@ -315,17 +295,15 @@ public class RoutingNodesIntegrityTests extends ESAllocationTestCase { assertThat(routingNodes.hasInactivePrimaries(), equalTo(false)); assertThat(routingNodes.hasUnassignedPrimaries(), equalTo(true)); - assertThat(routingTable.index("test1").shards().size(), equalTo(3)); + assertThat(clusterState.routingTable().index("test1").shards().size(), equalTo(3)); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("Reroute, assign"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes())).build(); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); assertThat(assertShardStats(routingNodes), equalTo(true)); @@ -333,13 +311,12 @@ public class RoutingNodesIntegrityTests extends ESAllocationTestCase { assertThat(routingNodes.hasInactivePrimaries(), equalTo(true)); assertThat(routingNodes.hasUnassignedPrimaries(), equalTo(false)); - assertThat(prevRoutingTable == routingTable, equalTo(true)); + assertFalse(routingResult.changed()); logger.info("Reroute, start the primaries"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); assertThat(assertShardStats(routingNodes), equalTo(true)); @@ -349,9 +326,8 @@ public class RoutingNodesIntegrityTests extends ESAllocationTestCase { logger.info("Reroute, start the replicas"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); assertThat(assertShardStats(routingNodes), equalTo(true)); @@ -369,10 +345,10 @@ public class RoutingNodesIntegrityTests extends ESAllocationTestCase { assertThat(routingNodes.node("node3").shardsWithState("test1", STARTED).size(), equalTo(2)); logger.info("kill one node"); - IndexShardRoutingTable indexShardRoutingTable = routingTable.index("test").shard(0); + IndexShardRoutingTable indexShardRoutingTable = clusterState.routingTable().index("test").shard(0); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).remove(indexShardRoutingTable.primaryShard().currentNodeId())).build(); - routingTable = strategy.deassociateDeadNodes(clusterState, true, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.deassociateDeadNodes(clusterState, true, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); assertThat(assertShardStats(routingNodes), equalTo(true)); @@ -383,9 +359,8 @@ public class RoutingNodesIntegrityTests extends ESAllocationTestCase { logger.info("Start Recovering shards round 1"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); assertThat(assertShardStats(routingNodes), equalTo(true)); @@ -395,9 +370,8 @@ public class RoutingNodesIntegrityTests extends ESAllocationTestCase { logger.info("Start Recovering shards round 2"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); assertThat(assertShardStats(routingNodes), equalTo(true)); diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/SameShardRoutingTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/SameShardRoutingTests.java index eaf2f88f734..4def9e6ed71 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/SameShardRoutingTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/SameShardRoutingTests.java @@ -67,15 +67,14 @@ public class SameShardRoutingTests extends ESAllocationTestCase { MASTER_DATA_ROLES, Version.CURRENT)) .add(new DiscoveryNode("node2", "node2", "node2", "test1", "test1", LocalTransportAddress.buildUnique(), emptyMap(), MASTER_DATA_ROLES, Version.CURRENT))).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(numberOfShardsOfType(clusterState.getRoutingNodes(), ShardRoutingState.INITIALIZING), equalTo(2)); logger.info("--> start all primary shards, no replica will be started since its on the same host"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)) - .routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(numberOfShardsOfType(clusterState.getRoutingNodes(), ShardRoutingState.STARTED), equalTo(2)); assertThat(numberOfShardsOfType(clusterState.getRoutingNodes(), ShardRoutingState.INITIALIZING), equalTo(0)); @@ -84,8 +83,8 @@ public class SameShardRoutingTests extends ESAllocationTestCase { clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(new DiscoveryNode("node3", "node3", "node3", "test2", "test2", LocalTransportAddress.buildUnique(), emptyMap(), MASTER_DATA_ROLES, Version.CURRENT))).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(numberOfShardsOfType(clusterState.getRoutingNodes(), ShardRoutingState.STARTED), equalTo(2)); assertThat(numberOfShardsOfType(clusterState.getRoutingNodes(), ShardRoutingState.INITIALIZING), equalTo(2)); diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/ShardsLimitAllocationTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/ShardsLimitAllocationTests.java index af4a71eb0f9..b01fb9bcc6e 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/ShardsLimitAllocationTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/ShardsLimitAllocationTests.java @@ -64,16 +64,16 @@ public class ShardsLimitAllocationTests extends ESAllocationTestCase { ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); logger.info("Adding two nodes and performing rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().node("node1").numberOfShardsWithState(ShardRoutingState.INITIALIZING), equalTo(2)); assertThat(clusterState.getRoutingNodes().node("node2").numberOfShardsWithState(ShardRoutingState.INITIALIZING), equalTo(2)); logger.info("Start the primary shards"); RoutingNodes routingNodes = clusterState.getRoutingNodes(); - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().node("node1").numberOfShardsWithState(ShardRoutingState.STARTED), equalTo(2)); assertThat(clusterState.getRoutingNodes().node("node1").numberOfShardsWithState(ShardRoutingState.INITIALIZING), equalTo(0)); @@ -83,8 +83,8 @@ public class ShardsLimitAllocationTests extends ESAllocationTestCase { logger.info("Do another reroute, make sure its still not allocated"); routingNodes = clusterState.getRoutingNodes(); - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); } public void testClusterLevelShardsLimitAllocate() { @@ -108,16 +108,16 @@ public class ShardsLimitAllocationTests extends ESAllocationTestCase { ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); logger.info("Adding two nodes and performing rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().node("node1").numberOfShardsWithState(ShardRoutingState.INITIALIZING), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node2").numberOfShardsWithState(ShardRoutingState.INITIALIZING), equalTo(1)); logger.info("Start the primary shards"); RoutingNodes routingNodes = clusterState.getRoutingNodes(); - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().node("node1").numberOfShardsWithState(ShardRoutingState.STARTED), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node2").numberOfShardsWithState(ShardRoutingState.STARTED), equalTo(1)); @@ -130,15 +130,15 @@ public class ShardsLimitAllocationTests extends ESAllocationTestCase { .build()); logger.info("Do another reroute, make sure shards are now allocated"); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().node("node1").numberOfShardsWithState(ShardRoutingState.INITIALIZING), equalTo(1)); assertThat(clusterState.getRoutingNodes().node("node2").numberOfShardsWithState(ShardRoutingState.INITIALIZING), equalTo(1)); routingNodes = clusterState.getRoutingNodes(); - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().node("node1").numberOfShardsWithState(ShardRoutingState.STARTED), equalTo(2)); assertThat(clusterState.getRoutingNodes().node("node2").numberOfShardsWithState(ShardRoutingState.STARTED), equalTo(2)); @@ -164,44 +164,44 @@ public class ShardsLimitAllocationTests extends ESAllocationTestCase { )) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = 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(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("Adding one node and reroute"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1"))).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("Start the primary shards"); RoutingNodes routingNodes = clusterState.getRoutingNodes(); - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(numberOfShardsOfType(clusterState.getRoutingNodes(), STARTED), equalTo(5)); logger.info("add another index with 5 shards"); - metaData = MetaData.builder(metaData) + metaData = MetaData.builder(clusterState.metaData()) .put(IndexMetaData.builder("test1").settings(settings(Version.CURRENT) .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 5) .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0) )) .build(); - routingTable = RoutingTable.builder(routingTable) - .addAsNew(metaData.index("test1")) - .build(); + RoutingTable updatedRoutingTable = RoutingTable.builder(clusterState.routingTable()) + .addAsNew(metaData.index("test1")) + .build(); - clusterState = ClusterState.builder(clusterState).metaData(metaData).routingTable(routingTable).build(); + clusterState = ClusterState.builder(clusterState).metaData(metaData).routingTable(updatedRoutingTable).build(); logger.info("Add another one node and reroute"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node2"))).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(numberOfShardsOfType(clusterState.getRoutingNodes(), STARTED), equalTo(10)); @@ -213,8 +213,8 @@ public class ShardsLimitAllocationTests extends ESAllocationTestCase { } logger.info("update {} for test, see that things move", ShardsLimitAllocationDecider.INDEX_TOTAL_SHARDS_PER_NODE_SETTING.getKey()); - metaData = MetaData.builder(metaData) - .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT) + metaData = MetaData.builder(clusterState.metaData()) + .put(IndexMetaData.builder(clusterState.metaData().index("test")).settings(settings(Version.CURRENT) .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 5) .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0) .put(ShardsLimitAllocationDecider.INDEX_TOTAL_SHARDS_PER_NODE_SETTING.getKey(), 3) @@ -225,8 +225,8 @@ public class ShardsLimitAllocationTests extends ESAllocationTestCase { clusterState = ClusterState.builder(clusterState).metaData(metaData).build(); logger.info("reroute after setting"); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().node("node1").numberOfShardsWithState(STARTED), equalTo(3)); assertThat(clusterState.getRoutingNodes().node("node1").numberOfShardsWithState(RELOCATING), equalTo(2)); @@ -235,8 +235,8 @@ public class ShardsLimitAllocationTests extends ESAllocationTestCase { // the first move will destroy the balance and the balancer will move 2 shards from node2 to node one right after // moving the nodes to node2 since we consider INITIALIZING nodes during rebalance routingNodes = clusterState.getRoutingNodes(); - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); // now we are done compared to EvenShardCountAllocator since the Balancer is not soely based on the average assertThat(clusterState.getRoutingNodes().node("node1").numberOfShardsWithState(STARTED), equalTo(5)); assertThat(clusterState.getRoutingNodes().node("node2").numberOfShardsWithState(STARTED), equalTo(5)); diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/SingleShardNoReplicasRoutingTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/SingleShardNoReplicasRoutingTests.java index acb5efc48b9..0a1bf218b59 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/SingleShardNoReplicasRoutingTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/SingleShardNoReplicasRoutingTests.java @@ -67,96 +67,87 @@ public class SingleShardNoReplicasRoutingTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(0)) .build(); - RoutingTable routingTable = RoutingTable.builder() - .addAsNew(metaData.index("test")) - .build(); + RoutingTable initialRoutingTable = 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(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); - assertThat(routingTable.index("test").shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).shards().get(0).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(0).shards().get(0).currentNodeId(), nullValue()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().get(0).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().get(0).currentNodeId(), nullValue()); logger.info("Adding one node and performing rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1"))).build(); - RoutingTable prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(routingTable.index("test").shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).shards().get(0).state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test").shard(0).shards().get(0).currentNodeId(), equalTo("node1")); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().get(0).state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().get(0).currentNodeId(), equalTo("node1")); logger.info("Rerouting again, nothing should change"); - prevRoutingTable = routingTable; clusterState = ClusterState.builder(clusterState).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - assertThat(routingTable == prevRoutingTable, equalTo(true)); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + assertThat(routingResult.changed(), equalTo(false)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("Marking the shard as started"); RoutingNodes routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.node("node1").shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.node("node1").shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(routingTable != prevRoutingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).shards().get(0).state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(0).shards().get(0).currentNodeId(), equalTo("node1")); + assertThat(routingResult.changed(), equalTo(true)); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().get(0).state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().get(0).currentNodeId(), equalTo("node1")); logger.info("Starting another node and making sure nothing changed"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node2"))).build(); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(routingTable == prevRoutingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).shards().get(0).state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(0).shards().get(0).currentNodeId(), equalTo("node1")); + assertThat(routingResult.changed(), equalTo(false)); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().get(0).state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().get(0).currentNodeId(), equalTo("node1")); logger.info("Killing node1 where the shard is, checking the shard is relocated"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).remove("node1")).build(); - prevRoutingTable = routingTable; - routingTable = strategy.deassociateDeadNodes(clusterState, true, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.deassociateDeadNodes(clusterState, true, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(routingTable != prevRoutingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).shards().get(0).state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test").shard(0).shards().get(0).currentNodeId(), equalTo("node2")); + assertThat(routingResult.changed(), equalTo(true)); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().get(0).state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().get(0).currentNodeId(), equalTo("node2")); logger.info("Start another node, make sure that things remain the same (shard is in node2 and initializing)"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node3"))).build(); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - assertThat(routingTable == prevRoutingTable, equalTo(true)); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); + assertThat(routingResult.changed(), equalTo(false)); logger.info("Start the shard on node 2"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.node("node2").shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.node("node2").shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(routingTable != prevRoutingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).shards().get(0).state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(0).shards().get(0).currentNodeId(), equalTo("node2")); + assertThat(routingResult.changed(), equalTo(true)); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().get(0).state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().get(0).currentNodeId(), equalTo("node2")); } public void testSingleIndexShardFailed() { @@ -168,44 +159,41 @@ public class SingleShardNoReplicasRoutingTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(0)) .build(); - RoutingTable routingTable = RoutingTable.builder() - .addAsNew(metaData.index("test")) - .build(); + RoutingTable.Builder routingTableBuilder = RoutingTable.builder() + .addAsNew(metaData.index("test")); - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTableBuilder.build()).build(); - assertThat(routingTable.index("test").shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).shards().get(0).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(0).shards().get(0).currentNodeId(), nullValue()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().get(0).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().get(0).currentNodeId(), nullValue()); logger.info("Adding one node and rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1"))).build(); - RoutingTable prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).shards().get(0).unassigned(), equalTo(false)); - assertThat(routingTable.index("test").shard(0).shards().get(0).state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test").shard(0).shards().get(0).currentNodeId(), equalTo("node1")); + assertThat(routingResult.changed(), equalTo(true)); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().get(0).unassigned(), equalTo(false)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().get(0).state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().get(0).currentNodeId(), equalTo("node1")); logger.info("Marking the shard as failed"); RoutingNodes routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyFailedShard(clusterState, routingNodes.node("node1").shardsWithState(INITIALIZING).get(0)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyFailedShard(clusterState, routingNodes.node("node1").shardsWithState(INITIALIZING).get(0)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).shards().get(0).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(0).shards().get(0).currentNodeId(), nullValue()); + assertThat(routingResult.changed(), equalTo(true)); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().get(0).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().get(0).currentNodeId(), nullValue()); } public void testMultiIndexEvenDistribution() { @@ -228,16 +216,15 @@ public class SingleShardNoReplicasRoutingTests extends ESAllocationTestCase { for (int i = 0; i < numberOfIndices; i++) { routingTableBuilder.addAsNew(metaData.index("test" + i)); } - RoutingTable routingTable = routingTableBuilder.build(); - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTableBuilder.build()).build(); - assertThat(routingTable.indicesRouting().size(), equalTo(numberOfIndices)); + assertThat(clusterState.routingTable().indicesRouting().size(), equalTo(numberOfIndices)); for (int i = 0; i < numberOfIndices; i++) { - assertThat(routingTable.index("test" + i).shards().size(), equalTo(1)); - assertThat(routingTable.index("test" + i).shard(0).size(), equalTo(1)); - assertThat(routingTable.index("test" + i).shard(0).shards().size(), equalTo(1)); - assertThat(routingTable.index("test" + i).shard(0).shards().get(0).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test" + i).shard(0).shards().get(0).currentNodeId(), nullValue()); + assertThat(clusterState.routingTable().index("test" + i).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test" + i).shard(0).size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test" + i).shard(0).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test" + i).shard(0).shards().get(0).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test" + i).shard(0).shards().get(0).currentNodeId(), nullValue()); } logger.info("Adding " + (numberOfIndices / 2) + " nodes"); @@ -246,21 +233,20 @@ public class SingleShardNoReplicasRoutingTests extends ESAllocationTestCase { for (int i = 0; i < (numberOfIndices / 2); i++) { nodesBuilder.add(newNode("node" + i)); } - RoutingTable prevRoutingTable = routingTable; clusterState = ClusterState.builder(clusterState).nodes(nodesBuilder).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); + assertThat(routingResult.changed(), equalTo(true)); for (int i = 0; i < numberOfIndices; i++) { - assertThat(routingTable.index("test" + i).shards().size(), equalTo(1)); - assertThat(routingTable.index("test" + i).shard(0).size(), equalTo(1)); - assertThat(routingTable.index("test" + i).shard(0).shards().size(), equalTo(1)); - assertThat(routingTable.index("test" + i).shard(0).shards().get(0).unassigned(), equalTo(false)); - assertThat(routingTable.index("test" + i).shard(0).shards().get(0).state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test" + i).shard(0).shards().get(0).primary(), equalTo(true)); + assertThat(clusterState.routingTable().index("test" + i).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test" + i).shard(0).size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test" + i).shard(0).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test" + i).shard(0).shards().get(0).unassigned(), equalTo(false)); + assertThat(clusterState.routingTable().index("test" + i).shard(0).shards().get(0).state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test" + i).shard(0).shards().get(0).primary(), equalTo(true)); // make sure we still have 2 shards initializing per node on the first 25 nodes - String nodeId = routingTable.index("test" + i).shard(0).shards().get(0).currentNodeId(); + String nodeId = clusterState.routingTable().index("test" + i).shard(0).shards().get(0).currentNodeId(); int nodeIndex = Integer.parseInt(nodeId.substring("node".length())); assertThat(nodeIndex, lessThan(25)); } @@ -284,35 +270,33 @@ public class SingleShardNoReplicasRoutingTests extends ESAllocationTestCase { for (int i = (numberOfIndices / 2); i < numberOfIndices; i++) { nodesBuilder.add(newNode("node" + i)); } - prevRoutingTable = routingTable; clusterState = ClusterState.builder(clusterState).nodes(nodesBuilder).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(false)); + assertThat(routingResult.changed(), equalTo(false)); logger.info("Marking the shard as started"); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); + assertThat(routingResult.changed(), equalTo(true)); int numberOfRelocatingShards = 0; int numberOfStartedShards = 0; for (int i = 0; i < numberOfIndices; i++) { - assertThat(routingTable.index("test" + i).shards().size(), equalTo(1)); - assertThat(routingTable.index("test" + i).shard(0).size(), equalTo(1)); - assertThat(routingTable.index("test" + i).shard(0).shards().size(), equalTo(1)); - assertThat(routingTable.index("test" + i).shard(0).shards().get(0).unassigned(), equalTo(false)); - assertThat(routingTable.index("test" + i).shard(0).shards().get(0).state(), anyOf(equalTo(STARTED), equalTo(RELOCATING))); - if (routingTable.index("test" + i).shard(0).shards().get(0).state() == STARTED) { + assertThat(clusterState.routingTable().index("test" + i).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test" + i).shard(0).size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test" + i).shard(0).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test" + i).shard(0).shards().get(0).unassigned(), equalTo(false)); + assertThat(clusterState.routingTable().index("test" + i).shard(0).shards().get(0).state(), anyOf(equalTo(STARTED), equalTo(RELOCATING))); + if (clusterState.routingTable().index("test" + i).shard(0).shards().get(0).state() == STARTED) { numberOfStartedShards++; - } else if (routingTable.index("test" + i).shard(0).shards().get(0).state() == RELOCATING) { + } else if (clusterState.routingTable().index("test" + i).shard(0).shards().get(0).state() == RELOCATING) { numberOfRelocatingShards++; } - assertThat(routingTable.index("test" + i).shard(0).shards().get(0).primary(), equalTo(true)); + assertThat(clusterState.routingTable().index("test" + i).shard(0).shards().get(0).primary(), equalTo(true)); // make sure we still have 2 shards either relocating or started on the first 25 nodes (still) - String nodeId = routingTable.index("test" + i).shard(0).shards().get(0).currentNodeId(); + String nodeId = clusterState.routingTable().index("test" + i).shard(0).shards().get(0).currentNodeId(); int nodeIndex = Integer.parseInt(nodeId.substring("node".length())); assertThat(nodeIndex, lessThan(25)); } @@ -340,26 +324,24 @@ public class SingleShardNoReplicasRoutingTests extends ESAllocationTestCase { for (int i = 0; i < numberOfIndices; i++) { routingTableBuilder.addAsNew(metaData.index("test" + i)); } - RoutingTable routingTable = routingTableBuilder.build(); - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTableBuilder.build()).build(); - assertThat(routingTable.indicesRouting().size(), equalTo(numberOfIndices)); + assertThat(clusterState.routingTable().indicesRouting().size(), equalTo(numberOfIndices)); logger.info("Starting 3 nodes and rerouting"); clusterState = ClusterState.builder(clusterState) .nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2")).add(newNode("node3"))) .build(); - RoutingTable prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); + assertThat(routingResult.changed(), equalTo(true)); for (int i = 0; i < numberOfIndices; i++) { - assertThat(routingTable.index("test" + i).shards().size(), equalTo(1)); - assertThat(routingTable.index("test" + i).shard(0).size(), equalTo(1)); - assertThat(routingTable.index("test" + i).shard(0).shards().size(), equalTo(1)); - assertThat(routingTable.index("test" + i).shard(0).shards().get(0).state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test" + i).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test" + i).shard(0).size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test" + i).shard(0).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test" + i).shard(0).shards().get(0).state(), equalTo(INITIALIZING)); } RoutingNodes routingNodes = clusterState.getRoutingNodes(); assertThat(numberOfShardsOfType(routingNodes, INITIALIZING), equalTo(numberOfIndices)); @@ -371,41 +353,36 @@ public class SingleShardNoReplicasRoutingTests extends ESAllocationTestCase { clusterState = ClusterState.builder(clusterState) .nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node4")).add(newNode("node5"))) .build(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - - assertThat(prevRoutingTable == routingTable, equalTo(true)); + assertThat(routingResult.changed(), equalTo(false)); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); + assertThat(routingResult.changed(), equalTo(true)); for (int i = 0; i < numberOfIndices; i++) { - assertThat(routingTable.index("test" + i).shards().size(), equalTo(1)); - assertThat(routingTable.index("test" + i).shard(0).size(), equalTo(1)); - assertThat(routingTable.index("test" + i).shard(0).shards().size(), equalTo(1)); - assertThat(routingTable.index("test" + i).shard(0).shards().get(0).state(), anyOf(equalTo(RELOCATING), equalTo(STARTED))); + assertThat(clusterState.routingTable().index("test" + i).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test" + i).shard(0).size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test" + i).shard(0).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test" + i).shard(0).shards().get(0).state(), anyOf(equalTo(RELOCATING), equalTo(STARTED))); } routingNodes = clusterState.getRoutingNodes(); assertThat("4 source shard routing are relocating", numberOfShardsOfType(routingNodes, RELOCATING), equalTo(4)); assertThat("4 target shard routing are initializing", numberOfShardsOfType(routingNodes, INITIALIZING), equalTo(4)); logger.info("Now, mark the relocated as started"); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); // routingTable = strategy.reroute(new RoutingStrategyInfo(metaData, routingTable), nodes); - assertThat(prevRoutingTable != routingTable, equalTo(true)); + assertThat(routingResult.changed(), equalTo(true)); for (int i = 0; i < numberOfIndices; i++) { - assertThat(routingTable.index("test" + i).shards().size(), equalTo(1)); - assertThat(routingTable.index("test" + i).shard(0).size(), equalTo(1)); - assertThat(routingTable.index("test" + i).shard(0).shards().size(), equalTo(1)); - assertThat(routingTable.index("test" + i).shard(0).shards().get(0).state(), anyOf(equalTo(RELOCATING), equalTo(STARTED))); + assertThat(clusterState.routingTable().index("test" + i).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test" + i).shard(0).size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test" + i).shard(0).shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test" + i).shard(0).shards().get(0).state(), anyOf(equalTo(RELOCATING), equalTo(STARTED))); } routingNodes = clusterState.getRoutingNodes(); assertThat(numberOfShardsOfType(routingNodes, STARTED), equalTo(numberOfIndices)); diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/SingleShardOneReplicaRoutingTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/SingleShardOneReplicaRoutingTests.java index 75893cc135a..95d36815293 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/SingleShardOneReplicaRoutingTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/SingleShardOneReplicaRoutingTests.java @@ -52,118 +52,111 @@ public class SingleShardOneReplicaRoutingTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = 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(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); - assertThat(routingTable.index("test").shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(0).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(0).shards().get(0).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(0).shards().get(1).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(0).shards().get(0).currentNodeId(), nullValue()); - assertThat(routingTable.index("test").shard(0).shards().get(1).currentNodeId(), nullValue()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().get(0).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().get(1).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().get(0).currentNodeId(), nullValue()); + assertThat(clusterState.routingTable().index("test").shard(0).shards().get(1).currentNodeId(), nullValue()); logger.info("Adding one node and performing rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1"))).build(); - RoutingTable prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(0).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(0).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test").shard(0).primaryShard().currentNodeId(), equalTo("node1")); - assertThat(routingTable.index("test").shard(0).replicaShards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).replicaShards().get(0).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(0).replicaShards().get(0).currentNodeId(), nullValue()); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(0).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test").shard(0).primaryShard().currentNodeId(), equalTo("node1")); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShards().get(0).currentNodeId(), nullValue()); logger.info("Add another node and perform rerouting, nothing will happen since primary shards not started"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node2"))).build(); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable == routingTable, equalTo(true)); + assertFalse(routingResult.changed()); logger.info("Start the primary shard (on node1)"); RoutingNodes routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.node("node1").shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.node("node1").shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(0).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(0).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(0).primaryShard().currentNodeId(), equalTo("node1")); - assertThat(routingTable.index("test").shard(0).replicaShards().size(), equalTo(1)); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(0).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(0).primaryShard().currentNodeId(), equalTo("node1")); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShards().size(), equalTo(1)); // backup shards are initializing as well, we make sure that they recover from primary *started* shards in the IndicesClusterStateService - assertThat(routingTable.index("test").shard(0).replicaShards().get(0).state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test").shard(0).replicaShards().get(0).currentNodeId(), equalTo("node2")); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShards().get(0).state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShards().get(0).currentNodeId(), equalTo("node2")); logger.info("Reroute, nothing should change"); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - assertThat(prevRoutingTable == routingTable, equalTo(true)); + routingResult = strategy.reroute(clusterState, "reroute"); + assertFalse(routingResult.changed()); logger.info("Start the backup shard"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.node("node2").shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.node("node2").shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(0).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(0).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(0).primaryShard().currentNodeId(), equalTo("node1")); - assertThat(routingTable.index("test").shard(0).replicaShards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).replicaShards().get(0).state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(0).replicaShards().get(0).currentNodeId(), equalTo("node2")); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(0).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(0).primaryShard().currentNodeId(), equalTo("node1")); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShards().get(0).state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShards().get(0).currentNodeId(), equalTo("node2")); logger.info("Kill node1, backup shard should become primary"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).remove("node1")).build(); - prevRoutingTable = routingTable; - routingTable = strategy.deassociateDeadNodes(clusterState, true, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.deassociateDeadNodes(clusterState, true, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(0).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(0).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(0).primaryShard().currentNodeId(), equalTo("node2")); - assertThat(routingTable.index("test").shard(0).replicaShards().size(), equalTo(1)); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(0).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(0).primaryShard().currentNodeId(), equalTo("node2")); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShards().size(), equalTo(1)); // backup shards are initializing as well, we make sure that they recover from primary *started* shards in the IndicesClusterStateService - assertThat(routingTable.index("test").shard(0).replicaShards().get(0).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(0).replicaShards().get(0).currentNodeId(), nullValue()); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShards().get(0).currentNodeId(), nullValue()); logger.info("Start another node, backup shard should start initializing"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node3"))).build(); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(0).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(0).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(0).primaryShard().currentNodeId(), equalTo("node2")); - assertThat(routingTable.index("test").shard(0).replicaShards().size(), equalTo(1)); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(0).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(0).primaryShard().currentNodeId(), equalTo("node2")); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShards().size(), equalTo(1)); // backup shards are initializing as well, we make sure that they recover from primary *started* shards in the IndicesClusterStateService - assertThat(routingTable.index("test").shard(0).replicaShards().get(0).state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test").shard(0).replicaShards().get(0).currentNodeId(), equalTo("node3")); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShards().get(0).state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShards().get(0).currentNodeId(), equalTo("node3")); } } diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/StartedShardsRoutingTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/StartedShardsRoutingTests.java index cc3d0054d82..74688056892 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/StartedShardsRoutingTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/StartedShardsRoutingTests.java @@ -24,6 +24,7 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.cluster.node.DiscoveryNodes; +import org.elasticsearch.cluster.routing.AllocationId; import org.elasticsearch.cluster.routing.IndexRoutingTable; import org.elasticsearch.cluster.routing.IndexShardRoutingTable; import org.elasticsearch.cluster.routing.RoutingTable; @@ -36,6 +37,7 @@ import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.cluster.ESAllocationTestCase; import java.util.Arrays; +import java.util.Collections; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.nullValue; @@ -45,9 +47,11 @@ public class StartedShardsRoutingTests extends ESAllocationTestCase { AllocationService allocation = createAllocationService(); logger.info("--> building initial cluster state"); + AllocationId allocationId = AllocationId.newRelocation(AllocationId.newInitializing()); final IndexMetaData indexMetaData = IndexMetaData.builder("test") .settings(settings(Version.CURRENT)) .numberOfShards(2).numberOfReplicas(0) + .putActiveAllocationIds(1, Collections.singleton(allocationId.getId())) .build(); final Index index = indexMetaData.getIndex(); ClusterState.Builder stateBuilder = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)) @@ -55,7 +59,7 @@ public class StartedShardsRoutingTests extends ESAllocationTestCase { .metaData(MetaData.builder().put(indexMetaData, false)); final ShardRouting initShard = TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", true, ShardRoutingState.INITIALIZING); - final ShardRouting relocatingShard = TestShardRouting.newShardRouting(new ShardId(index, 1), "node1", "node2", true, ShardRoutingState.RELOCATING); + final ShardRouting relocatingShard = TestShardRouting.newShardRouting(new ShardId(index, 1), "node1", "node2", true, ShardRoutingState.RELOCATING, allocationId); stateBuilder.routingTable(RoutingTable.builder().add(IndexRoutingTable.builder(index) .addIndexShard(new IndexShardRoutingTable.Builder(initShard.shardId()).addShard(initShard).build()) .addIndexShard(new IndexShardRoutingTable.Builder(relocatingShard.shardId()).addShard(relocatingShard).build())).build()); diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/TenShardsOneReplicaRoutingTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/TenShardsOneReplicaRoutingTests.java index 9d5f4803a27..f629e627671 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/TenShardsOneReplicaRoutingTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/TenShardsOneReplicaRoutingTests.java @@ -63,103 +63,97 @@ public class TenShardsOneReplicaRoutingTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(10).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = 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(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); - assertThat(routingTable.index("test").shards().size(), equalTo(10)); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).shards().get(0).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(i).shards().get(1).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(i).shards().get(0).currentNodeId(), nullValue()); - assertThat(routingTable.index("test").shard(i).shards().get(1).currentNodeId(), nullValue()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(10)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(0).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(1).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(0).currentNodeId(), nullValue()); + assertThat(clusterState.routingTable().index("test").shard(i).shards().get(1).currentNodeId(), nullValue()); } logger.info("Adding one node and performing rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1"))).build(); - RoutingTable prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(10)); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test").shard(i).primaryShard().currentNodeId(), equalTo("node1")); - assertThat(routingTable.index("test").shard(i).replicaShards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).currentNodeId(), nullValue()); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(10)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().currentNodeId(), equalTo("node1")); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).currentNodeId(), nullValue()); } logger.info("Add another node and perform rerouting, nothing will happen since primary not started"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node2"))).build(); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable == routingTable, equalTo(true)); + assertFalse(routingResult.changed()); logger.info("Start the primary shard (on node1)"); RoutingNodes routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.node("node1").shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.node("node1").shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(10)); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(i).primaryShard().currentNodeId(), equalTo("node1")); - assertThat(routingTable.index("test").shard(i).replicaShards().size(), equalTo(1)); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(10)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().currentNodeId(), equalTo("node1")); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().size(), equalTo(1)); // backup shards are initializing as well, we make sure that they recover from primary *started* shards in the IndicesClusterStateService - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).currentNodeId(), equalTo("node2")); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).currentNodeId(), equalTo("node2")); } logger.info("Reroute, nothing should change"); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - assertThat(prevRoutingTable == routingTable, equalTo(true)); + routingResult = strategy.reroute(clusterState, "reroute"); + assertFalse(routingResult.changed()); logger.info("Start the backup shard"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.node("node2").shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.node("node2").shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(10)); - for (int i = 0; i < routingTable.index("test").shards().size(); i++) { - assertThat(routingTable.index("test").shard(i).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(i).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(i).primaryShard().currentNodeId(), equalTo("node1")); - assertThat(routingTable.index("test").shard(i).replicaShards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(i).replicaShards().get(0).currentNodeId(), equalTo("node2")); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(10)); + for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) { + assertThat(clusterState.routingTable().index("test").shard(i).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().currentNodeId(), equalTo("node1")); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).currentNodeId(), equalTo("node2")); } assertThat(routingNodes.node("node1").numberOfShardsWithState(STARTED), equalTo(10)); assertThat(routingNodes.node("node2").numberOfShardsWithState(STARTED), equalTo(10)); logger.info("Add another node and perform rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node3"))).build(); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(10)); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(10)); assertThat(routingNodes.node("node1").numberOfShardsWithState(STARTED, RELOCATING), equalTo(10)); assertThat(routingNodes.node("node1").numberOfShardsWithState(STARTED), lessThan(10)); assertThat(routingNodes.node("node2").numberOfShardsWithState(STARTED, RELOCATING), equalTo(10)); @@ -168,13 +162,12 @@ public class TenShardsOneReplicaRoutingTests extends ESAllocationTestCase { logger.info("Start the shards on node 3"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.node("node3").shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.node("node3").shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); routingNodes = clusterState.getRoutingNodes(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(10)); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(10)); assertThat(routingNodes.node("node1").numberOfShardsWithState(STARTED), equalTo(7)); assertThat(routingNodes.node("node2").numberOfShardsWithState(STARTED), equalTo(7)); assertThat(routingNodes.node("node3").numberOfShardsWithState(STARTED), equalTo(6)); diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/ThrottlingAllocationTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/ThrottlingAllocationTests.java index 9fa83f74208..14ba4dc8d3d 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/ThrottlingAllocationTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/ThrottlingAllocationTests.java @@ -61,50 +61,50 @@ public class ThrottlingAllocationTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(10).numberOfReplicas(1)) .build(); - RoutingTable routingTable = createRecoveryRoutingTable(metaData.index("test")); + RoutingTable initialRoutingTable = createRecoveryRoutingTable(metaData.index("test")); - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("start one node, do reroute, only 3 should initialize"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1"))).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(0)); - assertThat(routingTable.shardsWithState(INITIALIZING).size(), equalTo(3)); - assertThat(routingTable.shardsWithState(UNASSIGNED).size(), equalTo(17)); + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(0)); + assertThat(clusterState.routingTable().shardsWithState(INITIALIZING).size(), equalTo(3)); + assertThat(clusterState.routingTable().shardsWithState(UNASSIGNED).size(), equalTo(17)); logger.info("start initializing, another 3 should initialize"); - routingTable = strategy.applyStartedShards(clusterState, routingTable.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.routingTable().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(3)); - assertThat(routingTable.shardsWithState(INITIALIZING).size(), equalTo(3)); - assertThat(routingTable.shardsWithState(UNASSIGNED).size(), equalTo(14)); + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(3)); + assertThat(clusterState.routingTable().shardsWithState(INITIALIZING).size(), equalTo(3)); + assertThat(clusterState.routingTable().shardsWithState(UNASSIGNED).size(), equalTo(14)); logger.info("start initializing, another 3 should initialize"); - routingTable = strategy.applyStartedShards(clusterState, routingTable.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.routingTable().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(6)); - assertThat(routingTable.shardsWithState(INITIALIZING).size(), equalTo(3)); - assertThat(routingTable.shardsWithState(UNASSIGNED).size(), equalTo(11)); + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(6)); + assertThat(clusterState.routingTable().shardsWithState(INITIALIZING).size(), equalTo(3)); + assertThat(clusterState.routingTable().shardsWithState(UNASSIGNED).size(), equalTo(11)); logger.info("start initializing, another 1 should initialize"); - routingTable = strategy.applyStartedShards(clusterState, routingTable.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.routingTable().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(9)); - assertThat(routingTable.shardsWithState(INITIALIZING).size(), equalTo(1)); - assertThat(routingTable.shardsWithState(UNASSIGNED).size(), equalTo(10)); + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(9)); + assertThat(clusterState.routingTable().shardsWithState(INITIALIZING).size(), equalTo(1)); + assertThat(clusterState.routingTable().shardsWithState(UNASSIGNED).size(), equalTo(10)); logger.info("start initializing, all primaries should be started"); - routingTable = strategy.applyStartedShards(clusterState, routingTable.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.routingTable().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(10)); - assertThat(routingTable.shardsWithState(INITIALIZING).size(), equalTo(0)); - assertThat(routingTable.shardsWithState(UNASSIGNED).size(), equalTo(10)); + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(10)); + assertThat(clusterState.routingTable().shardsWithState(INITIALIZING).size(), equalTo(0)); + assertThat(clusterState.routingTable().shardsWithState(UNASSIGNED).size(), equalTo(10)); } public void testReplicaAndPrimaryRecoveryThrottling() { @@ -120,59 +120,59 @@ public class ThrottlingAllocationTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(5).numberOfReplicas(1)) .build(); - RoutingTable routingTable = createRecoveryRoutingTable(metaData.index("test")); + RoutingTable initialRoutingTable = createRecoveryRoutingTable(metaData.index("test")); - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("start one node, do reroute, only 3 should initialize"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1"))).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(0)); - assertThat(routingTable.shardsWithState(INITIALIZING).size(), equalTo(3)); - assertThat(routingTable.shardsWithState(UNASSIGNED).size(), equalTo(7)); + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(0)); + assertThat(clusterState.routingTable().shardsWithState(INITIALIZING).size(), equalTo(3)); + assertThat(clusterState.routingTable().shardsWithState(UNASSIGNED).size(), equalTo(7)); logger.info("start initializing, another 2 should initialize"); - routingTable = strategy.applyStartedShards(clusterState, routingTable.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.routingTable().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(3)); - assertThat(routingTable.shardsWithState(INITIALIZING).size(), equalTo(2)); - assertThat(routingTable.shardsWithState(UNASSIGNED).size(), equalTo(5)); + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(3)); + assertThat(clusterState.routingTable().shardsWithState(INITIALIZING).size(), equalTo(2)); + assertThat(clusterState.routingTable().shardsWithState(UNASSIGNED).size(), equalTo(5)); logger.info("start initializing, all primaries should be started"); - routingTable = strategy.applyStartedShards(clusterState, routingTable.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.routingTable().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(5)); - assertThat(routingTable.shardsWithState(INITIALIZING).size(), equalTo(0)); - assertThat(routingTable.shardsWithState(UNASSIGNED).size(), equalTo(5)); + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(5)); + assertThat(clusterState.routingTable().shardsWithState(INITIALIZING).size(), equalTo(0)); + assertThat(clusterState.routingTable().shardsWithState(UNASSIGNED).size(), equalTo(5)); logger.info("start another node, replicas should start being allocated"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node2"))).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(5)); - assertThat(routingTable.shardsWithState(INITIALIZING).size(), equalTo(3)); - assertThat(routingTable.shardsWithState(UNASSIGNED).size(), equalTo(2)); + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(5)); + assertThat(clusterState.routingTable().shardsWithState(INITIALIZING).size(), equalTo(3)); + assertThat(clusterState.routingTable().shardsWithState(UNASSIGNED).size(), equalTo(2)); logger.info("start initializing replicas"); - routingTable = strategy.applyStartedShards(clusterState, routingTable.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.routingTable().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(8)); - assertThat(routingTable.shardsWithState(INITIALIZING).size(), equalTo(2)); - assertThat(routingTable.shardsWithState(UNASSIGNED).size(), equalTo(0)); + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(8)); + assertThat(clusterState.routingTable().shardsWithState(INITIALIZING).size(), equalTo(2)); + assertThat(clusterState.routingTable().shardsWithState(UNASSIGNED).size(), equalTo(0)); logger.info("start initializing replicas, all should be started"); - routingTable = strategy.applyStartedShards(clusterState, routingTable.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.routingTable().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(10)); - assertThat(routingTable.shardsWithState(INITIALIZING).size(), equalTo(0)); - assertThat(routingTable.shardsWithState(UNASSIGNED).size(), equalTo(0)); + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(10)); + assertThat(clusterState.routingTable().shardsWithState(INITIALIZING).size(), equalTo(0)); + assertThat(clusterState.routingTable().shardsWithState(UNASSIGNED).size(), equalTo(0)); } public void testThrottleIncomingAndOutgoing() { @@ -188,52 +188,52 @@ public class ThrottlingAllocationTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(9).numberOfReplicas(0)) .build(); - RoutingTable routingTable = createRecoveryRoutingTable(metaData.index("test")); + RoutingTable initialRoutingTable = createRecoveryRoutingTable(metaData.index("test")); - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("start one node, do reroute, only 5 should initialize"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1"))).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(0)); - assertThat(routingTable.shardsWithState(INITIALIZING).size(), equalTo(5)); - assertThat(routingTable.shardsWithState(UNASSIGNED).size(), equalTo(4)); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(0)); + assertThat(clusterState.routingTable().shardsWithState(INITIALIZING).size(), equalTo(5)); + assertThat(clusterState.routingTable().shardsWithState(UNASSIGNED).size(), equalTo(4)); assertEquals(clusterState.getRoutingNodes().getIncomingRecoveries("node1"), 5); logger.info("start initializing, all primaries should be started"); - routingTable = strategy.applyStartedShards(clusterState, routingTable.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.routingTable().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(5)); - assertThat(routingTable.shardsWithState(INITIALIZING).size(), equalTo(4)); - assertThat(routingTable.shardsWithState(UNASSIGNED).size(), equalTo(0)); + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(5)); + assertThat(clusterState.routingTable().shardsWithState(INITIALIZING).size(), equalTo(4)); + assertThat(clusterState.routingTable().shardsWithState(UNASSIGNED).size(), equalTo(0)); - routingTable = strategy.applyStartedShards(clusterState, routingTable.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.routingTable().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("start another 2 nodes, 5 shards should be relocating - at most 5 are allowed per node"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node2")).add(newNode("node3"))).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(4)); - assertThat(routingTable.shardsWithState(RELOCATING).size(), equalTo(5)); - assertThat(routingTable.shardsWithState(INITIALIZING).size(), equalTo(5)); - assertThat(routingTable.shardsWithState(UNASSIGNED).size(), equalTo(0)); + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(4)); + assertThat(clusterState.routingTable().shardsWithState(RELOCATING).size(), equalTo(5)); + assertThat(clusterState.routingTable().shardsWithState(INITIALIZING).size(), equalTo(5)); + assertThat(clusterState.routingTable().shardsWithState(UNASSIGNED).size(), equalTo(0)); assertEquals(clusterState.getRoutingNodes().getIncomingRecoveries("node2"), 3); assertEquals(clusterState.getRoutingNodes().getIncomingRecoveries("node3"), 2); assertEquals(clusterState.getRoutingNodes().getIncomingRecoveries("node1"), 0); assertEquals(clusterState.getRoutingNodes().getOutgoingRecoveries("node1"), 5); - routingTable = strategy.applyStartedShards(clusterState, routingTable.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.routingTable().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("start the relocating shards, one more shard should relocate away from node1"); - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(8)); - assertThat(routingTable.shardsWithState(RELOCATING).size(), equalTo(1)); - assertThat(routingTable.shardsWithState(INITIALIZING).size(), equalTo(1)); - assertThat(routingTable.shardsWithState(UNASSIGNED).size(), equalTo(0)); + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(8)); + assertThat(clusterState.routingTable().shardsWithState(RELOCATING).size(), equalTo(1)); + assertThat(clusterState.routingTable().shardsWithState(INITIALIZING).size(), equalTo(1)); + assertThat(clusterState.routingTable().shardsWithState(UNASSIGNED).size(), equalTo(0)); assertEquals(clusterState.getRoutingNodes().getIncomingRecoveries("node2"), 0); assertEquals(clusterState.getRoutingNodes().getIncomingRecoveries("node3"), 1); assertEquals(clusterState.getRoutingNodes().getIncomingRecoveries("node1"), 0); @@ -251,59 +251,59 @@ public class ThrottlingAllocationTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(2)) .build(); - RoutingTable routingTable = createRecoveryRoutingTable(metaData.index("test")); + RoutingTable initialRoutingTable = createRecoveryRoutingTable(metaData.index("test")); - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("start one node, do reroute, only 1 should initialize"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1"))).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(0)); - assertThat(routingTable.shardsWithState(INITIALIZING).size(), equalTo(1)); - assertThat(routingTable.shardsWithState(UNASSIGNED).size(), equalTo(2)); + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(0)); + assertThat(clusterState.routingTable().shardsWithState(INITIALIZING).size(), equalTo(1)); + assertThat(clusterState.routingTable().shardsWithState(UNASSIGNED).size(), equalTo(2)); logger.info("start initializing"); - routingTable = strategy.applyStartedShards(clusterState, routingTable.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.routingTable().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(1)); - assertThat(routingTable.shardsWithState(INITIALIZING).size(), equalTo(0)); - assertThat(routingTable.shardsWithState(UNASSIGNED).size(), equalTo(2)); + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(1)); + assertThat(clusterState.routingTable().shardsWithState(INITIALIZING).size(), equalTo(0)); + assertThat(clusterState.routingTable().shardsWithState(UNASSIGNED).size(), equalTo(2)); logger.info("start one more node, first non-primary should start being allocated"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node2"))).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(1)); - assertThat(routingTable.shardsWithState(INITIALIZING).size(), equalTo(1)); - assertThat(routingTable.shardsWithState(UNASSIGNED).size(), equalTo(1)); + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(1)); + assertThat(clusterState.routingTable().shardsWithState(INITIALIZING).size(), equalTo(1)); + assertThat(clusterState.routingTable().shardsWithState(UNASSIGNED).size(), equalTo(1)); assertEquals(clusterState.getRoutingNodes().getOutgoingRecoveries("node1"), 1); logger.info("start initializing non-primary"); - routingTable = strategy.applyStartedShards(clusterState, routingTable.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(2)); - assertThat(routingTable.shardsWithState(INITIALIZING).size(), equalTo(0)); - assertThat(routingTable.shardsWithState(UNASSIGNED).size(), equalTo(1)); + routingResult = strategy.applyStartedShards(clusterState, clusterState.routingTable().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(2)); + assertThat(clusterState.routingTable().shardsWithState(INITIALIZING).size(), equalTo(0)); + assertThat(clusterState.routingTable().shardsWithState(UNASSIGNED).size(), equalTo(1)); assertEquals(clusterState.getRoutingNodes().getOutgoingRecoveries("node1"), 0); logger.info("start one more node, initializing second non-primary"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node3"))).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(2)); - assertThat(routingTable.shardsWithState(INITIALIZING).size(), equalTo(1)); - assertThat(routingTable.shardsWithState(UNASSIGNED).size(), equalTo(0)); + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(2)); + assertThat(clusterState.routingTable().shardsWithState(INITIALIZING).size(), equalTo(1)); + assertThat(clusterState.routingTable().shardsWithState(UNASSIGNED).size(), equalTo(0)); assertEquals(clusterState.getRoutingNodes().getOutgoingRecoveries("node1"), 1); logger.info("start one more node"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node4"))).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertEquals(clusterState.getRoutingNodes().getOutgoingRecoveries("node1"), 1); @@ -315,11 +315,10 @@ public class ThrottlingAllocationTests extends ESAllocationTestCase { // even though it is throttled, move command still forces allocation clusterState = ClusterState.builder(clusterState).routingResult(reroute).build(); - routingTable = clusterState.routingTable(); - assertThat(routingTable.shardsWithState(STARTED).size(), equalTo(1)); - assertThat(routingTable.shardsWithState(RELOCATING).size(), equalTo(1)); - assertThat(routingTable.shardsWithState(INITIALIZING).size(), equalTo(2)); - assertThat(routingTable.shardsWithState(UNASSIGNED).size(), equalTo(0)); + assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(1)); + assertThat(clusterState.routingTable().shardsWithState(RELOCATING).size(), equalTo(1)); + assertThat(clusterState.routingTable().shardsWithState(INITIALIZING).size(), equalTo(2)); + assertThat(clusterState.routingTable().shardsWithState(UNASSIGNED).size(), equalTo(0)); assertEquals(clusterState.getRoutingNodes().getOutgoingRecoveries("node1"), 2); assertEquals(clusterState.getRoutingNodes().getOutgoingRecoveries("node2"), 0); } diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/UpdateNumberOfReplicasTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/UpdateNumberOfReplicasTests.java index c80a413fd59..9486c4c3fcc 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/UpdateNumberOfReplicasTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/UpdateNumberOfReplicasTests.java @@ -54,127 +54,118 @@ public class UpdateNumberOfReplicasTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = 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(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); - assertThat(routingTable.index("test").shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(0).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(0).shards().get(0).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(0).shards().get(1).state(), equalTo(UNASSIGNED)); - assertThat(routingTable.index("test").shard(0).shards().get(0).currentNodeId(), nullValue()); - assertThat(routingTable.index("test").shard(0).shards().get(1).currentNodeId(), nullValue()); + assertThat(initialRoutingTable.index("test").shards().size(), equalTo(1)); + assertThat(initialRoutingTable.index("test").shard(0).size(), equalTo(2)); + assertThat(initialRoutingTable.index("test").shard(0).shards().size(), equalTo(2)); + assertThat(initialRoutingTable.index("test").shard(0).shards().get(0).state(), equalTo(UNASSIGNED)); + assertThat(initialRoutingTable.index("test").shard(0).shards().get(1).state(), equalTo(UNASSIGNED)); + assertThat(initialRoutingTable.index("test").shard(0).shards().get(0).currentNodeId(), nullValue()); + assertThat(initialRoutingTable.index("test").shard(0).shards().get(1).currentNodeId(), nullValue()); logger.info("Adding two nodes and performing rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build(); - RoutingTable prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("Start all the primary shards"); RoutingNodes routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("Start all the replica shards"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); - final String nodeHoldingPrimary = routingTable.index("test").shard(0).primaryShard().currentNodeId(); - final String nodeHoldingReplica = routingTable.index("test").shard(0).replicaShards().get(0).currentNodeId(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); + final String nodeHoldingPrimary = clusterState.routingTable().index("test").shard(0).primaryShard().currentNodeId(); + final String nodeHoldingReplica = clusterState.routingTable().index("test").shard(0).replicaShards().get(0).currentNodeId(); assertThat(nodeHoldingPrimary, not(equalTo(nodeHoldingReplica))); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(0).shards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(0).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(0).primaryShard().currentNodeId(), equalTo(nodeHoldingPrimary)); - assertThat(routingTable.index("test").shard(0).replicaShards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).replicaShards().get(0).state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(0).replicaShards().get(0).currentNodeId(), equalTo(nodeHoldingReplica)); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(0).shards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(0).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(0).primaryShard().currentNodeId(), equalTo(nodeHoldingPrimary)); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShards().get(0).state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShards().get(0).currentNodeId(), equalTo(nodeHoldingReplica)); logger.info("add another replica"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = RoutingTable.builder(routingTable).updateNumberOfReplicas(2).build(); + RoutingTable updatedRoutingTable = RoutingTable.builder(clusterState.routingTable()).updateNumberOfReplicas(2).build(); metaData = MetaData.builder(clusterState.metaData()).updateNumberOfReplicas(2).build(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).metaData(metaData).build(); + clusterState = ClusterState.builder(clusterState).routingTable(updatedRoutingTable).metaData(metaData).build(); assertThat(clusterState.metaData().index("test").getNumberOfReplicas(), equalTo(2)); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).size(), equalTo(3)); - assertThat(routingTable.index("test").shard(0).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(0).primaryShard().currentNodeId(), equalTo(nodeHoldingPrimary)); - assertThat(routingTable.index("test").shard(0).replicaShards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(0).replicaShards().get(0).state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(0).replicaShards().get(0).currentNodeId(), equalTo(nodeHoldingReplica)); - assertThat(routingTable.index("test").shard(0).replicaShards().get(1).state(), equalTo(UNASSIGNED)); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).size(), equalTo(3)); + assertThat(clusterState.routingTable().index("test").shard(0).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(0).primaryShard().currentNodeId(), equalTo(nodeHoldingPrimary)); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShards().get(0).state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShards().get(0).currentNodeId(), equalTo(nodeHoldingReplica)); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShards().get(1).state(), equalTo(UNASSIGNED)); logger.info("Add another node and start the added replica"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node3"))).build(); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).size(), equalTo(3)); - assertThat(routingTable.index("test").shard(0).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(0).primaryShard().currentNodeId(), equalTo(nodeHoldingPrimary)); - assertThat(routingTable.index("test").shard(0).replicaShards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(0).replicaShardsWithState(STARTED).size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).replicaShardsWithState(STARTED).get(0).currentNodeId(), equalTo(nodeHoldingReplica)); - assertThat(routingTable.index("test").shard(0).replicaShardsWithState(INITIALIZING).size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).replicaShardsWithState(INITIALIZING).get(0).currentNodeId(), equalTo("node3")); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).size(), equalTo(3)); + assertThat(clusterState.routingTable().index("test").shard(0).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(0).primaryShard().currentNodeId(), equalTo(nodeHoldingPrimary)); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShardsWithState(STARTED).size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShardsWithState(STARTED).get(0).currentNodeId(), equalTo(nodeHoldingReplica)); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShardsWithState(INITIALIZING).size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShardsWithState(INITIALIZING).get(0).currentNodeId(), equalTo("node3")); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).size(), equalTo(3)); - assertThat(routingTable.index("test").shard(0).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(0).primaryShard().currentNodeId(), equalTo(nodeHoldingPrimary)); - assertThat(routingTable.index("test").shard(0).replicaShards().size(), equalTo(2)); - assertThat(routingTable.index("test").shard(0).replicaShardsWithState(STARTED).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(0).replicaShardsWithState(STARTED).get(0).currentNodeId(), anyOf(equalTo(nodeHoldingReplica), equalTo("node3"))); - assertThat(routingTable.index("test").shard(0).replicaShardsWithState(STARTED).get(1).currentNodeId(), anyOf(equalTo(nodeHoldingReplica), equalTo("node3"))); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).size(), equalTo(3)); + assertThat(clusterState.routingTable().index("test").shard(0).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(0).primaryShard().currentNodeId(), equalTo(nodeHoldingPrimary)); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShards().size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShardsWithState(STARTED).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShardsWithState(STARTED).get(0).currentNodeId(), anyOf(equalTo(nodeHoldingReplica), equalTo("node3"))); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShardsWithState(STARTED).get(1).currentNodeId(), anyOf(equalTo(nodeHoldingReplica), equalTo("node3"))); logger.info("now remove a replica"); routingNodes = clusterState.getRoutingNodes(); - prevRoutingTable = routingTable; - routingTable = RoutingTable.builder(routingTable).updateNumberOfReplicas(1).build(); + updatedRoutingTable = RoutingTable.builder(clusterState.routingTable()).updateNumberOfReplicas(1).build(); metaData = MetaData.builder(clusterState.metaData()).updateNumberOfReplicas(1).build(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).metaData(metaData).build(); + clusterState = ClusterState.builder(clusterState).routingTable(updatedRoutingTable).metaData(metaData).build(); assertThat(clusterState.metaData().index("test").getNumberOfReplicas(), equalTo(1)); - assertThat(prevRoutingTable != routingTable, equalTo(true)); - assertThat(routingTable.index("test").shards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).size(), equalTo(2)); - assertThat(routingTable.index("test").shard(0).primaryShard().state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(0).primaryShard().currentNodeId(), equalTo(nodeHoldingPrimary)); - assertThat(routingTable.index("test").shard(0).replicaShards().size(), equalTo(1)); - assertThat(routingTable.index("test").shard(0).replicaShards().get(0).state(), equalTo(STARTED)); - assertThat(routingTable.index("test").shard(0).replicaShards().get(0).currentNodeId(), anyOf(equalTo(nodeHoldingReplica), equalTo("node3"))); + assertTrue(routingResult.changed()); + assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).size(), equalTo(2)); + assertThat(clusterState.routingTable().index("test").shard(0).primaryShard().state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(0).primaryShard().currentNodeId(), equalTo(nodeHoldingPrimary)); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShards().size(), equalTo(1)); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShards().get(0).state(), equalTo(STARTED)); + assertThat(clusterState.routingTable().index("test").shard(0).replicaShards().get(0).currentNodeId(), anyOf(equalTo(nodeHoldingReplica), equalTo("node3"))); logger.info("do a reroute, should remain the same"); - prevRoutingTable = routingTable; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - assertThat(prevRoutingTable != routingTable, equalTo(false)); + assertFalse(routingResult.changed()); } } diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/DiskThresholdDeciderTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/DiskThresholdDeciderTests.java index aedfe2bcace..904cc01ad2c 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/DiskThresholdDeciderTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/DiskThresholdDeciderTests.java @@ -120,29 +120,28 @@ public class DiskThresholdDeciderTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() + final RoutingTable initialRoutingTable = RoutingTable.builder() .addAsNew(metaData.index("test")) .build(); ClusterState clusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData) - .routingTable(routingTable).build(); + .routingTable(initialRoutingTable).build(); logger.info("--> adding two nodes"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder() .add(newNode("node1")) .add(newNode("node2")) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logShardStates(clusterState); // Primary shard should be initializing, replica should not assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(1)); logger.info("--> start the shards (primaries)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)) - .routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logShardStates(clusterState); // Assert that we're able to start the primary @@ -151,9 +150,8 @@ public class DiskThresholdDeciderTests extends ESAllocationTestCase { assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(0)); logger.info("--> start the shards (replicas)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)) - .routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logShardStates(clusterState); // Assert that the replica couldn't be started since node1 doesn't have enough space @@ -164,8 +162,8 @@ public class DiskThresholdDeciderTests extends ESAllocationTestCase { clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node3")) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logShardStates(clusterState); // Assert that the replica is initialized now that node3 is available with enough space @@ -173,9 +171,8 @@ public class DiskThresholdDeciderTests extends ESAllocationTestCase { assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.INITIALIZING).size(), equalTo(1)); logger.info("--> start the shards (replicas)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)) - .routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logShardStates(clusterState); // Assert that the replica couldn't be started since node1 doesn't have enough space @@ -205,8 +202,8 @@ public class DiskThresholdDeciderTests extends ESAllocationTestCase { .put("cluster.routing.allocation.cluster_concurrent_rebalance", -1) .build(), deciders, NoopGatewayAllocator.INSTANCE, new BalancedShardsAllocator(Settings.EMPTY), cis); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logShardStates(clusterState); // Shards remain started @@ -236,8 +233,8 @@ public class DiskThresholdDeciderTests extends ESAllocationTestCase { .put("cluster.routing.allocation.cluster_concurrent_rebalance", -1) .build(), deciders, NoopGatewayAllocator.INSTANCE, new BalancedShardsAllocator(Settings.EMPTY), cis); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logShardStates(clusterState); // Shards remain started @@ -252,8 +249,8 @@ public class DiskThresholdDeciderTests extends ESAllocationTestCase { clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node4")) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logShardStates(clusterState); // Shards remain started @@ -261,9 +258,8 @@ public class DiskThresholdDeciderTests extends ESAllocationTestCase { assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(1)); logger.info("--> apply INITIALIZING shards"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)) - .routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logShardStates(clusterState); assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(0)); @@ -321,12 +317,12 @@ public class DiskThresholdDeciderTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(2)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = RoutingTable.builder() .addAsNew(metaData.index("test")) .build(); ClusterState clusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData) - .routingTable(routingTable).build(); + .routingTable(initialRoutingTable).build(); logger.info("--> adding node1 and node2 node"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder() @@ -334,8 +330,8 @@ public class DiskThresholdDeciderTests extends ESAllocationTestCase { .add(newNode("node2")) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logShardStates(clusterState); // Primary should initialize, even though both nodes are over the limit initialize @@ -375,17 +371,16 @@ public class DiskThresholdDeciderTests extends ESAllocationTestCase { .put("cluster.routing.allocation.cluster_concurrent_rebalance", -1) .build(), deciders, NoopGatewayAllocator.INSTANCE, new BalancedShardsAllocator(Settings.EMPTY), cis); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logShardStates(clusterState); // Now the replica should be able to initialize assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(2)); logger.info("--> start the shards (primaries)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)) - .routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logShardStates(clusterState); // Assert that we're able to start the primary and replica, since they were both initializing @@ -403,8 +398,8 @@ public class DiskThresholdDeciderTests extends ESAllocationTestCase { clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node3")) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logShardStates(clusterState); // Assert that the replica is initialized now that node3 is available with enough space @@ -412,9 +407,8 @@ public class DiskThresholdDeciderTests extends ESAllocationTestCase { assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.INITIALIZING).size(), equalTo(1)); logger.info("--> start the shards (replicas)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)) - .routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logShardStates(clusterState); // Assert that all replicas could be started @@ -444,8 +438,8 @@ public class DiskThresholdDeciderTests extends ESAllocationTestCase { .put("cluster.routing.allocation.cluster_concurrent_rebalance", -1) .build(), deciders, NoopGatewayAllocator.INSTANCE, new BalancedShardsAllocator(Settings.EMPTY), cis); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logShardStates(clusterState); // Shards remain started @@ -475,8 +469,8 @@ public class DiskThresholdDeciderTests extends ESAllocationTestCase { .put("cluster.routing.allocation.cluster_concurrent_rebalance", -1) .build(), deciders, NoopGatewayAllocator.INSTANCE, new BalancedShardsAllocator(Settings.EMPTY), cis); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logShardStates(clusterState); // Shards remain started @@ -491,8 +485,8 @@ public class DiskThresholdDeciderTests extends ESAllocationTestCase { clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node4")) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logShardStates(clusterState); // Shards remain started @@ -502,9 +496,8 @@ public class DiskThresholdDeciderTests extends ESAllocationTestCase { assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(1)); logger.info("--> apply INITIALIZING shards"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)) - .routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logShardStates(clusterState); // primary shard already has been relocated away @@ -519,8 +512,8 @@ public class DiskThresholdDeciderTests extends ESAllocationTestCase { clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()) .add(newNode("node5")) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logShardStates(clusterState); // Shards remain started on node3 and node4 @@ -531,9 +524,8 @@ public class DiskThresholdDeciderTests extends ESAllocationTestCase { assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(1)); logger.info("--> apply INITIALIZING shards"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)) - .routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("--> final cluster state:"); logShardStates(clusterState); @@ -766,30 +758,29 @@ public class DiskThresholdDeciderTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test2").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = RoutingTable.builder() .addAsNew(metaData.index("test")) .addAsNew(metaData.index("test2")) .build(); ClusterState clusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData) - .routingTable(routingTable).build(); + .routingTable(initialRoutingTable).build(); logger.info("--> adding two nodes"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder() .add(newNode("node1")) .add(newNode("node2")) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logShardStates(clusterState); // shards should be initializing assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(4)); logger.info("--> start the shards"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)) - .routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logShardStates(clusterState); // Assert that we're able to start the primary and replicas @@ -803,8 +794,8 @@ public class DiskThresholdDeciderTests extends ESAllocationTestCase { AllocationCommand relocate1 = new MoveAllocationCommand("test", 0, "node2", "node3"); AllocationCommands cmds = new AllocationCommands(relocate1); - routingTable = strategy.reroute(clusterState, cmds, false, false).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, cmds, false, false); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logShardStates(clusterState); AllocationCommand relocate2 = new MoveAllocationCommand("test2", 0, "node2", "node3"); @@ -850,7 +841,7 @@ public class DiskThresholdDeciderTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(2).numberOfReplicas(0)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = RoutingTable.builder() .addAsNew(metaData.index("test")) .build(); @@ -862,7 +853,7 @@ public class DiskThresholdDeciderTests extends ESAllocationTestCase { ClusterState baseClusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)) .metaData(metaData) - .routingTable(routingTable) + .routingTable(initialRoutingTable) .nodes(discoveryNodes) .build(); @@ -965,7 +956,7 @@ public class DiskThresholdDeciderTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(2).numberOfReplicas(0)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = RoutingTable.builder() .addAsNew(metaData.index("test")) .build(); @@ -978,7 +969,7 @@ public class DiskThresholdDeciderTests extends ESAllocationTestCase { DiscoveryNodes discoveryNodes = DiscoveryNodes.builder().add(discoveryNode1).add(discoveryNode2).build(); ClusterState baseClusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)) .metaData(metaData) - .routingTable(routingTable) + .routingTable(initialRoutingTable) .nodes(discoveryNodes) .build(); diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/EnableAllocationTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/EnableAllocationTests.java index 076fe2ee5a5..0e0ba4cea0b 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/EnableAllocationTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/EnableAllocationTests.java @@ -28,6 +28,7 @@ import org.elasticsearch.cluster.node.DiscoveryNodes; import org.elasticsearch.cluster.routing.RoutingTable; import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.cluster.routing.allocation.AllocationService; +import org.elasticsearch.cluster.routing.allocation.RoutingAllocation; import org.elasticsearch.cluster.routing.allocation.decider.EnableAllocationDecider.Allocation; import org.elasticsearch.cluster.routing.allocation.decider.EnableAllocationDecider.Rebalance; import org.elasticsearch.common.logging.ESLogger; @@ -125,27 +126,27 @@ public class EnableAllocationTests extends ESAllocationTestCase { .put(IndexMetaData.builder("enabled").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = RoutingTable.builder() .addAsNew(metaData.index("disabled")) .addAsNew(metaData.index("enabled")) .build(); - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("--> adding two nodes and do rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder() .add(newNode("node1")) .add(newNode("node2")) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(1)); logger.info("--> start the shards (primaries)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("--> start the shards (replicas)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); logger.info("--> verify only enabled index has been routed"); assertThat(clusterState.getRoutingNodes().shardsWithState("enabled", STARTED).size(), equalTo(2)); @@ -170,29 +171,29 @@ public class EnableAllocationTests extends ESAllocationTestCase { .put(IndexMetaData.builder("always_disabled").settings(settings(Version.CURRENT).put(EnableAllocationDecider.INDEX_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), Rebalance.NONE)).numberOfShards(1).numberOfReplicas(1)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = RoutingTable.builder() .addAsNew(metaData.index("test")) .addAsNew(metaData.index("always_disabled")) .build(); - ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("--> adding one nodes and do rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder() .add(newNode("node1")) .add(newNode("node2")) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(4)); logger.info("--> start the shards (primaries)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(4)); assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(4)); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(8)); assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(0)); @@ -202,29 +203,26 @@ public class EnableAllocationTests extends ESAllocationTestCase { .add(newNode("node2")) .add(newNode("node3")) ).build(); - ClusterState prevState = clusterState; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(8)); assertThat(clusterState.getRoutingNodes().shardsWithState(RELOCATING).size(), equalTo(0)); if (useClusterSetting) { - prevState = clusterState; - clusterState = ClusterState.builder(clusterState).metaData(MetaData.builder(metaData).transientSettings(Settings.builder() + clusterState = ClusterState.builder(clusterState).metaData(MetaData.builder(clusterState.metaData()).transientSettings(Settings.builder() .put(CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), allowedOnes) .build())).build(); } else { - prevState = clusterState; IndexMetaData meta = clusterState.getMetaData().index("test"); IndexMetaData meta1 = clusterState.getMetaData().index("always_disabled"); - clusterState = ClusterState.builder(clusterState).metaData(MetaData.builder(metaData).removeAllIndices().put(IndexMetaData.builder(meta1)) + clusterState = ClusterState.builder(clusterState).metaData(MetaData.builder(clusterState.metaData()).removeAllIndices().put(IndexMetaData.builder(meta1)) .put(IndexMetaData.builder(meta).settings(Settings.builder().put(meta.getSettings()).put(EnableAllocationDecider.INDEX_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), allowedOnes).build()))) .build(); } clusterSettings.applySettings(clusterState.metaData().settings()); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat("expected 6 shards to be started 2 to relocate useClusterSettings: " + useClusterSetting, clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(6)); assertThat("expected 2 shards to relocate useClusterSettings: " + useClusterSetting, clusterState.getRoutingNodes().shardsWithState(RELOCATING).size(), equalTo(2)); List mutableShardRoutings = clusterState.getRoutingNodes().shardsWithState(RELOCATING); @@ -249,8 +247,8 @@ public class EnableAllocationTests extends ESAllocationTestCase { default: fail("only replicas, primaries or all are allowed"); } - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(8)); assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(0)); @@ -271,23 +269,23 @@ public class EnableAllocationTests extends ESAllocationTestCase { .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT).put(indexSettings)).numberOfShards(6).numberOfReplicas(0)) .build(); - RoutingTable routingTable = RoutingTable.builder() + RoutingTable initialRoutingTable = 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(); + ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build(); logger.info("--> adding one nodes and do rerouting"); clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder() .add(newNode("node1")) .add(newNode("node2")) ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(6)); logger.info("--> start the shards (primaries)"); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(6)); assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(0)); @@ -297,25 +295,23 @@ public class EnableAllocationTests extends ESAllocationTestCase { .add(newNode("node2")) .add(newNode("node3")) ).build(); - ClusterState prevState = clusterState; - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(6)); assertThat(clusterState.getRoutingNodes().shardsWithState(RELOCATING).size(), equalTo(0)); + metaData = clusterState.metaData(); if (useClusterSetting) { - prevState = clusterState; clusterState = ClusterState.builder(clusterState).metaData(MetaData.builder(metaData).transientSettings(Settings.builder() .put(CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), randomBoolean() ? Rebalance.PRIMARIES : Rebalance.ALL) .build())).build(); } else { - prevState = clusterState; IndexMetaData meta = clusterState.getMetaData().index("test"); clusterState = ClusterState.builder(clusterState).metaData(MetaData.builder(metaData).removeAllIndices() .put(IndexMetaData.builder(meta).settings(Settings.builder().put(meta.getSettings()).put(EnableAllocationDecider.INDEX_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), randomBoolean() ? Rebalance.PRIMARIES : Rebalance.ALL).build()))).build(); } clusterSettings.applySettings(clusterState.metaData().settings()); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); assertThat("expected 4 primaries to be started and 2 to relocate useClusterSettings: " + useClusterSetting, clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(4)); assertThat("expected 2 primaries to relocate useClusterSettings: " + useClusterSetting, clusterState.getRoutingNodes().shardsWithState(RELOCATING).size(), equalTo(2)); diff --git a/core/src/test/java/org/elasticsearch/cluster/structure/RoutingIteratorTests.java b/core/src/test/java/org/elasticsearch/cluster/structure/RoutingIteratorTests.java index 6a64fea9315..8ebf2c3eb94 100644 --- a/core/src/test/java/org/elasticsearch/cluster/structure/RoutingIteratorTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/structure/RoutingIteratorTests.java @@ -35,6 +35,7 @@ import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.cluster.routing.ShardShuffler; import org.elasticsearch.cluster.routing.ShardsIterator; import org.elasticsearch.cluster.routing.allocation.AllocationService; +import org.elasticsearch.cluster.routing.allocation.RoutingAllocation; import org.elasticsearch.cluster.routing.allocation.decider.ClusterRebalanceAllocationDecider; import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.Settings; @@ -250,14 +251,14 @@ public class RoutingIteratorTests extends ESAllocationTestCase { .add(newNode("node2", unmodifiableMap(node2Attributes))) .localNodeId("node1") ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); // after all are started, check routing iteration ShardIterator shardIterator = clusterState.routingTable().index("test").shard(0).preferAttributesActiveInitializingShardsIt(new String[]{"rack_id"}, clusterState.nodes()); @@ -299,11 +300,11 @@ public class RoutingIteratorTests extends ESAllocationTestCase { .localNodeId("node1") ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); ShardsIterator shardsIterator = clusterState.routingTable().index("test").shard(0).onlyNodeSelectorActiveInitializingShardsIt("disk:ebs",clusterState.nodes()); assertThat(shardsIterator.size(), equalTo(1)); @@ -372,14 +373,14 @@ public class RoutingIteratorTests extends ESAllocationTestCase { .add(newNode("node2")) .localNodeId("node1") ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); OperationRouting operationRouting = new OperationRouting(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)); @@ -446,11 +447,11 @@ public class RoutingIteratorTests extends ESAllocationTestCase { .add(newNode("node3")) .localNodeId("node1") ).build(); - routingTable = strategy.reroute(clusterState, "reroute").routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.reroute(clusterState, "reroute"); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); // When replicas haven't initialized, it comes back with the primary first, then initializing replicas GroupShardsIterator shardIterators = operationRouting.searchShards(clusterState, new String[]{"test"}, null, "_replica_first"); @@ -471,11 +472,11 @@ public class RoutingIteratorTests extends ESAllocationTestCase { assertFalse(routing.primary()); assertTrue(routing.initializing()); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); - routingTable = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)).routingTable(); - clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build(); + routingResult = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); shardIterators = operationRouting.searchShards(clusterState, new String[]{"test"}, null, "_replica"); diff --git a/test/framework/src/main/java/org/elasticsearch/cluster/ESAllocationTestCase.java b/test/framework/src/main/java/org/elasticsearch/cluster/ESAllocationTestCase.java index 9bd069e8c4b..79c50ce21c8 100644 --- a/test/framework/src/main/java/org/elasticsearch/cluster/ESAllocationTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/cluster/ESAllocationTestCase.java @@ -139,9 +139,9 @@ public abstract class ESAllocationTestCase extends ESTestCase { if (initializingShards.isEmpty()) { return clusterState; } - RoutingTable routingTable = strategy.applyStartedShards(clusterState, - arrayAsArrayList(initializingShards.get(randomInt(initializingShards.size() - 1)))).routingTable(); - return ClusterState.builder(clusterState).routingTable(routingTable).build(); + RoutingAllocation.Result routingResult = strategy.applyStartedShards(clusterState, + arrayAsArrayList(initializingShards.get(randomInt(initializingShards.size() - 1)))); + return ClusterState.builder(clusterState).routingResult(routingResult).build(); } protected static AllocationDeciders yesAllocationDeciders() { @@ -160,6 +160,16 @@ public abstract class ESAllocationTestCase extends ESTestCase { new SameShardAllocationDecider(Settings.EMPTY))); } + protected ClusterState applyStartedShardsUntilNoChange(ClusterState clusterState, AllocationService service) { + RoutingAllocation.Result routingResult; + do { + logger.debug("ClusterState: {}", clusterState.getRoutingNodes().prettyPrint()); + routingResult = service.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING)); + clusterState = ClusterState.builder(clusterState).routingResult(routingResult).build(); + } while (routingResult.changed()); + return clusterState; + } + public static class TestAllocateDecision extends AllocationDecider { private final Decision decision; From 743b2ed1b2c7166d30dd3dd1721120bd9738a172 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Fri, 19 Aug 2016 11:38:46 -0400 Subject: [PATCH 09/15] Set default min heap equal to default max heap This commit sets the default min heap equal to the default max heap. This is to align the default out-of-box settings with the heap size bootstrap check. --- distribution/build.gradle | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/distribution/build.gradle b/distribution/build.gradle index bfbf96b5d2a..5409cfd3bc6 100644 --- a/distribution/build.gradle +++ b/distribution/build.gradle @@ -479,6 +479,8 @@ task run(type: RunTask) { * */ Map expansionsForDistribution(distributionType) { + final String defaultHeapSize = "2g" + String footer = "# Built for ${project.name}-${project.version} " + "(${distributionType})" Map expansions = [ @@ -498,8 +500,8 @@ Map expansionsForDistribution(distributionType) { 'def': '', ], - 'heap.min': "256m", - 'heap.max': "2g", + 'heap.min': defaultHeapSize, + 'heap.max': defaultHeapSize, 'stopping.timeout': [ 'rpm': 86400, From 13e20e3320941f569efd18e9614bf673aa1b9dee Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Fri, 19 Aug 2016 12:55:48 -0400 Subject: [PATCH 10/15] Set external nodes to default to 512m of heap This commit sets external nodes for integration tests to default to using 512m of heap. This can be overridden using tests.heap.size (a system property that we already use elesewhere for setting the size of the heap for the test runner) or using tests.jvm.argline (this last one takes precedence). --- .../org/elasticsearch/gradle/test/ClusterConfiguration.groovy | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/ClusterConfiguration.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/ClusterConfiguration.groovy index d45741a50b8..6a2375efc62 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/ClusterConfiguration.groovy +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/ClusterConfiguration.groovy @@ -54,7 +54,9 @@ class ClusterConfiguration { boolean debug = false @Input - String jvmArgs = System.getProperty('tests.jvm.argline', '') + String jvmArgs = "-Xms" + System.getProperty('tests.heap.size', '512m') + + " " + "-Xmx" + System.getProperty('tests.heap.size', '512m') + + " " + System.getProperty('tests.jvm.argline', '') /** * The seed nodes port file. In the case the cluster has more than one node we use a seed node From 069fc2269664c5b2a4a393c893825f982b55ccc7 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Fri, 19 Aug 2016 14:21:17 -0400 Subject: [PATCH 11/15] Remove minimum master nodes bootstrap check This commit removes the minimum master nodes bootstrap check. The motivation for this check was to raise awareness of the minimum master nodes setting but this check gives a false sense of security because it's too easy to set the setting to one when first standing up a cluster and never update it when adding master-eligible nodes, or have it out of sync on various nodes and still pass this check. Since this check does not have the security that other bootstrap checks provide, it should be removed in favor of a stronger guarantee in the future. We do log a warning if an election occurs with minimum master nodes less than a quorum of master-eligible nodes that participated in an election and this is the best that we can do right now. Relates #20082 --- .../bootstrap/BootstrapCheck.java | 27 ------------------- .../bootstrap/BootstrapCheckTests.java | 9 ------- 2 files changed, 36 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/bootstrap/BootstrapCheck.java b/core/src/main/java/org/elasticsearch/bootstrap/BootstrapCheck.java index b32751d757d..94143f9986c 100644 --- a/core/src/main/java/org/elasticsearch/bootstrap/BootstrapCheck.java +++ b/core/src/main/java/org/elasticsearch/bootstrap/BootstrapCheck.java @@ -166,7 +166,6 @@ final class BootstrapCheck { if (Constants.LINUX || Constants.MAC_OS_X) { checks.add(new MaxSizeVirtualMemoryCheck()); } - checks.add(new MinMasterNodesCheck(ElectMasterService.DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING.exists(settings))); if (Constants.LINUX) { checks.add(new MaxMapCountCheck()); } @@ -330,32 +329,6 @@ final class BootstrapCheck { } - static class MinMasterNodesCheck implements Check { - - final boolean minMasterNodesIsSet; - - MinMasterNodesCheck(boolean minMasterNodesIsSet) { - this.minMasterNodesIsSet = minMasterNodesIsSet; - } - - @Override - public boolean check() { - return minMasterNodesIsSet == false; - } - - @Override - public String errorMessage() { - return "please set [" + ElectMasterService.DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING.getKey() + - "] to a majority of the number of master eligible nodes in your cluster"; - } - - @Override - public final boolean isSystemCheck() { - return false; - } - - } - static class MaxNumberOfThreadsCheck implements Check { private final long maxNumberOfThreadsThreshold = 1 << 11; diff --git a/core/src/test/java/org/elasticsearch/bootstrap/BootstrapCheckTests.java b/core/src/test/java/org/elasticsearch/bootstrap/BootstrapCheckTests.java index 1c4cd5b4e87..cbbe05f6e74 100644 --- a/core/src/test/java/org/elasticsearch/bootstrap/BootstrapCheckTests.java +++ b/core/src/test/java/org/elasticsearch/bootstrap/BootstrapCheckTests.java @@ -385,15 +385,6 @@ public class BootstrapCheckTests extends ESTestCase { BootstrapCheck.check(true, false, Collections.singletonList(check), "testMaxMapCountCheck"); } - public void testMinMasterNodes() { - boolean isSet = randomBoolean(); - BootstrapCheck.Check check = new BootstrapCheck.MinMasterNodesCheck(isSet); - assertThat(check.check(), not(equalTo(isSet))); - List defaultChecks = BootstrapCheck.checks(Settings.EMPTY); - - expectThrows(RuntimeException.class, () -> BootstrapCheck.check(true, false, defaultChecks, "testMinMasterNodes")); - } - public void testClientJvmCheck() { final AtomicReference vmName = new AtomicReference<>("Java HotSpot(TM) 32-Bit Client VM"); final BootstrapCheck.Check check = new BootstrapCheck.ClientJvmCheck() { From cf32f8de346eb3101b9bd0f57aec62fcdc4638d3 Mon Sep 17 00:00:00 2001 From: Ali Beyad Date: Fri, 19 Aug 2016 14:40:00 -0400 Subject: [PATCH 12/15] Fixes tests so allocation ids in IndexMetaData is in sync with what is in the RoutingTable --- .../elasticsearch/cluster/routing/RoutingTableTests.java | 5 +++-- .../discovery/zen/NodeJoinControllerTests.java | 7 +++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/cluster/routing/RoutingTableTests.java b/core/src/test/java/org/elasticsearch/cluster/routing/RoutingTableTests.java index a60796447ef..239653a8d22 100644 --- a/core/src/test/java/org/elasticsearch/cluster/routing/RoutingTableTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/routing/RoutingTableTests.java @@ -332,11 +332,12 @@ public class RoutingTableTests extends ESAllocationTestCase { expectThrows(IllegalStateException.class, () -> indexRoutingTable.validate(metaData4)); } - private IndexMetaData updateActiveAllocations(IndexRoutingTable indexRoutingTable, IndexMetaData indexMetaData) { + public static IndexMetaData updateActiveAllocations(IndexRoutingTable indexRoutingTable, IndexMetaData indexMetaData) { IndexMetaData.Builder imdBuilder = IndexMetaData.builder(indexMetaData); for (IndexShardRoutingTable shardTable : indexRoutingTable) { for (ShardRouting shardRouting : shardTable) { - Set activeAllocations = shardTable.activeShards().stream().map(shr -> shr.allocationId().getId()).collect(Collectors.toSet()); + Set activeAllocations = shardTable.activeShards().stream().map( + shr -> shr.allocationId().getId()).collect(Collectors.toSet()); imdBuilder.putActiveAllocationIds(shardRouting.id(), activeAllocations); } } diff --git a/core/src/test/java/org/elasticsearch/discovery/zen/NodeJoinControllerTests.java b/core/src/test/java/org/elasticsearch/discovery/zen/NodeJoinControllerTests.java index 30fde9b974c..cbb27ff97ac 100644 --- a/core/src/test/java/org/elasticsearch/discovery/zen/NodeJoinControllerTests.java +++ b/core/src/test/java/org/elasticsearch/discovery/zen/NodeJoinControllerTests.java @@ -80,6 +80,7 @@ import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_CREATION_ import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS; import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS; import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_VERSION_CREATED; +import static org.elasticsearch.cluster.routing.RoutingTableTests.updateActiveAllocations; import static org.elasticsearch.test.ClusterServiceUtils.createClusterService; import static org.elasticsearch.test.ClusterServiceUtils.setState; import static org.elasticsearch.cluster.ESAllocationTestCase.createAllocationService; @@ -596,7 +597,6 @@ public class NodeJoinControllerTests extends ESTestCase { .put(SETTING_VERSION_CREATED, Version.CURRENT) .put(SETTING_NUMBER_OF_SHARDS, 1).put(SETTING_NUMBER_OF_REPLICAS, 1) .put(SETTING_CREATION_DATE, System.currentTimeMillis())).build(); - stateBuilder.metaData(MetaData.builder().put(indexMetaData, false).generateClusterUuidIfNeeded()); IndexRoutingTable.Builder indexRoutingTableBuilder = IndexRoutingTable.builder(indexMetaData.getIndex()); RoutingTable.Builder routing = new RoutingTable.Builder(); routing.addAsNew(indexMetaData); @@ -619,7 +619,10 @@ public class NodeJoinControllerTests extends ESTestCase { ShardRoutingState.UNASSIGNED, new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "life sucks"))); } indexRoutingTableBuilder.addIndexShard(indexShardRoutingBuilder.build()); - stateBuilder.routingTable(RoutingTable.builder().add(indexRoutingTableBuilder.build()).build()); + IndexRoutingTable indexRoutingTable = indexRoutingTableBuilder.build(); + IndexMetaData updatedIndexMetaData = updateActiveAllocations(indexRoutingTable, indexMetaData); + stateBuilder.metaData(MetaData.builder().put(updatedIndexMetaData, false).generateClusterUuidIfNeeded()) + .routingTable(RoutingTable.builder().add(indexRoutingTable).build()); } setState(clusterService, stateBuilder.build()); From 3a6f7eb07ad8da9736e79c570305cc24b1e29251 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Fri, 19 Aug 2016 14:51:54 -0400 Subject: [PATCH 13/15] Rename StartupError to StartupException This commit renames StartupError to StartupException. This rename is due to the fact that this class inherits from Exception not Error in the Throwable class hierarchy. --- .../elasticsearch/bootstrap/Bootstrap.java | 2 +- .../bootstrap/Elasticsearch.java | 3 +-- ...tartupError.java => StartupException.java} | 22 +++++++++---------- 3 files changed, 13 insertions(+), 14 deletions(-) rename core/src/main/java/org/elasticsearch/bootstrap/{StartupError.java => StartupException.java} (94%) diff --git a/core/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java b/core/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java index 7fa6245d1ec..e2c2e3dce76 100644 --- a/core/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java +++ b/core/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java @@ -273,7 +273,7 @@ final class Bootstrap { // guice: log the shortened exc to the log file ByteArrayOutputStream os = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(os, false, "UTF-8"); - new StartupError(e).printStackTrace(ps); + new StartupException(e).printStackTrace(ps); ps.flush(); logger.error("Guice Exception: {}", os.toString("UTF-8")); } else { diff --git a/core/src/main/java/org/elasticsearch/bootstrap/Elasticsearch.java b/core/src/main/java/org/elasticsearch/bootstrap/Elasticsearch.java index 9c76fdfb030..de6a0339cb0 100644 --- a/core/src/main/java/org/elasticsearch/bootstrap/Elasticsearch.java +++ b/core/src/main/java/org/elasticsearch/bootstrap/Elasticsearch.java @@ -23,7 +23,6 @@ import joptsimple.OptionSet; import joptsimple.OptionSpec; import joptsimple.OptionSpecBuilder; import joptsimple.util.PathConverter; -import joptsimple.util.PathProperties; import org.elasticsearch.Build; import org.elasticsearch.cli.ExitCodes; import org.elasticsearch.cli.SettingCommand; @@ -102,7 +101,7 @@ class Elasticsearch extends SettingCommand { } catch (final Throwable t) { // format exceptions to the console in a special way // to avoid 2MB stacktraces from guice, etc. - throw new StartupError(t); + throw new StartupException(t); } } diff --git a/core/src/main/java/org/elasticsearch/bootstrap/StartupError.java b/core/src/main/java/org/elasticsearch/bootstrap/StartupException.java similarity index 94% rename from core/src/main/java/org/elasticsearch/bootstrap/StartupError.java rename to core/src/main/java/org/elasticsearch/bootstrap/StartupException.java index 781aac31f65..e7646e6ac02 100644 --- a/core/src/main/java/org/elasticsearch/bootstrap/StartupError.java +++ b/core/src/main/java/org/elasticsearch/bootstrap/StartupException.java @@ -32,18 +32,18 @@ import java.io.PrintStream; */ //TODO: remove this when guice is removed, and exceptions are cleaned up //this is horrible, but its what we must do -final class StartupError extends RuntimeException { - +final class StartupException extends RuntimeException { + /** maximum length of a stacktrace, before we truncate it */ static final int STACKTRACE_LIMIT = 30; /** all lines from this package are RLE-compressed */ static final String GUICE_PACKAGE = "org.elasticsearch.common.inject"; - - /** - * Create a new StartupError that will format {@code cause} + + /** + * Create a new StartupException that will format {@code cause} * to the console on failure. */ - StartupError(Throwable cause) { + StartupException(Throwable cause) { super(cause); } @@ -58,10 +58,10 @@ final class StartupError extends RuntimeException { if (cause instanceof CreationException) { cause = getFirstGuiceCause((CreationException)cause); } - + String message = cause.toString(); s.println(message); - + if (cause != null) { // walk to the root cause while (cause.getCause() != null) { @@ -82,7 +82,7 @@ final class StartupError extends RuntimeException { break; } String line = stack[i].toString(); - + // skip past contiguous runs of this garbage: if (line.startsWith(GUICE_PACKAGE)) { while (i + 1 < stack.length && stack[i + 1].toString().startsWith(GUICE_PACKAGE)) { @@ -103,8 +103,8 @@ final class StartupError extends RuntimeException { s.println("Refer to the log for complete error details."); } } - - /** + + /** * Returns first cause from a guice error (it can have multiple). */ static Throwable getFirstGuiceCause(CreationException guice) { From c3849d9e7d6b0fe157b881a1e1cce29f6efe8c3f Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Fri, 19 Aug 2016 15:10:54 -0400 Subject: [PATCH 14/15] Add print stack trace override to StartupException StartupException overrides Throwable#printStackTrace(PrintStream) but not Throwable#printStackTrace(PrintWriter). The former override is used when the JVM terminates with an exception, but the latter override can be used in some logging frameworks when rendering an exception (e.g., log4j). This commit adds an override for the latter, with the behavior for the two overrides being the same. --- .../bootstrap/StartupException.java | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/bootstrap/StartupException.java b/core/src/main/java/org/elasticsearch/bootstrap/StartupException.java index e7646e6ac02..a78f82ef3e7 100644 --- a/core/src/main/java/org/elasticsearch/bootstrap/StartupException.java +++ b/core/src/main/java/org/elasticsearch/bootstrap/StartupException.java @@ -23,6 +23,8 @@ import org.elasticsearch.common.inject.CreationException; import org.elasticsearch.common.inject.spi.Message; import java.io.PrintStream; +import java.io.PrintWriter; +import java.util.function.Consumer; /** * Wraps an exception in a special way that it gets formatted @@ -53,6 +55,15 @@ final class StartupException extends RuntimeException { */ @Override public void printStackTrace(PrintStream s) { + printStackTrace(s::println); + } + + @Override + public void printStackTrace(PrintWriter s) { + printStackTrace(s::println); + } + + private void printStackTrace(Consumer consumer) { Throwable originalCause = getCause(); Throwable cause = originalCause; if (cause instanceof CreationException) { @@ -60,7 +71,7 @@ final class StartupException extends RuntimeException { } String message = cause.toString(); - s.println(message); + consumer.accept(message); if (cause != null) { // walk to the root cause @@ -70,7 +81,7 @@ final class StartupException extends RuntimeException { // print the root cause message, only if it differs! if (cause != originalCause && (message.equals(cause.toString()) == false)) { - s.println("Likely root cause: " + cause); + consumer.accept("Likely root cause: " + cause); } // print stacktrace of cause @@ -78,7 +89,7 @@ final class StartupException extends RuntimeException { int linesWritten = 0; for (int i = 0; i < stack.length; i++) { if (linesWritten == STACKTRACE_LIMIT) { - s.println("\t<<>>"); + consumer.accept("\t<<>>"); break; } String line = stack[i].toString(); @@ -88,19 +99,19 @@ final class StartupException extends RuntimeException { while (i + 1 < stack.length && stack[i + 1].toString().startsWith(GUICE_PACKAGE)) { i++; } - s.println("\tat <<>>"); + consumer.accept("\tat <<>>"); linesWritten++; continue; } - s.println("\tat " + line.toString()); + consumer.accept("\tat " + line.toString()); linesWritten++; } } // if its a guice exception, the whole thing really will not be in the log, its megabytes. // refer to the hack in bootstrap, where we don't log it if (originalCause instanceof CreationException == false) { - s.println("Refer to the log for complete error details."); + consumer.accept("Refer to the log for complete error details."); } } @@ -116,4 +127,5 @@ final class StartupException extends RuntimeException { } return guice; // we tried } + } From 1c9b64e09a779cc402c459297bcaab4679044b20 Mon Sep 17 00:00:00 2001 From: Ali Beyad Date: Fri, 19 Aug 2016 16:19:56 -0400 Subject: [PATCH 15/15] Adds ignoreUnavailable option to the snapshot status API (#20066) Adds ignoreUnavailable to the snapshot status API to be consistent with the get snapshots API which has a similar parameter. If ignoreUnavailable is set to true, then the snapshot status request will ignore any snapshots that were not found in the repository, instead of throwing a SnapshotMissingException. Closes #18522 --- .../status/SnapshotsStatusRequest.java | 25 ++++++++ .../status/SnapshotsStatusRequestBuilder.java | 12 ++++ .../TransportSnapshotsStatusAction.java | 9 ++- .../cluster/RestSnapshotsStatusAction.java | 8 +-- .../snapshots/SnapshotMissingException.java | 6 +- .../SharedClusterSnapshotRestoreIT.java | 24 ++++++-- .../rest-api-spec/api/snapshot.get.json | 4 ++ .../rest-api-spec/api/snapshot.status.json | 6 +- .../test/snapshot.get/10_basic.yaml | 61 +++++++++++++++++++ .../test/snapshot.status/10_basic.yaml | 61 +++++++++++++++++++ 10 files changed, 201 insertions(+), 15 deletions(-) create mode 100644 rest-api-spec/src/main/resources/rest-api-spec/test/snapshot.get/10_basic.yaml create mode 100644 rest-api-spec/src/main/resources/rest-api-spec/test/snapshot.status/10_basic.yaml diff --git a/core/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotsStatusRequest.java b/core/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotsStatusRequest.java index 015134aba72..cc6ca276721 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotsStatusRequest.java +++ b/core/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotsStatusRequest.java @@ -38,6 +38,8 @@ public class SnapshotsStatusRequest extends MasterNodeRequesttrue to ignore unavailable snapshots, instead of throwing an exception. + * Defaults to false, which means unavailable snapshots cause an exception to be thrown. + * + * @param ignoreUnavailable whether to ignore unavailable snapshots + * @return this request + */ + public SnapshotsStatusRequest ignoreUnavailable(boolean ignoreUnavailable) { + this.ignoreUnavailable = ignoreUnavailable; + return this; + } + + /** + * Returns whether the request permits unavailable snapshots to be ignored. + * + * @return true if the request will ignore unavailable snapshots, false if it will throw an exception on unavailable snapshots + */ + public boolean ignoreUnavailable() { + return ignoreUnavailable; + } + @Override public void readFrom(StreamInput in) throws IOException { super.readFrom(in); repository = in.readString(); snapshots = in.readStringArray(); + ignoreUnavailable = in.readBoolean(); } @Override @@ -124,5 +148,6 @@ public class SnapshotsStatusRequest extends MasterNodeRequesttrue to ignore unavailable snapshots, instead of throwing an exception. + * Defaults to false, which means unavailable snapshots cause an exception to be thrown. + * + * @param ignoreUnavailable whether to ignore unavailable snapshots. + * @return this builder + */ + public SnapshotsStatusRequestBuilder setIgnoreUnavailable(boolean ignoreUnavailable) { + request.ignoreUnavailable(ignoreUnavailable); + return this; + } } diff --git a/core/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/TransportSnapshotsStatusAction.java b/core/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/TransportSnapshotsStatusAction.java index 76fe9510ef5..cf00784dc3f 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/TransportSnapshotsStatusAction.java +++ b/core/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/TransportSnapshotsStatusAction.java @@ -214,7 +214,14 @@ public class TransportSnapshotsStatusAction extends TransportMasterNodeAction shardStatusBuilder = new ArrayList<>(); diff --git a/core/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestSnapshotsStatusAction.java b/core/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestSnapshotsStatusAction.java index bd2fd54420f..4333dfc0271 100644 --- a/core/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestSnapshotsStatusAction.java +++ b/core/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestSnapshotsStatusAction.java @@ -20,7 +20,6 @@ package org.elasticsearch.rest.action.admin.cluster; import org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotsStatusRequest; -import org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotsStatusResponse; import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.Strings; import org.elasticsearch.common.inject.Inject; @@ -54,9 +53,10 @@ public class RestSnapshotsStatusAction extends BaseRestHandler { if (snapshots.length == 1 && "_all".equalsIgnoreCase(snapshots[0])) { snapshots = Strings.EMPTY_ARRAY; } - SnapshotsStatusRequest snapshotsStatusResponse = snapshotsStatusRequest(repository).snapshots(snapshots); + SnapshotsStatusRequest snapshotsStatusRequest = snapshotsStatusRequest(repository).snapshots(snapshots); + snapshotsStatusRequest.ignoreUnavailable(request.paramAsBoolean("ignore_unavailable", snapshotsStatusRequest.ignoreUnavailable())); - snapshotsStatusResponse.masterNodeTimeout(request.paramAsTime("master_timeout", snapshotsStatusResponse.masterNodeTimeout())); - client.admin().cluster().snapshotsStatus(snapshotsStatusResponse, new RestToXContentListener(channel)); + snapshotsStatusRequest.masterNodeTimeout(request.paramAsTime("master_timeout", snapshotsStatusRequest.masterNodeTimeout())); + client.admin().cluster().snapshotsStatus(snapshotsStatusRequest, new RestToXContentListener<>(channel)); } } diff --git a/core/src/main/java/org/elasticsearch/snapshots/SnapshotMissingException.java b/core/src/main/java/org/elasticsearch/snapshots/SnapshotMissingException.java index 5f0979e38d8..b7f2c6af4a1 100644 --- a/core/src/main/java/org/elasticsearch/snapshots/SnapshotMissingException.java +++ b/core/src/main/java/org/elasticsearch/snapshots/SnapshotMissingException.java @@ -30,15 +30,15 @@ import java.io.IOException; public class SnapshotMissingException extends SnapshotException { public SnapshotMissingException(final String repositoryName, final SnapshotId snapshotId, final Throwable cause) { - super(repositoryName, snapshotId, "is missing", cause); + super(repositoryName, snapshotId, " is missing", cause); } public SnapshotMissingException(final String repositoryName, final SnapshotId snapshotId) { - super(repositoryName, snapshotId, "is missing"); + super(repositoryName, snapshotId, " is missing"); } public SnapshotMissingException(final String repositoryName, final String snapshotName) { - super(repositoryName, snapshotName, "is missing"); + super(repositoryName, snapshotName, " is missing"); } public SnapshotMissingException(StreamInput in) throws IOException { diff --git a/core/src/test/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java b/core/src/test/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java index 54a0539192e..3e43cc83045 100644 --- a/core/src/test/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java +++ b/core/src/test/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java @@ -1504,12 +1504,24 @@ public class SharedClusterSnapshotRestoreIT extends AbstractSnapshotIntegTestCas logger.info("--> checking that _current no longer returns the snapshot"); assertThat(client.admin().cluster().prepareGetSnapshots("test-repo").addSnapshots("_current").execute().actionGet().getSnapshots().isEmpty(), equalTo(true)); - try { - client.admin().cluster().prepareSnapshotStatus("test-repo").addSnapshots("test-snap-doesnt-exist").execute().actionGet(); - fail(); - } catch (SnapshotMissingException ex) { - // Expected - } + // test that getting an unavailable snapshot status throws an exception if ignoreUnavailable is false on the request + SnapshotMissingException ex = expectThrows(SnapshotMissingException.class, () -> + client.admin().cluster().prepareSnapshotStatus("test-repo").addSnapshots("test-snap-doesnt-exist").get()); + assertEquals("[test-repo:test-snap-doesnt-exist] is missing", ex.getMessage()); + // test that getting an unavailable snapshot status does not throw an exception if ignoreUnavailable is true on the request + response = client.admin().cluster().prepareSnapshotStatus("test-repo") + .addSnapshots("test-snap-doesnt-exist") + .setIgnoreUnavailable(true) + .get(); + assertTrue(response.getSnapshots().isEmpty()); + // test getting snapshot status for available and unavailable snapshots where ignoreUnavailable is true + // (available one should be returned) + response = client.admin().cluster().prepareSnapshotStatus("test-repo") + .addSnapshots("test-snap", "test-snap-doesnt-exist") + .setIgnoreUnavailable(true) + .get(); + assertEquals(1, response.getSnapshots().size()); + assertEquals("test-snap", response.getSnapshots().get(0).getSnapshot().getSnapshotId().getName()); } public void testSnapshotRelocatingPrimary() throws Exception { diff --git a/rest-api-spec/src/main/resources/rest-api-spec/api/snapshot.get.json b/rest-api-spec/src/main/resources/rest-api-spec/api/snapshot.get.json index 65a8deace7a..760809cdf9b 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/api/snapshot.get.json +++ b/rest-api-spec/src/main/resources/rest-api-spec/api/snapshot.get.json @@ -21,6 +21,10 @@ "master_timeout": { "type" : "time", "description" : "Explicit operation timeout for connection to master node" + }, + "ignore_unavailable": { + "type": "boolean", + "description": "Whether to ignore unavailable snapshots, defaults to false which means a SnapshotMissingException is thrown" } } }, diff --git a/rest-api-spec/src/main/resources/rest-api-spec/api/snapshot.status.json b/rest-api-spec/src/main/resources/rest-api-spec/api/snapshot.status.json index ebc9180cb52..cba488de793 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/api/snapshot.status.json +++ b/rest-api-spec/src/main/resources/rest-api-spec/api/snapshot.status.json @@ -19,9 +19,13 @@ "master_timeout": { "type" : "time", "description" : "Explicit operation timeout for connection to master node" + }, + "ignore_unavailable": { + "type": "boolean", + "description": "Whether to ignore unavailable snapshots, defaults to false which means a SnapshotMissingException is thrown" } } }, "body": null } -} \ No newline at end of file +} diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/snapshot.get/10_basic.yaml b/rest-api-spec/src/main/resources/rest-api-spec/test/snapshot.get/10_basic.yaml new file mode 100644 index 00000000000..bd609e3e3bf --- /dev/null +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/snapshot.get/10_basic.yaml @@ -0,0 +1,61 @@ +--- +setup: + + - do: + snapshot.create_repository: + repository: test_repo_get_1 + body: + type: fs + settings: + location: "test_repo_get_1_loc" + +--- +teardown: + + - do: + snapshot.delete_repository: + repository: test_repo_get_1 + +--- +"Get snapshot info": + + - do: + indices.create: + index: test_index + body: + settings: + number_of_shards: 1 + number_of_replicas: 0 + + - do: + snapshot.create: + repository: test_repo_get_1 + snapshot: test_snapshot + wait_for_completion: true + + - do: + snapshot.get: + repository: test_repo_get_1 + snapshot: test_snapshot + + - is_true: snapshots + +--- +"Get missing snapshot info throws an exception": + + - do: + catch: /snapshot_missing_exception.+ is missing/ + snapshot.get: + repository: test_repo_get_1 + snapshot: test_nonexistent_snapshot + +--- +"Get missing snapshot info succeeds when ignoreUnavailable is true": + + - do: + snapshot.get: + repository: test_repo_get_1 + snapshot: test_nonexistent_snapshot + ignore_unavailable: true + + - is_true: snapshots diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/snapshot.status/10_basic.yaml b/rest-api-spec/src/main/resources/rest-api-spec/test/snapshot.status/10_basic.yaml new file mode 100644 index 00000000000..d4548553e25 --- /dev/null +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/snapshot.status/10_basic.yaml @@ -0,0 +1,61 @@ +--- +setup: + + - do: + snapshot.create_repository: + repository: test_repo_status_1 + body: + type: fs + settings: + location: "test_repo_status_1_loc" + +--- +teardown: + + - do: + snapshot.delete_repository: + repository: test_repo_status_1 + +--- +"Get snapshot status": + + - do: + indices.create: + index: test_index + body: + settings: + number_of_shards: 1 + number_of_replicas: 0 + + - do: + snapshot.create: + repository: test_repo_status_1 + snapshot: test_snapshot + wait_for_completion: true + + - do: + snapshot.status: + repository: test_repo_status_1 + snapshot: test_snapshot + + - is_true: snapshots + +--- +"Get missing snapshot status throws an exception": + + - do: + catch: /snapshot_missing_exception.+ is missing/ + snapshot.status: + repository: test_repo_status_1 + snapshot: test_nonexistent_snapshot + +--- +"Get missing snapshot status succeeds when ignoreUnavailable is true": + + - do: + snapshot.status: + repository: test_repo_status_1 + snapshot: test_nonexistent_snapshot + ignore_unavailable: true + + - is_true: snapshots