-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Handle long running queries in DirectTrinoClient #26371
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
Merged
kokosing
merged 1 commit into
trinodb:master
from
mwd410:mdeady/directTrinoClientHeartbeat
Aug 8, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
| import io.trino.dispatcher.DispatchQuery; | ||
| import io.trino.exchange.DirectExchangeInput; | ||
| import io.trino.execution.QueryManager; | ||
| import io.trino.execution.QueryManagerConfig; | ||
| import io.trino.execution.QueryState; | ||
| import io.trino.execution.buffer.PageDeserializer; | ||
| import io.trino.memory.context.SimpleLocalMemoryContext; | ||
|
|
@@ -40,6 +41,8 @@ | |
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.TimeoutException; | ||
|
|
||
| import static io.airlift.concurrent.MoreFutures.whenAnyComplete; | ||
| import static io.trino.SystemSessionProperties.getRetryPolicy; | ||
|
|
@@ -56,13 +59,20 @@ public class DirectTrinoClient | |
| private final QueryManager queryManager; | ||
| private final DirectExchangeClientSupplier directExchangeClientSupplier; | ||
| private final BlockEncodingSerde blockEncodingSerde; | ||
| private final long heartBeatIntervalMillis; | ||
|
|
||
| public DirectTrinoClient(DispatchManager dispatchManager, QueryManager queryManager, DirectExchangeClientSupplier directExchangeClientSupplier, BlockEncodingSerde blockEncodingSerde) | ||
| public DirectTrinoClient( | ||
| DispatchManager dispatchManager, | ||
| QueryManager queryManager, | ||
| QueryManagerConfig queryManagerConfig, | ||
| DirectExchangeClientSupplier directExchangeClientSupplier, | ||
| BlockEncodingSerde blockEncodingSerde) | ||
| { | ||
| this.dispatchManager = requireNonNull(dispatchManager, "dispatchManager is null"); | ||
| this.queryManager = requireNonNull(queryManager, "queryManager is null"); | ||
| this.directExchangeClientSupplier = requireNonNull(directExchangeClientSupplier, "directExchangeClientSupplier is null"); | ||
| this.blockEncodingSerde = requireNonNull(blockEncodingSerde, "blockEncodingSerde is null"); | ||
| this.heartBeatIntervalMillis = requireNonNull(queryManagerConfig, "queryManagerConfig is null").getClientTimeout().toMillis() / 2; | ||
| } | ||
|
|
||
| public DispatchQuery execute(SessionContext sessionContext, @Language("SQL") String sql, QueryResultsListener queryResultsListener) | ||
|
|
@@ -103,7 +113,27 @@ public DispatchQuery execute(SessionContext sessionContext, @Language("SQL") Str | |
| Page page = pageDeserializer.deserialize(serializedPage); | ||
| queryResultsListener.consumeOutputPage(page); | ||
| } | ||
| getQueryFuture(whenAnyComplete(ImmutableList.of(queryManager.getStateChange(queryId, state), exchangeClient.isBlocked()))); | ||
|
|
||
| ListenableFuture<Object> anyCompleteFuture = whenAnyComplete(ImmutableList.of( | ||
| queryManager.getStateChange(queryId, state), | ||
| exchangeClient.isBlocked())); | ||
| while (!anyCompleteFuture.isDone()) { | ||
| try { | ||
| anyCompleteFuture.get(heartBeatIntervalMillis, TimeUnit.MILLISECONDS); | ||
| } | ||
| catch (TimeoutException e) { | ||
| // continue waiting until the query state changes or the exchange client is blocked. | ||
| // we need to periodically record the heartbeat to prevent the query from being canceled | ||
| dispatchQuery.recordHeartbeat(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! I’ve seen abandoned queries before, especially when the cluster was under high pressure — it have been the actual cause back then |
||
| } | ||
| catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| throw new TrinoException(GENERIC_INTERNAL_ERROR, "Thread interrupted", e); | ||
| } | ||
| catch (ExecutionException e) { | ||
| throw new TrinoException(GENERIC_INTERNAL_ERROR, "Error processing query", e.getCause()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
64 changes: 64 additions & 0 deletions
64
testing/trino-tests/src/test/java/io/trino/client/direct/TestDirectTrinoClient.java
This file contains hidden or 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,64 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.trino.client.direct; | ||
|
|
||
| import com.google.common.collect.ImmutableMap; | ||
| import io.trino.plugin.blackhole.BlackHolePlugin; | ||
| import io.trino.testing.QueryRunner; | ||
| import io.trino.testing.StandaloneQueryRunner; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.TestInstance; | ||
| import org.junit.jupiter.api.Timeout; | ||
| import org.junit.jupiter.api.parallel.Execution; | ||
|
|
||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static io.trino.SessionTestUtils.TEST_SESSION; | ||
| import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; | ||
| import static org.junit.jupiter.api.parallel.ExecutionMode.CONCURRENT; | ||
|
|
||
| @TestInstance(PER_CLASS) | ||
| @Execution(CONCURRENT) | ||
| public class TestDirectTrinoClient | ||
| { | ||
| private QueryRunner queryRunner; | ||
|
|
||
| @BeforeAll | ||
| public void setup() | ||
| throws Exception | ||
| { | ||
| queryRunner = new StandaloneQueryRunner( | ||
| TEST_SESSION, | ||
| builder -> builder.overrideProperties(ImmutableMap.of( | ||
| "query.client.timeout", "1s"))); | ||
| queryRunner.installPlugin(new BlackHolePlugin()); | ||
| queryRunner.createCatalog("blackhole", "blackhole"); | ||
| queryRunner.execute("CREATE SCHEMA blackhole.test_schema"); | ||
| queryRunner.execute("CREATE TABLE blackhole.test_schema.slow_test_table (col1 VARCHAR, col2 INTEGER)" + | ||
| "WITH (" + | ||
| " split_count = 1, " + | ||
| " pages_per_split = 1, " + | ||
| " rows_per_page = 1, " + | ||
| " page_processing_delay = '3s'" + | ||
| ")"); | ||
| } | ||
|
|
||
| @Test | ||
mwd410 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @Timeout(value = 20, unit = TimeUnit.SECONDS) | ||
| public void testDirectTrinoClientLongQuery() | ||
| { | ||
| queryRunner.execute(TEST_SESSION, "SELECT * FROM blackhole.test_schema.slow_test_table"); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.