Search in sources :

Example 41 with IndexMetadata

use of org.opensearch.cluster.metadata.IndexMetadata in project OpenSearch by opensearch-project.

the class MappingStats method of.

/**
 * Create {@link MappingStats} from the given cluster state.
 */
public static MappingStats of(ClusterState state) {
    Map<String, IndexFeatureStats> fieldTypes = new HashMap<>();
    for (IndexMetadata indexMetadata : state.metadata()) {
        Set<String> indexFieldTypes = new HashSet<>();
        MappingMetadata mappingMetadata = indexMetadata.mapping();
        if (mappingMetadata != null) {
            MappingVisitor.visitMapping(mappingMetadata.getSourceAsMap(), fieldMapping -> {
                String type = null;
                Object typeO = fieldMapping.get("type");
                if (typeO != null) {
                    type = typeO.toString();
                } else if (fieldMapping.containsKey("properties")) {
                    type = "object";
                }
                if (type != null) {
                    IndexFeatureStats stats = fieldTypes.computeIfAbsent(type, IndexFeatureStats::new);
                    stats.count++;
                    if (indexFieldTypes.add(type)) {
                        stats.indexCount++;
                    }
                }
            });
        }
    }
    return new MappingStats(fieldTypes.values());
}
Also used : HashMap(java.util.HashMap) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) MappingMetadata(org.opensearch.cluster.metadata.MappingMetadata) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 42 with IndexMetadata

use of org.opensearch.cluster.metadata.IndexMetadata in project OpenSearch by opensearch-project.

the class TransportSnapshotsStatusAction method snapshotShards.

/**
 * Returns status of shards currently finished snapshots
 * <p>
 * This method is executed on master node and it's complimentary to the
 * {@link SnapshotShardsService#currentSnapshotShards(Snapshot)} because it
 * returns similar information but for already finished snapshots.
 * </p>
 *
 * @param repositoryName  repository name
 * @param snapshotInfo    snapshot info
 * @return map of shard id to snapshot status
 */
private Map<ShardId, IndexShardSnapshotStatus> snapshotShards(final String repositoryName, final RepositoryData repositoryData, final SnapshotInfo snapshotInfo) throws IOException {
    final Repository repository = repositoriesService.repository(repositoryName);
    final Map<ShardId, IndexShardSnapshotStatus> shardStatus = new HashMap<>();
    for (String index : snapshotInfo.indices()) {
        IndexId indexId = repositoryData.resolveIndexId(index);
        IndexMetadata indexMetadata = repository.getSnapshotIndexMetaData(repositoryData, snapshotInfo.snapshotId(), indexId);
        if (indexMetadata != null) {
            int numberOfShards = indexMetadata.getNumberOfShards();
            for (int i = 0; i < numberOfShards; i++) {
                ShardId shardId = new ShardId(indexMetadata.getIndex(), i);
                SnapshotShardFailure shardFailure = findShardFailure(snapshotInfo.shardFailures(), shardId);
                if (shardFailure != null) {
                    shardStatus.put(shardId, IndexShardSnapshotStatus.newFailed(shardFailure.reason()));
                } else {
                    final IndexShardSnapshotStatus shardSnapshotStatus;
                    if (snapshotInfo.state() == SnapshotState.FAILED) {
                        // If the snapshot failed, but the shard's snapshot does
                        // not have an exception, it means that partial snapshots
                        // were disabled and in this case, the shard snapshot will
                        // *not* have any metadata, so attempting to read the shard
                        // snapshot status will throw an exception. Instead, we create
                        // a status for the shard to indicate that the shard snapshot
                        // could not be taken due to partial being set to false.
                        shardSnapshotStatus = IndexShardSnapshotStatus.newFailed("skipped");
                    } else {
                        shardSnapshotStatus = repository.getShardSnapshotStatus(snapshotInfo.snapshotId(), indexId, shardId);
                    }
                    shardStatus.put(shardId, shardSnapshotStatus);
                }
            }
        }
    }
    return unmodifiableMap(shardStatus);
}
Also used : ShardId(org.opensearch.index.shard.ShardId) IndexShardSnapshotStatus(org.opensearch.index.snapshots.IndexShardSnapshotStatus) IndexId(org.opensearch.repositories.IndexId) Repository(org.opensearch.repositories.Repository) SnapshotShardFailure(org.opensearch.snapshots.SnapshotShardFailure) HashMap(java.util.HashMap) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata)

Example 43 with IndexMetadata

use of org.opensearch.cluster.metadata.IndexMetadata in project OpenSearch by opensearch-project.

the class TransportShardBulkAction method executeBulkItemRequest.

/**
 * Executes bulk item requests and handles request execution exceptions.
 * @return {@code true} if request completed on this thread and the listener was invoked, {@code false} if the request triggered
 *                      a mapping update that will finish and invoke the listener on a different thread
 */
