Skip to content

Commit 8a6ab09

Browse files
committed
add enmu DatastoreBackend, add javadoc
1 parent 28879c5 commit 8a6ab09

File tree

8 files changed

+79
-23
lines changed

8 files changed

+79
-23
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package io.trino.gateway.ha.config;
15+
16+
public enum DataStoreBackend {
17+
ORACLE,
18+
MYSQL,
19+
POSTGRES,
20+
H2
21+
}

gateway-ha/src/main/java/io/trino/gateway/ha/config/DataStoreConfiguration.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*/
1414
package io.trino.gateway.ha.config;
1515

16+
import java.util.Locale;
17+
1618
public class DataStoreConfiguration
1719
{
1820
private String jdbcUrl;
@@ -21,6 +23,7 @@ public class DataStoreConfiguration
2123
private String driver;
2224
private Integer queryHistoryHoursRetention = 4;
2325
private boolean runMigrationsEnabled = true;
26+
private DataStoreBackend dataStoreBackendType;
2427

2528
// TODO: Refactor to decouple DataStoreConfiguration from a specific
2629
// database implementation after adopting the Airlift configuration framework (https://github.com/trinodb/trino-gateway/issues/378)
@@ -107,4 +110,34 @@ public void setMysqlConfiguration(MysqlConfiguration mysqlConfig)
107110
{
108111
this.mysqlConfiguration = mysqlConfig;
109112
}
113+
114+
public DataStoreBackend getDataStoreBackendType()
115+
{
116+
if (dataStoreBackendType != null) {
117+
return dataStoreBackendType;
118+
}
119+
120+
if (jdbcUrl != null) {
121+
String url = jdbcUrl.toLowerCase(Locale.ROOT);
122+
if (url.startsWith("jdbc:oracle:")) {
123+
return DataStoreBackend.ORACLE;
124+
}
125+
if (url.startsWith("jdbc:mysql:")) {
126+
return DataStoreBackend.MYSQL;
127+
}
128+
if (url.startsWith("jdbc:postgresql:")) {
129+
return DataStoreBackend.POSTGRES;
130+
}
131+
if (url.startsWith("jdbc:h2:")) {
132+
return DataStoreBackend.H2;
133+
}
134+
}
135+
136+
throw new IllegalStateException("Cannot infer DataStoreBackend from jdbcUrl: " + jdbcUrl);
137+
}
138+
139+
public void setDataStoreBackendType(DataStoreBackend backendType)
140+
{
141+
this.dataStoreBackendType = backendType;
142+
}
110143
}

gateway-ha/src/main/java/io/trino/gateway/ha/config/HaGatewayConfiguration.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public class HaGatewayConfiguration
4343
private List<String> statementPaths = ImmutableList.of(V1_STATEMENT_PATH);
4444
private boolean includeClusterHostInResponse;
4545
private ProxyResponseConfiguration proxyResponseConfiguration = new ProxyResponseConfiguration();
46-
private boolean oracleBackend;
4746

4847
private RequestAnalyzerConfig requestAnalyzerConfig = new RequestAnalyzerConfig();
4948

@@ -298,14 +297,4 @@ public HaGatewayConfigurationException(String message)
298297
super(message);
299298
}
300299
}
301-
302-
public boolean isOracleBackend()
303-
{
304-
return oracleBackend;
305-
}
306-
307-
public void setOracleBackend(boolean oracleBackend)
308-
{
309-
this.oracleBackend = oracleBackend;
310-
}
311300
}

gateway-ha/src/main/java/io/trino/gateway/ha/persistence/BasicJdbcPropertiesProvider.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@
1717

1818
import java.util.Properties;
1919

20+
/**
21+
* Default JDBC properties provider used as a fallback when no database-specific
22+
* {@link JdbcPropertiesProvider} supports the given {@link DataStoreConfiguration}.
23+
*
24+
* <p>This provider simply sets the basic "user" and "password" properties
25+
* and should always be the last provider in the list of available providers.
26+
*
27+
* <p>If a more specific provider (e.g., for MySQL, Oracle, etc.) supports the configuration,
28+
* it should be preferred over this basic fallback.
29+
*/
2030
public class BasicJdbcPropertiesProvider
2131
implements JdbcPropertiesProvider
2232
{

gateway-ha/src/main/java/io/trino/gateway/ha/persistence/JdbcConnectionManager.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ public Jdbi getJdbi(@Nullable String routingGroupDatabase)
6868
.registerRowMapper(new RecordAndAnnotatedConstructorMapper());
6969
}
7070

