Search in sources :

Example 36 with IndexNotFoundException

use of org.elasticsearch.index.IndexNotFoundException in project crate by crate.

the class TransportClusterHealthAction method prepareResponse.

static int prepareResponse(final ClusterHealthRequest request, final ClusterHealthResponse response, final ClusterState clusterState, final IndexNameExpressionResolver indexNameExpressionResolver) {
    int waitForCounter = 0;
    if (request.waitForStatus() != null && response.getStatus().value() <= request.waitForStatus().value()) {
        waitForCounter++;
    }
    if (request.waitForNoRelocatingShards() && response.getRelocatingShards() == 0) {
        waitForCounter++;
    }
    if (request.waitForNoInitializingShards() && response.getInitializingShards() == 0) {
        waitForCounter++;
    }
    if (request.waitForActiveShards().equals(ActiveShardCount.NONE) == false) {
        ActiveShardCount waitForActiveShards = request.waitForActiveShards();
        assert waitForActiveShards.equals(ActiveShardCount.DEFAULT) == false : "waitForActiveShards must not be DEFAULT on the request object, instead it should be NONE";
        if (waitForActiveShards.equals(ActiveShardCount.ALL)) {
            if (response.getUnassignedShards() == 0 && response.getInitializingShards() == 0) {
                // if we are waiting for all shards to be active, then the num of unassigned and num of initializing shards must be 0
                waitForCounter++;
            }
        } else if (waitForActiveShards.enoughShardsActive(response.getActiveShards())) {
            // there are enough active shards to meet the requirements of the request
            waitForCounter++;
        }
    }
    if (request.indices() != null && request.indices().length > 0) {
        try {
            indexNameExpressionResolver.concreteIndexNames(clusterState, IndicesOptions.strictExpand(), request.indices());
            waitForCounter++;
        } catch (IndexNotFoundException e) {
            // no indices, make sure its RED
            response.setStatus(ClusterHealthStatus.RED);
        // missing indices, wait a bit more...
        }
    }
    if (!request.waitForNodes().isEmpty()) {
        if (request.waitForNodes().startsWith(">=")) {
            int expected = Integer.parseInt(request.waitForNodes().substring(2));
            if (response.getNumberOfNodes() >= expected) {
                waitForCounter++;
            }
        } else if (request.waitForNodes().startsWith("ge(")) {
            int expected = Integer.parseInt(request.waitForNodes().substring(3, request.waitForNodes().length() - 1));
            if (response.getNumberOfNodes() >= expected) {
                waitForCounter++;
            }
        } else if (request.waitForNodes().startsWith("<=")) {
            int expected = Integer.parseInt(request.waitForNodes().substring(2));
            if (response.getNumberOfNodes() <= expected) {
                waitForCounter++;
            }
        } else if (request.waitForNodes().startsWith("le(")) {
            int expected = Integer.parseInt(request.waitForNodes().substring(3, request.waitForNodes().length() - 1));
            if (response.getNumberOfNodes() <= expected) {
                waitForCounter++;
            }
        } else if (request.waitForNodes().startsWith(">")) {
            int expected = Integer.parseInt(request.waitForNodes().substring(1));
            if (response.getNumberOfNodes() > expected) {
                waitForCounter++;
            }
        } else if (request.waitForNodes().startsWith("gt(")) {
            int expected = Integer.parseInt(request.waitForNodes().substring(3, request.waitForNodes().length() - 1));
            if (response.getNumberOfNodes() > expected) {
                waitForCounter++;
            }
        } else if (request.waitForNodes().startsWith("<")) {
            int expected = Integer.parseInt(request.waitForNodes().substring(1));
            if (response.getNumberOfNodes() < expected) {
                waitForCounter++;
            }
        } else if (request.waitForNodes().startsWith("lt(")) {
            int expected = Integer.parseInt(request.waitForNodes().substring(3, request.waitForNodes().length() - 1));
            if (response.getNumberOfNodes() < expected) {
                waitForCounter++;
            }
        } else {
            int expected = Integer.parseInt(request.waitForNodes());
            if (response.getNumberOfNodes() == expected) {
                waitForCounter++;
            }
        }
    }
    return waitForCounter;
}
Also used : IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) ActiveShardCount(org.elasticsearch.action.support.ActiveShardCount)

Example 37 with IndexNotFoundException

use of org.elasticsearch.index.IndexNotFoundException in project crate by crate.

the class TransportCloseTable method closeRoutingTable.

/**
 * Step 3 - Move index states from OPEN to CLOSE in cluster state for indices that are ready for closing.
 * @param target
 */
