Search in sources :

Example 16 with Engine

use of org.elasticsearch.index.engine.Engine in project elasticsearch by elastic.

the class IndexShard method updateAllocationIdsFromMaster.

/**
     * Notifies the service of the current allocation IDs in the cluster state. See
     * {@link GlobalCheckpointTracker#updateAllocationIdsFromMaster(Set, Set)} for details.
     *
     * @param activeAllocationIds       the allocation IDs of the currently active shard copies
     * @param initializingAllocationIds the allocation IDs of the currently initializing shard copies
     */
public void updateAllocationIdsFromMaster(final Set<String> activeAllocationIds, final Set<String> initializingAllocationIds) {
    verifyPrimary();
    final Engine engine = getEngineOrNull();
    // if the engine is not yet started, we are not ready yet and can just ignore this
    if (engine != null) {
        engine.seqNoService().updateAllocationIdsFromMaster(activeAllocationIds, initializingAllocationIds);
    }
}
Also used : Engine(org.elasticsearch.index.engine.Engine)

Example 17 with Engine

use of org.elasticsearch.index.engine.Engine in project elasticsearch by elastic.

the class IndexShard method close.

public void close(String reason, boolean flushEngine) throws IOException {
    synchronized (mutex) {
        try {
            changeState(IndexShardState.CLOSED, reason);
        } finally {
            final Engine engine = this.currentEngineReference.getAndSet(null);
            try {
                if (engine != null && flushEngine) {
                    engine.flushAndClose();
                }
            } finally {
                // playing safe here and close the engine even if the above succeeds - close can be called multiple times
                // Also closing refreshListeners to prevent us from accumulating any more listeners
                IOUtils.close(engine, refreshListeners);
                indexShardOperationsLock.close();
            }
        }
    }
}
Also used : Engine(org.elasticsearch.index.engine.Engine)

Example 18 with Engine

use of org.elasticsearch.index.engine.Engine in project elasticsearch by elastic.

the class IndexShard method flush.

public Engine.CommitId flush(FlushRequest request) throws ElasticsearchException {
    boolean waitIfOngoing = request.waitIfOngoing();
    boolean force = request.force();
    if (logger.isTraceEnabled()) {
        logger.trace("flush with {}", request);
    }
    // we allows flush while recovering, since we allow for operations to happen
    // while recovering, and we want to keep the translog at bay (up to deletes, which
    // we don't gc). Yet, we don't use flush internally to clear deletes and flush the indexwriter since
    // we use #writeIndexingBuffer for this now.
    verifyNotClosed();
    Engine engine = getEngine();
    if (engine.isRecovering()) {
        throw new IllegalIndexShardStateException(shardId(), state, "flush is only allowed if the engine is not recovery" + " from translog");
    }
    long time = System.nanoTime();
    Engine.CommitId commitId = engine.flush(force, waitIfOngoing);
    flushMetric.inc(System.nanoTime() - time);
    return commitId;
}
Also used : Engine(org.elasticsearch.index.engine.Engine)

Example 19 with Engine

use of org.elasticsearch.index.engine.Engine in project elasticsearch by elastic.

the class TranslogRecoveryPerformer method performRecoveryOperation.

/**
     * Performs a single recovery operation.
     *
     * @param allowMappingUpdates true if mapping update should be accepted (but collected). Setting it to false will
     *                            cause a {@link MapperException} to be thrown if an update
     *                            is encountered.
     */
private void performRecoveryOperation(Engine engine, Translog.Operation operation, boolean allowMappingUpdates, Engine.Operation.Origin origin) throws IOException {
    try {
        switch(operation.opType()) {
            case INDEX:
                Translog.Index index = (Translog.Index) operation;
                // we set canHaveDuplicates to true all the time such that we de-optimze the translog case and ensure that all
                // autoGeneratedID docs that are coming from the primary are updated correctly.
                Engine.Index engineIndex = IndexShard.prepareIndex(docMapper(index.type()), source(shardId.getIndexName(), index.type(), index.id(), index.source(), XContentFactory.xContentType(index.source())).routing(index.routing()).parent(index.parent()), index.seqNo(), index.primaryTerm(), index.version(), index.versionType().versionTypeForReplicationAndRecovery(), origin, index.getAutoGeneratedIdTimestamp(), true);
                maybeAddMappingUpdate(engineIndex.type(), engineIndex.parsedDoc().dynamicMappingsUpdate(), engineIndex.id(), allowMappingUpdates);
                logger.trace("[translog] recover [index] op [({}, {})] of [{}][{}]", index.seqNo(), index.primaryTerm(), index.type(), index.id());
                index(engine, engineIndex);
                break;
            case DELETE:
                Translog.Delete delete = (Translog.Delete) operation;
                Uid uid = Uid.createUid(delete.uid().text());
                logger.trace("[translog] recover [delete] op [({}, {})] of [{}][{}]", delete.seqNo(), delete.primaryTerm(), uid.type(), uid.id());
                final Engine.Delete engineDelete = new Engine.Delete(uid.type(), uid.id(), delete.uid(), delete.seqNo(), delete.primaryTerm(), delete.version(), delete.versionType().versionTypeForReplicationAndRecovery(), origin, System.nanoTime());
                delete(engine, engineDelete);
                break;
            case NO_OP:
                final Translog.NoOp noOp = (Translog.NoOp) operation;
                final long seqNo = noOp.seqNo();
                final long primaryTerm = noOp.primaryTerm();
                final String reason = noOp.reason();
                logger.trace("[translog] recover [no_op] op [({}, {})] of [{}]", seqNo, primaryTerm, reason);
                final Engine.NoOp engineNoOp = new Engine.NoOp(null, seqNo, primaryTerm, 0, VersionType.INTERNAL, origin, System.nanoTime(), reason);
                noOp(engine, engineNoOp);
                break;
            default:
                throw new IllegalStateException("No operation defined for [" + operation + "]");
        }
    } catch (ElasticsearchException e) {
        boolean hasIgnoreOnRecoveryException = false;
        ElasticsearchException current = e;
        while (true) {
            if (current instanceof IgnoreOnRecoveryEngineException) {
                hasIgnoreOnRecoveryException = true;
                break;
            }
            if (current.getCause() instanceof ElasticsearchException) {
                current = (ElasticsearchException) current.getCause();
            } else {
                break;
            }
        }
        if (!hasIgnoreOnRecoveryException) {
            throw e;
        }
    }
    operationProcessed();
}
Also used : IgnoreOnRecoveryEngineException(org.elasticsearch.index.engine.IgnoreOnRecoveryEngineException) ElasticsearchException(org.elasticsearch.ElasticsearchException) Translog(org.elasticsearch.index.translog.Translog) Uid(org.elasticsearch.index.mapper.Uid) Engine(org.elasticsearch.index.engine.Engine)

