Skip to content

Commit 39449b2

Browse files
authored
Fix query profiler test basic with concurrent execution (opensearch-project#11397)
Signed-off-by: Ticheng Lin <ticheng@amazon.com>
1 parent edf7861 commit 39449b2

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

server/src/main/java/org/opensearch/search/profile/query/ConcurrentQueryProfileBreakdown.java

+19-10
Original file line numberDiff line numberDiff line change
@@ -185,23 +185,25 @@ Map<Collector, Map<String, Long>> buildSliceLevelBreakdown() {
185185
}
186186
final Map<String, Long> currentSliceLeafBreakdownMap = contexts.get(sliceLeaf).toBreakdownMap();
187187
// get the count for current leaf timing type
188+
final long sliceLeafTimingTypeCount = currentSliceLeafBreakdownMap.get(timingTypeCountKey);
188189
currentSliceBreakdown.compute(
189190
timingTypeCountKey,
190-
(key, value) -> (value == null)
191-
? currentSliceLeafBreakdownMap.get(timingTypeCountKey)
192-
: value + currentSliceLeafBreakdownMap.get(timingTypeCountKey)
191+
(key, value) -> (value == null) ? sliceLeafTimingTypeCount : value + sliceLeafTimingTypeCount
193192
);
194193

195-
// compute the sliceStartTime for timingType using min of startTime across slice leaves
196-
final long sliceLeafTimingTypeStartTime = currentSliceLeafBreakdownMap.get(timingTypeStartKey);
197-
if (sliceLeafTimingTypeStartTime == 0L && currentSliceBreakdown.get(timingTypeCountKey) != 0L) {
194+
if (sliceLeafTimingTypeCount == 0L) {
198195
// In case where a slice with multiple leaves, it is possible that any one of the leaves has 0 invocations for a
199-
// specific breakdown type. For instance, let's consider a slice with three leaves: leaf A with a score count of 5,
200-
// leaf B with a score count of 0, and leaf C with a score count of 4. In this situation, we only compute the timing
201-
// type slice start/end time based on leaf A and leaf C. This is because leaf B has a start time of zero. And it
202-
// doesn't represent an actual timing; rather, it indicates no invocations.
196+
// specific breakdown type. We should skip the slice start/end time computation for any leaf with 0 invocations on a
197+
// timing type, as 0 does not represent an actual timing.
198+
// For example, a slice has 0 invocations for a breakdown type from its leading leaves. Another example, let's
199+
// consider a slice with three leaves: leaf A with a score count of 5, leaf B with a score count of 0,
200+
// and leaf C with a score count of 4. In this situation, we only compute the timing type slice start/end time based
201+
// on leaf A and leaf C. This is because leaf B has a start time of zero.
203202
continue;
204203
}
204+
205+
// compute the sliceStartTime for timingType using min of startTime across slice leaves
206+
final long sliceLeafTimingTypeStartTime = currentSliceLeafBreakdownMap.get(timingTypeStartKey);
205207
currentSliceBreakdown.compute(
206208
timingTypeSliceStartTimeKey,
207209
(key, value) -> (value == null) ? sliceLeafTimingTypeStartTime : Math.min(value, sliceLeafTimingTypeStartTime)
@@ -216,6 +218,13 @@ Map<Collector, Map<String, Long>> buildSliceLevelBreakdown() {
216218
(key, value) -> (value == null) ? sliceLeafTimingTypeEndTime : Math.max(value, sliceLeafTimingTypeEndTime)
217219
);
218220
}
221+
// Only when we've checked all leaves in a slice and still find no invocations, then we should set the slice start/end time
222+
// to the default 0L. This is because buildQueryBreakdownMap expects timingTypeSliceStartTimeKey and
223+
// timingTypeSliceEndTimeKey in the slice level breakdowns.
224+
if (currentSliceBreakdown.get(timingTypeCountKey) != null && currentSliceBreakdown.get(timingTypeCountKey) == 0L) {
225+
currentSliceBreakdown.put(timingTypeSliceStartTimeKey, 0L);
226+
currentSliceBreakdown.put(timingTypeSliceEndTimeKey, 0L);
227+
}
219228
// compute sliceMaxEndTime as max of sliceEndTime across all timing types
220229
sliceMaxEndTime = Math.max(sliceMaxEndTime, currentSliceBreakdown.getOrDefault(timingTypeSliceEndTimeKey, Long.MIN_VALUE));
221230
long currentSliceStartTime = currentSliceBreakdown.getOrDefault(timingTypeSliceStartTimeKey, Long.MAX_VALUE);

0 commit comments

Comments
 (0)