Search in sources :

Example 21 with MockTerminal

use of org.opensearch.cli.MockTerminal in project OpenSearch by opensearch-project.

the class OverrideNodeVersionCommandTests method testOverwritesIfTooNew.

public void testOverwritesIfTooNew() throws Exception {
    final Version nodeVersion = NodeMetadataTests.tooNewVersion();
    PersistedClusterStateService.overrideVersion(nodeVersion, nodePaths);
    final MockTerminal mockTerminal = new MockTerminal();
    mockTerminal.addTextInput(randomFrom("y", "Y"));
    new OverrideNodeVersionCommand().processNodePaths(mockTerminal, nodePaths, 0, noOptions, environment);
    assertThat(mockTerminal.getOutput(), allOf(containsString("data loss"), containsString("You should not use this tool"), containsString(Version.CURRENT.toString()), containsString(nodeVersion.toString()), containsString(OverrideNodeVersionCommand.SUCCESS_MESSAGE)));
    expectThrows(IllegalStateException.class, () -> mockTerminal.readText(""));
    final NodeMetadata nodeMetadata = PersistedClusterStateService.nodeMetadata(nodePaths);
    assertThat(nodeMetadata.nodeVersion(), equalTo(Version.CURRENT));
}
Also used : Version(org.opensearch.Version) MockTerminal(org.opensearch.cli.MockTerminal)

Example 22 with MockTerminal

use of org.opensearch.cli.MockTerminal in project OpenSearch by opensearch-project.

the class RemoveCorruptedShardDataCommandTests method testShardLock.

public void testShardLock() throws Exception {
    indexDocs(indexShard, true);
    final RemoveCorruptedShardDataCommand command = new RemoveCorruptedShardDataCommand();
    final MockTerminal t = new MockTerminal();
    final OptionParser parser = command.getParser();
    // Try running it before the shard is closed, it should flip out because it can't acquire the lock
    try {
        final OptionSet options = parser.parse("-d", indexPath.toString());
        command.execute(t, options, environment);
        fail("expected the command to fail not being able to acquire the lock");
    } catch (Exception e) {
        assertThat(e.getMessage(), containsString("Failed to lock shard's directory"));
    }
    // close shard
    closeShards(indexShard);
    // Try running it before the shard is corrupted
    try {
        final OptionSet options = parser.parse("-d", indexPath.toString());
        command.execute(t, options, environment);
        fail("expected the command to fail not being able to find a corrupt file marker");
    } catch (OpenSearchException e) {
        assertThat(e.getMessage(), startsWith("Shard does not seem to be corrupted at"));
        assertThat(t.getOutput(), containsString("Lucene index is clean at"));
    }
}
Also used : OpenSearchException(org.opensearch.OpenSearchException) OptionSet(joptsimple.OptionSet) MockTerminal(org.opensearch.cli.MockTerminal) OptionParser(joptsimple.OptionParser) EngineException(org.opensearch.index.engine.EngineException) TranslogCorruptedException(org.opensearch.index.translog.TranslogCorruptedException) OpenSearchException(org.opensearch.OpenSearchException) IOException(java.io.IOException)

Example 23 with MockTerminal

use of org.opensearch.cli.MockTerminal in project OpenSearch by opensearch-project.

the class RemoveCorruptedShardDataCommandTests method testCorruptedIndex.

public void testCorruptedIndex() throws Exception {
    final int numDocs = indexDocs(indexShard, true);
    // close shard
    closeShards(indexShard);
    final boolean corruptSegments = randomBoolean();
    CorruptionUtils.corruptIndex(random(), indexPath, corruptSegments);
    if (randomBoolean()) {
        // test corrupted shard and add corruption marker
        final IndexShard corruptedShard = reopenIndexShard(true);
        allowShardFailures();
        expectThrows(IndexShardRecoveryException.class, () -> newStartedShard(p -> corruptedShard, true));
        closeShards(corruptedShard);
    }
    final RemoveCorruptedShardDataCommand command = new RemoveCorruptedShardDataCommand();
    final MockTerminal t = new MockTerminal();
    final OptionParser parser = command.getParser();
    // run command with dry-run
    // mean dry run
    t.addTextInput("n");
    final OptionSet options = parser.parse("-d", indexPath.toString());
    t.setVerbosity(Terminal.Verbosity.VERBOSE);
    try {
        command.execute(t, options, environment);
        fail();
    } catch (OpenSearchException e) {
        if (corruptSegments) {
            assertThat(e.getMessage(), either(is("Index is unrecoverable")).or(startsWith("unable to list commits")));
        } else {
            assertThat(e.getMessage(), containsString("aborted by user"));
        }
    } finally {
        logger.info("--> output:\n{}", t.getOutput());
    }
    if (corruptSegments == false) {
        // run command without dry-run
        t.addTextInput("y");
        command.execute(t, options, environment);
        final String output = t.getOutput();
        logger.info("--> output:\n{}", output);
        // reopen shard
        failOnShardFailures();
        final IndexShard newShard = newStartedShard(p -> reopenIndexShard(false), true);
        final Set<String> shardDocUIDs = getShardDocUIDs(newShard);
        final Matcher matcher = NUM_CORRUPT_DOCS_PATTERN.matcher(output);
        assertThat(matcher.find(), equalTo(true));
        final int expectedNumDocs = numDocs - Integer.parseInt(matcher.group("docs"));
        assertThat(shardDocUIDs.size(), equalTo(expectedNumDocs));
        closeShards(newShard);
    }
}
Also used : 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) BaseDirectoryWrapper(org.apache.lucene.tests.store.BaseDirectoryWrapper) 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) 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) Matcher(java.util.regex.Matcher) OpenSearchException(org.opensearch.OpenSearchException) Matchers.containsString(org.hamcrest.Matchers.containsString) OptionSet(joptsimple.OptionSet) MockTerminal(org.opensearch.cli.MockTerminal) OptionParser(joptsimple.OptionParser)

