Search in sources :

Example 1 with FakeThreadPoolMasterService

use of org.opensearch.cluster.service.FakeThreadPoolMasterService in project OpenSearch by opensearch-project.

the class NodeJoinTests method setupFakeMasterServiceAndCoordinator.

private void setupFakeMasterServiceAndCoordinator(long term, ClusterState initialState, NodeHealthService nodeHealthService) {
    deterministicTaskQueue = new DeterministicTaskQueue(Settings.builder().put(Node.NODE_NAME_SETTING.getKey(), "test").build(), random());
    final ThreadPool fakeThreadPool = deterministicTaskQueue.getThreadPool();
    FakeThreadPoolMasterService fakeMasterService = new FakeThreadPoolMasterService("test_node", "test", fakeThreadPool, deterministicTaskQueue::scheduleNow);
    setupMasterServiceAndCoordinator(term, initialState, fakeMasterService, fakeThreadPool, Randomness.get(), nodeHealthService);
    fakeMasterService.setClusterStatePublisher((event, publishListener, ackListener) -> {
        coordinator.handlePublishRequest(new PublishRequest(event.state()));
        publishListener.onResponse(null);
    });
    fakeMasterService.start();
}
Also used : TestThreadPool(org.opensearch.threadpool.TestThreadPool) ThreadPool(org.opensearch.threadpool.ThreadPool) FakeThreadPoolMasterService(org.opensearch.cluster.service.FakeThreadPoolMasterService)

Example 2 with FakeThreadPoolMasterService

use of org.opensearch.cluster.service.FakeThreadPoolMasterService in project OpenSearch by opensearch-project.

the class InternalClusterInfoServiceSchedulingTests method testScheduling.

