org.elasticsearch.index.translog.Translog Java Examples

The following examples show how to use org.elasticsearch.index.translog.Translog. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: InternalEngine.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private void commitIndexWriter(IndexWriter writer, Translog translog, String syncId) throws IOException {
    try {
        Translog.TranslogGeneration translogGeneration = translog.getGeneration();
        logger.trace("committing writer with translog id [{}]  and sync id [{}] ", translogGeneration.translogFileGeneration, syncId);
        Map<String, String> commitData = new HashMap<>(2);
        commitData.put(Translog.TRANSLOG_GENERATION_KEY, Long.toString(translogGeneration.translogFileGeneration));
        commitData.put(Translog.TRANSLOG_UUID_KEY, translogGeneration.translogUUID);
        if (syncId != null) {
            commitData.put(Engine.SYNC_COMMIT_ID, syncId);
        }
        indexWriter.setCommitData(commitData);
        writer.commit();
    } catch (Throwable ex) {
        failEngine("lucene commit failed", ex);
        throw ex;
    }
}
 
Example #2
Source File: PrimaryReplicaSyncer.java    From crate with Apache License 2.0 6 votes vote down vote up
SnapshotSender(Logger logger, SyncAction syncAction, ResyncTask task, ShardId shardId, String primaryAllocationId, long primaryTerm,
               Translog.Snapshot snapshot, int chunkSizeInBytes, long startingSeqNo, long maxSeqNo,
               long maxSeenAutoIdTimestamp, ActionListener<Void> listener) {
    this.logger = logger;
    this.syncAction = syncAction;
    this.task = task;
    this.shardId = shardId;
    this.primaryAllocationId = primaryAllocationId;
    this.primaryTerm = primaryTerm;
    this.snapshot = snapshot;
    this.chunkSizeInBytes = chunkSizeInBytes;
    this.startingSeqNo = startingSeqNo;
    this.maxSeqNo = maxSeqNo;
    this.maxSeenAutoIdTimestamp = maxSeenAutoIdTimestamp;
    this.listener = listener;
    task.setTotalOperations(snapshot.totalOperations());
}
 
Example #3
Source File: RemoteRecoveryTargetHandler.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public void indexTranslogOperations(List<Translog.Operation> operations,
                                    int totalTranslogOps,
                                    long maxSeenAutoIdTimestampOnPrimary,
                                    long maxSeqNoOfDeletesOrUpdatesOnPrimary,
                                    ActionListener<Long> listener) {
    final RecoveryTranslogOperationsRequest request = new RecoveryTranslogOperationsRequest(
        recoveryId,
        shardId,
        operations,
        totalTranslogOps,
        maxSeenAutoIdTimestampOnPrimary,
        maxSeqNoOfDeletesOrUpdatesOnPrimary);
    transportService.submitRequest(
        targetNode,
        PeerRecoveryTargetService.Actions.TRANSLOG_OPS,
        request,
        translogOpsRequestOptions,
        new ActionListenerResponseHandler<>(
            ActionListener.wrap(
                r -> listener.onResponse(r.localCheckpoint), listener::onFailure),
            RecoveryTranslogOperationsResponse::new,
            ThreadPool.Names.GENERIC)
    );
}
 
Example #4
Source File: TransportIndexAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
protected Tuple<IndexResponse, IndexRequest> shardOperationOnPrimary(MetaData metaData, IndexRequest request) throws Throwable {

    // validate, if routing is required, that we got routing
    IndexMetaData indexMetaData = metaData.index(request.shardId().getIndex());
    MappingMetaData mappingMd = indexMetaData.mappingOrDefault(request.type());
    if (mappingMd != null && mappingMd.routing().required()) {
        if (request.routing() == null) {
            throw new RoutingMissingException(request.shardId().getIndex(), request.type(), request.id());
        }
    }

    IndexService indexService = indicesService.indexServiceSafe(request.shardId().getIndex());
    IndexShard indexShard = indexService.shardSafe(request.shardId().id());
    indexShard.checkDiskSpace(fsService);
    final WriteResult<IndexResponse> result = executeIndexRequestOnPrimary(null, request, indexShard, mappingUpdatedAction);
    final IndexResponse response = result.response;
    final Translog.Location location = result.location;
    processAfterWrite(request.refresh(), indexShard, location);
    return new Tuple<>(response, request);
}
 
