Search in sources :

Example 31 with ShardNotFoundException

use of org.elasticsearch.index.shard.ShardNotFoundException in project crate by crate.

the class ShardCollectSource method getIterators.

private List<CompletableFuture<BatchIterator<Row>>> getIterators(CollectTask collectTask, RoutedCollectPhase collectPhase, boolean requiresScroll, Map<String, IntIndexedContainer> indexShards) {
    Metadata metadata = clusterService.state().metadata();
    List<CompletableFuture<BatchIterator<Row>>> iterators = new ArrayList<>();
    for (Map.Entry<String, IntIndexedContainer> entry : indexShards.entrySet()) {
        String indexName = entry.getKey();
        IndexMetadata indexMD = metadata.index(indexName);
        if (indexMD == null) {
            if (IndexParts.isPartitioned(indexName)) {
                continue;
            }
            throw new IndexNotFoundException(indexName);
        }
        Index index = indexMD.getIndex();
        try {
            indicesService.indexServiceSafe(index);
        } catch (IndexNotFoundException e) {
            if (IndexParts.isPartitioned(indexName)) {
                continue;
            }
            throw e;
        }
        for (IntCursor shardCursor : entry.getValue()) {
            ShardId shardId = new ShardId(index, shardCursor.value);
            try {
                ShardCollectorProvider shardCollectorProvider = getCollectorProviderSafe(shardId);
                CompletableFuture<BatchIterator<Row>> iterator = shardCollectorProvider.getFutureIterator(collectPhase, requiresScroll, collectTask);
                iterators.add(iterator);
            } catch (ShardNotFoundException | IllegalIndexShardStateException e) {
                // and the reader required in the fetchPhase would be missing.
                if (Symbols.containsColumn(collectPhase.toCollect(), DocSysColumns.FETCHID)) {
                    throw e;
                }
                iterators.add(remoteCollectorFactory.createCollector(shardId, collectPhase, collectTask, shardCollectorProviderFactory));
            } catch (IndexNotFoundException e) {
                // Prevent wrapping this to not break retry-detection
                throw e;
            } catch (Throwable t) {
                Exceptions.rethrowRuntimeException(t);
            }
        }
    }
    return iterators;
}
Also used : IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) ArrayList(java.util.ArrayList) IntIndexedContainer(com.carrotsearch.hppc.IntIndexedContainer) Index(org.elasticsearch.index.Index) InMemoryBatchIterator(io.crate.data.InMemoryBatchIterator) CompositeBatchIterator(io.crate.data.CompositeBatchIterator) BatchIterator(io.crate.data.BatchIterator) IllegalIndexShardStateException(org.elasticsearch.index.shard.IllegalIndexShardStateException) ShardId(org.elasticsearch.index.shard.ShardId) CompletableFuture(java.util.concurrent.CompletableFuture) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) IntCursor(com.carrotsearch.hppc.cursors.IntCursor) ShardCollectorProvider(io.crate.execution.engine.collect.ShardCollectorProvider) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) Row(io.crate.data.Row) SentinelRow(io.crate.data.SentinelRow) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 32 with ShardNotFoundException

use of org.elasticsearch.index.shard.ShardNotFoundException in project crate by crate.

the class BlobIntegrationTest method getBlobShard.

@Nullable
private BlobShard getBlobShard(String digest) {
    Iterable<BlobIndicesService> services = internalCluster().getInstances(BlobIndicesService.class);
    Iterator<BlobIndicesService> it = services.iterator();
    BlobShard blobShard = null;
    while (it.hasNext()) {
        BlobIndicesService nextService = it.next();
        try {
            blobShard = nextService.localBlobShard(".blob_test", digest);
        } catch (ShardNotFoundException | IndexNotFoundException e) {
            continue;
        }
        if (blobShard != null) {
            break;
        }
    }
    return blobShard;
}
Also used : BlobIndicesService(io.crate.blob.v2.BlobIndicesService) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) BlobShard(io.crate.blob.v2.BlobShard) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) Nullable(javax.annotation.Nullable)

Example 33 with ShardNotFoundException

use of org.elasticsearch.index.shard.ShardNotFoundException in project crate by crate.

the class AllocationCommandsTests method testAllocateCommand.

