Search in sources :

Example 6 with IndexTemplateMetadata

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

the class TransportBulkActionIngestTests method testFindDefaultPipelineFromTemplateMatch.

public void testFindDefaultPipelineFromTemplateMatch() {
    Exception exception = new Exception("fake exception");
    ClusterState state = clusterService.state();
    ImmutableOpenMap.Builder<String, IndexTemplateMetadata> templateMetadataBuilder = ImmutableOpenMap.builder();
    templateMetadataBuilder.put("template1", IndexTemplateMetadata.builder("template1").patterns(Arrays.asList("missing_index")).order(1).settings(Settings.builder().put(IndexSettings.DEFAULT_PIPELINE.getKey(), "pipeline1").build()).build());
    templateMetadataBuilder.put("template2", IndexTemplateMetadata.builder("template2").patterns(Arrays.asList("missing_*")).order(2).settings(Settings.builder().put(IndexSettings.DEFAULT_PIPELINE.getKey(), "pipeline2").build()).build());
    templateMetadataBuilder.put("template3", IndexTemplateMetadata.builder("template3").patterns(Arrays.asList("missing*")).order(3).build());
    templateMetadataBuilder.put("template4", IndexTemplateMetadata.builder("template4").patterns(Arrays.asList("nope")).order(4).settings(Settings.builder().put(IndexSettings.DEFAULT_PIPELINE.getKey(), "pipeline4").build()).build());
    Metadata metadata = mock(Metadata.class);
    when(state.metadata()).thenReturn(metadata);
    when(state.getMetadata()).thenReturn(metadata);
    when(metadata.templates()).thenReturn(templateMetadataBuilder.build());
    when(metadata.getTemplates()).thenReturn(templateMetadataBuilder.build());
    when(metadata.indices()).thenReturn(ImmutableOpenMap.of());
    IndexRequest indexRequest = new IndexRequest("missing_index").id("id");
    indexRequest.source(emptyMap());
    AtomicBoolean responseCalled = new AtomicBoolean(false);
    AtomicBoolean failureCalled = new AtomicBoolean(false);
    singleItemBulkWriteAction.execute(null, indexRequest, ActionListener.wrap(response -> responseCalled.set(true), e -> {
        assertThat(e, sameInstance(exception));
        failureCalled.set(true);
    }));
    assertEquals("pipeline2", indexRequest.getPipeline());
    verify(ingestService).executeBulkRequest(eq(1), bulkDocsItr.capture(), failureHandler.capture(), completionHandler.capture(), any(), eq(Names.WRITE));
}
Also used : ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) Arrays(java.util.Arrays) Metadata(org.opensearch.cluster.metadata.Metadata) IndexResponse(org.opensearch.action.index.IndexResponse) Version(org.opensearch.Version) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) ClusterStateApplier(org.opensearch.cluster.ClusterStateApplier) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ComposableIndexTemplate(org.opensearch.cluster.metadata.ComposableIndexTemplate) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ActionListener(org.opensearch.action.ActionListener) TimeValue(org.opensearch.common.unit.TimeValue) IndexingPressureService(org.opensearch.index.IndexingPressureService) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Settings(org.opensearch.common.settings.Settings) Task(org.opensearch.tasks.Task) TransportService(org.opensearch.transport.TransportService) Nullable(org.opensearch.common.Nullable) ActionFilters(org.opensearch.action.support.ActionFilters) ActionTestUtils(org.opensearch.action.support.ActionTestUtils) IndexSettings(org.opensearch.index.IndexSettings) IndexAction(org.opensearch.action.index.IndexAction) UpdateRequest(org.opensearch.action.update.UpdateRequest) Mockito.any(org.mockito.Mockito.any) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) Names(org.opensearch.threadpool.ThreadPool.Names) Mockito.eq(org.mockito.Mockito.eq) Mockito.mock(org.mockito.Mockito.mock) MapBuilder(org.opensearch.common.collect.MapBuilder) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ThreadPool(org.opensearch.threadpool.ThreadPool) DocWriteRequest(org.opensearch.action.DocWriteRequest) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Template(org.opensearch.cluster.metadata.Template) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) OpenSearchExecutors(org.opensearch.common.util.concurrent.OpenSearchExecutors) AutoCreateIndex(org.opensearch.action.support.AutoCreateIndex) ClusterState(org.opensearch.cluster.ClusterState) ArgumentCaptor(org.mockito.ArgumentCaptor) VersionUtils(org.opensearch.test.VersionUtils) BiConsumer(java.util.function.BiConsumer) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Mockito.anyString(org.mockito.Mockito.anyString) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) Collections.emptyMap(java.util.Collections.emptyMap) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) IngestService(org.opensearch.ingest.IngestService) Iterator(java.util.Iterator) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) TransportResponseHandler(org.opensearch.transport.TransportResponseHandler) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) SystemIndices(org.opensearch.indices.SystemIndices) Mockito.never(org.mockito.Mockito.never) AtomicArray(org.opensearch.common.util.concurrent.AtomicArray) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) ClusterService(org.opensearch.cluster.service.ClusterService) Mockito.anyInt(org.mockito.Mockito.anyInt) IndexRequest(org.opensearch.action.index.IndexRequest) Collections(java.util.Collections) Mockito.reset(org.mockito.Mockito.reset) ClusterChangedEvent(org.opensearch.cluster.ClusterChangedEvent) ClusterState(org.opensearch.cluster.ClusterState) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.anyString(org.mockito.Mockito.anyString) ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) IndexRequest(org.opensearch.action.index.IndexRequest) IndexNotFoundException(org.opensearch.index.IndexNotFoundException)

