use of org.elasticsearch.index.IndexNotFoundException in project elasticsearch by elastic.
the class MetaDataIndexAliasesService method innerExecute.
ClusterState innerExecute(ClusterState currentState, Iterable<AliasAction> actions) {
List<Index> indicesToClose = new ArrayList<>();
Map<String, IndexService> indices = new HashMap<>();
try {
boolean changed = false;
// Gather all the indexes that must be removed first so:
// 1. We don't cause error when attempting to replace an index with a alias of the same name.
// 2. We don't allow removal of aliases from indexes that we're just going to delete anyway. That'd be silly.
Set<Index> indicesToDelete = new HashSet<>();
for (AliasAction action : actions) {
if (action.removeIndex()) {
IndexMetaData index = currentState.metaData().getIndices().get(action.getIndex());
if (index == null) {
throw new IndexNotFoundException(action.getIndex());
}
indicesToDelete.add(index.getIndex());
changed = true;
}
}
// Remove the indexes if there are any to remove
if (changed) {
currentState = deleteIndexService.deleteIndices(currentState, indicesToDelete);
}
MetaData.Builder metadata = MetaData.builder(currentState.metaData());
// Run the remaining alias actions
for (AliasAction action : actions) {
if (action.removeIndex()) {
// Handled above
continue;
}
IndexMetaData index = metadata.get(action.getIndex());
if (index == null) {
throw new IndexNotFoundException(action.getIndex());
}
NewAliasValidator newAliasValidator = (alias, indexRouting, filter) -> {
/* It is important that we look up the index using the metadata builder we are modifying so we can remove an
* index and replace it with an alias. */
Function<String, IndexMetaData> indexLookup = name -> metadata.get(name);
aliasValidator.validateAlias(alias, action.getIndex(), indexRouting, indexLookup);
if (Strings.hasLength(filter)) {
IndexService indexService = indices.get(index.getIndex());
if (indexService == null) {
indexService = indicesService.indexService(index.getIndex());
if (indexService == null) {
// temporarily create the index and add mappings so we can parse the filter
try {
indexService = indicesService.createIndex(index, emptyList(), shardId -> {
});
indicesToClose.add(index.getIndex());
} catch (IOException e) {
throw new ElasticsearchException("Failed to create temporary index for parsing the alias", e);
}
indexService.mapperService().merge(index, MapperService.MergeReason.MAPPING_RECOVERY, false);
}
indices.put(action.getIndex(), indexService);
}
// the context is only used for validation so it's fine to pass fake values for the shard id and the current
// timestamp
aliasValidator.validateAliasFilter(alias, filter, indexService.newQueryShardContext(0, null, () -> 0L), xContentRegistry);
}
};
changed |= action.apply(newAliasValidator, metadata, index);
}
if (changed) {
ClusterState updatedState = ClusterState.builder(currentState).metaData(metadata).build();
// i.e. remove and add the same alias to the same index
if (!updatedState.metaData().equalsAliases(currentState.metaData())) {
return updatedState;
}
}
return currentState;
} finally {
for (Index index : indicesToClose) {
indicesService.removeIndex(index, NO_LONGER_ASSIGNED, "created for alias processing");
}
}
}
use of org.elasticsearch.index.IndexNotFoundException in project elasticsearch by elastic.
the class RoutingTable method shardRoutingTable.
/**
* All shards for the provided index and shard id
* @return All the shard routing entries for the given index and shard id
* @throws IndexNotFoundException if provided index does not exist
* @throws ShardNotFoundException if provided shard id is unknown
*/
public IndexShardRoutingTable shardRoutingTable(String index, int shardId) {
IndexRoutingTable indexRouting = index(index);
if (indexRouting == null) {
throw new IndexNotFoundException(index);
}
IndexShardRoutingTable shard = indexRouting.shard(shardId);
if (shard == null) {
throw new ShardNotFoundException(new ShardId(indexRouting.getIndex(), shardId));
}
return shard;
}
use of org.elasticsearch.index.IndexNotFoundException in project crate by crate.
the class ShardCollectSource method getDocCollectors.
private Collection<CrateCollector.Builder> getDocCollectors(JobCollectContext jobCollectContext, RoutedCollectPhase collectPhase, boolean requiresScroll, Map<String, List<Integer>> indexShards) {
List<CrateCollector.Builder> crateCollectors = new ArrayList<>();
for (Map.Entry<String, List<Integer>> entry : indexShards.entrySet()) {
String indexName = entry.getKey();
try {
indicesService.indexServiceSafe(indexName);
} catch (IndexNotFoundException e) {
if (PartitionName.isPartition(indexName)) {
continue;
}
throw e;
}
for (Integer shardNum : entry.getValue()) {
ShardId shardId = new ShardId(indexName, shardNum);
try {
ShardCollectorProvider shardCollectorProvider = getCollectorProviderSafe(shardId);
CrateCollector.Builder collector = shardCollectorProvider.getCollectorBuilder(collectPhase, requiresScroll, jobCollectContext);
crateCollectors.add(collector);
} catch (ShardNotFoundException | IllegalIndexShardStateException e) {
// and the reader required in the fetchPhase would be missing.
if (Symbols.containsColumn(collectPhase.toCollect(), DocSysColumns.FETCHID)) {
throw e;
}
crateCollectors.add(remoteCollectorFactory.createCollector(shardId.getIndex(), shardId.id(), collectPhase, jobCollectContext.queryPhaseRamAccountingContext()));
} catch (InterruptedException e) {
throw Throwables.propagate(e);
} catch (Throwable t) {
throw new UnhandledServerException(t);
}
}
}
return crateCollectors;
}
use of org.elasticsearch.index.IndexNotFoundException in project crate by crate.
the class ShardCollectSource method createMultiShardScoreDocCollector.
private CrateCollector createMultiShardScoreDocCollector(RoutedCollectPhase collectPhase, BatchConsumer consumer, JobCollectContext jobCollectContext, String localNodeId) {
Map<String, Map<String, List<Integer>>> locations = collectPhase.routing().locations();
SharedShardContexts sharedShardContexts = jobCollectContext.sharedShardContexts();
Map<String, List<Integer>> indexShards = locations.get(localNodeId);
List<OrderedDocCollector> orderedDocCollectors = new ArrayList<>();
for (Map.Entry<String, List<Integer>> entry : indexShards.entrySet()) {
String indexName = entry.getKey();
for (Integer shardNum : entry.getValue()) {
ShardId shardId = new ShardId(indexName, shardNum);
SharedShardContext context = sharedShardContexts.getOrCreateContext(shardId);
try {
ShardCollectorProvider shardCollectorProvider = getCollectorProviderSafe(shardId);
orderedDocCollectors.add(shardCollectorProvider.getOrderedCollector(collectPhase, context, jobCollectContext, consumer.requiresScroll()));
} catch (ShardNotFoundException | IllegalIndexShardStateException e) {
throw e;
} catch (IndexNotFoundException e) {
if (PartitionName.isPartition(indexName)) {
break;
}
throw e;
} catch (Throwable t) {
throw new UnhandledServerException(t);
}
}
}
OrderBy orderBy = collectPhase.orderBy();
assert orderBy != null : "orderBy must not be null";
return BatchIteratorCollectorBridge.newInstance(OrderedLuceneBatchIteratorFactory.newInstance(orderedDocCollectors, collectPhase.toCollect().size(), OrderingByPosition.rowOrdering(OrderByPositionVisitor.orderByPositions(orderBy.orderBySymbols(), collectPhase.toCollect()), orderBy.reverseFlags(), orderBy.nullsFirst()), executor, consumer.requiresScroll()), consumer);
}
use of org.elasticsearch.index.IndexNotFoundException in project crate by crate.
the class InternalCountOperation method count.
@Override
public long count(String index, int shardId, WhereClause whereClause) throws IOException, InterruptedException {
IndexService indexService;
try {
indexService = indicesService.indexServiceSafe(index);
} catch (IndexNotFoundException e) {
if (PartitionName.isPartition(index)) {
return 0L;
}
throw e;
}
IndexShard indexShard = indexService.shardSafe(shardId);
try (Engine.Searcher searcher = indexShard.acquireSearcher("count-operation")) {
LuceneQueryBuilder.Context queryCtx = queryBuilder.convert(whereClause, indexService.mapperService(), indexService.fieldData(), indexService.cache());
if (Thread.interrupted()) {
throw new InterruptedException("thread interrupted during count-operation");
}
return searcher.searcher().count(queryCtx.query());
}
}
Aggregations