Search in sources :

Example 41 with RoutingNode

use of org.elasticsearch.cluster.routing.RoutingNode in project elasticsearch by elastic.

the class DecisionsImpactOnClusterHealthTests method testPrimaryShardYesDecisionOnIndexCreation.

public void testPrimaryShardYesDecisionOnIndexCreation() throws IOException {
    final String indexName = "test-idx";
    Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toAbsolutePath().toString()).build();
    AllocationDecider decider = new TestAllocateDecision(Decision.YES) {

        @Override
        public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
            if (node.getByShardId(shardRouting.shardId()) == null) {
                return Decision.YES;
            } else {
                return Decision.NO;
            }
        }
    };
    // if deciders say YES to allocating primary shards, stay in YELLOW state
    ClusterState clusterState = runAllocationTest(settings, indexName, Collections.singleton(decider), ClusterHealthStatus.YELLOW);
    // make sure primaries are initialized
    RoutingTable routingTable = clusterState.routingTable();
    for (IndexShardRoutingTable indexShardRoutingTable : routingTable.index(indexName)) {
        assertTrue(indexShardRoutingTable.primaryShard().initializing());
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) Settings(org.elasticsearch.common.settings.Settings) AllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.AllocationDecider)

Example 42 with RoutingNode

use of org.elasticsearch.cluster.routing.RoutingNode in project elasticsearch by elastic.

the class DecisionsImpactOnClusterHealthTests method testPrimaryShardThrottleDecisionOnIndexCreation.

public void testPrimaryShardThrottleDecisionOnIndexCreation() throws IOException {
    final String indexName = "test-idx";
    Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toAbsolutePath().toString()).build();
    AllocationDecider decider = new TestAllocateDecision(Decision.THROTTLE) {

        // the only allocation decider that implements this is ShardsLimitAllocationDecider and it always
        // returns only YES or NO, never THROTTLE
        @Override
        public Decision canAllocate(RoutingNode node, RoutingAllocation allocation) {
            return randomBoolean() ? Decision.YES : Decision.NO;
        }
    };
    // if deciders THROTTLE allocating a primary shard, stay in YELLOW state
    runAllocationTest(settings, indexName, Collections.singleton(decider), ClusterHealthStatus.YELLOW);
}
Also used : RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) Settings(org.elasticsearch.common.settings.Settings) AllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.AllocationDecider)

Example 43 with RoutingNode

use of org.elasticsearch.cluster.routing.RoutingNode in project elasticsearch by elastic.

the class FailedNodeRoutingTests method testSimpleFailedNodeTest.

public void testSimpleFailedNodeTest() {
    AllocationService strategy = createAllocationService(Settings.builder().put(ClusterRebalanceAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ALLOW_REBALANCE_SETTING.getKey(), ClusterRebalanceAllocationDecider.ClusterRebalanceType.ALWAYS.toString()).build());
    MetaData metaData = MetaData.builder().put(IndexMetaData.builder("test1").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1)).put(IndexMetaData.builder("test2").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1)).build();
    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(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();
    clusterState = strategy.reroute(clusterState, "reroute");
    logger.info("start all the primary shards, replicas will start initializing");
    RoutingNodes routingNodes = clusterState.getRoutingNodes();
    clusterState = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING));
    routingNodes = clusterState.getRoutingNodes();
    logger.info("start the replica shards");
    routingNodes = clusterState.getRoutingNodes();
    clusterState = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING));
    routingNodes = clusterState.getRoutingNodes();
    assertThat(routingNodes.node("node1").numberOfShardsWithState(STARTED), equalTo(1));
    assertThat(routingNodes.node("node2").numberOfShardsWithState(STARTED), equalTo(1));
    assertThat(routingNodes.node("node3").numberOfShardsWithState(STARTED), equalTo(1));
    assertThat(routingNodes.node("node4").numberOfShardsWithState(STARTED), equalTo(1));
    logger.info("remove 2 nodes where primaries are allocated, reroute");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).remove(clusterState.routingTable().index("test1").shard(0).primaryShard().currentNodeId()).remove(clusterState.routingTable().index("test2").shard(0).primaryShard().currentNodeId())).build();
    clusterState = strategy.deassociateDeadNodes(clusterState, true, "reroute");
    routingNodes = clusterState.getRoutingNodes();
    for (RoutingNode routingNode : routingNodes) {
        assertThat(routingNode.numberOfShardsWithState(STARTED), equalTo(1));
        assertThat(routingNode.numberOfShardsWithState(INITIALIZING), equalTo(1));
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 44 with RoutingNode

use of org.elasticsearch.cluster.routing.RoutingNode in project elasticsearch by elastic.

the class ReplicaShardAllocatorTests method testThrottleWhenAllocatingToMatchingNode.

/**
     * Tests when the node to allocate to due to matching is being throttled, we move the shard to ignored
     * to wait till throttling on it is done.
     */
public void testThrottleWhenAllocatingToMatchingNode() {
    RoutingAllocation allocation = onePrimaryOnNode1And1Replica(new AllocationDeciders(Settings.EMPTY, Arrays.asList(new TestAllocateDecision(Decision.YES), new SameShardAllocationDecider(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)), new AllocationDecider(Settings.EMPTY) {

        @Override
        public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
            if (node.node().equals(node2)) {
                return Decision.THROTTLE;
            }
            return Decision.YES;
        }
    })));
    testAllocator.addData(node1, "MATCH", new StoreFileMetaData("file1", 10, "MATCH_CHECKSUM")).addData(node2, "MATCH", new StoreFileMetaData("file1", 10, "MATCH_CHECKSUM"));
    testAllocator.allocateUnassigned(allocation);
    assertThat(allocation.routingNodes().unassigned().ignored().size(), equalTo(1));
    assertThat(allocation.routingNodes().unassigned().ignored().get(0).shardId(), equalTo(shardId));
}
Also used : SameShardAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.SameShardAllocationDecider) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) StoreFileMetaData(org.elasticsearch.index.store.StoreFileMetaData) AllocationDeciders(org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders) RoutingAllocation(org.elasticsearch.cluster.routing.allocation.RoutingAllocation) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) SameShardAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.SameShardAllocationDecider) AllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.AllocationDecider) Decision(org.elasticsearch.cluster.routing.allocation.decider.Decision)

