Search in sources :

Example 1 with ClusterChangedEvent

use of org.opensearch.cluster.ClusterChangedEvent in project OpenSearch by opensearch-project.

the class IndicesClusterStateServiceRandomUpdatesTests method testJoiningNewClusterOnlyRemovesInMemoryIndexStructures.

/**
 * This test ensures that when a node joins a brand new cluster (different cluster UUID),
 * different from the cluster it was previously a part of, the in-memory index data structures
 * are all removed but the on disk contents of those indices remain so that they can later be
 * imported as dangling indices.  Normally, the first cluster state update that the node
 * receives from the new cluster would contain a cluster block that would cause all in-memory
 * structures to be removed (see {@link IndicesClusterStateService#applyClusterState(ClusterChangedEvent)}),
 * but in the case where the node joined and was a few cluster state updates behind, it would
 * not have received the cluster block, in which case we still need to remove the in-memory
 * structures while ensuring the data remains on disk.  This test executes this particular
 * scenario.
 */
public void testJoiningNewClusterOnlyRemovesInMemoryIndexStructures() {
    // a cluster state derived from the initial state that includes a created index
    String name = "index_" + randomAlphaOfLength(8).toLowerCase(Locale.ROOT);
    ShardRoutingState[] replicaStates = new ShardRoutingState[randomIntBetween(0, 3)];
    Arrays.fill(replicaStates, ShardRoutingState.INITIALIZING);
    ClusterState stateWithIndex = ClusterStateCreationUtils.state(name, randomBoolean(), ShardRoutingState.INITIALIZING, replicaStates);
    // the initial state which is derived from the newly created cluster state but doesn't contain the index
    ClusterState initialState = ClusterState.builder(stateWithIndex).metadata(Metadata.builder(stateWithIndex.metadata()).remove(name)).routingTable(RoutingTable.builder().build()).build();
    // pick a data node to simulate the adding an index cluster state change event on, that has shards assigned to it
    DiscoveryNode node = stateWithIndex.nodes().get(randomFrom(stateWithIndex.routingTable().index(name).shardsWithState(INITIALIZING)).currentNodeId());
    // simulate the cluster state change on the node
    ClusterState localState = adaptClusterStateToLocalNode(stateWithIndex, node);
    ClusterState previousLocalState = adaptClusterStateToLocalNode(initialState, node);
    IndicesClusterStateService indicesCSSvc = createIndicesClusterStateService(node, RecordingIndicesService::new);
    indicesCSSvc.start();
    indicesCSSvc.applyClusterState(new ClusterChangedEvent("cluster state change that adds the index", localState, previousLocalState));
    // create a new empty cluster state with a brand new cluster UUID
    ClusterState newClusterState = ClusterState.builder(initialState).metadata(Metadata.builder(initialState.metadata()).clusterUUID(UUIDs.randomBase64UUID())).build();
    // simulate the cluster state change on the node
    localState = adaptClusterStateToLocalNode(newClusterState, node);
    previousLocalState = adaptClusterStateToLocalNode(stateWithIndex, node);
    indicesCSSvc.applyClusterState(new ClusterChangedEvent("cluster state change with a new cluster UUID (and doesn't contain the index)", localState, previousLocalState));
    // check that in memory data structures have been removed once the new cluster state is applied,
    // but the persistent data is still there
    RecordingIndicesService indicesService = (RecordingIndicesService) indicesCSSvc.indicesService;
    for (IndexMetadata indexMetadata : stateWithIndex.metadata()) {
        Index index = indexMetadata.getIndex();
        assertNull(indicesService.indexService(index));
        assertFalse(indicesService.isDeleted(index));
    }
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ClusterChangedEvent(org.opensearch.cluster.ClusterChangedEvent) Index(org.opensearch.index.Index) ShardRoutingState(org.opensearch.cluster.routing.ShardRoutingState) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata)

Example 2 with ClusterChangedEvent

use of org.opensearch.cluster.ClusterChangedEvent in project OpenSearch by opensearch-project.

the class IngestServiceTests method testUpdatePipelines.

