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);
}
}
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());
}
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());
}
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;
}
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();
}
Aggregations