-
Notifications
You must be signed in to change notification settings - Fork 121
Add cache in HaGatewayManager #783
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,8 +13,13 @@ | |
| */ | ||
| package io.trino.gateway.ha.router; | ||
|
|
||
| import com.google.common.cache.CacheBuilder; | ||
|
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. Let's also use this opportunity to upgrade the caching dependency from guava to caffeine. |
||
| import com.google.common.cache.CacheLoader; | ||
| import com.google.common.cache.LoadingCache; | ||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.util.concurrent.MoreExecutors; | ||
| import io.airlift.log.Logger; | ||
| import io.airlift.stats.CounterStat; | ||
| import io.trino.gateway.ha.config.ProxyBackendConfiguration; | ||
| import io.trino.gateway.ha.config.RoutingConfiguration; | ||
| import io.trino.gateway.ha.persistence.dao.GatewayBackend; | ||
|
|
@@ -24,35 +29,92 @@ | |
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.Executors; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkState; | ||
| import static com.google.common.collect.ImmutableList.toImmutableList; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| public class HaGatewayManager | ||
| implements GatewayBackendManager | ||
| { | ||
| private static final Logger log = Logger.get(HaGatewayManager.class); | ||
| private static final Object ALL_BACKEND_CACHE_KEY = new Object(); | ||
|
|
||
| private final GatewayBackendDao dao; | ||
| private final String defaultRoutingGroup; | ||
| private final boolean cacheEnabled; | ||
| private final LoadingCache<Object, List<GatewayBackend>> backendCache; | ||
|
|
||
| private final CounterStat backendLookupSuccesses = new CounterStat(); | ||
| private final CounterStat backendLookupFailures = new CounterStat(); | ||
|
|
||
| public HaGatewayManager(Jdbi jdbi, RoutingConfiguration routingConfiguration) | ||
| { | ||
| dao = requireNonNull(jdbi, "jdbi is null").onDemand(GatewayBackendDao.class); | ||
| this.defaultRoutingGroup = routingConfiguration.getDefaultRoutingGroup(); | ||
| if (!routingConfiguration.getDatabaseCacheTTL().isZero()) { | ||
| cacheEnabled = true; | ||
| backendCache = CacheBuilder | ||
| .newBuilder() | ||
| .initialCapacity(1) | ||
| .refreshAfterWrite(routingConfiguration.getDatabaseCacheTTL().toJavaTime()) | ||
| .build(CacheLoader.asyncReloading( | ||
| CacheLoader.from(this::fetchAllBackends), | ||
| MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor()))); | ||
|
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. How about using a ThreadFactory to prevent thread leaks?
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. |
||
| // Load the data once during initialization. This ensures a fail-fast behavior in case of database misconfiguration. | ||
| backendCache.getUnchecked(ALL_BACKEND_CACHE_KEY); | ||
| } | ||
| else { | ||
| cacheEnabled = false; | ||
| backendCache = null; | ||
| } | ||
| } | ||
|
|
||
| private List<GatewayBackend> fetchAllBackends() | ||
| { | ||
| try { | ||
| List<GatewayBackend> backends = dao.findAll(); | ||
| backendLookupSuccesses.update(1); | ||
| return backends; | ||
| } | ||
| catch (Exception e) { | ||
| backendLookupFailures.update(1); | ||
| log.warn(e, "Failed to fetch backends"); | ||
| throw e; | ||
|
Comment on lines
+83
to
+84
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. Should we throw exception? or just return empty list to maintain service availability |
||
| } | ||
| } | ||
|
|
||
| private void invalidateBackendCache() | ||
| { | ||
| if (cacheEnabled) { | ||
| backendCache.invalidateAll(); | ||
| } | ||
| } | ||
|
|
||
| private List<GatewayBackend> getOrFetchAllBackends() | ||
| { | ||
| if (cacheEnabled) { | ||
| return backendCache.getUnchecked(ALL_BACKEND_CACHE_KEY); | ||
| } | ||
| else { | ||
| return fetchAllBackends(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public List<ProxyBackendConfiguration> getAllBackends() | ||
| { | ||
| List<GatewayBackend> proxyBackendList = dao.findAll(); | ||
| List<GatewayBackend> proxyBackendList = getOrFetchAllBackends(); | ||
| return upcast(proxyBackendList); | ||
| } | ||
|
|
||
| @Override | ||
| public List<ProxyBackendConfiguration> getAllActiveBackends() | ||
| { | ||
| List<GatewayBackend> proxyBackendList = dao.findActiveBackend(); | ||
| List<GatewayBackend> proxyBackendList = getOrFetchAllBackends().stream() | ||
| .filter(GatewayBackend::active) | ||
| .collect(toImmutableList()); | ||
| return upcast(proxyBackendList); | ||
| } | ||
|
|
||
|
|
@@ -71,14 +133,19 @@ public List<ProxyBackendConfiguration> getActiveDefaultBackends() | |
| @Override | ||
| public List<ProxyBackendConfiguration> getActiveBackends(String routingGroup) | ||
| { | ||
| List<GatewayBackend> proxyBackendList = dao.findActiveBackendByRoutingGroup(routingGroup); | ||
| List<GatewayBackend> proxyBackendList = getOrFetchAllBackends().stream() | ||
| .filter(GatewayBackend::active) | ||
| .filter(backend -> backend.routingGroup().equals(routingGroup)) | ||
| .collect(toImmutableList()); | ||
| return upcast(proxyBackendList); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<ProxyBackendConfiguration> getBackendByName(String name) | ||
| { | ||
| List<GatewayBackend> proxyBackendList = dao.findByName(name); | ||
| List<GatewayBackend> proxyBackendList = getOrFetchAllBackends().stream() | ||
| .filter(backend -> backend.name().equals(name)) | ||
| .collect(toImmutableList()); | ||
| return upcast(proxyBackendList).stream().findAny(); | ||
| } | ||
|
|
||
|
|
@@ -102,6 +169,7 @@ private void updateClusterActivationStatus(String clusterName, boolean newStatus | |
| boolean previousStatus = model.active(); | ||
| changeActiveStatus.run(); | ||
| logActivationStatusChange(clusterName, newStatus, previousStatus); | ||
| invalidateBackendCache(); | ||
| } | ||
|
|
||
| private static void logActivationStatusChange(String clusterName, boolean newStatus, boolean previousStatus) | ||
|
|
@@ -117,6 +185,7 @@ public ProxyBackendConfiguration addBackend(ProxyBackendConfiguration backend) | |
| String backendProxyTo = removeTrailingSlash(backend.getProxyTo()); | ||
| String backendExternalUrl = removeTrailingSlash(backend.getExternalUrl()); | ||
| dao.create(backend.getName(), backend.getRoutingGroup(), backendProxyTo, backendExternalUrl, backend.isActive()); | ||
| invalidateBackendCache(); | ||
| return backend; | ||
| } | ||
|
|
||
|
|
@@ -133,12 +202,14 @@ public ProxyBackendConfiguration updateBackend(ProxyBackendConfiguration backend | |
| dao.update(backend.getName(), backend.getRoutingGroup(), backendProxyTo, backendExternalUrl, backend.isActive()); | ||
| logActivationStatusChange(backend.getName(), backend.isActive(), model.active()); | ||
| } | ||
| invalidateBackendCache(); | ||
| return backend; | ||
| } | ||
|
|
||
| public void deleteBackend(String name) | ||
| { | ||
| dao.deleteByName(name); | ||
| invalidateBackendCache(); | ||
| } | ||
|
|
||
| private static List<ProxyBackendConfiguration> upcast(List<GatewayBackend> gatewayBackendList) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's create a
CacheConfigurationinstead of putting all the configs under routing configs. We can definecacheEnabled,expireAfterWrite, andmaximumSizeexplicitly instead of usingif (!routingConfiguration.getDatabaseCacheTTL().isZero())to decide if cache is enabled, and set default values for them. Also, we can make cache behavior configurable there.