public void testUpdatePipelines() {
    IngestService ingestService = createWithProcessors();
    ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).build();
    ClusterState previousClusterState = clusterState;
    ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState));
    assertThat(ingestService.pipelines().size(), is(0));
    PipelineConfiguration pipeline = new PipelineConfiguration("_id", new BytesArray("{\"processors\": [{\"set\" : {\"field\": \"_field\", \"value\": \"_value\"}}]}"), XContentType.JSON);
    IngestMetadata ingestMetadata = new IngestMetadata(Collections.singletonMap("_id", pipeline));
    clusterState = ClusterState.builder(clusterState).metadata(Metadata.builder().putCustom(IngestMetadata.TYPE, ingestMetadata)).build();
    ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState));
    assertThat(ingestService.pipelines().size(), is(1));
    assertThat(ingestService.pipelines().get("_id").pipeline.getId(), equalTo("_id"));
    assertThat(ingestService.pipelines().get("_id").pipeline.getDescription(), nullValue());
    assertThat(ingestService.pipelines().get("_id").pipeline.getProcessors().size(), equalTo(1));
    assertThat(ingestService.pipelines().get("_id").pipeline.getProcessors().get(0).getType(), equalTo("set"));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) BytesArray(org.opensearch.common.bytes.BytesArray) ClusterName(org.opensearch.cluster.ClusterName) ClusterChangedEvent(org.opensearch.cluster.ClusterChangedEvent)

Example 3 with ClusterChangedEvent

use of org.opensearch.cluster.ClusterChangedEvent in project OpenSearch by opensearch-project.

the class IngestServiceTests method testPut.

public void testPut() {
    IngestService ingestService = createWithProcessors();
    String id = "_id";
    Pipeline pipeline = ingestService.getPipeline(id);
    assertThat(pipeline, nullValue());
    ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).build();
    // add a new pipeline:
    PutPipelineRequest putRequest = new PutPipelineRequest(id, new BytesArray("{\"processors\": []}"), XContentType.JSON);
    ClusterState previousClusterState = clusterState;
    clusterState = IngestService.innerPut(putRequest, clusterState);
    ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState));
    pipeline = ingestService.getPipeline(id);
    assertThat(pipeline, notNullValue());
    assertThat(pipeline.getId(), equalTo(id));
    assertThat(pipeline.getDescription(), nullValue());
    assertThat(pipeline.getProcessors().size(), equalTo(0));
    // overwrite existing pipeline:
    putRequest = new PutPipelineRequest(id, new BytesArray("{\"processors\": [], \"description\": \"_description\"}"), XContentType.JSON);
    previousClusterState = clusterState;
    clusterState = IngestService.innerPut(putRequest, clusterState);
    ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState));
    pipeline = ingestService.getPipeline(id);
    assertThat(pipeline, notNullValue());
    assertThat(pipeline.getId(), equalTo(id));
    assertThat(pipeline.getDescription(), equalTo("_description"));
    assertThat(pipeline.getProcessors().size(), equalTo(0));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) BytesArray(org.opensearch.common.bytes.BytesArray) ClusterName(org.opensearch.cluster.ClusterName) PutPipelineRequest(org.opensearch.action.ingest.PutPipelineRequest) ClusterChangedEvent(org.opensearch.cluster.ClusterChangedEvent) Mockito.anyString(org.mockito.Mockito.anyString)

Example 4 with ClusterChangedEvent

use of org.opensearch.cluster.ClusterChangedEvent in project OpenSearch by opensearch-project.

the class IngestServiceTests method testCrud.

public void testCrud() throws Exception {
    IngestService ingestService = createWithProcessors();
    String id = "_id";
    Pipeline pipeline = ingestService.getPipeline(id);
    assertThat(pipeline, nullValue());
    // Start empty
    ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).build();
    PutPipelineRequest putRequest = new PutPipelineRequest(id, new BytesArray("{\"processors\": [{\"set\" : {\"field\": \"_field\", \"value\": \"_value\"}}]}"), XContentType.JSON);
    ClusterState previousClusterState = clusterState;
    clusterState = IngestService.innerPut(putRequest, clusterState);
    ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState));
    pipeline = ingestService.getPipeline(id);
    assertThat(pipeline, notNullValue());
    assertThat(pipeline.getId(), equalTo(id));
    assertThat(pipeline.getDescription(), nullValue());
    assertThat(pipeline.getProcessors().size(), equalTo(1));
    assertThat(pipeline.getProcessors().get(0).getType(), equalTo("set"));
    DeletePipelineRequest deleteRequest = new DeletePipelineRequest(id);
    previousClusterState = clusterState;
    clusterState = IngestService.innerDelete(deleteRequest, clusterState);
    ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState));
    pipeline = ingestService.getPipeline(id);
    assertThat(pipeline, nullValue());
}
Also used : DeletePipelineRequest(org.opensearch.action.ingest.DeletePipelineRequest) ClusterState(org.opensearch.cluster.ClusterState) BytesArray(org.opensearch.common.bytes.BytesArray) ClusterName(org.opensearch.cluster.ClusterName) PutPipelineRequest(org.opensearch.action.ingest.PutPipelineRequest) ClusterChangedEvent(org.opensearch.cluster.ClusterChangedEvent) Mockito.anyString(org.mockito.Mockito.anyString)

