Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix](Nereids) use Stopwatch as timeout checker #23383

Merged
merged 1 commit into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -115,7 +114,9 @@ public void plan(StatementBase queryStmt, org.apache.doris.thrift.TQueryOptions
NereidsTracer.logImportantTime("EndParsePlan");
setParsedPlan(parsedPlan);
PhysicalProperties requireProperties = buildInitRequireProperties();
statementContext.getStopwatch().start();
Plan resultPlan = plan(parsedPlan, requireProperties, explainLevel);
statementContext.getStopwatch().stop();
setOptimizedPlan(resultPlan);
if (explainLevel.isPlanLevel) {
return;
Expand Down Expand Up @@ -182,7 +183,6 @@ public Plan plan(LogicalPlan plan, PhysicalProperties requireProperties, Explain

try (Lock lock = new Lock(plan, cascadesContext)) {
// resolve column, table and function

Span queryAnalysisSpan =
statementContext.getConnectContext().getTracer()
.spanBuilder("query analysis").setParent(Context.current()).startSpan();
Expand Down Expand Up @@ -214,11 +214,6 @@ public Plan plan(LogicalPlan plan, PhysicalProperties requireProperties, Explain
}
}

Optional<ScheduledExecutorService> timeoutExecutor = Optional.empty();
if (statementContext.getConnectContext().getSessionVariable().enableNereidsTimeout) {
timeoutExecutor = Optional.of(runTimeoutExecutor());
}

// rule-based optimize
rewrite();
if (explainLevel == ExplainLevel.REWRITTEN_PLAN || explainLevel == ExplainLevel.ALL_PLAN) {
Expand Down Expand Up @@ -252,7 +247,6 @@ public Plan plan(LogicalPlan plan, PhysicalProperties requireProperties, Explain
// serialize optimized plan to dumpfile, dumpfile do not have this part means optimize failed
MinidumpUtils.serializeOutputToDumpFile(physicalPlan);
NereidsTracer.output(statementContext.getConnectContext());
timeoutExecutor.ifPresent(ExecutorService::shutdown);

return physicalPlan;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.qe.OriginStatement;

import com.google.common.base.Stopwatch;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableList;
Expand All @@ -54,6 +55,8 @@ public class StatementContext {

private ConnectContext connectContext;

private final Stopwatch stopwatch = Stopwatch.createUnstarted();

@GuardedBy("this")
private final Map<String, Supplier<Object>> contextCacheMap = Maps.newLinkedHashMap();

Expand Down Expand Up @@ -108,6 +111,10 @@ public OriginStatement getOriginStatement() {
return originStatement;
}

public Stopwatch getStopwatch() {
return stopwatch;
}

public void setMaxNAryInnerJoin(int maxNAryInnerJoin) {
if (maxNAryInnerJoin > this.maxNAryInnerJoin) {
this.maxNAryInnerJoin = maxNAryInnerJoin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.apache.doris.nereids.CascadesContext;
import org.apache.doris.nereids.jobs.Job;

import java.util.concurrent.TimeUnit;

/**
* Single thread, serial scheduler.
*/
Expand All @@ -29,7 +31,8 @@ public void executeJobPool(ScheduleContext scheduleContext) {
JobPool pool = scheduleContext.getJobPool();
while (!pool.isEmpty()) {
CascadesContext context = (CascadesContext) scheduleContext;
if (context.isTimeout()) {
if (context.getConnectContext().getSessionVariable().enableNereidsTimeout
&& context.getStatementContext().getStopwatch().elapsed(TimeUnit.MILLISECONDS) > 5000) {
throw new RuntimeException("Nereids cost too much time ( > 5s )");
}
Job job = pool.pop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.doris.nereids.rules.RuleType;
import org.apache.doris.nereids.trees.expressions.CTEId;
import org.apache.doris.nereids.trees.expressions.Slot;
import org.apache.doris.nereids.trees.expressions.StatementScopeIdGenerator;
import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.logical.LogicalCTE;
import org.apache.doris.nereids.trees.plans.logical.LogicalCTEAnchor;
Expand Down Expand Up @@ -89,7 +90,7 @@ private Pair<CTEContext, List<LogicalCTEProducer<Plan>>> analyzeCte(
innerCascadesCtx.newAnalyzer().analyze();
LogicalPlan analyzedCtePlan = (LogicalPlan) innerCascadesCtx.getRewritePlan();
checkColumnAlias(aliasQuery, analyzedCtePlan.getOutput());
CTEId cteId = cascadesContext.getStatementContext().getNextCTEId();
CTEId cteId = StatementScopeIdGenerator.newCTEId();
LogicalSubQueryAlias<Plan> logicalSubQueryAlias =
aliasQuery.withChildren(ImmutableList.of(analyzedCtePlan));
outerCteCtx = new CTEContext(cteId, logicalSubQueryAlias, outerCteCtx);
Expand Down