Skip to content

Commit d505b71

Browse files
Replace H2 use for testing with PostgreSQL
1 parent ea7e29b commit d505b71

File tree

9 files changed

+471
-173
lines changed

9 files changed

+471
-173
lines changed

gateway-ha/config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ routingRules:
77
# rulesConfigPath: "src/main/resources/rules/routing_rules.yml"
88

99
dataStore:
10-
jdbcUrl: jdbc:postgresql://localhost:5432/trino_gateway_db
11-
user: trino_gateway_db_admin
12-
password: P0stG&es
10+
jdbcUrl: jdbc:postgresql://${ENV:DB_HOSTNAME:localhost}:${ENV:DB_PORT:5432}/${ENV:DB_NAME:trino_gateway_db}
11+
user: ${ENV:DB_USER:trino_gateway_db_admin}
12+
password: ${ENV:DB_PASSWORD:P0stG&es}
1313
driver: org.postgresql.Driver
1414
queryHistoryHoursRetention: 24
1515

gateway-ha/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,6 @@
322322
</dependency>
323323

324324
<!-- Test deps -->
325-
<dependency>
326-
<groupId>com.h2database</groupId>
327-
<artifactId>h2</artifactId>
328-
<version>1.4.192</version>
329-
<scope>test</scope>
330-
</dependency>
331325

332326
<dependency>
333327
<groupId>com.squareup.okhttp3</groupId>

gateway-ha/src/main/java/io/trino/gateway/ha/persistence/dao/ExactMatchSourceSelectorsDao.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,46 @@
1414
package io.trino.gateway.ha.persistence.dao;
1515

1616
import io.trino.gateway.ha.router.ResourceGroupsManager;
17+
import org.jdbi.v3.core.mapper.ColumnMapper;
18+
import org.jdbi.v3.core.statement.StatementContext;
19+
import org.jdbi.v3.sqlobject.config.RegisterColumnMapper;
1720
import org.jdbi.v3.sqlobject.customizer.BindBean;
1821
import org.jdbi.v3.sqlobject.statement.SqlQuery;
1922
import org.jdbi.v3.sqlobject.statement.SqlUpdate;
2023

24+
import java.sql.ResultSet;
25+
import java.sql.SQLException;
26+
import java.sql.Timestamp;
27+
import java.time.LocalDateTime;
28+
import java.time.ZoneId;
29+
import java.time.format.DateTimeFormatter;
2130
import java.util.List;
2231

