Skip to content

Commit 8287f40

Browse files
committed
Optimize MlConfigVersion#getMinMaxMlConfigVersion (elastic#125432)
1 parent 1ba6ed2 commit 8287f40

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MlConfigVersion.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Collection;
2626
import java.util.Collections;
2727
import java.util.HashMap;
28+
import java.util.HashSet;
2829
import java.util.Map;
2930
import java.util.NavigableMap;
3031
import java.util.Set;
@@ -280,17 +281,24 @@ public static MlConfigVersion getMaxMlConfigVersion(DiscoveryNodes nodes) {
280281
public static Tuple<MlConfigVersion, MlConfigVersion> getMinMaxMlConfigVersion(DiscoveryNodes nodes) {
281282
MlConfigVersion minMlConfigVersion = MlConfigVersion.CURRENT;
282283
MlConfigVersion maxMlConfigVersion = MlConfigVersion.FIRST_ML_VERSION;
284+
285+
// many nodes will have the same versions, so de-duplicate versions first so that we only have to parse unique versions
286+
final Set<String> versions = new HashSet<>();
283287
for (DiscoveryNode node : nodes) {
284-
try {
285-
MlConfigVersion mlConfigVersion = getMlConfigVersionForNode(node);
286-
if (mlConfigVersion.before(minMlConfigVersion)) {
287-
minMlConfigVersion = mlConfigVersion;
288-
}
289-
if (mlConfigVersion.after(maxMlConfigVersion)) {
290-
maxMlConfigVersion = mlConfigVersion;
291-
}
292-
} catch (IllegalStateException e) {
293-
// This means we encountered a node that is after 8.10.0 but has the ML plugin disabled - ignore it
288+
String mlConfigVerStr = node.getAttributes().get(ML_CONFIG_VERSION_NODE_ATTR);
289+
if (mlConfigVerStr != null) { // ignore nodes that don't have an ML config version
290+
versions.add(mlConfigVerStr);
291+
}
292+
}
293+
294+
// of the unique versions, find the min and max
295+
for (String version : versions) {
296+
MlConfigVersion mlConfigVersion = fromString(version);
297+
if (mlConfigVersion.before(minMlConfigVersion)) {
298+
minMlConfigVersion = mlConfigVersion;
299+
}
300+
if (mlConfigVersion.after(maxMlConfigVersion)) {
301+
maxMlConfigVersion = mlConfigVersion;
294302
}
295303
}
296304
return new Tuple<>(minMlConfigVersion, maxMlConfigVersion);
@@ -304,6 +312,9 @@ public static MlConfigVersion getMlConfigVersionForNode(DiscoveryNode node) {
304312
return fromId(node.getPre811VersionId().orElseThrow(() -> new IllegalStateException("getting legacy version id not possible")));
305313
}
306314

315+
// supports fromString below
316+
private static final Pattern ML_VERSION_PATTERN = Pattern.compile("^(\\d+)\\.(\\d+)\\.(\\d+)(?:-\\w+)?$");
317+
307318
// Parse an MlConfigVersion from a string.
308319
// Note that version "8.10.x" and "8.11.0" are silently converted to "10.0.0".
309320
// This is to support upgrade scenarios in pre-prod QA environments.
@@ -319,7 +330,7 @@ public static MlConfigVersion fromString(String str) {
319330
if (str.startsWith("8.10.") || str.equals("8.11.0")) {
320331
return V_10;
321332
}
322-
Matcher matcher = Pattern.compile("^(\\d+)\\.(\\d+)\\.(\\d+)(?:-\\w+)?$").matcher(str);
333+
Matcher matcher = ML_VERSION_PATTERN.matcher(str);
323334
if (matcher.matches() == false) {
324335
throw new IllegalArgumentException("ML config version [" + str + "] not valid");
325336
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/GetTrainedModelsStatsAction.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,6 @@ public static class TrainedModelStats implements ToXContentObject, Writeable {
9191
private final AssignmentStats deploymentStats;
9292
private final int pipelineCount;
9393

94-
private static final IngestStats EMPTY_INGEST_STATS = new IngestStats(
95-
IngestStats.Stats.IDENTITY,
96-
Collections.emptyList(),
97-
Collections.emptyMap()
98-
);
99-
10094
public TrainedModelStats(
10195
String modelId,
10296
TrainedModelSizeStats modelSizeStats,
@@ -107,7 +101,7 @@ public TrainedModelStats(
107101
) {
108102
this.modelId = Objects.requireNonNull(modelId);
109103
this.modelSizeStats = modelSizeStats;
110-
this.ingestStats = ingestStats == null ? EMPTY_INGEST_STATS : ingestStats;
104+
this.ingestStats = ingestStats == null ? IngestStats.IDENTITY : ingestStats;
111105
if (pipelineCount < 0) {
112106
throw new ElasticsearchException("[{}] must be a greater than or equal to 0", PIPELINE_COUNT.getPreferredName());
113107
}

0 commit comments

Comments
 (0)