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

The following examples show how to use org.elasticsearch.index.engine.Engine#IndexResult . 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: WebSocketIndexListener.java    From es-change-feed-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public void postIndex(ShardId shardId, Engine.Index index, Engine.IndexResult result) {

    ChangeEvent change=new ChangeEvent(
            shardId.getIndex().getName(),
            index.type(),
            index.id(),
            new DateTime(),
            result.isCreated() ? ChangeEvent.Operation.CREATE : ChangeEvent.Operation.INDEX,
            result.getVersion(),
            index.source()
    );

    addChange(change);
}
 
Example 2
Source File: IndexingOperationListener.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public void postIndex(ShardId shardId, Engine.Index index, Engine.IndexResult result) {
    assert index != null;
    for (IndexingOperationListener listener : listeners) {
        try {
            listener.postIndex(shardId, index, result);
        } catch (Exception e) {
            logger.warn(() -> new ParameterizedMessage("postIndex listener [{}] failed", listener), e);
        }
    }
}
 
Example 3
Source File: IndexShard.java    From crate with Apache License 2.0 5 votes vote down vote up
public Engine.IndexResult applyIndexOperationOnPrimary(long version,
                                                       VersionType versionType,
                                                       SourceToParse sourceToParse,
                                                       long ifSeqNo,
                                                       long ifPrimaryTerm,
                                                       long autoGeneratedTimestamp,
                                                       boolean isRetry)
    throws IOException {
    return applyIndexOperation(getEngine(), UNASSIGNED_SEQ_NO, operationPrimaryTerm, version, versionType, ifSeqNo,
                               ifPrimaryTerm, autoGeneratedTimestamp, isRetry, Engine.Operation.Origin.PRIMARY, sourceToParse);
}
 
Example 4
Source File: IndexShard.java    From crate with Apache License 2.0 5 votes vote down vote up
public Engine.IndexResult applyIndexOperationOnReplica(long seqNo,
                                                       long version,
                                                       long autoGeneratedTimeStamp,
                                                       boolean isRetry,
                                                       SourceToParse sourceToParse)
    throws IOException {
    return applyIndexOperation(getEngine(), seqNo, operationPrimaryTerm, version, null, UNASSIGNED_SEQ_NO, 0,
                               autoGeneratedTimeStamp, isRetry, Engine.Operation.Origin.REPLICA, sourceToParse);
}
 
Example 5
Source File: Translog.java    From crate with Apache License 2.0 5 votes vote down vote up
public Index(Engine.Index index, Engine.IndexResult indexResult) {
    this.id = index.id();
    this.type = index.type();
    this.source = index.source();
    this.routing = index.routing();
    this.seqNo = indexResult.getSeqNo();
    this.primaryTerm = index.primaryTerm();
    this.version = indexResult.getVersion();
    this.autoGeneratedIdTimestamp = index.getAutoGeneratedIdTimestamp();
}
 
Example 6
Source File: IndexingMemoryController.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public void postIndex(ShardId shardId, Engine.Index index, Engine.IndexResult result) {
    recordOperationBytes(index, result);
}
 
Example 7
Source File: IndexShard.java    From crate with Apache License 2.0 4 votes vote down vote up
private Engine.IndexResult applyIndexOperation(Engine engine,
                                               long seqNo,
                                               long opPrimaryTerm,
                                               long version,
                                               @Nullable VersionType versionType,
                                               long ifSeqNo,
                                               long ifPrimaryTerm,
                                               long autoGeneratedTimeStamp,
                                               boolean isRetry,
                                               Engine.Operation.Origin origin,
                                               SourceToParse sourceToParse) throws IOException {
    assert opPrimaryTerm <= this.operationPrimaryTerm : "op term [ " + opPrimaryTerm + " ] > shard term [" + this.operationPrimaryTerm
        + "]";
    ensureWriteAllowed(origin);
    Engine.Index operation;
    try {
        operation = prepareIndex(
            defaultDocMapper(),
            sourceToParse,
            seqNo,
            opPrimaryTerm,
            version,
            versionType,
            origin,
            autoGeneratedTimeStamp,
            isRetry,
            ifSeqNo,
            ifPrimaryTerm
        );
        Mapping update = operation.parsedDoc().dynamicMappingsUpdate();
        if (update != null) {
            return new Engine.IndexResult(update);
        }
    } catch (Exception e) {
        // We treat any exception during parsing and or mapping update as a document level failure
        // with the exception side effects of closing the shard. Since we don't have the shard, we
        // can not raise an exception that may block any replication of previous operations to the
        // replicas
        verifyNotClosed(e);
        return new Engine.IndexResult(e, version, opPrimaryTerm, seqNo);
    }

    return index(engine, operation);
}
 
