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

The following examples show how to use org.elasticsearch.common.lucene.uid.Versions. 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   Source 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   Source File: VersionFetchSubPhase.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void hitExecute(SearchContext context, HitContext hitContext) {
    // it might make sense to cache the TermDocs on a shared fetch context and just skip here)
    // it is going to mean we work on the high level multi reader and not the lower level reader as is
    // the case below...
    long version;
    try {
        BytesRef uid = Uid.createUidAsBytes(hitContext.hit().type(), hitContext.hit().id());
        version = Versions.loadVersion(
                hitContext.readerContext().reader(),
                new Term(UidFieldMapper.NAME, uid)
        );
    } catch (IOException e) {
        throw new ElasticsearchException("Could not query index for _version", e);
    }

    if (version < 0) {
        version = -1;
    }
    hitContext.hit().version(version);
}
 
Example 3
Source Project: Elasticsearch   Source 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 4
Source Project: crate   Source File: TransportShardUpsertAction.java    License: Apache License 2.0 6 votes vote down vote up
private static Doc getDocument(IndexShard indexShard, String id, long version, long seqNo, long primaryTerm) {
    // when sequence versioning is used, this lookup will throw VersionConflictEngineException
    Doc doc = PKLookupOperation.lookupDoc(indexShard, id, Versions.MATCH_ANY, VersionType.INTERNAL, seqNo, primaryTerm);
    if (doc == null) {
        throw new DocumentMissingException(indexShard.shardId(), Constants.DEFAULT_MAPPING_TYPE, id);
    }
    if (doc.getSource() == null) {
        throw new DocumentSourceMissingException(indexShard.shardId(), Constants.DEFAULT_MAPPING_TYPE, id);
    }
    if (version != Versions.MATCH_ANY && version != doc.getVersion()) {
        throw new VersionConflictEngineException(
            indexShard.shardId(),
            id,
            "Requested version: " + version + " but got version: " + doc.getVersion());
    }
    return doc;
}
 
Example 5
Source Project: crate   Source File: RecoverySourceHandlerTests.java    License: Apache License 2.0 6 votes vote down vote up
private Engine.Index getIndex(final String id) {
    final String type = "test";
    final ParseContext.Document document = new ParseContext.Document();
    document.add(new TextField("test", "test", Field.Store.YES));
    final Field idField = new Field("_id", Uid.encodeId(id), IdFieldMapper.Defaults.FIELD_TYPE);
    final Field versionField = new NumericDocValuesField("_version", Versions.MATCH_ANY);
    final SeqNoFieldMapper.SequenceIDFields seqID = SeqNoFieldMapper.SequenceIDFields.emptySeqID();
    document.add(idField);
    document.add(versionField);
    document.add(seqID.seqNo);
    document.add(seqID.seqNoDocValue);
    document.add(seqID.primaryTerm);
    final BytesReference source = new BytesArray(new byte[] { 1 });
    final ParsedDocument doc =
        new ParsedDocument(versionField, seqID, id, type, List.of(document), source, null);
    return new Engine.Index(
        new Term("_id", Uid.encodeId(doc.id())), doc, UNASSIGNED_SEQ_NO, 0,
        Versions.MATCH_ANY, VersionType.INTERNAL, PRIMARY, System.nanoTime(), -1, false, UNASSIGNED_SEQ_NO, 0);

}
 
Example 6
Source Project: vscode-extension   Source 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 7
Source Project: vscode-extension   Source 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 8
Source Project: Elasticsearch   Source File: IndicesTTLService.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void collect(int doc) {
    try {
        FieldsVisitor fieldsVisitor = new FieldsVisitor(false);
        context.reader().document(doc, fieldsVisitor);
        Uid uid = fieldsVisitor.uid();
        final long version = Versions.loadVersion(context.reader(), new Term(UidFieldMapper.NAME, uid.toBytesRef()));
        docsToPurge.add(new DocToPurge(uid.type(), uid.id(), version, fieldsVisitor.routing()));
    } catch (Exception e) {
        logger.trace("failed to collect doc", e);
    }
}
 
Example 9
Source Project: Elasticsearch   Source File: MoreLikeThisQueryBuilder.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject();
    if (this.index != null) {
        builder.field(Field.INDEX.getPreferredName(), this.index);
    }
    if (this.type != null) {
        builder.field(Field.TYPE.getPreferredName(), this.type);
    }
    if (this.id != null && this.doc == null) {
        builder.field(Field.ID.getPreferredName(), this.id);
    }
    if (this.doc != null) {
        XContentType contentType = XContentFactory.xContentType(this.doc);
        if (contentType == builder.contentType()) {
            builder.rawField(Field.DOC.getPreferredName(), this.doc);
        } else {
            XContentParser parser = XContentFactory.xContent(contentType).createParser(this.doc);
            parser.nextToken();
            builder.field(Field.DOC.getPreferredName());
            builder.copyCurrentStructure(parser);
        }
    }
    if (this.fields != null) {
        builder.array(Field.FIELDS.getPreferredName(), this.fields);
    }
    if (this.perFieldAnalyzer != null) {
        builder.field(Field.PER_FIELD_ANALYZER.getPreferredName(), this.perFieldAnalyzer);
    }
    if (this.routing != null) {
        builder.field(Field.ROUTING.getPreferredName(), this.routing);
    }
    if (this.version != Versions.MATCH_ANY) {
        builder.field(Field.VERSION.getPreferredName(), this.version);
    }
    if (this.versionType != VersionType.INTERNAL) {
        builder.field(Field.VERSION_TYPE.getPreferredName(), this.versionType.toString().toLowerCase(Locale.ROOT));
    }
    return builder.endObject();
}
 