static ClusterState closeRoutingTable(ClusterState currentState, AlterTableTarget target, DDLClusterStateService ddlClusterStateService, Map<Index, ClusterBlock> blockedIndices, Map<Index, AcknowledgedResponse> results) {
    // Remove the index routing table of closed indices if the cluster is in a mixed version
    // that does not support the replication of closed indices
    final boolean removeRoutingTable = currentState.nodes().getMinNodeVersion().before(Version.V_4_3_0);
    IndexTemplateMetadata templateMetadata = target.templateMetadata();
    ClusterState updatedState;
    if (templateMetadata == null) {
        updatedState = currentState;
    } else {
        Metadata.Builder metadata = Metadata.builder(currentState.metadata());
        metadata.put(closePartitionTemplate(templateMetadata));
        updatedState = ClusterState.builder(currentState).metadata(metadata).build();
    }
    String partition = target.partition();
    if (partition != null) {
        PartitionName partitionName = PartitionName.fromIndexOrTemplate(partition);
        updatedState = ddlClusterStateService.onCloseTablePartition(updatedState, partitionName);
    } else {
        updatedState = ddlClusterStateService.onCloseTable(updatedState, target.table());
    }
    final Metadata.Builder metadata = Metadata.builder(updatedState.metadata());
    final ClusterBlocks.Builder blocks = ClusterBlocks.builder().blocks(updatedState.blocks());
    final RoutingTable.Builder routingTable = RoutingTable.builder(updatedState.routingTable());
    final Set<String> closedIndices = new HashSet<>();
    for (Map.Entry<Index, AcknowledgedResponse> result : results.entrySet()) {
        final Index index = result.getKey();
        final boolean acknowledged = result.getValue().isAcknowledged();
        try {
            if (acknowledged == false) {
                LOGGER.debug("verification of shards before closing {} failed", index);
                continue;
            }
            final IndexMetadata indexMetadata = metadata.getSafe(index);
            if (indexMetadata.getState() == IndexMetadata.State.CLOSE) {
                LOGGER.debug("verification of shards before closing {} succeeded but index is already closed", index);
                assert currentState.blocks().hasIndexBlock(index.getName(), IndexMetadata.INDEX_CLOSED_BLOCK);
                continue;
            }
            final ClusterBlock closingBlock = blockedIndices.get(index);
            if (currentState.blocks().hasIndexBlock(index.getName(), closingBlock) == false) {
                LOGGER.debug("verification of shards before closing {} succeeded but block has been removed in the meantime", index);
                continue;
            }
            Set<Index> restoringIndices = RestoreService.restoringIndices(updatedState, Set.of(index));
            if (restoringIndices.isEmpty() == false) {
                result.setValue(new AcknowledgedResponse(false));
                LOGGER.debug("verification of shards before closing {} succeeded but index is being restored in the meantime", index);
                continue;
            }
            Set<Index> snapshottingIndices = SnapshotsService.snapshottingIndices(updatedState, Set.of(index));
            if (snapshottingIndices.isEmpty() == false) {
                result.setValue(new AcknowledgedResponse(false));
                LOGGER.debug("verification of shards before closing {} succeeded but index is being snapshot in the meantime", index);
                continue;
            }
            blocks.removeIndexBlockWithId(index.getName(), INDEX_CLOSED_BLOCK_ID);
            blocks.addIndexBlock(index.getName(), IndexMetadata.INDEX_CLOSED_BLOCK);
            final IndexMetadata.Builder updatedMetadata = IndexMetadata.builder(indexMetadata).state(IndexMetadata.State.CLOSE);
            if (removeRoutingTable) {
                metadata.put(updatedMetadata);
                routingTable.remove(index.getName());
            } else {
                metadata.put(updatedMetadata.settingsVersion(indexMetadata.getSettingsVersion() + 1).settings(Settings.builder().put(indexMetadata.getSettings()).put(IndexMetadata.VERIFIED_BEFORE_CLOSE_SETTING.getKey(), true)));
                routingTable.addAsFromOpenToClose(metadata.getSafe(index));
            }
            LOGGER.debug("closing index {} succeeded", index);
            closedIndices.add(index.getName());
        } catch (IndexNotFoundException e) {
            LOGGER.debug("index {} has been deleted since it was blocked before closing, ignoring", index);
        }
    }
    LOGGER.info("completed closing of indices {}", closedIndices);
    return ClusterState.builder(currentState).blocks(blocks).metadata(metadata).routingTable(routingTable.build()).build();
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) Index(org.elasticsearch.index.Index) PartitionName(io.crate.metadata.PartitionName) ClusterBlock(org.elasticsearch.cluster.block.ClusterBlock) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Map(java.util.Map) ImmutableOpenIntMap(org.elasticsearch.common.collect.ImmutableOpenIntMap) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 38 with IndexNotFoundException

