Search in sources :

Example 1 with EngineException

use of org.opensearch.index.engine.EngineException 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) 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) 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 2 with EngineException

use of org.opensearch.index.engine.EngineException in project OpenSearch by opensearch-project.

the class StoreRecovery method internalRecoverFromStore.

/**
 * Recovers the state of the shard from the store.
 */
private void internalRecoverFromStore(IndexShard indexShard) throws IndexShardRecoveryException {
    indexShard.preRecovery();
    final RecoveryState recoveryState = indexShard.recoveryState();
    final boolean indexShouldExists = recoveryState.getRecoverySource().getType() != RecoverySource.Type.EMPTY_STORE;
    indexShard.prepareForIndexRecovery();
    SegmentInfos si = null;
    final Store store = indexShard.store();
    store.incRef();
    try {
        try {
            store.failIfCorrupted();
            try {
                si = store.readLastCommittedSegmentsInfo();
            } catch (Exception e) {
                String files = "_unknown_";
                try {
                    files = Arrays.toString(store.directory().listAll());
                } catch (Exception inner) {
                    inner.addSuppressed(e);
                    files += " (failure=" + ExceptionsHelper.detailedMessage(inner) + ")";
                }
                if (indexShouldExists) {
                    throw new IndexShardRecoveryException(shardId, "shard allocated for local recovery (post api), should exist, but doesn't, current files: " + files, e);
                }
            }
            if (si != null && indexShouldExists == false) {
                // it exists on the directory, but shouldn't exist on the FS, its a leftover (possibly dangling)
                // its a "new index create" API, we have to do something, so better to clean it than use same data
                logger.trace("cleaning existing shard, shouldn't exists");
                Lucene.cleanLuceneIndex(store.directory());
                si = null;
            }
        } catch (Exception e) {
            throw new IndexShardRecoveryException(shardId, "failed to fetch index version after copying it over", e);
        }
        if (recoveryState.getRecoverySource().getType() == RecoverySource.Type.LOCAL_SHARDS) {
            assert indexShouldExists;
            bootstrap(indexShard, store);
            writeEmptyRetentionLeasesFile(indexShard);
        } else if (indexShouldExists) {
            if (recoveryState.getRecoverySource().shouldBootstrapNewHistoryUUID()) {
                store.bootstrapNewHistory();
                writeEmptyRetentionLeasesFile(indexShard);
            }
            // since we recover from local, just fill the files and size
            final RecoveryState.Index index = recoveryState.getIndex();
            try {
                if (si != null) {
                    addRecoveredFileDetails(si, store, index);
                }
            } catch (IOException e) {
                logger.debug("failed to list file details", e);
            }
            index.setFileDetailsComplete();
        } else {
            store.createEmpty(indexShard.indexSettings().getIndexVersionCreated().luceneVersion);
            final String translogUUID = Translog.createEmptyTranslog(indexShard.shardPath().resolveTranslog(), SequenceNumbers.NO_OPS_PERFORMED, shardId, indexShard.getPendingPrimaryTerm());
            store.associateIndexWithNewTranslog(translogUUID);
            writeEmptyRetentionLeasesFile(indexShard);
            indexShard.recoveryState().getIndex().setFileDetailsComplete();
        }
        indexShard.openEngineAndRecoverFromTranslog();
        indexShard.getEngine().fillSeqNoGaps(indexShard.getPendingPrimaryTerm());
        indexShard.finalizeRecovery();
        indexShard.postRecovery("post recovery from shard_store");
    } catch (EngineException | IOException e) {
        throw new IndexShardRecoveryException(shardId, "failed to recover from gateway", e);
    } finally {
        store.decRef();
    }
}
Also used : SegmentInfos(org.apache.lucene.index.SegmentInfos) EngineException(org.opensearch.index.engine.EngineException) Store(org.opensearch.index.store.Store) Index(org.opensearch.index.Index) IOException(java.io.IOException) RecoveryState(org.opensearch.indices.recovery.RecoveryState) EngineException(org.opensearch.index.engine.EngineException) IndexShardRestoreFailedException(org.opensearch.index.snapshots.IndexShardRestoreFailedException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)2 Files (java.nio.file.Files)1 Path (java.nio.file.Path)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 SegmentInfos (org.apache.lucene.index.SegmentInfos)1 BaseDirectoryWrapper (org.apache.lucene.store.BaseDirectoryWrapper)1 Matchers.allOf (org.hamcrest.Matchers.allOf)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Matchers.either (org.hamcrest.Matchers.either)1 Matchers.equalTo (org.hamcrest.Matchers.equalTo)1 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)1 Matchers.is (org.hamcrest.Matchers.is)1 Matchers.startsWith (org.hamcrest.Matchers.startsWith)1 Before (org.junit.Before)1