@Test
public void testAllocateCommand() {
    AllocationService allocation = createAllocationService(Settings.builder().put(EnableAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ENABLE_SETTING.getKey(), "none").put(EnableAllocationDecider.CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), "none").build());
    final String index = "test";
    logger.info("--> building initial routing table");
    Metadata metadata = Metadata.builder().put(IndexMetadata.builder(index).settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1).putInSyncAllocationIds(0, Collections.singleton("asdf")).putInSyncAllocationIds(1, Collections.singleton("qwertz"))).build();
    // shard routing is added as "from recovery" instead of "new index creation" so that we can test below that allocating an empty
    // primary with accept_data_loss flag set to false fails
    RoutingTable routingTable = RoutingTable.builder().addAsRecovery(metadata.index(index)).build();
    ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metadata(metadata).routingTable(routingTable).build();
    final ShardId shardId = new ShardId(metadata.index(index).getIndex(), 0);
    logger.info("--> adding 3 nodes on same rack and do rerouting");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2")).add(newNode("node3")).add(newNode("node4", singleton(DiscoveryNodeRole.MASTER_ROLE)))).build();
    clusterState = allocation.reroute(clusterState, "reroute");
    assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(0));
    logger.info("--> allocating to non-existent node, should fail");
    try {
        allocation.reroute(clusterState, new AllocationCommands(randomAllocateCommand(index, shardId.id(), "node42")), false, false);
        fail("expected IllegalArgumentException when allocating to non-existing node");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("failed to resolve [node42], no matching nodes"));
    }
    logger.info("--> allocating to non-data node, should fail");
    try {
        allocation.reroute(clusterState, new AllocationCommands(randomAllocateCommand(index, shardId.id(), "node4")), false, false);
        fail("expected IllegalArgumentException when allocating to non-data node");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("allocation can only be done on data nodes"));
    }
    logger.info("--> allocating non-existing shard, should fail");
    try {
        allocation.reroute(clusterState, new AllocationCommands(randomAllocateCommand("test", 1, "node2")), false, false);
        fail("expected ShardNotFoundException when allocating non-existing shard");
    } catch (ShardNotFoundException e) {
        assertThat(e.getMessage(), containsString("no such shard"));
    }
    logger.info("--> allocating non-existing index, should fail");
    try {
        allocation.reroute(clusterState, new AllocationCommands(randomAllocateCommand("test2", 0, "node2")), false, false);
        fail("expected ShardNotFoundException when allocating non-existing index");
    } catch (IndexNotFoundException e) {
        assertThat(e.getMessage(), containsString("no such index"));
    }
    logger.info("--> allocating empty primary with acceptDataLoss flag set to false");
    try {
        allocation.reroute(clusterState, new AllocationCommands(new AllocateEmptyPrimaryAllocationCommand("test", 0, "node1", false)), false, false);
        fail("expected IllegalArgumentException when allocating empty primary with acceptDataLoss flag set to false");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("allocating an empty primary for " + shardId + " can result in data loss. Please confirm by setting the accept_data_loss parameter to true"));
    }
    logger.info("--> allocating stale primary with acceptDataLoss flag set to false");
    try {
        allocation.reroute(clusterState, new AllocationCommands(new AllocateStalePrimaryAllocationCommand(index, shardId.id(), "node1", false)), false, false);
        fail("expected IllegalArgumentException when allocating stale primary with acceptDataLoss flag set to false");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("allocating an empty primary for " + shardId + " can result in data loss. Please confirm by setting the accept_data_loss parameter to true"));
    }
    logger.info("--> allocating empty primary with acceptDataLoss flag set to true");
    ClusterState newState = allocation.reroute(clusterState, new AllocationCommands(new AllocateEmptyPrimaryAllocationCommand("test", 0, "node1", true)), false, false).getClusterState();
    assertThat(newState, not(equalTo(clusterState)));
    clusterState = newState;
    assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(INITIALIZING).size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(0));
    logger.info("--> start the primary shard");
    clusterState = startInitializingShardsAndReroute(allocation, clusterState);
    assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(0));
    logger.info("--> allocate the replica shard on the primary shard node, should fail");
    try {
        allocation.reroute(clusterState, new AllocationCommands(new AllocateReplicaAllocationCommand("test", 0, "node1")), false, false);
        fail("expected IllegalArgumentException when allocating replica shard on the primary shard node");
    } catch (IllegalArgumentException e) {
    }
    logger.info("--> allocate the replica shard on on the second node");
    newState = allocation.reroute(clusterState, new AllocationCommands(new AllocateReplicaAllocationCommand("test", 0, "node2")), false, false).getClusterState();
    assertThat(newState, not(equalTo(clusterState)));
    clusterState = newState;
    assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(INITIALIZING).size(), equalTo(1));
    logger.info("--> start the replica shard");
    clusterState = startInitializingShardsAndReroute(allocation, clusterState);
    assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(STARTED).size(), equalTo(1));
    logger.info("--> verify that we fail when there are no unassigned shards");
    try {
        allocation.reroute(clusterState, new AllocationCommands(randomAllocateCommand("test", 0, "node3")), false, false);
        fail("expected IllegalArgumentException when allocating shard while no unassigned shard available");
    } catch (IllegalArgumentException e) {
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) AllocateStalePrimaryAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.AllocateStalePrimaryAllocationCommand) AllocateEmptyPrimaryAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.AllocateEmptyPrimaryAllocationCommand) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) AllocateReplicaAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.AllocateReplicaAllocationCommand) Matchers.containsString(org.hamcrest.Matchers.containsString) AllocationCommands(org.elasticsearch.cluster.routing.allocation.command.AllocationCommands) ShardId(org.elasticsearch.index.shard.ShardId) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) Test(org.junit.Test)

Aggregations

ShardNotFoundException (org.elasticsearch.index.shard.ShardNotFoundException)33 IndexNotFoundException (org.elasticsearch.index.IndexNotFoundException)23 ShardId (org.elasticsearch.index.shard.ShardId)16 ArrayList (java.util.ArrayList)9 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)9 Map (java.util.Map)8 RoutingNode (org.elasticsearch.cluster.routing.RoutingNode)8 IllegalIndexShardStateException (org.elasticsearch.index.shard.IllegalIndexShardStateException)8 ElasticsearchException (org.elasticsearch.ElasticsearchException)7 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)7 IndexService (org.elasticsearch.index.IndexService)7 IndexShard (org.elasticsearch.index.shard.IndexShard)7 IOException (java.io.IOException)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)6 RoutingNodes (org.elasticsearch.cluster.routing.RoutingNodes)6 RerouteExplanation (org.elasticsearch.cluster.routing.allocation.RerouteExplanation)6 List (java.util.List)5 ClusterState (org.elasticsearch.cluster.ClusterState)5 ShardCollectorProvider (io.crate.execution.engine.collect.ShardCollectorProvider)4