use of org.elasticsearch.index.IndexNotFoundException in project crate by crate.

the class RenameTableClusterStateExecutor method execute.

public ClusterState execute(ClusterState currentState, RenameTableRequest request) throws Exception {
    RelationName source = request.sourceTableIdent();
    RelationName target = request.targetTableIdent();
    boolean isPartitioned = request.isPartitioned();
    Metadata currentMetadata = currentState.getMetadata();
    Metadata.Builder newMetadata = Metadata.builder(currentMetadata);
    if (isPartitioned) {
        IndexTemplateMetadata indexTemplateMetadata = DDLClusterStateHelpers.templateMetadata(currentMetadata, source);
        if (indexTemplateMetadata == null) {
            throw new IndexTemplateMissingException("Template for partitioned table is missing");
        }
        renameTemplate(newMetadata, indexTemplateMetadata, target);
    }
    RoutingTable.Builder newRoutingTable = RoutingTable.builder(currentState.routingTable());
    ClusterBlocks.Builder blocksBuilder = ClusterBlocks.builder().blocks(currentState.blocks());
    logger.info("renaming table '{}' to '{}'", source.fqn(), target.fqn());
    try {
        Index[] sourceIndices = indexNameExpressionResolver.concreteIndices(currentState, STRICT_INDICES_OPTIONS, source.indexNameOrAlias());
        for (Index sourceIndex : sourceIndices) {
            IndexMetadata sourceIndexMetadata = currentMetadata.getIndexSafe(sourceIndex);
            String sourceIndexName = sourceIndex.getName();
            newMetadata.remove(sourceIndexName);
            newRoutingTable.remove(sourceIndexName);
            blocksBuilder.removeIndexBlocks(sourceIndexName);
            IndexMetadata targetMd;
            if (isPartitioned) {
                PartitionName partitionName = PartitionName.fromIndexOrTemplate(sourceIndexName);
                String targetIndexName = IndexParts.toIndexName(target, partitionName.ident());
                targetMd = IndexMetadata.builder(sourceIndexMetadata).removeAllAliases().putAlias(AliasMetadata.builder(target.indexNameOrAlias()).build()).index(targetIndexName).build();
            } else {
                targetMd = IndexMetadata.builder(sourceIndexMetadata).index(target.indexNameOrAlias()).build();
            }
            newMetadata.put(targetMd, true);
            newRoutingTable.addAsFromCloseToOpen(targetMd);
            blocksBuilder.addBlocks(targetMd);
        }
    } catch (IndexNotFoundException e) {
        if (isPartitioned == false) {
            throw e;
        }
    // empty partition case, no indices, just a template exists
    }
    ClusterState clusterStateAfterRename = ClusterState.builder(currentState).metadata(newMetadata).routingTable(newRoutingTable.build()).blocks(blocksBuilder).build();
    return allocationService.reroute(ddlClusterStateService.onRenameTable(clusterStateAfterRename, source, target, request.isPartitioned()), "rename-table");
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexTemplateMissingException(org.elasticsearch.indices.IndexTemplateMissingException) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) AliasMetadata(org.elasticsearch.cluster.metadata.AliasMetadata) Index(org.elasticsearch.index.Index) PartitionName(io.crate.metadata.PartitionName) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) RelationName(io.crate.metadata.RelationName) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata)

Example 39 with IndexNotFoundException

use of org.elasticsearch.index.IndexNotFoundException in project crate by crate.

the class FetchTask method start.

