@@ -437,7 +437,7 @@ async fn handle_query_running_on_trino(
437437 . persistence
438438 . load_query ( & query_id)
439439 . await
440- . context ( StoreQueryInPersistenceSnafu {
440+ . context ( LoadQueryFromPersistenceSnafu {
441441 query_id : query_id. clone ( ) ,
442442 } ) ?;
443443
@@ -455,13 +455,11 @@ async fn handle_query_running_on_trino(
455455 . await
456456 . context ( AskTrinoForQueryStateSnafu ) ?;
457457
458- if trino_query_api_response. next_uri . is_some ( ) {
459- // Change the nextUri to actually point to trino-lb instead of Trino.
460- trino_query_api_response
461- . change_next_uri_to_trino_lb ( & state. config . trino_lb . external_address )
462- . context ( ModifyNextUriSnafu ) ?;
463- } else {
464- info ! ( %query_id, "Query completed (no next_uri send)" ) ;
458+ // Just to be safe the query needs to be completed and not contain any nextUri for the client
459+ // to call to, before being considered done. We don't expect any future calls to done queries,
460+ // so we can (hopefully) safely remove them from the persistence.
461+ if trino_query_api_response. is_query_finished ( ) && trino_query_api_response. next_uri . is_none ( ) {
462+ info ! ( %query_id, "Query completed, removing it from the persistence" ) ;
465463
466464 tokio:: try_join!(
467465 state. persistence. remove_query( & query_id) . map_err( |err| {
@@ -480,6 +478,11 @@ async fn handle_query_running_on_trino(
480478 }
481479 } ) ,
482480 ) ?;
481+ } else {
482+ // Change the nextUri to actually point to trino-lb instead of Trino.
483+ trino_query_api_response
484+ . change_next_uri_to_trino_lb ( & state. config . trino_lb . external_address )
485+ . context ( ModifyNextUriSnafu ) ?;
483486 }
484487
485488 Ok ( ( trino_headers, Json ( trino_query_api_response) ) )
@@ -582,7 +585,7 @@ async fn cancel_query_on_trino(
582585 . persistence
583586 . load_query ( & query_id)
584587 . await
585- . context ( StoreQueryInPersistenceSnafu {
588+ . context ( LoadQueryFromPersistenceSnafu {
586589 query_id : query_id. clone ( ) ,
587590 } ) ?;
588591
0 commit comments