Search in sources :

Example 16 with ShardRoutingState

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

the class ClusterAllocationExplainActionTests method testInitializingOrRelocatingShardExplanation.

public void testInitializingOrRelocatingShardExplanation() throws Exception {
    ShardRoutingState shardRoutingState = randomFrom(ShardRoutingState.INITIALIZING, ShardRoutingState.RELOCATING);
    ClusterState clusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), shardRoutingState);
    ShardRouting shard = clusterState.getRoutingTable().index("idx").shard(0).primaryShard();
    RoutingAllocation allocation = new RoutingAllocation(new AllocationDeciders(Settings.EMPTY, Collections.emptyList()), clusterState.getRoutingNodes(), clusterState, null, System.nanoTime(), randomBoolean());
    ClusterAllocationExplanation cae = TransportClusterAllocationExplainAction.explainShard(shard, allocation, null, randomBoolean(), new TestGatewayAllocator(), new ShardsAllocator() {

        @Override
        public void allocate(RoutingAllocation allocation) {
        // no-op
        }

        @Override
        public ShardAllocationDecision decideShardAllocation(ShardRouting shard, RoutingAllocation allocation) {
            if (shard.initializing() || shard.relocating()) {
                return ShardAllocationDecision.NOT_TAKEN;
            } else {
                throw new UnsupportedOperationException("cannot explain");
            }
        }
    });
    assertEquals(shard.currentNodeId(), cae.getCurrentNode().getId());
    assertFalse(cae.getShardAllocationDecision().isDecisionTaken());
    assertFalse(cae.getShardAllocationDecision().getAllocateDecision().isDecisionTaken());
    assertFalse(cae.getShardAllocationDecision().getMoveDecision().isDecisionTaken());
    XContentBuilder builder = XContentFactory.jsonBuilder();
    cae.toXContent(builder, ToXContent.EMPTY_PARAMS);
    String explanation;
    if (shardRoutingState == ShardRoutingState.RELOCATING) {
        explanation = "the shard is in the process of relocating from node [] to node [], wait until " + "relocation has completed";
    } else {
        explanation = "the shard is in the process of initializing on node [], " + "wait until initialization has completed";
    }
    assertEquals("{\"index\":\"idx\",\"shard\":0,\"primary\":true,\"current_state\":\"" + shardRoutingState.toString().toLowerCase(Locale.ROOT) + "\",\"current_node\":" + "{\"id\":\"" + cae.getCurrentNode().getId() + "\",\"name\":\"" + cae.getCurrentNode().getName() + "\",\"transport_address\":\"" + cae.getCurrentNode().getAddress() + "\"},\"explanation\":\"" + explanation + "\"}", builder.string());
}
Also used : TestGatewayAllocator(org.elasticsearch.test.gateway.TestGatewayAllocator) ClusterState(org.elasticsearch.cluster.ClusterState) ShardRoutingState(org.elasticsearch.cluster.routing.ShardRoutingState) AllocationDeciders(org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders) ShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.ShardsAllocator) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) RoutingAllocation(org.elasticsearch.cluster.routing.allocation.RoutingAllocation) ShardAllocationDecision(org.elasticsearch.cluster.routing.allocation.ShardAllocationDecision) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 17 with ShardRoutingState

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

the class ClusterAllocationExplainIT method testCannotAllocateStaleReplicaExplanation.

