Search in sources :

Example 11 with Index

use of org.opensearch.index.Index in project OpenSearch by opensearch-project.

the class SharedClusterSnapshotRestoreIT method testDeleteSnapshot.

public void testDeleteSnapshot() throws Exception {
    final int numberOfSnapshots = between(5, 15);
    Client client = client();
    Path repo = randomRepoPath();
    createRepository("test-repo", "fs", Settings.builder().put("location", repo).put("compress", false).put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES));
    createIndex("test-idx");
    ensureGreen();
    int[] numberOfFiles = new int[numberOfSnapshots];
    logger.info("--> creating {} snapshots ", numberOfSnapshots);
    for (int i = 0; i < numberOfSnapshots; i++) {
        for (int j = 0; j < 10; j++) {
            index("test-idx", "_doc", Integer.toString(i * 10 + j), "foo", "bar" + i * 10 + j);
        }
        refresh();
        createSnapshot("test-repo", "test-snap-" + i, Collections.singletonList("test-idx"));
        // Store number of files after each snapshot
        numberOfFiles[i] = numberOfFiles(repo);
    }
    assertDocCount("test-idx", 10L * numberOfSnapshots);
    int numberOfFilesBeforeDeletion = numberOfFiles(repo);
    logger.info("--> delete all snapshots except the first one and last one");
    if (randomBoolean()) {
        for (int i = 1; i < numberOfSnapshots - 1; i++) {
            client.admin().cluster().prepareDeleteSnapshot("test-repo", new String[] { "test-snap-" + i }).get();
        }
    } else {
        client.admin().cluster().prepareDeleteSnapshot("test-repo", IntStream.range(1, numberOfSnapshots - 1).mapToObj(i -> "test-snap-" + i).toArray(String[]::new)).get();
    }
    int numberOfFilesAfterDeletion = numberOfFiles(repo);
    assertThat(numberOfFilesAfterDeletion, lessThan(numberOfFilesBeforeDeletion));
    logger.info("--> delete index");
    cluster().wipeIndices("test-idx");
    logger.info("--> restore index");
    String lastSnapshot = "test-snap-" + (numberOfSnapshots - 1);
    RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", lastSnapshot).setWaitForCompletion(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    assertDocCount("test-idx", 10L * numberOfSnapshots);
    startDeleteSnapshot("test-repo", lastSnapshot).get();
    logger.info("--> make sure that number of files is back to what it was when the first snapshot was made");
    assertFileCount(repo, numberOfFiles[0]);
}
Also used : Path(java.nio.file.Path) IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) Arrays(java.util.Arrays) SnapshotIndexShardStatus(org.opensearch.action.admin.cluster.snapshots.status.SnapshotIndexShardStatus) BlobStoreRepository(org.opensearch.repositories.blobstore.BlobStoreRepository) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) Matchers.not(org.hamcrest.Matchers.not) ByteSizeUnit(org.opensearch.common.unit.ByteSizeUnit) SETTING_ALLOCATION_MAX_RETRY(org.opensearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider.SETTING_ALLOCATION_MAX_RETRY) Version(org.opensearch.Version) OpenSearchException(org.opensearch.OpenSearchException) SnapshotsStatusResponse(org.opensearch.action.admin.cluster.snapshots.status.SnapshotsStatusResponse) MapperService(org.opensearch.index.mapper.MapperService) IndexId(org.opensearch.repositories.IndexId) OpenSearchAssertions.assertHitCount(org.opensearch.test.hamcrest.OpenSearchAssertions.assertHitCount) SnapshotStatus(org.opensearch.action.admin.cluster.snapshots.status.SnapshotStatus) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) UnassignedInfo(org.opensearch.cluster.routing.UnassignedInfo) Path(java.nio.file.Path) RepositoryException(org.opensearch.repositories.RepositoryException) OpenSearchAssertions.assertRequestBuilderThrows(org.opensearch.test.hamcrest.OpenSearchAssertions.assertRequestBuilderThrows) Client(org.opensearch.client.Client) EngineTestCase(org.opensearch.index.engine.EngineTestCase) TimeValue(org.opensearch.common.unit.TimeValue) OpenSearchAssertions.assertNoFailures(org.opensearch.test.hamcrest.OpenSearchAssertions.assertNoFailures) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) BytesRef(org.apache.lucene.util.BytesRef) Index(org.opensearch.index.Index) Predicate(java.util.function.Predicate) IndicesService(org.opensearch.indices.IndicesService) StandardOpenOption(java.nio.file.StandardOpenOption) ExceptionsHelper(org.opensearch.ExceptionsHelper) SnapshotIndexShardStage(org.opensearch.action.admin.cluster.snapshots.status.SnapshotIndexShardStage) Settings(org.opensearch.common.settings.Settings) MockRepository(org.opensearch.snapshots.mockstore.MockRepository) Engine(org.opensearch.index.engine.Engine) SeekableByteChannel(java.nio.channels.SeekableByteChannel) List(java.util.List) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Matchers.equalTo(org.hamcrest.Matchers.equalTo) IndicesStatsResponse(org.opensearch.action.admin.indices.stats.IndicesStatsResponse) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) State(org.opensearch.cluster.SnapshotsInProgress.State) Matchers.anyOf(org.hamcrest.Matchers.anyOf) Matchers.containsString(org.hamcrest.Matchers.containsString) Matchers.endsWith(org.hamcrest.Matchers.endsWith) INDICES_RECOVERY_MAX_BYTES_PER_SEC_SETTING(org.opensearch.indices.recovery.RecoverySettings.INDICES_RECOVERY_MAX_BYTES_PER_SEC_SETTING) FlushResponse(org.opensearch.action.admin.indices.flush.FlushResponse) RepositoriesService(org.opensearch.repositories.RepositoriesService) IntStream(java.util.stream.IntStream) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) SnapshotIndexStatus(org.opensearch.action.admin.cluster.snapshots.status.SnapshotIndexStatus) ThreadPool(org.opensearch.threadpool.ThreadPool) HashMap(java.util.HashMap) IndicesOptions(org.opensearch.action.support.IndicesOptions) ArrayList(java.util.ArrayList) RecoverySource(org.opensearch.cluster.routing.RecoverySource) RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) ClusterState(org.opensearch.cluster.ClusterState) IndexShard(org.opensearch.index.shard.IndexShard) Numbers(org.opensearch.common.Numbers) ShardRoutingState(org.opensearch.cluster.routing.ShardRoutingState) Matchers.lessThan(org.hamcrest.Matchers.lessThan) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MetadataIndexStateService(org.opensearch.cluster.metadata.MetadataIndexStateService) OpenSearchAssertions.assertAcked(org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked) RepositoryData(org.opensearch.repositories.RepositoryData) CreateSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) Matchers.empty(org.hamcrest.Matchers.empty) Files(java.nio.file.Files) IndexService(org.opensearch.index.IndexService) ActionFuture(org.opensearch.action.ActionFuture) ActiveShardCount(org.opensearch.action.support.ActiveShardCount) ShardRouting(org.opensearch.cluster.routing.ShardRouting) ShardId(org.opensearch.index.shard.ShardId) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) OpenSearchAssertions.assertAllSuccessful(org.opensearch.test.hamcrest.OpenSearchAssertions.assertAllSuccessful) ClusterService(org.opensearch.cluster.service.ClusterService) RestoreInProgress(org.opensearch.cluster.RestoreInProgress) SETTING_NUMBER_OF_SHARDS(org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SHARDS) ShardStats(org.opensearch.action.admin.indices.stats.ShardStats) GetSnapshotsResponse(org.opensearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse) Collections(java.util.Collections) IndexShardTests.getEngineFromShard(org.opensearch.index.shard.IndexShardTests.getEngineFromShard) Matchers.containsString(org.hamcrest.Matchers.containsString) Client(org.opensearch.client.Client) RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Example 12 with Index

