/* * Licensed to Elasticsearch under one or more contributor * license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright * ownership. Elasticsearch licenses this file to you under * the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.elasticsearch.action.admin.cluster.allocation; import org.elasticsearch.action.support.ActiveShardCount; import org.elasticsearch.cluster.ClusterInfo; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.cluster.routing.ShardRoutingState; import org.elasticsearch.cluster.routing.UnassignedInfo; import org.elasticsearch.cluster.routing.UnassignedInfo.AllocationStatus; import org.elasticsearch.cluster.routing.UnassignedInfo.Reason; import org.elasticsearch.cluster.routing.allocation.AllocateUnassignedDecision; import org.elasticsearch.cluster.routing.allocation.AllocationDecision; import org.elasticsearch.cluster.routing.allocation.MoveDecision; import org.elasticsearch.cluster.routing.allocation.NodeAllocationResult; import org.elasticsearch.cluster.routing.allocation.decider.Decision; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.set.Sets; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser.Token; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.InternalTestCluster; import org.elasticsearch.test.junit.annotations.TestLogging; import java.io.IOException; import java.util.*; import static org.hamcrest.Matchers.*; /** * Tests for the cluster allocation explanation */ @ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0) public final class ClusterBugIT extends ESIntegTestCase { //@TestLogging("org.elasticsearch:DEBUG") public void testBug() throws Exception { List nodes = internalCluster().startNodes(6); Set excludeNodes = new HashSet<>(nodes.subList(0, 3)); Set includeNodes = new HashSet<>(nodes.subList(3, 6)); logger.info("--> exclude: [{}], include: [{}]", Strings.collectionToCommaDelimitedString(excludeNodes), Strings.collectionToCommaDelimitedString(includeNodes)); ensureStableCluster(6); client().admin().indices().prepareCreate("test").get(); ensureGreen("test"); Settings exclude = Settings.builder().put("cluster.routing.allocation.exclude._name", Strings.collectionToCommaDelimitedString(excludeNodes)).build(); logger.info("--> updating settings"); client().admin().cluster().prepareUpdateSettings().setTransientSettings(exclude).get(); logger.info("--> waiting for relocation"); waitForRelocation(ClusterHealthStatus.GREEN); ClusterState state = client().admin().cluster().prepareState().get().getState(); for (ShardRouting shard : state.getRoutingTable().shardsWithState(ShardRoutingState.STARTED)) { String node = state.getRoutingNodes().node(shard.currentNodeId()).node().getName(); logger.info("--> shard on {} - {}", node, shard); assertTrue("shard on " + node + " but should only be on the include node list: " + Strings.collectionToCommaDelimitedString(includeNodes), includeNodes.contains(node)); } Settings other = Settings.builder().put("cluster.info.update.interval", "45s").build(); logger.info("--> updating settings with random persistent setting"); client().admin().cluster().prepareUpdateSettings() .setPersistentSettings(other).setTransientSettings(exclude).get(); logger.info("--> waiting for relocation"); waitForRelocation(ClusterHealthStatus.GREEN); state = client().admin().cluster().prepareState().get().getState(); // The transient settings still exist in the state assertThat(state.metaData().transientSettings(), equalTo(exclude)); for (ShardRouting shard : state.getRoutingTable().shardsWithState(ShardRoutingState.STARTED)) { String node = state.getRoutingNodes().node(shard.currentNodeId()).node().getName(); logger.info("--> shard on {} - {}", node, shard); assertTrue("shard on " + node + " but should only be on the include node list: " + Strings.collectionToCommaDelimitedString(includeNodes), includeNodes.contains(node)); } } }