Skip to content
Draft
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
1 change: 1 addition & 0 deletions gateway-ha/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Djol.skipHotspotSAAttach=true</argLine>
<!-- This is needed because we call main() in test setups. Otherwise Guice singleton won't work. -->
<reuseForks>false</reuseForks>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import io.trino.gateway.ha.resource.PublicResource;
import io.trino.gateway.ha.resource.TrinoResource;
import io.trino.gateway.ha.router.ForRouter;
import io.trino.gateway.ha.router.RoutingRulesManager;
import io.trino.gateway.ha.router.ForwardingRoutingRulesManager;
import io.trino.gateway.ha.security.AuthorizedExceptionMapper;
import io.trino.gateway.proxyserver.ForProxy;
import io.trino.gateway.proxyserver.ProxyRequestHandler;
Expand Down Expand Up @@ -145,7 +145,7 @@ public void configure(Binder binder)
jaxrsBinder(binder).bind(AuthorizedExceptionMapper.class);
binder.bind(ProxyHandlerStats.class).in(Scopes.SINGLETON);
newExporter(binder).export(ProxyHandlerStats.class).withGeneratedName();
binder.bind(RoutingRulesManager.class);
binder.bind(ForwardingRoutingRulesManager.class);
}

private static void addManagedApps(HaGatewayConfiguration configuration, Binder binder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ public enum RulesType
* The service URL can implement dynamic rule changes.
*/
EXTERNAL,

/**
* Routing rules stored in the database.
*/
DB
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,7 @@ public RoutingGroupSelector getRoutingGroupSelector(@ForRouter HttpClient httpCl
if (routingRulesConfig.isRulesEngineEnabled()) {
try {
return switch (routingRulesConfig.getRulesType()) {
case FILE -> RoutingGroupSelector.byRoutingRulesEngine(
routingRulesConfig.getRulesConfigPath(),
routingRulesConfig.getRulesRefreshPeriod(),
configuration.getRequestAnalyzerConfig());
case DB, FILE -> RoutingGroupSelector.byRoutingRulesEngine(configuration);
case EXTERNAL -> {
RulesExternalConfiguration rulesExternalConfiguration = routingRulesConfig.getRulesExternalConfiguration();
yield RoutingGroupSelector.byRoutingExternal(httpClient, rulesExternalConfiguration, configuration.getRequestAnalyzerConfig());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ public class RouterBaseModule
public RouterBaseModule(HaGatewayConfiguration configuration)
{
Jdbi jdbi = Jdbi.create(configuration.getDataStore().getJdbcUrl(), configuration.getDataStore().getUser(), configuration.getDataStore().getPassword());
boolean isOracleBackend = configuration.getDataStore().getJdbcUrl().startsWith("jdbc:oracle");
connectionManager = new JdbcConnectionManager(jdbi, configuration.getDataStore());
resourceGroupsManager = new HaResourceGroupsManager(connectionManager);
gatewayBackendManager = new HaGatewayManager(jdbi);
queryHistoryManager = new HaQueryHistoryManager(jdbi, configuration.getDataStore().getJdbcUrl().startsWith("jdbc:oracle"));
gatewayBackendManager = new HaGatewayManager(jdbi, isOracleBackend);
queryHistoryManager = new HaQueryHistoryManager(jdbi, isOracleBackend);
}

@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,60 +13,169 @@
*/
package io.trino.gateway.ha.persistence.dao;

import org.jdbi.v3.core.mapper.RowMapper;
import org.jdbi.v3.core.statement.StatementContext;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;
import org.jdbi.v3.sqlobject.statement.UseRowMapper;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.List;

import static java.util.Locale.ENGLISH;

public interface GatewayBackendDao
{
@UseRowMapper(GatewayBackendIntToBooleanMapper.class)
@SqlQuery("SELECT * FROM gateway_backend")
List<GatewayBackend> findAll();

@UseRowMapper(GatewayBackendIntToBooleanMapper.class)
@SqlQuery("""
SELECT * FROM gateway_backend
WHERE active = true
""")
List<GatewayBackend> findActiveBackend();

@UseRowMapper(GatewayBackendIntToBooleanMapper.class)
@SqlQuery("""
SELECT * FROM gateway_backend
WHERE active = 1
""")
List<GatewayBackend> findActiveBackendNoBoolean();

default List<GatewayBackend> findActiveBackend(boolean isSupportsBooleanColumn)
{
if (isSupportsBooleanColumn) {
return findActiveBackend();
}

return findActiveBackendNoBoolean();
}

@UseRowMapper(GatewayBackendIntToBooleanMapper.class)
@SqlQuery("""
SELECT * FROM gateway_backend
WHERE active = true AND routing_group = 'adhoc'
""")
List<GatewayBackend> findActiveAdhocBackend();

@UseRowMapper(GatewayBackendIntToBooleanMapper.class)
@SqlQuery("""
SELECT * FROM gateway_backend
WHERE active = 1 AND routing_group = 'adhoc'
""")
List<GatewayBackend> findActiveAdhocBackendNoBoolean();

default List<GatewayBackend> findActiveAdhocBackend(boolean isSupportsBooleanColumn)
{
if (isSupportsBooleanColumn) {
return findActiveAdhocBackend();
}

return findActiveAdhocBackendNoBoolean();
}

@UseRowMapper(GatewayBackendIntToBooleanMapper.class)
@SqlQuery("""
SELECT * FROM gateway_backend
WHERE active = true AND routing_group = :routingGroup
""")
List<GatewayBackend> findActiveBackendByRoutingGroup(String routingGroup);

@UseRowMapper(GatewayBackendIntToBooleanMapper.class)
@SqlQuery("""
SELECT * FROM gateway_backend
WHERE active = 1 AND routing_group = :routingGroup
""")
List<GatewayBackend> findActiveBackendByRoutingGroupNoBoolean(String routingGroup);

default List<GatewayBackend> findActiveBackendByRoutingGroup(String routingGroup, boolean isSupportsBooleanColumn)
{
if (isSupportsBooleanColumn) {
return findActiveBackendByRoutingGroup(routingGroup);
}

return findActiveBackendByRoutingGroupNoBoolean(routingGroup);
}

@UseRowMapper(GatewayBackendIntToBooleanMapper.class)
@SqlQuery("""
SELECT * FROM gateway_backend
WHERE name = :name
""")
List<GatewayBackend> findByName(String name);

@UseRowMapper(GatewayBackendIntToBooleanMapper.class)
@SqlQuery("""
SELECT * FROM gateway_backend
WHERE name = :name
LIMIT 1
""")
GatewayBackend findFirstByName(String name);

@UseRowMapper(GatewayBackendIntToBooleanMapper.class)
@SqlQuery("""
SELECT * FROM gateway_backend
WHERE name = :name
FETCH FIRST 1 ROWS ONLY
""")
GatewayBackend findFirstByNameWithFetch(String name);

default GatewayBackend findFirstByName(String name, boolean isLimitUnsupported){
if (isLimitUnsupported) {
return findFirstByNameWithFetch(name);
}

return findFirstByName(name);
}

@SqlUpdate("""
INSERT INTO gateway_backend (name, routing_group, backend_url, external_url, active)
VALUES (:name, :routingGroup, :backendUrl, :externalUrl, :active)
""")
void create(String name, String routingGroup, String backendUrl, String externalUrl, boolean active);

@SqlUpdate("""
INSERT INTO gateway_backend (name, routing_group, backend_url, external_url, active)
VALUES (:name, :routingGroup, :backendUrl, :externalUrl, :active)
""")
void createNoBoolean(String name, String routingGroup, String backendUrl, String externalUrl, int active);

default void create(String name, String routingGroup, String backendUrl, String externalUrl, boolean active, boolean isSupportsBooleanColumn)
{
if (isSupportsBooleanColumn) {
create(name, routingGroup, backendUrl, externalUrl, active);
return;
}
createNoBoolean(name, routingGroup, backendUrl, externalUrl, active ? 1 : 0);
}

@SqlUpdate("""
UPDATE gateway_backend
SET routing_group = :routingGroup, backend_url = :backendUrl, external_url = :externalUrl, active = :active
WHERE name = :name
""")
void update(String name, String routingGroup, String backendUrl, String externalUrl, boolean active);

@SqlUpdate("""
UPDATE gateway_backend
SET routing_group = :routingGroup, backend_url = :backendUrl, external_url = :externalUrl, active = :active
WHERE name = :name
""")
void updateNoBoolean(String name, String routingGroup, String backendUrl, String externalUrl, int active);

default void update(String name, String routingGroup, String backendUrl, String externalUrl, boolean active, boolean isSupportsBooleanColumn)
{
if (isSupportsBooleanColumn) {
update(name, routingGroup, backendUrl, externalUrl, active);
return;
}
updateNoBoolean(name, routingGroup, backendUrl, externalUrl, active ? 1 : 0);
}

@SqlUpdate("""
UPDATE gateway_backend
SET active = false
Expand All @@ -86,4 +195,28 @@ INSERT INTO gateway_backend (name, routing_group, backend_url, external_url, act
WHERE name = :name
""")
void deleteByName(String name);

class GatewayBackendIntToBooleanMapper
implements RowMapper<GatewayBackend>
{
@Override
public GatewayBackend map(ResultSet resultSet, StatementContext ctx)
throws SQLException
{
boolean active;
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
if (resultSetMetaData.getColumnClassName(5).toLowerCase(ENGLISH).startsWith("int")) {
active = resultSet.getInt(5) != 0;
}
else {
active = resultSet.getBoolean(5);
}
return new GatewayBackend(
resultSet.getString("name"),
resultSet.getString("routing_group"),
resultSet.getString("backend_url"),
resultSet.getString("external_url"),
active);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.gateway.ha.domain;
package io.trino.gateway.ha.persistence.dao;

import com.google.common.collect.ImmutableList;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.jdbi.v3.core.mapper.reflect.ColumnName;

import java.util.List;

import static java.util.Objects.requireNonNull;
import static java.util.Objects.requireNonNullElse;

/**
* RoutingRules
Expand All @@ -28,19 +28,22 @@
* @param priority priority of the routing rule. Higher number represents higher priority. If two rules have same priority then order of execution is not guaranteed.
* @param actions actions of the routing rule
* @param condition condition of the routing rule
* @param routingRuleEngine the engine used for rule evaluation
*/

public record RoutingRule(
String name,
String description,
Integer priority,
List<String> actions,
String condition)
@JsonProperty("name") @ColumnName("name") String name,
@JsonProperty("description") @ColumnName("description") String description,
@JsonProperty("priority") @ColumnName("priority") Integer priority,
// "conditionExpression" is used as a column name because "condition" is a reserved word in MySQL
@JsonProperty("condition") @ColumnName("conditionExpression") String condition,
@JsonProperty("actions") @ColumnName("actions") List<String> actions,
@JsonProperty("routingRuleEngine") @ColumnName("routingRuleEngine") RoutingRuleEngine routingRuleEngine)
{
public RoutingRule {
requireNonNull(name, "name is null");
description = requireNonNullElse(description, "");
priority = requireNonNullElse(priority, 0);
actions = ImmutableList.copyOf(actions);
requireNonNull(condition, "condition is null");
public RoutingRule
{
requireNonNull(name);
requireNonNull(condition);
requireNonNull(actions);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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.persistence.dao;

public enum RoutingRuleEngine
{
MVEL
}
Loading