Search in sources :

Example 1 with Mapping

use of org.opensearch.index.mapper.Mapping in project OpenSearch by opensearch-project.

the class InternalEngineTests method dynamicUpdate.

private Mapping dynamicUpdate() {
    BuilderContext context = new BuilderContext(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build(), new ContentPath());
    final RootObjectMapper root = new RootObjectMapper.Builder("some_type").build(context);
    return new Mapping(Version.CURRENT, root, new MetadataFieldMapper[0], emptyMap());
}
Also used : RootObjectMapper(org.opensearch.index.mapper.RootObjectMapper) BuilderContext(org.opensearch.index.mapper.Mapper.BuilderContext) Mapping(org.opensearch.index.mapper.Mapping) ContentPath(org.opensearch.index.mapper.ContentPath)

Example 2 with Mapping

use of org.opensearch.index.mapper.Mapping in project OpenSearch by opensearch-project.

the class TransportShardBulkActionTests method testRetries.

public void testRetries() throws Exception {
    IndexSettings indexSettings = new IndexSettings(indexMetadata(), Settings.EMPTY);
    UpdateRequest writeRequest = new UpdateRequest("index", "id").doc(Requests.INDEX_CONTENT_TYPE, "field", "value");
    // the beating will continue until success has come.
    writeRequest.retryOnConflict(Integer.MAX_VALUE);
    BulkItemRequest primaryRequest = new BulkItemRequest(0, writeRequest);
    IndexRequest updateResponse = new IndexRequest("index").id("id").source(Requests.INDEX_CONTENT_TYPE, "field", "value");
    Exception err = new VersionConflictEngineException(shardId, "id", "I'm conflicted <(;_;)>");
    Engine.IndexResult conflictedResult = new Engine.IndexResult(err, 0);
    Engine.IndexResult mappingUpdate = new Engine.IndexResult(new Mapping(null, mock(RootObjectMapper.class), new MetadataFieldMapper[0], Collections.emptyMap()));
    Translog.Location resultLocation = new Translog.Location(42, 42, 42);
    Engine.IndexResult success = new FakeIndexResult(1, 1, 13, true, resultLocation);
    IndexShard shard = mock(IndexShard.class);
    when(shard.applyIndexOperationOnPrimary(anyLong(), any(), any(), anyLong(), anyLong(), anyLong(), anyBoolean())).thenAnswer(ir -> {
        if (randomBoolean()) {
            return conflictedResult;
        }
        if (randomBoolean()) {
            return mappingUpdate;
        } else {
            return success;
        }
    });
    when(shard.indexSettings()).thenReturn(indexSettings);
    when(shard.shardId()).thenReturn(shardId);
    when(shard.mapperService()).thenReturn(mock(MapperService.class));
    UpdateHelper updateHelper = mock(UpdateHelper.class);
    when(updateHelper.prepare(any(), eq(shard), any())).thenReturn(new UpdateHelper.Result(updateResponse, randomBoolean() ? DocWriteResponse.Result.CREATED : DocWriteResponse.Result.UPDATED, Collections.singletonMap("field", "value"), Requests.INDEX_CONTENT_TYPE));
    BulkItemRequest[] items = new BulkItemRequest[] { primaryRequest };
    BulkShardRequest bulkShardRequest = new BulkShardRequest(shardId, RefreshPolicy.NONE, items);
    final CountDownLatch latch = new CountDownLatch(1);
    TransportShardBulkAction.performOnPrimary(bulkShardRequest, shard, updateHelper, threadPool::absoluteTimeInMillis, new NoopMappingUpdatePerformer(), listener -> listener.onResponse(null), new LatchedActionListener<>(ActionTestUtils.assertNoFailureListener(result -> {
        assertThat(((WritePrimaryResult<BulkShardRequest, BulkShardResponse>) result).location, equalTo(resultLocation));
        BulkItemResponse primaryResponse = result.replicaRequest().items()[0].getPrimaryResponse();
        assertThat(primaryResponse.getItemId(), equalTo(0));
        assertThat(primaryResponse.getId(), equalTo("id"));
        assertThat(primaryResponse.getOpType(), equalTo(DocWriteRequest.OpType.UPDATE));
        DocWriteResponse response = primaryResponse.getResponse();
        assertThat(response.status(), equalTo(RestStatus.CREATED));
        assertThat(response.getSeqNo(), equalTo(13L));
    }), latch), threadPool, Names.WRITE);
    latch.await();
}
Also used : IndexSettings(org.opensearch.index.IndexSettings) Mapping(org.opensearch.index.mapper.Mapping) IndexRequest(org.opensearch.action.index.IndexRequest) Translog(org.opensearch.index.translog.Translog) UpdateHelper(org.opensearch.action.update.UpdateHelper) VersionConflictEngineException(org.opensearch.index.engine.VersionConflictEngineException) Engine(org.opensearch.index.engine.Engine) UpdateRequest(org.opensearch.action.update.UpdateRequest) IndexShard(org.opensearch.index.shard.IndexShard) DocWriteResponse(org.opensearch.action.DocWriteResponse) CountDownLatch(java.util.concurrent.CountDownLatch) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) OpenSearchStatusException(org.opensearch.OpenSearchStatusException) OpenSearchException(org.opensearch.OpenSearchException) VersionConflictEngineException(org.opensearch.index.engine.VersionConflictEngineException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) MetadataFieldMapper(org.opensearch.index.mapper.MetadataFieldMapper) MapperService(org.opensearch.index.mapper.MapperService)