71+
public DataStoreConfiguration getConfiguration()
72+
{
73+
return configuration;
74+
}
75+
7176
private String buildJdbcUrl(@Nullable String routingGroupDatabase)
7277
{
7378
String jdbcUrl = configuration.getJdbcUrl();

gateway-ha/src/main/java/io/trino/gateway/ha/router/HaQueryHistoryManager.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
import com.google.common.base.Strings;
1717
import com.google.inject.Inject;
18-
import io.trino.gateway.ha.config.HaGatewayConfiguration;
18+
import io.trino.gateway.ha.config.DataStoreBackend;
19+
import io.trino.gateway.ha.config.DataStoreConfiguration;
1920
import io.trino.gateway.ha.domain.TableData;
2021
import io.trino.gateway.ha.domain.request.QueryHistoryRequest;
2122
import io.trino.gateway.ha.domain.response.DistributionResponse;
@@ -40,13 +41,14 @@ public class HaQueryHistoryManager
4041
private static final int FIRST_PAGE_NO = 1;
4142

4243
private final QueryHistoryDao dao;
43-
private final boolean isOracleBackend;
44+
private final DataStoreBackend backend;
4445

4546
@Inject
46-
public HaQueryHistoryManager(Jdbi jdbi, HaGatewayConfiguration configuration)
47+
public HaQueryHistoryManager(Jdbi jdbi, DataStoreConfiguration configuration)
4748
{
49+
requireNonNull(configuration, "DataStoreConfiguration is null");
4850
dao = requireNonNull(jdbi, "jdbi is null").onDemand(QueryHistoryDao.class);
49-
this.isOracleBackend = configuration.isOracleBackend();
51+
this.backend = configuration.getDataStoreBackendType();
5052
}
5153

5254
@Override
@@ -72,10 +74,10 @@ public List<QueryDetail> fetchQueryHistory(Optional<String> user)
7274
{
7375
List<QueryHistory> histories;
7476
if (user.isPresent()) {
75-
histories = dao.findRecentQueriesByUserName(user.orElseThrow(), isOracleBackend);
77+
histories = dao.findRecentQueriesByUserName(user.orElseThrow(), backend == DataStoreBackend.ORACLE);
7678
}
7779
else {
78-
histories = dao.findRecentQueries(isOracleBackend);
80+
histories = dao.findRecentQueries(backend == DataStoreBackend.ORACLE);
7981
}
8082
return upcast(histories);
8183
}

gateway-ha/src/test/java/io/trino/gateway/ha/router/BaseTestQueryHistoryManager.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
package io.trino.gateway.ha.router;
1515

1616
import io.trino.gateway.ha.config.DataStoreConfiguration;
17-
import io.trino.gateway.ha.config.HaGatewayConfiguration;
1817
import io.trino.gateway.ha.domain.response.DistributionResponse;
1918
import io.trino.gateway.ha.persistence.FlywayMigration;
2019
import io.trino.gateway.ha.persistence.JdbcConnectionManager;
@@ -51,9 +50,7 @@ void setUp()
5150
true);
5251
FlywayMigration.migrate(config);
5352
JdbcConnectionManager jdbcConnectionManager = createTestingJdbcConnectionManager(container, config);
54-
HaGatewayConfiguration haGatewayConfiguration = new HaGatewayConfiguration();
55-
haGatewayConfiguration.setOracleBackend(container.getJdbcUrl().startsWith("jdbc:oracle"));
56-
queryHistoryManager = new HaQueryHistoryManager(jdbcConnectionManager.getJdbi(), haGatewayConfiguration);
53+
queryHistoryManager = new HaQueryHistoryManager(jdbcConnectionManager.getJdbi(), config);
5754
}
5855

5956
@AfterAll

gateway-ha/src/test/java/io/trino/gateway/ha/router/TestStochasticRoutingManager.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
package io.trino.gateway.ha.router;
1515

1616
import io.trino.gateway.ha.clustermonitor.TrinoStatus;
17-
import io.trino.gateway.ha.config.HaGatewayConfiguration;
1817
import io.trino.gateway.ha.config.ProxyBackendConfiguration;
1918
import io.trino.gateway.ha.persistence.JdbcConnectionManager;
2019
import org.junit.jupiter.api.BeforeAll;
@@ -37,7 +36,7 @@ void setUp()
3736
{
3837
JdbcConnectionManager connectionManager = createTestingJdbcConnectionManager();
3938
backendManager = new HaGatewayManager(connectionManager.getJdbi());
40-
historyManager = new HaQueryHistoryManager(connectionManager.getJdbi(), new HaGatewayConfiguration());
39+
historyManager = new HaQueryHistoryManager(connectionManager.getJdbi(), connectionManager.getConfiguration());
4140
haRoutingManager = new StochasticRoutingManager(backendManager, historyManager);
4241
}
4342

0 commit comments

Comments
 (0)