Example 5 with ClusterChangedEvent

use of org.opensearch.cluster.ClusterChangedEvent in project OpenSearch by opensearch-project.

the class IngestServiceTests method testExecuteIndexPipelineExistsButFailedParsing.

public void testExecuteIndexPipelineExistsButFailedParsing() {
    IngestService ingestService = createWithProcessors(Collections.singletonMap("mock", (factories, tag, description, config) -> new AbstractProcessor("mock", "description") {

        @Override
        public IngestDocument execute(IngestDocument ingestDocument) {
            throw new IllegalStateException("error");
        }

        @Override
        public String getType() {
            return null;
        }
    }));
    // Start empty
    ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).build();
    String id = "_id";
    PutPipelineRequest putRequest = new PutPipelineRequest(id, new BytesArray("{\"processors\": [{\"mock\" : {}}]}"), XContentType.JSON);
    ClusterState previousClusterState = clusterState;
    clusterState = IngestService.innerPut(putRequest, clusterState);
    ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState));
    final SetOnce<Boolean> failure = new SetOnce<>();
    BulkRequest bulkRequest = new BulkRequest();
    final IndexRequest indexRequest1 = new IndexRequest("_index").id("_id1").source(emptyMap()).setPipeline("_none").setFinalPipeline("_none");
    bulkRequest.add(indexRequest1);
    IndexRequest indexRequest2 = new IndexRequest("_index").id("_id2").source(emptyMap()).setPipeline(id).setFinalPipeline("_none");
    bulkRequest.add(indexRequest2);
    final BiConsumer<Integer, Exception> failureHandler = (slot, e) -> {
        assertThat(e.getCause(), instanceOf(IllegalStateException.class));
        assertThat(e.getCause().getMessage(), equalTo("error"));
        failure.set(true);
        assertThat(slot, equalTo(1));
    };
    @SuppressWarnings("unchecked") final BiConsumer<Thread, Exception> completionHandler = mock(BiConsumer.class);
    ingestService.executeBulkRequest(bulkRequest.numberOfActions(), bulkRequest.requests(), failureHandler, completionHandler, indexReq -> {
    }, Names.WRITE);
    assertTrue(failure.get());
    verify(completionHandler, times(1)).accept(Thread.currentThread(), null);
}
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) SetOnce(org.apache.lucene.util.SetOnce) 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) BulkRequest(org.opensearch.action.bulk.BulkRequest) ClusterName(org.opensearch.cluster.ClusterName) PutPipelineRequest(org.opensearch.action.ingest.PutPipelineRequest)

Aggregations

ClusterChangedEvent (org.opensearch.cluster.ClusterChangedEvent)56 ClusterState (org.opensearch.cluster.ClusterState)54 ClusterName (org.opensearch.cluster.ClusterName)32 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)27 Mockito.anyString (org.mockito.Mockito.anyString)25 BytesArray (org.opensearch.common.bytes.BytesArray)24 Metadata (org.opensearch.cluster.metadata.Metadata)22 HashMap (java.util.HashMap)21 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)21 PutPipelineRequest (org.opensearch.action.ingest.PutPipelineRequest)19 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)18 AtomicReference (java.util.concurrent.atomic.AtomicReference)18 ResourceNotFoundException (org.opensearch.ResourceNotFoundException)18 ClusterService (org.opensearch.cluster.service.ClusterService)18 ThreadPool (org.opensearch.threadpool.ThreadPool)18 Before (org.junit.Before)16 IndexRequest (org.opensearch.action.index.IndexRequest)16 Collections (java.util.Collections)15 Collections.emptyMap (java.util.Collections.emptyMap)15 List (java.util.List)15