Example 8
Source File: IndexShard.java    From crate with Apache License 2.0 4 votes vote down vote up
public Engine.IndexResult getFailedIndexResult(Exception e, long version) {
    return new Engine.IndexResult(e, version, operationPrimaryTerm);
}
 
Example 9
Source File: TransportShardUpsertAction.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
protected WriteReplicaResult<ShardUpsertRequest> processRequestItemsOnReplica(IndexShard indexShard, ShardUpsertRequest request) throws IOException {
    Translog.Location location = null;
    for (ShardUpsertRequest.Item item : request.items()) {
        if (item.source() == null) {
            if (logger.isTraceEnabled()) {
                logger.trace("[{} (R)] Document with id {}, has no source, primary operation must have failed",
                    indexShard.shardId(), item.id());
            }
            continue;
        }
        SourceToParse sourceToParse = new SourceToParse(
            indexShard.shardId().getIndexName(),
            item.id(),
            item.source(),
            XContentType.JSON
        );

        Engine.IndexResult indexResult = indexShard.applyIndexOperationOnReplica(
            item.seqNo(),
            item.version(),
            Translog.UNSET_AUTO_GENERATED_TIMESTAMP,
            false,
            sourceToParse
        );
        if (indexResult.getResultType() == Engine.Result.Type.MAPPING_UPDATE_REQUIRED) {
            // Even though the primary waits on all nodes to ack the mapping changes to the master
            // (see MappingUpdatedAction.updateMappingOnMaster) we still need to protect against missing mappings
            // and wait for them. The reason is concurrent requests. Request r1 which has new field f triggers a
            // mapping update. Assume that that update is first applied on the primary, and only later on the replica
            // (it’s happening concurrently). Request r2, which now arrives on the primary and which also has the new
            // field f might see the updated mapping (on the primary), and will therefore proceed to be replicated
            // to the replica. When it arrives on the replica, there’s no guarantee that the replica has already
            // applied the new mapping, so there is no other option than to wait.
            throw new TransportReplicationAction.RetryOnReplicaException(indexShard.shardId(),
                "Mappings are not available on the replica yet, triggered update: " + indexResult.getRequiredMappingUpdate());
        }
        location = indexResult.getTranslogLocation();
    }
    return new WriteReplicaResult<>(request, location, null, indexShard, logger);
}
 
Example 10
Source File: TransportShardUpsertAction.java    From crate with Apache License 2.0 4 votes vote down vote up
private Engine.IndexResult index(ShardUpsertRequest.Item item,
                                 IndexShard indexShard,
                                 boolean isRetry,
                                 long seqNo,
                                 long primaryTerm,
                                 long version) throws Exception {
    SourceToParse sourceToParse = new SourceToParse(
        indexShard.shardId().getIndexName(),
        item.id(),
        item.source(),
        XContentType.JSON
    );

    Engine.IndexResult indexResult = executeOnPrimaryHandlingMappingUpdate(
        indexShard.shardId(),
        () -> indexShard.applyIndexOperationOnPrimary(
            version,
            VersionType.INTERNAL,
            sourceToParse,
            seqNo,
            primaryTerm,
            Translog.UNSET_AUTO_GENERATED_TIMESTAMP,
            isRetry
        ),
        e -> indexShard.getFailedIndexResult(e, Versions.MATCH_ANY)
    );

    switch (indexResult.getResultType()) {
        case SUCCESS:
            // update the seqNo and version on request for the replicas
            if (logger.isTraceEnabled()) {
                logger.trace("SUCCESS - id={}, primary_term={}, seq_no={}",
                             item.id(),
                             primaryTerm,
                             indexResult.getSeqNo());
            }
            item.seqNo(indexResult.getSeqNo());
            item.version(indexResult.getVersion());
            return indexResult;

        case FAILURE:
            Exception failure = indexResult.getFailure();
            if (logger.isTraceEnabled()) {
                logger.trace("FAILURE - id={}, primary_term={}, seq_no={}",
                            item.id(),
                            primaryTerm,
                            indexResult.getSeqNo());
            }
            assert failure != null : "Failure must not be null if resultType is FAILURE";
            throw failure;

        case MAPPING_UPDATE_REQUIRED:
        default:
            throw new AssertionError("IndexResult must either succeed or fail. Required mapping updates must have been handled.");
    }
}
 