Example 10
Source Project: Elasticsearch   Source File: Engine.java    License: Apache License 2.0 5 votes vote down vote up
public GetResult(Searcher searcher, Versions.DocIdAndVersion docIdAndVersion) {
    this.exists = true;
    this.source = null;
    this.version = docIdAndVersion.version;
    this.docIdAndVersion = docIdAndVersion;
    this.searcher = searcher;
}
 
Example 11
Source Project: Elasticsearch   Source File: RestActions.java    License: Apache License 2.0 5 votes vote down vote up
public static long parseVersion(RestRequest request) {
    if (request.hasParam("version")) {
        return request.paramAsLong("version", Versions.MATCH_ANY);
    }
    String ifMatch = request.header("If-Match");
    if (ifMatch != null) {
        return Long.parseLong(ifMatch);
    }
    return Versions.MATCH_ANY;
}
 
Example 12
Source Project: Elasticsearch   Source File: MappingMetaData.java    License: Apache License 2.0 5 votes vote down vote up
public ParseContext createParseContext(@Nullable String id, @Nullable String routing, @Nullable String timestamp, long version) {
    // We parse the routing even if there is already a routing key in the request in order to make sure that
    // they are the same
    return new ParseContext(
            id == null && id().hasPath(),
            routing().hasPath(),
            timestamp == null && timestamp().hasPath(),
            version == Versions.MATCH_ANY && version().hasPath()
    );
}
 
Example 13
Source Project: Elasticsearch   Source File: UpdateRequest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ActionRequestValidationException validate() {
    ActionRequestValidationException validationException = super.validate();
    if (type == null) {
        validationException = addValidationError("type is missing", validationException);
    }
    if (id == null) {
        validationException = addValidationError("id is missing", validationException);
    }

    if (!(versionType == VersionType.INTERNAL || versionType == VersionType.FORCE)) {
        validationException = addValidationError("version type [" + versionType + "] is not supported by the update API", validationException);
    } else {

        if (version != Versions.MATCH_ANY && retryOnConflict > 0) {
            validationException = addValidationError("can't provide both retry_on_conflict and a specific version", validationException);
        }

        if (!versionType.validateVersionForWrites(version)) {
            validationException = addValidationError("illegal version value [" + version + "] for version type [" + versionType.name() + "]", validationException);
        }
    }

    if (script == null && doc == null) {
        validationException = addValidationError("script or doc is missing", validationException);
    }
    if (script != null && doc != null) {
        validationException = addValidationError("can't provide both script and doc", validationException);
    }
    if (doc == null && docAsUpsert) {
        validationException = addValidationError("doc must be specified if doc_as_upsert is enabled", validationException);
    }
    return validationException;
}
 
Example 14
public ElasticsearchDocument(String id, String type, String index, String resourceId, String context,
		Function<? super String, ? extends SpatialContext> geoContextMapper) {
	this(id, type, index, Versions.MATCH_ANY, new HashMap<>(), geoContextMapper);
	fields.put(SearchFields.URI_FIELD_NAME, resourceId);
	if (context != null) {
		fields.put(SearchFields.CONTEXT_FIELD_NAME, context);
	}
}
 
Example 15
Source Project: crate   Source 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 16
Source Project: crate   Source 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 17
Source Project: Elasticsearch   Source 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 18
Source Project: Elasticsearch   Source 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 19
Source Project: Elasticsearch   Source File: DLBasedEngine.java    License: Apache License 2.0 4 votes vote down vote up
private long loadCurrentVersionFromIndex(Term uid) throws IOException {
    try (final Searcher searcher = acquireSearcher("load_version", false)) {
        return Versions.loadVersion(searcher.reader(), uid);
    }
}
 
Example 20
Source Project: Elasticsearch   Source File: ShardUpsertRequest.java    License: Apache License 2.0 4 votes vote down vote up
public boolean retryOnConflict() {
    return version == Versions.MATCH_ANY;
}
 
Example 21
Source Project: Elasticsearch   Source File: TransportShardUpsertAction.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Prepares an update request by converting it into an index request.
 * <p/>
 * TODO: detect a NOOP and return an update response if true
 */
