Search in sources :

Example 41 with IndexNotFoundException

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

the class IndexNameExpressionResolver method concreteIndices.

Index[] concreteIndices(Context context, String... indexExpressions) {
    if (indexExpressions == null || indexExpressions.length == 0) {
        indexExpressions = new String[] { Metadata.ALL };
    }
    Metadata metadata = context.getState().metadata();
    IndicesOptions options = context.getOptions();
    final boolean failClosed = options.forbidClosedIndices() && options.ignoreUnavailable() == false;
    // If only one index is specified then whether we fail a request if an index is missing depends on the allow_no_indices
    // option. At some point we should change this, because there shouldn't be a reason why whether a single index
    // or multiple indices are specified yield different behaviour.
    final boolean failNoIndices = indexExpressions.length == 1 ? !options.allowNoIndices() : !options.ignoreUnavailable();
    List<String> expressions = expressionResolver.resolve(context, Arrays.asList(indexExpressions));
    if (expressions.isEmpty()) {
        if (!options.allowNoIndices()) {
            IndexNotFoundException infe = new IndexNotFoundException((String) null);
            infe.setResources("index_expression", indexExpressions);
            throw infe;
        } else {
            return Index.EMPTY_ARRAY;
        }
    }
    final Set<Index> concreteIndices = new HashSet<>(expressions.size());
    for (String expression : expressions) {
        AliasOrIndex aliasOrIndex = metadata.getAliasAndIndexLookup().get(expression);
        if (aliasOrIndex == null) {
            if (failNoIndices) {
                IndexNotFoundException infe = new IndexNotFoundException(expression);
                infe.setResources("index_expression", expression);
                throw infe;
            } else {
                continue;
            }
        } else if (aliasOrIndex.isAlias() && context.getOptions().ignoreAliases()) {
            if (failNoIndices) {
                throw aliasesNotSupportedException(expression);
            } else {
                continue;
            }
        }
        if (aliasOrIndex.isAlias() && context.isResolveToWriteIndex()) {
            AliasOrIndex.Alias alias = (AliasOrIndex.Alias) aliasOrIndex;
            IndexMetadata writeIndex = alias.getWriteIndex();
            if (writeIndex == null) {
                throw new IllegalArgumentException("no write index is defined for alias [" + alias.getAliasName() + "]." + " The write index may be explicitly disabled using is_write_index=false or the alias points to multiple" + " indices without one being designated as a write index");
            }
            concreteIndices.add(writeIndex.getIndex());
        } else {
            if (aliasOrIndex.getIndices().size() > 1 && !options.allowAliasesToMultipleIndices()) {
                String[] indexNames = new String[aliasOrIndex.getIndices().size()];
                int i = 0;
                for (IndexMetadata indexMetadata : aliasOrIndex.getIndices()) {
                    indexNames[i++] = indexMetadata.getIndex().getName();
                }
                throw new IllegalArgumentException("Alias [" + expression + "] has more than one indices associated with it [" + Arrays.toString(indexNames) + "], can't execute a single index op");
            }
            for (IndexMetadata index : aliasOrIndex.getIndices()) {
                if (index.getState() == IndexMetadata.State.CLOSE) {
                    if (failClosed) {
                        throw new IndexClosedException(index.getIndex());
                    } else {
                        if (options.forbidClosedIndices() == false) {
                            concreteIndices.add(index.getIndex());
                        }
                    }
                } else if (index.getState() == IndexMetadata.State.OPEN) {
                    concreteIndices.add(index.getIndex());
                } else {
                    throw new IllegalStateException("index state [" + index.getState() + "] not supported");
                }
            }
        }
    }
    if (options.allowNoIndices() == false && concreteIndices.isEmpty()) {
        IndexNotFoundException infe = new IndexNotFoundException((String) null);
        infe.setResources("index_expression", indexExpressions);
        throw infe;
    }
    return concreteIndices.toArray(new Index[concreteIndices.size()]);
}
Also used : Index(org.elasticsearch.index.Index) IndexClosedException(org.elasticsearch.indices.IndexClosedException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) HashSet(java.util.HashSet)

Example 42 with IndexNotFoundException

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

the class AllocateReplicaAllocationCommand method execute.