static boolean executeBulkItemRequest(BulkPrimaryExecutionContext context, UpdateHelper updateHelper, LongSupplier nowInMillisSupplier, MappingUpdatePerformer mappingUpdater, Consumer<ActionListener<Void>> waitForMappingUpdate, ActionListener<Void> itemDoneListener) throws Exception {
    final DocWriteRequest.OpType opType = context.getCurrent().opType();
    final UpdateHelper.Result updateResult;
    if (opType == DocWriteRequest.OpType.UPDATE) {
        final UpdateRequest updateRequest = (UpdateRequest) context.getCurrent();
        try {
            updateResult = updateHelper.prepare(updateRequest, context.getPrimary(), nowInMillisSupplier);
        } catch (Exception failure) {
            // we may fail translating a update to index or delete operation
            // we use index result to communicate failure while translating update request
            final Engine.Result result = new Engine.IndexResult(failure, updateRequest.version());
            context.setRequestToExecute(updateRequest);
            context.markOperationAsExecuted(result);
            context.markAsCompleted(context.getExecutionResult());
            return true;
        }
        // execute translated update request
        switch(updateResult.getResponseResult()) {
            case CREATED:
            case UPDATED:
                IndexRequest indexRequest = updateResult.action();
                IndexMetadata metadata = context.getPrimary().indexSettings().getIndexMetadata();
                MappingMetadata mappingMd = metadata.mapping();
                indexRequest.process(metadata.getCreationVersion(), mappingMd, updateRequest.concreteIndex());
                context.setRequestToExecute(indexRequest);
                break;
            case DELETED:
                context.setRequestToExecute(updateResult.action());
                break;
            case NOOP:
                context.markOperationAsNoOp(updateResult.action());
                context.markAsCompleted(context.getExecutionResult());
                return true;
            default:
                throw new IllegalStateException("Illegal update operation " + updateResult.getResponseResult());
        }
    } else {
        context.setRequestToExecute(context.getCurrent());
        updateResult = null;
    }
    // also checks that we're in TRANSLATED state
    assert context.getRequestToExecute() != null;
    final IndexShard primary = context.getPrimary();
    final long version = context.getRequestToExecute().version();
    final boolean isDelete = context.getRequestToExecute().opType() == DocWriteRequest.OpType.DELETE;
    final Engine.Result result;
    if (isDelete) {
        final DeleteRequest request = context.getRequestToExecute();
        result = primary.applyDeleteOperationOnPrimary(version, MapperService.SINGLE_MAPPING_NAME, request.id(), request.versionType(), request.ifSeqNo(), request.ifPrimaryTerm());
    } else {
        final IndexRequest request = context.getRequestToExecute();
        result = primary.applyIndexOperationOnPrimary(version, request.versionType(), new SourceToParse(request.index(), MapperService.SINGLE_MAPPING_NAME, request.id(), request.source(), request.getContentType(), request.routing()), request.ifSeqNo(), request.ifPrimaryTerm(), request.getAutoGeneratedTimestamp(), request.isRetry());
    }
    if (result.getResultType() == Engine.Result.Type.MAPPING_UPDATE_REQUIRED) {
        try {
            primary.mapperService().merge(MapperService.SINGLE_MAPPING_NAME, new CompressedXContent(result.getRequiredMappingUpdate(), ToXContent.EMPTY_PARAMS), MapperService.MergeReason.MAPPING_UPDATE_PREFLIGHT);
        } catch (Exception e) {
            logger.info(() -> new ParameterizedMessage("{} mapping update rejected by primary", primary.shardId()), e);
            onComplete(exceptionToResult(e, primary, isDelete, version), context, updateResult);
            return true;
        }
        mappingUpdater.updateMappings(result.getRequiredMappingUpdate(), primary.shardId(), new ActionListener<Void>() {

            @Override
            public void onResponse(Void v) {
                context.markAsRequiringMappingUpdate();
                waitForMappingUpdate.accept(ActionListener.runAfter(new ActionListener<Void>() {

                    @Override
                    public void onResponse(Void v) {
                        assert context.requiresWaitingForMappingUpdate();
                        context.resetForExecutionForRetry();
                    }

                    @Override
                    public void onFailure(Exception e) {
                        context.failOnMappingUpdate(e);
                    }
                }, () -> itemDoneListener.onResponse(null)));
            }

            @Override
            public void onFailure(Exception e) {
                onComplete(exceptionToResult(e, primary, isDelete, version), context, updateResult);
                // Requesting mapping update failed, so we don't have to wait for a cluster state update
                assert context.isInitial();
                itemDoneListener.onResponse(null);
            }
        });
        return false;
    } else {
        onComplete(result, context, updateResult);
    }
    return true;
}
Also used : UpdateRequest(org.opensearch.action.update.UpdateRequest) IndexShard(org.opensearch.index.shard.IndexShard) SourceToParse(org.opensearch.index.mapper.SourceToParse) IndexRequest(org.opensearch.action.index.IndexRequest) NodeClosedException(org.opensearch.node.NodeClosedException) MapperException(org.opensearch.index.mapper.MapperException) VersionConflictEngineException(org.opensearch.index.engine.VersionConflictEngineException) IOException(java.io.IOException) GetResult(org.opensearch.index.get.GetResult) UpdateHelper(org.opensearch.action.update.UpdateHelper) CompressedXContent(org.opensearch.common.compress.CompressedXContent) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) DocWriteRequest(org.opensearch.action.DocWriteRequest) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) MappingMetadata(org.opensearch.cluster.metadata.MappingMetadata) DeleteRequest(org.opensearch.action.delete.DeleteRequest) Engine(org.opensearch.index.engine.Engine)

