Search in sources :

Example 1 with ClusterApplierService

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

the class IndicesStoreIntegrationIT method testShardActiveElseWhere.

public void testShardActiveElseWhere() throws Exception {
    List<String> nodes = internalCluster().startNodes(2);
    final String masterNode = internalCluster().getMasterName();
    final String nonMasterNode = nodes.get(0).equals(masterNode) ? nodes.get(1) : nodes.get(0);
    final String masterId = internalCluster().clusterService(masterNode).localNode().getId();
    final String nonMasterId = internalCluster().clusterService(nonMasterNode).localNode().getId();
    final int numShards = scaledRandomIntBetween(2, 10);
    assertAcked(prepareCreate("test").setSettings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, numShards)));
    ensureGreen("test");
    waitNoPendingTasksOnAll();
    ClusterStateResponse stateResponse = client().admin().cluster().prepareState().get();
    final Index index = stateResponse.getState().metadata().index("test").getIndex();
    RoutingNode routingNode = stateResponse.getState().getRoutingNodes().node(nonMasterId);
    final int[] node2Shards = new int[routingNode.numberOfOwningShards()];
    int i = 0;
    for (ShardRouting shardRouting : routingNode) {
        node2Shards[i] = shardRouting.shardId().id();
        i++;
    }
    logger.info("Node [{}] has shards: {}", nonMasterNode, Arrays.toString(node2Shards));
    // disable relocations when we do this, to make sure the shards are not relocated from node2
    // due to rebalancing, and delete its content
    client().admin().cluster().prepareUpdateSettings().setTransientSettings(Settings.builder().put(EnableAllocationDecider.CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), EnableAllocationDecider.Rebalance.NONE)).get();
    ClusterApplierService clusterApplierService = internalCluster().getInstance(ClusterService.class, nonMasterNode).getClusterApplierService();
    ClusterState currentState = clusterApplierService.state();
    IndexRoutingTable.Builder indexRoutingTableBuilder = IndexRoutingTable.builder(index);
    for (int j = 0; j < numShards; j++) {
        indexRoutingTableBuilder.addIndexShard(new IndexShardRoutingTable.Builder(new ShardId(index, j)).addShard(TestShardRouting.newShardRouting("test", j, masterId, true, ShardRoutingState.STARTED)).build());
    }
    ClusterState newState = ClusterState.builder(currentState).incrementVersion().routingTable(RoutingTable.builder().add(indexRoutingTableBuilder).build()).build();
    CountDownLatch latch = new CountDownLatch(1);
    clusterApplierService.onNewClusterState("test", () -> newState, new ClusterApplyListener() {

        @Override
        public void onSuccess(String source) {
            latch.countDown();
        }

        @Override
        public void onFailure(String source, Exception e) {
            latch.countDown();
            throw new AssertionError("Expected a proper response", e);
        }
    });
    latch.await();
    waitNoPendingTasksOnAll();
    logger.info("Checking if shards aren't removed");
    for (int shard : node2Shards) {
        assertShardExists(nonMasterNode, index, shard);
    }
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) Index(org.opensearch.index.Index) ClusterApplierService(org.opensearch.cluster.service.ClusterApplierService) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectTransportException(org.opensearch.transport.ConnectTransportException) IOException(java.io.IOException) ShardId(org.opensearch.index.shard.ShardId) ClusterService(org.opensearch.cluster.service.ClusterService) ClusterApplyListener(org.opensearch.cluster.service.ClusterApplier.ClusterApplyListener) RoutingNode(org.opensearch.cluster.routing.RoutingNode) ShardRouting(org.opensearch.cluster.routing.ShardRouting) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting)

Example 2 with ClusterApplierService

use of org.opensearch.cluster.service.ClusterApplierService 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)

Example 3 with ClusterApplierService

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

the class ClusterStateObserverTests method testClusterStateListenerToStringIncludesListenerToString.