use of org.opensearch.index.Index in project OpenSearch by opensearch-project.

the class DocWriteResponse method parseInnerToXContent.

/**
 * Parse the output of the {@link #innerToXContent(XContentBuilder, Params)} method.
 *
 * This method is intended to be called by subclasses and must be called multiple times to parse all the information concerning
 * {@link DocWriteResponse} objects. It always parses the current token, updates the given parsing context accordingly
 * if needed and then immediately returns.
 */
protected static void parseInnerToXContent(XContentParser parser, Builder context) throws IOException {
    XContentParser.Token token = parser.currentToken();
    ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser);
    String currentFieldName = parser.currentName();
    token = parser.nextToken();
    if (token.isValue()) {
        if (_INDEX.equals(currentFieldName)) {
            // index uuid and shard id are unknown and can't be parsed back for now.
            context.setShardId(new ShardId(new Index(parser.text(), IndexMetadata.INDEX_UUID_NA_VALUE), -1));
        } else if (_ID.equals(currentFieldName)) {
            context.setId(parser.text());
        } else if (_VERSION.equals(currentFieldName)) {
            context.setVersion(parser.longValue());
        } else if (RESULT.equals(currentFieldName)) {
            String result = parser.text();
            for (Result r : Result.values()) {
                if (r.getLowercase().equals(result)) {
                    context.setResult(r);
                    break;
                }
            }
        } else if (FORCED_REFRESH.equals(currentFieldName)) {
            context.setForcedRefresh(parser.booleanValue());
        } else if (_SEQ_NO.equals(currentFieldName)) {
            context.setSeqNo(parser.longValue());
        } else if (_PRIMARY_TERM.equals(currentFieldName)) {
            context.setPrimaryTerm(parser.longValue());
        }
    } else if (token == XContentParser.Token.START_OBJECT) {
        if (_SHARDS.equals(currentFieldName)) {
            context.setShardInfo(ShardInfo.fromXContent(parser));
        } else {
            // skip potential inner objects for forward compatibility
            parser.skipChildren();
        }
    } else if (token == XContentParser.Token.START_ARRAY) {
        // skip potential inner arrays for forward compatibility
        parser.skipChildren();
    }
}
Also used : ShardId(org.opensearch.index.shard.ShardId) Index(org.opensearch.index.Index) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 13 with Index

