Skip to content

Commit cfa2883

Browse files
authored
fix: Division by zero when all clusters of a group are not ready (#47)
* fix: Division by zero when all clusters of a group are not ready * udapte docs * update docs * changelog * Use match instead of if
1 parent e8a6938 commit cfa2883

4 files changed

Lines changed: 51 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@ All notable changes to this project will be documented in this file.
44

55
## [Unreleased]
66

7+
## [0.3.2] - 2024-08-20
8+
79
### Changed
810

911
- Don't use the `aws-lc-rs` crate (introduced in [#45]), as it broke the Tilt build ([#46]).
1012

13+
### Fixed
14+
15+
- Fix division by zero when all clusters of a cluster group are not ready to accept queries ([#47]).
16+
1117
[#46]: https://github.com/stackabletech/trino-lb/pull/46
18+
[#47]: https://github.com/stackabletech/trino-lb/pull/47
1219

1320
## [0.3.1] - 2024-08-16
1421

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Make sure you clone this repo and run the following command from the root direct
2424
In case you don't have any Trino cluster at hand you can start trino-lb without any Trino cluster as follows:
2525

2626
```bash
27-
docker run -p 8080:8080 -v ./example-configs/simple-no-trino.yaml:/etc/trino-lb-config.yaml --rm oci.stackable.tech/stackable/trino-lb:0.1.0
27+
docker run -p 8080:8080 -v ./example-configs/simple-no-trino.yaml:/etc/trino-lb-config.yaml --rm oci.stackable.tech/stackable/trino-lb:0.3.1
2828
```
2929

3030
This starts trino-lb listening on http://127.0.0.1:8080.
@@ -44,12 +44,12 @@ As you can see by `QUEUED_IN_TRINO_LB`, the query is queued in trino-lb indefini
4444
4545
Let's assume your Trino cluster is accessible at `https://127.0.0.1:8443` using the username `admin` and password `admin`.
4646
47-
First check the config file `example-configs/simple-single-trino.yaml` and swap out the hostname and credentials corresponding to your Trino cluster.
47+
First check the config file `example-configs/docker-simple-single-trino.yaml` and swap out the hostname and credentials corresponding to your Trino cluster.
4848
Afterwards start trino-lb using the following command.
4949
We are using self-signed certificates for testing purpose here.
5050
5151
```bash
52-
docker run -p 443:8443 -v ./example-configs/simple-single-trino.yaml:/etc/trino-lb-config.yaml -v ./example-configs/self-signed-certs/:/self-signed-certs/ --rm oci.stackable.tech/stackable/trino-lb:0.1.0
52+
docker run -p 443:8443 -v ./example-configs/docker-simple-single-trino.yaml:/etc/trino-lb-config.yaml -v ./example-configs/self-signed-certs/:/self-signed-certs/ --rm oci.stackable.tech/stackable/trino-lb:0.3.1
5353
```
5454
5555
> [!NOTE]
@@ -58,7 +58,7 @@ docker run -p 443:8443 -v ./example-configs/simple-single-trino.yaml:/etc/trino-
5858
We can send queries to trino-lb and it will forward them to the configured Trino cluster.
5959
6060
```bash
61-
java -jar ~/Downloads/trino-cli-435-executable.jar --server https://127.0.0.1:443 --insecure --user admin --password
61+
java -jar ~/Downloads/trino-cli-*-executable.jar --server https://127.0.0.1:443 --insecure --user admin --password
6262
Password:
6363
trino> select 42;
6464
_col0
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
trinoLb:
2+
externalAddress: https://127.0.0.1:443 # exposed docker port is 443
3+
# When you enable authentication trino-clients enforce https encryption
4+
tls:
5+
enabled: true
6+
certPemFile: /self-signed-certs/cert.pem
7+
keyPemFile: /self-signed-certs/key.pem
8+
# Use in-memory persistence which will loose all queued running queries on restart
9+
persistence:
10+
inMemory: {}
11+
trinoClusterGroups:
12+
default:
13+
# Once a cluster has more running queries than this no further queries will be send to it
14+
# They will be queued in trino-lb instead
15+
maxRunningQueries: 1
16+
trinoClusters:
17+
- name: trino-default-1
18+
endpoint: https://5.250.181.98:8443 # FIXME
19+
credentials:
20+
username: admin
21+
password: adminadmin # FIXME
22+
# Your Trino probably does not have a globally trusted certificate
23+
trinoClusterGroupsIgnoreCert: true
24+
25+
# Route all queries to the "default" cluster group
26+
routers: []
27+
routingFallback: default

trino-lb/src/scaling/mod.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ impl Scaler {
317317
target_states.insert(to_start.name.to_owned(), ClusterState::Starting);
318318
}
319319
}
320-
} else {
320+
} else if queued == 0 {
321321
// Determine excess clusters, this only makes sense when we don't upscale
322322
let cluster_query_counters = try_join_all(
323323
clusters
@@ -335,7 +335,18 @@ impl Scaler {
335335
.map(|(c, _)| c.max_running_queries)
336336
.sum();
337337
let current_running_queries: u64 = cluster_query_counters.iter().sum();
338-
let utilization_percent = 100 * current_running_queries / max_running_queries;
338+
339+
let utilization_percent = match (max_running_queries, current_running_queries) {
340+
// No cluster is ready to accept queries and no queries running
341+
(0, 0) => 0,
342+
// No cluster is ready to accept queries but there are some queries still running
343+
// This means the clusters are even more utilized than they normally should have (although they e.g. can
344+
// have some queries running during draining)
345+
// So we set the utilization to 100%
346+
(0, _) => 100,
347+
// We can calculate the percentage normally, it's safe to divide by max_running_queries
348+
(_, _) => 100 * current_running_queries / max_running_queries,
349+
};
339350

340351
debug!(
341352
current_running_queries,

0 commit comments

Comments
 (0)