Skip to content

Commit a709769

Browse files
committed
Migrate from Guava Cache to Caffeine
1 parent a97dac8 commit a709769

File tree

3 files changed

+20
-30
lines changed

3 files changed

+20
-30
lines changed

gateway-ha/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@
7474
<artifactId>jackson-dataformat-yaml</artifactId>
7575
</dependency>
7676

77+
<dependency>
78+
<groupId>com.github.ben-manes.caffeine</groupId>
79+
<artifactId>caffeine</artifactId>
80+
<version>3.2.3</version>
81+
</dependency>
82+
7783
<dependency>
7884
<groupId>com.google.guava</groupId>
7985
<artifactId>guava</artifactId>

gateway-ha/src/main/java/io/trino/gateway/ha/router/BaseRoutingManager.java

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@
1313
*/
1414
package io.trino.gateway.ha.router;
1515

16+
import com.github.benmanes.caffeine.cache.Caffeine;
17+
import com.github.benmanes.caffeine.cache.LoadingCache;
1618
import com.google.common.annotations.VisibleForTesting;
1719
import com.google.common.base.Function;
1820
import com.google.common.base.Strings;
19-
import com.google.common.cache.CacheBuilder;
20-
import com.google.common.cache.CacheLoader;
21-
import com.google.common.cache.LoadingCache;
2221
import io.airlift.log.Logger;
2322
import io.trino.gateway.ha.clustermonitor.ClusterStats;
2423
import io.trino.gateway.ha.clustermonitor.TrinoStatus;
@@ -36,7 +35,6 @@
3635
import java.util.Map;
3736
import java.util.Optional;
3837
import java.util.concurrent.ConcurrentHashMap;
39-
import java.util.concurrent.ExecutionException;
4038
import java.util.concurrent.ExecutorService;
4139
import java.util.concurrent.Executors;
4240
import java.util.concurrent.Future;
@@ -123,7 +121,7 @@ public String findBackendForQueryId(String queryId)
123121
try {
124122
backendAddress = queryIdBackendCache.get(queryId);
125123
}
126-
catch (ExecutionException e) {
124+
catch (RuntimeException e) {
127125
log.warn("Exception while loading queryId from cache %s", e.getLocalizedMessage());
128126
}
129127
return backendAddress;
@@ -137,7 +135,7 @@ public String findExternalUrlForQueryId(String queryId)
137135
try {
138136
externalUrl = queryIdExternalUrlCache.get(queryId);
139137
}
140-
catch (ExecutionException e) {
138+
catch (RuntimeException e) {
141139
log.warn("Exception while loading queryId from cache %s", e.getLocalizedMessage());
142140
}
143141
return externalUrl;
@@ -155,7 +153,7 @@ public String findRoutingGroupForQueryId(String queryId)
155153
try {
156154
routingGroup = queryIdRoutingGroupCache.get(queryId);
157155
}
158-
catch (ExecutionException e) {
156+
catch (RuntimeException e) {
159157
log.warn("Exception while loading queryId from routing group cache %s", e.getLocalizedMessage());
160158
}
161159
return routingGroup;
@@ -245,35 +243,23 @@ private String searchAllBackendForQuery(String queryId)
245243
*/
246244
private String findRoutingGroupForUnknownQueryId(String queryId)
247245
{
248-
String routingGroup = queryHistoryManager.getRoutingGroupForQueryId(queryId);
249-
setRoutingGroupForQueryId(queryId, routingGroup);
250-
return routingGroup;
246+
return queryHistoryManager.getRoutingGroupForQueryId(queryId);
251247
}
252248

253249
/**
254250
* Attempts to look up the external url associated with the query id from query history table
255251
*/
256252
private String findExternalUrlForUnknownQueryId(String queryId)
257253
{
258-
String externalUrl = queryHistoryManager.getExternalUrlForQueryId(queryId);
259-
setExternalUrlForQueryId(queryId, externalUrl);
260-
return externalUrl;
254+
return queryHistoryManager.getExternalUrlForQueryId(queryId);
261255
}
262256

263257
private LoadingCache<String, String> buildCache(Function<String, String> loader)
264258
{
265-
return CacheBuilder.newBuilder()
259+
return Caffeine.newBuilder()
266260
.maximumSize(10000)
267261
.expireAfterAccess(30, TimeUnit.MINUTES)
268-
.build(
269-
new CacheLoader<>()
270-
{
271-
@Override
272-
public String load(String queryId)
273-
{
274-
return loader.apply(queryId);
275-
}
276-
});
262+
.build(loader::apply);
277263
}
278264

279265
private boolean isBackendHealthy(String backendId)

gateway-ha/src/main/java/io/trino/gateway/ha/router/TrinoRequestUser.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@
2222
import com.fasterxml.jackson.databind.SerializerProvider;
2323
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
2424
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
25-
import com.google.common.cache.CacheBuilder;
26-
import com.google.common.cache.CacheLoader;
27-
import com.google.common.cache.LoadingCache;
25+
import com.github.benmanes.caffeine.cache.Caffeine;
26+
import com.github.benmanes.caffeine.cache.LoadingCache;
2827
import com.nimbusds.oauth2.sdk.ParseException;
2928
import com.nimbusds.oauth2.sdk.Request;
3029
import com.nimbusds.oauth2.sdk.token.BearerAccessToken;
@@ -42,7 +41,6 @@
4241
import java.util.Base64;
4342
import java.util.Map;
4443
import java.util.Optional;
45-
import java.util.concurrent.ExecutionException;
4644
import java.util.concurrent.TimeUnit;
4745

4846
import static com.nimbusds.openid.connect.sdk.UserInfoResponse.parse;
@@ -198,7 +196,7 @@ private Optional<String> extractUserFromBearerAuth(String header, String userFie
198196
userInfo = Optional.of(userInfoCache.orElseThrow().get(token));
199197
return Optional.of(userInfo.orElseThrow().getSubject().toString());
200198
}
201-
catch (ExecutionException e) {
199+
catch (RuntimeException e) {
202200
log.error(e, "Could not get userInfo");
203201
}
204202
}
@@ -216,10 +214,10 @@ public TrinoRequestUserProvider(RequestAnalyzerConfig config)
216214
userField = config.getTokenUserField();
217215
if (config.getOauthTokenInfoUrl() != null) {
218216
oauthUserInfoUrl = Optional.of(URI.create(config.getOauthTokenInfoUrl()));
219-
userInfoCache = Optional.of(CacheBuilder.newBuilder()
217+
userInfoCache = Optional.of(Caffeine.newBuilder()
220218
.maximumSize(10000)
221219
.expireAfterAccess(10, TimeUnit.MINUTES)
222-
.build(CacheLoader.from(this::getUserInfo)));
220+
.build(this::getUserInfo));
223221
}
224222
else {
225223
oauthUserInfoUrl = Optional.empty();

0 commit comments

Comments
 (0)