Search in sources :

Example 21 with UnassignedInfo

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

the class ReplicaShardAllocatorTests method onePrimaryOnNode1And1Replica.

private RoutingAllocation onePrimaryOnNode1And1Replica(AllocationDeciders deciders, Settings settings, UnassignedInfo.Reason reason) {
    ShardRouting primaryShard = TestShardRouting.newShardRouting(shardId, node1.getId(), true, ShardRoutingState.STARTED);
    MetaData metaData = MetaData.builder().put(IndexMetaData.builder(shardId.getIndexName()).settings(settings(Version.CURRENT).put(settings)).numberOfShards(1).numberOfReplicas(1).putInSyncAllocationIds(0, Sets.newHashSet(primaryShard.allocationId().getId()))).build();
    // mark shard as delayed if reason is NODE_LEFT
    boolean delayed = reason == UnassignedInfo.Reason.NODE_LEFT && UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.get(settings).nanos() > 0;
    int failedAllocations = reason == UnassignedInfo.Reason.ALLOCATION_FAILED ? 1 : 0;
    RoutingTable routingTable = RoutingTable.builder().add(IndexRoutingTable.builder(shardId.getIndex()).addIndexShard(new IndexShardRoutingTable.Builder(shardId).addShard(primaryShard).addShard(ShardRouting.newUnassigned(shardId, false, RecoverySource.PeerRecoverySource.INSTANCE, new UnassignedInfo(reason, null, null, failedAllocations, System.nanoTime(), System.currentTimeMillis(), delayed, UnassignedInfo.AllocationStatus.NO_ATTEMPT))).build())).build();
    ClusterState state = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).nodes(DiscoveryNodes.builder().add(node1).add(node2).add(node3)).build();
    return new RoutingAllocation(deciders, new RoutingNodes(state, false), state, ClusterInfo.EMPTY, System.nanoTime(), false);
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) MetaData(org.elasticsearch.cluster.metadata.MetaData) StoreFileMetaData(org.elasticsearch.index.store.StoreFileMetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) TransportNodesListShardStoreMetaData(org.elasticsearch.indices.store.TransportNodesListShardStoreMetaData) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) RoutingAllocation(org.elasticsearch.cluster.routing.allocation.RoutingAllocation)

Example 22 with UnassignedInfo

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

the class IndexShardTests method testFailIfIndexNotPresentInRecoverFromStore.

public void testFailIfIndexNotPresentInRecoverFromStore() throws Exception {
    final IndexShard shard = newStartedShard(true);
    indexDoc(shard, "test", "0");
    if (randomBoolean()) {
        flushShard(shard);
    }
    Store store = shard.store();
    store.incRef();
    closeShards(shard);
    cleanLuceneIndex(store.directory());
    store.decRef();
    IndexShard newShard = reinitShard(shard);
    DiscoveryNode localNode = new DiscoveryNode("foo", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT);
    ShardRouting routing = newShard.routingEntry();
    newShard.markAsRecovering("store", new RecoveryState(routing, localNode, null));
    try {
        newShard.recoverFromStore();
        fail("index not there!");
    } catch (IndexShardRecoveryException ex) {
        assertTrue(ex.getMessage().contains("failed to fetch index version after copying it over"));
    }
    routing = ShardRoutingHelper.moveToUnassigned(routing, new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "because I say so"));
    routing = ShardRoutingHelper.initialize(routing, newShard.routingEntry().currentNodeId());
    assertTrue("it's already recovering, we should ignore new ones", newShard.ignoreRecoveryAttempt());
    try {
        newShard.markAsRecovering("store", new RecoveryState(routing, localNode, null));
        fail("we are already recovering, can't mark again");
    } catch (IllegalIndexShardStateException e) {
    // OK!
    }
    newShard = reinitShard(newShard, ShardRoutingHelper.initWithSameId(routing, RecoverySource.StoreRecoverySource.EMPTY_STORE_INSTANCE));
    newShard.markAsRecovering("store", new RecoveryState(newShard.routingEntry(), localNode, null));
    assertTrue("recover even if there is nothing to recover", newShard.recoverFromStore());
    newShard.updateRoutingEntry(newShard.routingEntry().moveToStarted());
    assertDocCount(newShard, 0);
    // we can't issue this request through a client because of the inconsistencies we created with the cluster state
    // doing it directly instead
    indexDoc(newShard, "test", "0");
    newShard.refresh("test");
    assertDocCount(newShard, 1);
    closeShards(newShard);
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) Store(org.elasticsearch.index.store.Store) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) RecoveryState(org.elasticsearch.indices.recovery.RecoveryState)