Example 7 with IndexTemplateMetadata

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

the class RestTemplatesAction method buildTable.

private Table buildTable(RestRequest request, ClusterStateResponse clusterStateResponse, String patternString) {
    Table table = getTableWithHeader(request);
    Metadata metadata = clusterStateResponse.getState().metadata();
    for (ObjectObjectCursor<String, IndexTemplateMetadata> entry : metadata.templates()) {
        IndexTemplateMetadata indexData = entry.value;
        if (patternString == null || Regex.simpleMatch(patternString, indexData.name())) {
            table.startRow();
            table.addCell(indexData.name());
            table.addCell("[" + String.join(", ", indexData.patterns()) + "]");
            table.addCell(indexData.getOrder());
            table.addCell(indexData.getVersion());
            table.addCell("");
            table.endRow();
        }
    }
    for (Map.Entry<String, ComposableIndexTemplate> entry : metadata.templatesV2().entrySet()) {
        String name = entry.getKey();
        ComposableIndexTemplate template = entry.getValue();
        if (patternString == null || Regex.simpleMatch(patternString, name)) {
            table.startRow();
            table.addCell(name);
            table.addCell("[" + String.join(", ", template.indexPatterns()) + "]");
            table.addCell(template.priorityOrZero());
            table.addCell(template.version());
            table.addCell("[" + String.join(", ", template.composedOf()) + "]");
            table.endRow();
        }
    }
    return table;
}
Also used : ComposableIndexTemplate(org.opensearch.cluster.metadata.ComposableIndexTemplate) Table(org.opensearch.common.Table) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) Metadata(org.opensearch.cluster.metadata.Metadata) Map(java.util.Map)

Example 8 with IndexTemplateMetadata

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

the class RestoreService method restoreSnapshot.

/**
 * Restores snapshot specified in the restore request.
 *
 * @param request  restore request
 * @param listener restore listener
 */
