Skip to content

Commit 671e103

Browse files
committed
fix
Signed-off-by: Jackie <jkhanjob@gmail.com>
1 parent 200d461 commit 671e103

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/main/java/org/opensearch/ad/InsightsJobProcessor.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,12 @@ public void registerSettings(Settings settings) {
116116

117117
public void setXContentRegistry(NamedXContentRegistry xContentRegistry) {
118118
this.xContentRegistry = xContentRegistry;
119-
initMlCommonsClient();
120119
}
121120

122121
@Override
123122
public void setClient(org.opensearch.transport.client.Client client) {
124123
super.setClient(client);
125124
this.localClient = client;
126-
initMlCommonsClient();
127125
}
128126

129127
@Override
@@ -134,7 +132,15 @@ public void setThreadPool(ThreadPool threadPool) {
134132

135133
private synchronized void initMlCommonsClient() {
136134
if (this.mlCommonsClient == null && this.localClient != null && this.xContentRegistry != null) {
137-
this.mlCommonsClient = new MLCommonsClient(this.localClient, this.xContentRegistry);
135+
try {
136+
this.mlCommonsClient = new MLCommonsClient(this.localClient, this.xContentRegistry);
137+
} catch (NoClassDefFoundError e) {
138+
log.warn("ML Commons classes not found; Insights correlation will be skipped", e);
139+
this.mlCommonsClient = null;
140+
} catch (Throwable t) {
141+
log.warn("Failed to initialize ML Commons client; Insights correlation will be skipped", t);
142+
this.mlCommonsClient = null;
143+
}
138144
}
139145
}
140146

@@ -294,7 +300,7 @@ private void querySystemResultIndex(
294300
null
295301
)
296302
.sort("data_start_time", SortOrder.ASC)
297-
.sort("_shard_doc");
303+
.sort("_id", SortOrder.ASC);
298304

299305
InjectSecurity injectSecurity = new InjectSecurity(jobParameter.getName(), settings, localClient.threadPool().getThreadContext());
300306
try {
@@ -337,7 +343,7 @@ private void fetchPagedAnomalies(
337343
.size(baseSource.size())
338344
.fetchSource(baseSource.fetchSource())
339345
.sort("data_start_time", SortOrder.ASC)
340-
.sort("_shard_doc");
346+
.sort("_id", SortOrder.ASC);
341347

342348
if (searchAfter != null) {
343349
pageSource.searchAfter(searchAfter);
@@ -439,6 +445,11 @@ private void processAnomaliesWithMLCommons(
439445
}
440446

441447
initMlCommonsClient();
448+
if (mlCommonsClient == null) {
449+
log.info("Skipping ML correlation because ML Commons is not available");
450+
releaseLock(jobParameter, lockService, lock);
451+
return;
452+
}
442453
mlCommonsClient.executeMetricsCorrelation(input, ActionListener.wrap(mlOutput -> {
443454
log.info("ML Commons correlation completed, found {} event clusters", mlOutput.getInferenceResults().size());
444455

0 commit comments

Comments
 (0)