forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor and work on the PR comments (opensearch-project#10352)
Signed-off-by: Ticheng Lin <[email protected]>
- Loading branch information
1 parent
45c8f78
commit 8e540e1
Showing
8 changed files
with
187 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
server/src/main/java/org/opensearch/search/profile/query/ConcurrentQueryProfiler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.search.profile.query; | ||
|
||
import org.apache.lucene.search.Query; | ||
import org.opensearch.search.profile.ContextualProfileBreakdown; | ||
import org.opensearch.search.profile.ProfileResult; | ||
import org.opensearch.search.profile.Timer; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
/** | ||
* This class acts as a thread-local storage for profiling a query with concurrent execution | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public final class ConcurrentQueryProfiler extends QueryProfiler { | ||
|
||
private final Map<Long, ConcurrentQueryProfileTree> threadToProfileTree; | ||
private final Map<Long, LinkedList<Timer>> threadToRewriteTimers; | ||
|
||
public ConcurrentQueryProfiler() { | ||
super(new ConcurrentQueryProfileTree()); | ||
long threadId = getCurrentThreadId(); | ||
threadToProfileTree = new ConcurrentHashMap<>(); | ||
threadToProfileTree.put(threadId, (ConcurrentQueryProfileTree) profileTree); | ||
threadToRewriteTimers = new ConcurrentHashMap<>(); | ||
threadToRewriteTimers.put(threadId, new LinkedList<>()); | ||
} | ||
|
||
@Override | ||
public ContextualProfileBreakdown<QueryTimingType> getQueryBreakdown(Query query) { | ||
ConcurrentQueryProfileTree profileTree = threadToProfileTree.computeIfAbsent( | ||
getCurrentThreadId(), | ||
k -> new ConcurrentQueryProfileTree() | ||
); | ||
return profileTree.getProfileBreakdown(query); | ||
} | ||
|
||
/** | ||
* Removes the last (e.g. most recent) element on ConcurrentQueryProfileTree stack. | ||
*/ | ||
@Override | ||
public void pollLastElement() { | ||
ConcurrentQueryProfileTree concurrentProfileTree = threadToProfileTree.get(getCurrentThreadId()); | ||
concurrentProfileTree.pollLast(); | ||
} | ||
|
||
/** | ||
* @return a hierarchical representation of the profiled tree | ||
*/ | ||
@Override | ||
public List<ProfileResult> getTree() { | ||
List<ProfileResult> profileResults = new ArrayList<>(); | ||
for (Map.Entry<Long, ConcurrentQueryProfileTree> profile : threadToProfileTree.entrySet()) { | ||
profileResults.addAll(profile.getValue().getTree()); | ||
} | ||
return profileResults; | ||
} | ||
|
||
/** | ||
* Begin timing the rewrite phase of a request | ||
*/ | ||
@Override | ||
public void startRewriteTime() { | ||
Timer rewriteTimer = new Timer(); | ||
threadToRewriteTimers.computeIfAbsent(getCurrentThreadId(), k -> new LinkedList<>()).add(rewriteTimer); | ||
rewriteTimer.start(); | ||
} | ||
|
||
/** | ||
* Stop recording the current rewrite timer | ||
*/ | ||
public void stopAndAddRewriteTime() { | ||
Timer rewriteTimer = threadToRewriteTimers.get(getCurrentThreadId()).getLast(); | ||
rewriteTimer.stop(); | ||
} | ||
|
||
/** | ||
* @return total time taken to rewrite all queries in this concurrent query profiler | ||
*/ | ||
@Override | ||
public long getRewriteTime() { | ||
long totalRewriteTime = 0L; | ||
List<Timer> rewriteTimers = new LinkedList<>(); | ||
threadToRewriteTimers.values().forEach(rewriteTimers::addAll); | ||
LinkedList<long[]> mergedIntervals = mergeRewriteTimeIntervals(rewriteTimers); | ||
for (long[] interval : mergedIntervals) { | ||
totalRewriteTime += interval[1] - interval[0]; | ||
} | ||
return totalRewriteTime; | ||
} | ||
|
||
// package private for unit testing | ||
LinkedList<long[]> mergeRewriteTimeIntervals(List<Timer> timers) { | ||
LinkedList<long[]> mergedIntervals = new LinkedList<>(); | ||
timers.sort(Comparator.comparingLong(Timer::getEarliestTimerStartTime)); | ||
for (Timer timer : timers) { | ||
long startTime = timer.getEarliestTimerStartTime(); | ||
long endTime = timer.getEarliestTimerStartTime() + timer.getApproximateTiming(); | ||
if (mergedIntervals.isEmpty() || mergedIntervals.getLast()[1] < timer.getEarliestTimerStartTime()) { | ||
long[] interval = new long[2]; | ||
interval[0] = startTime; | ||
interval[1] = endTime; | ||
mergedIntervals.add(interval); | ||
} else { | ||
mergedIntervals.getLast()[1] = Math.max(mergedIntervals.getLast()[1], endTime); | ||
} | ||
} | ||
return mergedIntervals; | ||
} | ||
|
||
private long getCurrentThreadId() { | ||
return Thread.currentThread().getId(); | ||
} | ||
} |
Oops, something went wrong.