Java Code Examples for org.elasticsearch.common.lucene.uid.Versions#NOT_FOUND

The following examples show how to use org.elasticsearch.common.lucene.uid.Versions#NOT_FOUND . These examples are extracted from open source projects. 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 Project: Elasticsearch   File: DLBasedEngine.java    License: Apache License 2.0 6 votes vote down vote up
private void innerCreate(Create create) throws IOException {
    
    if (engineConfig.isOptimizeAutoGenerateId() && create.autoGeneratedId() && !create.canHaveDuplicates()) {
        innerCreateNoLock(create, Versions.NOT_FOUND, null);
    } else {
        synchronized (dirtyLock(create.uid())) {
            final long currentVersion;
            final VersionValue versionValue;
            versionValue = versionMap.getUnderLock(create.uid().bytes());
            if (versionValue == null) {
                currentVersion = loadCurrentVersionFromIndex(create.uid());
            } else {
                if (versionValue.delete() && (
                        create.origin() == Operation.Origin.RECOVERY 
                        || engineConfig.isEnableGcDeletes() && (engineConfig.getThreadPool().estimatedTimeInMillis() - versionValue.time()) > engineConfig.getGcDeletesInMillis())) {
                    currentVersion = Versions.NOT_FOUND; // deleted, and GC
                } else {
                    currentVersion = versionValue.version();
                }
            }
            innerCreateNoLock(create, currentVersion, versionValue);
        }
    }
}
 
Example 2
Source Project: Elasticsearch   File: InternalEngine.java    License: Apache License 2.0 6 votes vote down vote up
private void innerCreate(Create create) throws IOException {
    if (engineConfig.isOptimizeAutoGenerateId() && create.autoGeneratedId() && !create.canHaveDuplicates()) {
        // We don't need to lock because this ID cannot be concurrently updated:
        innerCreateNoLock(create, Versions.NOT_FOUND, null);
    } else {
        synchronized (dirtyLock(create.uid())) {
            final long currentVersion;
            final VersionValue versionValue;
            versionValue = versionMap.getUnderLock(create.uid().bytes());
            if (versionValue == null) {
                currentVersion = loadCurrentVersionFromIndex(create.uid());
            } else {
                if (engineConfig.isEnableGcDeletes() && versionValue.delete() && (engineConfig.getThreadPool().estimatedTimeInMillis
                        () - versionValue.time()) > engineConfig.getGcDeletesInMillis()) {
                    currentVersion = Versions.NOT_FOUND; // deleted, and GC
                } else {
                    currentVersion = versionValue.version();
                }
            }
            innerCreateNoLock(create, currentVersion, versionValue);
        }
    }
}
 
Example 3
Source Project: vscode-extension   File: test.java    License: MIT License 5 votes vote down vote up
public static IndexingStrategy skipDueToVersionConflict(
        VersionConflictEngineException e, boolean currentNotFoundOrDeleted, long currentVersion, long term) {
    final IndexResult result = new IndexResult(e, currentVersion, term);
    return new IndexingStrategy(
            currentNotFoundOrDeleted, false, false, false,
        SequenceNumbers.UNASSIGNED_SEQ_NO, Versions.NOT_FOUND, result);
}
 
Example 4
Source Project: vscode-extension   File: test.java    License: MIT License 5 votes vote down vote up
public static DeletionStrategy skipDueToVersionConflict(
        VersionConflictEngineException e, long currentVersion, long term, boolean currentlyDeleted) {
    final long unassignedSeqNo = SequenceNumbers.UNASSIGNED_SEQ_NO;
    final DeleteResult deleteResult = new DeleteResult(e, currentVersion, term, unassignedSeqNo, currentlyDeleted == false);
    return new DeletionStrategy(false, false, currentlyDeleted, unassignedSeqNo,
        Versions.NOT_FOUND, deleteResult);
}
 
Example 5
Source Project: crate   File: Engine.java    License: Apache License 2.0 5 votes vote down vote up
protected Result(Operation.TYPE operationType, Mapping requiredMappingUpdate) {
    this.operationType = operationType;
    this.version = Versions.NOT_FOUND;
    this.seqNo = SequenceNumbers.UNASSIGNED_SEQ_NO;
    this.term = 0L;
    this.failure = null;
    this.requiredMappingUpdate = requiredMappingUpdate;
    this.resultType = Type.MAPPING_UPDATE_REQUIRED;
}
 
Example 6
Source Project: crate   File: InternalEngine.java    License: Apache License 2.0 5 votes vote down vote up
public static IndexingStrategy skipDueToVersionConflict(
        VersionConflictEngineException e, boolean currentNotFoundOrDeleted, long currentVersion, long term) {
    final IndexResult result = new IndexResult(e, currentVersion, term);
    return new IndexingStrategy(
            currentNotFoundOrDeleted, false, false, false,
        Versions.NOT_FOUND, result);
}
 
Example 7
Source Project: Elasticsearch   File: DLBasedEngine.java    License: 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);
}
 
