Skip to content
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

Add a cache for GatewayBackend to HaGatewayManager #501

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package io.trino.gateway.ha.router;

import com.google.common.collect.ImmutableList;
import io.airlift.log.Logger;
import io.trino.gateway.ha.config.ProxyBackendConfiguration;
import io.trino.gateway.ha.persistence.dao.GatewayBackend;
import io.trino.gateway.ha.persistence.dao.GatewayBackendDao;
Expand All @@ -23,78 +22,84 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

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 final GatewayBackendDao dao;
private final AtomicReference<List<GatewayBackend>> cache = new AtomicReference<>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use something like a Guava/Caffeine cache here that has more proper cache management including cache expiration times. Remember that the same database will be shared by multiple GW instances. This code seems to assume that it can keep the cache in sync by simply invalidating the cache when update operations are made, but updates may be made out-of-band by other GW instances.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xkrogen
Thank you for your review. @oneonestar and I are currently working on addressing the issues in this PR based on your feedback and advice :)


public HaGatewayManager(Jdbi jdbi)
{
dao = requireNonNull(jdbi, "jdbi is null").onDemand(GatewayBackendDao.class);
cache.set(ImmutableList.of());
fetchAllBackendsToCache();
}

@Override
public List<ProxyBackendConfiguration> getAllBackends()
{
List<GatewayBackend> proxyBackendList = dao.findAll();
List<GatewayBackend> proxyBackendList = cache.get();
return upcast(proxyBackendList);
}

@Override
public List<ProxyBackendConfiguration> getAllActiveBackends()
{
List<GatewayBackend> proxyBackendList = dao.findActiveBackend();
List<GatewayBackend> proxyBackendList = cache.get().stream()
.filter(GatewayBackend::active)
.collect(toImmutableList());
return upcast(proxyBackendList);
}

@Override
public List<ProxyBackendConfiguration> getActiveAdhocBackends()
{
try {
List<GatewayBackend> proxyBackendList = dao.findActiveAdhocBackend();
return upcast(proxyBackendList);
}
catch (Exception e) {
log.info("Error fetching all backends: %s", e.getLocalizedMessage());
}
return ImmutableList.of();
return getActiveBackends("adhoc");
}

@Override
public List<ProxyBackendConfiguration> getActiveBackends(String routingGroup)
{
List<GatewayBackend> proxyBackendList = dao.findActiveBackendByRoutingGroup(routingGroup);
List<GatewayBackend> proxyBackendList = cache.get().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 = cache.get().stream()
.filter(backend -> backend.name().equals(name))
.collect(toImmutableList());
return upcast(proxyBackendList).stream().findAny();
}

@Override
public void deactivateBackend(String backendName)
{
dao.deactivate(backendName);
fetchAllBackendsToCache();
}

@Override
public void activateBackend(String backendName)
{
dao.activate(backendName);
fetchAllBackendsToCache();
}

@Override
public ProxyBackendConfiguration addBackend(ProxyBackendConfiguration backend)
{
dao.create(backend.getName(), backend.getRoutingGroup(), backend.getProxyTo(), backend.getExternalUrl(), backend.isActive());
fetchAllBackendsToCache();
return backend;
}

Expand All @@ -108,12 +113,14 @@ public ProxyBackendConfiguration updateBackend(ProxyBackendConfiguration backend
else {
dao.update(backend.getName(), backend.getRoutingGroup(), backend.getProxyTo(), backend.getExternalUrl(), backend.isActive());
}
fetchAllBackendsToCache();
return backend;
}

public void deleteBackend(String name)
{
dao.deleteByName(name);
fetchAllBackendsToCache();
}

private static List<ProxyBackendConfiguration> upcast(List<GatewayBackend> gatewayBackendList)
Expand All @@ -130,4 +137,9 @@ private static List<ProxyBackendConfiguration> upcast(List<GatewayBackend> gatew
}
return proxyBackendConfigurations;
}

private void fetchAllBackendsToCache()
{
cache.set(dao.findAll());
}
}
Loading