Example 24 with MockTerminal

use of org.opensearch.cli.MockTerminal in project OpenSearch by opensearch-project.

the class RemoveCorruptedShardDataCommandTests method testCorruptedTranslog.

public void testCorruptedTranslog() throws Exception {
    final int numDocsToKeep = indexDocs(indexShard, false);
    // close shard
    closeShards(indexShard);
    TestTranslog.corruptRandomTranslogFile(logger, random(), translogPath);
    // test corrupted shard
    final IndexShard corruptedShard = reopenIndexShard(true);
    allowShardFailures();
    // it has to fail on start up due to index.shard.check_on_startup = checksum
    final Exception exception = expectThrows(Exception.class, () -> newStartedShard(p -> corruptedShard, true));
    final Throwable cause = exception.getCause() instanceof EngineException ? exception.getCause().getCause() : exception.getCause();
    assertThat(cause, instanceOf(TranslogCorruptedException.class));
    // translog is corrupted already - do not check consistency
    closeShard(corruptedShard, false);
    final RemoveCorruptedShardDataCommand command = new RemoveCorruptedShardDataCommand();
    final MockTerminal t = new MockTerminal();
    final OptionParser parser = command.getParser();
    final OptionSet options = parser.parse("-d", translogPath.toString());
    // run command with dry-run
    // mean dry run
    t.addTextInput("n");
    t.setVerbosity(Terminal.Verbosity.VERBOSE);
    try {
        command.execute(t, options, environment);
        fail();
    } catch (OpenSearchException e) {
        assertThat(e.getMessage(), containsString("aborted by user"));
        assertThat(t.getOutput(), containsString("Continue and remove corrupted data from the shard ?"));
    }
    logger.info("--> output:\n{}", t.getOutput());
    // run command without dry-run
    t.reset();
    t.addTextInput("y");
    command.execute(t, options, environment);
    final String output = t.getOutput();
    logger.info("--> output:\n{}", output);
    // reopen shard
    failOnShardFailures();
    final IndexShard newShard = newStartedShard(p -> reopenIndexShard(false), true);
    final Set<String> shardDocUIDs = getShardDocUIDs(newShard);
    assertThat(shardDocUIDs.size(), equalTo(numDocsToKeep));
    closeShards(newShard);
}
Also used : 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) BaseDirectoryWrapper(org.apache.lucene.tests.store.BaseDirectoryWrapper) 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) 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) EngineException(org.opensearch.index.engine.EngineException) Matchers.containsString(org.hamcrest.Matchers.containsString) MockTerminal(org.opensearch.cli.MockTerminal) OptionParser(joptsimple.OptionParser) EngineException(org.opensearch.index.engine.EngineException) TranslogCorruptedException(org.opensearch.index.translog.TranslogCorruptedException) OpenSearchException(org.opensearch.OpenSearchException) IOException(java.io.IOException) TranslogCorruptedException(org.opensearch.index.translog.TranslogCorruptedException) OpenSearchException(org.opensearch.OpenSearchException) OptionSet(joptsimple.OptionSet)

Example 25 with MockTerminal

use of org.opensearch.cli.MockTerminal in project OpenSearch by opensearch-project.

the class RemoveCorruptedShardDataCommandTests method testTruncatesCleanTranslogIfRequested.

public void testTruncatesCleanTranslogIfRequested() throws Exception {
    indexDocs(indexShard, true);
    closeShards(indexShard);
    final RemoveCorruptedShardDataCommand command = new RemoveCorruptedShardDataCommand();
    final MockTerminal t = new MockTerminal();
    final OptionParser parser = command.getParser();
    final OptionSet options = parser.parse("-d", translogPath.toString(), "--" + TRUNCATE_CLEAN_TRANSLOG_FLAG);
    t.addTextInput("y");
    t.setVerbosity(Terminal.Verbosity.VERBOSE);
    command.execute(t, options, environment);
    assertThat(t.getOutput(), containsString("Lucene index is clean"));
    assertThat(t.getOutput(), containsString("Translog was not analysed and will be truncated"));
    assertThat(t.getOutput(), containsString("Creating new empty translog"));
}
Also used : OptionSet(joptsimple.OptionSet) MockTerminal(org.opensearch.cli.MockTerminal) OptionParser(joptsimple.OptionParser)

Aggregations

MockTerminal (org.opensearch.cli.MockTerminal)51 Environment (org.opensearch.env.Environment)17 Matchers.containsString (org.hamcrest.Matchers.containsString)16 Settings (org.opensearch.common.settings.Settings)16 TestEnvironment (org.opensearch.env.TestEnvironment)16 Path (java.nio.file.Path)15 OptionSet (joptsimple.OptionSet)13 OpenSearchException (org.opensearch.OpenSearchException)12 IOException (java.io.IOException)10 OptionParser (joptsimple.OptionParser)10 Version (org.opensearch.Version)10 ClusterState (org.opensearch.cluster.ClusterState)10 NodeEnvironment (org.opensearch.env.NodeEnvironment)10 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)8 TranslogCorruptedException (org.opensearch.index.translog.TranslogCorruptedException)8 Metadata (org.opensearch.cluster.metadata.Metadata)7 IndexSettings (org.opensearch.index.IndexSettings)7 Files (java.nio.file.Files)6 Arrays (java.util.Arrays)6 Matcher (java.util.regex.Matcher)6