Example 8
Source Project: Elasticsearch   File: DLBasedEngine.java    License: 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 9
Source Project: Elasticsearch   File: InternalEngine.java    License: 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 (engineConfig.isEnableGcDeletes() && versionValue.delete() && (engineConfig.getThreadPool().estimatedTimeInMillis() -
                    versionValue.time()) > engineConfig.getGcDeletesInMillis()) {
                currentVersion = Versions.NOT_FOUND; // deleted, and GC
            } else {
                currentVersion = versionValue.version();
            }
        }

        long updatedVersion;
        long expectedVersion = index.version();
        if (index.versionType().isVersionConflictForWrites(currentVersion, expectedVersion)) {
            if (index.origin() == Operation.Origin.RECOVERY) {
                return false;
            } else {
                throw new VersionConflictEngineException(shardId, index.type(), index.id(), currentVersion, expectedVersion);
            }
        }
        // for reindex current version has to equal to expected version or it means the doc is updated
        if (index.reindex() && 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.versionType().updateVersion(currentVersion, expectedVersion);
        final boolean created;
        index.updateVersion(updatedVersion);
        if (currentVersion == Versions.NOT_FOUND) {
            // document does not exists, we can optimize for create
            created = true;
            if (index.docs().size() > 1) {
                indexWriter.addDocuments(index.docs());
            } else {
                indexWriter.addDocument(index.docs().get(0));
            }
        } 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());
            } else {
                indexWriter.updateDocument(index.uid(), index.docs().get(0));
            }
        }
        Translog.Location translogLocation = translog.add(new Translog.Index(index));
        versionMap.putUnderLock(index.uid().bytes(), new VersionValue(updatedVersion, translogLocation));
        index.setTranslogLocation(translogLocation);
        indexingService.postIndexUnderLock(index);
        return created;
    }
}
 
Example 10
Source Project: Elasticsearch   File: InternalEngine.java    License: Apache License 2.0 4 votes vote down vote up
private void innerDelete(Delete delete) throws IOException {
    synchronized (dirtyLock(delete.uid())) {
        final long currentVersion;
        VersionValue versionValue = versionMap.getUnderLock(delete.uid().bytes());
        if (versionValue == null) {
            currentVersion = loadCurrentVersionFromIndex(delete.uid());
        } else {
            if (engineConfig.isEnableGcDeletes() && versionValue.delete() && (engineConfig.getThreadPool().estimatedTimeInMillis() -
                    versionValue.time()) > engineConfig.getGcDeletesInMillis()) {
                currentVersion = Versions.NOT_FOUND; // deleted, and GC
            } else {
                currentVersion = versionValue.version();
            }
        }

        long updatedVersion;
        long expectedVersion = delete.version();
        if (delete.versionType().isVersionConflictForWrites(currentVersion, expectedVersion)) {
            if (delete.origin() == Operation.Origin.RECOVERY) {
                return;
            } else {
                throw new VersionConflictEngineException(shardId, delete.type(), delete.id(), currentVersion, expectedVersion);
            }
        }
        updatedVersion = delete.versionType().updateVersion(currentVersion, expectedVersion);
        final boolean found;
        if (currentVersion == Versions.NOT_FOUND) {
            // doc does not exist and no prior deletes
            found = false;
        } else if (versionValue != null && versionValue.delete()) {
            // a "delete on delete", in this case, we still increment the version, log it, and return that version
            found = false;
        } else {
            // we deleted a currently existing document
            indexWriter.deleteDocuments(delete.uid());
            found = true;
        }

        delete.updateVersion(updatedVersion, found);
        Translog.Location translogLocation = translog.add(new Translog.Delete(delete));
        versionMap.putUnderLock(delete.uid().bytes(), new DeleteVersionValue(updatedVersion, engineConfig.getThreadPool()
                .estimatedTimeInMillis(), translogLocation));
        delete.setTranslogLocation(translogLocation);
        indexingService.postDeleteUnderLock(delete);
    }
}
 
Example 11
Source Project: crate   File: Engine.java    License: Apache License 2.0 4 votes vote down vote up
public NoOp(final long seqNo, final long primaryTerm, final Origin origin, final long startTime, final String reason) {
    super(null, seqNo, primaryTerm, Versions.NOT_FOUND, null, origin, startTime);
    this.reason = reason;
}
 
Example 12
Source Project: crate   File: InternalEngine.java    License: Apache License 2.0 4 votes vote down vote up
public static DeletionStrategy skipDueToVersionConflict(
        VersionConflictEngineException e, long currentVersion, long term, boolean currentlyDeleted) {
    final long unassignedSeqNo = SequenceNumbers.UNASSIGNED_SEQ_NO;
    final DeleteResult deleteResult = new DeleteResult(e, currentVersion, term, unassignedSeqNo, currentlyDeleted == false);
    return new DeletionStrategy(false, false, currentlyDeleted, Versions.NOT_FOUND, deleteResult);
}