Java Code Examples for org.elasticsearch.index.engine.Engine#Result

The following examples show how to use org.elasticsearch.index.engine.Engine#Result . 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: TransportResyncReplicationAction.java    From crate with Apache License 2.0 6 votes vote down vote up
public static Translog.Location performOnReplica(ResyncReplicationRequest request, IndexShard replica) throws Exception {
    Translog.Location location = null;
    /*
     * Operations received from resync do not have auto_id_timestamp individually, we need to bootstrap this max_seen_timestamp
     * (at least the highest timestamp from any of these operations) to make sure that we will disable optimization for the same
     * append-only requests with timestamp (sources of these operations) that are replicated; otherwise we may have duplicates.
     */
    replica.updateMaxUnsafeAutoIdTimestamp(request.getMaxSeenAutoIdTimestampOnPrimary());
    for (Translog.Operation operation : request.getOperations()) {
        final Engine.Result operationResult = replica.applyTranslogOperation(operation, Engine.Operation.Origin.REPLICA);
        if (operationResult.getResultType() == Engine.Result.Type.MAPPING_UPDATE_REQUIRED) {
            throw new TransportReplicationAction.RetryOnReplicaException(replica.shardId(),
                "Mappings are not available on the replica yet, triggered update: " + operationResult.getRequiredMappingUpdate());
        }
        location = syncOperationResultOrThrow(operationResult, location);
    }
    if (request.getTrimAboveSeqNo() != SequenceNumbers.UNASSIGNED_SEQ_NO) {
        replica.trimOperationOfPreviousPrimaryTerms(request.getTrimAboveSeqNo());
    }
    return location;
}
 
Example 2
Source File: TransportShardAction.java    From crate with Apache License 2.0 6 votes vote down vote up
protected <T extends Engine.Result> T executeOnPrimaryHandlingMappingUpdate(ShardId shardId,
                                                                            CheckedSupplier<T, IOException> execute,
                                                                            Function<Exception, T> onMappingUpdateError) throws IOException {
    T result = execute.get();
    if (result.getResultType() == Engine.Result.Type.MAPPING_UPDATE_REQUIRED) {
        try {
            mappingUpdate.updateMappings(result.getRequiredMappingUpdate(), shardId, Constants.DEFAULT_MAPPING_TYPE);
        } catch (Exception e) {
            return onMappingUpdateError.apply(e);
        }
        result = execute.get();
        if (result.getResultType() == Engine.Result.Type.MAPPING_UPDATE_REQUIRED) {
            // double mapping update. We assume that the successful mapping update wasn't yet processed on the node
            // and retry the entire request again.
            throw new ReplicationOperation.RetryOnPrimaryException(shardId,
                "Dynamic mappings are not available on the node that holds the primary yet");
        }
    }
    assert result.getFailure() instanceof ReplicationOperation.RetryOnPrimaryException == false :
        "IndexShard shouldn't use RetryOnPrimaryException. got " + result.getFailure();
    return result;
}
 
Example 3
Source File: IndexShard.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Replays translog operations from the provided translog {@code snapshot} to the current engine using the given {@code origin}.
 * The callback {@code onOperationRecovered} is notified after each translog operation is replayed successfully.
 */
int runTranslogRecovery(Engine engine, Translog.Snapshot snapshot, Engine.Operation.Origin origin,
                        Runnable onOperationRecovered) throws IOException {
    int opsRecovered = 0;
    Translog.Operation operation;
    while ((operation = snapshot.next()) != null) {
        try {
            logger.trace("[translog] recover op {}", operation);
            Engine.Result result = applyTranslogOperation(engine, operation, origin);
            switch (result.getResultType()) {
                case FAILURE:
                    throw result.getFailure();
                case MAPPING_UPDATE_REQUIRED:
                    throw new IllegalArgumentException("unexpected mapping update: " + result.getRequiredMappingUpdate());
                case SUCCESS:
                    break;
                default:
                    throw new AssertionError("Unknown result type [" + result.getResultType() + "]");
            }

            opsRecovered++;
            onOperationRecovered.run();
        } catch (Exception e) {
            if (ExceptionsHelper.status(e) == RestStatus.BAD_REQUEST) {
                // mainly for MapperParsingException and Failure to detect xcontent
                logger.info("ignoring recovery of a corrupt translog entry", e);
            } else {
                throw ExceptionsHelper.convertToRuntime(e);
            }
        }
    }
    return opsRecovered;
}
 
Example 4
Source File: TransportWriteAction.java    From crate with Apache License 2.0 5 votes vote down vote up
/** Syncs operation result to the translog or throws a shard not available failure */
protected static Location syncOperationResultOrThrow(final Engine.Result operationResult,
                                                     final Location currentLocation) throws Exception {
    final Location location;
    if (operationResult.getFailure() != null) {
        // check if any transient write operation failures should be bubbled up
        Exception failure = operationResult.getFailure();
        assert failure instanceof MapperParsingException : "expected mapper parsing failures. got " + failure;
        throw failure;
    } else {
        location = locationToSync(currentLocation, operationResult.getTranslogLocation());
    }
    return location;
}
 
Example 5
Source File: IndexingMemoryController.java    From crate with Apache License 2.0 4 votes vote down vote up
/** called by IndexShard to record estimated bytes written to translog for the operation */
private void recordOperationBytes(Engine.Operation operation, Engine.Result result) {
    if (result.getResultType() == Engine.Result.Type.SUCCESS) {
        statusChecker.bytesWritten(operation.estimatedSizeInBytes());
    }
}
 
Example 6
Source File: IndexShard.java    From crate with Apache License 2.0 4 votes vote down vote up
public Engine.Result applyTranslogOperation(Translog.Operation operation, Engine.Operation.Origin origin) throws IOException {
    return applyTranslogOperation(getEngine(), operation, origin);
}
 
Example 7
Source File: IndexShard.java    From crate with Apache License 2.0 4 votes vote down vote up
private Engine.Result applyTranslogOperation(Engine engine, Translog.Operation operation,
                                             Engine.Operation.Origin origin) throws IOException {
    // If a translog op is replayed on the primary (eg. ccr), we need to use external instead of null for its version type.
    final VersionType versionType = (origin == Engine.Operation.Origin.PRIMARY) ? VersionType.EXTERNAL : null;
    final Engine.Result result;
    switch (operation.opType()) {
        case INDEX:
            final 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.
            result = applyIndexOperation(
                engine,
                index.seqNo(),
                index.primaryTerm(),
                index.version(),
                versionType,
                UNASSIGNED_SEQ_NO,
                0,
                index.getAutoGeneratedIdTimestamp(),
                true,
                origin,
                new SourceToParse(
                    shardId.getIndexName(),
                    index.id(),
                    index.source(),
                    XContentHelper.xContentType(index.source()), index.routing())
            );
            break;
        case DELETE:
            final Translog.Delete delete = (Translog.Delete) operation;
            result = applyDeleteOperation(engine, delete.seqNo(), delete.primaryTerm(), delete.version(), delete.type(), delete.id(),
                                          versionType, UNASSIGNED_SEQ_NO, 0, origin);
            break;
        case NO_OP:
            final Translog.NoOp noOp = (Translog.NoOp) operation;
            result = markSeqNoAsNoop(engine, noOp.seqNo(), noOp.primaryTerm(), noOp.reason(), origin);
            break;
        default:
            throw new IllegalStateException("No operation defined for [" + operation + "]");
    }
    return result;
}