@Override
public void start() {
    synchronized (jobId) {
        if (killed != null) {
            result.completeExceptionally(killed);
            return;
        }
        HashMap<String, RelationName> index2TableIdent = new HashMap<>();
        for (Map.Entry<RelationName, Collection<String>> entry : phase.tableIndices().entrySet()) {
            for (String indexName : entry.getValue()) {
                index2TableIdent.put(indexName, entry.getKey());
            }
        }
        Set<RelationName> tablesWithFetchRefs = new HashSet<>();
        for (Reference reference : phase.fetchRefs()) {
            tablesWithFetchRefs.add(reference.ident().tableIdent());
        }
        String source = "fetch-task: " + jobId.toString() + '-' + phase.phaseId() + '-' + phase.name();
        for (Routing routing : routingIterable) {
            Map<String, Map<String, IntIndexedContainer>> locations = routing.locations();
            Map<String, IntIndexedContainer> indexShards = locations.get(localNodeId);
            for (Map.Entry<String, IntIndexedContainer> indexShardsEntry : indexShards.entrySet()) {
                String indexName = indexShardsEntry.getKey();
                Integer base = phase.bases().get(indexName);
                if (base == null) {
                    continue;
                }
                IndexMetadata indexMetadata = metadata.index(indexName);
                if (indexMetadata == null) {
                    if (IndexParts.isPartitioned(indexName)) {
                        continue;
                    }
                    throw new IndexNotFoundException(indexName);
                }
                Index index = indexMetadata.getIndex();
                RelationName ident = index2TableIdent.get(indexName);
                assert ident != null : "no relationName found for index " + indexName;
                tableIdents.put(base, ident);
                toFetch.put(ident, new ArrayList<>());
                for (IntCursor shard : indexShardsEntry.getValue()) {
                    ShardId shardId = new ShardId(index, shard.value);
                    int readerId = base + shardId.id();
                    SharedShardContext shardContext = shardContexts.get(readerId);
                    if (shardContext == null) {
                        try {
                            shardContext = sharedShardContexts.createContext(shardId, readerId);
                            shardContexts.put(readerId, shardContext);
                            if (tablesWithFetchRefs.contains(ident)) {
                                searchers.put(readerId, shardContext.acquireSearcher(source));
                            }
                        } catch (IndexNotFoundException e) {
                            if (!IndexParts.isPartitioned(indexName)) {
                                throw e;
                            }
                        }
                    }
                }
            }
        }
        for (Reference reference : phase.fetchRefs()) {
            Collection<Reference> references = toFetch.get(reference.ident().tableIdent());
            if (references != null) {
                references.add(reference);
            }
        }
    }
    if (searchers.isEmpty() || phase.fetchRefs().isEmpty()) {
        // no fetch references means there will be no fetch requests
        // this context is only here to allow the collectors to generate docids with the right bases
        // the bases are fetched in the prepare phase therefore this context can be closed
        close();
    }
}
Also used : HashMap(java.util.HashMap) IntObjectHashMap(com.carrotsearch.hppc.IntObjectHashMap) Index(org.elasticsearch.index.Index) ShardId(org.elasticsearch.index.shard.ShardId) IntCursor(com.carrotsearch.hppc.cursors.IntCursor) RelationName(io.crate.metadata.RelationName) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) HashSet(java.util.HashSet) Reference(io.crate.metadata.Reference) Routing(io.crate.metadata.Routing) IntIndexedContainer(com.carrotsearch.hppc.IntIndexedContainer) Collection(java.util.Collection) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) IntObjectHashMap(com.carrotsearch.hppc.IntObjectHashMap) SharedShardContext(io.crate.execution.jobs.SharedShardContext)

Example 40 with IndexNotFoundException

use of org.elasticsearch.index.IndexNotFoundException in project crate by crate.

the class GroupRowsByShard method getShardLocation.

@Nullable
private ShardLocation getShardLocation(String indexName, String id, @Nullable String routing) {
    try {
        ShardIterator shardIterator = clusterService.operationRouting().indexShards(clusterService.state(), indexName, id, routing);
        final String nodeId;
        ShardRouting shardRouting = shardIterator.nextOrNull();
        if (shardRouting == null) {
            nodeId = null;
        } else if (shardRouting.active() == false) {
            nodeId = shardRouting.relocatingNodeId();
        } else {
            nodeId = shardRouting.currentNodeId();
        }
        if (nodeId == null && LOGGER.isDebugEnabled()) {
            LOGGER.debug("Unable to get the node id for index {} and shard {}", indexName, id);
        }
        return new ShardLocation(shardIterator.shardId(), nodeId);
    } catch (IndexNotFoundException e) {
        if (!autoCreateIndices) {
            throw e;
        }
        return null;
    }
}
Also used : ShardIterator(org.elasticsearch.cluster.routing.ShardIterator) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) Nullable(javax.annotation.Nullable)

Aggregations

IndexNotFoundException (org.elasticsearch.index.IndexNotFoundException)92 ClusterState (org.elasticsearch.cluster.ClusterState)22 ShardNotFoundException (org.elasticsearch.index.shard.ShardNotFoundException)21 ShardId (org.elasticsearch.index.shard.ShardId)19 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)16 Index (org.elasticsearch.index.Index)16 Map (java.util.Map)15 ArrayList (java.util.ArrayList)14 IOException (java.io.IOException)13 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)12 List (java.util.List)11 IndicesOptions (org.elasticsearch.action.support.IndicesOptions)11 ClusterName (org.elasticsearch.cluster.ClusterName)9 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)9 Settings (org.elasticsearch.common.settings.Settings)9 Matchers.containsString (org.hamcrest.Matchers.containsString)9 HashMap (java.util.HashMap)8 Nullable (javax.annotation.Nullable)8 RoutingNode (org.elasticsearch.cluster.routing.RoutingNode)8 RoutingNodes (org.elasticsearch.cluster.routing.RoutingNodes)8