Search in sources :

Example 51 with RoutingNode

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

the class IndicesStoreIntegrationIT method testShardActiveElseWhere.

public void testShardActiveElseWhere() throws Exception {
    List<String> nodes = internalCluster().startNodes(2);
    final String masterNode = internalCluster().getMasterName();
    final String nonMasterNode = nodes.get(0).equals(masterNode) ? nodes.get(1) : nodes.get(0);
    final String masterId = internalCluster().clusterService(masterNode).localNode().getId();
    final String nonMasterId = internalCluster().clusterService(nonMasterNode).localNode().getId();
    final int numShards = scaledRandomIntBetween(2, 10);
    assertAcked(prepareCreate("test").setSettings(Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, numShards)));
    ensureGreen("test");
    waitNoPendingTasksOnAll();
    ClusterStateResponse stateResponse = client().admin().cluster().prepareState().get();
    final Index index = stateResponse.getState().metaData().index("test").getIndex();
    RoutingNode routingNode = stateResponse.getState().getRoutingNodes().node(nonMasterId);
    final int[] node2Shards = new int[routingNode.numberOfOwningShards()];
    int i = 0;
    for (ShardRouting shardRouting : routingNode) {
        node2Shards[i] = shardRouting.shardId().id();
        i++;
    }
    logger.info("Node [{}] has shards: {}", nonMasterNode, Arrays.toString(node2Shards));
    // disable relocations when we do this, to make sure the shards are not relocated from node2
    // due to rebalancing, and delete its content
    client().admin().cluster().prepareUpdateSettings().setTransientSettings(Settings.builder().put(EnableAllocationDecider.CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), EnableAllocationDecider.Rebalance.NONE)).get();
    internalCluster().getInstance(ClusterService.class, nonMasterNode).submitStateUpdateTask("test", new LocalClusterUpdateTask(Priority.IMMEDIATE) {

        @Override
        public ClusterTasksResult<LocalClusterUpdateTask> execute(ClusterState currentState) throws Exception {
            IndexRoutingTable.Builder indexRoutingTableBuilder = IndexRoutingTable.builder(index);
            for (int i = 0; i < numShards; i++) {
                indexRoutingTableBuilder.addIndexShard(new IndexShardRoutingTable.Builder(new ShardId(index, i)).addShard(TestShardRouting.newShardRouting("test", i, masterId, true, ShardRoutingState.STARTED)).build());
            }
            return newState(ClusterState.builder(currentState).routingTable(RoutingTable.builder().add(indexRoutingTableBuilder).build()).build());
        }

        @Override
        public void onFailure(String source, Exception e) {
        }
    });
    waitNoPendingTasksOnAll();
    logger.info("Checking if shards aren't removed");
    for (int shard : node2Shards) {
        assertTrue(waitForShardDeletion(nonMasterNode, index, shard));
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) LocalClusterUpdateTask(org.elasticsearch.cluster.LocalClusterUpdateTask) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) Index(org.elasticsearch.index.Index) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) IOException(java.io.IOException) ShardId(org.elasticsearch.index.shard.ShardId) ClusterService(org.elasticsearch.cluster.service.ClusterService) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 52 with RoutingNode

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

the class RandomAllocationDeciderTests method testRandomDecisions.

/* This test will make random allocation decision on a growing and shrinking
     * cluster leading to a random distribution of the shards. After a certain
     * amount of iterations the test allows allocation unless the same shard is
     * already allocated on a node and balances the cluster to gain optimal
     * balance.*/