public void testClusterStateListenerToStringIncludesListenerToString() {
    final ClusterApplierService clusterApplierService = mock(ClusterApplierService.class);
    final AtomicBoolean listenerAdded = new AtomicBoolean();
    doAnswer(invocation -> {
        assertThat(Arrays.toString(invocation.getArguments()), containsString("test-listener"));
        listenerAdded.set(true);
        return null;
    }).when(clusterApplierService).addTimeoutListener(any(), any());
    final ClusterState clusterState = ClusterState.builder(new ClusterName("test")).nodes(DiscoveryNodes.builder()).build();
    when(clusterApplierService.state()).thenReturn(clusterState);
    final ClusterStateObserver clusterStateObserver = new ClusterStateObserver(clusterState, clusterApplierService, null, logger, new ThreadContext(Settings.EMPTY));
    clusterStateObserver.waitForNextChange(new ClusterStateObserver.Listener() {

        @Override
        public void onNewClusterState(ClusterState state) {
        }

        @Override
        public void onClusterServiceClose() {
        }

        @Override
        public void onTimeout(TimeValue timeout) {
        }

        @Override
        public String toString() {
            return "test-listener";
        }
    });
    assertTrue(listenerAdded.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) Matchers.containsString(org.hamcrest.Matchers.containsString) ClusterApplierService(org.opensearch.cluster.service.ClusterApplierService) TimeValue(org.opensearch.common.unit.TimeValue)

Example 4 with ClusterApplierService

use of org.opensearch.cluster.service.ClusterApplierService 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)

Example 5 with ClusterApplierService

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

the class RepositoriesServiceTests method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    ThreadPool threadPool = mock(ThreadPool.class);
    final TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, boundAddress -> DiscoveryNode.createLocal(Settings.EMPTY, boundAddress.publishAddress(), UUIDs.randomBase64UUID()), null, Collections.emptySet());
    final ClusterApplierService clusterApplierService = mock(ClusterApplierService.class);
    when(clusterApplierService.threadPool()).thenReturn(threadPool);
    final ClusterService clusterService = mock(ClusterService.class);
    when(clusterService.getClusterApplierService()).thenReturn(clusterApplierService);
    Map<String, Repository.Factory> typesRegistry = org.opensearch.common.collect.Map.of(TestRepository.TYPE, TestRepository::new, MeteredRepositoryTypeA.TYPE, metadata -> new MeteredRepositoryTypeA(metadata, clusterService), MeteredRepositoryTypeB.TYPE, metadata -> new MeteredRepositoryTypeB(metadata, clusterService));
    repositoriesService = new RepositoriesService(Settings.EMPTY, mock(ClusterService.class), transportService, typesRegistry, typesRegistry, threadPool);
    repositoriesService.start();
}
Also used : ClusterService(org.opensearch.cluster.service.ClusterService) TransportService(org.opensearch.transport.TransportService) ThreadPool(org.opensearch.threadpool.ThreadPool) Transport(org.opensearch.transport.Transport) ClusterApplierService(org.opensearch.cluster.service.ClusterApplierService)

Aggregations

ClusterApplierService (org.opensearch.cluster.service.ClusterApplierService)5 ClusterService (org.opensearch.cluster.service.ClusterService)4 ThreadPool (org.opensearch.threadpool.ThreadPool)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 ClusterState (org.opensearch.cluster.ClusterState)2 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)2 IOException (java.io.IOException)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 SameThreadExecutorService (org.apache.lucene.util.SameThreadExecutorService)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 ClusterStateResponse (org.opensearch.action.admin.cluster.state.ClusterStateResponse)1 ClusterChangedEvent (org.opensearch.cluster.ClusterChangedEvent)1 ClusterStateApplier (org.opensearch.cluster.ClusterStateApplier)1 ClusterStateUpdateTask (org.opensearch.cluster.ClusterStateUpdateTask)1 DeterministicTaskQueue (org.opensearch.cluster.coordination.DeterministicTaskQueue)1 MockSinglePrioritizingExecutor (org.opensearch.cluster.coordination.MockSinglePrioritizingExecutor)1 DiscoveryNodes (org.opensearch.cluster.node.DiscoveryNodes)1 IndexRoutingTable (org.opensearch.cluster.routing.IndexRoutingTable)1