Example #5
Source File: TranslogRecoveryPerformer.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public int recoveryFromSnapshot(Engine engine, Translog.Snapshot snapshot) throws IOException {
    Translog.Operation operation;
    int opsRecovered = 0;
    while ((operation = snapshot.next()) != null) {
        try {
            performRecoveryOperation(engine, operation, true);
            opsRecovered++;
        } catch (ElasticsearchException e) {
            if (e.status() == RestStatus.BAD_REQUEST) {
                // mainly for MapperParsingException and Failure to detect xcontent
                logger.info("ignoring recovery of a corrupt translog entry", e);
            } else {
                throw e;
            }
        }
    }
    return opsRecovered;
}
 
Example #6
Source File: InternalEngine.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
/**
 * Reads the current stored translog ID from the IW commit data. If the id is not found, recommits the current
 * translog id into lucene and returns null.
 */
@Nullable
private Translog.TranslogGeneration loadTranslogIdFromCommit(IndexWriter writer) throws IOException {
    // commit on a just opened writer will commit even if there are no changes done to it
    // we rely on that for the commit data translog id key
    final Map<String, String> commitUserData = writer.getCommitData();
    if (commitUserData.containsKey("translog_id")) {
        assert commitUserData.containsKey(Translog.TRANSLOG_UUID_KEY) == false : "legacy commit contains translog UUID";
        return new Translog.TranslogGeneration(null, Long.parseLong(commitUserData.get("translog_id")));
    } else if (commitUserData.containsKey(Translog.TRANSLOG_GENERATION_KEY)) {
        if (commitUserData.containsKey(Translog.TRANSLOG_UUID_KEY) == false) {
            throw new IllegalStateException("commit doesn't contain translog UUID");
        }
        final String translogUUID = commitUserData.get(Translog.TRANSLOG_UUID_KEY);
        final long translogGen = Long.parseLong(commitUserData.get(Translog.TRANSLOG_GENERATION_KEY));
        return new Translog.TranslogGeneration(translogUUID, translogGen);
    }
    return null;
}
 
Example #7
Source File: InternalEngine.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
protected void recoverFromTranslog(EngineConfig engineConfig, Translog.TranslogGeneration translogGeneration) throws IOException {
    int opsRecovered = 0;
    final TranslogRecoveryPerformer handler = engineConfig.getTranslogRecoveryPerformer();
    try (Translog.Snapshot snapshot = translog.newSnapshot()) {
        opsRecovered = handler.recoveryFromSnapshot(this, snapshot);
    } catch (Throwable e) {
        throw new EngineException(shardId, "failed to recover from translog", e);
    }

    // flush if we recovered something or if we have references to older translogs
    // note: if opsRecovered == 0 and we have older translogs it means they are corrupted or 0 length.
    if (opsRecovered > 0) {
        logger.trace("flushing post recovery from translog. ops recovered [{}]. committed translog id [{}]. current id [{}]",
                opsRecovered, translogGeneration == null ? null : translogGeneration.translogFileGeneration, translog
                        .currentFileGeneration());
        flush(true, true);
    } else if (translog.isCurrent(translogGeneration) == false) {
        commitIndexWriter(indexWriter, translog, lastCommittedSegmentInfos.getUserData().get(Engine.SYNC_COMMIT_ID));
    }
}
 
