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;
}
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();
}
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");
}
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();
}
}
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;
}
}
Aggregations