Example 11
Source File: IndexShardTestCase.java    From crate with Apache License 2.0 4 votes vote down vote up
protected Engine.IndexResult indexDoc(IndexShard shard, String id) throws IOException {
    return indexDoc(shard, id, "{}");
}
 
Example 12
Source File: IndexShardTestCase.java    From crate with Apache License 2.0 4 votes vote down vote up
protected Engine.IndexResult indexDoc(IndexShard shard, String id, String source) throws IOException {
    return indexDoc(shard, id, source, XContentType.JSON, null);
}
 
Example 13
Source File: IndexShardTestCase.java    From crate with Apache License 2.0 4 votes vote down vote up
protected Engine.IndexResult indexDoc(IndexShard shard,
                                      String id,
                                      String source,
                                      XContentType xContentType,
                                      String routing) throws IOException {
    SourceToParse sourceToParse = new SourceToParse(
        shard.shardId().getIndexName(), id, new BytesArray(source), xContentType, routing);
    Engine.IndexResult result;
    if (shard.routingEntry().primary()) {
        result = shard.applyIndexOperationOnPrimary(
            Versions.MATCH_ANY,
            VersionType.INTERNAL,
            sourceToParse,
            SequenceNumbers.UNASSIGNED_SEQ_NO,
            0,
            UNSET_AUTO_GENERATED_TIMESTAMP,
            false);
        if (result.getResultType() == Engine.Result.Type.MAPPING_UPDATE_REQUIRED) {
            updateMappings(shard, IndexMetaData.builder(shard.indexSettings().getIndexMetaData())
                .putMapping("default", result.getRequiredMappingUpdate().toString()).build());
            result = shard.applyIndexOperationOnPrimary(
                Versions.MATCH_ANY,
                VersionType.INTERNAL,
                sourceToParse,
                SequenceNumbers.UNASSIGNED_SEQ_NO,
                0,
                UNSET_AUTO_GENERATED_TIMESTAMP,
                false);
        }
        shard.sync(); // advance local checkpoint
        shard.updateLocalCheckpointForShard(shard.routingEntry().allocationId().getId(),
                                            shard.getLocalCheckpoint());
    } else {
        final long seqNo = shard.seqNoStats().getMaxSeqNo() + 1;
        shard.advanceMaxSeqNoOfUpdatesOrDeletes(seqNo); // manually replicate max_seq_no_of_updates
        result = shard.applyIndexOperationOnReplica(seqNo, 0, UNSET_AUTO_GENERATED_TIMESTAMP, false, sourceToParse);
        shard.sync(); // advance local checkpoint
        if (result.getResultType() == Engine.Result.Type.MAPPING_UPDATE_REQUIRED) {
            throw new TransportReplicationAction.RetryOnReplicaException(
                shard.shardId,
                "Mappings are not available on the replica yet, triggered update: " +
                result.getRequiredMappingUpdate());
        }
    }
    return result;
}
 
Example 14
Source File: IndexingOperationListener.java    From crate with Apache License 2.0 2 votes vote down vote up
/**
 * Called after the indexing operation occurred. Note that this is
 * also called when indexing a document did not succeed due to document
 * related failures. See {@link #postIndex(ShardId, Engine.Index, Exception)}
 * for engine level failures
 */
default void postIndex(ShardId shardId, Engine.Index index, Engine.IndexResult result) {
}