Skip to content

Commit 16ecc7a

Browse files
committed
Resolve duplicate key exception in GetDatafeedRunningStateAction
Implement merge function for duplicate datafeed states when a datafeed is force-stopped and restarted before cancellation completes. Select the most appropriate state based on: 1. Prefer state with more recent searchInterval.startMs when both exist 2. Prefer states with searchInterval over those without 3. Default to second state when all criteria are equal
1 parent f7d7ce7 commit 16ecc7a

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

docs/changelog/125477.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 125477
2+
summary: Prevent get datafeeds stats API returning an error when local tasks are slow to stop
3+
area: Machine Learning
4+
type: bug
5+
issues:
6+
- 104160

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,28 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
145145
}
146146

147147
private final Map<String, RunningState> datafeedRunningState;
148+
private static RunningState selectMostRecentState(RunningState state1, RunningState state2) {
148149

150+
if (state1.searchInterval != null && state2.searchInterval != null) {
151+
return state1.searchInterval.startMs() > state2.searchInterval.startMs() ? state1 : state2;
152+
}
153+
154+
if (state1.searchInterval != null) {
155+
return state1;
156+
}
157+
if (state2.searchInterval != null) {
158+
return state2;
159+
}
160+
161+
162+
return state2;
163+
}
149164
public static Response fromResponses(List<Response> responses) {
150165
return new Response(
151166
responses.stream()
152167
.flatMap(r -> r.datafeedRunningState.entrySet().stream())
153168
.filter(entry -> entry.getValue() != null)
154-
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))
169+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, Response::selectMostRecentState))
155170
);
156171
}
157172

0 commit comments

Comments
 (0)