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

Conversation

dominikzalewski
Copy link
Member

@dominikzalewski dominikzalewski commented Dec 27, 2023

Description

For really bad queries, the ones that do not even parse to a proper SQL, we have the following sequence of calls:

  • EventListener.queryCompleted()
  • query is added to the QueryTracker

To work around incorrect sequence of these calls, an EventListener will typically use a retry mechanism combined with switching to a separate thread.

This PR inverts the sequence of calls so that EventListeners can now be coded single-threaded.

Additional context and related issues

Release notes

( X ) This is not user-visible or is docs only, and no release notes are required.
( ) Release notes are required. Please propose a release note for me.
( ) Release notes are required, with the following suggested text:

@@ -22,6 +22,7 @@
import io.opentelemetry.api.trace.Tracer;
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.

@@ -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

@dominikzalewski dominikzalewski force-pushed the query_completed_too_early branch from 54cc3a6 to bc8163a Compare January 2, 2024 12:39
@dominikzalewski dominikzalewski changed the title Bugfix: sometimes EventListener.queryCompleted() called too early Sometimes EventListener.queryCompleted() called too early Jan 2, 2024
@@ -236,6 +241,7 @@ 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);
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?

For happy path and parsable queries, we have the following order of calls:
- query is added to the QueryTracker
- EventListener.queryCompleted() is called

For really bad queries, the ones that do not even parse to a proper SQL, the order is inverted.
As a result, when implementing an EventListener that wants to access query detailed information
we have to use a workaround for this edge case and artificially invert the sequence of calls.
EventListener will typically use a retry mechanism combined with switching to a separate thread,
which adds unnecessary complexity and multi-threading to a code that otherwise would be
single-threaded.
@dominikzalewski dominikzalewski force-pushed the query_completed_too_early branch from bc8163a to 7b8bafb Compare January 2, 2024 17:11
@losipiuk losipiuk merged commit 8aab363 into trinodb:master Jan 3, 2024
88 checks passed
@github-actions github-actions bot added this to the 436 milestone Jan 3, 2024
@dominikzalewski dominikzalewski deleted the query_completed_too_early branch January 3, 2024 10:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants