Skip to content

Commit a6c0443

Browse files
committed
fix: add additional checks for initializing the stats job collector to minimize jvm usage
Signed-off-by: Pavan Yekbote <pybot@amazon.com>
1 parent 5964268 commit a6c0443

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

plugin/src/main/java/org/opensearch/ml/cluster/MLCommonsClusterEventListener.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
package org.opensearch.ml.cluster;
77

8-
import static org.opensearch.ml.common.CommonValue.TASK_POLLING_JOB_INDEX;
8+
import static org.opensearch.ml.common.CommonValue.ML_JOBS_INDEX;
99
import static org.opensearch.ml.common.settings.MLCommonsSettings.ML_COMMONS_MONITORING_REQUEST_COUNT;
1010

1111
import java.util.List;
@@ -39,6 +39,7 @@ public class MLCommonsClusterEventListener implements ClusterStateListener {
3939
private final MLModelAutoReDeployer mlModelAutoReDeployer;
4040
private final Client client;
4141
private final MLFeatureEnabledSetting mlFeatureEnabledSetting;
42+
private boolean startedStatsJob;
4243

4344
public MLCommonsClusterEventListener(
4445
ClusterService clusterService,
@@ -90,12 +91,11 @@ public void clusterChanged(ClusterChangedEvent event) {
9091
*/
9192
for (DiscoveryNode node : state.nodes()) {
9293
if (node.isDataNode() && node.getVersion().onOrAfter(Version.V_3_1_0)) {
93-
if (mlFeatureEnabledSetting.isMetricCollectionEnabled() && mlFeatureEnabledSetting.isStaticMetricCollectionEnabled()) {
94-
mlTaskManager.indexStatsCollectorJob(true);
95-
}
96-
97-
if (clusterService.state().getMetadata().hasIndex(TASK_POLLING_JOB_INDEX)) {
98-
mlTaskManager.startTaskPollingJob();
94+
if (mlFeatureEnabledSetting.isMetricCollectionEnabled() && mlFeatureEnabledSetting.isStaticMetricCollectionEnabled()
95+
&& !clusterService.state().getMetadata().hasIndex(ML_JOBS_INDEX) && !this.startedStatsJob) {
96+
mlTaskManager.indexStatsCollectorJob(true);
97+
// using this variable in case if same node has a cluster state change event and the state is not updated yet
98+
this.startedStatsJob = true;
9999
}
100100

101101
break;

plugin/src/main/java/org/opensearch/ml/task/MLTaskManager.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,10 @@ public void onStaticMetricCollectionEnabledChanged(boolean isEnabled) {
577577
}
578578

579579
public void indexStatsCollectorJob(boolean enabled) {
580+
if (this.statsCollectorJobStarted && enabled) {
581+
return;
582+
}
583+
580584
try {
581585
MLJobParameter jobParameter = new MLJobParameter(
582586
MLJobType.STATS_COLLECTOR.name(),
@@ -593,7 +597,7 @@ public void indexStatsCollectorJob(boolean enabled) {
593597
.source(jobParameter.toXContent(JsonXContent.contentBuilder(), null))
594598
.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
595599

596-
indexJob(indexRequest, MLJobType.STATS_COLLECTOR, () -> {});
600+
indexJob(indexRequest, MLJobType.STATS_COLLECTOR, () -> this.statsCollectorJobStarted = enabled);
597601
} catch (IOException e) {
598602
log.error("Failed to index stats collection job", e);
599603
}

0 commit comments

Comments
 (0)