Search in sources :

Example 1 with ClusterStateApplier

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

the class TransportBulkActionIngestTests method setupAction.

@Before
public void setupAction() {
    // initialize captors, which must be members to use @Capture because of generics
    threadPool = mock(ThreadPool.class);
    final ExecutorService direct = OpenSearchExecutors.newDirectExecutorService();
    when(threadPool.executor(anyString())).thenReturn(direct);
    bulkDocsItr = ArgumentCaptor.forClass(Iterable.class);
    failureHandler = ArgumentCaptor.forClass(BiConsumer.class);
    completionHandler = ArgumentCaptor.forClass(BiConsumer.class);
    remoteResponseHandler = ArgumentCaptor.forClass(TransportResponseHandler.class);
    // setup services that will be called by action
    transportService = mock(TransportService.class);
    clusterService = mock(ClusterService.class);
    localIngest = true;
    // setup nodes for local and remote
    DiscoveryNode localNode = mock(DiscoveryNode.class);
    when(localNode.isIngestNode()).thenAnswer(stub -> localIngest);
    when(clusterService.localNode()).thenReturn(localNode);
    remoteNode1 = mock(DiscoveryNode.class);
    remoteNode2 = mock(DiscoveryNode.class);
    nodes = mock(DiscoveryNodes.class);
    ImmutableOpenMap<String, DiscoveryNode> ingestNodes = ImmutableOpenMap.<String, DiscoveryNode>builder(2).fPut("node1", remoteNode1).fPut("node2", remoteNode2).build();
    when(nodes.getIngestNodes()).thenReturn(ingestNodes);
    when(nodes.getMinNodeVersion()).thenReturn(VersionUtils.randomCompatibleVersion(random(), Version.CURRENT));
    ClusterState state = mock(ClusterState.class);
    when(state.getNodes()).thenReturn(nodes);
    Metadata metadata = Metadata.builder().indices(ImmutableOpenMap.<String, IndexMetadata>builder().putAll(MapBuilder.<String, IndexMetadata>newMapBuilder().put(WITH_DEFAULT_PIPELINE, IndexMetadata.builder(WITH_DEFAULT_PIPELINE).settings(settings(Version.CURRENT).put(IndexSettings.DEFAULT_PIPELINE.getKey(), "default_pipeline").build()).putAlias(AliasMetadata.builder(WITH_DEFAULT_PIPELINE_ALIAS).build()).numberOfShards(1).numberOfReplicas(1).build()).put(".system", IndexMetadata.builder(".system").settings(settings(Version.CURRENT)).system(true).numberOfShards(1).numberOfReplicas(0).build()).map()).build()).build();
    when(state.getMetadata()).thenReturn(metadata);
    when(state.metadata()).thenReturn(metadata);
    when(clusterService.state()).thenReturn(state);
    doAnswer(invocation -> {
        ClusterChangedEvent event = mock(ClusterChangedEvent.class);
        when(event.state()).thenReturn(state);
        ((ClusterStateApplier) invocation.getArguments()[0]).applyClusterState(event);
        return null;
    }).when(clusterService).addStateApplier(any(ClusterStateApplier.class));
    // setup the mocked ingest service for capturing calls
    ingestService = mock(IngestService.class);
    action = new TestTransportBulkAction();
    singleItemBulkWriteAction = new TestSingleItemBulkWriteAction(action);
    // call on construction of action
    reset(transportService);
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) IngestService(org.opensearch.ingest.IngestService) ThreadPool(org.opensearch.threadpool.ThreadPool) TransportResponseHandler(org.opensearch.transport.TransportResponseHandler) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) ClusterChangedEvent(org.opensearch.cluster.ClusterChangedEvent) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.anyString(org.mockito.Mockito.anyString) ClusterService(org.opensearch.cluster.service.ClusterService) TransportService(org.opensearch.transport.TransportService) ExecutorService(java.util.concurrent.ExecutorService) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ClusterStateApplier(org.opensearch.cluster.ClusterStateApplier) BiConsumer(java.util.function.BiConsumer) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) Before(org.junit.Before)