@Override
public RerouteExplanation execute(RoutingAllocation allocation, boolean explain) {
    final DiscoveryNode discoNode;
    try {
        discoNode = allocation.nodes().resolveNode(node);
    } catch (IllegalArgumentException e) {
        return explainOrThrowRejectedCommand(explain, allocation, e);
    }
    final RoutingNodes routingNodes = allocation.routingNodes();
    RoutingNode routingNode = routingNodes.node(discoNode.getId());
    if (routingNode == null) {
        return explainOrThrowMissingRoutingNode(allocation, explain, discoNode);
    }
    try {
        allocation.routingTable().shardRoutingTable(index, shardId).primaryShard();
    } catch (IndexNotFoundException | ShardNotFoundException e) {
        return explainOrThrowRejectedCommand(explain, allocation, e);
    }
    ShardRouting primaryShardRouting = null;
    for (RoutingNode node : allocation.routingNodes()) {
        for (ShardRouting shard : node) {
            if (shard.getIndexName().equals(index) && shard.getId() == shardId && shard.primary()) {
                primaryShardRouting = shard;
                break;
            }
        }
    }
    if (primaryShardRouting == null) {
        return explainOrThrowRejectedCommand(explain, allocation, "trying to allocate a replica shard [" + index + "][" + shardId + "], while corresponding primary shard is still unassigned");
    }
    List<ShardRouting> replicaShardRoutings = new ArrayList<>();
    for (ShardRouting shard : allocation.routingNodes().unassigned()) {
        if (shard.getIndexName().equals(index) && shard.getId() == shardId && shard.primary() == false) {
            replicaShardRoutings.add(shard);
        }
    }
    ShardRouting shardRouting;
    if (replicaShardRoutings.isEmpty()) {
        return explainOrThrowRejectedCommand(explain, allocation, "all copies of [" + index + "][" + shardId + "] are already assigned. Use the move allocation command instead");
    } else {
        shardRouting = replicaShardRoutings.get(0);
    }
    Decision decision = allocation.deciders().canAllocate(shardRouting, routingNode, allocation);
    if (decision.type() == Decision.Type.NO) {
        // don't use explainOrThrowRejectedCommand to keep the original "NO" decision
        if (explain) {
            return new RerouteExplanation(this, decision);
        }
        throw new IllegalArgumentException("[" + name() + "] allocation of [" + index + "][" + shardId + "] on node " + discoNode + " is not allowed, reason: " + decision);
    }
    initializeUnassignedShard(allocation, routingNodes, routingNode, shardRouting);
    return new RerouteExplanation(this, decision);
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) ArrayList(java.util.ArrayList) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) RerouteExplanation(org.elasticsearch.cluster.routing.allocation.RerouteExplanation) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) Decision(org.elasticsearch.cluster.routing.allocation.decider.Decision)

Example 43 with IndexNotFoundException

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

the class SyncedFlushService method getShardRoutingTable.

final IndexShardRoutingTable getShardRoutingTable(ShardId shardId, ClusterState state) {
    final IndexRoutingTable indexRoutingTable = state.routingTable().index(shardId.getIndexName());
    if (indexRoutingTable == null) {
        IndexMetadata index = state.getMetadata().index(shardId.getIndex());
        if (index != null && index.getState() == IndexMetadata.State.CLOSE) {
            throw new IndexClosedException(shardId.getIndex());
        }
        throw new IndexNotFoundException(shardId.getIndexName());
    }
    final IndexShardRoutingTable shardRoutingTable = indexRoutingTable.shard(shardId.id());
    if (shardRoutingTable == null) {
        throw new ShardNotFoundException(shardId);
    }
    return shardRoutingTable;
}
Also used : IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) IndexClosedException(org.elasticsearch.indices.IndexClosedException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata)

Example 44 with IndexNotFoundException

use of org.elasticsearch.index.IndexNotFoundException in project metron by apache.

the class ElasticsearchMetaAlertDaoTest method testUpdateShouldUpdateOnMissingMetaAlertIndex.

