Search in sources :

Example 6 with VersionType

use of org.opensearch.index.VersionType in project OpenSearch by opensearch-project.

the class RandomDocumentPicks method randomIngestDocument.

/**
 * Generates a document that holds random metadata and the document provided as a map argument
 */
public static IngestDocument randomIngestDocument(Random random, Map<String, Object> source) {
    String index = randomString(random);
    String id = randomString(random);
    String routing = null;
    Long version = randomNonNegtiveLong(random);
    VersionType versionType = RandomPicks.randomFrom(random, new VersionType[] { VersionType.INTERNAL, VersionType.EXTERNAL, VersionType.EXTERNAL_GTE });
    if (random.nextBoolean()) {
        routing = randomString(random);
    }
    return new IngestDocument(index, id, routing, version, versionType, source);
}
Also used : VersionType(org.opensearch.index.VersionType)

Example 7 with VersionType

use of org.opensearch.index.VersionType in project OpenSearch by opensearch-project.

the class IngestServiceTests method testExecutePropagateAllMetadataUpdates.

public void testExecutePropagateAllMetadataUpdates() throws Exception {
    final CompoundProcessor processor = mockCompoundProcessor();
    IngestService ingestService = createWithProcessors(Collections.singletonMap("mock", (factories, tag, description, config) -> processor));
    PutPipelineRequest putRequest = new PutPipelineRequest("_id", new BytesArray("{\"processors\": [{\"mock\" : {}}]}"), XContentType.JSON);
    // Start empty
    ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).build();
    ClusterState previousClusterState = clusterState;
    clusterState = IngestService.innerPut(putRequest, clusterState);
    ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState));
    final long newVersion = randomLong();
    final String versionType = randomFrom("internal", "external", "external_gt", "external_gte");
    final long ifSeqNo = randomNonNegativeLong();
    final long ifPrimaryTerm = randomNonNegativeLong();
    doAnswer((InvocationOnMock invocationOnMock) -> {
        IngestDocument ingestDocument = (IngestDocument) invocationOnMock.getArguments()[0];
        for (IngestDocument.Metadata metadata : IngestDocument.Metadata.values()) {
            if (metadata == IngestDocument.Metadata.VERSION) {
                ingestDocument.setFieldValue(metadata.getFieldName(), newVersion);
            } else if (metadata == IngestDocument.Metadata.VERSION_TYPE) {
                ingestDocument.setFieldValue(metadata.getFieldName(), versionType);
            } else if (metadata == IngestDocument.Metadata.IF_SEQ_NO) {
                ingestDocument.setFieldValue(metadata.getFieldName(), ifSeqNo);
            } else if (metadata == IngestDocument.Metadata.IF_PRIMARY_TERM) {
                ingestDocument.setFieldValue(metadata.getFieldName(), ifPrimaryTerm);
            } else {
                ingestDocument.setFieldValue(metadata.getFieldName(), "update" + metadata.getFieldName());
            }
        }
        @SuppressWarnings("unchecked") BiConsumer<IngestDocument, Exception> handler = (BiConsumer<IngestDocument, Exception>) invocationOnMock.getArguments()[1];
        handler.accept(ingestDocument, null);
        return null;
    }).when(processor).execute(any(), any());
    final IndexRequest indexRequest = new IndexRequest("_index").id("_id").source(emptyMap()).setPipeline("_id").setFinalPipeline("_none");
    @SuppressWarnings("unchecked") final BiConsumer<Integer, Exception> failureHandler = mock(BiConsumer.class);
    @SuppressWarnings("unchecked") final BiConsumer<Thread, Exception> completionHandler = mock(BiConsumer.class);
    ingestService.executeBulkRequest(1, Collections.singletonList(indexRequest), failureHandler, completionHandler, indexReq -> {
    }, Names.WRITE);
    verify(processor).execute(any(), any());
    verify(failureHandler, never()).accept(any(), any());
    verify(completionHandler, times(1)).accept(Thread.currentThread(), null);
    assertThat(indexRequest.index(), equalTo("update_index"));
    assertThat(indexRequest.id(), equalTo("update_id"));
    assertThat(indexRequest.routing(), equalTo("update_routing"));
    assertThat(indexRequest.version(), equalTo(newVersion));
    assertThat(indexRequest.versionType(), equalTo(VersionType.fromString(versionType)));
    assertThat(indexRequest.ifSeqNo(), equalTo(ifSeqNo));
    assertThat(indexRequest.ifPrimaryTerm(), equalTo(ifPrimaryTerm));
}
Also used : DeletePipelineRequest(org.opensearch.action.ingest.DeletePipelineRequest) Arrays(java.util.Arrays) Metadata(org.opensearch.cluster.metadata.Metadata) LongSupplier(java.util.function.LongSupplier) ScriptModule(org.opensearch.script.ScriptModule) BulkRequest(org.opensearch.action.bulk.BulkRequest) IntConsumer(java.util.function.IntConsumer) Level(org.apache.logging.log4j.Level) Version(org.opensearch.Version) PutPipelineRequest(org.opensearch.action.ingest.PutPipelineRequest) Mockito.argThat(org.mockito.Mockito.argThat) ScriptType(org.opensearch.script.ScriptType) ArgumentMatcher(org.mockito.ArgumentMatcher) Mockito.doThrow(org.mockito.Mockito.doThrow) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Mockito.doAnswer(org.mockito.Mockito.doAnswer) MockScriptEngine(org.opensearch.script.MockScriptEngine) DeleteRequest(org.opensearch.action.delete.DeleteRequest) ScriptService(org.opensearch.script.ScriptService) Client(org.opensearch.client.Client) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Script(org.opensearch.script.Script) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) OpenSearchParseException(org.opensearch.OpenSearchParseException) Settings(org.opensearch.common.settings.Settings) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) VersionType(org.opensearch.index.VersionType) List(java.util.List) BytesArray(org.opensearch.common.bytes.BytesArray) Matchers.equalTo(org.hamcrest.Matchers.equalTo) IndexSettings(org.opensearch.index.IndexSettings) UpdateRequest(org.opensearch.action.update.UpdateRequest) XContentType(org.opensearch.common.xcontent.XContentType) Matchers.is(org.hamcrest.Matchers.is) Mockito.any(org.mockito.Mockito.any) TransportBulkAction(org.opensearch.action.bulk.TransportBulkAction) Names(org.opensearch.threadpool.ThreadPool.Names) Mockito.eq(org.mockito.Mockito.eq) Mockito.mock(org.mockito.Mockito.mock) MockLogAppender(org.opensearch.test.MockLogAppender) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) CborXContent(org.opensearch.common.xcontent.cbor.CborXContent) ThreadPool(org.opensearch.threadpool.ThreadPool) DocWriteRequest(org.opensearch.action.DocWriteRequest) HashMap(java.util.HashMap) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) ResourceNotFoundException(org.opensearch.ResourceNotFoundException) OpenSearchExecutors(org.opensearch.common.util.concurrent.OpenSearchExecutors) AtomicReference(java.util.concurrent.atomic.AtomicReference) Requests(org.opensearch.client.Requests) ClusterState(org.opensearch.cluster.ClusterState) InvocationOnMock(org.mockito.invocation.InvocationOnMock) BiConsumer(java.util.function.BiConsumer) Mockito.anyString(org.mockito.Mockito.anyString) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) Collections.emptyMap(java.util.Collections.emptyMap) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) SetOnce(org.apache.lucene.util.SetOnce) Collections.emptySet(java.util.Collections.emptySet) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) Consumer(java.util.function.Consumer) Mockito.never(org.mockito.Mockito.never) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) ClusterService(org.opensearch.cluster.service.ClusterService) ClusterName(org.opensearch.cluster.ClusterName) Mockito.anyInt(org.mockito.Mockito.anyInt) IndexRequest(org.opensearch.action.index.IndexRequest) Comparator(java.util.Comparator) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) IngestPlugin(org.opensearch.plugins.IngestPlugin) ClusterChangedEvent(org.opensearch.cluster.ClusterChangedEvent) ClusterState(org.opensearch.cluster.ClusterState) BytesArray(org.opensearch.common.bytes.BytesArray) ClusterChangedEvent(org.opensearch.cluster.ClusterChangedEvent) Mockito.anyString(org.mockito.Mockito.anyString) IndexRequest(org.opensearch.action.index.IndexRequest) OpenSearchParseException(org.opensearch.OpenSearchParseException) ResourceNotFoundException(org.opensearch.ResourceNotFoundException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ClusterName(org.opensearch.cluster.ClusterName) PutPipelineRequest(org.opensearch.action.ingest.PutPipelineRequest) BiConsumer(java.util.function.BiConsumer)

Example 8 with VersionType

use of org.opensearch.index.VersionType in project OpenSearch by opensearch-project.

the class InternalEngineTests method testNonInternalVersioningOnPrimary.

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, 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));
                assertThat(result.getFailure().getStackTrace(), emptyArray());
            }
        } 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));
                assertThat(result.getFailure().getStackTrace(), emptyArray());
            }
        }
        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 (Engine.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) Matchers.containsString(org.hamcrest.Matchers.containsString) Term(org.apache.lucene.index.Term) VersionType(org.opensearch.index.VersionType) TotalHitCountCollector(org.apache.lucene.search.TotalHitCountCollector) HashSet(java.util.HashSet)