Example 2 with ClusterStateApplier

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

the class BlobStoreTestUtil method mockClusterService.

private static ClusterService mockClusterService(ClusterState initialState) {
    final ThreadPool threadPool = mock(ThreadPool.class);
    when(threadPool.executor(ThreadPool.Names.SNAPSHOT)).thenReturn(new SameThreadExecutorService());
    when(threadPool.generic()).thenReturn(new SameThreadExecutorService());
    when(threadPool.info(ThreadPool.Names.SNAPSHOT)).thenReturn(new ThreadPool.Info(ThreadPool.Names.SNAPSHOT, ThreadPool.ThreadPoolType.FIXED, randomIntBetween(1, 10)));
    final ClusterService clusterService = mock(ClusterService.class);
    final ClusterApplierService clusterApplierService = mock(ClusterApplierService.class);
    when(clusterService.getClusterApplierService()).thenReturn(clusterApplierService);
    // Setting local node as master so it may update the repository metadata in the cluster state
    final DiscoveryNode localNode = new DiscoveryNode("", buildNewFakeTransportAddress(), Version.CURRENT);
    final AtomicReference<ClusterState> currentState = new AtomicReference<>(ClusterState.builder(initialState).nodes(DiscoveryNodes.builder().add(localNode).masterNodeId(localNode.getId()).localNodeId(localNode.getId()).build()).build());
    when(clusterService.state()).then(invocationOnMock -> currentState.get());
    final List<ClusterStateApplier> appliers = new CopyOnWriteArrayList<>();
    doAnswer(invocation -> {
        final ClusterStateUpdateTask task = ((ClusterStateUpdateTask) invocation.getArguments()[1]);
        final ClusterState current = currentState.get();
        final ClusterState next = task.execute(current);
        currentState.set(next);
        appliers.forEach(applier -> applier.applyClusterState(new ClusterChangedEvent((String) invocation.getArguments()[0], next, current)));
        task.clusterStateProcessed((String) invocation.getArguments()[0], current, next);
        return null;
    }).when(clusterService).submitStateUpdateTask(anyString(), any(ClusterStateUpdateTask.class));
    doAnswer(invocation -> {
        appliers.add((ClusterStateApplier) invocation.getArguments()[0]);
        return null;
    }).when(clusterService).addStateApplier(any(ClusterStateApplier.class));
    when(clusterApplierService.threadPool()).thenReturn(threadPool);
    return clusterService;
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) SameThreadExecutorService(org.apache.lucene.util.SameThreadExecutorService) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ThreadPool(org.opensearch.threadpool.ThreadPool) ClusterStateUpdateTask(org.opensearch.cluster.ClusterStateUpdateTask) AtomicReference(java.util.concurrent.atomic.AtomicReference) ClusterChangedEvent(org.opensearch.cluster.ClusterChangedEvent) ClusterApplierService(org.opensearch.cluster.service.ClusterApplierService) ClusterService(org.opensearch.cluster.service.ClusterService) ClusterStateApplier(org.opensearch.cluster.ClusterStateApplier) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

ClusterChangedEvent (org.opensearch.cluster.ClusterChangedEvent)2 ClusterState (org.opensearch.cluster.ClusterState)2 ClusterStateApplier (org.opensearch.cluster.ClusterStateApplier)2 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)2 ClusterService (org.opensearch.cluster.service.ClusterService)2 ThreadPool (org.opensearch.threadpool.ThreadPool)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 BiConsumer (java.util.function.BiConsumer)1 SameThreadExecutorService (org.apache.lucene.util.SameThreadExecutorService)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Before (org.junit.Before)1 Mockito.anyString (org.mockito.Mockito.anyString)1 ClusterStateUpdateTask (org.opensearch.cluster.ClusterStateUpdateTask)1 AliasMetadata (org.opensearch.cluster.metadata.AliasMetadata)1 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)1 IndexTemplateMetadata (org.opensearch.cluster.metadata.IndexTemplateMetadata)1 Metadata (org.opensearch.cluster.metadata.Metadata)1 DiscoveryNodes (org.opensearch.cluster.node.DiscoveryNodes)1