Search in sources :

Example 1 with ClusterApplierService

use of org.elasticsearch.cluster.service.ClusterApplierService in project crate by crate.

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());
    final ClusterService clusterService = mock(ClusterService.class);
    final ClusterApplierService clusterApplierService = mock(ClusterApplierService.class);
    when(clusterService.getClusterApplierService()).thenReturn(clusterApplierService);
    final AtomicReference<ClusterState> currentState = new AtomicReference<>(initialState);
    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.elasticsearch.cluster.ClusterState) SameThreadExecutorService(org.apache.lucene.util.SameThreadExecutorService) ClusterService(org.elasticsearch.cluster.service.ClusterService) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask) AtomicReference(java.util.concurrent.atomic.AtomicReference) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) ClusterApplierService(org.elasticsearch.cluster.service.ClusterApplierService) ClusterStateApplier(org.elasticsearch.cluster.ClusterStateApplier) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 2 with ClusterApplierService

use of org.elasticsearch.cluster.service.ClusterApplierService in project crate by crate.

the class CrateDummyClusterServiceUnitTest method createClusterService.

protected ClusterService createClusterService(Collection<Setting<?>> additionalClusterSettings, Version version) {
    Set<Setting<?>> clusterSettingsSet = new HashSet<>(ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
    clusterSettingsSet.addAll(additionalClusterSettings);
    ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, clusterSettingsSet);
    ClusterService clusterService = new ClusterService(Settings.builder().put("cluster.name", "ClusterServiceTests").put(Node.NODE_NAME_SETTING.getKey(), NODE_NAME).build(), clusterSettings, THREAD_POOL);
    clusterService.setNodeConnectionsService(createNoOpNodeConnectionsService());
    DiscoveryNode discoveryNode = new DiscoveryNode(NODE_NAME, NODE_ID, buildNewFakeTransportAddress(), Collections.emptyMap(), DiscoveryNodeRole.BUILT_IN_ROLES, version);
    DiscoveryNodes nodes = DiscoveryNodes.builder().add(discoveryNode).localNodeId(NODE_ID).masterNodeId(NODE_ID).build();
    ClusterState clusterState = ClusterState.builder(new ClusterName(this.getClass().getSimpleName())).nodes(nodes).blocks(ClusterBlocks.EMPTY_CLUSTER_BLOCK).build();
    ClusterApplierService clusterApplierService = clusterService.getClusterApplierService();
    clusterApplierService.setInitialState(clusterState);
    MasterService masterService = clusterService.getMasterService();
    masterService.setClusterStatePublisher(createClusterStatePublisher(clusterApplierService));
    masterService.setClusterStateSupplier(clusterApplierService::state);
    clusterService.start();
    return clusterService;
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) ClusterService(org.elasticsearch.cluster.service.ClusterService) Setting(org.elasticsearch.common.settings.Setting) ClusterName(org.elasticsearch.cluster.ClusterName) ClusterApplierService(org.elasticsearch.cluster.service.ClusterApplierService) MasterService(org.elasticsearch.cluster.service.MasterService) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) HashSet(java.util.HashSet)

Aggregations

ClusterState (org.elasticsearch.cluster.ClusterState)2 ClusterApplierService (org.elasticsearch.cluster.service.ClusterApplierService)2 ClusterService (org.elasticsearch.cluster.service.ClusterService)2 HashSet (java.util.HashSet)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 SameThreadExecutorService (org.apache.lucene.util.SameThreadExecutorService)1 ClusterChangedEvent (org.elasticsearch.cluster.ClusterChangedEvent)1 ClusterName (org.elasticsearch.cluster.ClusterName)1 ClusterStateApplier (org.elasticsearch.cluster.ClusterStateApplier)1 ClusterStateUpdateTask (org.elasticsearch.cluster.ClusterStateUpdateTask)1 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)1 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)1 MasterService (org.elasticsearch.cluster.service.MasterService)1 ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)1 Setting (org.elasticsearch.common.settings.Setting)1 ThreadPool (org.elasticsearch.threadpool.ThreadPool)1