public void testRandomDecisions() {
    RandomAllocationDecider randomAllocationDecider = new RandomAllocationDecider(random());
    AllocationService strategy = new AllocationService(Settings.builder().build(), new AllocationDeciders(Settings.EMPTY, new HashSet<>(Arrays.asList(new SameShardAllocationDecider(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)), new ReplicaAfterPrimaryActiveAllocationDecider(Settings.EMPTY), randomAllocationDecider))), new TestGatewayAllocator(), new BalancedShardsAllocator(Settings.EMPTY), EmptyClusterInfoService.INSTANCE);
    int indices = scaledRandomIntBetween(1, 20);
    Builder metaBuilder = MetaData.builder();
    int maxNumReplicas = 1;
    int totalNumShards = 0;
    for (int i = 0; i < indices; i++) {
        int replicas = scaledRandomIntBetween(0, 6);
        maxNumReplicas = Math.max(maxNumReplicas, replicas + 1);
        int numShards = scaledRandomIntBetween(1, 20);
        totalNumShards += numShards * (replicas + 1);
        metaBuilder.put(IndexMetaData.builder("INDEX_" + i).settings(settings(Version.CURRENT)).numberOfShards(numShards).numberOfReplicas(replicas));
    }
    MetaData metaData = metaBuilder.build();
    RoutingTable.Builder routingTableBuilder = RoutingTable.builder();
    for (int i = 0; i < indices; i++) {
        routingTableBuilder.addAsNew(metaData.index("INDEX_" + i));
    }
    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();
    AllocationService.CommandsResult routingResult;
    for (int i = 0; i < numIters; i++) {
        logger.info("Start iteration [{}]", i);
        ClusterState.Builder stateBuilder = ClusterState.builder(clusterState);
        DiscoveryNodes.Builder newNodesBuilder = DiscoveryNodes.builder(clusterState.nodes());
        if (clusterState.nodes().getSize() <= atMostNodes && (nodeIdCounter == 0 || (frequentNodes ? frequently() : rarely()))) {
            int numNodes = scaledRandomIntBetween(1, 3);
            for (int j = 0; j < numNodes; j++) {
                logger.info("adding node [{}]", nodeIdCounter);
                newNodesBuilder.add(newNode("NODE_" + (nodeIdCounter++)));
            }
        }
        boolean nodesRemoved = false;
        if (nodeIdCounter > 1 && rarely()) {
            int nodeId = scaledRandomIntBetween(0, nodeIdCounter - 2);
            final String node = "NODE_" + nodeId;
            boolean safeToRemove = true;
            RoutingNode routingNode = clusterState.getRoutingNodes().node(node);
            for (ShardRouting shard : routingNode != null ? routingNode : Collections.<ShardRouting>emptyList()) {
                if (shard.active() && shard.primary()) {
                    // make sure there is an active replica to prevent from going red
                    if (clusterState.routingTable().shardRoutingTable(shard.shardId()).activeShards().size() <= 1) {
                        safeToRemove = false;
                        break;
                    }
                }
            }
            if (safeToRemove) {
                logger.info("removing node [{}]", nodeId);
                newNodesBuilder.remove(node);
                nodesRemoved = true;
            } else {
                logger.debug("not removing node [{}] as it holds a primary with no replacement", nodeId);
            }
        }
        stateBuilder.nodes(newNodesBuilder.build());
        clusterState = stateBuilder.build();
        if (nodesRemoved) {
            clusterState = strategy.deassociateDeadNodes(clusterState, true, "reroute");
        } else {
            clusterState = strategy.reroute(clusterState, "reroute");
        }
        if (clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size() > 0) {
            clusterState = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
        }
    }
    logger.info("Fill up nodes such that every shard can be allocated");
    if (clusterState.nodes().getSize() < maxNumReplicas) {
        ClusterState.Builder stateBuilder = ClusterState.builder(clusterState);
        DiscoveryNodes.Builder newNodesBuilder = DiscoveryNodes.builder(clusterState.nodes());
        for (int j = 0; j < (maxNumReplicas - clusterState.nodes().getSize()); j++) {
            logger.info("adding node [{}]", nodeIdCounter);
            newNodesBuilder.add(newNode("NODE_" + (nodeIdCounter++)));
        }
        stateBuilder.nodes(newNodesBuilder.build());
        clusterState = stateBuilder.build();
    }
    randomAllocationDecider.alwaysSayYes = true;
    logger.info("now say YES to everything");
    int iterations = 0;
    do {
        iterations++;
        clusterState = strategy.reroute(clusterState, "reroute");
        if (clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size() > 0) {
            clusterState = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
        }
    } while (clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.INITIALIZING).size() != 0 || clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.UNASSIGNED).size() != 0 && iterations < 200);
    logger.info("Done Balancing after [{}] iterations. State:\n{}", iterations, clusterState);
    // we stop after 200 iterations if it didn't stabelize by then something is likely to be wrong
    assertThat("max num iteration exceeded", iterations, Matchers.lessThan(200));
    assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.INITIALIZING).size(), equalTo(0));
    assertThat(clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.UNASSIGNED).size(), equalTo(0));
    int shards = clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED).size();
    assertThat(shards, equalTo(totalNumShards));
    final int numNodes = clusterState.nodes().getSize();
    final int upperBound = (int) Math.round(((shards / numNodes) * 1.10));
    final int lowerBound = (int) Math.round(((shards / numNodes) * 0.90));
    for (int i = 0; i < nodeIdCounter; i++) {
        if (clusterState.getRoutingNodes().node("NODE_" + i) == null) {
            continue;
        }
        assertThat(clusterState.getRoutingNodes().node("NODE_" + i).size(), Matchers.anyOf(Matchers.anyOf(equalTo((shards / numNodes) + 1), equalTo((shards / numNodes) - 1), equalTo((shards / numNodes))), Matchers.allOf(Matchers.greaterThanOrEqualTo(lowerBound), Matchers.lessThanOrEqualTo(upperBound))));
    }
}
Also used : TestGatewayAllocator(org.elasticsearch.test.gateway.TestGatewayAllocator) ClusterState(org.elasticsearch.cluster.ClusterState) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) BalancedShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) Builder(org.elasticsearch.cluster.metadata.MetaData.Builder) AllocationDeciders(org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders) ReplicaAfterPrimaryActiveAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.ReplicaAfterPrimaryActiveAllocationDecider) SameShardAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.SameShardAllocationDecider) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) HashSet(java.util.HashSet)