public void restoreSnapshot(final RestoreSnapshotRequest request, final ActionListener<RestoreCompletionResponse> listener) {
    try {
        // Read snapshot info and metadata from the repository
        final String repositoryName = request.repository();
        Repository repository = repositoriesService.repository(repositoryName);
        final StepListener<RepositoryData> repositoryDataListener = new StepListener<>();
        repository.getRepositoryData(repositoryDataListener);
        repositoryDataListener.whenComplete(repositoryData -> {
            final String snapshotName = request.snapshot();
            final Optional<SnapshotId> matchingSnapshotId = repositoryData.getSnapshotIds().stream().filter(s -> snapshotName.equals(s.getName())).findFirst();
            if (matchingSnapshotId.isPresent() == false) {
                throw new SnapshotRestoreException(repositoryName, snapshotName, "snapshot does not exist");
            }
            final SnapshotId snapshotId = matchingSnapshotId.get();
            if (request.snapshotUuid() != null && request.snapshotUuid().equals(snapshotId.getUUID()) == false) {
                throw new SnapshotRestoreException(repositoryName, snapshotName, "snapshot UUID mismatch: expected [" + request.snapshotUuid() + "] but got [" + snapshotId.getUUID() + "]");
            }
            final SnapshotInfo snapshotInfo = repository.getSnapshotInfo(snapshotId);
            final Snapshot snapshot = new Snapshot(repositoryName, snapshotId);
            // Make sure that we can restore from this snapshot
            validateSnapshotRestorable(repositoryName, snapshotInfo);
            Metadata globalMetadata = null;
            // Resolve the indices from the snapshot that need to be restored
            Map<String, DataStream> dataStreams;
            List<String> requestIndices = new ArrayList<>(Arrays.asList(request.indices()));
            List<String> requestedDataStreams = filterIndices(snapshotInfo.dataStreams(), requestIndices.toArray(new String[0]), IndicesOptions.fromOptions(true, true, true, true));
            if (requestedDataStreams.isEmpty()) {
                dataStreams = new HashMap<>();
            } else {
                globalMetadata = repository.getSnapshotGlobalMetadata(snapshotId);
                final Map<String, DataStream> dataStreamsInSnapshot = globalMetadata.dataStreams();
                dataStreams = new HashMap<>(requestedDataStreams.size());
                for (String requestedDataStream : requestedDataStreams) {
                    final DataStream dataStreamInSnapshot = dataStreamsInSnapshot.get(requestedDataStream);
                    assert dataStreamInSnapshot != null : "DataStream [" + requestedDataStream + "] not found in snapshot";
                    dataStreams.put(requestedDataStream, dataStreamInSnapshot);
                }
            }
            requestIndices.removeAll(dataStreams.keySet());
            Set<String> dataStreamIndices = dataStreams.values().stream().flatMap(ds -> ds.getIndices().stream()).map(Index::getName).collect(Collectors.toSet());
            requestIndices.addAll(dataStreamIndices);
            final List<String> indicesInSnapshot = filterIndices(snapshotInfo.indices(), requestIndices.toArray(new String[0]), request.indicesOptions());
            final Metadata.Builder metadataBuilder;
            if (request.includeGlobalState()) {
                if (globalMetadata == null) {
                    globalMetadata = repository.getSnapshotGlobalMetadata(snapshotId);
                }
                metadataBuilder = Metadata.builder(globalMetadata);
            } else {
                metadataBuilder = Metadata.builder();
            }
            final List<IndexId> indexIdsInSnapshot = repositoryData.resolveIndices(indicesInSnapshot);
            for (IndexId indexId : indexIdsInSnapshot) {
                metadataBuilder.put(repository.getSnapshotIndexMetaData(repositoryData, snapshotId, indexId), false);
            }
            final Metadata metadata = metadataBuilder.build();
            // Apply renaming on index names, returning a map of names where
            // the key is the renamed index and the value is the original name
            final Map<String, String> indices = renamedIndices(request, indicesInSnapshot, dataStreamIndices);
            // Now we can start the actual restore process by adding shards to be recovered in the cluster state
            // and updating cluster metadata (global and index) as needed
            clusterService.submitStateUpdateTask("restore_snapshot[" + snapshotName + ']', new ClusterStateUpdateTask() {

                final String restoreUUID = UUIDs.randomBase64UUID();

                RestoreInfo restoreInfo = null;

                @Override
                public ClusterState execute(ClusterState currentState) {
                    RestoreInProgress restoreInProgress = currentState.custom(RestoreInProgress.TYPE, RestoreInProgress.EMPTY);
                    if (currentState.getNodes().getMinNodeVersion().before(LegacyESVersion.V_7_0_0)) {
                        // same time in versions prior to 7.0
                        if (restoreInProgress.isEmpty() == false) {
                            throw new ConcurrentSnapshotExecutionException(snapshot, "Restore process is already running in this cluster");
                        }
                    }
                    // Check if the snapshot to restore is currently being deleted
                    SnapshotDeletionsInProgress deletionsInProgress = currentState.custom(SnapshotDeletionsInProgress.TYPE, SnapshotDeletionsInProgress.EMPTY);
                    if (deletionsInProgress.getEntries().stream().anyMatch(entry -> entry.getSnapshots().contains(snapshotId))) {
                        throw new ConcurrentSnapshotExecutionException(snapshot, "cannot restore a snapshot while a snapshot deletion is in-progress [" + deletionsInProgress.getEntries().get(0) + "]");
                    }
                    // Updating cluster state
                    ClusterState.Builder builder = ClusterState.builder(currentState);
                    Metadata.Builder mdBuilder = Metadata.builder(currentState.metadata());
                    ClusterBlocks.Builder blocks = ClusterBlocks.builder().blocks(currentState.blocks());
                    RoutingTable.Builder rtBuilder = RoutingTable.builder(currentState.routingTable());
                    ImmutableOpenMap<ShardId, RestoreInProgress.ShardRestoreStatus> shards;
                    Set<String> aliases = new HashSet<>();
                    if (indices.isEmpty() == false) {
                        // We have some indices to restore
                        ImmutableOpenMap.Builder<ShardId, RestoreInProgress.ShardRestoreStatus> shardsBuilder = ImmutableOpenMap.builder();
                        final Version minIndexCompatibilityVersion = currentState.getNodes().getMaxNodeVersion().minimumIndexCompatibilityVersion();
                        for (Map.Entry<String, String> indexEntry : indices.entrySet()) {
                            String index = indexEntry.getValue();
                            boolean partial = checkPartial(index);
                            SnapshotRecoverySource recoverySource = new SnapshotRecoverySource(restoreUUID, snapshot, snapshotInfo.version(), repositoryData.resolveIndexId(index));
                            String renamedIndexName = indexEntry.getKey();
                            IndexMetadata snapshotIndexMetadata = metadata.index(index);
                            snapshotIndexMetadata = updateIndexSettings(snapshotIndexMetadata, request.indexSettings(), request.ignoreIndexSettings());
                            try {
                                snapshotIndexMetadata = metadataIndexUpgradeService.upgradeIndexMetadata(snapshotIndexMetadata, minIndexCompatibilityVersion);
                            } catch (Exception ex) {
                                throw new SnapshotRestoreException(snapshot, "cannot restore index [" + index + "] because it cannot be upgraded", ex);
                            }
                            // Check that the index is closed or doesn't exist
                            IndexMetadata currentIndexMetadata = currentState.metadata().index(renamedIndexName);
                            IntSet ignoreShards = new IntHashSet();
                            final Index renamedIndex;
                            if (currentIndexMetadata == null) {
                                // Index doesn't exist - create it and start recovery
                                // Make sure that the index we are about to create has a validate name
                                boolean isHidden = IndexMetadata.INDEX_HIDDEN_SETTING.get(snapshotIndexMetadata.getSettings());
                                createIndexService.validateIndexName(renamedIndexName, currentState);
                                createIndexService.validateDotIndex(renamedIndexName, isHidden);
                                createIndexService.validateIndexSettings(renamedIndexName, snapshotIndexMetadata.getSettings(), false);
                                IndexMetadata.Builder indexMdBuilder = IndexMetadata.builder(snapshotIndexMetadata).state(IndexMetadata.State.OPEN).index(renamedIndexName);
                                indexMdBuilder.settings(Settings.builder().put(snapshotIndexMetadata.getSettings()).put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()));
                                shardLimitValidator.validateShardLimit(snapshotIndexMetadata.getSettings(), currentState);
                                if (!request.includeAliases() && !snapshotIndexMetadata.getAliases().isEmpty()) {
                                    // Remove all aliases - they shouldn't be restored
                                    indexMdBuilder.removeAllAliases();
                                } else {
                                    for (ObjectCursor<String> alias : snapshotIndexMetadata.getAliases().keys()) {
                                        aliases.add(alias.value);
                                    }
                                }
                                IndexMetadata updatedIndexMetadata = indexMdBuilder.build();
                                if (partial) {
                                    populateIgnoredShards(index, ignoreShards);
                                }
                                rtBuilder.addAsNewRestore(updatedIndexMetadata, recoverySource, ignoreShards);
                                blocks.addBlocks(updatedIndexMetadata);
                                mdBuilder.put(updatedIndexMetadata, true);
                                renamedIndex = updatedIndexMetadata.getIndex();
                            } else {
                                validateExistingIndex(currentIndexMetadata, snapshotIndexMetadata, renamedIndexName, partial);
                                // Index exists and it's closed - open it in metadata and start recovery
                                IndexMetadata.Builder indexMdBuilder = IndexMetadata.builder(snapshotIndexMetadata).state(IndexMetadata.State.OPEN);
                                indexMdBuilder.version(Math.max(snapshotIndexMetadata.getVersion(), 1 + currentIndexMetadata.getVersion()));
                                indexMdBuilder.mappingVersion(Math.max(snapshotIndexMetadata.getMappingVersion(), 1 + currentIndexMetadata.getMappingVersion()));
                                indexMdBuilder.settingsVersion(Math.max(snapshotIndexMetadata.getSettingsVersion(), 1 + currentIndexMetadata.getSettingsVersion()));
                                indexMdBuilder.aliasesVersion(Math.max(snapshotIndexMetadata.getAliasesVersion(), 1 + currentIndexMetadata.getAliasesVersion()));
                                for (int shard = 0; shard < snapshotIndexMetadata.getNumberOfShards(); shard++) {
                                    indexMdBuilder.primaryTerm(shard, Math.max(snapshotIndexMetadata.primaryTerm(shard), currentIndexMetadata.primaryTerm(shard)));
                                }
                                if (!request.includeAliases()) {
                                    // Remove all snapshot aliases
                                    if (!snapshotIndexMetadata.getAliases().isEmpty()) {
                                        indexMdBuilder.removeAllAliases();
                                    }
                                    // / Add existing aliases
                                    for (ObjectCursor<AliasMetadata> alias : currentIndexMetadata.getAliases().values()) {
                                        indexMdBuilder.putAlias(alias.value);
                                    }
                                } else {
                                    for (ObjectCursor<String> alias : snapshotIndexMetadata.getAliases().keys()) {
                                        aliases.add(alias.value);
                                    }
                                }
                                final Settings.Builder indexSettingsBuilder = Settings.builder().put(snapshotIndexMetadata.getSettings()).put(IndexMetadata.SETTING_INDEX_UUID, currentIndexMetadata.getIndexUUID());
                                // setting anyway
                                if (snapshotIndexMetadata.getCreationVersion().onOrAfter(LegacyESVersion.V_7_9_0) || currentState.nodes().getMinNodeVersion().onOrAfter(LegacyESVersion.V_7_9_0)) {
                                    indexSettingsBuilder.put(SETTING_HISTORY_UUID, UUIDs.randomBase64UUID());
                                }
                                indexMdBuilder.settings(indexSettingsBuilder);
                                IndexMetadata updatedIndexMetadata = indexMdBuilder.index(renamedIndexName).build();
                                rtBuilder.addAsRestore(updatedIndexMetadata, recoverySource);
                                blocks.updateBlocks(updatedIndexMetadata);
                                mdBuilder.put(updatedIndexMetadata, true);
                                renamedIndex = updatedIndexMetadata.getIndex();
                            }
                            for (int shard = 0; shard < snapshotIndexMetadata.getNumberOfShards(); shard++) {
                                if (!ignoreShards.contains(shard)) {
                                    shardsBuilder.put(new ShardId(renamedIndex, shard), new RestoreInProgress.ShardRestoreStatus(clusterService.state().nodes().getLocalNodeId()));
                                } else {
                                    shardsBuilder.put(new ShardId(renamedIndex, shard), new RestoreInProgress.ShardRestoreStatus(clusterService.state().nodes().getLocalNodeId(), RestoreInProgress.State.FAILURE));
                                }
                            }
                        }
                        shards = shardsBuilder.build();
                        RestoreInProgress.Entry restoreEntry = new RestoreInProgress.Entry(restoreUUID, snapshot, overallState(RestoreInProgress.State.INIT, shards), Collections.unmodifiableList(new ArrayList<>(indices.keySet())), shards);
                        builder.putCustom(RestoreInProgress.TYPE, new RestoreInProgress.Builder(currentState.custom(RestoreInProgress.TYPE, RestoreInProgress.EMPTY)).add(restoreEntry).build());
                    } else {
                        shards = ImmutableOpenMap.of();
                    }
                    checkAliasNameConflicts(indices, aliases);
                    Map<String, DataStream> updatedDataStreams = new HashMap<>(currentState.metadata().dataStreams());
                    updatedDataStreams.putAll(dataStreams.values().stream().map(ds -> updateDataStream(ds, mdBuilder, request)).collect(Collectors.toMap(DataStream::getName, Function.identity())));
                    mdBuilder.dataStreams(updatedDataStreams);
                    // Restore global state if needed
                    if (request.includeGlobalState()) {
                        if (metadata.persistentSettings() != null) {
                            Settings settings = metadata.persistentSettings();
                            clusterSettings.validateUpdate(settings);
                            mdBuilder.persistentSettings(settings);
                        }
                        if (metadata.templates() != null) {
                            // TODO: Should all existing templates be deleted first?
                            for (ObjectCursor<IndexTemplateMetadata> cursor : metadata.templates().values()) {
                                mdBuilder.put(cursor.value);
                            }
                        }
                        if (metadata.customs() != null) {
                            for (ObjectObjectCursor<String, Metadata.Custom> cursor : metadata.customs()) {
                                if (RepositoriesMetadata.TYPE.equals(cursor.key) == false && DataStreamMetadata.TYPE.equals(cursor.key) == false) {
                                    // Don't restore repositories while we are working with them
                                    // TODO: Should we restore them at the end?
                                    // Also, don't restore data streams here, we already added them to the metadata builder above
                                    mdBuilder.putCustom(cursor.key, cursor.value);
                                }
                            }
                        }
                    }
                    if (completed(shards)) {
                        // We don't have any indices to restore - we are done
                        restoreInfo = new RestoreInfo(snapshotId.getName(), Collections.unmodifiableList(new ArrayList<>(indices.keySet())), shards.size(), shards.size() - failedShards(shards));
                    }
                    RoutingTable rt = rtBuilder.build();
                    ClusterState updatedState = builder.metadata(mdBuilder).blocks(blocks).routingTable(rt).build();
                    return allocationService.reroute(updatedState, "restored snapshot [" + snapshot + "]");
                }

                private void checkAliasNameConflicts(Map<String, String> renamedIndices, Set<String> aliases) {
                    for (Map.Entry<String, String> renamedIndex : renamedIndices.entrySet()) {
                        if (aliases.contains(renamedIndex.getKey())) {
                            throw new SnapshotRestoreException(snapshot, "cannot rename index [" + renamedIndex.getValue() + "] into [" + renamedIndex.getKey() + "] because of conflict with an alias with the same name");
                        }
                    }
                }

                private void populateIgnoredShards(String index, IntSet ignoreShards) {
                    for (SnapshotShardFailure failure : snapshotInfo.shardFailures()) {
                        if (index.equals(failure.index())) {
                            ignoreShards.add(failure.shardId());
                        }
                    }
                }

                private boolean checkPartial(String index) {
                    // Make sure that index was fully snapshotted
                    if (failed(snapshotInfo, index)) {
                        if (request.partial()) {
                            return true;
                        } else {
                            throw new SnapshotRestoreException(snapshot, "index [" + index + "] wasn't fully snapshotted - cannot " + "restore");
                        }
                    } else {
                        return false;
                    }
                }

                private void validateExistingIndex(IndexMetadata currentIndexMetadata, IndexMetadata snapshotIndexMetadata, String renamedIndex, boolean partial) {
                    // Index exist - checking that it's closed
                    if (currentIndexMetadata.getState() != IndexMetadata.State.CLOSE) {
                        // TODO: Enable restore for open indices
                        throw new SnapshotRestoreException(snapshot, "cannot restore index [" + renamedIndex + "] because an open index " + "with same name already exists in the cluster. Either close or delete the existing index or restore the " + "index under a different name by providing a rename pattern and replacement name");
                    }
                    // Index exist - checking if it's partial restore
                    if (partial) {
                        throw new SnapshotRestoreException(snapshot, "cannot restore partial index [" + renamedIndex + "] because such index already exists");
                    }
                    // Make sure that the number of shards is the same. That's the only thing that we cannot change
                    if (currentIndexMetadata.getNumberOfShards() != snapshotIndexMetadata.getNumberOfShards()) {
                        throw new SnapshotRestoreException(snapshot, "cannot restore index [" + renamedIndex + "] with [" + currentIndexMetadata.getNumberOfShards() + "] shards from a snapshot of index [" + snapshotIndexMetadata.getIndex().getName() + "] with [" + snapshotIndexMetadata.getNumberOfShards() + "] shards");
                    }
                }

                /**
                 * Optionally updates index settings in indexMetadata by removing settings listed in ignoreSettings and
                 * merging them with settings in changeSettings.
                 */
                private IndexMetadata updateIndexSettings(IndexMetadata indexMetadata, Settings changeSettings, String[] ignoreSettings) {
                    Settings normalizedChangeSettings = Settings.builder().put(changeSettings).normalizePrefix(IndexMetadata.INDEX_SETTING_PREFIX).build();
                    if (IndexSettings.INDEX_SOFT_DELETES_SETTING.get(indexMetadata.getSettings()) && IndexSettings.INDEX_SOFT_DELETES_SETTING.exists(changeSettings) && IndexSettings.INDEX_SOFT_DELETES_SETTING.get(changeSettings) == false) {
                        throw new SnapshotRestoreException(snapshot, "cannot disable setting [" + IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey() + "] on restore");
                    }
                    IndexMetadata.Builder builder = IndexMetadata.builder(indexMetadata);
                    Settings settings = indexMetadata.getSettings();
                    Set<String> keyFilters = new HashSet<>();
                    List<String> simpleMatchPatterns = new ArrayList<>();
                    for (String ignoredSetting : ignoreSettings) {
                        if (!Regex.isSimpleMatchPattern(ignoredSetting)) {
                            if (UNREMOVABLE_SETTINGS.contains(ignoredSetting)) {
                                throw new SnapshotRestoreException(snapshot, "cannot remove setting [" + ignoredSetting + "] on restore");
                            } else {
                                keyFilters.add(ignoredSetting);
                            }
                        } else {
                            simpleMatchPatterns.add(ignoredSetting);
                        }
                    }
                    Predicate<String> settingsFilter = k -> {
                        if (UNREMOVABLE_SETTINGS.contains(k) == false) {
                            for (String filterKey : keyFilters) {
                                if (k.equals(filterKey)) {
                                    return false;
                                }
                            }
                            for (String pattern : simpleMatchPatterns) {
                                if (Regex.simpleMatch(pattern, k)) {
                                    return false;
                                }
                            }
                        }
                        return true;
                    };
                    Settings.Builder settingsBuilder = Settings.builder().put(settings.filter(settingsFilter)).put(normalizedChangeSettings.filter(k -> {
                        if (UNMODIFIABLE_SETTINGS.contains(k)) {
                            throw new SnapshotRestoreException(snapshot, "cannot modify setting [" + k + "] on restore");
                        } else {
                            return true;
                        }
                    }));
                    settingsBuilder.remove(MetadataIndexStateService.VERIFIED_BEFORE_CLOSE_SETTING.getKey());
                    return builder.settings(settingsBuilder).build();
                }

                @Override
                public void onFailure(String source, Exception e) {
                    logger.warn(() -> new ParameterizedMessage("[{}] failed to restore snapshot", snapshotId), e);
                    listener.onFailure(e);
                }

                @Override
                public TimeValue timeout() {
                    return request.masterNodeTimeout();
                }

                @Override
                public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
                    listener.onResponse(new RestoreCompletionResponse(restoreUUID, snapshot, restoreInfo));
                }
            });
        }, listener::onFailure);
    } catch (Exception e) {
        logger.warn(() -> new ParameterizedMessage("[{}] failed to restore snapshot", request.repository() + ":" + request.snapshot()), e);
        listener.onFailure(e);
    }
}
Also used : ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) Arrays(java.util.Arrays) Metadata(org.opensearch.cluster.metadata.Metadata) SETTING_AUTO_EXPAND_REPLICAS(org.opensearch.cluster.metadata.IndexMetadata.SETTING_AUTO_EXPAND_REPLICAS) DataStream(org.opensearch.cluster.metadata.DataStream) SETTING_VERSION_CREATED(org.opensearch.cluster.metadata.IndexMetadata.SETTING_VERSION_CREATED) SnapshotRecoverySource(org.opensearch.cluster.routing.RecoverySource.SnapshotRecoverySource) AllocationService(org.opensearch.cluster.routing.allocation.AllocationService) Version(org.opensearch.Version) ClusterStateApplier(org.opensearch.cluster.ClusterStateApplier) Regex(org.opensearch.common.regex.Regex) SETTING_VERSION_UPGRADED(org.opensearch.cluster.metadata.IndexMetadata.SETTING_VERSION_UPGRADED) MetadataIndexUpgradeService(org.opensearch.cluster.metadata.MetadataIndexUpgradeService) Sets.newHashSet(org.opensearch.common.util.set.Sets.newHashSet) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) IndexId(org.opensearch.repositories.IndexId) SETTING_HISTORY_UUID(org.opensearch.cluster.metadata.IndexMetadata.SETTING_HISTORY_UUID) RestoreSnapshotRequest(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotRequest) Map(java.util.Map) Lucene(org.opensearch.common.lucene.Lucene) ActionListener(org.opensearch.action.ActionListener) UnassignedInfo(org.opensearch.cluster.routing.UnassignedInfo) SETTING_NUMBER_OF_REPLICAS(org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_REPLICAS) Repository(org.opensearch.repositories.Repository) TimeValue(org.opensearch.common.unit.TimeValue) Index(org.opensearch.index.Index) Predicate(java.util.function.Predicate) Set(java.util.Set) ClusterStateTaskExecutor(org.opensearch.cluster.ClusterStateTaskExecutor) Settings(org.opensearch.common.settings.Settings) ObjectCursor(com.carrotsearch.hppc.cursors.ObjectCursor) Collectors(java.util.stream.Collectors) List(java.util.List) Logger(org.apache.logging.log4j.Logger) DataStreamMetadata(org.opensearch.cluster.metadata.DataStreamMetadata) ClusterStateUpdateTask(org.opensearch.cluster.ClusterStateUpdateTask) IndexSettings(org.opensearch.index.IndexSettings) SETTING_CREATION_DATE(org.opensearch.cluster.metadata.IndexMetadata.SETTING_CREATION_DATE) Optional(java.util.Optional) StepListener(org.opensearch.action.StepListener) ClusterStateTaskListener(org.opensearch.cluster.ClusterStateTaskListener) RepositoriesService(org.opensearch.repositories.RepositoriesService) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) SETTING_INDEX_UUID(org.opensearch.cluster.metadata.IndexMetadata.SETTING_INDEX_UUID) Priority(org.opensearch.common.Priority) HashMap(java.util.HashMap) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) IndicesOptions(org.opensearch.action.support.IndicesOptions) SnapshotDeletionsInProgress(org.opensearch.cluster.SnapshotDeletionsInProgress) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Function(java.util.function.Function) ArrayList(java.util.ArrayList) RecoverySource(org.opensearch.cluster.routing.RecoverySource) HashSet(java.util.HashSet) ClusterState(org.opensearch.cluster.ClusterState) MetadataCreateIndexService(org.opensearch.cluster.metadata.MetadataCreateIndexService) IndexShard(org.opensearch.index.shard.IndexShard) LegacyESVersion(org.opensearch.LegacyESVersion) ClusterStateTaskConfig(org.opensearch.cluster.ClusterStateTaskConfig) UUIDs(org.opensearch.common.UUIDs) ClusterSettings(org.opensearch.common.settings.ClusterSettings) ClusterBlocks(org.opensearch.cluster.block.ClusterBlocks) ShardRestoreStatus(org.opensearch.cluster.RestoreInProgress.ShardRestoreStatus) MetadataIndexStateService(org.opensearch.cluster.metadata.MetadataIndexStateService) RepositoryData(org.opensearch.repositories.RepositoryData) SnapshotUtils.filterIndices(org.opensearch.snapshots.SnapshotUtils.filterIndices) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) RoutingChangesObserver(org.opensearch.cluster.routing.RoutingChangesObserver) ShardLimitValidator(org.opensearch.indices.ShardLimitValidator) RepositoriesMetadata(org.opensearch.cluster.metadata.RepositoriesMetadata) IntHashSet(com.carrotsearch.hppc.IntHashSet) IntSet(com.carrotsearch.hppc.IntSet) ShardRouting(org.opensearch.cluster.routing.ShardRouting) ShardId(org.opensearch.index.shard.ShardId) Collections.unmodifiableSet(java.util.Collections.unmodifiableSet) ClusterService(org.opensearch.cluster.service.ClusterService) RestoreInProgress(org.opensearch.cluster.RestoreInProgress) RoutingTable(org.opensearch.cluster.routing.RoutingTable) SETTING_NUMBER_OF_SHARDS(org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SHARDS) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) ClusterChangedEvent(org.opensearch.cluster.ClusterChangedEvent) DataStream(org.opensearch.cluster.metadata.DataStream) ArrayList(java.util.ArrayList) IntHashSet(com.carrotsearch.hppc.IntHashSet) Index(org.opensearch.index.Index) SnapshotDeletionsInProgress(org.opensearch.cluster.SnapshotDeletionsInProgress) Predicate(java.util.function.Predicate) Version(org.opensearch.Version) LegacyESVersion(org.opensearch.LegacyESVersion) List(java.util.List) ArrayList(java.util.ArrayList) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings) ClusterSettings(org.opensearch.common.settings.ClusterSettings) TimeValue(org.opensearch.common.unit.TimeValue) ClusterState(org.opensearch.cluster.ClusterState) RestoreInProgress(org.opensearch.cluster.RestoreInProgress) SnapshotRecoverySource(org.opensearch.cluster.routing.RecoverySource.SnapshotRecoverySource) RoutingTable(org.opensearch.cluster.routing.RoutingTable) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) ObjectCursor(com.carrotsearch.hppc.cursors.ObjectCursor) ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) Map(java.util.Map) HashMap(java.util.HashMap) Sets.newHashSet(org.opensearch.common.util.set.Sets.newHashSet) Set(java.util.Set) HashSet(java.util.HashSet) IntHashSet(com.carrotsearch.hppc.IntHashSet) IntSet(com.carrotsearch.hppc.IntSet) Collections.unmodifiableSet(java.util.Collections.unmodifiableSet) IntSet(com.carrotsearch.hppc.IntSet) Metadata(org.opensearch.cluster.metadata.Metadata) DataStreamMetadata(org.opensearch.cluster.metadata.DataStreamMetadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) RepositoriesMetadata(org.opensearch.cluster.metadata.RepositoriesMetadata) ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) ShardId(org.opensearch.index.shard.ShardId) IndexId(org.opensearch.repositories.IndexId) ClusterStateUpdateTask(org.opensearch.cluster.ClusterStateUpdateTask) RepositoryData(org.opensearch.repositories.RepositoryData) Repository(org.opensearch.repositories.Repository) ShardRestoreStatus(org.opensearch.cluster.RestoreInProgress.ShardRestoreStatus) StepListener(org.opensearch.action.StepListener) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage)