Example 20 with Engine

use of org.elasticsearch.index.engine.Engine in project elasticsearch by elastic.

the class RefreshListenersTests method setupListeners.

@Before
public void setupListeners() throws Exception {
    // Setup dependencies of the listeners
    maxListeners = randomIntBetween(1, 1000);
    listeners = new RefreshListeners(() -> maxListeners, () -> engine.refresh("too-many-listeners"), // Immediately run listeners rather than adding them to the listener thread pool like IndexShard does to simplify the test.
    Runnable::run, logger);
    // Now setup the InternalEngine which is much more complicated because we aren't mocking anything
    threadPool = new TestThreadPool(getTestName());
    IndexSettings indexSettings = IndexSettingsModule.newIndexSettings("index", Settings.EMPTY);
    ShardId shardId = new ShardId(new Index("index", "_na_"), 1);
    Directory directory = newDirectory();
    DirectoryService directoryService = new DirectoryService(shardId, indexSettings) {

        @Override
        public Directory newDirectory() throws IOException {
            return directory;
        }
    };
    store = new Store(shardId, indexSettings, directoryService, new DummyShardLock(shardId));
    IndexWriterConfig iwc = newIndexWriterConfig();
    TranslogConfig translogConfig = new TranslogConfig(shardId, createTempDir("translog"), indexSettings, BigArrays.NON_RECYCLING_INSTANCE);
    Engine.EventListener eventListener = new Engine.EventListener() {

        @Override
        public void onFailedEngine(String reason, @Nullable Exception e) {
        // we don't need to notify anybody in this test
        }
    };
    TranslogHandler translogHandler = new TranslogHandler(xContentRegistry(), shardId.getIndexName(), logger);
    EngineConfig config = new EngineConfig(EngineConfig.OpenMode.CREATE_INDEX_AND_TRANSLOG, shardId, threadPool, indexSettings, null, store, new SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy()), newMergePolicy(), iwc.getAnalyzer(), iwc.getSimilarity(), new CodecService(null, logger), eventListener, translogHandler, IndexSearcher.getDefaultQueryCache(), IndexSearcher.getDefaultQueryCachingPolicy(), translogConfig, TimeValue.timeValueMinutes(5), listeners, IndexRequest.UNSET_AUTO_GENERATED_TIMESTAMP);
    engine = new InternalEngine(config);
    listeners.setTranslog(engine.getTranslog());
}
Also used : KeepOnlyLastCommitDeletionPolicy(org.apache.lucene.index.KeepOnlyLastCommitDeletionPolicy) TranslogConfig(org.elasticsearch.index.translog.TranslogConfig) IndexSettings(org.elasticsearch.index.IndexSettings) Store(org.elasticsearch.index.store.Store) Index(org.elasticsearch.index.Index) DirectoryService(org.elasticsearch.index.store.DirectoryService) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) IOException(java.io.IOException) SnapshotDeletionPolicy(org.apache.lucene.index.SnapshotDeletionPolicy) InternalEngine(org.elasticsearch.index.engine.InternalEngine) TranslogHandler(org.elasticsearch.index.engine.InternalEngineTests.TranslogHandler) CodecService(org.elasticsearch.index.codec.CodecService) DummyShardLock(org.elasticsearch.test.DummyShardLock) EngineConfig(org.elasticsearch.index.engine.EngineConfig) Nullable(org.elasticsearch.common.Nullable) Engine(org.elasticsearch.index.engine.Engine) InternalEngine(org.elasticsearch.index.engine.InternalEngine) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Before(org.junit.Before)

Aggregations

Engine (org.elasticsearch.index.engine.Engine)20 IOException (java.io.IOException)6 ElasticsearchException (org.elasticsearch.ElasticsearchException)3 Translog (org.elasticsearch.index.translog.Translog)3 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)2 DocWriteResponse (org.elasticsearch.action.DocWriteResponse)2 IndexRequest (org.elasticsearch.action.index.IndexRequest)2 EngineConfig (org.elasticsearch.index.engine.EngineConfig)2 InternalEngine (org.elasticsearch.index.engine.InternalEngine)2 Store (org.elasticsearch.index.store.Store)2 RecoveryFailedException (org.elasticsearch.indices.recovery.RecoveryFailedException)2 FileNotFoundException (java.io.FileNotFoundException)1 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 Collections (java.util.Collections)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Future (java.util.concurrent.Future)1 TimeoutException (java.util.concurrent.TimeoutException)1 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)1 IndexCommit (org.apache.lucene.index.IndexCommit)1