public void testScheduling() {
    final DiscoveryNode discoveryNode = new DiscoveryNode("test", buildNewFakeTransportAddress(), Version.CURRENT);
    final DiscoveryNodes noMaster = DiscoveryNodes.builder().add(discoveryNode).localNodeId(discoveryNode.getId()).build();
    final DiscoveryNodes localMaster = DiscoveryNodes.builder(noMaster).masterNodeId(discoveryNode.getId()).build();
    final Settings.Builder settingsBuilder = Settings.builder().put(Node.NODE_NAME_SETTING.getKey(), discoveryNode.getName());
    if (randomBoolean()) {
        settingsBuilder.put(INTERNAL_CLUSTER_INFO_UPDATE_INTERVAL_SETTING.getKey(), randomIntBetween(10000, 60000) + "ms");
    }
    final Settings settings = settingsBuilder.build();
    final ClusterSettings clusterSettings = new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
    final DeterministicTaskQueue deterministicTaskQueue = new DeterministicTaskQueue(settings, random());
    final ThreadPool threadPool = deterministicTaskQueue.getThreadPool();
    final ClusterApplierService clusterApplierService = new ClusterApplierService("test", settings, clusterSettings, threadPool) {

        @Override
        protected PrioritizedOpenSearchThreadPoolExecutor createThreadPoolExecutor() {
            return new MockSinglePrioritizingExecutor("mock-executor", deterministicTaskQueue, threadPool);
        }
    };
    final MasterService masterService = new FakeThreadPoolMasterService("test", "masterService", threadPool, r -> {
        fail("master service should not run any tasks");
    });
    final ClusterService clusterService = new ClusterService(settings, clusterSettings, masterService, clusterApplierService);
    final FakeClusterInfoServiceClient client = new FakeClusterInfoServiceClient(threadPool);
    final InternalClusterInfoService clusterInfoService = new InternalClusterInfoService(settings, clusterService, threadPool, client);
    clusterService.addListener(clusterInfoService);
    clusterInfoService.addListener(ignored -> {
    });
    clusterService.setNodeConnectionsService(ClusterServiceUtils.createNoOpNodeConnectionsService());
    clusterApplierService.setInitialState(ClusterState.builder(new ClusterName("cluster")).nodes(noMaster).build());
    masterService.setClusterStatePublisher((clusterChangedEvent, publishListener, ackListener) -> fail("should not publish"));
    masterService.setClusterStateSupplier(clusterApplierService::state);
    clusterService.start();
    final AtomicBoolean becameMaster1 = new AtomicBoolean();
    clusterApplierService.onNewClusterState("become master 1", () -> ClusterState.builder(new ClusterName("cluster")).nodes(localMaster).build(), setFlagOnSuccess(becameMaster1));
    runUntilFlag(deterministicTaskQueue, becameMaster1);
    final AtomicBoolean failMaster1 = new AtomicBoolean();
    clusterApplierService.onNewClusterState("fail master 1", () -> ClusterState.builder(new ClusterName("cluster")).nodes(noMaster).build(), setFlagOnSuccess(failMaster1));
    runUntilFlag(deterministicTaskQueue, failMaster1);
    final AtomicBoolean becameMaster2 = new AtomicBoolean();
    clusterApplierService.onNewClusterState("become master 2", () -> ClusterState.builder(new ClusterName("cluster")).nodes(localMaster).build(), setFlagOnSuccess(becameMaster2));
    runUntilFlag(deterministicTaskQueue, becameMaster2);
    for (int i = 0; i < 3; i++) {
        final int initialRequestCount = client.requestCount;
        final long duration = INTERNAL_CLUSTER_INFO_UPDATE_INTERVAL_SETTING.get(settings).millis();
        runFor(deterministicTaskQueue, duration);
        deterministicTaskQueue.runAllRunnableTasks();
        // should have run two client requests per interval
        assertThat(client.requestCount, equalTo(initialRequestCount + 2));
    }
    final AtomicBoolean failMaster2 = new AtomicBoolean();
    clusterApplierService.onNewClusterState("fail master 2", () -> ClusterState.builder(new ClusterName("cluster")).nodes(noMaster).build(), setFlagOnSuccess(failMaster2));
    runUntilFlag(deterministicTaskQueue, failMaster2);
    runFor(deterministicTaskQueue, INTERNAL_CLUSTER_INFO_UPDATE_INTERVAL_SETTING.get(settings).millis());
    deterministicTaskQueue.runAllRunnableTasks();
    assertFalse(deterministicTaskQueue.hasRunnableTasks());
    assertFalse(deterministicTaskQueue.hasDeferredTasks());
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ClusterSettings(org.opensearch.common.settings.ClusterSettings) MockSinglePrioritizingExecutor(org.opensearch.cluster.coordination.MockSinglePrioritizingExecutor) ThreadPool(org.opensearch.threadpool.ThreadPool) ClusterApplierService(org.opensearch.cluster.service.ClusterApplierService) FakeThreadPoolMasterService(org.opensearch.cluster.service.FakeThreadPoolMasterService) MasterService(org.opensearch.cluster.service.MasterService) FakeThreadPoolMasterService(org.opensearch.cluster.service.FakeThreadPoolMasterService) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClusterService(org.opensearch.cluster.service.ClusterService) DeterministicTaskQueue(org.opensearch.cluster.coordination.DeterministicTaskQueue) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Settings(org.opensearch.common.settings.Settings)

Aggregations

FakeThreadPoolMasterService (org.opensearch.cluster.service.FakeThreadPoolMasterService)2 ThreadPool (org.opensearch.threadpool.ThreadPool)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 DeterministicTaskQueue (org.opensearch.cluster.coordination.DeterministicTaskQueue)1 MockSinglePrioritizingExecutor (org.opensearch.cluster.coordination.MockSinglePrioritizingExecutor)1 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)1 DiscoveryNodes (org.opensearch.cluster.node.DiscoveryNodes)1 ClusterApplierService (org.opensearch.cluster.service.ClusterApplierService)1 ClusterService (org.opensearch.cluster.service.ClusterService)1 MasterService (org.opensearch.cluster.service.MasterService)1 ClusterSettings (org.opensearch.common.settings.ClusterSettings)1 Settings (org.opensearch.common.settings.Settings)1 TestThreadPool (org.opensearch.threadpool.TestThreadPool)1