-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
QueryPerformanceRecorder: Hold Batches With Care
- Loading branch information
1 parent
f8eedf3
commit d467857
Showing
9 changed files
with
389 additions
and
85 deletions.
There are no files selected for viewing
186 changes: 186 additions & 0 deletions
186
...able/src/main/java/io/deephaven/engine/table/impl/perf/BatchQueryPerformanceRecorder.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,186 @@ | ||
package io.deephaven.engine.table.impl.perf; | ||
|
||
import io.deephaven.base.verify.Assert; | ||
import io.deephaven.engine.exceptions.CancellationException; | ||
import io.deephaven.engine.table.Table; | ||
import io.deephaven.util.QueryConstants; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.List; | ||
|
||
public class BatchQueryPerformanceRecorder extends QueryPerformanceRecorder { | ||
private final QueryPerformanceRecorder batchRecorder; | ||
|
||
public BatchQueryPerformanceRecorder(@NotNull final QueryPerformanceRecorder batchRecorder) { | ||
this.batchRecorder = batchRecorder; | ||
this.operationNuggets = null; // force NPE if accessed | ||
} | ||
|
||
@Override | ||
public synchronized QueryPerformanceNugget getNugget(String name, long inputSize) { | ||
synchronized (batchRecorder) { | ||
if (batchRecorder.getState() != QueryState.RUNNING) { | ||
return QueryPerformanceNugget.DUMMY_NUGGET; | ||
} | ||
if (Thread.interrupted()) { | ||
throw new CancellationException("interrupted in QueryPerformanceNugget"); | ||
} | ||
if (catchAllNugget != null) { | ||
stopCatchAll(false); | ||
} | ||
|
||
final int parentOperationNumber = | ||
userNuggetStack.size() > 0 ? userNuggetStack.getLast().getOperationNumber() | ||
: QueryConstants.NULL_INT; | ||
|
||
final QueryPerformanceNugget nugget = new QueryPerformanceNugget( | ||
getEvaluationNumber(), batchRecorder.operationNuggets.size(), parentOperationNumber, | ||
userNuggetStack.size(), name, true, inputSize); | ||
batchRecorder.operationNuggets.add(nugget); | ||
userNuggetStack.addLast(nugget); | ||
return nugget; | ||
} | ||
} | ||
|
||
@Override | ||
synchronized boolean releaseNugget(QueryPerformanceNugget nugget) { | ||
boolean shouldLog = nugget.shouldLogNugget(nugget == catchAllNugget); | ||
if (!nugget.isUser()) { | ||
return shouldLog; | ||
} | ||
|
||
final QueryPerformanceNugget removed = userNuggetStack.removeLast(); | ||
if (nugget != removed) { | ||
throw new IllegalStateException( | ||
"Released query performance nugget " + nugget + " (" + System.identityHashCode(nugget) + | ||
") didn't match the top of the user nugget stack " + removed + " (" | ||
+ System.identityHashCode(removed) + | ||
") - did you follow the correct try/finally pattern?"); | ||
} | ||
|
||
if (removed.shouldLogMeAndStackParents()) { | ||
shouldLog = true; | ||
if (userNuggetStack.size() > 0) { | ||
userNuggetStack.getLast().setShouldLogMeAndStackParents(); | ||
} | ||
} | ||
|
||
synchronized (batchRecorder) { | ||
if (userNuggetStack.isEmpty() && batchRecorder.getState() == QueryState.RUNNING) { | ||
startCatchAll(getEvaluationNumber()); | ||
} | ||
} | ||
|
||
return shouldLog; | ||
} | ||
|
||
@Override | ||
public void setQueryData(EntrySetter setter) { | ||
final int evaluationNumber; | ||
final int operationNumber; | ||
boolean uninstrumented = false; | ||
|
||
synchronized (this) { | ||
synchronized (batchRecorder) { | ||
evaluationNumber = getEvaluationNumber(); | ||
|
||
if (batchRecorder.getState() != QueryState.RUNNING) { | ||
operationNumber = QueryConstants.NULL_INT; | ||
} else { | ||
// ensure UPL and QOPL are consistent/joinable. | ||
if (userNuggetStack.size() > 0) { | ||
final QueryPerformanceNugget current = userNuggetStack.getLast(); | ||
operationNumber = current.getOperationNumber(); | ||
current.setShouldLogMeAndStackParents(); | ||
} else { | ||
uninstrumented = true; | ||
operationNumber = QueryConstants.NULL_INT; | ||
if (catchAllNugget != null) { | ||
catchAllNugget.setShouldLogMeAndStackParents(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
setter.set(evaluationNumber, operationNumber, uninstrumented); | ||
} | ||
|
||
@Override | ||
void stopCatchAll(boolean abort) { | ||
final boolean shouldLog; | ||
if (abort) { | ||
shouldLog = catchAllNugget.abort(this); | ||
} else { | ||
shouldLog = catchAllNugget.done(this); | ||
} | ||
if (shouldLog) { | ||
synchronized (batchRecorder) { | ||
catchAllNugget.setOperationNumber(batchRecorder.operationNuggets.size()); | ||
batchRecorder.operationNuggets.add(catchAllNugget); | ||
} | ||
} | ||
catchAllNugget = null; | ||
} | ||
|
||
@Override | ||
public int startQuery(String description) { | ||
clear(); | ||
int evaluationNumber = batchRecorder.getEvaluationNumber(); | ||
startCatchAll(evaluationNumber); | ||
return evaluationNumber; | ||
} | ||
|
||
@Override | ||
public synchronized boolean endQuery() { | ||
synchronized (batchRecorder) { | ||
if (batchRecorder.getState() != QueryState.RUNNING) { | ||
return false; | ||
} | ||
|
||
Assert.neqNull(catchAllNugget, "catchAllNugget"); | ||
stopCatchAll(false); | ||
return true; | ||
} | ||
} | ||
|
||
@Override | ||
public int getEvaluationNumber() { | ||
return batchRecorder.getEvaluationNumber(); | ||
} | ||
|
||
@Override | ||
public synchronized QueryState getState() { | ||
return batchRecorder.getState(); | ||
} | ||
|
||
@Override | ||
public synchronized QueryPerformanceNugget getOuterNugget() { | ||
return userNuggetStack.peekLast(); | ||
} | ||
|
||
@Override | ||
public Table getTimingResultsAsTable() { | ||
return batchRecorder.getTimingResultsAsTable(); | ||
} | ||
|
||
@Override | ||
public QueryPerformanceNugget getQueryLevelPerformanceData() { | ||
return unsupported("getQueryLevelPerformanceData"); | ||
} | ||
|
||
@Override | ||
public List<QueryPerformanceNugget> getOperationLevelPerformanceData() { | ||
return unsupported("getOperationLevelPerformanceData"); | ||
} | ||
|
||
@Override | ||
public void abortQuery() { | ||
unsupported("abortQuery"); | ||
} | ||
|
||
|
||
private <T> T unsupported(final String methodName) { | ||
throw new UnsupportedOperationException("BatchQueryPerformanceRecorder does not support " + methodName); | ||
} | ||
} |
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
Oops, something went wrong.