Example 9 with IndexTemplateMetadata

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

the class MetadataRolloverServiceTests method testHiddenAffectsResolvedTemplates.

public void testHiddenAffectsResolvedTemplates() {
    final IndexTemplateMetadata template = IndexTemplateMetadata.builder("test-template").patterns(Collections.singletonList("*")).putAlias(AliasMetadata.builder("foo-write")).putAlias(AliasMetadata.builder("bar-write").writeIndex(randomBoolean())).build();
    final Metadata metadata = Metadata.builder().put(createMetadata(randomAlphaOfLengthBetween(5, 7)), false).put(template).build();
    String indexName = randomFrom("foo-123", "bar-xyz");
    String aliasName = randomFrom("foo-write", "bar-write");
    // hidden shouldn't throw
    MetadataRolloverService.checkNoDuplicatedAliasInIndexTemplate(metadata, indexName, aliasName, Boolean.TRUE);
    // not hidden will throw
    final IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> MetadataRolloverService.checkNoDuplicatedAliasInIndexTemplate(metadata, indexName, aliasName, randomFrom(Boolean.FALSE, null)));
    assertThat(ex.getMessage(), containsString("index template [test-template]"));
}
Also used : IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 10 with IndexTemplateMetadata

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

the class GetIndexTemplatesResponseTests method createTestInstance.