Example #8
Source File: ESIntegTestCase.java    From crate with Apache License 2.0 6 votes vote down vote up
private static Settings.Builder setRandomIndexTranslogSettings(Random random, Settings.Builder builder) {
    if (random.nextBoolean()) {
        builder.put(IndexSettings.INDEX_TRANSLOG_FLUSH_THRESHOLD_SIZE_SETTING.getKey(),
                new ByteSizeValue(RandomNumbers.randomIntBetween(random, 1, 300), ByteSizeUnit.MB));
    }
    if (random.nextBoolean()) {
        builder.put(IndexSettings.INDEX_TRANSLOG_FLUSH_THRESHOLD_SIZE_SETTING.getKey(),
                new ByteSizeValue(1, ByteSizeUnit.PB)); // just don't flush
    }
    if (random.nextBoolean()) {
        builder.put(IndexSettings.INDEX_TRANSLOG_DURABILITY_SETTING.getKey(),
                RandomPicks.randomFrom(random, Translog.Durability.values()));
    }

    if (random.nextBoolean()) {
        builder.put(IndexSettings.INDEX_TRANSLOG_SYNC_INTERVAL_SETTING.getKey(),
                RandomNumbers.randomIntBetween(random, 100, 5000), TimeUnit.MILLISECONDS);
    }

    return builder;
}
 
Example #9
Source File: InternalEngine.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public Translog.Snapshot newChangesSnapshot(String source, MapperService mapperService,
                                            long fromSeqNo, long toSeqNo, boolean requiredFullRange) throws IOException {
    // TODO: Should we defer the refresh until we really need it?
    ensureOpen();
    refreshIfNeeded(source, toSeqNo);
    Searcher searcher = acquireSearcher(source, SearcherScope.INTERNAL);
    try {
        LuceneChangesSnapshot snapshot = new LuceneChangesSnapshot(
            searcher, mapperService, LuceneChangesSnapshot.DEFAULT_BATCH_SIZE, fromSeqNo, toSeqNo, requiredFullRange);
        searcher = null;
        return snapshot;
    } catch (Exception e) {
        try {
            maybeFailEngine("acquire changes snapshot", e);
        } catch (Exception inner) {
            e.addSuppressed(inner);
        }
        throw e;
    } finally {
        IOUtils.close(searcher);
    }
}
 
