Skip to content

Commit 045a3d1

Browse files
fix: Only delete queries from persistence if the query is finished (#98)
* test: Improve assertion * fix: Only delete queries from persistence if the query is finished * clippy * Update CONTRIBUTING.md Co-authored-by: Malte Sander <malte.sander.it@gmail.com> * Update CONTRIBUTING.md Co-authored-by: Malte Sander <malte.sander.it@gmail.com> --------- Co-authored-by: Malte Sander <malte.sander.it@gmail.com>
1 parent da09f30 commit 045a3d1

7 files changed

Lines changed: 78 additions & 25 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ All notable changes to this project will be documented in this file.
2222
### Fixed
2323

2424
- Set connection and response timeout for Redis connections ([#85]).
25+
- Only remove queries from the persistence in case they don't send a `nextUri` and are in state `FINISHED` ([#98]).
2526

2627
[#68]: https://github.com/stackabletech/trino-lb/pull/68
2728
[#85]: https://github.com/stackabletech/trino-lb/pull/85
2829
[#86]: https://github.com/stackabletech/trino-lb/pull/86
2930
[#91]: https://github.com/stackabletech/trino-lb/pull/91
3031
[#95]: https://github.com/stackabletech/trino-lb/pull/95
32+
[#98]: https://github.com/stackabletech/trino-lb/pull/98
3133

3234
## [0.5.0] - 2025-03-14
3335

CONTRIBUTING.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,30 @@ Last but not least run
2121
```bash
2222
./scripts/run_tests.sh
2323
```
24+
25+
## mitm proxy debugging
26+
27+
First, port-forward trino-lb to localhost:
28+
29+
```bash
30+
kubectl -n <namespace> port-forward svc/trino-lb 8443:8443
31+
```
32+
33+
Please make sure that `trinoLb.externalAddress` in your trino-lb config points to `https://127.0.0.1:8443`, so that it populates the nextUri correctly.
34+
35+
Then start `mitmproxy`:
36+
37+
```bash
38+
# nix-shell -p mitmproxy
39+
mitmproxy --listen-port 8080 --ssl-insecure
40+
```
41+
42+
Afterwards connect with trino-cli:
43+
44+
```bash
45+
~/Downloads/trino-cli-478 --server https://127.0.0.1:8443 --insecure --user admin --password --http-proxy=127.0.0.1:8080
46+
47+
export TRINO_USER="admin"
48+
export TRINO_PASSWORD="adminadmin"
49+
echo 'SELECT * FROM tpch.sf100.customer' | ~/Downloads/trino-cli-478 --server https://127.0.0.1:8443 --insecure --password --http-proxy 127.0.0.1:8080
50+
```

tests/templates/kuttl/client-spooling/10-install-trino.yaml.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ spec:
5151
min: 250m
5252
max: "1"
5353
memory:
54-
limit: 3Gi
54+
limit: 5Gi
5555
gracefulShutdownTimeout: 2m # Let the test run faster
5656
logging:
5757
enableVectorAgent: {{ lookup('env', 'VECTOR_AGGREGATOR') | length > 0 }}

tests/templates/kuttl/client-spooling/30-assert.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
apiVersion: kuttl.dev/v1beta1
33
kind: TestAssert
4-
timeout: 600
4+
timeout: 1200
55
---
66
apiVersion: batch/v1
77
kind: Job

tests/templates/kuttl/client-spooling/30-test-queries.yaml.j2

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,26 @@ spec:
5353
echo "Submitting queries with big result set to trigger client spooling"
5454
# Test big query result, so client spooling is used
5555
BIG_RESULT_QUERY="SELECT * FROM tpch.sf100.customer"
56+
EXPECTED_ROWS=15000000
57+
5658
for COORDINATOR in "${COORDINATORS[@]}"; do
5759
echo "Running query with big result set against $COORDINATOR"
58-
echo "$BIG_RESULT_QUERY" | java -jar trino-cli-executable.jar --server $COORDINATOR --insecure --user $TRINO_USER --password > /dev/null
60+
ROW_COUNT=$(
61+
echo "$BIG_RESULT_QUERY" | \
62+
java -jar trino-cli-executable.jar \
63+
--server "$COORDINATOR" \
64+
--insecure \
65+
--user "$TRINO_USER" \
66+
--password \
67+
| wc -l
68+
)
69+
70+
if [ "$ROW_COUNT" -ne "$EXPECTED_ROWS" ]; then
71+
echo "❌ Assertion failed on $COORDINATOR: expected $EXPECTED_ROWS rows, got $ROW_COUNT"
72+
exit 1
73+
else
74+
echo "✅ Assertion passed on $COORDINATOR: $ROW_COUNT rows"
75+
fi
5976
done
6077

6178
echo "All queries completed successfully."

trino-lb-core/src/trino_api.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,23 @@ pub struct Stat {
8686
}
8787

8888
impl TrinoQueryApiResponse {
89+
pub fn is_query_finished(&self) -> bool {
90+
self.stats.state == "FINISHED"
91+
}
92+
93+
#[instrument(
94+
skip(self),
95+
fields(trino_lb_addr = %trino_lb_addr),
96+
)]
97+
pub fn change_next_uri_to_trino_lb(&mut self, trino_lb_addr: &Url) -> Result<(), Error> {
98+
if let Some(next_uri) = &self.next_uri {
99+
let next_uri = Url::parse(next_uri).context(ParseNextUriFromTrinoSnafu)?;
100+
self.next_uri = Some(change_next_uri_to_trino_lb(&next_uri, trino_lb_addr).to_string());
101+
}
102+
103+
Ok(())
104+
}
105+
89106
#[instrument(
90107
skip(query),
91108
fields(trino_lb_addr = %trino_lb_addr),
@@ -155,19 +172,6 @@ impl TrinoQueryApiResponse {
155172
update_count: None,
156173
})
157174
}
158-
159-
#[instrument(
160-
skip(self),
161-
fields(trino_lb_addr = %trino_lb_addr),
162-
)]
163-
pub fn change_next_uri_to_trino_lb(&mut self, trino_lb_addr: &Url) -> Result<(), Error> {
164-
if let Some(next_uri) = &self.next_uri {
165-
let next_uri = Url::parse(next_uri).context(ParseNextUriFromTrinoSnafu)?;
166-
self.next_uri = Some(change_next_uri_to_trino_lb(&next_uri, trino_lb_addr).to_string());
167-
}
168-
169-
Ok(())
170-
}
171175
}
172176

173177
fn change_next_uri_to_trino_lb(next_uri: &Url, trino_lb_addr: &Url) -> Url {

trino-lb/src/http_server/v1/statement.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)