99
1010package org .elasticsearch .cluster .routing .allocation .allocator ;
1111
12+ import org .elasticsearch .cluster .node .DiscoveryNode ;
1213import org .elasticsearch .common .util .CollectionUtils ;
1314import org .elasticsearch .plugins .Plugin ;
1415import org .elasticsearch .plugins .PluginsService ;
1718import org .hamcrest .Matcher ;
1819
1920import java .util .Collection ;
21+ import java .util .stream .Collectors ;
2022
2123import static org .hamcrest .Matchers .empty ;
24+ import static org .hamcrest .Matchers .equalTo ;
25+ import static org .hamcrest .Matchers .greaterThanOrEqualTo ;
26+ import static org .hamcrest .Matchers .in ;
27+ import static org .hamcrest .Matchers .is ;
2228import static org .hamcrest .Matchers .not ;
2329
30+ @ ESIntegTestCase .ClusterScope (scope = ESIntegTestCase .Scope .TEST , numDataNodes = 0 )
2431public class DesiredBalanceReconcilerMetricsIT extends ESIntegTestCase {
2532
2633 @ Override
@@ -31,6 +38,7 @@ protected Collection<Class<? extends Plugin>> nodePlugins() {
3138 public void testDesiredBalanceGaugeMetricsAreOnlyPublishedByCurrentMaster () throws Exception {
3239 internalCluster ().ensureAtLeastNumDataNodes (2 );
3340 prepareCreate ("test" ).setSettings (indexSettings (2 , 1 )).get ();
41+ indexRandom (randomBoolean (), "test" , between (50 , 100 ));
3442 ensureGreen ();
3543
3644 assertOnlyMasterIsPublishingMetrics ();
@@ -45,6 +53,59 @@ public void testDesiredBalanceGaugeMetricsAreOnlyPublishedByCurrentMaster() thro
4553 }
4654 }
4755
56+ public void testDesiredBalanceNodeWeightMetrics () {
57+ internalCluster ().startNodes (2 );
58+ prepareCreate ("test" ).setSettings (indexSettings (2 , 1 )).get ();
59+ indexRandom (randomBoolean (), "test" , between (50 , 100 ));
60+ ensureGreen ();
61+ final var telemetryPlugin = getTelemetryPlugin (internalCluster ().getMasterName ());
62+ telemetryPlugin .collect ();
63+ assertThat (telemetryPlugin .getLongGaugeMeasurement (DesiredBalanceMetrics .UNASSIGNED_SHARDS_METRIC_NAME ), not (empty ()));
64+ assertThat (telemetryPlugin .getLongGaugeMeasurement (DesiredBalanceMetrics .TOTAL_SHARDS_METRIC_NAME ), not (empty ()));
65+ assertThat (telemetryPlugin .getLongGaugeMeasurement (DesiredBalanceMetrics .UNDESIRED_ALLOCATION_COUNT_METRIC_NAME ), not (empty ()));
66+ assertThat (telemetryPlugin .getDoubleGaugeMeasurement (DesiredBalanceMetrics .UNDESIRED_ALLOCATION_RATIO_METRIC_NAME ), not (empty ()));
67+
68+ var nodeIds = internalCluster ().clusterService ().state ().nodes ().stream ().map (DiscoveryNode ::getId ).collect (Collectors .toSet ());
69+ var nodeNames = internalCluster ().clusterService ().state ().nodes ().stream ().map (DiscoveryNode ::getName ).collect (Collectors .toSet ());
70+
71+ final var nodeWeightsMetrics = telemetryPlugin .getDoubleGaugeMeasurement (
72+ DesiredBalanceMetrics .DESIRED_BALANCE_NODE_WEIGHT_METRIC_NAME
73+ );
74+ assertThat (nodeWeightsMetrics .size (), equalTo (2 ));
75+ for (var nodeStat : nodeWeightsMetrics ) {
76+ assertThat (nodeStat .value ().doubleValue (), greaterThanOrEqualTo (0.0 ));
77+ assertThat ((String ) nodeStat .attributes ().get ("node_id" ), is (in (nodeIds )));
78+ assertThat ((String ) nodeStat .attributes ().get ("node_name" ), is (in (nodeNames )));
79+ }
80+ final var nodeShardCountMetrics = telemetryPlugin .getLongGaugeMeasurement (
81+ DesiredBalanceMetrics .DESIRED_BALANCE_NODE_SHARD_COUNT_METRIC_NAME
82+ );
83+ assertThat (nodeShardCountMetrics .size (), equalTo (2 ));
84+ for (var nodeStat : nodeShardCountMetrics ) {
85+ assertThat (nodeStat .value ().longValue (), equalTo (2L ));
86+ assertThat ((String ) nodeStat .attributes ().get ("node_id" ), is (in (nodeIds )));
87+ assertThat ((String ) nodeStat .attributes ().get ("node_name" ), is (in (nodeNames )));
88+ }
89+ final var nodeWriteLoadMetrics = telemetryPlugin .getDoubleGaugeMeasurement (
90+ DesiredBalanceMetrics .DESIRED_BALANCE_NODE_WRITE_LOAD_METRIC_NAME
91+ );
92+ assertThat (nodeWriteLoadMetrics .size (), equalTo (2 ));
93+ for (var nodeStat : nodeWriteLoadMetrics ) {
94+ assertThat (nodeStat .value ().doubleValue (), greaterThanOrEqualTo (0.0 ));
95+ assertThat ((String ) nodeStat .attributes ().get ("node_id" ), is (in (nodeIds )));
96+ assertThat ((String ) nodeStat .attributes ().get ("node_name" ), is (in (nodeNames )));
97+ }
98+ final var nodeDiskUsageMetrics = telemetryPlugin .getDoubleGaugeMeasurement (
99+ DesiredBalanceMetrics .DESIRED_BALANCE_NODE_DISK_USAGE_METRIC_NAME
100+ );
101+ assertThat (nodeDiskUsageMetrics .size (), equalTo (2 ));
102+ for (var nodeStat : nodeDiskUsageMetrics ) {
103+ assertThat (nodeStat .value ().doubleValue (), greaterThanOrEqualTo (0.0 ));
104+ assertThat ((String ) nodeStat .attributes ().get ("node_id" ), is (in (nodeIds )));
105+ assertThat ((String ) nodeStat .attributes ().get ("node_name" ), is (in (nodeNames )));
106+ }
107+ }
108+
48109 private static void assertOnlyMasterIsPublishingMetrics () {
49110 String masterNodeName = internalCluster ().getMasterName ();
50111 String [] nodeNames = internalCluster ().getNodeNames ();
@@ -54,16 +115,33 @@ private static void assertOnlyMasterIsPublishingMetrics() {
54115 }
55116
56117 private static void assertMetricsAreBeingPublished (String nodeName , boolean shouldBePublishing ) {
57- final TestTelemetryPlugin testTelemetryPlugin = internalCluster ().getInstance (PluginsService .class , nodeName )
58- .filterPlugins (TestTelemetryPlugin .class )
59- .findFirst ()
60- .orElseThrow ();
118+ final TestTelemetryPlugin testTelemetryPlugin = getTelemetryPlugin (nodeName );
61119 testTelemetryPlugin .resetMeter ();
62120 testTelemetryPlugin .collect ();
63121 Matcher <Collection <?>> matcher = shouldBePublishing ? not (empty ()) : empty ();
64122 assertThat (testTelemetryPlugin .getLongGaugeMeasurement (DesiredBalanceMetrics .UNASSIGNED_SHARDS_METRIC_NAME ), matcher );
65123 assertThat (testTelemetryPlugin .getLongGaugeMeasurement (DesiredBalanceMetrics .TOTAL_SHARDS_METRIC_NAME ), matcher );
66124 assertThat (testTelemetryPlugin .getLongGaugeMeasurement (DesiredBalanceMetrics .UNDESIRED_ALLOCATION_COUNT_METRIC_NAME ), matcher );
67125 assertThat (testTelemetryPlugin .getDoubleGaugeMeasurement (DesiredBalanceMetrics .UNDESIRED_ALLOCATION_RATIO_METRIC_NAME ), matcher );
126+ assertThat (testTelemetryPlugin .getDoubleGaugeMeasurement (DesiredBalanceMetrics .DESIRED_BALANCE_NODE_WEIGHT_METRIC_NAME ), matcher );
127+ assertThat (
128+ testTelemetryPlugin .getDoubleGaugeMeasurement (DesiredBalanceMetrics .DESIRED_BALANCE_NODE_WRITE_LOAD_METRIC_NAME ),
129+ matcher
130+ );
131+ assertThat (
132+ testTelemetryPlugin .getDoubleGaugeMeasurement (DesiredBalanceMetrics .DESIRED_BALANCE_NODE_DISK_USAGE_METRIC_NAME ),
133+ matcher
134+ );
135+ assertThat (
136+ testTelemetryPlugin .getLongGaugeMeasurement (DesiredBalanceMetrics .DESIRED_BALANCE_NODE_SHARD_COUNT_METRIC_NAME ),
137+ matcher
138+ );
139+ }
140+
141+ private static TestTelemetryPlugin getTelemetryPlugin (String nodeName ) {
142+ return internalCluster ().getInstance (PluginsService .class , nodeName )
143+ .filterPlugins (TestTelemetryPlugin .class )
144+ .findFirst ()
145+ .orElseThrow ();
68146 }
69147}
0 commit comments