Example 3 with Mapping

use of org.opensearch.index.mapper.Mapping in project OpenSearch by opensearch-project.

the class MappingUpdatedActionTests method testMappingUpdatedActionBlocks.

public void testMappingUpdatedActionBlocks() throws Exception {
    List<ActionListener<Void>> inFlightListeners = new CopyOnWriteArrayList<>();
    final MappingUpdatedAction mua = new MappingUpdatedAction(Settings.builder().put(MappingUpdatedAction.INDICES_MAX_IN_FLIGHT_UPDATES_SETTING.getKey(), 1).build(), new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), null) {

        @Override
        protected void sendUpdateMapping(Index index, Mapping mappingUpdate, ActionListener<Void> listener) {
            inFlightListeners.add(listener);
        }
    };
    PlainActionFuture<Void> fut1 = new PlainActionFuture<>();
    mua.updateMappingOnMaster(null, null, fut1);
    assertEquals(1, inFlightListeners.size());
    assertEquals(0, mua.blockedThreads());
    PlainActionFuture<Void> fut2 = new PlainActionFuture<>();
    Thread thread = new Thread(() -> {
        // blocked
        mua.updateMappingOnMaster(null, null, fut2);
    });
    thread.start();
    assertBusy(() -> assertEquals(1, mua.blockedThreads()));
    assertEquals(1, inFlightListeners.size());
    assertFalse(fut1.isDone());
    inFlightListeners.remove(0).onResponse(null);
    assertTrue(fut1.isDone());
    thread.join();
    assertEquals(0, mua.blockedThreads());
    assertEquals(1, inFlightListeners.size());
    assertFalse(fut2.isDone());
    inFlightListeners.remove(0).onResponse(null);
    assertTrue(fut2.isDone());
}
Also used : ClusterSettings(org.opensearch.common.settings.ClusterSettings) ActionListener(org.opensearch.action.ActionListener) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) Index(org.opensearch.index.Index) Mapping(org.opensearch.index.mapper.Mapping) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 4 with Mapping

use of org.opensearch.index.mapper.Mapping in project OpenSearch by opensearch-project.

the class IndexShard method applyDeleteOperation.