Example 9 with VersionType

use of org.opensearch.index.VersionType in project OpenSearch by opensearch-project.

the class TranslogHandler method convertToEngineOp.

public Engine.Operation convertToEngineOp(Translog.Operation operation, Engine.Operation.Origin origin) {
    // If a translog op is replayed on the primary (eg. ccr), we need to use external instead of null for its version type.
    final VersionType versionType = (origin == Engine.Operation.Origin.PRIMARY) ? VersionType.EXTERNAL : null;
    switch(operation.opType()) {
        case INDEX:
            final Translog.Index index = (Translog.Index) operation;
            final String indexName = mapperService.index().getName();
            final Engine.Index engineIndex = IndexShard.prepareIndex(docMapper(MapperService.SINGLE_MAPPING_NAME), new SourceToParse(indexName, index.id(), index.source(), XContentHelper.xContentType(index.source()), index.routing()), index.seqNo(), index.primaryTerm(), index.version(), versionType, origin, index.getAutoGeneratedIdTimestamp(), true, SequenceNumbers.UNASSIGNED_SEQ_NO, SequenceNumbers.UNASSIGNED_PRIMARY_TERM);
            return engineIndex;
        case DELETE:
            final Translog.Delete delete = (Translog.Delete) operation;
            return IndexShard.prepareDelete(delete.id(), delete.seqNo(), delete.primaryTerm(), delete.version(), versionType, origin, SequenceNumbers.UNASSIGNED_SEQ_NO, SequenceNumbers.UNASSIGNED_PRIMARY_TERM);
        case NO_OP:
            final Translog.NoOp noOp = (Translog.NoOp) operation;
            final Engine.NoOp engineNoOp = new Engine.NoOp(noOp.seqNo(), noOp.primaryTerm(), origin, System.nanoTime(), noOp.reason());
            return engineNoOp;
        default:
            throw new IllegalStateException("No operation defined for [" + operation + "]");
    }
}
Also used : SourceToParse(org.opensearch.index.mapper.SourceToParse) VersionType(org.opensearch.index.VersionType) Translog(org.opensearch.index.translog.Translog)

