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

The following examples show how to use org.elasticsearch.index.translog.Translog#Create . 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 void innerCreateNoLock(Create create, long currentVersion, VersionValue versionValue) throws IOException {
    
        long updatedVersion;
        long expectedVersion = create.version();
        if (create.versionType().isVersionConflictForWrites(currentVersion, expectedVersion)) {
            // recovery means reindex or replay from dl
            // replay from dl: it never happens, because primary will check it
            // reindex: means user insert a new record, but reindex never go here
            if (create.origin() == Operation.Origin.RECOVERY) {
                logger.info("create origin recovery but meet create conflicts, should not happern {} {} {}", create.versionType(), currentVersion, expectedVersion);
                return;
            } else {
                throw new VersionConflictEngineException(shardId, create.type(), create.id(), currentVersion, expectedVersion);
            }
        }
        
        updatedVersion = create.versionType().updateVersion(currentVersion, expectedVersion);
        boolean doUpdate = false;
        if (create.origin() == Operation.Origin.RECOVERY) {
            updatedVersion = create.version();
            if (versionValue != null || currentVersion != Versions.NOT_FOUND) {
                doUpdate = true;
            }
        } else if (create.origin() == Operation.Origin.PRIMARY) {
            if ((versionValue != null && versionValue.delete() == false) || (versionValue == null && currentVersion != Versions.NOT_FOUND)) {
                if (create.autoGeneratedId() && create.canHaveDuplicates() && currentVersion == 1 && create.version() == Versions.MATCH_ANY) {
                    /**
                     * If bulk index request fails due to a disconnect, unavailable shard etc. then the request is
                     * retried before it actually fails. However, the documents might already be indexed.
                     * For autogenerated ids this means that a version conflict will be reported in the bulk request
                     * although the document was indexed properly.
                     * To avoid this we have to make sure that the index request is treated as an update and set updatedVersion to 1.
                     * See also discussion on https://github.com/elasticsearch/elasticsearch/pull/9125
                     */
                    doUpdate = true;
                    updatedVersion = 1;
                } else {
                    throw new DocumentAlreadyExistsException(shardId, create.type(), create.id());
                }
            }
        }

        create.updateVersion(updatedVersion);

        // If it is a write request from primary, then should write the log first
        WriteDistributedLogCallBack callBack = null;
        if (create.origin() == Operation.Origin.PRIMARY) {
            callBack = new WriteDistributedLogCallBack(new Translog.Create(create));
        }
        if (doUpdate) {
            if (create.docs().size() > 1) {
                indexWriter.updateDocuments(create.uid(), create.docs(), callBack);
            } else {
                indexWriter.updateDocument(create.uid(), create.docs().get(0), callBack);
            }
        } else {
            if (create.docs().size() > 1) {
                indexWriter.addDocuments(create.docs(), callBack);
            } else {
                indexWriter.addDocument(create.docs().get(0), callBack);
            }
        }
        if (callBack != null) {
            versionMap.putUnderLock(create.uid().bytes(), new VersionValue(updatedVersion, callBack.location));
            create.setTranslogLocation(callBack.location);
        } else {
            versionMap.putUnderLock(create.uid().bytes(), new VersionValue(updatedVersion, null));
        }
        indexingService.postCreateUnderLock(create);
}