Skip to content
Open
Show file tree
Hide file tree
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
5 changes: 0 additions & 5 deletions gateway-ha/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>

<dependency>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_annotations</artifactId>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public HealthCheckObserver(RoutingManager routingManager)
@Override
public void observe(java.util.List<ClusterStats> clustersStats)
{
routingManager.updateBackEndStats(clustersStats);
routingManager.updateClusterStats(clustersStats);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.gateway.ha.router;

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;
import io.trino.gateway.ha.config.ProxyBackendConfiguration;
import io.trino.gateway.ha.config.RoutingConfiguration;
import jakarta.annotation.Nullable;
import jakarta.ws.rs.HttpMethod;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
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;
import java.util.concurrent.TimeUnit;

/**
* This class performs health check, stats counts for each backend and provides a backend given
* request object. Default implementation comes here.
*/
public abstract class BaseRoutingManager

Choose a reason for hiding this comment

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

Suggestion IMO, for ease of reading you may want to group methods by visibility and order them.

1/ constructor
2/ abstract methods
3/ public method
4/ protected - package
5/ private

I see the abstract methods as an interface for subclasses so it is easier to discover them by future maintainers.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

implements RoutingManager
{
private static final Logger log = Logger.get(BaseRoutingManager.class);
private final ExecutorService executorService = Executors.newFixedThreadPool(5);
private final GatewayBackendManager gatewayBackendManager;
private final ConcurrentHashMap<String, TrinoStatus> backendToStatus;
private final String defaultRoutingGroup;
private final QueryHistoryManager queryHistoryManager;
private final LoadingCache<String, String> queryIdBackendCache;
private final LoadingCache<String, String> queryIdRoutingGroupCache;
private final LoadingCache<String, String> queryIdExternalUrlCache;

public BaseRoutingManager(GatewayBackendManager gatewayBackendManager, QueryHistoryManager queryHistoryManager, RoutingConfiguration routingConfiguration)
{
this.gatewayBackendManager = gatewayBackendManager;
this.defaultRoutingGroup = routingConfiguration.getDefaultRoutingGroup();
this.queryHistoryManager = queryHistoryManager;
this.queryIdBackendCache = buildCache(this::findBackendForUnknownQueryId);
this.queryIdRoutingGroupCache = buildCache(this::findRoutingGroupForUnknownQueryId);
this.queryIdExternalUrlCache = buildCache(this::findExternalUrlForUnknownQueryId);
this.backendToStatus = new ConcurrentHashMap<>();
}

/**
* Provide a strategy to select a backend out of all available backends
*/
protected abstract Optional<ProxyBackendConfiguration> selectBackend(List<ProxyBackendConfiguration> backends, String user);

@Override
public void setBackendForQueryId(String queryId, String backend)
{
queryIdBackendCache.put(queryId, backend);
}

@Override
public void setRoutingGroupForQueryId(String queryId, String routingGroup)
{
queryIdRoutingGroupCache.put(queryId, routingGroup);
}

/**
* Performs routing to a default backend.
*/
public ProxyBackendConfiguration provideDefaultBackendConfiguration(String user)
{
List<ProxyBackendConfiguration> backends = gatewayBackendManager.getActiveDefaultBackends().stream()
.filter(backEnd -> isBackendHealthy(backEnd.getName()))
.toList();
return selectBackend(backends, user).orElseThrow(() -> new IllegalStateException("Number of active backends found zero"));
}

/**
* Performs routing to a given cluster group. This falls back to a default backend, if no scheduled
* backend is found.
*/
@Override
public ProxyBackendConfiguration provideBackendConfiguration(String routingGroup, String user)
{
List<ProxyBackendConfiguration> backends = gatewayBackendManager.getActiveBackends(routingGroup).stream()
.filter(backEnd -> isBackendHealthy(backEnd.getName()))
.toList();
return selectBackend(backends, user).orElseGet(() -> provideDefaultBackendConfiguration(user));
}

/**
* Performs cache look up, if a backend not found, it checks with all backends and tries to find
* out which backend has info about given query id.
*/
@Nullable
@Override
public String findBackendForQueryId(String queryId)
{
String backendAddress = null;
try {
backendAddress = queryIdBackendCache.get(queryId);
}
catch (ExecutionException e) {
log.warn("Exception while loading queryId from cache %s", e.getLocalizedMessage());
}
return backendAddress;
}

@Nullable
@Override
public String findExternalUrlForQueryId(String queryId)
{
String externalUrl = null;
try {
externalUrl = queryIdExternalUrlCache.get(queryId);
}
catch (ExecutionException e) {
log.warn("Exception while loading queryId from cache %s", e.getLocalizedMessage());
}
return externalUrl;
}

/**
* Looks up the routing group associated with the queryId in the cache.
* If it's not in the cache, look up in query history
*/
@Nullable
@Override
public String findRoutingGroupForQueryId(String queryId)
{
String routingGroup = null;
try {
routingGroup = queryIdRoutingGroupCache.get(queryId);
}
catch (ExecutionException e) {
log.warn("Exception while loading queryId from routing group cache %s", e.getLocalizedMessage());
}
return routingGroup;
}

@Override
public void updateBackEndHealth(String backendId, TrinoStatus value)
{
log.info("backend %s isHealthy %s", backendId, value);
backendToStatus.put(backendId, value);
}

@Override
public void updateClusterStats(List<ClusterStats> stats)
{
for (ClusterStats clusterStats : stats) {
updateBackEndHealth(clusterStats.clusterId(), clusterStats.trinoStatus());
}
}

@VisibleForTesting
void setExternalUrlForQueryId(String queryId, String externalUrl)
{
queryIdExternalUrlCache.put(queryId, externalUrl);
}

@VisibleForTesting
String findBackendForUnknownQueryId(String queryId)
{
String backend;
backend = queryHistoryManager.getBackendForQueryId(queryId);
if (Strings.isNullOrEmpty(backend)) {
log.debug("Unable to find backend mapping for [%s]. Searching for suitable backend", queryId);
backend = searchAllBackendForQuery(queryId);
}
return backend;
}

/**
* This tries to find out which backend may have info about given query id. If not found returns
* the first healthy backend.
*/
private String searchAllBackendForQuery(String queryId)
{
List<ProxyBackendConfiguration> backends = gatewayBackendManager.getAllBackends();

Map<String, Future<Integer>> responseCodes = new HashMap<>();
try {
for (ProxyBackendConfiguration backend : backends) {
String target = backend.getProxyTo() + "/v1/query/" + queryId;

Future<Integer> call =
executorService.submit(
() -> {
URL url = new URL(target);
Copy link
Member

Choose a reason for hiding this comment

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

This is deprecated in java 20.
https://stackoverflow.com/questions/75966165/how-to-replace-the-deprecated-url-constructors-in-java-20

Suggested change
URL url = new URL(target);
URL url = URI.create(target).toURL();

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(5));
conn.setReadTimeout((int) TimeUnit.SECONDS.toMillis(5));
conn.setRequestMethod(HttpMethod.HEAD);
return conn.getResponseCode();
});
responseCodes.put(backend.getProxyTo(), call);
}
for (Map.Entry<String, Future<Integer>> entry : responseCodes.entrySet()) {
if (entry.getValue().isDone()) {
int responseCode = entry.getValue().get();
if (responseCode == 200) {
log.info("Found query [%s] on backend [%s]", queryId, entry.getKey());
setBackendForQueryId(queryId, entry.getKey());
return entry.getKey();
}
}
Comment on lines +221 to +228

Choose a reason for hiding this comment

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

Q: Curious, Why did you add condition check for future.isDone() ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Most of these code are existing - https://github.com/trinodb/trino-gateway/blob/main/gateway-ha/src/main/java/io/trino/gateway/ha/router/RoutingManager.java
I changed the file name from RoutingManager to BaseRoutingManager and created interface file with name RoutingManager. So git instead of marking it as file name change, assumed everything in the file as new changes.

}
}
catch (Exception e) {
log.warn("Query id [%s] not found", queryId);
}
// Fallback on first active backend if queryId mapping not found.
return gatewayBackendManager.getActiveBackends(defaultRoutingGroup).get(0).getProxyTo();
Copy link
Member

Choose a reason for hiding this comment

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

Handle Index Out of Bounds Risk?

Suggested change
return gatewayBackendManager.getActiveBackends(defaultRoutingGroup).get(0).getProxyTo();
List<ProxyBackendConfiguration> fallbackBackends = gatewayBackendManager.getActiveBackends(defaultRoutingGroup);
if (fallbackBackends.isEmpty()) {
throw new IllegalStateException("No active backends available for default routing group: " + defaultRoutingGroup);
}
return fallbackBackends.get(0).getProxyTo();

}

/**
* Attempts to look up the routing group associated with the query id from query history table
*/
private String findRoutingGroupForUnknownQueryId(String queryId)
{
String routingGroup = queryHistoryManager.getRoutingGroupForQueryId(queryId);
setRoutingGroupForQueryId(queryId, routingGroup);
return routingGroup;
}

/**
* 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);
return externalUrl;
}

private static LoadingCache<String, String> buildCache(Function<String, String> loader)
Copy link
Member

Choose a reason for hiding this comment

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

does this have to be a static method?

{
return CacheBuilder.newBuilder()
.maximumSize(10000)
.expireAfterAccess(30, TimeUnit.MINUTES)
.build(
new CacheLoader<>()
{
@Override
public String load(String queryId)
{
return loader.apply(queryId);
}
});
}

private boolean isBackendHealthy(String backendId)
{
TrinoStatus status = backendToStatus.getOrDefault(backendId, TrinoStatus.UNKNOWN);
return status == TrinoStatus.HEALTHY;
}
}
Loading