-
Notifications
You must be signed in to change notification settings - Fork 129
Migrate from Guava Cache to Caffeine #808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,12 +13,11 @@ | |
| */ | ||
| package io.trino.gateway.ha.router; | ||
|
|
||
| import com.github.benmanes.caffeine.cache.Caffeine; | ||
| import com.github.benmanes.caffeine.cache.LoadingCache; | ||
| import com.google.common.annotations.VisibleForTesting; | ||
| import com.google.common.base.Function; | ||
| import com.google.common.base.Strings; | ||
| import com.google.common.cache.CacheBuilder; | ||
| import com.google.common.cache.CacheLoader; | ||
| import com.google.common.cache.LoadingCache; | ||
| import io.airlift.log.Logger; | ||
| import io.trino.gateway.ha.clustermonitor.ClusterStats; | ||
| import io.trino.gateway.ha.clustermonitor.TrinoStatus; | ||
|
|
@@ -36,7 +35,6 @@ | |
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.Future; | ||
|
|
@@ -123,7 +121,7 @@ public String findBackendForQueryId(String queryId) | |
| try { | ||
| backendAddress = queryIdBackendCache.get(queryId); | ||
| } | ||
| catch (ExecutionException e) { | ||
| catch (RuntimeException e) { | ||
| log.warn("Exception while loading queryId from cache %s", e.getLocalizedMessage()); | ||
| } | ||
| return backendAddress; | ||
|
|
@@ -137,7 +135,7 @@ public String findExternalUrlForQueryId(String queryId) | |
| try { | ||
| externalUrl = queryIdExternalUrlCache.get(queryId); | ||
| } | ||
| catch (ExecutionException e) { | ||
| catch (RuntimeException e) { | ||
| log.warn("Exception while loading queryId from cache %s", e.getLocalizedMessage()); | ||
| } | ||
| return externalUrl; | ||
|
|
@@ -155,7 +153,7 @@ public String findRoutingGroupForQueryId(String queryId) | |
| try { | ||
| routingGroup = queryIdRoutingGroupCache.get(queryId); | ||
| } | ||
| catch (ExecutionException e) { | ||
| catch (RuntimeException e) { | ||
| log.warn("Exception while loading queryId from routing group cache %s", e.getLocalizedMessage()); | ||
| } | ||
| return routingGroup; | ||
|
|
@@ -245,35 +243,23 @@ private String searchAllBackendForQuery(String queryId) | |
| */ | ||
| private String findRoutingGroupForUnknownQueryId(String queryId) | ||
| { | ||
| String routingGroup = queryHistoryManager.getRoutingGroupForQueryId(queryId); | ||
| setRoutingGroupForQueryId(queryId, routingGroup); | ||
| return routingGroup; | ||
| return queryHistoryManager.getRoutingGroupForQueryId(queryId); | ||
| } | ||
|
|
||
| /** | ||
| * Attempts to look up the external url associated with the query id from query history table | ||
| */ | ||
| private String findExternalUrlForUnknownQueryId(String queryId) | ||
| { | ||
| String externalUrl = queryHistoryManager.getExternalUrlForQueryId(queryId); | ||
| setExternalUrlForQueryId(queryId, externalUrl); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This means that when calling get() on the cache, the value will be inserted into the cache.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then how does it impact the testing, do we still need the function
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Perhaps we could add another constructor to the |
||
| return externalUrl; | ||
| return queryHistoryManager.getExternalUrlForQueryId(queryId); | ||
| } | ||
|
|
||
| private LoadingCache<String, String> buildCache(Function<String, String> loader) | ||
| { | ||
| return CacheBuilder.newBuilder() | ||
| return Caffeine.newBuilder() | ||
| .maximumSize(10000) | ||
| .expireAfterAccess(30, TimeUnit.MINUTES) | ||
| .build( | ||
| new CacheLoader<>() | ||
| { | ||
| @Override | ||
| public String load(String queryId) | ||
| { | ||
| return loader.apply(queryId); | ||
| } | ||
| }); | ||
| .build(loader::apply); | ||
| } | ||
|
|
||
| private boolean isBackendHealthy(String backendId) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.