Example 10 with VersionType

use of org.opensearch.index.VersionType in project OpenSearch by opensearch-project.

the class RequestConvertersTests method setRandomVersionType.

private static void setRandomVersionType(Consumer<VersionType> setter, Map<String, String> expectedParams) {
    if (randomBoolean()) {
        VersionType versionType = randomFrom(VersionType.values());
        setter.accept(versionType);
        if (versionType != VersionType.INTERNAL) {
            expectedParams.put("version_type", versionType.name().toLowerCase(Locale.ROOT));
        }
    }
}
Also used : VersionType(org.opensearch.index.VersionType)

Aggregations

VersionType (org.opensearch.index.VersionType)13 HashMap (java.util.HashMap)4 DeleteRequest (org.opensearch.action.delete.DeleteRequest)4 IndexRequest (org.opensearch.action.index.IndexRequest)4 UpdateRequest (org.opensearch.action.update.UpdateRequest)4 Map (java.util.Map)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 XContentParser (org.opensearch.common.xcontent.XContentParser)2 FetchSourceContext (org.opensearch.search.fetch.subphase.FetchSourceContext)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Collections.emptyMap (java.util.Collections.emptyMap)1 Collections.emptySet (java.util.Collections.emptySet)1 Comparator (java.util.Comparator)1 HashSet (java.util.HashSet)1 Objects (java.util.Objects)1 ExecutorService (java.util.concurrent.ExecutorService)1