Search in sources :

Example 1 with PersistedClusterStateService

use of org.opensearch.gateway.PersistedClusterStateService in project OpenSearch by opensearch-project.

the class NodeRepurposeCommand method processMasterNoDataNode.

private void processMasterNoDataNode(Terminal terminal, Path[] dataPaths, Environment env) throws IOException {
    NodeEnvironment.NodePath[] nodePaths = toNodePaths(dataPaths);
    terminal.println(Terminal.Verbosity.VERBOSE, "Collecting shard data paths");
    List<Path> shardDataPaths = NodeEnvironment.collectShardDataPaths(nodePaths);
    if (shardDataPaths.isEmpty()) {
        terminal.println(NO_SHARD_DATA_TO_CLEAN_UP_FOUND);
        return;
    }
    final PersistedClusterStateService persistedClusterStateService = createPersistedClusterStateService(env.settings(), dataPaths);
    final Metadata metadata = loadClusterState(terminal, env, persistedClusterStateService).metadata();
    final Set<Path> indexPaths = uniqueParentPaths(shardDataPaths);
    final Set<String> indexUUIDs = indexUUIDsFor(indexPaths);
    outputVerboseInformation(terminal, shardDataPaths, indexUUIDs, metadata);
    terminal.println(shardMessage(shardDataPaths.size(), indexUUIDs.size()));
    outputHowToSeeVerboseInformation(terminal);
    terminal.println("Node is being re-purposed as master and no-data. Clean-up of shard data will be performed.");
    confirm(terminal, "Do you want to proceed?");
    // clean-up shard dirs
    removePaths(terminal, shardDataPaths);
    terminal.println("Node successfully repurposed to master and no-data.");
}
Also used : Path(java.nio.file.Path) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) PersistedClusterStateService(org.opensearch.gateway.PersistedClusterStateService)

Example 2 with PersistedClusterStateService

use of org.opensearch.gateway.PersistedClusterStateService in project OpenSearch by opensearch-project.

the class NodeRepurposeCommand method processNoMasterNoDataNode.

private void processNoMasterNoDataNode(Terminal terminal, Path[] dataPaths, Environment env) throws IOException {
    NodeEnvironment.NodePath[] nodePaths = toNodePaths(dataPaths);
    terminal.println(Terminal.Verbosity.VERBOSE, "Collecting shard data paths");
    List<Path> shardDataPaths = NodeEnvironment.collectShardDataPaths(nodePaths);
    terminal.println(Terminal.Verbosity.VERBOSE, "Collecting index metadata paths");
    List<Path> indexMetadataPaths = NodeEnvironment.collectIndexMetadataPaths(nodePaths);
    Set<Path> indexPaths = uniqueParentPaths(shardDataPaths, indexMetadataPaths);
    final PersistedClusterStateService persistedClusterStateService = createPersistedClusterStateService(env.settings(), dataPaths);
    final Metadata metadata = loadClusterState(terminal, env, persistedClusterStateService).metadata();
    if (indexPaths.isEmpty() && metadata.indices().isEmpty()) {
        terminal.println(Terminal.Verbosity.NORMAL, NO_DATA_TO_CLEAN_UP_FOUND);
        return;
    }
    final Set<String> indexUUIDs = Sets.union(indexUUIDsFor(indexPaths), StreamSupport.stream(metadata.indices().values().spliterator(), false).map(imd -> imd.value.getIndexUUID()).collect(Collectors.toSet()));
    outputVerboseInformation(terminal, indexPaths, indexUUIDs, metadata);
    terminal.println(noMasterMessage(indexUUIDs.size(), shardDataPaths.size(), indexMetadataPaths.size()));
    outputHowToSeeVerboseInformation(terminal);
    terminal.println("Node is being re-purposed as no-master and no-data. Clean-up of index data will be performed.");
    confirm(terminal, "Do you want to proceed?");
    // clean-up shard dirs
    removePaths(terminal, indexPaths);
    // clean-up all metadata dirs
    MetadataStateFormat.deleteMetaState(dataPaths);
    IOUtils.rm(Stream.of(dataPaths).map(path -> path.resolve(INDICES_FOLDER)).toArray(Path[]::new));
    terminal.println("Node successfully repurposed to no-master and no-data.");
}
Also used : Path(java.nio.file.Path) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) PersistedClusterStateService(org.opensearch.gateway.PersistedClusterStateService)

