4646public class InternalClusterInfoServiceRefreshTests extends ESTestCase {
4747
4848 public void testEstimatedHeapUsageCollectorSuccessAndFailure () {
49- final Settings settings = Settings .builder ()
50- .put (DiskThresholdSettings .CLUSTER_ROUTING_ALLOCATION_DISK_THRESHOLD_ENABLED_SETTING .getKey (), false )
51- .put (
52- WriteLoadConstraintSettings .WRITE_LOAD_DECIDER_ENABLED_SETTING .getKey (),
53- WriteLoadConstraintSettings .WriteLoadDeciderStatus .DISABLED
54- )
55- .put (InternalClusterInfoService .CLUSTER_ROUTING_ALLOCATION_ESTIMATED_HEAP_THRESHOLD_DECIDER_ENABLED .getKey (), true )
56- .build ();
57- final ClusterSettings clusterSettings = new ClusterSettings (settings , ClusterSettings .BUILT_IN_CLUSTER_SETTINGS );
58- final DeterministicTaskQueue deterministicTaskQueue = new DeterministicTaskQueue ();
59- final ThreadPool threadPool = deterministicTaskQueue .getThreadPool ();
49+ final Settings settings = baseSettingsBuilder ().put (
50+ InternalClusterInfoService .CLUSTER_ROUTING_ALLOCATION_ESTIMATED_HEAP_THRESHOLD_DECIDER_ENABLED .getKey (),
51+ true
52+ ).build ();
6053
61- try (ClusterService clusterService = ClusterServiceUtils . createClusterService ( threadPool , clusterSettings )) {
54+ try (RefreshTestContext context = RefreshTestContext . create ( settings )) {
6255 final Map <String , NodeHeapEstimates > nodeHeapEstimates = Map .of ("node-id" , new NodeHeapEstimates (100L , 20L ));
6356 final ShardId shardId = new ShardId ("index" , "uuid" , 0 );
6457 final ShardHeapUsageEstimates shardHeapUsageEstimates = new ShardHeapUsageEstimates (
@@ -78,43 +71,13 @@ public void testEstimatedHeapUsageCollectorSuccessAndFailure() {
7871 return null ;
7972 }).when (estimatedHeapUsageCollector ).collectEstimatedHeapUsage (any ());
8073
81- final InternalClusterInfoService clusterInfoService = new InternalClusterInfoService (
82- settings ,
83- new WriteLoadConstraintSettings (clusterService .getClusterSettings ()),
84- clusterService ,
85- threadPool ,
86- new NoOpClient (threadPool ) {
87- @ Override
88- @ SuppressWarnings ("unchecked" )
89- protected <Request extends ActionRequest , Response extends ActionResponse > void doExecute (
90- ActionType <Response > action ,
91- Request request ,
92- ActionListener <Response > listener
93- ) {
94- if (request instanceof NodesStatsRequest ) {
95- NodeStats nodeStats = mock (NodeStats .class );
96- JvmStats jvmStats = mock (JvmStats .class );
97- JvmStats .Mem mem = mock (JvmStats .Mem .class );
98- Mockito .when (nodeStats .getNode ()).thenReturn (DiscoveryNodeUtils .create ("node-id" ));
99- Mockito .when (nodeStats .getJvm ()).thenReturn (jvmStats );
100- Mockito .when (jvmStats .getMem ()).thenReturn (mem );
101- Mockito .when (mem .getHeapMax ()).thenReturn (ByteSizeValue .ofBytes (1_000L ));
102- listener .onResponse (
103- (Response ) new NodesStatsResponse (new ClusterName ("cluster" ), List .of (nodeStats ), List .of ())
104- );
105- } else {
106- fail ("unexpected action: " + action .name ());
107- }
108- }
109- },
74+ final InternalClusterInfoService clusterInfoService = context .createClusterInfoService (
75+ nodeStatsClient (context .threadPool ()),
11076 estimatedHeapUsageCollector ,
11177 CacheSizesAndCommitmentCollector .EMPTY ,
11278 PartitionSizeCollector .EMPTY ,
113- NodeUsageStatsForThreadPoolsCollector .EMPTY ,
11479 SearchLaneRequirementsCollector .EMPTY
11580 );
116- // AsyncRefresh asserts that each refresh notifies at least one registered cluster info listener.
117- clusterInfoService .addListener (ignored -> {});
11881
11982 ClusterInfo clusterInfo = refresh (clusterInfoService );
12083 verify (estimatedHeapUsageCollector ).collectEstimatedHeapUsage (any ());
@@ -136,18 +99,7 @@ protected <Request extends ActionRequest, Response extends ActionResponse> void
13699 }
137100
138101 public void testPartitionSizeCollectorSuccessAndFailure () {
139- final Settings settings = Settings .builder ()
140- .put (DiskThresholdSettings .CLUSTER_ROUTING_ALLOCATION_DISK_THRESHOLD_ENABLED_SETTING .getKey (), false )
141- .put (
142- WriteLoadConstraintSettings .WRITE_LOAD_DECIDER_ENABLED_SETTING .getKey (),
143- WriteLoadConstraintSettings .WriteLoadDeciderStatus .DISABLED
144- )
145- .build ();
146- final ClusterSettings clusterSettings = new ClusterSettings (settings , ClusterSettings .BUILT_IN_CLUSTER_SETTINGS );
147- final DeterministicTaskQueue deterministicTaskQueue = new DeterministicTaskQueue ();
148- final ThreadPool threadPool = deterministicTaskQueue .getThreadPool ();
149-
150- try (ClusterService clusterService = ClusterServiceUtils .createClusterService (threadPool , clusterSettings )) {
102+ try (RefreshTestContext context = RefreshTestContext .create (baseSettings ())) {
151103 final Map <String , Long > partitionSizes = Map .of ("node-id" , 1234L );
152104 final AtomicBoolean failPartitionSizes = new AtomicBoolean ();
153105 final PartitionSizeCollector partitionSizeCollector = mock (PartitionSizeCollector .class );
@@ -161,20 +113,12 @@ public void testPartitionSizeCollectorSuccessAndFailure() {
161113 return null ;
162114 }).when (partitionSizeCollector ).collectHostedShardsPartitionSizes (any (), any ());
163115
164- final InternalClusterInfoService clusterInfoService = new InternalClusterInfoService (
165- settings ,
166- new WriteLoadConstraintSettings (clusterService .getClusterSettings ()),
167- clusterService ,
168- threadPool ,
169- new NoOpClient (threadPool ),
116+ final InternalClusterInfoService clusterInfoService = context .createClusterInfoService (
170117 EstimatedHeapUsageCollector .EMPTY ,
171118 CacheSizesAndCommitmentCollector .EMPTY ,
172119 partitionSizeCollector ,
173- NodeUsageStatsForThreadPoolsCollector .EMPTY ,
174120 SearchLaneRequirementsCollector .EMPTY
175121 );
176- // Refresh is a no-op if there are no listeners
177- clusterInfoService .addListener (ignored -> {});
178122
179123 // Success populates the ClusterInfo
180124 ClusterInfo clusterInfo = refresh (clusterInfoService );
@@ -191,18 +135,7 @@ public void testPartitionSizeCollectorSuccessAndFailure() {
191135 }
192136
193137 public void testCacheSizesAndCommitmentCollectorSuccessAndFailure () {
194- final Settings settings = Settings .builder ()
195- .put (DiskThresholdSettings .CLUSTER_ROUTING_ALLOCATION_DISK_THRESHOLD_ENABLED_SETTING .getKey (), false )
196- .put (
197- WriteLoadConstraintSettings .WRITE_LOAD_DECIDER_ENABLED_SETTING .getKey (),
198- WriteLoadConstraintSettings .WriteLoadDeciderStatus .DISABLED
199- )
200- .build ();
201- final ClusterSettings clusterSettings = new ClusterSettings (settings , ClusterSettings .BUILT_IN_CLUSTER_SETTINGS );
202- final DeterministicTaskQueue deterministicTaskQueue = new DeterministicTaskQueue ();
203- final ThreadPool threadPool = deterministicTaskQueue .getThreadPool ();
204-
205- try (ClusterService clusterService = ClusterServiceUtils .createClusterService (threadPool , clusterSettings )) {
138+ try (RefreshTestContext context = RefreshTestContext .create (baseSettings ())) {
206139 final Map <ShardId , BoostedAndUnboostedCacheRequirements > shardCacheRequirements = Map .of (
207140 new ShardId ("index" , "uuid" , 0 ),
208141 new BoostedAndUnboostedCacheRequirements (10L , 20L )
@@ -227,20 +160,12 @@ public void testCacheSizesAndCommitmentCollectorSuccessAndFailure() {
227160 return null ;
228161 }).when (cacheSizesAndCommitmentCollector ).collectCacheSizesAndCommitmentStats (any (), any ());
229162
230- final InternalClusterInfoService clusterInfoService = new InternalClusterInfoService (
231- settings ,
232- new WriteLoadConstraintSettings (clusterService .getClusterSettings ()),
233- clusterService ,
234- threadPool ,
235- new NoOpClient (threadPool ),
163+ final InternalClusterInfoService clusterInfoService = context .createClusterInfoService (
236164 EstimatedHeapUsageCollector .EMPTY ,
237165 cacheSizesAndCommitmentCollector ,
238166 PartitionSizeCollector .EMPTY ,
239- NodeUsageStatsForThreadPoolsCollector .EMPTY ,
240167 SearchLaneRequirementsCollector .EMPTY
241168 );
242- // AsyncRefresh asserts that each refresh notifies at least one registered cluster info listener.
243- clusterInfoService .addListener (ignored -> {});
244169
245170 ClusterInfo clusterInfo = refresh (clusterInfoService );
246171 verify (cacheSizesAndCommitmentCollector ).collectCacheSizesAndCommitmentStats (any (), any ());
@@ -257,18 +182,7 @@ public void testCacheSizesAndCommitmentCollectorSuccessAndFailure() {
257182 }
258183
259184 public void testSearchLaneRequirementsCollectorSuccessAndFailure () {
260- final Settings settings = Settings .builder ()
261- .put (DiskThresholdSettings .CLUSTER_ROUTING_ALLOCATION_DISK_THRESHOLD_ENABLED_SETTING .getKey (), false )
262- .put (
263- WriteLoadConstraintSettings .WRITE_LOAD_DECIDER_ENABLED_SETTING .getKey (),
264- WriteLoadConstraintSettings .WriteLoadDeciderStatus .DISABLED
265- )
266- .build ();
267- final ClusterSettings clusterSettings = new ClusterSettings (settings , ClusterSettings .BUILT_IN_CLUSTER_SETTINGS );
268- final DeterministicTaskQueue deterministicTaskQueue = new DeterministicTaskQueue ();
269- final ThreadPool threadPool = deterministicTaskQueue .getThreadPool ();
270-
271- try (ClusterService clusterService = ClusterServiceUtils .createClusterService (threadPool , clusterSettings )) {
185+ try (RefreshTestContext context = RefreshTestContext .create (baseSettings ())) {
272186 final Map <ShardId , Double > laneRequirements = Map .of (new ShardId ("index" , "uuid" , 0 ), 3.2 );
273187 final AtomicBoolean failLaneRequirements = new AtomicBoolean ();
274188 final SearchLaneRequirementsCollector searchLaneRequirementsCollector = mock (SearchLaneRequirementsCollector .class );
@@ -282,20 +196,12 @@ public void testSearchLaneRequirementsCollectorSuccessAndFailure() {
282196 return null ;
283197 }).when (searchLaneRequirementsCollector ).collectSearchLaneRequirements (any (), any ());
284198
285- final InternalClusterInfoService clusterInfoService = new InternalClusterInfoService (
286- settings ,
287- new WriteLoadConstraintSettings (clusterService .getClusterSettings ()),
288- clusterService ,
289- threadPool ,
290- new NoOpClient (threadPool ),
199+ final InternalClusterInfoService clusterInfoService = context .createClusterInfoService (
291200 EstimatedHeapUsageCollector .EMPTY ,
292201 CacheSizesAndCommitmentCollector .EMPTY ,
293202 PartitionSizeCollector .EMPTY ,
294- NodeUsageStatsForThreadPoolsCollector .EMPTY ,
295203 searchLaneRequirementsCollector
296204 );
297- // Refresh is a no-op if there are no listeners
298- clusterInfoService .addListener (ignored -> {});
299205
300206 // Success populates the ClusterInfo
301207 ClusterInfo clusterInfo = refresh (clusterInfoService );
@@ -310,4 +216,96 @@ public void testSearchLaneRequirementsCollectorSuccessAndFailure() {
310216 assertThat (clusterInfo .getShardSearchLaneRequirements (), equalTo (Map .of ()));
311217 }
312218 }
219+
220+ private static Settings baseSettings () {
221+ return baseSettingsBuilder ().build ();
222+ }
223+
224+ private static Settings .Builder baseSettingsBuilder () {
225+ return Settings .builder ()
226+ .put (DiskThresholdSettings .CLUSTER_ROUTING_ALLOCATION_DISK_THRESHOLD_ENABLED_SETTING .getKey (), false )
227+ .put (
228+ WriteLoadConstraintSettings .WRITE_LOAD_DECIDER_ENABLED_SETTING .getKey (),
229+ WriteLoadConstraintSettings .WriteLoadDeciderStatus .DISABLED
230+ );
231+ }
232+
233+ private static NoOpClient nodeStatsClient (ThreadPool threadPool ) {
234+ return new NoOpClient (threadPool ) {
235+ @ Override
236+ @ SuppressWarnings ("unchecked" )
237+ protected <Request extends ActionRequest , Response extends ActionResponse > void doExecute (
238+ ActionType <Response > action ,
239+ Request request ,
240+ ActionListener <Response > listener
241+ ) {
242+ if (request instanceof NodesStatsRequest ) {
243+ NodeStats nodeStats = mock (NodeStats .class );
244+ JvmStats jvmStats = mock (JvmStats .class );
245+ JvmStats .Mem mem = mock (JvmStats .Mem .class );
246+ Mockito .when (nodeStats .getNode ()).thenReturn (DiscoveryNodeUtils .create ("node-id" ));
247+ Mockito .when (nodeStats .getJvm ()).thenReturn (jvmStats );
248+ Mockito .when (jvmStats .getMem ()).thenReturn (mem );
249+ Mockito .when (mem .getHeapMax ()).thenReturn (ByteSizeValue .ofBytes (1_000L ));
250+ listener .onResponse ((Response ) new NodesStatsResponse (new ClusterName ("cluster" ), List .of (nodeStats ), List .of ()));
251+ } else {
252+ fail ("unexpected action: " + action .name ());
253+ }
254+ }
255+ };
256+ }
257+
258+ private record RefreshTestContext (Settings settings , ThreadPool threadPool , ClusterService clusterService ) implements AutoCloseable {
259+
260+ static RefreshTestContext create (Settings settings ) {
261+ final ClusterSettings clusterSettings = new ClusterSettings (settings , ClusterSettings .BUILT_IN_CLUSTER_SETTINGS );
262+ final DeterministicTaskQueue deterministicTaskQueue = new DeterministicTaskQueue ();
263+ final ThreadPool threadPool = deterministicTaskQueue .getThreadPool ();
264+ return new RefreshTestContext (settings , threadPool , ClusterServiceUtils .createClusterService (threadPool , clusterSettings ));
265+ }
266+
267+ InternalClusterInfoService createClusterInfoService (
268+ EstimatedHeapUsageCollector estimatedHeapUsageCollector ,
269+ CacheSizesAndCommitmentCollector cacheSizesAndCommitmentCollector ,
270+ PartitionSizeCollector partitionSizeCollector ,
271+ SearchLaneRequirementsCollector searchLaneRequirementsCollector
272+ ) {
273+ return createClusterInfoService (
274+ new NoOpClient (threadPool ),
275+ estimatedHeapUsageCollector ,
276+ cacheSizesAndCommitmentCollector ,
277+ partitionSizeCollector ,
278+ searchLaneRequirementsCollector
279+ );
280+ }
281+
282+ InternalClusterInfoService createClusterInfoService (
283+ NoOpClient client ,
284+ EstimatedHeapUsageCollector estimatedHeapUsageCollector ,
285+ CacheSizesAndCommitmentCollector cacheSizesAndCommitmentCollector ,
286+ PartitionSizeCollector partitionSizeCollector ,
287+ SearchLaneRequirementsCollector searchLaneRequirementsCollector
288+ ) {
289+ final InternalClusterInfoService clusterInfoService = new InternalClusterInfoService (
290+ settings ,
291+ new WriteLoadConstraintSettings (clusterService .getClusterSettings ()),
292+ clusterService ,
293+ threadPool ,
294+ client ,
295+ estimatedHeapUsageCollector ,
296+ cacheSizesAndCommitmentCollector ,
297+ partitionSizeCollector ,
298+ NodeUsageStatsForThreadPoolsCollector .EMPTY ,
299+ searchLaneRequirementsCollector
300+ );
301+ // Refresh is a no-op if there are no listeners, and AsyncRefresh asserts that at least one listener is notified.
302+ clusterInfoService .addListener (ignored -> {});
303+ return clusterInfoService ;
304+ }
305+
306+ @ Override
307+ public void close () {
308+ clusterService .close ();
309+ }
310+ }
313311}
0 commit comments