public void testCannotAllocateStaleReplicaExplanation() throws Exception {
    logger.info("--> starting 3 nodes");
    final String masterNode = internalCluster().startNode();
    // start replica node first, so it's path will be used first when we start a node after
    // stopping all of them at end of test.
    final String replicaNode = internalCluster().startNode();
    final String primaryNode = internalCluster().startNode();
    logger.info("--> creating an index with 1 primary and 1 replica");
    createIndexAndIndexData(1, 1, Settings.builder().put("index.routing.allocation.include._name", primaryNode).put("index.routing.allocation.exclude._name", masterNode).build(), ActiveShardCount.ONE);
    client().admin().indices().prepareUpdateSettings("idx").setSettings(Settings.builder().put("index.routing.allocation.include._name", (String) null)).get();
    ensureGreen();
    assertThat(replicaNode().getName(), equalTo(replicaNode));
    assertThat(primaryNodeName(), equalTo(primaryNode));
    logger.info("--> stop node with the replica shard");
    internalCluster().stopRandomNode(InternalTestCluster.nameFilter(replicaNode));
    logger.info("--> index more data, now the replica is stale");
    indexData();
    logger.info("--> stop the node with the primary");
    internalCluster().stopRandomNode(InternalTestCluster.nameFilter(primaryNode));
    logger.info("--> restart the node with the stale replica");
    String restartedNode = internalCluster().startDataOnlyNode();
    // wait for the master to finish processing join.
    ensureClusterSizeConsistency();
    // wait until the system has fetched shard data and we know there is no valid shard copy
    assertBusy(() -> {
        ClusterAllocationExplanation explanation = client().admin().cluster().prepareAllocationExplain().setIndex("idx").setShard(0).setPrimary(true).get().getExplanation();
        assertTrue(explanation.getShardAllocationDecision().getAllocateDecision().isDecisionTaken());
        assertEquals(AllocationDecision.NO_VALID_SHARD_COPY, explanation.getShardAllocationDecision().getAllocateDecision().getAllocationDecision());
    });
    boolean includeYesDecisions = randomBoolean();
    boolean includeDiskInfo = randomBoolean();
    ClusterAllocationExplanation explanation = runExplain(true, includeYesDecisions, includeDiskInfo);
    ShardId shardId = explanation.getShard();
    boolean isPrimary = explanation.isPrimary();
    ShardRoutingState shardRoutingState = explanation.getShardState();
    DiscoveryNode currentNode = explanation.getCurrentNode();
    UnassignedInfo unassignedInfo = explanation.getUnassignedInfo();
    AllocateUnassignedDecision allocateDecision = explanation.getShardAllocationDecision().getAllocateDecision();
    MoveDecision moveDecision = explanation.getShardAllocationDecision().getMoveDecision();
    // verify shard info
    assertEquals("idx", shardId.getIndexName());
    assertEquals(0, shardId.getId());
    assertTrue(isPrimary);
    // verify current node info
    assertEquals(ShardRoutingState.UNASSIGNED, shardRoutingState);
    assertNull(currentNode);
    // verify unassigned info
    assertNotNull(unassignedInfo);
    // verify decision object
    assertTrue(allocateDecision.isDecisionTaken());
    assertFalse(moveDecision.isDecisionTaken());
    assertEquals(AllocationDecision.NO_VALID_SHARD_COPY, allocateDecision.getAllocationDecision());
    assertEquals(2, allocateDecision.getNodeDecisions().size());
    for (NodeAllocationResult nodeAllocationResult : allocateDecision.getNodeDecisions()) {
        if (nodeAllocationResult.getNode().getName().equals(restartedNode)) {
            assertNotNull(nodeAllocationResult.getShardStoreInfo());
            assertNotNull(nodeAllocationResult.getShardStoreInfo().getAllocationId());
            assertFalse(nodeAllocationResult.getShardStoreInfo().isInSync());
            assertNull(nodeAllocationResult.getShardStoreInfo().getStoreException());
        } else {
            assertNotNull(nodeAllocationResult.getShardStoreInfo());
            assertNull(nodeAllocationResult.getShardStoreInfo().getAllocationId());
            assertFalse(nodeAllocationResult.getShardStoreInfo().isInSync());
            assertNull(nodeAllocationResult.getShardStoreInfo().getStoreException());
        }
    }
    // verify JSON output
    try (XContentParser parser = getParser(explanation)) {
        verifyShardInfo(parser, true, includeDiskInfo, ShardRoutingState.UNASSIGNED);
        parser.nextToken();
        assertEquals("can_allocate", parser.currentName());
        parser.nextToken();
        assertEquals(AllocationDecision.NO_VALID_SHARD_COPY.toString(), parser.text());
        parser.nextToken();
        assertEquals("allocate_explanation", parser.currentName());
        parser.nextToken();
        assertEquals("cannot allocate because all found copies of the shard are either stale or corrupt", parser.text());
        verifyStaleShardCopyNodeDecisions(parser, 2, Collections.singleton(restartedNode));
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) MoveDecision(org.elasticsearch.cluster.routing.allocation.MoveDecision) AllocateUnassignedDecision(org.elasticsearch.cluster.routing.allocation.AllocateUnassignedDecision) Matchers.containsString(org.hamcrest.Matchers.containsString) ShardRoutingState(org.elasticsearch.cluster.routing.ShardRoutingState) NodeAllocationResult(org.elasticsearch.cluster.routing.allocation.NodeAllocationResult) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

ShardRoutingState (org.elasticsearch.cluster.routing.ShardRoutingState)17 ShardId (org.elasticsearch.index.shard.ShardId)13 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)12 UnassignedInfo (org.elasticsearch.cluster.routing.UnassignedInfo)12 AllocateUnassignedDecision (org.elasticsearch.cluster.routing.allocation.AllocateUnassignedDecision)10 MoveDecision (org.elasticsearch.cluster.routing.allocation.MoveDecision)10 XContentParser (org.elasticsearch.common.xcontent.XContentParser)10 ClusterInfo (org.elasticsearch.cluster.ClusterInfo)9 NodeAllocationResult (org.elasticsearch.cluster.routing.allocation.NodeAllocationResult)9 AllocationDecision (org.elasticsearch.cluster.routing.allocation.AllocationDecision)8 Decision (org.elasticsearch.cluster.routing.allocation.decider.Decision)8 ClusterState (org.elasticsearch.cluster.ClusterState)6 Matchers.containsString (org.hamcrest.Matchers.containsString)6 HashMap (java.util.HashMap)4 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)4 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)4 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)3 HashSet (java.util.HashSet)2 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)2 IndexRoutingTable (org.elasticsearch.cluster.routing.IndexRoutingTable)2