Skip to content

Commit b73c305

Browse files
committed
added valkey dependency as a distributed cach
1 parent f88ba97 commit b73c305

23 files changed

+1432
-14
lines changed

docs/config.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,11 @@ dataStore:
1111

1212
clusterStatsConfiguration:
1313
monitorType: INFO_API
14+
15+
# Valkey distributed cache (optional - for multi-instance deployments)
16+
valkeyConfiguration:
17+
enabled: false
18+
host: localhost
19+
port: 6379
20+
# password: ${VALKEY_PASSWORD} # Uncomment if Valkey requires AUTH
21+
# cacheTtlSeconds: 1800 # Cache TTL in seconds (default: 1800 = 30 minutes)

docs/installation.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,37 @@ Find more information in the [routing rules documentation](routing-rules.md).
157157
To configure the logging level for various classes, specify the path to the
158158
`log.properties` file by setting `log.levels-file` in `serverConfig`.
159159

160-
For additional configurations, use the `log.*` properties from the
160+
For additional configurations, use the `log.*` properties from the
161161
[Trino logging properties documentation](https://trino.io/docs/current/admin/properties-logging.html) and specify
162162
the properties in `serverConfig`.
163163

164+
### Configure distributed cache (optional)
165+
166+
For multi-instance deployments, Trino Gateway supports distributed caching
167+
using Valkey (or Redis) to share query metadata across gateway instances.
168+
This improves query routing and enables horizontal scaling.
169+
170+
For single gateway deployments, distributed caching is not needed - the
171+
local cache is sufficient.
172+
173+
```yaml
174+
valkeyConfiguration:
175+
enabled: true
176+
host: valkey.internal.prod
177+
port: 6379
178+
password: ${ENV:VALKEY_PASSWORD}
179+
cacheTtlSeconds: 1800 # Cache TTL (default: 1800 = 30 minutes)
180+
```
181+
182+
Optional parameters: You can customize `cacheTtlSeconds` based on your query duration:
183+
184+
- Short queries (< 5 min): 600 seconds (10 minutes)
185+
- Default queries: 1800 seconds (30 minutes)
186+
- Long-running queries: 3600 seconds (1 hour)
187+
188+
See Valkey distributed cache configuration for detailed configuration options,
189+
deployment scenarios, and performance tuning.
190+
164191
### Proxying additional paths
165192

166193
By default, Trino Gateway only proxies requests to paths starting with

gateway-ha/config.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
serverConfig:
22
node.environment: test
3-
http-server.http.port: 8080
3+
http-server.http.port: 9080
44
tracing.enabled: true
55
otel.exporter.endpoint: http://localhost:4318/v1/traces
66
otel.exporter.protocol: http/protobuf
@@ -23,3 +23,11 @@ clusterStatsConfiguration:
2323
monitor:
2424
taskDelay: 1m
2525
clusterMetricsRegistryRefreshPeriod: 30s
26+
27+
# Valkey distributed cache (optional - for multi-instance deployments)
28+
valkeyConfiguration:
29+
enabled: false # Set to true to enable distributed caching
30+
host: localhost
31+
port: 6379
32+
# password: ${VALKEY_PASSWORD} # Uncomment if Valkey requires AUTH
33+
# cacheTtlSeconds: 1800 # Cache TTL in seconds (default: 1800 = 30 minutes)

gateway-ha/gateway.log

Lines changed: 427 additions & 0 deletions
Large diffs are not rendered by default.

gateway-ha/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@
189189
<version>${dep.trino.version}</version>
190190
</dependency>
191191

192+
<dependency>
193+
<groupId>io.valkey</groupId>
194+
<artifactId>valkey-java</artifactId>
195+
<version>5.5.0</version>
196+
</dependency>
197+
192198
<dependency>
193199
<groupId>jakarta.annotation</groupId>
194200
<artifactId>jakarta.annotation-api</artifactId>

gateway-ha/src/main/java/io/trino/gateway/ha/config/HaGatewayConfiguration.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public class HaGatewayConfiguration
4747

4848
private UIConfiguration uiConfiguration = new UIConfiguration();
4949

50+
private ValkeyConfiguration valkeyConfiguration = new ValkeyConfiguration();
51+
5052
// List of Modules with FQCN (Fully Qualified Class Name)
5153
private List<String> modules;
5254

@@ -215,6 +217,16 @@ public void setUiConfiguration(UIConfiguration uiConfiguration)
215217
this.uiConfiguration = uiConfiguration;
216218
}
217219