Example 44 with IndexMetadata

use of org.opensearch.cluster.metadata.IndexMetadata in project OpenSearch by opensearch-project.

the class TransportSimulateIndexTemplateAction method resolveTemporaryState.

/**
 * Return a temporary cluster state with an index that exists using the
 * matched template's settings
 */
public static ClusterState resolveTemporaryState(final String matchingTemplate, final String indexName, final ClusterState simulatedState) {
    Settings settings = resolveSettings(simulatedState.metadata(), matchingTemplate);
    // create the index with dummy settings in the cluster state so we can parse and validate the aliases
    Settings dummySettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(settings).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()).build();
    final IndexMetadata indexMetadata = IndexMetadata.builder(indexName).settings(dummySettings).build();
    return ClusterState.builder(simulatedState).metadata(Metadata.builder(simulatedState.metadata()).put(indexMetadata, true).build()).build();
}
Also used : IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) MetadataIndexTemplateService.resolveSettings(org.opensearch.cluster.metadata.MetadataIndexTemplateService.resolveSettings) Settings(org.opensearch.common.settings.Settings)

Example 45 with IndexMetadata

use of org.opensearch.cluster.metadata.IndexMetadata in project OpenSearch by opensearch-project.

the class ClusterChangedEvent method indicesDeletedFromClusterState.

// Get the deleted indices by comparing the index metadatas in the previous and new cluster states.
// If an index exists in the previous cluster state, but not in the new cluster state, it must have been deleted.
private List<Index> indicesDeletedFromClusterState() {
    // https://github.com/elastic/elasticsearch/issues/11665
    if (metadataChanged() == false || isNewCluster()) {
        return Collections.emptyList();
    }
    Set<Index> deleted = null;
    final Metadata previousMetadata = previousState.metadata();
    final Metadata currentMetadata = state.metadata();
    for (ObjectCursor<IndexMetadata> cursor : previousMetadata.indices().values()) {
        IndexMetadata index = cursor.value;
        IndexMetadata current = currentMetadata.index(index.getIndex());
        if (current == null) {
            if (deleted == null) {
                deleted = new HashSet<>();
            }
            deleted.add(index.getIndex());
        }
    }
    final IndexGraveyard currentGraveyard = currentMetadata.indexGraveyard();
    final IndexGraveyard previousGraveyard = previousMetadata.indexGraveyard();
    // each node should make sure to delete any related data.
    if (currentGraveyard != previousGraveyard) {
        final IndexGraveyardDiff indexGraveyardDiff = (IndexGraveyardDiff) currentGraveyard.diff(previousGraveyard);
        final List<IndexGraveyard.Tombstone> added = indexGraveyardDiff.getAdded();
        if (added.isEmpty() == false) {
            if (deleted == null) {
                deleted = new HashSet<>();
            }
            for (IndexGraveyard.Tombstone tombstone : added) {
                deleted.add(tombstone.getIndex());
            }
        }
    }
    return deleted == null ? Collections.<Index>emptyList() : new ArrayList<>(deleted);
}
Also used : IndexGraveyardDiff(org.opensearch.cluster.metadata.IndexGraveyard.IndexGraveyardDiff) IndexGraveyard(org.opensearch.cluster.metadata.IndexGraveyard) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Index(org.opensearch.index.Index) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata)

Aggregations

IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)403 ClusterState (org.opensearch.cluster.ClusterState)144 Metadata (org.opensearch.cluster.metadata.Metadata)126 Settings (org.opensearch.common.settings.Settings)125 IndexSettings (org.opensearch.index.IndexSettings)87 Index (org.opensearch.index.Index)80 ShardRouting (org.opensearch.cluster.routing.ShardRouting)65 ShardId (org.opensearch.index.shard.ShardId)61 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)53 IOException (java.io.IOException)49 ArrayList (java.util.ArrayList)49 HashMap (java.util.HashMap)49 HashSet (java.util.HashSet)48 Matchers.containsString (org.hamcrest.Matchers.containsString)47 IndexShardRoutingTable (org.opensearch.cluster.routing.IndexShardRoutingTable)46 RoutingTable (org.opensearch.cluster.routing.RoutingTable)46 Map (java.util.Map)42 ClusterService (org.opensearch.cluster.service.ClusterService)40 List (java.util.List)38 ActionListener (org.opensearch.action.ActionListener)35