if(clusterService.state().metaData().index(this.searchguardIndex).mapping("config") != null) {
client.admin().indices().prepareRefresh(index).get(); ImmutableOpenMap<String, MappingMetaData> mappings = client.admin().cluster().prepareState().get().getState().getMetaData() .index(index).mappings(); logger.trace("mappings contains type {}: {}", type, mappings.containsKey(type)); if (mappings.containsKey(type)) {
/** * Returns the average of shards per node for the given index */ public float avgShardsPerNode(String index) { return ((float) metaData.index(index).getTotalNumberOfShards()) / nodes.size(); }
protected IndexMetaData indexMetaData(ClusterState clusterState, String index) { IndexMetaData indexMetaData = clusterState.metaData().index(index); if (indexMetaData == null) { throw new IndexNotFoundException(index); } return indexMetaData; }
public IndexMetaData index(Index index) { IndexMetaData metaData = index(index.getName()); if (metaData != null && metaData.getIndexUUID().equals(index.getUUID())) { return metaData; } return null; }
private int getTotalNewShards(Index index, ClusterState currentState, int updatedNumberOfReplicas) { IndexMetaData indexMetaData = currentState.metaData().index(index); int shardsInIndex = indexMetaData.getNumberOfShards(); int oldNumberOfReplicas = indexMetaData.getNumberOfReplicas(); int replicaIncrease = updatedNumberOfReplicas - oldNumberOfReplicas; return replicaIncrease * shardsInIndex; }
/** * Cleans dangling indices if they are already allocated on the provided meta data. */ void cleanupAllocatedDangledIndices(MetaData metaData) { for (Index index : danglingIndices.keySet()) { final IndexMetaData indexMetaData = metaData.index(index); if (indexMetaData != null && indexMetaData.getIndex().getName().equals(index.getName())) { if (indexMetaData.getIndex().getUUID().equals(index.getUUID()) == false) { logger.warn("[{}] can not be imported as a dangling index, as there is already another index " + "with the same name but a different uuid. local index will be ignored (but not deleted)", index); } else { logger.debug("[{}] no longer dangling (created), removing from dangling list", index); } danglingIndices.remove(index); } } }
private static int getTotalShardCount(ClusterState state, Index index) { IndexMetaData indexMetaData = state.metaData().index(index); return indexMetaData.getNumberOfShards() * (1 + indexMetaData.getNumberOfReplicas()); }
/** * Validates whether a list of indices can be opened without going over the cluster shard limit. Only counts indices which are * currently closed and will be opened, ignores indices which are already open. * * @param currentState The current cluster state. * @param indices The indices which are to be opened. * @param deprecationLogger The logger to use to emit a deprecation warning, if appropriate. * @throws ValidationException If this operation would take the cluster over the limit and enforcement is enabled. */ static void validateShardLimit(ClusterState currentState, Index[] indices, DeprecationLogger deprecationLogger) { int shardsToOpen = Arrays.stream(indices) .filter(index -> currentState.metaData().index(index).getState().equals(IndexMetaData.State.CLOSE)) .mapToInt(index -> getTotalShardCount(currentState, index)) .sum(); Optional<String> error = IndicesService.checkShardLimit(shardsToOpen, currentState, deprecationLogger); if (error.isPresent()) { ValidationException ex = new ValidationException(); ex.addValidationError(error.get()); throw ex; } }
/** * Returns the {@link IndexMetaData} for this index. * @throws IndexNotFoundException if no metadata for this index is found */ public IndexMetaData getIndexSafe(Index index) { IndexMetaData metaData = index(index.getName()); if (metaData != null) { if(metaData.getIndexUUID().equals(index.getUUID())) { return metaData; } throw new IndexNotFoundException(index, new IllegalStateException("index uuid doesn't match expected: [" + index.getUUID() + "] but got: [" + metaData.getIndexUUID() +"]")); } throw new IndexNotFoundException(index); }
/** * Check if any of the indices to be closed are currently being restored from a snapshot and fail closing if such an index * is found as closing an index that is being restored makes the index unusable (it cannot be recovered). */ public static void checkIndexClosing(ClusterState currentState, Set<IndexMetaData> indices) { RestoreInProgress restore = currentState.custom(RestoreInProgress.TYPE); if (restore != null) { Set<Index> indicesToFail = null; for (RestoreInProgress.Entry entry : restore) { for (ObjectObjectCursor<ShardId, RestoreInProgress.ShardRestoreStatus> shard : entry.shards()) { if (!shard.value.state().completed()) { IndexMetaData indexMetaData = currentState.metaData().index(shard.key.getIndex()); if (indexMetaData != null && indices.contains(indexMetaData)) { if (indicesToFail == null) { indicesToFail = new HashSet<>(); } indicesToFail.add(shard.key.getIndex()); } } } } if (indicesToFail != null) { throw new IllegalArgumentException("Cannot close indices that are being restored: " + indicesToFail); } } }
private List<Index> indicesDeletedFromClusterState() { // If the new cluster state has a new cluster UUID, the likely scenario is that a node was elected // master that has had its data directory wiped out, in which case we don't want to delete the indices and lose data; // rather we want to import them as dangling indices instead. So we check here if the cluster UUID differs from the previous // cluster UUID, in which case, we don't want to delete indices that the master erroneously believes shouldn't exist. // See test DiscoveryWithServiceDisruptionsIT.testIndicesDeleted() // See discussion on https://github.com/elastic/elasticsearch/pull/9952 and // https://github.com/elastic/elasticsearch/issues/11665 if (metaDataChanged() == false || isNewCluster()) { return Collections.emptyList(); } List<Index> deleted = null; for (ObjectCursor<IndexMetaData> cursor : previousState.metaData().indices().values()) { IndexMetaData index = cursor.value; IndexMetaData current = state.metaData().index(index.getIndex()); if (current == null) { if (deleted == null) { deleted = new ArrayList<>(); } deleted.add(index.getIndex()); } } return deleted == null ? Collections.<Index>emptyList() : deleted; }
public boolean equalsAliases(MetaData other) { for (ObjectCursor<IndexMetaData> cursor : other.indices().values()) { IndexMetaData otherIndex = cursor.value; IndexMetaData thisIndex = index(otherIndex.getIndex()); if (thisIndex == null) { return false; } if (otherIndex.getAliases().equals(thisIndex.getAliases()) == false) { return false; } } return true; }
public AliasFilter buildAliasFilter(ClusterState state, String index, String... expressions) { /* Being static, parseAliasFilter doesn't have access to whatever guts it needs to parse a query. Instead of passing in a bunch * of dependencies we pass in a function that can perform the parsing. */ CheckedFunction<byte[], QueryBuilder, IOException> filterParser = bytes -> { try (XContentParser parser = XContentFactory.xContent(bytes) .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, bytes)) { return parseInnerQueryBuilder(parser); } }; String[] aliases = indexNameExpressionResolver.filteringAliases(state, index, expressions); IndexMetaData indexMetaData = state.metaData().index(index); return new AliasFilter(ShardSearchRequest.parseAliasFilter(filterParser, indexMetaData, aliases), aliases); }
/** * Deletes an index that is not assigned to this node. This method cleans up all disk folders relating to the index * but does not deal with in-memory structures. For those call {@link #removeIndex(Index, IndexRemovalReason, String)} */ @Override public void deleteUnassignedIndex(String reason, IndexMetaData metaData, ClusterState clusterState) { if (nodeEnv.hasNodeFile()) { String indexName = metaData.getIndex().getName(); try { if (clusterState.metaData().hasIndex(indexName)) { final IndexMetaData index = clusterState.metaData().index(indexName); throw new IllegalStateException("Can't delete unassigned index store for [" + indexName + "] - it's still part of " + "the cluster state [" + index.getIndexUUID() + "] [" + metaData.getIndexUUID() + "]"); } deleteIndexStore(reason, metaData, clusterState); } catch (Exception e) { logger.warn(() -> new ParameterizedMessage("[{}] failed to delete unassigned index (reason [{}])", metaData.getIndex(), reason), e); } } }
/** * Finds the next (closest) delay expiration of an delayed shard in nanoseconds based on current time. * Returns 0 if delay is negative. * Returns -1 if no delayed shard is found. */ public static long findNextDelayedAllocation(long currentNanoTime, ClusterState state) { MetaData metaData = state.metaData(); RoutingTable routingTable = state.routingTable(); long nextDelayNanos = Long.MAX_VALUE; for (ShardRouting shard : routingTable.shardsWithState(ShardRoutingState.UNASSIGNED)) { UnassignedInfo unassignedInfo = shard.unassignedInfo(); if (unassignedInfo.isDelayed()) { Settings indexSettings = metaData.index(shard.index()).getSettings(); // calculate next time to schedule final long newComputedLeftDelayNanos = unassignedInfo.getRemainingDelay(currentNanoTime, indexSettings); if (newComputedLeftDelayNanos < nextDelayNanos) { nextDelayNanos = newComputedLeftDelayNanos; } } } return nextDelayNanos == Long.MAX_VALUE ? -1L : nextDelayNanos; }
/** * Validate the name for an index against some static rules and a cluster state. */ public static void validateIndexName(String index, ClusterState state) { validateIndexOrAliasName(index, InvalidIndexNameException::new); if (!index.toLowerCase(Locale.ROOT).equals(index)) { throw new InvalidIndexNameException(index, "must be lowercase"); } if (state.routingTable().hasIndex(index)) { throw new ResourceAlreadyExistsException(state.routingTable().index(index).getIndex()); } if (state.metaData().hasIndex(index)) { throw new ResourceAlreadyExistsException(state.metaData().index(index).getIndex()); } if (state.metaData().hasAlias(index)) { throw new InvalidIndexNameException(index, "already exists as alias"); } }
private void applyStartedShards(RoutingAllocation routingAllocation, List<ShardRouting> startedShardEntries) { assert startedShardEntries.isEmpty() == false : "non-empty list of started shard entries expected"; RoutingNodes routingNodes = routingAllocation.routingNodes(); for (ShardRouting startedShard : startedShardEntries) { assert startedShard.initializing() : "only initializing shards can be started"; assert routingAllocation.metaData().index(startedShard.shardId().getIndex()) != null : "shard started for unknown index (shard entry: " + startedShard + ")"; assert startedShard == routingNodes.getByAllocationId(startedShard.shardId(), startedShard.allocationId().getId()) : "shard routing to start does not exist in routing table, expected: " + startedShard + " but was: " + routingNodes.getByAllocationId(startedShard.shardId(), startedShard.allocationId().getId()); routingNodes.startShard(logger, startedShard, routingAllocation.changes()); } }
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; }
@Override protected void resolveRequest(ClusterState state, InternalRequest request) { IndexMetaData indexMeta = state.getMetaData().index(request.concreteIndex()); // update the routing (request#index here is possibly an alias) request.request().routing(state.metaData().resolveIndexRouting(request.request().parent(), request.request().routing(), request.request().index())); // Fail fast on the node that received the request. if (request.request().routing() == null && state.getMetaData().routingRequired(request.concreteIndex(), request.request().type())) { throw new RoutingMissingException(request.concreteIndex(), request.request().type(), request.request().id()); } }