220+
public ValkeyConfiguration getValkeyConfiguration()
221+
{
222+
return valkeyConfiguration;
223+
}
224+
225+
public void setValkeyConfiguration(ValkeyConfiguration valkeyConfiguration)
226+
{
227+
this.valkeyConfiguration = valkeyConfiguration;
228+
}
229+
218230
public List<String> getModules()
219231
{
220232
return this.modules;
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package io.trino.gateway.ha.config;
15+
16+
public class ValkeyConfiguration
17+
{
18+
private boolean enabled;
19+
private String host = "localhost";
20+
private int port = 6379;
21+
private String password;
22+
private int database;
23+
private int maxTotal = 20;
24+
private int maxIdle = 10;
25+
private int minIdle = 5;
26+
private int timeoutMs = 2000;
27+
private long cacheTtlSeconds = 1800;
28+
29+
public boolean isEnabled()
30+
{
31+
return enabled;
32+
}
33+
34+
public void setEnabled(boolean enabled)
35+
{
36+
this.enabled = enabled;
37+
}
38+
39+
public String getHost()
40+
{
41+
return host;
42+
}
43+
44+
public void setHost(String host)
45+
{
46+
this.host = host;
47+
}
48+
49+
public int getPort()
50+
{
51+
return port;
52+
}
53+
54+
public void setPort(int port)
55+
{
56+
this.port = port;
57+
}
58+
59+
public String getPassword()
60+
{
61+
return password;
62+
}
63+
64+
public void setPassword(String password)
65+
{
66+
this.password = password;
67+
}
68+
69+
public int getDatabase()
70+
{
71+
return database;
72+
}
73+
74+
public void setDatabase(int database)
75+
{
76+
this.database = database;
77+
}
78+
79+
public int getMaxTotal()
80+
{
81+
return maxTotal;
82+
}
83+
84+
public void setMaxTotal(int maxTotal)
85+
{
86+
this.maxTotal = maxTotal;
87+
}
88+
89+
public int getMaxIdle()
90+
{
91+
return maxIdle;
92+
}
93+
94+
public void setMaxIdle(int maxIdle)
95+
{
96+
this.maxIdle = maxIdle;
97+
}
98+
99+
public int getMinIdle()
100+
{
101+
return minIdle;
102+
}
103+
104+
public void setMinIdle(int minIdle)
105+
{
106+
this.minIdle = minIdle;
107+
}
108+
109+
public int getTimeoutMs()
110+
{
111+
return timeoutMs;
112+
}
113+
114+
public void setTimeoutMs(int timeoutMs)
115+
{
116+
this.timeoutMs = timeoutMs;
117+
}
118+
119+
public long getCacheTtlSeconds()
120+
{
121+
return cacheTtlSeconds;
122+
}
123+
124+
public void setCacheTtlSeconds(long cacheTtlSeconds)
125+
{
126+
this.cacheTtlSeconds = cacheTtlSeconds;
127+
}
128+
}

gateway-ha/src/main/java/io/trino/gateway/ha/module/HaGatewayProviderModule.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@
4040
import io.trino.gateway.ha.config.OAuth2GatewayCookieConfigurationPropertiesProvider;
4141
import io.trino.gateway.ha.config.RoutingRulesConfiguration;
4242
import io.trino.gateway.ha.config.RulesExternalConfiguration;
43+
import io.trino.gateway.ha.config.ValkeyConfiguration;
4344
import io.trino.gateway.ha.persistence.JdbcConnectionManager;
4445
import io.trino.gateway.ha.persistence.RecordAndAnnotatedConstructorMapper;
4546
import io.trino.gateway.ha.router.BackendStateManager;
47+
import io.trino.gateway.ha.router.DistributedCache;
4648
import io.trino.gateway.ha.router.ForRouter;
4749
import io.trino.gateway.ha.router.GatewayBackendManager;
4850
import io.trino.gateway.ha.router.HaGatewayManager;
@@ -52,6 +54,7 @@
5254
import io.trino.gateway.ha.router.QueryHistoryManager;
5355
import io.trino.gateway.ha.router.ResourceGroupsManager;
5456
import io.trino.gateway.ha.router.RoutingGroupSelector;
57+
import io.trino.gateway.ha.router.ValkeyDistributedCache;
5558
import io.trino.gateway.ha.security.AuthorizationManager;
5659
import io.trino.gateway.ha.security.LbAuthorizer;
5760
import io.trino.gateway.ha.security.LbFormAuthManager;
@@ -198,4 +201,28 @@ public static ClusterStatsMonitor getClusterStatsMonitor(@ForMonitor HttpClient
198201
case NOOP -> new NoopClusterStatsMonitor();
199202
};
200203
}
204+
205+
@Provides
206+
@Singleton
207+
public ValkeyConfiguration getValkeyConfiguration()
208+
{
209+
return configuration.getValkeyConfiguration();
210+
}
211+
212+
@Provides
213+
@Singleton
214+
public DistributedCache getDistributedCache()
215+
{
216+
ValkeyConfiguration valkeyConfig = configuration.getValkeyConfiguration();
217+
return new ValkeyDistributedCache(
218+
valkeyConfig.getHost(),
219+
valkeyConfig.getPort(),
220+
valkeyConfig.getPassword(),
221+
valkeyConfig.getDatabase(),
222+
valkeyConfig.isEnabled(),
223+
valkeyConfig.getMaxTotal(),
224+
valkeyConfig.getMaxIdle(),
225+
valkeyConfig.getMinIdle(),
226+
valkeyConfig.getTimeoutMs());
227+
}
201228
}

0 commit comments

Comments
 (0)