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

The following examples show how to use org.elasticsearch.common.lucene.uid.Versions. 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: DLBasedEngine.java    From Elasticsearch with 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 File: RecoverySourceHandlerTests.java    From crate with 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 #3
Source File: TransportShardUpsertAction.java    From crate with 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 #4
Source File: VersionFetchSubPhase.java    From Elasticsearch with 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 #5
Source File: InternalEngine.java    From Elasticsearch with 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 #6
Source File: test.java    From vscode-extension with 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 #7
Source File: Engine.java    From crate with 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 #8
Source File: ElasticsearchDocument.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 #9
Source File: UpdateRequest.java    From Elasticsearch with 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 #10
Source File: MappingMetaData.java    From Elasticsearch with 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 #11
Source File: RestActions.java    From Elasticsearch with 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 File: InternalEngine.java    From crate with 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 #13
Source File: Engine.java    From Elasticsearch with 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 #14
Source File: MoreLikeThisQueryBuilder.java    From Elasticsearch with 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 #15
Source File: IndicesTTLService.java    From Elasticsearch with 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 #16
Source File: test.java    From vscode-extension with 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 #17
Source File: Get.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public ExecutionPlan build(PlannerContext plannerContext,
                           ProjectionBuilder projectionBuilder,
                           int limitHint,
                           int offsetHint,
                           @Nullable OrderBy order,
                           @Nullable Integer pageSizeHint,
                           Row params,
                           SubQueryResults subQueryResults) {
    HashMap<String, Map<ShardId, List<PKAndVersion>>> idsByShardByNode = new HashMap<>();
    DocTableInfo docTableInfo = tableRelation.tableInfo();
    List<Symbol> boundOutputs = Lists2.map(
        outputs, s -> SubQueryAndParamBinder.convert(s, params, subQueryResults));
    for (DocKeys.DocKey docKey : docKeys) {
        String id = docKey.getId(plannerContext.transactionContext(), plannerContext.functions(), params, subQueryResults);
        if (id == null) {
            continue;
        }
        List<String> partitionValues = docKey.getPartitionValues(
            plannerContext.transactionContext(), plannerContext.functions(), params, subQueryResults);
        String indexName = indexName(docTableInfo, partitionValues);

        String routing = docKey.getRouting(
            plannerContext.transactionContext(), plannerContext.functions(), params, subQueryResults);
        ShardRouting shardRouting;
        try {
            shardRouting = plannerContext.resolveShard(indexName, id, routing);
        } catch (IndexNotFoundException e) {
            if (docTableInfo.isPartitioned()) {
                continue;
            }
            throw e;
        }
        String currentNodeId = shardRouting.currentNodeId();
        if (currentNodeId == null) {
            // If relocating is fast enough this will work, otherwise it will result in a shard failure which
            // will cause a statement retry
            currentNodeId = shardRouting.relocatingNodeId();
            if (currentNodeId == null) {
                throw new ShardNotFoundException(shardRouting.shardId());
            }
        }
        Map<ShardId, List<PKAndVersion>> idsByShard = idsByShardByNode.get(currentNodeId);
        if (idsByShard == null) {
            idsByShard = new HashMap<>();
            idsByShardByNode.put(currentNodeId, idsByShard);
        }
        List<PKAndVersion> pkAndVersions = idsByShard.get(shardRouting.shardId());
        if (pkAndVersions == null) {
            pkAndVersions = new ArrayList<>();
            idsByShard.put(shardRouting.shardId(), pkAndVersions);
        }
        long version = docKey
            .version(plannerContext.transactionContext(), plannerContext.functions(), params, subQueryResults)
            .orElse(Versions.MATCH_ANY);
        long sequenceNumber = docKey.sequenceNo(plannerContext.transactionContext(), plannerContext.functions(), params, subQueryResults)
            .orElse(SequenceNumbers.UNASSIGNED_SEQ_NO);
        long primaryTerm = docKey.primaryTerm(plannerContext.transactionContext(), plannerContext.functions(), params, subQueryResults)
            .orElse(SequenceNumbers.UNASSIGNED_PRIMARY_TERM);
        pkAndVersions.add(new PKAndVersion(id, version, sequenceNumber, primaryTerm));
    }
    return new Collect(
        new PKLookupPhase(
            plannerContext.jobId(),
            plannerContext.nextExecutionPhaseId(),
            docTableInfo.partitionedBy(),
            boundOutputs,
            idsByShardByNode
        ),
        TopN.NO_LIMIT,
        0,
        boundOutputs.size(),
        docKeys.size(),
        null
    );
}
 