Example 53 with RoutingNode

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

the class DiskThresholdDeciderUnitTests method testShardSizeAndRelocatingSize.

public void testShardSizeAndRelocatingSize() {
    ImmutableOpenMap.Builder<String, Long> shardSizes = ImmutableOpenMap.builder();
    shardSizes.put("[test][0][r]", 10L);
    shardSizes.put("[test][1][r]", 100L);
    shardSizes.put("[test][2][r]", 1000L);
    shardSizes.put("[other][0][p]", 10000L);
    ClusterInfo info = new DevNullClusterInfo(ImmutableOpenMap.of(), ImmutableOpenMap.of(), shardSizes.build());
    MetaData.Builder metaBuilder = MetaData.builder();
    metaBuilder.put(IndexMetaData.builder("test").settings(settings(Version.CURRENT).put("index.uuid", "1234")).numberOfShards(3).numberOfReplicas(1));
    metaBuilder.put(IndexMetaData.builder("other").settings(settings(Version.CURRENT).put("index.uuid", "5678")).numberOfShards(1).numberOfReplicas(1));
    MetaData metaData = metaBuilder.build();
    RoutingTable.Builder routingTableBuilder = RoutingTable.builder();
    routingTableBuilder.addAsNew(metaData.index("test"));
    routingTableBuilder.addAsNew(metaData.index("other"));
    ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTableBuilder.build()).build();
    RoutingAllocation allocation = new RoutingAllocation(null, null, clusterState, info, 0, false);
    final Index index = new Index("test", "1234");
    ShardRouting test_0 = ShardRouting.newUnassigned(new ShardId(index, 0), false, PeerRecoverySource.INSTANCE, new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "foo"));
    test_0 = ShardRoutingHelper.initialize(test_0, "node1");
    test_0 = ShardRoutingHelper.moveToStarted(test_0);
    test_0 = ShardRoutingHelper.relocate(test_0, "node2");
    ShardRouting test_1 = ShardRouting.newUnassigned(new ShardId(index, 1), false, PeerRecoverySource.INSTANCE, new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "foo"));
    test_1 = ShardRoutingHelper.initialize(test_1, "node2");
    test_1 = ShardRoutingHelper.moveToStarted(test_1);
    test_1 = ShardRoutingHelper.relocate(test_1, "node1");
    ShardRouting test_2 = ShardRouting.newUnassigned(new ShardId(index, 2), false, PeerRecoverySource.INSTANCE, new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "foo"));
    test_2 = ShardRoutingHelper.initialize(test_2, "node1");
    test_2 = ShardRoutingHelper.moveToStarted(test_2);
    assertEquals(1000L, DiskThresholdDecider.getExpectedShardSize(test_2, allocation, 0));
    assertEquals(100L, DiskThresholdDecider.getExpectedShardSize(test_1, allocation, 0));
    assertEquals(10L, DiskThresholdDecider.getExpectedShardSize(test_0, allocation, 0));
    RoutingNode node = new RoutingNode("node1", new DiscoveryNode("node1", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT), test_0, test_1.getTargetRelocatingShard(), test_2);
    assertEquals(100L, DiskThresholdDecider.sizeOfRelocatingShards(node, allocation, false, "/dev/null"));
    assertEquals(90L, DiskThresholdDecider.sizeOfRelocatingShards(node, allocation, true, "/dev/null"));
    assertEquals(0L, DiskThresholdDecider.sizeOfRelocatingShards(node, allocation, true, "/dev/some/other/dev"));
    assertEquals(0L, DiskThresholdDecider.sizeOfRelocatingShards(node, allocation, true, "/dev/some/other/dev"));
    ShardRouting test_3 = ShardRouting.newUnassigned(new ShardId(index, 3), false, PeerRecoverySource.INSTANCE, new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "foo"));
    test_3 = ShardRoutingHelper.initialize(test_3, "node1");
    test_3 = ShardRoutingHelper.moveToStarted(test_3);
    assertEquals(0L, DiskThresholdDecider.getExpectedShardSize(test_3, allocation, 0));
    ShardRouting other_0 = ShardRouting.newUnassigned(new ShardId("other", "5678", 0), randomBoolean(), PeerRecoverySource.INSTANCE, new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "foo"));
    other_0 = ShardRoutingHelper.initialize(other_0, "node2");
    other_0 = ShardRoutingHelper.moveToStarted(other_0);
    other_0 = ShardRoutingHelper.relocate(other_0, "node1");
    node = new RoutingNode("node1", new DiscoveryNode("node1", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT), test_0, test_1.getTargetRelocatingShard(), test_2, other_0.getTargetRelocatingShard());
    if (other_0.primary()) {
        assertEquals(10100L, DiskThresholdDecider.sizeOfRelocatingShards(node, allocation, false, "/dev/null"));
        assertEquals(10090L, DiskThresholdDecider.sizeOfRelocatingShards(node, allocation, true, "/dev/null"));
    } else {
        assertEquals(100L, DiskThresholdDecider.sizeOfRelocatingShards(node, allocation, false, "/dev/null"));
        assertEquals(90L, DiskThresholdDecider.sizeOfRelocatingShards(node, allocation, true, "/dev/null"));
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) DevNullClusterInfo(org.elasticsearch.cluster.MockInternalClusterInfoService.DevNullClusterInfo) Index(org.elasticsearch.index.Index) Matchers.containsString(org.hamcrest.Matchers.containsString) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) ShardId(org.elasticsearch.index.shard.ShardId) DevNullClusterInfo(org.elasticsearch.cluster.MockInternalClusterInfoService.DevNullClusterInfo) ClusterInfo(org.elasticsearch.cluster.ClusterInfo) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) RoutingAllocation(org.elasticsearch.cluster.routing.allocation.RoutingAllocation) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 54 with RoutingNode

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

