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

Sometimes EventListener.queryCompleted() called too early #20231

Merged
merged 1 commit into from
Jan 3, 2024
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 @@ -22,6 +22,7 @@
import io.opentelemetry.api.trace.Tracer;
dominikzalewski marked this conversation as resolved.
Show resolved Hide resolved
import io.opentelemetry.context.Context;
import io.trino.Session;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please extend commit message, e.g: what was the problem?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is described in the PR description, but I now copied it to the commit extended message.

import io.trino.event.QueryMonitor;
import io.trino.execution.QueryIdGenerator;
import io.trino.execution.QueryInfo;
import io.trino.execution.QueryManagerConfig;
Expand Down Expand Up @@ -56,6 +57,7 @@
import static io.trino.execution.QueryState.RUNNING;
import static io.trino.spi.StandardErrorCode.QUERY_TEXT_TOO_LARGE;
import static io.trino.tracing.ScopedSpan.scopedSpan;
import static io.trino.util.Failures.toFailure;
import static io.trino.util.StatementUtils.getQueryType;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
Expand All @@ -80,6 +82,7 @@ public class DispatchManager
private final QueryTracker<DispatchQuery> queryTracker;

private final QueryManagerStats stats = new QueryManagerStats();
private final QueryMonitor queryMonitor;

@Inject
public DispatchManager(
Expand All @@ -94,7 +97,8 @@ public DispatchManager(
SessionPropertyManager sessionPropertyManager,
Tracer tracer,
QueryManagerConfig queryManagerConfig,
DispatchExecutor dispatchExecutor)
DispatchExecutor dispatchExecutor,
QueryMonitor queryMonitor)
{
this.queryIdGenerator = requireNonNull(queryIdGenerator, "queryIdGenerator is null");
this.queryPreparer = requireNonNull(queryPreparer, "queryPreparer is null");
Expand All @@ -112,6 +116,7 @@ public DispatchManager(
this.dispatchExecutor = dispatchExecutor.getExecutor();

this.queryTracker = new QueryTracker<>(queryManagerConfig, dispatchExecutor.getScheduledExecutor());
this.queryMonitor = requireNonNull(queryMonitor, "queryMonitor is null");
}

@PostConstruct
Expand Down Expand Up @@ -236,6 +241,11 @@ private <C> void createQueryInternal(QueryId queryId, Span querySpan, Slug slug,
Optional<String> preparedSql = Optional.ofNullable(preparedQuery).flatMap(PreparedQuery::getPrepareSql);
DispatchQuery failedDispatchQuery = failedDispatchQueryFactory.createFailedDispatchQuery(session, query, preparedSql, Optional.empty(), throwable);
queryCreated(failedDispatchQuery);
// maintain proper order of calls such that EventListener has access to QueryInfo
// - add query to tracker
// - fire query created event
// - fire query completed event
queryMonitor.queryImmediateFailureEvent(failedDispatchQuery.getBasicQueryInfo(), toFailure(throwable));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but add a comment whgy this is not happening within createFailedDispatchQuery for future readers.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment added. Let me know if this is what you meant and whether I can do anything else to get an approval?

querySpan.setStatus(StatusCode.ERROR, throwable.getMessage())
.recordException(throwable)
.end();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.Optional;
import java.util.concurrent.ExecutorService;

import static io.trino.util.Failures.toFailure;
import static java.util.Objects.requireNonNull;

public class FailedDispatchQueryFactory
Expand Down Expand Up @@ -58,7 +57,6 @@ public FailedDispatchQuery createFailedDispatchQuery(Session session, String que
BasicQueryInfo queryInfo = failedDispatchQuery.getBasicQueryInfo();

queryMonitor.queryCreatedEvent(queryInfo);
queryMonitor.queryImmediateFailureEvent(queryInfo, toFailure(throwable));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why having it here was causing race condition? queryCreatedEvent is called just before anyway.

Why query has to be registered via queryTracker.addQuery?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have provided relevant information in the extended commit message, like you asked in the other comment. Let me know whether it's clear now. If not, I will try again to describe it better.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding QueryTracker as an arg to createFailedDispatchQuery. This way you don't have to add queryMonitor to DispatchManager


return failedDispatchQuery;
}
Expand Down
Loading