Example #18
Source File: Translog.java    From crate with Apache License 2.0 4 votes vote down vote up
/** utility for testing */
public Delete(String type, String id, long seqNo, long primaryTerm, Term uid) {
    this(type, id, uid, seqNo, primaryTerm, Versions.MATCH_ANY);
}
 
Example #19
Source File: ShardUpsertRequest.java    From crate with Apache License 2.0 4 votes vote down vote up
boolean retryOnConflict() {
    return seqNo == SequenceNumbers.UNASSIGNED_SEQ_NO && version == Versions.MATCH_ANY;
}
 
Example #20
Source File: Translog.java    From crate with Apache License 2.0 4 votes vote down vote up
public Index(String type, String id, long seqNo, long primaryTerm, byte[] source) {
    this(type, id, seqNo, primaryTerm, Versions.MATCH_ANY, source, null, -1);
}
 
Example #21
Source File: InternalEngine.java    From crate with 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);
}
 
Example #22
Source File: Engine.java    From crate with 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 #23
Source File: EngineTestCase.java    From crate with Apache License 2.0 4 votes vote down vote up
protected Engine.Index indexForDoc(ParsedDocument doc) {
    return new Engine.Index(
        newUid(doc), doc, UNASSIGNED_SEQ_NO, primaryTerm.get(),
        Versions.MATCH_ANY, VersionType.INTERNAL, Engine.Operation.Origin.PRIMARY,
        System.nanoTime(), -1, false, UNASSIGNED_SEQ_NO, 0);
}
 
Example #24
Source File: EngineTestCase.java    From crate with Apache License 2.0 4 votes vote down vote up
public static List<Engine.Operation> generateSingleDocHistory(
    final boolean forReplica,
    final VersionType versionType,
    final boolean partialOldPrimary,
    final long primaryTerm,
    final int minOpCount,
    final int maxOpCount,
    final String docId) {
    final int numOfOps = randomIntBetween(minOpCount, maxOpCount);
    final List<Engine.Operation> ops = new ArrayList<>();
    final Term id = newUid(docId);
    final int startWithSeqNo;
    if (partialOldPrimary) {
        startWithSeqNo = randomBoolean() ? numOfOps - 1 : randomIntBetween(0, numOfOps - 1);
    } else {
        startWithSeqNo = 0;
    }
    final String valuePrefix = (forReplica ? "r_" : "p_") + docId + "_";
    final boolean incrementTermWhenIntroducingSeqNo = randomBoolean();
    for (int i = 0; i < numOfOps; i++) {
        final Engine.Operation op;
        final long version;
        switch (versionType) {
            case INTERNAL:
                version = forReplica ? i : Versions.MATCH_ANY;
                break;
            case EXTERNAL:
                version = i;
                break;
            case EXTERNAL_GTE:
                version = randomBoolean() ? Math.max(i - 1, 0) : i;
                break;
            case FORCE:
                version = randomNonNegativeLong();
                break;
            default:
                throw new UnsupportedOperationException("unknown version type: " + versionType);
        }
        if (randomBoolean()) {
            op = new Engine.Index(id, testParsedDocument(docId, null, testDocumentWithTextField(valuePrefix + i), SOURCE, null),
                                  forReplica && i >= startWithSeqNo ? i * 2 : UNASSIGNED_SEQ_NO,
                                  forReplica && i >= startWithSeqNo && incrementTermWhenIntroducingSeqNo ? primaryTerm + 1 : primaryTerm,
                                  version,
                                  forReplica ? null : versionType,
                                  forReplica ? REPLICA : PRIMARY,
                                  System.currentTimeMillis(), -1, false,
                                  UNASSIGNED_SEQ_NO, 0);
        } else {
            op = new Engine.Delete("test", docId, id,
                                   forReplica && i >= startWithSeqNo ? i * 2 : UNASSIGNED_SEQ_NO,
                                   forReplica && i >= startWithSeqNo && incrementTermWhenIntroducingSeqNo ? primaryTerm + 1 : primaryTerm,
                                   version,
                                   forReplica ? null : versionType,
                                   forReplica ? REPLICA : PRIMARY,
                                   System.currentTimeMillis(), UNASSIGNED_SEQ_NO, 0);
        }
        ops.add(op);
    }
    return ops;
}
 
Example #25
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 #26
Source File: RestActions.java    From Elasticsearch with 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 #27
Source File: InternalEngine.java    From Elasticsearch with 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 File: InternalEngine.java    From Elasticsearch with 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 #29
Source File: InternalEngine.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 (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 #30
Source File: Engine.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public Versions.DocIdAndVersion docIdAndVersion() {
    return docIdAndVersion;
}