the class DiskThresholdDeciderUnitTests method testCanAllocateUsesMaxAvailableSpace.

public void testCanAllocateUsesMaxAvailableSpace() {
    ClusterSettings nss = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
    DiskThresholdDecider decider = new DiskThresholdDecider(Settings.EMPTY, nss);
    MetaData metaData = MetaData.builder().put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1)).build();
    final Index index = metaData.index("test").getIndex();
    ShardRouting test_0 = ShardRouting.newUnassigned(new ShardId(index, 0), true, StoreRecoverySource.EMPTY_STORE_INSTANCE, new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "foo"));
    DiscoveryNode node_0 = new DiscoveryNode("node_0", buildNewFakeTransportAddress(), Collections.emptyMap(), new HashSet<>(Arrays.asList(DiscoveryNode.Role.values())), Version.CURRENT);
    DiscoveryNode node_1 = new DiscoveryNode("node_1", buildNewFakeTransportAddress(), Collections.emptyMap(), new HashSet<>(Arrays.asList(DiscoveryNode.Role.values())), Version.CURRENT);
    RoutingTable routingTable = 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.builder(clusterState).nodes(DiscoveryNodes.builder().add(node_0).add(node_1)).build();
    // actual test -- after all that bloat :)
    ImmutableOpenMap.Builder<String, DiskUsage> leastAvailableUsages = ImmutableOpenMap.builder();
    // all full
    leastAvailableUsages.put("node_0", new DiskUsage("node_0", "node_0", "_na_", 100, 0));
    // all full
    leastAvailableUsages.put("node_1", new DiskUsage("node_1", "node_1", "_na_", 100, 0));
    ImmutableOpenMap.Builder<String, DiskUsage> mostAvailableUsage = ImmutableOpenMap.builder();
    // 20 - 99 percent since after allocation there must be at least 10% left and shard is 10byte
    mostAvailableUsage.put("node_0", new DiskUsage("node_0", "node_0", "_na_", 100, randomIntBetween(20, 100)));
    // this is weird and smells like a bug! it should be up to 20%?
    mostAvailableUsage.put("node_1", new DiskUsage("node_1", "node_1", "_na_", 100, randomIntBetween(0, 10)));
    ImmutableOpenMap.Builder<String, Long> shardSizes = ImmutableOpenMap.builder();
    // 10 bytes
    shardSizes.put("[test][0][p]", 10L);
    final ClusterInfo clusterInfo = new ClusterInfo(leastAvailableUsages.build(), mostAvailableUsage.build(), shardSizes.build(), ImmutableOpenMap.of());
    RoutingAllocation allocation = new RoutingAllocation(new AllocationDeciders(Settings.EMPTY, Collections.singleton(decider)), clusterState.getRoutingNodes(), clusterState, clusterInfo, System.nanoTime(), false);
    allocation.debugDecision(true);
    Decision decision = decider.canAllocate(test_0, new RoutingNode("node_0", node_0), allocation);
    assertEquals(mostAvailableUsage.toString(), Decision.Type.YES, decision.type());
    assertThat(((Decision.Single) decision).getExplanation(), containsString("enough disk for shard on node"));
    decision = decider.canAllocate(test_0, new RoutingNode("node_1", node_1), allocation);
    assertEquals(mostAvailableUsage.toString(), Decision.Type.NO, decision.type());
    assertThat(((Decision.Single) decision).getExplanation(), containsString("the node is above the high watermark cluster setting [cluster.routing.allocation.disk.watermark.high=90%], using more " + "disk space than the maximum allowed [90.0%]"));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) Index(org.elasticsearch.index.Index) Matchers.containsString(org.hamcrest.Matchers.containsString) DiskUsage(org.elasticsearch.cluster.DiskUsage) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) ShardId(org.elasticsearch.index.shard.ShardId) DevNullClusterInfo(org.elasticsearch.cluster.MockInternalClusterInfoService.DevNullClusterInfo) ClusterInfo(org.elasticsearch.cluster.ClusterInfo) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) RoutingAllocation(org.elasticsearch.cluster.routing.allocation.RoutingAllocation)