32+
@RegisterColumnMapper(ExactMatchSourceSelectorsDao.TimestampColumnMapper.class)
2333
public interface ExactMatchSourceSelectorsDao
2434
{
35+
class TimestampColumnMapper
36+
implements ColumnMapper<String>
37+
{
38+
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
39+
40+
@Override
41+
public String map(ResultSet result, int columnNumber, StatementContext ctx)
42+
throws SQLException
43+
{
44+
String columnName = result.getMetaData().getColumnName(columnNumber);
45+
if ("update_time".equals(columnName)) {
46+
try {
47+
Timestamp timestamp = result.getTimestamp(columnNumber);
48+
return timestamp != null ? timestamp.toLocalDateTime().format(FORMATTER) : null;
49+
}
50+
catch (SQLException e) {
51+
return LocalDateTime.now(ZoneId.systemDefault()).format(FORMATTER);
52+
}
53+
}
54+
return result.getString(columnNumber);
55+
}
56+
}
2557
@SqlQuery("""
2658
SELECT * FROM exact_match_source_selectors
2759
""")
@@ -30,7 +62,20 @@ public interface ExactMatchSourceSelectorsDao
3062
@SqlUpdate("""
3163
INSERT INTO exact_match_source_selectors
3264
(resource_group_id, update_time, source, environment, query_type)
33-
VALUES (:resourceGroupId, :updateTime, :source, :environment, :queryType)
65+
VALUES (:resourceGroupId,
66+
CASE
67+
WHEN :updateTime IS NULL OR :updateTime = ''
68+
THEN CURRENT_TIMESTAMP
69+
ELSE CAST(:updateTime AS TIMESTAMP)
70+
END,
71+
:source, :environment, :queryType)
3472
""")
3573
void insert(@BindBean ResourceGroupsManager.ExactSelectorsDetail exactSelectors);
74+
75+
@SqlUpdate("""
76+
INSERT INTO exact_match_source_selectors
77+
(resource_group_id, update_time, source, environment, query_type)
78+
VALUES (:resourceGroupId, CURRENT_TIMESTAMP, :source, :environment, :queryType)
79+
""")
80+
void insertWithCurrentTimestamp(@BindBean ResourceGroupsManager.ExactSelectorsDetail exactSelectors);
3681
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
import io.trino.gateway.ha.persistence.dao.SelectorsDao;
2626
import jakarta.annotation.Nullable;
2727

28+
import java.time.LocalDateTime;
29+
import java.time.ZoneId;
30+
import java.time.format.DateTimeFormatter;
2831
import java.util.ArrayList;
2932
import java.util.List;
3033

@@ -235,11 +238,18 @@ public void deleteGlobalProperty(String name, @Nullable String routingGroupDatab
235238

236239
/**
237240
* Creates exact match source selector for db.
241+
* If no updateTime is provided, the current timestamp will be used automatically.
238242
*/
239243
@Override
240244
public ExactSelectorsDetail createExactMatchSourceSelector(
241245
ExactSelectorsDetail exactSelectorDetail)
242246
{
247+
// If updateTime is null or empty, set current timestamp
248+
if (exactSelectorDetail.getUpdateTime() == null || exactSelectorDetail.getUpdateTime().trim().isEmpty()) {
249+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
250+
exactSelectorDetail.setUpdateTime(LocalDateTime.now(ZoneId.systemDefault()).format(formatter));
251+
}
252+
243253
exactMatchSourceSelectorsDao.insert(exactSelectorDetail);
244254
return exactSelectorDetail;
245255
}

gateway-ha/src/test/java/io/trino/gateway/ha/HaGatewayTestUtils.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,11 @@ public static void prepareMockBackend(
7070
.setResponseCode(200));
7171
}
7272

73-
public static void seedRequiredData(String h2DbFilePath)
73+
public static void seedRequiredData(PostgreSQLContainer<?> container)
7474
{
75-
String jdbcUrl = "jdbc:h2:" + h2DbFilePath;
76-
Jdbi jdbi = Jdbi.create(jdbcUrl, "sa", "sa");
75+
Jdbi jdbi = Jdbi.create(container.getJdbcUrl(), container.getUsername(), container.getPassword());
7776
try (Handle handle = jdbi.open()) {
78-
handle.createUpdate(HaGatewayTestUtils.getResourceFileContent("gateway-ha-persistence-mysql.sql"))
77+
handle.createUpdate(HaGatewayTestUtils.getResourceFileContent("gateway-ha-persistence-postgres.sql"))
7978
.execute();
8079
}
8180
}

gateway-ha/src/test/java/io/trino/gateway/ha/TestingJdbcConnectionManager.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,26 @@
1717
import io.trino.gateway.ha.persistence.JdbcConnectionManager;
1818
import org.jdbi.v3.core.Jdbi;
1919
import org.testcontainers.containers.JdbcDatabaseContainer;
20-
21-
import java.io.File;
22-
import java.nio.file.Path;
20+
import org.testcontainers.containers.PostgreSQLContainer;
2321

2422
public final class TestingJdbcConnectionManager
2523
{
2624
private TestingJdbcConnectionManager() {}
2725

2826
public static JdbcConnectionManager createTestingJdbcConnectionManager()
2927
{
30-
File tempH2DbDir = Path.of(System.getProperty("java.io.tmpdir"), "h2db-" + System.currentTimeMillis()).toFile();
31-
tempH2DbDir.deleteOnExit();
32-
String jdbcUrl = "jdbc:h2:" + tempH2DbDir.getAbsolutePath();
33-
HaGatewayTestUtils.seedRequiredData(tempH2DbDir.getAbsolutePath());
34-
DataStoreConfiguration db = new DataStoreConfiguration(jdbcUrl, "sa", "sa", "org.h2.Driver", 4, false);
35-
Jdbi jdbi = Jdbi.create(jdbcUrl, "sa", "sa");
28+
PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:14-alpine")
29+
.withDatabaseName("testdb")
30+
.withInitScript("gateway-ha-persistence-postgres.sql");
31+
postgres.start();
32+
33+
String jdbcUrl = postgres.getJdbcUrl();
34+
String username = postgres.getUsername();
35+
String password = postgres.getPassword();
36+
37+
DataStoreConfiguration db = new DataStoreConfiguration(jdbcUrl, username, password, "org.postgresql.Driver", 4, false);
38+
Jdbi jdbi = Jdbi.create(jdbcUrl, username, password);
39+
3640
return new JdbcConnectionManager(jdbi, db);
3741
}
3842

gateway-ha/src/test/java/io/trino/gateway/ha/persistence/TestJdbcConnectionManager.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,6 @@
2424

2525
final class TestJdbcConnectionManager
2626
{
27-
@Test
28-
void testBuildJdbcUrlWithH2AndNoRoutingGroupDatabase()
29-
{
30-
JdbcConnectionManager connectionManager = createConnectionManager("jdbc:h2:/mydb");
31-
assertThat(connectionManager.buildJdbcUrl(null)).isEqualTo("jdbc:h2:/mydb");
32-
}
33-
34-
@Test
35-
void testBuildJdbcUrlWithH2AndRoutingGroupDatabase()
36-
{
37-
JdbcConnectionManager connectionManager = createConnectionManager("jdbc:h2:/mydb");
38-
assertThat(connectionManager.buildJdbcUrl("newdb")).isEqualTo("jdbc:h2:/newdb");
39-
}
40-
4127
@Test
4228
void testBuildJdbcUrlWithMySQLAndNoRoutingGroupDatabase()
4329
{
@@ -108,7 +94,7 @@ void testBuildJdbcUrlWithNullJdbcUrlThrowsException()
10894
DataStoreConfiguration dataStoreConfiguration = Mockito.mock(DataStoreConfiguration.class);
10995
when(dataStoreConfiguration.getJdbcUrl()).thenReturn(null);
11096

111-
JdbcConnectionManager connectionManager = new JdbcConnectionManager(Jdbi.create("jdbc:h2:/mydb", "sa", "sa"), dataStoreConfiguration);
97+
JdbcConnectionManager connectionManager = new JdbcConnectionManager(Jdbi.create("jdbc:postgresql://localhost:5432/mydb", "postgres", "postgres"), dataStoreConfiguration);
11298
assertThatThrownBy(() -> connectionManager.buildJdbcUrl(null))
11399
.isInstanceOf(IllegalArgumentException.class)
114100
.hasMessage("JDBC URL cannot be null");
@@ -117,10 +103,10 @@ void testBuildJdbcUrlWithNullJdbcUrlThrowsException()
117103
@Test
118104
void testBuildJdbcUrlWithNoSlashThrowsException()
119105
{
120-
JdbcConnectionManager connectionManager = createConnectionManager("jdbc:h2:mem:test");
106+
JdbcConnectionManager connectionManager = createConnectionManager("jdbc:postgresql:mydb");
121107
assertThatThrownBy(() -> connectionManager.buildJdbcUrl("newdb"))
122108
.isInstanceOf(IllegalArgumentException.class)
123-
.hasMessage("Invalid JDBC URL: no '/' found in jdbc:h2:mem:test");
109+
.hasMessage("Invalid JDBC URL: no '/' found in jdbc:postgresql:mydb");
124110
}
125111

126112
private static JdbcConnectionManager createConnectionManager(String jdbcUrl)

0 commit comments

Comments
 (0)