@Override
protected GetIndexTemplatesResponse createTestInstance() {
    List<IndexTemplateMetadata> templates = new ArrayList<>();
    int numTemplates = between(0, 10);
    for (int t = 0; t < numTemplates; t++) {
        IndexTemplateMetadata.Builder templateBuilder = IndexTemplateMetadata.builder("template-" + t);
        templateBuilder.patterns(IntStream.range(0, between(1, 5)).mapToObj(i -> "pattern-" + i).collect(Collectors.toList()));
        int numAlias = between(0, 5);
        for (int i = 0; i < numAlias; i++) {
            templateBuilder.putAlias(AliasMetadata.builder(randomAlphaOfLengthBetween(1, 10)));
        }
        if (randomBoolean()) {
            templateBuilder.settings(Settings.builder().put("index.setting-1", randomLong()));
        }
        if (randomBoolean()) {
            templateBuilder.order(randomInt());
        }
        if (randomBoolean()) {
            templateBuilder.version(between(0, 100));
        }
        if (randomBoolean()) {
            try {
                templateBuilder.putMapping("doc", "{\"properties\":{\"type\":\"text\"}}");
            } catch (IOException ex) {
                throw new UncheckedIOException(ex);
            }
        }
        templates.add(templateBuilder.build());
    }
    return new GetIndexTemplatesResponse(templates);
}
Also used : IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) ArrayList(java.util.ArrayList) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Aggregations

IndexTemplateMetadata (org.opensearch.cluster.metadata.IndexTemplateMetadata)14 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)8 Metadata (org.opensearch.cluster.metadata.Metadata)8 ArrayList (java.util.ArrayList)5 AliasMetadata (org.opensearch.cluster.metadata.AliasMetadata)5 Map (java.util.Map)4 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 List (java.util.List)2 Version (org.opensearch.Version)2 ActionListener (org.opensearch.action.ActionListener)2 ClusterState (org.opensearch.cluster.ClusterState)2 ComposableIndexTemplate (org.opensearch.cluster.metadata.ComposableIndexTemplate)2 RoutingTable (org.opensearch.cluster.routing.RoutingTable)2 Settings (org.opensearch.common.settings.Settings)2 IndexSettings (org.opensearch.index.IndexSettings)2 IntHashSet (com.carrotsearch.hppc.IntHashSet)1 IntSet (com.carrotsearch.hppc.IntSet)1 ObjectCursor (com.carrotsearch.hppc.cursors.ObjectCursor)1