use of org.opensearch.index.Index in project OpenSearch by opensearch-project.

the class TransportBulkAction method doInternalExecute.

protected void doInternalExecute(Task task, BulkRequest bulkRequest, String executorName, ActionListener<BulkResponse> listener) {
    final long startTime = relativeTime();
    final AtomicArray<BulkItemResponse> responses = new AtomicArray<>(bulkRequest.requests.size());
    boolean hasIndexRequestsWithPipelines = false;
    final Metadata metadata = clusterService.state().getMetadata();
    final Version minNodeVersion = clusterService.state().getNodes().getMinNodeVersion();
    for (DocWriteRequest<?> actionRequest : bulkRequest.requests) {
        IndexRequest indexRequest = getIndexWriteRequest(actionRequest);
        if (indexRequest != null) {
            // Each index request needs to be evaluated, because this method also modifies the IndexRequest
            boolean indexRequestHasPipeline = IngestService.resolvePipelines(actionRequest, indexRequest, metadata);
            hasIndexRequestsWithPipelines |= indexRequestHasPipeline;
        }
        if (actionRequest instanceof IndexRequest) {
            IndexRequest ir = (IndexRequest) actionRequest;
            ir.checkAutoIdWithOpTypeCreateSupportedByVersion(minNodeVersion);
            if (ir.getAutoGeneratedTimestamp() != IndexRequest.UNSET_AUTO_GENERATED_TIMESTAMP) {
                throw new IllegalArgumentException("autoGeneratedTimestamp should not be set externally");
            }
        }
    }
    if (hasIndexRequestsWithPipelines) {
        // this path is never taken.
        try {
            if (Assertions.ENABLED) {
                final boolean arePipelinesResolved = bulkRequest.requests().stream().map(TransportBulkAction::getIndexWriteRequest).filter(Objects::nonNull).allMatch(IndexRequest::isPipelineResolved);
                assert arePipelinesResolved : bulkRequest;
            }
            if (clusterService.localNode().isIngestNode()) {
                processBulkIndexIngestRequest(task, bulkRequest, executorName, listener);
            } else {
                ingestForwarder.forwardIngestRequest(BulkAction.INSTANCE, bulkRequest, listener);
            }
        } catch (Exception e) {
            listener.onFailure(e);
        }
        return;
    }
    final boolean includesSystem = includesSystem(bulkRequest, clusterService.state().metadata().getIndicesLookup(), systemIndices);
    if (includesSystem || needToCheck()) {
        // Attempt to create all the indices that we're going to need during the bulk before we start.
        // Step 1: collect all the indices in the request
        final Map<String, Boolean> indices = bulkRequest.requests.stream().filter(request -> request.opType() != DocWriteRequest.OpType.DELETE || request.versionType() == VersionType.EXTERNAL || request.versionType() == VersionType.EXTERNAL_GTE).collect(Collectors.toMap(DocWriteRequest::index, DocWriteRequest::isRequireAlias, (v1, v2) -> v1 || v2));
        /* Step 2: filter that to indices that don't exist and we can create. At the same time build a map of indices we can't create
             * that we'll use when we try to run the requests. */
        final Map<String, IndexNotFoundException> indicesThatCannotBeCreated = new HashMap<>();
        Set<String> autoCreateIndices = new HashSet<>();
        ClusterState state = clusterService.state();
        for (Map.Entry<String, Boolean> indexAndFlag : indices.entrySet()) {
            boolean shouldAutoCreate;
            final String index = indexAndFlag.getKey();
            try {
                shouldAutoCreate = shouldAutoCreate(index, state);
            } catch (IndexNotFoundException e) {
                shouldAutoCreate = false;
                indicesThatCannotBeCreated.put(index, e);
            }
            // We should only auto create if we are not requiring it to be an alias
            if (shouldAutoCreate && (indexAndFlag.getValue() == false)) {
                autoCreateIndices.add(index);
            }
        }
        // Step 3: create all the indices that are missing, if there are any missing. start the bulk after all the creates come back.
        if (autoCreateIndices.isEmpty()) {
            executeBulk(task, bulkRequest, startTime, listener, responses, indicesThatCannotBeCreated);
        } else {
            final AtomicInteger counter = new AtomicInteger(autoCreateIndices.size());
            for (String index : autoCreateIndices) {
                createIndex(index, bulkRequest.timeout(), minNodeVersion, new ActionListener<CreateIndexResponse>() {

                    @Override
                    public void onResponse(CreateIndexResponse result) {
                        if (counter.decrementAndGet() == 0) {
                            threadPool.executor(executorName).execute(new ActionRunnable<BulkResponse>(listener) {

                                @Override
                                protected void doRun() {
                                    executeBulk(task, bulkRequest, startTime, listener, responses, indicesThatCannotBeCreated);
                                }
                            });
                        }
                    }

                    @Override
                    public void onFailure(Exception e) {
                        if (!(ExceptionsHelper.unwrapCause(e) instanceof ResourceAlreadyExistsException)) {
                            // fail all requests involving this index, if create didn't work
                            for (int i = 0; i < bulkRequest.requests.size(); i++) {
                                DocWriteRequest<?> request = bulkRequest.requests.get(i);
                                if (request != null && setResponseFailureIfIndexMatches(responses, i, request, index, e)) {
                                    bulkRequest.requests.set(i, null);
                                }
                            }
                        }
                        if (counter.decrementAndGet() == 0) {
                            final ActionListener<BulkResponse> wrappedListener = ActionListener.wrap(listener::onResponse, inner -> {
                                inner.addSuppressed(e);
                                listener.onFailure(inner);
                            });
                            threadPool.executor(executorName).execute(new ActionRunnable<BulkResponse>(wrappedListener) {

                                @Override
                                protected void doRun() {
                                    executeBulk(task, bulkRequest, startTime, wrappedListener, responses, indicesThatCannotBeCreated);
                                }

                                @Override
                                public void onRejection(Exception rejectedException) {
                                    rejectedException.addSuppressed(e);
                                    super.onRejection(rejectedException);
                                }
                            });
                        }
                    }
                });
            }
        }
    } else {
        executeBulk(task, bulkRequest, startTime, listener, responses, emptyMap());
    }
}
Also used : SequenceNumbers(org.opensearch.index.seqno.SequenceNumbers) IndexAbstraction(org.opensearch.cluster.metadata.IndexAbstraction) Metadata(org.opensearch.cluster.metadata.Metadata) LongSupplier(java.util.function.LongSupplier) DataStream(org.opensearch.cluster.metadata.DataStream) Version(org.opensearch.Version) TransportUpdateAction(org.opensearch.action.update.TransportUpdateAction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) Assertions(org.opensearch.Assertions) Map(java.util.Map) NodeClosedException(org.opensearch.node.NodeClosedException) AutoCreateAction(org.opensearch.action.admin.indices.create.AutoCreateAction) Inject(org.opensearch.common.inject.Inject) ActionListener(org.opensearch.action.ActionListener) AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) CreateIndexRequest(org.opensearch.action.admin.indices.create.CreateIndexRequest) TimeValue(org.opensearch.common.unit.TimeValue) NodeClient(org.opensearch.client.node.NodeClient) Index(org.opensearch.index.Index) IndexingPressureService(org.opensearch.index.IndexingPressureService) OpenSearchParseException(org.opensearch.OpenSearchParseException) ExceptionsHelper(org.opensearch.ExceptionsHelper) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) Set(java.util.Set) Task(org.opensearch.tasks.Task) TransportService(org.opensearch.transport.TransportService) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) ActionFilters(org.opensearch.action.support.ActionFilters) VersionType(org.opensearch.index.VersionType) List(java.util.List) Logger(org.apache.logging.log4j.Logger) SparseFixedBitSet(org.apache.lucene.util.SparseFixedBitSet) EXCLUDED_DATA_STREAMS_KEY(org.opensearch.cluster.metadata.IndexNameExpressionResolver.EXCLUDED_DATA_STREAMS_KEY) ResourceAlreadyExistsException(org.opensearch.ResourceAlreadyExistsException) DocWriteResponse(org.opensearch.action.DocWriteResponse) UpdateRequest(org.opensearch.action.update.UpdateRequest) SortedMap(java.util.SortedMap) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) Names(org.opensearch.threadpool.ThreadPool.Names) MappingMetadata(org.opensearch.cluster.metadata.MappingMetadata) HandledTransportAction(org.opensearch.action.support.HandledTransportAction) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ActionRunnable(org.opensearch.action.ActionRunnable) UpdateResponse(org.opensearch.action.update.UpdateResponse) ThreadPool(org.opensearch.threadpool.ThreadPool) RoutingMissingException(org.opensearch.action.RoutingMissingException) DocWriteRequest(org.opensearch.action.DocWriteRequest) HashMap(java.util.HashMap) Releasable(org.opensearch.common.lease.Releasable) ArrayList(java.util.ArrayList) AutoCreateIndex(org.opensearch.action.support.AutoCreateIndex) HashSet(java.util.HashSet) ClusterState(org.opensearch.cluster.ClusterState) UNASSIGNED_SEQ_NO(org.opensearch.index.seqno.SequenceNumbers.UNASSIGNED_SEQ_NO) LegacyESVersion(org.opensearch.LegacyESVersion) IndexClosedException(org.opensearch.indices.IndexClosedException) ClusterStateObserver(org.opensearch.cluster.ClusterStateObserver) Collections.emptyMap(java.util.Collections.emptyMap) IngestService(org.opensearch.ingest.IngestService) Iterator(java.util.Iterator) IngestActionForwarder(org.opensearch.action.ingest.IngestActionForwarder) ClusterBlockLevel(org.opensearch.cluster.block.ClusterBlockLevel) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse) UNASSIGNED_PRIMARY_TERM(org.opensearch.index.seqno.SequenceNumbers.UNASSIGNED_PRIMARY_TERM) ShardId(org.opensearch.index.shard.ShardId) TimeUnit(java.util.concurrent.TimeUnit) SystemIndices(org.opensearch.indices.SystemIndices) AtomicArray(org.opensearch.common.util.concurrent.AtomicArray) ClusterService(org.opensearch.cluster.service.ClusterService) IndexRequest(org.opensearch.action.index.IndexRequest) LogManager(org.apache.logging.log4j.LogManager) AtomicArray(org.opensearch.common.util.concurrent.AtomicArray) ActionRunnable(org.opensearch.action.ActionRunnable) HashMap(java.util.HashMap) Metadata(org.opensearch.cluster.metadata.Metadata) MappingMetadata(org.opensearch.cluster.metadata.MappingMetadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) CreateIndexRequest(org.opensearch.action.admin.indices.create.CreateIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest) Version(org.opensearch.Version) LegacyESVersion(org.opensearch.LegacyESVersion) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse) HashSet(java.util.HashSet) ClusterState(org.opensearch.cluster.ClusterState) ResourceAlreadyExistsException(org.opensearch.ResourceAlreadyExistsException) NodeClosedException(org.opensearch.node.NodeClosedException) OpenSearchParseException(org.opensearch.OpenSearchParseException) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) ResourceAlreadyExistsException(org.opensearch.ResourceAlreadyExistsException) RoutingMissingException(org.opensearch.action.RoutingMissingException) IndexClosedException(org.opensearch.indices.IndexClosedException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) ActionListener(org.opensearch.action.ActionListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) DocWriteRequest(org.opensearch.action.DocWriteRequest) Map(java.util.Map) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) Collections.emptyMap(java.util.Collections.emptyMap)