Example 45 with RoutingNode

use of org.elasticsearch.cluster.routing.RoutingNode in project elasticsearch by elastic.

the class IndexWithShadowReplicasIT method testShadowReplicaNaturalRelocation.

/**
     * Tests that shadow replicas can be "naturally" rebalanced and relocated
     * around the cluster. By "naturally" I mean without using the reroute API
     */
// This test failed on CI when trying to assert that all the shard data has been deleted
// from the index path. It has not been reproduced locally. Despite the IndicesService
// deleting the index and hence, deleting all the shard data for the index, the test
// failure still showed some Lucene files in the data directory for that index. Not sure
// why that is, so turning on more logging here.
@TestLogging("org.elasticsearch.indices:TRACE,org.elasticsearch.env:TRACE,_root:DEBUG")
public void testShadowReplicaNaturalRelocation() throws Exception {
    Path dataPath = createTempDir();
    Settings nodeSettings = nodeSettings(dataPath);
    final List<String> nodes = internalCluster().startNodes(2, nodeSettings);
    String IDX = "test";
    Settings idxSettings = Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 5).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1).put(IndexMetaData.SETTING_DATA_PATH, dataPath.toAbsolutePath().toString()).put(IndexMetaData.SETTING_SHADOW_REPLICAS, true).put(IndexMetaData.SETTING_SHARED_FILESYSTEM, true).build();
    prepareCreate(IDX).setSettings(idxSettings).addMapping("doc", "foo", "type=text").get();
    ensureGreen(IDX);
    int docCount = randomIntBetween(10, 100);
    List<IndexRequestBuilder> builders = new ArrayList<>();
    for (int i = 0; i < docCount; i++) {
        builders.add(client().prepareIndex(IDX, "doc", i + "").setSource("foo", "bar"));
    }
    indexRandom(true, true, true, builders);
    flushAndRefresh(IDX);
    // start a third node, with 5 shards each on the other nodes, they
    // should relocate some to the third node
    final String node3 = internalCluster().startNode(nodeSettings);
    nodes.add(node3);
    assertBusy(new Runnable() {

        @Override
        public void run() {
            client().admin().cluster().prepareHealth().setWaitForNodes("3").get();
            ClusterStateResponse resp = client().admin().cluster().prepareState().get();
            RoutingNodes nodes = resp.getState().getRoutingNodes();
            for (RoutingNode node : nodes) {
                logger.info("--> node has {} shards (needs at least 2)", node.numberOfOwningShards());
                assertThat("at least 2 shards on node", node.numberOfOwningShards(), greaterThanOrEqualTo(2));
            }
        }
    });
    ensureYellow(IDX);
    logger.info("--> performing query");
    SearchResponse resp = client().prepareSearch(IDX).setQuery(matchAllQuery()).get();
    assertHitCount(resp, docCount);
    assertAcked(client().admin().indices().prepareDelete(IDX));
    assertAllIndicesRemovedAndDeletionCompleted(internalCluster().getInstances(IndicesService.class));
    assertPathHasBeenCleared(dataPath);
//TODO: uncomment the test below when https://github.com/elastic/elasticsearch/issues/17695 is resolved.
//assertIndicesDirsDeleted(nodes);
}
Also used : Path(java.nio.file.Path) RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) IndicesService(org.elasticsearch.indices.IndicesService) SearchResponse(org.elasticsearch.action.search.SearchResponse) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) Settings(org.elasticsearch.common.settings.Settings) TestLogging(org.elasticsearch.test.junit.annotations.TestLogging)

Aggregations

RoutingNode (org.elasticsearch.cluster.routing.RoutingNode)61 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)45 ClusterState (org.elasticsearch.cluster.ClusterState)28 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)23 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)20 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)16 RoutingNodes (org.elasticsearch.cluster.routing.RoutingNodes)15 MetaData (org.elasticsearch.cluster.metadata.MetaData)13 Settings (org.elasticsearch.common.settings.Settings)12 Decision (org.elasticsearch.cluster.routing.allocation.decider.Decision)10 ShardId (org.elasticsearch.index.shard.ShardId)10 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)9 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)9 Matchers.containsString (org.hamcrest.Matchers.containsString)9 UnassignedInfo (org.elasticsearch.cluster.routing.UnassignedInfo)8 RoutingAllocation (org.elasticsearch.cluster.routing.allocation.RoutingAllocation)8 ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)8 IndexShard (org.elasticsearch.index.shard.IndexShard)8 ArrayList (java.util.ArrayList)7 HashSet (java.util.HashSet)7