private Engine.DeleteResult applyDeleteOperation(Engine engine, long seqNo, long opPrimaryTerm, long version, String type, String id, @Nullable VersionType versionType, long ifSeqNo, long ifPrimaryTerm, Engine.Operation.Origin origin) throws IOException {
    assert opPrimaryTerm <= getOperationPrimaryTerm() : "op term [ " + opPrimaryTerm + " ] > shard term [" + getOperationPrimaryTerm() + "]";
    ensureWriteAllowed(origin);
    // TODO: clean this up when types are gone
    try {
        Mapping update = docMapper(type).getMapping();
        if (update != null) {
            return new Engine.DeleteResult(update);
        }
    } catch (MapperParsingException | IllegalArgumentException | TypeMissingException e) {
        return new Engine.DeleteResult(e, version, getOperationPrimaryTerm(), seqNo, false);
    }
    if (mapperService.resolveDocumentType(type).equals(mapperService.documentMapper().type()) == false) {
        // document in the wrong type.
        throw new IllegalStateException("Deleting document from type [" + mapperService.resolveDocumentType(type) + "] while current type is [" + mapperService.documentMapper().type() + "]");
    }
    final Term uid = new Term(IdFieldMapper.NAME, Uid.encodeId(id));
    final Engine.Delete delete = prepareDelete(type, id, uid, seqNo, opPrimaryTerm, version, versionType, origin, ifSeqNo, ifPrimaryTerm);
    return delete(engine, delete);
}
Also used : MapperParsingException(org.opensearch.index.mapper.MapperParsingException) TypeMissingException(org.opensearch.indices.TypeMissingException) Mapping(org.opensearch.index.mapper.Mapping) Term(org.apache.lucene.index.Term) Engine(org.opensearch.index.engine.Engine) ReadOnlyEngine(org.opensearch.index.engine.ReadOnlyEngine)

Example 5 with Mapping

use of org.opensearch.index.mapper.Mapping in project OpenSearch by opensearch-project.

the class TranslogHandler method applyOperation.

private void applyOperation(Engine engine, Engine.Operation operation) throws IOException {
    switch(operation.operationType()) {
        case INDEX:
            Engine.Index engineIndex = (Engine.Index) operation;
            Mapping update = engineIndex.parsedDoc().dynamicMappingsUpdate();
            if (engineIndex.parsedDoc().dynamicMappingsUpdate() != null) {
                recoveredTypes.compute(engineIndex.type(), (k, mapping) -> mapping == null ? update : mapping.merge(update, MapperService.MergeReason.MAPPING_RECOVERY));
            }
            engine.index(engineIndex);
            break;
        case DELETE:
            engine.delete((Engine.Delete) operation);
            break;
        case NO_OP:
            engine.noOp((Engine.NoOp) operation);
            break;
        default:
            throw new IllegalStateException("No operation defined for [" + operation + "]");
    }
}
Also used : Mapping(org.opensearch.index.mapper.Mapping)

Aggregations

Mapping (org.opensearch.index.mapper.Mapping)12 Engine (org.opensearch.index.engine.Engine)6 MetadataFieldMapper (org.opensearch.index.mapper.MetadataFieldMapper)6 OpenSearchException (org.opensearch.OpenSearchException)5 IOException (java.io.IOException)4 IndexRequest (org.opensearch.action.index.IndexRequest)4 ClusterSettings (org.opensearch.common.settings.ClusterSettings)4 Settings (org.opensearch.common.settings.Settings)4 Index (org.opensearch.index.Index)4 MapperService (org.opensearch.index.mapper.MapperService)4 RootObjectMapper (org.opensearch.index.mapper.RootObjectMapper)4 IndexShard (org.opensearch.index.shard.IndexShard)4 Translog (org.opensearch.index.translog.Translog)4 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 OpenSearchStatusException (org.opensearch.OpenSearchStatusException)3 ActionListener (org.opensearch.action.ActionListener)3 Client (org.opensearch.client.Client)3 IndicesAdminClient (org.opensearch.client.IndicesAdminClient)3