2020import org .elasticsearch .common .bytes .BytesReference ;
2121import org .elasticsearch .common .settings .Settings ;
2222import org .elasticsearch .common .xcontent .LoggingDeprecationHandler ;
23- import org .elasticsearch .features .FeatureService ;
2423import org .elasticsearch .health .HealthIndicatorDetails ;
2524import org .elasticsearch .health .HealthStatus ;
2625import org .elasticsearch .health .metadata .HealthMetadata ;
3938import org .junit .AfterClass ;
4039import org .junit .Before ;
4140import org .junit .BeforeClass ;
42- import org .mockito .Mockito ;
4341
4442import java .io .IOException ;
4543import java .util .List ;
6159import static org .elasticsearch .indices .ShardLimitValidator .FROZEN_GROUP ;
6260import static org .elasticsearch .indices .ShardLimitValidator .INDEX_SETTING_SHARD_LIMIT_GROUP ;
6361import static org .elasticsearch .indices .ShardLimitValidator .NORMAL_GROUP ;
62+ import static org .elasticsearch .indices .ShardLimitValidator .SETTING_CLUSTER_MAX_SHARDS_PER_NODE ;
63+ import static org .elasticsearch .indices .ShardLimitValidator .SETTING_CLUSTER_MAX_SHARDS_PER_NODE_FROZEN ;
64+ import static org .hamcrest .Matchers .allOf ;
65+ import static org .hamcrest .Matchers .containsString ;
6466import static org .hamcrest .Matchers .equalTo ;
6567import static org .hamcrest .Matchers .hasSize ;
6668import static org .hamcrest .Matchers .is ;
67- import static org .mockito .ArgumentMatchers .any ;
6869
6970public class ShardsCapacityHealthIndicatorServiceTests extends ESTestCase {
7071
@@ -73,7 +74,6 @@ public class ShardsCapacityHealthIndicatorServiceTests extends ESTestCase {
7374 private static ThreadPool threadPool ;
7475
7576 private ClusterService clusterService ;
76- private FeatureService featureService ;
7777 private DiscoveryNode dataNode ;
7878 private DiscoveryNode frozenNode ;
7979
@@ -92,9 +92,6 @@ public void setUp() throws Exception {
9292 .build ();
9393
9494 clusterService = ClusterServiceUtils .createClusterService (threadPool );
95-
96- featureService = Mockito .mock (FeatureService .class );
97- Mockito .when (featureService .clusterHasFeature (any (), any ())).thenReturn (true );
9895 }
9996
10097 @ After
@@ -122,7 +119,7 @@ public void testNoShardsCapacityMetadata() throws IOException {
122119 createIndexInDataNode (100 )
123120 )
124121 );
125- var target = new ShardsCapacityHealthIndicatorService (clusterService , featureService );
122+ var target = new ShardsCapacityHealthIndicatorService (clusterService );
126123 var indicatorResult = target .calculate (true , HealthInfo .EMPTY_HEALTH_INFO );
127124
128125 assertEquals (indicatorResult .status (), HealthStatus .UNKNOWN );
@@ -136,10 +133,7 @@ public void testIndicatorYieldsGreenInCaseThereIsRoom() throws IOException {
136133 int maxShardsPerNode = randomValidMaxShards ();
137134 int maxShardsPerNodeFrozen = randomValidMaxShards ();
138135 var clusterService = createClusterService (maxShardsPerNode , maxShardsPerNodeFrozen , createIndexInDataNode (maxShardsPerNode / 4 ));
139- var indicatorResult = new ShardsCapacityHealthIndicatorService (clusterService , featureService ).calculate (
140- true ,
141- HealthInfo .EMPTY_HEALTH_INFO
142- );
136+ var indicatorResult = new ShardsCapacityHealthIndicatorService (clusterService ).calculate (true , HealthInfo .EMPTY_HEALTH_INFO );
143137
144138 assertEquals (indicatorResult .status (), HealthStatus .GREEN );
145139 assertTrue (indicatorResult .impacts ().isEmpty ());
@@ -158,15 +152,36 @@ public void testIndicatorYieldsGreenInCaseThereIsRoom() throws IOException {
158152 );
159153 }
160154
155+ public void testDiagnoses () {
156+ assertEquals ("shards_capacity" , SHARDS_MAX_CAPACITY_REACHED_DATA_NODES .definition ().indicatorName ());
157+ assertEquals ("decrease_shards_per_non_frozen_node" , SHARDS_MAX_CAPACITY_REACHED_DATA_NODES .definition ().id ());
158+ assertThat (
159+ SHARDS_MAX_CAPACITY_REACHED_DATA_NODES .definition ().cause (),
160+ allOf (containsString ("maximum number of shards" ), containsString (SETTING_CLUSTER_MAX_SHARDS_PER_NODE .getKey ()))
161+ );
162+ assertThat (
163+ SHARDS_MAX_CAPACITY_REACHED_DATA_NODES .definition ().action (),
164+ allOf (containsString ("Increase the number of nodes in your cluster" ), containsString ("remove some non-frozen indices" ))
165+ );
166+
167+ assertEquals ("shards_capacity" , SHARDS_MAX_CAPACITY_REACHED_FROZEN_NODES .definition ().indicatorName ());
168+ assertEquals ("decrease_shards_per_frozen_node" , SHARDS_MAX_CAPACITY_REACHED_FROZEN_NODES .definition ().id ());
169+ assertThat (
170+ SHARDS_MAX_CAPACITY_REACHED_FROZEN_NODES .definition ().cause (),
171+ allOf (containsString ("maximum number of shards" ), containsString (SETTING_CLUSTER_MAX_SHARDS_PER_NODE_FROZEN .getKey ()))
172+ );
173+ assertThat (
174+ SHARDS_MAX_CAPACITY_REACHED_FROZEN_NODES .definition ().action (),
175+ allOf (containsString ("Increase the number of nodes in your cluster" ), containsString ("remove some frozen indices" ))
176+ );
177+ }
178+
161179 public void testIndicatorYieldsYellowInCaseThereIsNotEnoughRoom () throws IOException {
162180 {
163181 // Only data_nodes does not have enough space
164182 int maxShardsPerNodeFrozen = randomValidMaxShards ();
165183 var clusterService = createClusterService (25 , maxShardsPerNodeFrozen , createIndexInDataNode (4 ));
166- var indicatorResult = new ShardsCapacityHealthIndicatorService (clusterService , featureService ).calculate (
167- true ,
168- HealthInfo .EMPTY_HEALTH_INFO
169- );
184+ var indicatorResult = new ShardsCapacityHealthIndicatorService (clusterService ).calculate (true , HealthInfo .EMPTY_HEALTH_INFO );
170185
171186 assertEquals (indicatorResult .status (), YELLOW );
172187 assertEquals (indicatorResult .symptom (), "Cluster is close to reaching the configured maximum number of shards for data nodes." );
@@ -189,10 +204,7 @@ public void testIndicatorYieldsYellowInCaseThereIsNotEnoughRoom() throws IOExcep
189204 // Only frozen_nodes does not have enough space
190205 int maxShardsPerNode = randomValidMaxShards ();
191206 var clusterService = createClusterService (maxShardsPerNode , 25 , createIndexInFrozenNode (4 ));
192- var indicatorResult = new ShardsCapacityHealthIndicatorService (clusterService , featureService ).calculate (
193- true ,
194- HealthInfo .EMPTY_HEALTH_INFO
195- );
207+ var indicatorResult = new ShardsCapacityHealthIndicatorService (clusterService ).calculate (true , HealthInfo .EMPTY_HEALTH_INFO );
196208
197209 assertEquals (indicatorResult .status (), YELLOW );
198210 assertEquals (
@@ -217,10 +229,7 @@ public void testIndicatorYieldsYellowInCaseThereIsNotEnoughRoom() throws IOExcep
217229 {
218230 // Both data and frozen nodes does not have enough space
219231 var clusterService = createClusterService (25 , 25 , createIndexInDataNode (4 ), createIndexInFrozenNode (4 ));
220- var indicatorResult = new ShardsCapacityHealthIndicatorService (clusterService , featureService ).calculate (
221- true ,
222- HealthInfo .EMPTY_HEALTH_INFO
223- );
232+ var indicatorResult = new ShardsCapacityHealthIndicatorService (clusterService ).calculate (true , HealthInfo .EMPTY_HEALTH_INFO );
224233
225234 assertEquals (indicatorResult .status (), YELLOW );
226235 assertEquals (
@@ -251,10 +260,7 @@ public void testIndicatorYieldsRedInCaseThereIsNotEnoughRoom() throws IOExceptio
251260 // Only data_nodes does not have enough space
252261 int maxShardsPerNodeFrozen = randomValidMaxShards ();
253262 var clusterService = createClusterService (25 , maxShardsPerNodeFrozen , createIndexInDataNode (11 ));
254- var indicatorResult = new ShardsCapacityHealthIndicatorService (clusterService , featureService ).calculate (
255- true ,
256- HealthInfo .EMPTY_HEALTH_INFO
257- );
263+ var indicatorResult = new ShardsCapacityHealthIndicatorService (clusterService ).calculate (true , HealthInfo .EMPTY_HEALTH_INFO );
258264
259265 assertEquals (indicatorResult .status (), RED );
260266 assertEquals (indicatorResult .symptom (), "Cluster is close to reaching the configured maximum number of shards for data nodes." );
@@ -277,10 +283,7 @@ public void testIndicatorYieldsRedInCaseThereIsNotEnoughRoom() throws IOExceptio
277283 // Only frozen_nodes does not have enough space
278284 int maxShardsPerNode = randomValidMaxShards ();
279285 var clusterService = createClusterService (maxShardsPerNode , 25 , createIndexInFrozenNode (11 ));
280- var indicatorResult = new ShardsCapacityHealthIndicatorService (clusterService , featureService ).calculate (
281- true ,
282- HealthInfo .EMPTY_HEALTH_INFO
283- );
286+ var indicatorResult = new ShardsCapacityHealthIndicatorService (clusterService ).calculate (true , HealthInfo .EMPTY_HEALTH_INFO );
284287
285288 assertEquals (indicatorResult .status (), RED );
286289 assertEquals (
@@ -305,10 +308,7 @@ public void testIndicatorYieldsRedInCaseThereIsNotEnoughRoom() throws IOExceptio
305308 {
306309 // Both data and frozen nodes does not have enough space
307310 var clusterService = createClusterService (25 , 25 , createIndexInDataNode (11 ), createIndexInFrozenNode (11 ));
308- var indicatorResult = new ShardsCapacityHealthIndicatorService (clusterService , featureService ).calculate (
309- true ,
310- HealthInfo .EMPTY_HEALTH_INFO
311- );
311+ var indicatorResult = new ShardsCapacityHealthIndicatorService (clusterService ).calculate (true , HealthInfo .EMPTY_HEALTH_INFO );
312312
313313 assertEquals (indicatorResult .status (), RED );
314314 assertEquals (
@@ -377,22 +377,19 @@ public void testCalculateMethods() {
377377 public void testMappedFieldsForTelemetry () {
378378 assertEquals (ShardsCapacityHealthIndicatorService .NAME , "shards_capacity" );
379379 assertEquals (
380- "elasticsearch:health:shards_capacity:diagnosis:increase_max_shards_per_node " ,
380+ "elasticsearch:health:shards_capacity:diagnosis:decrease_shards_per_non_frozen_node " ,
381381 SHARDS_MAX_CAPACITY_REACHED_DATA_NODES .definition ().getUniqueId ()
382382 );
383383 assertEquals (
384- "elasticsearch:health:shards_capacity:diagnosis:increase_max_shards_per_node_frozen " ,
384+ "elasticsearch:health:shards_capacity:diagnosis:decrease_shards_per_frozen_node " ,
385385 SHARDS_MAX_CAPACITY_REACHED_FROZEN_NODES .definition ().getUniqueId ()
386386 );
387387 }
388388
389389 public void testSkippingFieldsWhenVerboseIsFalse () {
390390 int maxShardsPerNodeFrozen = randomValidMaxShards ();
391391 var clusterService = createClusterService (25 , maxShardsPerNodeFrozen , createIndexInDataNode (11 ));
392- var indicatorResult = new ShardsCapacityHealthIndicatorService (clusterService , featureService ).calculate (
393- false ,
394- HealthInfo .EMPTY_HEALTH_INFO
395- );
392+ var indicatorResult = new ShardsCapacityHealthIndicatorService (clusterService ).calculate (false , HealthInfo .EMPTY_HEALTH_INFO );
396393
397394 assertEquals (indicatorResult .status (), RED );
398395 assertEquals (indicatorResult .symptom (), "Cluster is close to reaching the configured maximum number of shards for data nodes." );
@@ -441,7 +438,7 @@ private ClusterState createClusterState(
441438 var metadata = Metadata .builder ()
442439 .persistentSettings (
443440 Settings .builder ()
444- .put (ShardLimitValidator . SETTING_CLUSTER_MAX_SHARDS_PER_NODE .getKey (), maxShardsPerNode )
441+ .put (SETTING_CLUSTER_MAX_SHARDS_PER_NODE .getKey (), maxShardsPerNode )
445442 .put (ShardLimitValidator .SETTING_CLUSTER_MAX_SHARDS_PER_NODE_FROZEN .getKey (), maxShardsPerNodeFrozen )
446443 .build ()
447444 );
0 commit comments