Example 23 with UnassignedInfo

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

the class IndicesStoreTests method testShardCanBeDeletedNoShardStarted.

public void testShardCanBeDeletedNoShardStarted() throws Exception {
    int numShards = randomIntBetween(1, 7);
    int numReplicas = randomInt(2);
    IndexShardRoutingTable.Builder routingTable = new IndexShardRoutingTable.Builder(new ShardId("test", "_na_", 1));
    for (int i = 0; i < numShards; i++) {
        int unStartedShard = randomInt(numReplicas);
        for (int j = 0; j <= numReplicas; j++) {
            ShardRoutingState state;
            if (j == unStartedShard) {
                state = randomFrom(NOT_STARTED_STATES);
            } else {
                state = randomFrom(ShardRoutingState.values());
            }
            UnassignedInfo unassignedInfo = null;
            if (state == ShardRoutingState.UNASSIGNED) {
                unassignedInfo = new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, null);
            }
            String relocatingNodeId = state == ShardRoutingState.RELOCATING ? randomAsciiOfLength(10) : null;
            routingTable.addShard(TestShardRouting.newShardRouting("test", i, randomAsciiOfLength(10), relocatingNodeId, j == 0, state, unassignedInfo));
        }
    }
    assertFalse(IndicesStore.shardCanBeDeleted(localNode.getId(), routingTable.build()));
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) ShardRoutingState(org.elasticsearch.cluster.routing.ShardRoutingState)

Example 24 with UnassignedInfo

use of org.elasticsearch.cluster.routing.UnassignedInfo in project crate by crate.

the class SysShardsExpressionsTest method mockIndexShard.

private IndexShard mockIndexShard() {
    IndexService indexService = mock(IndexService.class);
    Index index = new Index(indexName);
    ShardId shardId = new ShardId(indexName, 1);
    IndexShard indexShard = mock(IndexShard.class);
    when(indexService.index()).thenReturn(index);
    when(indexShard.indexService()).thenReturn(indexService);
    when(indexShard.shardId()).thenReturn(shardId);
    when(indexShard.state()).thenReturn(IndexShardState.STARTED);
    StoreStats storeStats = mock(StoreStats.class);
    when(storeStats.getSizeInBytes()).thenReturn(123456L);
    when(indexShard.storeStats()).thenReturn(storeStats);
    Path dataPath = Paths.get("/dummy/" + indexName + "/1");
    when(indexShard.shardPath()).thenReturn(new ShardPath(false, dataPath, dataPath, "123", shardId));
    DocsStats docsStats = new DocsStats(654321L, 0L);
    when(indexShard.docStats()).thenReturn(docsStats).thenThrow(IllegalIndexShardStateException.class);
    ShardRouting shardRouting = ShardRouting.newUnassigned(index.name(), shardId.id(), null, true, new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "foo"));
    ShardRoutingHelper.initialize(shardRouting, "node1");
    ShardRoutingHelper.moveToStarted(shardRouting);
    ShardRoutingHelper.relocate(shardRouting, "node_X");
    when(indexShard.routingEntry()).thenReturn(shardRouting);
    when(indexShard.minimumCompatibleVersion()).thenReturn(Version.LATEST);
    RecoveryState recoveryState = mock(RecoveryState.class);
    when(indexShard.recoveryState()).thenReturn(recoveryState);
    RecoveryState.Index recoveryStateIndex = mock(RecoveryState.Index.class);
    RecoveryState.Timer recoveryStateTimer = mock(RecoveryState.Timer.class);
    when(recoveryState.getIndex()).thenReturn(recoveryStateIndex);
    when(recoveryState.getStage()).thenReturn(RecoveryState.Stage.DONE);
    when(recoveryState.getTimer()).thenReturn(recoveryStateTimer);
    when(recoveryState.getType()).thenReturn(RecoveryState.Type.REPLICA);
    when(recoveryStateIndex.totalBytes()).thenReturn(2048L);
    when(recoveryStateIndex.reusedBytes()).thenReturn(1024L);
    when(recoveryStateIndex.recoveredBytes()).thenReturn(1024L);
    when(recoveryStateIndex.totalFileCount()).thenReturn(2);
    when(recoveryStateIndex.reusedFileCount()).thenReturn(1);
    when(recoveryStateIndex.recoveredFileCount()).thenReturn(1);
    when(recoveryStateTimer.time()).thenReturn(10000L);
    return indexShard;
}
Also used : Path(java.nio.file.Path) StoreStats(org.elasticsearch.index.store.StoreStats) IndexService(org.elasticsearch.index.IndexService) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) Index(org.elasticsearch.index.Index) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) RecoveryState(org.elasticsearch.indices.recovery.RecoveryState)

