Skip to content

Commit ffbbe38

Browse files
committed
Split the Cascades Debugger interface around stats and symbols
This commit splits the com.apple.foundationdb.record.query.plan.cascades.debug.Debugger interface into two separate interfaces (StatsDebugger and SymbolDebugger) which both extend the base Debugger interface, in order to ensure that operations related to recording Debugger events (with the purpose of calculating stats based on the profiling of these events) make use of the StatsDebugger interface, and operations related to keeping a registry of all Symbols used within the planner for purposes of associating them with friendly names make use of the SymbolDebugger interface. In addition to splitting the Debugger interface, the State class is also split into two classes, one that keeps of the Debugger events and stats around them (EventState) and one that keeps track of the current Symbols seen during planning (SymbolTables).
1 parent afea9d8 commit ffbbe38

File tree

19 files changed

+475
-322
lines changed

19 files changed

+475
-322
lines changed

fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/query/plan/cascades/CascadesPlanner.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import com.apple.foundationdb.record.query.plan.cascades.debug.Debugger;
4242
import com.apple.foundationdb.record.query.plan.cascades.debug.Debugger.Location;
4343
import com.apple.foundationdb.record.query.plan.cascades.debug.RestartException;
44+
import com.apple.foundationdb.record.query.plan.cascades.debug.StatsDebugger;
4445
import com.apple.foundationdb.record.query.plan.cascades.explain.ExplainPlanVisitor;
4546
import com.apple.foundationdb.record.query.plan.cascades.explain.PlannerGraphVisitor;
4647
import com.apple.foundationdb.record.query.plan.cascades.expressions.RelationalExpression;
@@ -331,7 +332,7 @@ public QueryPlanResult planQuery(@Nonnull final RecordQuery query,
331332
.put(QueryPlanInfoKeys.MAX_TASK_QUEUE_SIZE, maxQueueSize)
332333
.put(QueryPlanInfoKeys.CONSTRAINTS, constraints)
333334
.put(QueryPlanInfoKeys.STATS_MAPS,
334-
Debugger.getDebuggerMaybe().flatMap(Debugger::getStatsMaps)
335+
StatsDebugger.getDebuggerMaybe().flatMap(StatsDebugger::getStatsMaps)
335336
.orElse(null))
336337
.build();
337338
return new QueryPlanResult(plan, info);
@@ -375,8 +376,8 @@ public QueryPlanResult planGraph(@Nonnull final Supplier<Reference> referenceSup
375376
QueryPlanInfo.newBuilder()
376377
.put(QueryPlanInfoKeys.CONSTRAINTS, constraints)
377378
.put(QueryPlanInfoKeys.STATS_MAPS,
378-
Debugger.getDebuggerMaybe()
379-
.flatMap(Debugger::getStatsMaps).orElse(null))
379+
StatsDebugger.getDebuggerMaybe().flatMap(StatsDebugger::getStatsMaps)
380+
.orElse(null))
380381
.build());
381382
} finally {
382383
Debugger.withDebugger(Debugger::onDone);
@@ -425,7 +426,7 @@ private void planPartial(@Nonnull final Supplier<Reference> referenceSupplier,
425426
}
426427
taskCount++;
427428

428-
Debugger.withDebugger(debugger -> debugger.onEvent(
429+
StatsDebugger.withDebugger(debugger -> debugger.onEvent(
429430
new Debugger.ExecutingTaskEvent(currentRoot, taskStack, Location.BEGIN,
430431
Objects.requireNonNull(taskStack.peek()))));
431432
Task nextTask = taskStack.pop();
@@ -434,7 +435,7 @@ private void planPartial(@Nonnull final Supplier<Reference> referenceSupplier,
434435
logger.trace(KeyValueLogMessage.of("executing task", "nextTask", nextTask.toString()));
435436
}
436437

437-
Debugger.withDebugger(debugger -> debugger.onEvent(nextTask.toTaskEvent(Location.BEGIN)));
438+
StatsDebugger.withDebugger(debugger -> debugger.onEvent(nextTask.toTaskEvent(Location.BEGIN)));
438439
try {
439440
nextTask.execute();
440441
Debugger.sanityCheck(() -> {
@@ -454,7 +455,7 @@ private void planPartial(@Nonnull final Supplier<Reference> referenceSupplier,
454455
});
455456

456457
} finally {
457-
Debugger.withDebugger(debugger -> debugger.onEvent(nextTask.toTaskEvent(Location.END)));
458+
StatsDebugger.withDebugger(debugger -> debugger.onEvent(nextTask.toTaskEvent(Location.END)));
458459
}
459460

460461
if (logger.isTraceEnabled()) {
@@ -469,7 +470,7 @@ private void planPartial(@Nonnull final Supplier<Reference> referenceSupplier,
469470
.addLogInfo(LogMessageKeys.TASK_QUEUE_SIZE, taskStack.size());
470471
}
471472
} finally {
472-
Debugger.withDebugger(debugger -> debugger.onEvent(
473+
StatsDebugger.withDebugger(debugger -> debugger.onEvent(
473474
new Debugger.ExecutingTaskEvent(currentRoot, taskStack, Location.END, nextTask)));
474475
}
475476
} catch (final RestartException restartException) {
@@ -1006,14 +1007,14 @@ public void execute() {
10061007
}
10071008
// we notify the debugger (if installed) that the transform task is succeeding and
10081009
// about begin and end of the rule call event
1009-
Debugger.withDebugger(debugger -> debugger.onEvent(toTaskEvent(Location.MATCH_PRE)));
1010-
Debugger.withDebugger(debugger ->
1010+
StatsDebugger.withDebugger(debugger -> debugger.onEvent(toTaskEvent(Location.MATCH_PRE)));
1011+
StatsDebugger.withDebugger(debugger ->
10111012
debugger.onEvent(new Debugger.TransformRuleCallEvent(plannerPhase, currentRoot,
10121013
taskStack, Location.BEGIN, group, getBindable(), rule, ruleCall)));
10131014
try {
10141015
executeRuleCall(ruleCall);
10151016
} finally {
1016-
Debugger.withDebugger(debugger ->
1017+
StatsDebugger.withDebugger(debugger ->
10171018
debugger.onEvent(new Debugger.TransformRuleCallEvent(plannerPhase, currentRoot,
10181019
taskStack, Location.END, group, getBindable(), rule, ruleCall)));
10191020
}
@@ -1033,21 +1034,21 @@ protected void executeRuleCall(@Nonnull CascadesRuleCall ruleCall) {
10331034
// Handle produced artifacts (through yield...() calls)
10341035
//
10351036
for (final PartialMatch newPartialMatch : ruleCall.getNewPartialMatches()) {
1036-
Debugger.withDebugger(debugger ->
1037+
StatsDebugger.withDebugger(debugger ->
10371038
debugger.onEvent(new Debugger.TransformRuleCallEvent(plannerPhase, currentRoot, taskStack,
10381039
Location.YIELD, group, getBindable(), rule, ruleCall)));
10391040
taskStack.push(new AdjustMatch(getPlannerPhase(), getGroup(), getExpression(), newPartialMatch));
10401041
}
10411042

10421043
for (final RelationalExpression newExpression : ruleCall.getNewFinalExpressions()) {
1043-
Debugger.withDebugger(debugger ->
1044+
StatsDebugger.withDebugger(debugger ->
10441045
debugger.onEvent(new Debugger.TransformRuleCallEvent(plannerPhase, currentRoot, taskStack,
10451046
Location.YIELD, group, getBindable(), rule, ruleCall)));
10461047
exploreExpressionAndOptimizeInputs(plannerPhase, getGroup(), newExpression, true);
10471048
}
10481049

10491050
for (final RelationalExpression newExpression : ruleCall.getNewExploratoryExpressions()) {
1050-
Debugger.withDebugger(debugger ->
1051+
StatsDebugger.withDebugger(debugger ->
10511052
debugger.onEvent(new Debugger.TransformRuleCallEvent(plannerPhase, currentRoot, taskStack,
10521053
Location.YIELD, group, getBindable(), rule, ruleCall)));
10531054
exploreExpression(plannerPhase, group, newExpression, true);

fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/query/plan/cascades/CascadesRuleCall.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.apple.foundationdb.record.query.plan.cascades.Quantifiers.AliasResolver;
2828
import com.apple.foundationdb.record.query.plan.cascades.debug.Debugger;
2929
import com.apple.foundationdb.record.query.plan.cascades.debug.Debugger.InsertIntoMemoEvent;
30+
import com.apple.foundationdb.record.query.plan.cascades.debug.StatsDebugger;
3031
import com.apple.foundationdb.record.query.plan.cascades.expressions.RelationalExpression;
3132
import com.apple.foundationdb.record.query.plan.cascades.matching.structure.PlannerBindings;
3233
import com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan;
@@ -185,7 +186,7 @@ public <T> void pushConstraint(@Nonnull final Reference reference,
185186
@Override
186187
public void emitEvent(@Nonnull final Debugger.Location location) {
187188
Verify.verify(location != Debugger.Location.BEGIN && location != Debugger.Location.END);
188-
Debugger.withDebugger(debugger ->
189+
StatsDebugger.withDebugger(debugger ->
189190
debugger.onEvent(
190191
new Debugger.TransformRuleCallEvent(plannerPhase, root, taskStack, location, root,
191192
bindings.get(rule.getMatcher()), rule, this)));
@@ -295,7 +296,7 @@ public EvaluationContext getEvaluationContext() {
295296
@Nonnull
296297
private Reference addNewReference(@Nonnull final Reference newRef) {
297298
for (RelationalExpression expression : newRef.getAllMemberExpressions()) {
298-
Debugger.withDebugger(debugger -> debugger.onEvent(InsertIntoMemoEvent.newExp(expression)));
299+
StatsDebugger.withDebugger(debugger -> debugger.onEvent(InsertIntoMemoEvent.newExp(expression)));
299300
traversal.addExpression(newRef, expression);
300301
}
301302
newReferences.add(newRef);
@@ -352,7 +353,7 @@ public Reference memoizeExploratoryExpressions(@Nonnull final Collection<? exten
352353
// least one variation) or it will be a new reference, but that reference must be missing at least
353354
// one child from the first variation and therefore cannot be reused
354355
//
355-
Debugger.withDebugger(debugger -> debugger.onEvent(InsertIntoMemoEvent.begin()));
356+
StatsDebugger.withDebugger(debugger -> debugger.onEvent(InsertIntoMemoEvent.begin()));
356357
try {
357358
Preconditions.checkArgument(expressions.stream().noneMatch(expression -> expression instanceof RecordQueryPlan));
358359

@@ -412,7 +413,7 @@ public Reference memoizeExploratoryExpressions(@Nonnull final Collection<? exten
412413
if (!existingRefs.isEmpty()) {
413414
Reference existingReference = existingRefs.get(0);
414415
expressions.forEach(expr ->
415-
Debugger.withDebugger(debugger ->
416+
StatsDebugger.withDebugger(debugger ->
416417
debugger.onEvent(InsertIntoMemoEvent.reusedExpWithReferences(expr, existingRefs))));
417418
Verify.verify(existingReference != this.root);
418419
return existingReference;
@@ -421,7 +422,7 @@ public Reference memoizeExploratoryExpressions(@Nonnull final Collection<? exten
421422
// If we didn't find one, create a new reference and add it to the memo
422423
return addNewReference(Reference.ofExploratoryExpressions(plannerPhase.getTargetPlannerStage(), expressions));
423424
} finally {
424-
Debugger.withDebugger(debugger -> debugger.onEvent(InsertIntoMemoEvent.end()));
425+
StatsDebugger.withDebugger(debugger -> debugger.onEvent(InsertIntoMemoEvent.end()));
425426
}
426427
}
427428

@@ -436,7 +437,7 @@ private boolean isEligibleForReuse(@Nonnull Set<CorrelationIdentifier> requiredC
436437

437438
@Nonnull
438439
private Reference memoizeLeafExpressions(@Nonnull final Collection<? extends RelationalExpression> expressions) {
439-
Debugger.withDebugger(debugger -> debugger.onEvent(InsertIntoMemoEvent.begin()));
440+
StatsDebugger.withDebugger(debugger -> debugger.onEvent(InsertIntoMemoEvent.begin()));
440441
try {
441442
Preconditions.checkArgument(expressions.stream()
442443
.allMatch(expression -> !(expression instanceof RecordQueryPlan) && expression.getQuantifiers().isEmpty()));
@@ -450,15 +451,15 @@ private Reference memoizeLeafExpressions(@Nonnull final Collection<? extends Rel
450451
}
451452
if (leafRef.containsAllInMemo(expressions, AliasMap.emptyMap(), false)) {
452453
for (RelationalExpression expression : expressions) {
453-
Debugger.withDebugger(debugger -> debugger.onEvent(InsertIntoMemoEvent.reusedExp(expression)));
454+
StatsDebugger.withDebugger(debugger -> debugger.onEvent(InsertIntoMemoEvent.reusedExp(expression)));
454455
}
455456
return leafRef;
456457
}
457458
}
458459

459460
return addNewReference(Reference.ofExploratoryExpressions(plannerPhase.getTargetPlannerStage(), expressions));
460461
} finally {
461-
Debugger.withDebugger(debugger -> debugger.onEvent(InsertIntoMemoEvent.end()));
462+
StatsDebugger.withDebugger(debugger -> debugger.onEvent(InsertIntoMemoEvent.end()));
462463
}
463464
}
464465

@@ -534,14 +535,14 @@ private Reference memoizeExpressionsExactly(@Nonnull final Collection<? extends
534535
@Nonnull BiFunction<Set<? extends RelationalExpression>, Set<? extends RelationalExpression>, Reference> referenceCreator) {
535536
final var allExpressions =
536537
Iterables.concat(exploratoryExpressions, finalExpressions);
537-
Debugger.withDebugger(debugger -> allExpressions.forEach(
538+
StatsDebugger.withDebugger(debugger -> allExpressions.forEach(
538539
expression -> debugger.onEvent(InsertIntoMemoEvent.begin())));
539540
try {
540541
final var exploratoryExpressionSet = new LinkedIdentitySet<>(exploratoryExpressions);
541542
final var finalExpressionSet = new LinkedIdentitySet<>(finalExpressions);
542543
return addNewReference(referenceCreator.apply(exploratoryExpressionSet, finalExpressionSet));
543544
} finally {
544-
Debugger.withDebugger(debugger -> allExpressions.forEach(
545+
StatsDebugger.withDebugger(debugger -> allExpressions.forEach(
545546
expression -> debugger.onEvent(InsertIntoMemoEvent.end())));
546547
}
547548
}

fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/query/plan/cascades/CorrelationIdentifier.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import com.apple.foundationdb.annotation.API;
2424
import com.apple.foundationdb.record.query.plan.cascades.debug.Debugger;
25+
import com.apple.foundationdb.record.query.plan.cascades.debug.SymbolDebugger;
2526
import com.apple.foundationdb.record.util.ProtoUtils;
2627
import com.google.common.collect.ImmutableSet;
2728

@@ -88,10 +89,10 @@ public static CorrelationIdentifier uniqueID(@Nonnull final Class<?> clazz) {
8889
@Nonnull
8990
public static CorrelationIdentifier uniqueID(@Nonnull final Class<?> clazz, @Nonnull final String prefix) {
9091
final CorrelationIdentifier id =
91-
Debugger.getIndexOptional(clazz)
92+
SymbolDebugger.getIndexOptional(clazz)
9293
.map(i -> CorrelationIdentifier.of(prefix + i))
9394
.orElseGet(() -> new CorrelationIdentifier(ProtoUtils.uniqueName(prefix)));
94-
Debugger.updateIndex(clazz, i -> i + 1);
95+
SymbolDebugger.updateIndex(clazz, i -> i + 1);
9596
return id;
9697
}
9798

@@ -106,7 +107,7 @@ public static CorrelationIdentifier uniqueID(@Nonnull final Class<?> clazz, @Non
106107
*/
107108
@Nonnull
108109
public static CorrelationIdentifier uniqueSingletonID(@Nonnull final UUID singleton, @Nonnull final String prefix) {
109-
return Debugger.getOrRegisterSingleton(singleton)
110+
return SymbolDebugger.getOrRegisterSingleton(singleton)
110111
.map(index -> new CorrelationIdentifier(prefix + index))
111112
.orElseGet(() -> new CorrelationIdentifier(singleton.toString()));
112113
}

fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/query/plan/cascades/PrimaryAccessExpansionVisitor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import com.apple.foundationdb.annotation.SpotBugsSuppressWarnings;
2424
import com.apple.foundationdb.record.metadata.RecordType;
2525
import com.apple.foundationdb.record.metadata.expressions.KeyExpression;
26-
import com.apple.foundationdb.record.query.plan.cascades.debug.Debugger;
26+
import com.apple.foundationdb.record.query.plan.cascades.debug.SymbolDebugger;
2727
import com.apple.foundationdb.record.query.plan.cascades.expressions.MatchableSortExpression;
2828
import com.apple.foundationdb.record.query.plan.cascades.predicates.PredicateWithValueAndRanges;
2929
import com.google.common.collect.ImmutableList;
@@ -58,7 +58,7 @@ public PrimaryScanMatchCandidate expand(@Nonnull final Supplier<Quantifier.ForEa
5858
@Nullable final KeyExpression primaryKey,
5959
final boolean isReverse) {
6060
Objects.requireNonNull(primaryKey);
61-
Debugger.updateIndex(PredicateWithValueAndRanges.class, old -> 0);
61+
SymbolDebugger.updateIndex(PredicateWithValueAndRanges.class, old -> 0);
6262

6363
final var baseQuantifier = baseQuantifierSupplier.get();
6464

fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/query/plan/cascades/Quantifier.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import com.apple.foundationdb.record.PlanSerializable;
2626
import com.apple.foundationdb.record.PlanSerializationContext;
2727
import com.apple.foundationdb.record.planprotos.PPhysicalQuantifier;
28-
import com.apple.foundationdb.record.query.plan.cascades.debug.Debugger;
28+
import com.apple.foundationdb.record.query.plan.cascades.debug.SymbolDebugger;
2929
import com.apple.foundationdb.record.query.plan.cascades.expressions.RelationalExpression;
3030
import com.apple.foundationdb.record.query.plan.cascades.typing.Type;
3131
import com.apple.foundationdb.record.query.plan.cascades.typing.Type.Record.Field;
@@ -625,7 +625,7 @@ protected Quantifier(@Nonnull final CorrelationIdentifier alias) {
625625
this.flowedColumnsSupplier = Suppliers.memoize(this::computeFlowedColumns);
626626
this.flowedValuesSupplier = Suppliers.memoize(this::computeFlowedValues);
627627
// Call debugger hook for this new quantifier.
628-
Debugger.registerQuantifier(this);
628+
SymbolDebugger.registerQuantifier(this);
629629
}
630630

631631
@Nonnull

0 commit comments

Comments
 (0)