Example 3 with PersistedClusterStateService

use of org.opensearch.gateway.PersistedClusterStateService in project OpenSearch by opensearch-project.

the class RemoveCustomsCommand method processNodePaths.

@Override
protected void processNodePaths(Terminal terminal, Path[] dataPaths, int nodeLockId, OptionSet options, Environment env) throws IOException, UserException {
    final List<String> customsToRemove = arguments.values(options);
    if (customsToRemove.isEmpty()) {
        throw new UserException(ExitCodes.USAGE, "Must supply at least one custom metadata name to remove");
    }
    final PersistedClusterStateService persistedClusterStateService = createPersistedClusterStateService(env.settings(), dataPaths);
    terminal.println(Terminal.Verbosity.VERBOSE, "Loading cluster state");
    final Tuple<Long, ClusterState> termAndClusterState = loadTermAndClusterState(persistedClusterStateService, env);
    final ClusterState oldClusterState = termAndClusterState.v2();
    terminal.println(Terminal.Verbosity.VERBOSE, "custom metadata names: " + oldClusterState.metadata().customs().keys());
    final Metadata.Builder metadataBuilder = Metadata.builder(oldClusterState.metadata());
    for (String customToRemove : customsToRemove) {
        boolean matched = false;
        for (ObjectCursor<String> customKeyCur : oldClusterState.metadata().customs().keys()) {
            final String customKey = customKeyCur.value;
            if (Regex.simpleMatch(customToRemove, customKey)) {
                metadataBuilder.removeCustom(customKey);
                if (matched == false) {
                    terminal.println("The following customs will be removed:");
                }
                matched = true;
                terminal.println(customKey);
            }
        }
        if (matched == false) {
            throw new UserException(ExitCodes.USAGE, "No custom metadata matching [" + customToRemove + "] were found on this node");
        }
    }
    final ClusterState newClusterState = ClusterState.builder(oldClusterState).metadata(metadataBuilder.build()).build();
    terminal.println(Terminal.Verbosity.VERBOSE, "[old cluster state = " + oldClusterState + ", new cluster state = " + newClusterState + "]");
    confirm(terminal, CONFIRMATION_MSG);
    try (PersistedClusterStateService.Writer writer = persistedClusterStateService.createWriter()) {
        writer.writeFullStateAndCommit(termAndClusterState.v1(), newClusterState);
    }
    terminal.println(CUSTOMS_REMOVED_MSG);
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) Metadata(org.opensearch.cluster.metadata.Metadata) UserException(org.opensearch.cli.UserException) PersistedClusterStateService(org.opensearch.gateway.PersistedClusterStateService)

Example 4 with PersistedClusterStateService

use of org.opensearch.gateway.PersistedClusterStateService in project OpenSearch by opensearch-project.

the class RemoveSettingsCommand method processNodePaths.

@Override
protected void processNodePaths(Terminal terminal, Path[] dataPaths, int nodeLockId, OptionSet options, Environment env) throws IOException, UserException {
    final List<String> settingsToRemove = arguments.values(options);
    if (settingsToRemove.isEmpty()) {
        throw new UserException(ExitCodes.USAGE, "Must supply at least one setting to remove");
    }
    final PersistedClusterStateService persistedClusterStateService = createPersistedClusterStateService(env.settings(), dataPaths);
    terminal.println(Terminal.Verbosity.VERBOSE, "Loading cluster state");
    final Tuple<Long, ClusterState> termAndClusterState = loadTermAndClusterState(persistedClusterStateService, env);
    final ClusterState oldClusterState = termAndClusterState.v2();
    final Settings oldPersistentSettings = oldClusterState.metadata().persistentSettings();
    terminal.println(Terminal.Verbosity.VERBOSE, "persistent settings: " + oldPersistentSettings);
    final Settings.Builder newPersistentSettingsBuilder = Settings.builder().put(oldPersistentSettings);
    for (String settingToRemove : settingsToRemove) {
        boolean matched = false;
        for (String settingKey : oldPersistentSettings.keySet()) {
            if (Regex.simpleMatch(settingToRemove, settingKey)) {
                newPersistentSettingsBuilder.remove(settingKey);
                if (matched == false) {
                    terminal.println("The following settings will be removed:");
                }
                matched = true;
                terminal.println(settingKey + ": " + oldPersistentSettings.get(settingKey));
            }
        }
        if (matched == false) {
            throw new UserException(ExitCodes.USAGE, "No persistent cluster settings matching [" + settingToRemove + "] were found on this node");
        }
    }
    final ClusterState newClusterState = ClusterState.builder(oldClusterState).metadata(Metadata.builder(oldClusterState.metadata()).persistentSettings(newPersistentSettingsBuilder.build()).build()).build();
    terminal.println(Terminal.Verbosity.VERBOSE, "[old cluster state = " + oldClusterState + ", new cluster state = " + newClusterState + "]");
    confirm(terminal, CONFIRMATION_MSG);
    try (PersistedClusterStateService.Writer writer = persistedClusterStateService.createWriter()) {
        writer.writeFullStateAndCommit(termAndClusterState.v1(), newClusterState);
    }
    terminal.println(SETTINGS_REMOVED_MSG);
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) UserException(org.opensearch.cli.UserException) PersistedClusterStateService(org.opensearch.gateway.PersistedClusterStateService) Settings(org.opensearch.common.settings.Settings)