Example 25 with UnassignedInfo

use of org.elasticsearch.cluster.routing.UnassignedInfo in project crate by crate.

the class DecommissionAllocationDeciderTest method init.

@Before
public void init() throws Exception {
    routingAllocation = mock(RoutingAllocation.class);
    when(routingAllocation.decision(any(Decision.class), anyString(), anyString())).then(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return invocation.getArguments()[0];
        }
    });
    primaryShard = ShardRouting.newUnassigned(new ShardId("t", UUIDs.randomBase64UUID(), 0), true, RecoverySource.PeerRecoverySource.INSTANCE, new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "dummy"));
    replicaShard = ShardRouting.newUnassigned(new ShardId("t", UUIDs.randomBase64UUID(), 0), false, RecoverySource.PeerRecoverySource.INSTANCE, new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "dummy"));
    n1 = new RoutingNode("n1", mock(DiscoveryNode.class));
    n2 = new RoutingNode("n2", mock(DiscoveryNode.class));
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RoutingAllocation(org.elasticsearch.cluster.routing.allocation.RoutingAllocation) Decision(org.elasticsearch.cluster.routing.allocation.decider.Decision) Before(org.junit.Before)

Aggregations

UnassignedInfo (org.elasticsearch.cluster.routing.UnassignedInfo)68 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)45 ShardId (org.elasticsearch.index.shard.ShardId)36 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)30 ClusterState (org.elasticsearch.cluster.ClusterState)20 Index (org.elasticsearch.index.Index)19 ClusterInfo (org.elasticsearch.cluster.ClusterInfo)18 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)18 RoutingNode (org.elasticsearch.cluster.routing.RoutingNode)18 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)18 RoutingNodes (org.elasticsearch.cluster.routing.RoutingNodes)17 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)16 RoutingAllocation (org.elasticsearch.cluster.routing.allocation.RoutingAllocation)16 Matchers.containsString (org.hamcrest.Matchers.containsString)16 ShardRoutingState (org.elasticsearch.cluster.routing.ShardRoutingState)14 AllocateUnassignedDecision (org.elasticsearch.cluster.routing.allocation.AllocateUnassignedDecision)12 NodeAllocationResult (org.elasticsearch.cluster.routing.allocation.NodeAllocationResult)11 Decision (org.elasticsearch.cluster.routing.allocation.decider.Decision)11 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)11 MetaData (org.elasticsearch.cluster.metadata.MetaData)10