Java Code Examples for org.elasticsearch.index.translog.Translog#Index

The following examples show how to use org.elasticsearch.index.translog.Translog#Index . 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: DistributedTranslog.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a new empty translog operation for the given {@link Translog.Operation.Type}
 */
public static Translog.Operation newOperationFromType(Translog.Operation.Type type) throws IOException {
    switch (type) {
        case CREATE:
            return new Translog.Create();
        case DELETE:
            return new Translog.Delete();
        case SAVE:
            return new Translog.Index();
        default:
            throw new IOException("No type for [" + type + "]");
    }
}
 
Example 2
Source File: DLBasedEngine.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private boolean innerIndex(Index index) throws IOException {
    synchronized (dirtyLock(index.uid())) {
        final long currentVersion;
        VersionValue versionValue = versionMap.getUnderLock(index.uid().bytes());
        if (versionValue == null) {
            currentVersion = loadCurrentVersionFromIndex(index.uid());
        } else {
            if (versionValue.delete() && (
                    index.origin() == Operation.Origin.RECOVERY || 
                    engineConfig.isEnableGcDeletes() && (engineConfig.getThreadPool().estimatedTimeInMillis() - versionValue.time()) > engineConfig.getGcDeletesInMillis())) {
                currentVersion = Versions.NOT_FOUND; // deleted, and GC
            } else {
                currentVersion = versionValue.version();
            }
        }

        long expectedVersion = index.version();
        long updatedVersion;
        // If it is a write request from primary, then should write the log first
        WriteDistributedLogCallBack callBack = null;
        if (index.origin() == Operation.Origin.PRIMARY  && !index.reindex()) {
            if (index.versionType().isVersionConflictForWrites(currentVersion, expectedVersion)) {
                throw new VersionConflictEngineException(shardId, index.type(), index.id(), currentVersion, expectedVersion);
            }
            updatedVersion = index.versionType().updateVersion(currentVersion, expectedVersion);
            index.updateVersion(updatedVersion);
            callBack = new WriteDistributedLogCallBack(new Translog.Index(index));
        } else {
            // for reindex request, the expected version has to equal to current version
            if (index.reindex()) {
                if (currentVersion != expectedVersion) {
                    VersionConflictEngineException exception = new VersionConflictEngineException(shardId, index.type(), index.id(), currentVersion, expectedVersion);
                    logger.info("reindex meet version conflict, maybe user deleted or updated the doc", exception);
                    throw exception;
                }
            }
            updatedVersion = index.version();
            index.updateVersion(updatedVersion); // has to update version here
        }

        final boolean created;
        if (currentVersion == Versions.NOT_FOUND) {
            created = true;
            if (index.docs().size() > 1) {
                indexWriter.addDocuments(index.docs(), callBack);
            } else {
                indexWriter.addDocument(index.docs().get(0), callBack);
            }
        } else {
            if (versionValue != null) {
                created = versionValue.delete(); // we have a delete which is not GC'ed...
            } else {
                created = false;
            }
            if (index.docs().size() > 1) {
                indexWriter.updateDocuments(index.uid(), index.docs(), callBack);
            } else {
                indexWriter.updateDocument(index.uid(), index.docs().get(0), callBack);
            }
        }
        if (callBack != null) {
            versionMap.putUnderLock(index.uid().bytes(), new VersionValue(updatedVersion, callBack.location));
            index.setTranslogLocation(callBack.location);
        } else {
            versionMap.putUnderLock(index.uid().bytes(), new VersionValue(updatedVersion, null));
        }
        indexingService.postIndexUnderLock(index);
        return created;
    }
}
 
Example 3
Source File: TranslogLeafReader.java    From crate with Apache License 2.0 4 votes vote down vote up
TranslogLeafReader(Translog.Index operation, Version indexVersionCreated) {
    this.operation = operation;
    this.indexVersionCreated = indexVersionCreated;
}
 
Example 4
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;
}
 
Example 5
Source File: TranslogHandler.java    From crate with Apache License 2.0 4 votes vote down vote up
private Engine.Operation convertToEngineOp(Translog.Operation operation, Engine.Operation.Origin origin) {
    switch (operation.opType()) {
        case INDEX:
            final Translog.Index index = (Translog.Index) operation;
            final String indexName = mapperService.index().getName();
            return IndexShard.prepareIndex(
                docMapper(index.type()),
                new SourceToParse(
                    indexName,
                    index.id(),
                    index.source(),
                    XContentHelper.xContentType(index.source()),
                    index.routing()
                ),
                index.seqNo(),
                index.primaryTerm(),
                index.version(),
                null,
                origin,
                index.getAutoGeneratedIdTimestamp(),
                true,
                SequenceNumbers.UNASSIGNED_SEQ_NO,
                0
            );
        case DELETE:
            final Translog.Delete delete = (Translog.Delete) operation;
            return new Engine.Delete(delete.type(),
                                     delete.id(),
                                     delete.uid(),
                                     delete.seqNo(),
                                     delete.primaryTerm(),
                                     delete.version(),
                                     null,
                                     origin,
                                     System.nanoTime(),
                                     SequenceNumbers.UNASSIGNED_SEQ_NO,
                                     0);
        case NO_OP:
            final Translog.NoOp noOp = (Translog.NoOp) operation;
            return new Engine.NoOp(noOp.seqNo(), noOp.primaryTerm(), origin, System.nanoTime(), noOp.reason());
        default:
            throw new IllegalStateException("No operation defined for [" + operation + "]");
    }
}