Example 5 with PersistedClusterStateService

use of org.opensearch.gateway.PersistedClusterStateService in project OpenSearch by opensearch-project.

the class RemoveCorruptedShardDataCommandTests method setup.

@Before
public void setup() throws IOException {
    shardId = new ShardId("index0", UUIDs.randomBase64UUID(), 0);
    final String nodeId = randomAlphaOfLength(10);
    routing = TestShardRouting.newShardRouting(shardId, nodeId, true, ShardRoutingState.INITIALIZING, RecoverySource.EmptyStoreRecoverySource.INSTANCE);
    final Path dataDir = createTempDir();
    environment = TestEnvironment.newEnvironment(Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), dataDir).putList(Environment.PATH_DATA_SETTING.getKey(), dataDir.toAbsolutePath().toString()).build());
    // create same directory structure as prod does
    final Path path = NodeEnvironment.resolveNodePath(dataDir, 0);
    Files.createDirectories(path);
    dataPaths = new Path[] { path };
    final Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(MergePolicyConfig.INDEX_MERGE_ENABLED, false).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetadata.SETTING_INDEX_UUID, shardId.getIndex().getUUID()).build();
    final NodeEnvironment.NodePath nodePath = new NodeEnvironment.NodePath(path);
    shardPath = new ShardPath(false, nodePath.resolve(shardId), nodePath.resolve(shardId), shardId);
    final IndexMetadata.Builder metadata = IndexMetadata.builder(routing.getIndexName()).settings(settings).primaryTerm(0, randomIntBetween(1, 100)).putMapping("_doc", "{ \"properties\": {} }");
    indexMetadata = metadata.build();
    clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(Metadata.builder().put(indexMetadata, false).build()).build();
    try (NodeEnvironment.NodeLock lock = new NodeEnvironment.NodeLock(0, logger, environment, Files::exists)) {
        final Path[] dataPaths = Arrays.stream(lock.getNodePaths()).filter(Objects::nonNull).map(p -> p.path).toArray(Path[]::new);
        try (PersistedClusterStateService.Writer writer = new PersistedClusterStateService(dataPaths, nodeId, xContentRegistry(), BigArrays.NON_RECYCLING_INSTANCE, new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), () -> 0L).createWriter()) {
            writer.writeFullStateAndCommit(1L, clusterState);
        }
    }
    indexShard = newStartedShard(p -> newShard(routing, shardPath, indexMetadata, null, null, new InternalEngineFactory(), new EngineConfigFactory(new IndexSettings(indexMetadata, settings)), () -> {
    }, RetentionLeaseSyncer.EMPTY, EMPTY_EVENT_LISTENER), true);
    translogPath = shardPath.resolveTranslog();
    indexPath = shardPath.resolveIndex();
}
Also used : Path(java.nio.file.Path) EngineException(org.opensearch.index.engine.EngineException) Arrays(java.util.Arrays) TranslogCorruptedException(org.opensearch.index.translog.TranslogCorruptedException) Matchers.either(org.hamcrest.Matchers.either) MockTerminal(org.opensearch.cli.MockTerminal) Metadata(org.opensearch.cluster.metadata.Metadata) TRUNCATE_CLEAN_TRANSLOG_FLAG(org.opensearch.index.shard.RemoveCorruptedShardDataCommand.TRUNCATE_CLEAN_TRANSLOG_FLAG) CheckedFunction(org.opensearch.common.CheckedFunction) Version(org.opensearch.Version) OpenSearchException(org.opensearch.OpenSearchException) Matcher(java.util.regex.Matcher) OptionParser(joptsimple.OptionParser) DummyShardLock(org.opensearch.test.DummyShardLock) Path(java.nio.file.Path) OptionSet(joptsimple.OptionSet) NodeEnvironment(org.opensearch.env.NodeEnvironment) Matchers.allOf(org.hamcrest.Matchers.allOf) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) Store(org.opensearch.index.store.Store) Matchers.startsWith(org.hamcrest.Matchers.startsWith) Objects(java.util.Objects) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) EngineConfigFactory(org.opensearch.index.engine.EngineConfigFactory) Matchers.equalTo(org.hamcrest.Matchers.equalTo) IndexSettings(org.opensearch.index.IndexSettings) Matchers.is(org.hamcrest.Matchers.is) ShardRoutingHelper(org.opensearch.cluster.routing.ShardRoutingHelper) Pattern(java.util.regex.Pattern) BigArrays(org.opensearch.common.util.BigArrays) Matchers.containsString(org.hamcrest.Matchers.containsString) TestEnvironment(org.opensearch.env.TestEnvironment) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) RecoverySource(org.opensearch.cluster.routing.RecoverySource) BaseDirectoryWrapper(org.apache.lucene.store.BaseDirectoryWrapper) PersistedClusterStateService(org.opensearch.gateway.PersistedClusterStateService) ClusterState(org.opensearch.cluster.ClusterState) ShardRoutingState(org.opensearch.cluster.routing.ShardRoutingState) UUIDs(org.opensearch.common.UUIDs) ClusterSettings(org.opensearch.common.settings.ClusterSettings) CorruptionUtils(org.opensearch.test.CorruptionUtils) Before(org.junit.Before) Environment(org.opensearch.env.Environment) InternalEngineFactory(org.opensearch.index.engine.InternalEngineFactory) Terminal(org.opensearch.cli.Terminal) Files(java.nio.file.Files) TestTranslog(org.opensearch.index.translog.TestTranslog) IOException(java.io.IOException) ShardRouting(org.opensearch.cluster.routing.ShardRouting) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting) ClusterName(org.opensearch.cluster.ClusterName) MergePolicyConfig(org.opensearch.index.MergePolicyConfig) RetentionLeaseSyncer(org.opensearch.index.seqno.RetentionLeaseSyncer) ClusterSettings(org.opensearch.common.settings.ClusterSettings) NodeEnvironment(org.opensearch.env.NodeEnvironment) IndexSettings(org.opensearch.index.IndexSettings) Matchers.containsString(org.hamcrest.Matchers.containsString) EngineConfigFactory(org.opensearch.index.engine.EngineConfigFactory) InternalEngineFactory(org.opensearch.index.engine.InternalEngineFactory) Objects(java.util.Objects) PersistedClusterStateService(org.opensearch.gateway.PersistedClusterStateService) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Files(java.nio.file.Files) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Before(org.junit.Before)

Aggregations

PersistedClusterStateService (org.opensearch.gateway.PersistedClusterStateService)10 ClusterState (org.opensearch.cluster.ClusterState)5 Metadata (org.opensearch.cluster.metadata.Metadata)5 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)4 ClusterSettings (org.opensearch.common.settings.ClusterSettings)4 Settings (org.opensearch.common.settings.Settings)4 Path (java.nio.file.Path)3 Before (org.junit.Before)3 OpenSearchException (org.opensearch.OpenSearchException)3 Matchers.containsString (org.hamcrest.Matchers.containsString)2 UserException (org.opensearch.cli.UserException)2 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 Arrays (java.util.Arrays)1 Objects (java.util.Objects)1 Set (java.util.Set)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 OptionParser (joptsimple.OptionParser)1 OptionSet (joptsimple.OptionSet)1