@SuppressWarnings("unchecked")
private SourceAndVersion prepareUpdate(DocTableInfo tableInfo,
                                       ShardUpsertRequest request,
                                       ShardUpsertRequest.Item item,
                                       IndexShard indexShard) throws ElasticsearchException {
    final GetResult getResult = indexShard.getService().get(request.type(), item.id(),
            new String[]{RoutingFieldMapper.NAME, ParentFieldMapper.NAME, TTLFieldMapper.NAME},
            true, Versions.MATCH_ANY, VersionType.INTERNAL, FetchSourceContext.FETCH_SOURCE, false);

    if (!getResult.isExists()) {
        throw new DocumentMissingException(new ShardId(request.index(), request.shardId().id()), request.type(), item.id());
    }

    if (getResult.internalSourceRef() == null) {
        // no source, we can't do nothing, through a failure...
        throw new DocumentSourceMissingException(new ShardId(request.index(), request.shardId().id()), request.type(), item.id());
    }

    if (item.version() != Versions.MATCH_ANY && item.version() != getResult.getVersion()) {
        throw new VersionConflictEngineException(
                indexShard.shardId(), Constants.DEFAULT_MAPPING_TYPE, item.id(), getResult.getVersion(), item.version());
    }

    Tuple<XContentType, Map<String, Object>> sourceAndContent = XContentHelper.convertToMap(getResult.internalSourceRef(), true);
    final Map<String, Object> updatedSourceAsMap;
    final XContentType updateSourceContentType = sourceAndContent.v1();

    updatedSourceAsMap = sourceAndContent.v2();

    SymbolToFieldExtractorContext ctx = new SymbolToFieldExtractorContext(functions, item.insertValues());

    Map<String, Object> pathsToUpdate = new LinkedHashMap<>();
    Map<String, Object> updatedGeneratedColumns = new LinkedHashMap<>();
    for (int i = 0; i < request.updateColumns().length; i++) {
        /**
         * NOTE: mapping isn't applied. So if an Insert was done using the ES Rest Endpoint
         * the data might be returned in the wrong format (date as string instead of long)
         */
        String columnPath = request.updateColumns()[i];
        Object value = SYMBOL_TO_FIELD_EXTRACTOR.convert(item.updateAssignments()[i], ctx).apply(getResult);
        ReferenceInfo referenceInfo = tableInfo.getReferenceInfo(ColumnIdent.fromPath(columnPath));
        if (referenceInfo instanceof GeneratedReferenceInfo) {
            updatedGeneratedColumns.put(columnPath, value);

        } else {
            pathsToUpdate.put(columnPath, value);
        }
    }

    processGeneratedColumns(tableInfo, pathsToUpdate, updatedGeneratedColumns, request.validateGeneratedColumns(), getResult);

    updateSourceByPaths(updatedSourceAsMap, pathsToUpdate);

    try {
        XContentBuilder builder = XContentFactory.contentBuilder(updateSourceContentType);
        builder.map(updatedSourceAsMap);
        return new SourceAndVersion(builder.bytes(), getResult.getVersion());
    } catch (IOException e) {
        throw new ElasticsearchGenerationException("Failed to generate [" + updatedSourceAsMap + "]", e);
    }
}
 
Example 22
Source Project: Elasticsearch   Source File: Engine.java    License: Apache License 2.0 4 votes vote down vote up
public IndexingOperation(Term uid, ParsedDocument doc) {
    this(uid, doc, Versions.MATCH_ANY, VersionType.INTERNAL, Origin.PRIMARY, System.nanoTime(), true);
}
 
Example 23
Source Project: Elasticsearch   Source File: Engine.java    License: Apache License 2.0 4 votes vote down vote up
public Delete(String type, String id, Term uid) {
    this(type, id, uid, Versions.MATCH_ANY, VersionType.INTERNAL, Origin.PRIMARY, System.nanoTime(), false);
}
 
Example 24
Source Project: Elasticsearch   Source File: Engine.java    License: Apache License 2.0 4 votes vote down vote up
public Versions.DocIdAndVersion docIdAndVersion() {
    return docIdAndVersion;
}
 
Example 25
Source Project: Elasticsearch   Source 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 26
Source Project: Elasticsearch   Source 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 27
Source Project: Elasticsearch   Source File: InternalEngine.java    License: Apache License 2.0 4 votes vote down vote up
private long loadCurrentVersionFromIndex(Term uid) throws IOException {
    try (final Searcher searcher = acquireSearcher("load_version", false)) {
        return Versions.loadVersion(searcher.reader(), uid);
    }
}
 
Example 28
Source Project: Elasticsearch   Source File: RestActions.java    License: Apache License 2.0 4 votes vote down vote up
public static long parseVersion(RestRequest request, long defaultVersion) {
    long version = parseVersion(request);
    return (version == Versions.MATCH_ANY) ? defaultVersion : version;
}
 
Example 29
Source Project: crate   Source 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 30
Source Project: crate   Source 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);
}