@Test
public void testUpdateShouldUpdateOnMissingMetaAlertIndex() throws Exception {
    ElasticsearchDao elasticsearchDao = mock(ElasticsearchDao.class);
    ElasticsearchMetaAlertRetrieveLatestDao elasticsearchMetaAlertRetrieveLatestDao = mock(ElasticsearchMetaAlertRetrieveLatestDao.class);
    MetaAlertConfig metaAlertConfig = mock(MetaAlertConfig.class);
    ElasticsearchMetaAlertUpdateDao emauDao = spy(new ElasticsearchMetaAlertUpdateDao(elasticsearchDao, elasticsearchMetaAlertRetrieveLatestDao, metaAlertConfig, 1));
    doThrow(new IndexNotFoundException(ElasticsearchMetaAlertDao.METAALERTS_INDEX)).when(emauDao).getMetaAlertsForAlert("alert_one");
    Document update = new Document(new HashMap<>(), "alert_one", "", 0L);
    emauDao.update(update, Optional.empty());
    Map<Document, Optional<String>> expectedUpdate = new HashMap<Document, Optional<String>>() {

        {
            put(update, Optional.empty());
        }
    };
    verify(elasticsearchDao).batchUpdate(expectedUpdate);
}
Also used : MetaAlertConfig(org.apache.metron.indexing.dao.metaalert.MetaAlertConfig) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) Document(org.apache.metron.indexing.dao.update.Document) Test(org.junit.jupiter.api.Test)

Example 45 with IndexNotFoundException

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

the class IndexNameExpressionResolver method concreteIndices.

Index[] concreteIndices(Context context, String... indexExpressions) {
    if (indexExpressions == null || indexExpressions.length == 0) {
        indexExpressions = new String[] { MetaData.ALL };
    }
    MetaData metaData = context.getState().metaData();
    IndicesOptions options = context.getOptions();
    boolean failClosed = options.forbidClosedIndices() && options.ignoreUnavailable() == false;
    boolean failNoIndices = options.ignoreUnavailable() == false;
    // or multiple indices are specified yield different behaviour.
    if (indexExpressions.length == 1) {
        failNoIndices = options.allowNoIndices() == false;
    }
    List<String> expressions = Arrays.asList(indexExpressions);
    for (ExpressionResolver expressionResolver : expressionResolvers) {
        expressions = expressionResolver.resolve(context, expressions);
    }
    if (expressions.isEmpty()) {
        if (!options.allowNoIndices()) {
            IndexNotFoundException infe = new IndexNotFoundException((String) null);
            infe.setResources("index_expression", indexExpressions);
            throw infe;
        } else {
            return Index.EMPTY_ARRAY;
        }
    }
    final Set<Index> concreteIndices = new HashSet<>(expressions.size());
    for (String expression : expressions) {
        AliasOrIndex aliasOrIndex = metaData.getAliasAndIndexLookup().get(expression);
        if (aliasOrIndex == null) {
            if (failNoIndices) {
                IndexNotFoundException infe = new IndexNotFoundException(expression);
                infe.setResources("index_expression", expression);
                throw infe;
            } else {
                continue;
            }
        }
        Collection<IndexMetaData> resolvedIndices = aliasOrIndex.getIndices();
        if (resolvedIndices.size() > 1 && !options.allowAliasesToMultipleIndices()) {
            String[] indexNames = new String[resolvedIndices.size()];
            int i = 0;
            for (IndexMetaData indexMetaData : resolvedIndices) {
                indexNames[i++] = indexMetaData.getIndex().getName();
            }
            throw new IllegalArgumentException("Alias [" + expression + "] has more than one indices associated with it [" + Arrays.toString(indexNames) + "], can't execute a single index op");
        }
        for (IndexMetaData index : resolvedIndices) {
            if (index.getState() == IndexMetaData.State.CLOSE) {
                if (failClosed) {
                    throw new IndexClosedException(index.getIndex());
                } else {
                    if (options.forbidClosedIndices() == false) {
                        concreteIndices.add(index.getIndex());
                    }
                }
            } else if (index.getState() == IndexMetaData.State.OPEN) {
                concreteIndices.add(index.getIndex());
            } else {
                throw new IllegalStateException("index state [" + index.getState() + "] not supported");
            }
        }
    }
    if (options.allowNoIndices() == false && concreteIndices.isEmpty()) {
        IndexNotFoundException infe = new IndexNotFoundException((String) null);
        infe.setResources("index_expression", indexExpressions);
        throw infe;
    }
    return concreteIndices.toArray(new Index[concreteIndices.size()]);
}
Also used : Index(org.elasticsearch.index.Index) IndexClosedException(org.elasticsearch.indices.IndexClosedException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) HashSet(java.util.HashSet)

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