Example 55 with RoutingNode

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

the class MockDiskUsagesIT method testRerouteOccursOnDiskPassingHighWatermark.

public void testRerouteOccursOnDiskPassingHighWatermark() throws Exception {
    List<String> nodes = internalCluster().startNodes(3);
    // Wait for all 3 nodes to be up
    assertBusy(new Runnable() {

        @Override
        public void run() {
            NodesStatsResponse resp = client().admin().cluster().prepareNodesStats().get();
            assertThat(resp.getNodes().size(), equalTo(3));
        }
    });
    // Start with all nodes at 50% usage
    final MockInternalClusterInfoService cis = (MockInternalClusterInfoService) internalCluster().getInstance(ClusterInfoService.class, internalCluster().getMasterName());
    cis.setUpdateFrequency(TimeValue.timeValueMillis(200));
    cis.onMaster();
    cis.setN1Usage(nodes.get(0), new DiskUsage(nodes.get(0), "n1", "/dev/null", 100, 50));
    cis.setN2Usage(nodes.get(1), new DiskUsage(nodes.get(1), "n2", "/dev/null", 100, 50));
    cis.setN3Usage(nodes.get(2), new DiskUsage(nodes.get(2), "n3", "/dev/null", 100, 50));
    client().admin().cluster().prepareUpdateSettings().setTransientSettings(Settings.builder().put(DiskThresholdSettings.CLUSTER_ROUTING_ALLOCATION_LOW_DISK_WATERMARK_SETTING.getKey(), randomFrom("20b", "80%")).put(DiskThresholdSettings.CLUSTER_ROUTING_ALLOCATION_HIGH_DISK_WATERMARK_SETTING.getKey(), randomFrom("10b", "90%")).put(DiskThresholdSettings.CLUSTER_ROUTING_ALLOCATION_REROUTE_INTERVAL_SETTING.getKey(), "1ms")).get();
    // Create an index with 10 shards so we can check allocation for it
    prepareCreate("test").setSettings(Settings.builder().put("number_of_shards", 10).put("number_of_replicas", 0).put("index.routing.allocation.exclude._name", "")).get();
    ensureGreen("test");
    // Block until the "fake" cluster info is retrieved at least once
    assertBusy(new Runnable() {

        @Override
        public void run() {
            ClusterInfo info = cis.getClusterInfo();
            logger.info("--> got: {} nodes", info.getNodeLeastAvailableDiskUsages().size());
            assertThat(info.getNodeLeastAvailableDiskUsages().size(), greaterThan(0));
        }
    });
    final List<String> realNodeNames = new ArrayList<>();
    ClusterStateResponse resp = client().admin().cluster().prepareState().get();
    Iterator<RoutingNode> iter = resp.getState().getRoutingNodes().iterator();
    while (iter.hasNext()) {
        RoutingNode node = iter.next();
        realNodeNames.add(node.nodeId());
        logger.info("--> node {} has {} shards", node.nodeId(), resp.getState().getRoutingNodes().node(node.nodeId()).numberOfOwningShards());
    }
    // Update the disk usages so one node has now passed the high watermark
    cis.setN1Usage(realNodeNames.get(0), new DiskUsage(nodes.get(0), "n1", "_na_", 100, 50));
    cis.setN2Usage(realNodeNames.get(1), new DiskUsage(nodes.get(1), "n2", "_na_", 100, 50));
    // nothing free on node3
    cis.setN3Usage(realNodeNames.get(2), new DiskUsage(nodes.get(2), "n3", "_na_", 100, 0));
    // Retrieve the count of shards on each node
    final Map<String, Integer> nodesToShardCount = new HashMap<>();
    assertBusy(new Runnable() {

        @Override
        public void run() {
            ClusterStateResponse resp = client().admin().cluster().prepareState().get();
            Iterator<RoutingNode> iter = resp.getState().getRoutingNodes().iterator();
            while (iter.hasNext()) {
                RoutingNode node = iter.next();
                logger.info("--> node {} has {} shards", node.nodeId(), resp.getState().getRoutingNodes().node(node.nodeId()).numberOfOwningShards());
                nodesToShardCount.put(node.nodeId(), resp.getState().getRoutingNodes().node(node.nodeId()).numberOfOwningShards());
            }
            assertThat("node1 has 5 shards", nodesToShardCount.get(realNodeNames.get(0)), equalTo(5));
            assertThat("node2 has 5 shards", nodesToShardCount.get(realNodeNames.get(1)), equalTo(5));
            assertThat("node3 has 0 shards", nodesToShardCount.get(realNodeNames.get(2)), equalTo(0));
        }
    });
    // Update the disk usages so one node is now back under the high watermark
    cis.setN1Usage(realNodeNames.get(0), new DiskUsage(nodes.get(0), "n1", "_na_", 100, 50));
    cis.setN2Usage(realNodeNames.get(1), new DiskUsage(nodes.get(1), "n2", "_na_", 100, 50));
    // node3 has free space now
    cis.setN3Usage(realNodeNames.get(2), new DiskUsage(nodes.get(2), "n3", "_na_", 100, 50));
    // Retrieve the count of shards on each node
    nodesToShardCount.clear();
    assertBusy(new Runnable() {

        @Override
        public void run() {
            ClusterStateResponse resp = client().admin().cluster().prepareState().get();
            Iterator<RoutingNode> iter = resp.getState().getRoutingNodes().iterator();
            while (iter.hasNext()) {
                RoutingNode node = iter.next();
                logger.info("--> node {} has {} shards", node.nodeId(), resp.getState().getRoutingNodes().node(node.nodeId()).numberOfOwningShards());
                nodesToShardCount.put(node.nodeId(), resp.getState().getRoutingNodes().node(node.nodeId()).numberOfOwningShards());
            }
            assertThat("node1 has at least 3 shards", nodesToShardCount.get(realNodeNames.get(0)), greaterThanOrEqualTo(3));
            assertThat("node2 has at least 3 shards", nodesToShardCount.get(realNodeNames.get(1)), greaterThanOrEqualTo(3));
            assertThat("node3 has at least 3 shards", nodesToShardCount.get(realNodeNames.get(2)), greaterThanOrEqualTo(3));
        }
    });
}
Also used : MockInternalClusterInfoService(org.elasticsearch.cluster.MockInternalClusterInfoService) ClusterInfoService(org.elasticsearch.cluster.ClusterInfoService) HashMap(java.util.HashMap) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) ArrayList(java.util.ArrayList) DiskUsage(org.elasticsearch.cluster.DiskUsage) NodesStatsResponse(org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse) ClusterInfo(org.elasticsearch.cluster.ClusterInfo) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) Iterator(java.util.Iterator) MockInternalClusterInfoService(org.elasticsearch.cluster.MockInternalClusterInfoService)

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