Search in sources :

Example 11 with VersionType

use of org.elasticsearch.index.VersionType in project crate by crate.

the class InternalEngineTests method testNonInternalVersioningOnPrimary.

@Test
public void testNonInternalVersioningOnPrimary() throws IOException {
    final Set<VersionType> nonInternalVersioning = new HashSet<>(Arrays.asList(VersionType.values()));
    nonInternalVersioning.remove(VersionType.INTERNAL);
    final VersionType versionType = randomFrom(nonInternalVersioning);
    final List<Engine.Operation> ops = generateSingleDocHistory(false, versionType, false, 2, 2, 20, "1");
    final Engine.Operation lastOp = ops.get(ops.size() - 1);
    final String lastFieldValue;
    if (lastOp instanceof Engine.Index) {
        Engine.Index index = (Engine.Index) lastOp;
        lastFieldValue = index.docs().get(0).get("value");
    } else {
        // delete
        lastFieldValue = null;
    }
    // other version types don't support out of order processing.
    if (versionType == VersionType.EXTERNAL) {
        shuffle(ops, random());
    }
    long highestOpVersion = Versions.NOT_FOUND;
    long seqNo = -1;
    boolean docDeleted = true;
    for (Engine.Operation op : ops) {
        logger.info("performing [{}], v [{}], seq# [{}], term [{}]", op.operationType().name().charAt(0), op.version(), op.seqNo(), op.primaryTerm());
        if (op instanceof Engine.Index) {
            final Engine.Index index = (Engine.Index) op;
            Engine.IndexResult result = engine.index(index);
            if (op.versionType().isVersionConflictForWrites(highestOpVersion, op.version(), docDeleted) == false) {
                seqNo++;
                assertThat(result.getSeqNo(), equalTo(seqNo));
                assertThat(result.isCreated(), equalTo(docDeleted));
                assertThat(result.getVersion(), equalTo(op.version()));
                assertThat(result.getResultType(), equalTo(Engine.Result.Type.SUCCESS));
                assertThat(result.getFailure(), nullValue());
                docDeleted = false;
                highestOpVersion = op.version();
            } else {
                assertThat(result.isCreated(), equalTo(false));
                assertThat(result.getVersion(), equalTo(highestOpVersion));
                assertThat(result.getResultType(), equalTo(Engine.Result.Type.FAILURE));
                assertThat(result.getFailure(), instanceOf(VersionConflictEngineException.class));
            }
        } else {
            final Engine.Delete delete = (Engine.Delete) op;
            Engine.DeleteResult result = engine.delete(delete);
            if (op.versionType().isVersionConflictForWrites(highestOpVersion, op.version(), docDeleted) == false) {
                seqNo++;
                assertThat(result.getSeqNo(), equalTo(seqNo));
                assertThat(result.isFound(), equalTo(docDeleted == false));
                assertThat(result.getVersion(), equalTo(op.version()));
                assertThat(result.getResultType(), equalTo(Engine.Result.Type.SUCCESS));
                assertThat(result.getFailure(), nullValue());
                docDeleted = true;
                highestOpVersion = op.version();
            } else {
                assertThat(result.isFound(), equalTo(docDeleted == false));
                assertThat(result.getVersion(), equalTo(highestOpVersion));
                assertThat(result.getResultType(), equalTo(Engine.Result.Type.FAILURE));
                assertThat(result.getFailure(), instanceOf(VersionConflictEngineException.class));
            }
        }
        if (randomBoolean()) {
            engine.refresh("test");
        }
        if (randomBoolean()) {
            engine.flush();
            engine.refresh("test");
        }
    }
    assertVisibleCount(engine, docDeleted ? 0 : 1);
    if (docDeleted == false) {
        logger.info("searching for [{}]", lastFieldValue);
        try (Searcher searcher = engine.acquireSearcher("test")) {
            final TotalHitCountCollector collector = new TotalHitCountCollector();
            searcher.search(new TermQuery(new Term("value", lastFieldValue)), collector);
            assertThat(collector.getTotalHits(), equalTo(1));
        }
    }
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) Searcher(org.elasticsearch.index.engine.Engine.Searcher) IndexSearcher(org.apache.lucene.search.IndexSearcher) Matchers.containsString(org.hamcrest.Matchers.containsString) Term(org.apache.lucene.index.Term) VersionType(org.elasticsearch.index.VersionType) TotalHitCountCollector(org.apache.lucene.search.TotalHitCountCollector) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

VersionType (org.elasticsearch.index.VersionType)11 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)4 IndexRequest (org.elasticsearch.action.index.IndexRequest)4 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)4 XContentParser (org.elasticsearch.common.xcontent.XContentParser)4 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)3 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)3 GetRequest (org.elasticsearch.action.get.GetRequest)3 WriteRequest (org.elasticsearch.action.support.WriteRequest)3 Engine (org.elasticsearch.index.engine.Engine)3 HashMap (java.util.HashMap)2 HttpEntity (org.apache.http.HttpEntity)2 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)2 Term (org.apache.lucene.index.Term)2 BulkShardRequest (org.elasticsearch.action.bulk.BulkShardRequest)2 ReplicatedWriteRequest (org.elasticsearch.action.support.replication.ReplicatedWriteRequest)2 ReplicationRequest (org.elasticsearch.action.support.replication.ReplicationRequest)2 BytesReference (org.elasticsearch.common.bytes.BytesReference)2 XContentType (org.elasticsearch.common.xcontent.XContentType)2 SourceToParse (org.elasticsearch.index.mapper.SourceToParse)2