Example 14 with Index

use of org.opensearch.index.Index 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)

Example 15 with Index

use of org.opensearch.index.Index in project OpenSearch by opensearch-project.

the class Gateway method performStateRecovery.

public void performStateRecovery(final GatewayStateRecoveredListener listener) throws GatewayException {
    final String[] nodesIds = clusterService.state().nodes().getMasterNodes().keys().toArray(String.class);
    logger.trace("performing state recovery from {}", Arrays.toString(nodesIds));
    final TransportNodesListGatewayMetaState.NodesGatewayMetaState nodesState = listGatewayMetaState.list(nodesIds, null).actionGet();
    final int requiredAllocation = 1;
    if (nodesState.hasFailures()) {
        for (final FailedNodeException failedNodeException : nodesState.failures()) {
            logger.warn("failed to fetch state from node", failedNodeException);
        }
    }
    final ObjectFloatHashMap<Index> indices = new ObjectFloatHashMap<>();
    Metadata electedGlobalState = null;
    int found = 0;
    for (final TransportNodesListGatewayMetaState.NodeGatewayMetaState nodeState : nodesState.getNodes()) {
        if (nodeState.metadata() == null) {
            continue;
        }
        found++;
        if (electedGlobalState == null) {
            electedGlobalState = nodeState.metadata();
        } else if (nodeState.metadata().version() > electedGlobalState.version()) {
            electedGlobalState = nodeState.metadata();
        }
        for (final ObjectCursor<IndexMetadata> cursor : nodeState.metadata().indices().values()) {
            indices.addTo(cursor.value.getIndex(), 1);
        }
    }
    if (found < requiredAllocation) {
        listener.onFailure("found [" + found + "] metadata states, required [" + requiredAllocation + "]");
        return;
    }
    // update the global state, and clean the indices, we elect them in the next phase
    final Metadata.Builder metadataBuilder = Metadata.builder(electedGlobalState).removeAllIndices();
    assert !indices.containsKey(null);
    final Object[] keys = indices.keys;
    for (int i = 0; i < keys.length; i++) {
        if (keys[i] != null) {
            final Index index = (Index) keys[i];
            IndexMetadata electedIndexMetadata = null;
            int indexMetadataCount = 0;
            for (final TransportNodesListGatewayMetaState.NodeGatewayMetaState nodeState : nodesState.getNodes()) {
                if (nodeState.metadata() == null) {
                    continue;
                }
                final IndexMetadata indexMetadata = nodeState.metadata().index(index);
                if (indexMetadata == null) {
                    continue;
                }
                if (electedIndexMetadata == null) {
                    electedIndexMetadata = indexMetadata;
                } else if (indexMetadata.getVersion() > electedIndexMetadata.getVersion()) {
                    electedIndexMetadata = indexMetadata;
                }
                indexMetadataCount++;
            }
            if (electedIndexMetadata != null) {
                if (indexMetadataCount < requiredAllocation) {
                    logger.debug("[{}] found [{}], required [{}], not adding", index, indexMetadataCount, requiredAllocation);
                }
                // TODO if this logging statement is correct then we are missing an else here
                metadataBuilder.put(electedIndexMetadata, false);
            }
        }
    }
    ClusterState recoveredState = Function.<ClusterState>identity().andThen(state -> ClusterStateUpdaters.upgradeAndArchiveUnknownOrInvalidSettings(state, clusterService.getClusterSettings())).apply(ClusterState.builder(clusterService.getClusterName()).metadata(metadataBuilder).build());
    listener.onSuccess(recoveredState);
}
Also used : Arrays(java.util.Arrays) FailedNodeException(org.opensearch.action.FailedNodeException) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Index(org.opensearch.index.Index) Settings(org.opensearch.common.settings.Settings) ObjectCursor(com.carrotsearch.hppc.cursors.ObjectCursor) Function(java.util.function.Function) ClusterState(org.opensearch.cluster.ClusterState) Logger(org.apache.logging.log4j.Logger) ClusterService(org.opensearch.cluster.service.ClusterService) LogManager(org.apache.logging.log4j.LogManager) ObjectFloatHashMap(com.carrotsearch.hppc.ObjectFloatHashMap) ClusterState(org.opensearch.cluster.ClusterState) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Index(org.opensearch.index.Index) FailedNodeException(org.opensearch.action.FailedNodeException) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ObjectFloatHashMap(com.carrotsearch.hppc.ObjectFloatHashMap)

Aggregations

Index (org.opensearch.index.Index)432 Settings (org.opensearch.common.settings.Settings)165 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)147 ClusterState (org.opensearch.cluster.ClusterState)143 ShardId (org.opensearch.index.shard.ShardId)140 ArrayList (java.util.ArrayList)101 HashMap (java.util.HashMap)93 IOException (java.io.IOException)92 Map (java.util.Map)88 List (java.util.List)87 HashSet (java.util.HashSet)84 ShardRouting (org.opensearch.cluster.routing.ShardRouting)80 ClusterService (org.opensearch.cluster.service.ClusterService)76 IndicesService (org.opensearch.indices.IndicesService)71 Collections (java.util.Collections)70 Version (org.opensearch.Version)68 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)67 Matchers.containsString (org.hamcrest.Matchers.containsString)66 Set (java.util.Set)63 Metadata (org.opensearch.cluster.metadata.Metadata)63