Example #10
Source File: LocalTranslog.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public Location writeToLocal(BytesReference data) throws IOException {
    final long position;
    final long generation;
    try (ReleasableLock lock = writeLock.acquire()) {
        ensureOpen();
        if (writtenOffset > TRANSLOG_ROLLING_SIZE_BYTES) {
            IOUtils.close(writeChannel);
            tmpTranslogGeneration.incrementAndGet();
            writeChannel = FileChannel.open(this.translogPath.resolve(getFileNameFromId(tmpTranslogGeneration.get())), StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
            writtenOffset = 0;
        }
        generation = tmpTranslogGeneration.get();
        position = writtenOffset;
        try {
            data.writeTo(writeChannel);
        } catch (Throwable e) {
            throw e;
        }
        writtenOffset = writtenOffset + data.length();
    }
    return new Translog.Location(generation, position, data.length());
}
 
Example #11
Source File: LocalTranslog.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public Translog.Operation readFromLocal(Location location) {
    try (ReleasableLock lock = readLock.acquire()) {
        ensureOpen();
        long fileGeneration = location.generation;
        FileChannel readChannel = readChannels.get(fileGeneration);
        if (readChannel == null) {
            readChannel = openReader(fileGeneration);
        }
        if (readChannel == null) {
            // throw exception since could not find reader
            throw new ElasticsearchException("could not open reader for file generation {}", fileGeneration);
        }
        ByteBuffer buffer = ByteBuffer.allocate(location.size);
        Channels.readFromFileChannelWithEofException(readChannel, location.translogLocation, buffer);
        buffer.flip();
        ByteBufferStreamInput in = new ByteBufferStreamInput(buffer);
        Translog.Operation.Type type = Translog.Operation.Type.fromId(in.readByte());
        Translog.Operation operation = DistributedTranslog.newOperationFromType(type);
        operation.readFrom(in);
        in.close();
        return operation;
    } catch (IOException e) {
        logger.error("errors while read from local", e);
        throw new ElasticsearchException("failed to read source from translog location " + location, e);
    }
}
 
Example #12
Source File: BlobRecoverySourceHandler.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
/**
 * Perform phase2 of the recovery process
 * <p/>
 * Phase2 takes a snapshot of the current translog *without* acquiring the
 * write lock (however, the translog snapshot is a point-in-time view of
 * the translog). It then sends each translog operation to the target node
 * so it can be replayed into the new shard.
 */
public void phase2(Translog.Snapshot snapshot) {
    if (shard.state() == IndexShardState.CLOSED) {
        throw new IndexShardClosedException(request.shardId());
    }
    cancellableThreads.checkForCancel();

    StopWatch stopWatch = new StopWatch().start();

    logger.trace("{} recovery [phase2] to {}: sending transaction log operations", request.shardId(), request.targetNode());
    // Send all the snapshot's translog operations to the target
    int totalOperations = sendSnapshot(snapshot);
    stopWatch.stop();
    logger.trace("{} recovery [phase2] to {}: took [{}]", request.shardId(), request.targetNode(), stopWatch.totalTime());
    response.phase2Time = stopWatch.totalTime().millis();
    response.phase2Operations = totalOperations;
}
 
Example #13
Source File: RecoverySourceHandler.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
/**
 * Perform phase2 of the recovery process
 * <p>
 * Phase2 takes a snapshot of the current translog *without* acquiring the
 * write lock (however, the translog snapshot is a point-in-time view of
 * the translog). It then sends each translog operation to the target node
 * so it can be replayed into the new shard.
 */
public void phase2(Translog.Snapshot snapshot) {
    if (shard.state() == IndexShardState.CLOSED) {
        throw new IndexShardClosedException(request.shardId());
    }
    cancellableThreads.checkForCancel();

    StopWatch stopWatch = new StopWatch().start();

    logger.trace("{} recovery [phase2] to {}: sending transaction log operations", request.shardId(), request.targetNode());
    // Send all the snapshot's translog operations to the target
    int totalOperations = sendSnapshot(snapshot);
    stopWatch.stop();
    logger.trace("{} recovery [phase2] to {}: took [{}]", request.shardId(), request.targetNode(), stopWatch.totalTime());
    response.phase2Time = stopWatch.totalTime().millis();
    response.phase2Operations = totalOperations;
}
 
Example #14
Source File: RecoverySourceHandler.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
protected void prepareTargetForTranslog(final Translog.View translogView) {
    StopWatch stopWatch = new StopWatch().start();
    logger.trace("{} recovery [phase1] to {}: prepare remote engine for translog", request.shardId(), request.targetNode());
    final long startEngineStart = stopWatch.totalTime().millis();
    cancellableThreads.execute(new Interruptable() {
        @Override
        public void run() throws InterruptedException {
            // Send a request preparing the new shard's translog to receive
            // operations. This ensures the shard engine is started and disables
            // garbage collection (not the JVM's GC!) of tombstone deletes
            transportService.submitRequest(request.targetNode(), RecoveryTarget.Actions.PREPARE_TRANSLOG,
                    new RecoveryPrepareForTranslogOperationsRequest(request.recoveryId(), request.shardId(), translogView.totalOperations()),
                    TransportRequestOptions.builder().withTimeout(recoverySettings.internalActionTimeout()).build(), EmptyTransportResponseHandler.INSTANCE_SAME).txGet();
        }
    });

    stopWatch.stop();

    response.startTime = stopWatch.totalTime().millis() - startEngineStart;
    logger.trace("{} recovery [phase1] to {}: remote engine start took [{}]",
            request.shardId(), request.targetNode(), stopWatch.totalTime());
}
 
Example #15
Source File: RecoveryTranslogOperationsRequest.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void writeTo(StreamOutput out) throws IOException {
    super.writeTo(out);
    out.writeLong(recoveryId);
    shardId.writeTo(out);
    Translog.writeOperations(out, operations);
    out.writeVInt(totalTranslogOps);
}
 
Example #16
Source File: RecoverySourceHandler.java    From crate with Apache License 2.0 5 votes vote down vote up
private void sendBatch(CheckedSupplier<List<Translog.Operation>, IOException> nextBatch,
                       boolean firstBatch,
                       long targetLocalCheckpoint,
                       int totalTranslogOps,
                       long maxSeenAutoIdTimestamp,
                       long maxSeqNoOfUpdatesOrDeletes,
                       ActionListener<Long> listener) throws IOException {
    final List<Translog.Operation> operations = nextBatch.get();
    // send the leftover operations or if no operations were sent, request
    // the target to respond with its local checkpoint
    if (operations.isEmpty() == false || firstBatch) {
        cancellableThreads.execute(() -> recoveryTarget.indexTranslogOperations(
            operations, totalTranslogOps, maxSeenAutoIdTimestamp, maxSeqNoOfUpdatesOrDeletes,
            ActionListener.wrap(newCheckpoint ->
                    sendBatch(
                        nextBatch,
                        false,
                        SequenceNumbers.max(targetLocalCheckpoint, newCheckpoint),
                        totalTranslogOps,
                        maxSeenAutoIdTimestamp,
                        maxSeqNoOfUpdatesOrDeletes,
                        listener),
                listener::onFailure
            ))
        );
    } else {
        listener.onResponse(targetLocalCheckpoint);
    }
}
 
Example #17
Source File: EngineTestCase.java    From crate with Apache License 2.0 5 votes vote down vote up
protected Engine.Index replicaIndexForDoc(ParsedDocument doc,
                                          long version,
                                          long seqNo,
                                          boolean isRetry) {
    return new Engine.Index(
        newUid(doc), doc, seqNo, primaryTerm.get(), version, null,
        Engine.Operation.Origin.REPLICA, System.nanoTime(), Translog.UNSET_AUTO_GENERATED_TIMESTAMP,
        isRetry, SequenceNumbers.UNASSIGNED_SEQ_NO, 0);
}
 
Example #18
Source File: TranslogRecoveryPerformer.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Applies all operations in the iterable to the current engine and returns the number of operations applied.
 * This operation will stop applying operations once an operation failed to apply.
 *
 * Throws a {@link MapperException} to be thrown if a mapping update is encountered.
 */
int performBatchRecovery(Engine engine, Iterable<Translog.Operation> operations) {
    int numOps = 0;
    try {
        for (Translog.Operation operation : operations) {
            performRecoveryOperation(engine, operation, false);
            numOps++;
        }
        engine.getTranslog().sync();
    } catch (Throwable t) {
        throw new BatchOperationException(shardId, "failed to apply batch translog operation [" + t.getMessage() + "]", numOps, t);
    }
    return numOps;
}
 
Example #19
Source File: IndexShard.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private static Translog.Durabilty getFromSettings(ESLogger logger, Settings settings, Translog.Durabilty defaultValue) {
    final String value = settings.get(TranslogConfig.INDEX_TRANSLOG_DURABILITY, defaultValue.name());
    try {
        return Translog.Durabilty.valueOf(value.toUpperCase(Locale.ROOT));
    } catch (IllegalArgumentException ex) {
        logger.warn("Can't apply {} illegal value: {} using {} instead, use one of: {}", TranslogConfig.INDEX_TRANSLOG_DURABILITY,
                value, defaultValue, Arrays.toString(Translog.Durabilty.values()));
        return defaultValue;
    }
}
 
Example #20
Source File: RefreshListeners.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Fire some listeners. Does nothing if the list of listeners is null.
 */
private void fireListeners(List<Tuple<Translog.Location, Consumer<Boolean>>> listenersToFire) {
    if (listenersToFire != null) {
        listenerExecutor.execute(() -> {
            for (Tuple<Translog.Location, Consumer<Boolean>> listener : listenersToFire) {
                try {
                    listener.v2().accept(false);
                } catch (Exception e) {
                    logger.warn("Error firing refresh listener", e);
                }
            }
        });
    }
}
 
Example #21
Source File: test.java    From vscode-extension with MIT License 5 votes vote down vote up
private Translog openTranslog(EngineConfig engineConfig, TranslogDeletionPolicy translogDeletionPolicy,
                                    LongSupplier globalCheckpointSupplier) throws IOException {

    final TranslogConfig translogConfig = engineConfig.getTranslogConfig();
    final String translogUUID = loadTranslogUUIDFromLastCommit();
    // We expect that this shard already exists, so it must already have an existing translog else something is badly wrong!
    return new Translog(translogConfig, translogUUID, translogDeletionPolicy, globalCheckpointSupplier,
        engineConfig.getPrimaryTermSupplier());
}
 
Example #22
Source File: RecoveryTranslogOperationsRequest.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void readFrom(StreamInput in) throws IOException {
    super.readFrom(in);
    recoveryId = in.readLong();
    shardId = ShardId.readShardId(in);
    operations = Translog.readOperations(in);
    totalTranslogOps = in.readVInt();
}
 
Example #23
Source File: RecoveryTranslogOperationsRequest.java    From crate with Apache License 2.0 5 votes vote down vote up
RecoveryTranslogOperationsRequest(long recoveryId,
                                  ShardId shardId,
                                  List<Translog.Operation> operations,
                                  int totalTranslogOps,
                                  long maxSeenAutoIdTimestampOnPrimary,
                                  long maxSeqNoOfUpdatesOrDeletesOnPrimary) {
    this.recoveryId = recoveryId;
    this.shardId = shardId;
    this.operations = operations;
    this.totalTranslogOps = totalTranslogOps;
    this.maxSeenAutoIdTimestampOnPrimary = maxSeenAutoIdTimestampOnPrimary;
    this.maxSeqNoOfUpdatesOrDeletesOnPrimary = maxSeqNoOfUpdatesOrDeletesOnPrimary;
}
 
Example #24
Source File: InternalEngine.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the estimated number of history operations whose seq# at least the provided seq# in this engine.
 */
@Override
public int estimateNumberOfHistoryOperations(String source, MapperService mapperService, long startingSeqNo) throws IOException {
    if (engineConfig.getIndexSettings().isSoftDeleteEnabled()) {
        try (Translog.Snapshot snapshot = newChangesSnapshot(source, mapperService, Math.max(0, startingSeqNo), Long.MAX_VALUE, false)) {
            return snapshot.totalOperations();
        }
    } else {
        return getTranslog().estimateTotalOperationsFromMinSeq(startingSeqNo);
    }
}
 
Example #25
Source File: InternalEngine.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new history snapshot for reading operations since the provided seqno.
 * The returned snapshot can be retrieved from either Lucene index or translog files.
 */
@Override
public Translog.Snapshot readHistoryOperations(String source, MapperService mapperService, long startingSeqNo) throws IOException {
    if (engineConfig.getIndexSettings().isSoftDeleteEnabled()) {
        return newChangesSnapshot(source, mapperService, Math.max(0, startingSeqNo), Long.MAX_VALUE, false);
    } else {
        return getTranslog().newSnapshotFromMinSeqNo(startingSeqNo);
    }
}
 
Example #26
Source File: RecoveryTranslogOperationsRequest.java    From crate with Apache License 2.0 5 votes vote down vote up
public RecoveryTranslogOperationsRequest(StreamInput in) throws IOException {
    super(in);
    recoveryId = in.readLong();
    shardId = new ShardId(in);
    operations = Translog.readOperations(in, "recovery");
    totalTranslogOps = in.readVInt();
    maxSeenAutoIdTimestampOnPrimary = in.readZLong();
    maxSeqNoOfUpdatesOrDeletesOnPrimary = in.readZLong();
}
 
Example #27
Source File: SharedFSRecoverySourceHandler.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public RecoveryResponse recoverToTarget() {
   boolean engineClosed = false;
    try {
        logger.trace("{} recovery [phase1] to {}: skipping phase 1 for shared filesystem", request.shardId(), request.targetNode());
        if (isPrimaryRelocation()) {
            logger.debug("[phase1] closing engine on primary for shared filesystem recovery");
            try {
                // if we relocate we need to close the engine in order to open a new
                // IndexWriter on the other end of the relocation
                engineClosed = true;
                shard.engine().flushAndClose();
            } catch (IOException e) {
                logger.warn("close engine failed", e);
                shard.failShard("failed to close engine (phase1)", e);
            }
        }
        prepareTargetForTranslog(Translog.View.EMPTY_VIEW);
        finalizeRecovery();
        return response;
    } catch (Throwable t) {
        if (engineClosed) {
            // If the relocation fails then the primary is closed and can't be
            // used anymore... (because it's closed) that's a problem, so in
            // that case, fail the shard to reallocate a new IndexShard and
            // create a new IndexWriter
            logger.info("recovery failed for primary shadow shard, failing shard");
            // pass the failure as null, as we want to ensure the store is not marked as corrupted
            shard.failShard("primary relocation failed on shared filesystem caused by: [" + t.getMessage() + "]", null);
        } else {
            logger.info("recovery failed on shared filesystem", t);
        }
        throw t;
    }
}
 
Example #28
Source File: RecoveryTranslogOperationsRequest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public void writeTo(StreamOutput out) throws IOException {
    super.writeTo(out);
    out.writeLong(recoveryId);
    shardId.writeTo(out);
    Translog.writeOperations(out, operations);
    out.writeVInt(totalTranslogOps);
    out.writeZLong(maxSeenAutoIdTimestampOnPrimary);
    out.writeZLong(maxSeqNoOfUpdatesOrDeletesOnPrimary);
}
 
Example #29
Source File: TransportShardDeleteAction.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
protected WriteReplicaResult<ShardDeleteRequest> processRequestItemsOnReplica(IndexShard indexShard, ShardDeleteRequest request) throws IOException {
    Translog.Location translogLocation = null;
    for (ShardDeleteRequest.Item item : request.items()) {
        int location = item.location();
        if (request.skipFromLocation() == location) {
            // skipping this and all next items, the primary did not processed them (mostly due to a kill request)
            break;
        }

        // Only execute delete operation on replica if the sequence number was applied from primary.
        // If that's not the case, the delete on primary didn't succeed. Note that we still need to
        // process the other items in case of a bulk request.
        if (item.seqNo() != SequenceNumbers.UNASSIGNED_SEQ_NO) {
            Engine.DeleteResult deleteResult = indexShard.applyDeleteOperationOnReplica(item.seqNo(),
                                                                                        item.version(),
                                                                                        Constants.DEFAULT_MAPPING_TYPE,
                                                                                        item.id());

            translogLocation = deleteResult.getTranslogLocation();
            if (logger.isTraceEnabled()) {
                logger.trace("shardId={} REPLICA: successfully deleted id={}", request.shardId(), item.id());
            }
        }
    }
    return new WriteReplicaResult<>(request, translogLocation, null, indexShard, logger);
}
 
Example #30
Source File: InternalEngine.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Reads the current stored translog ID from the last commit data.
 */
@Nullable
private String loadTranslogUUIDFromLastCommit() throws IOException {
    final Map<String, String> commitUserData = store.readLastCommittedSegmentsInfo().getUserData();
    if (commitUserData.containsKey(Translog.TRANSLOG_GENERATION_KEY) == false) {
        throw new IllegalStateException("commit doesn't contain translog generation id");
    }
    return commitUserData.get(Translog.TRANSLOG_UUID_KEY);
}