Skip to content

Fix buildJdbcUrl to apply connection properties #651

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
*/
package io.trino.gateway.ha.persistence;

import com.google.common.annotations.VisibleForTesting;
import io.airlift.log.Logger;
import io.trino.gateway.ha.config.DataStoreConfiguration;
import io.trino.gateway.ha.persistence.dao.QueryHistoryDao;
import jakarta.annotation.Nullable;
import org.jdbi.v3.core.Jdbi;
import org.jdbi.v3.sqlobject.SqlObjectPlugin;

import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -60,13 +64,42 @@ public Jdbi getJdbi(@Nullable String routingGroupDatabase)
.registerRowMapper(new RecordAndAnnotatedConstructorMapper());
}

private String buildJdbcUrl(@Nullable String routingGroupDatabase)
@VisibleForTesting
String buildJdbcUrl(@Nullable String routingGroupDatabase)
{
String jdbcUrl = configuration.getJdbcUrl();
if (routingGroupDatabase != null) {
jdbcUrl = jdbcUrl.substring(0, jdbcUrl.lastIndexOf('/') + 1) + routingGroupDatabase;
if (jdbcUrl == null) {
throw new IllegalArgumentException("JDBC URL cannot be null");
}
return jdbcUrl;
if (routingGroupDatabase == null) {
return jdbcUrl;
}
try {
int index = jdbcUrl.indexOf("/") + 1;
if (index == 0) {
throw new IllegalArgumentException("Invalid JDBC URL: no '/' found in " + jdbcUrl);
}

URI newUri = getUriWithRoutingGroupDatabase(routingGroupDatabase, index, jdbcUrl);
return jdbcUrl.substring(0, index) + newUri;
}
catch (URISyntaxException e) {
throw new RuntimeException(e);
Comment on lines +86 to +87
Copy link
Member

Choose a reason for hiding this comment

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

Hey @ebyhr WDYT of catching more broder exception like RuntimeException or Exception
Because NPE could happen if jdbcUrl is none, or IndexOutOfBounds if / is not present, or IllegalArgumentException if Path.of(uri.getPath()) is malformed path.

Suggested change
catch (URISyntaxException e) {
throw new RuntimeException(e);
catch (Exception e) { // catch all
Logger.get(getClass()).error(e, "Failed to build JDBC URL for routing group '%s'", routingGroupDatabase);
throw new RuntimeException(e);
}

}
}

private static URI getUriWithRoutingGroupDatabase(String routingGroupDatabase, int index, String jdbcUrl)
throws URISyntaxException
{
URI uri = new URI(jdbcUrl.substring(index));
return new URI(
uri.getScheme(),
uri.getUserInfo(),
uri.getHost(),
uri.getPort(),
Path.of(uri.getPath()).resolveSibling(routingGroupDatabase).toString(),
uri.getQuery(),
uri.getFragment());
}

private void startCleanUps()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* 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;

import io.trino.gateway.ha.config.DataStoreConfiguration;
import org.jdbi.v3.core.Jdbi;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.when;

final class TestJdbcConnectionManager
{
@Test
void testBuildJdbcUrlWithH2AndNoRoutingGroupDatabase()
{
JdbcConnectionManager connectionManager = createConnectionManager("jdbc:h2:/mydb");
assertThat(connectionManager.buildJdbcUrl(null)).isEqualTo("jdbc:h2:/mydb");
}

@Test
void testBuildJdbcUrlWithH2AndRoutingGroupDatabase()
{
JdbcConnectionManager connectionManager = createConnectionManager("jdbc:h2:/mydb");
assertThat(connectionManager.buildJdbcUrl("newdb")).isEqualTo("jdbc:h2:/newdb");
}

@Test
void testBuildJdbcUrlWithMySQLAndNoRoutingGroupDatabase()
{
JdbcConnectionManager connectionManager = createConnectionManager("jdbc:mysql://localhost:3306/mydb");
assertThat(connectionManager.buildJdbcUrl(null)).isEqualTo("jdbc:mysql://localhost:3306/mydb");
}

@Test
void testBuildJdbcUrlWithMySQLAndRoutingGroupDatabase()
{
JdbcConnectionManager connectionManager = createConnectionManager("jdbc:mysql://localhost:3306/mydb");
assertThat(connectionManager.buildJdbcUrl("newdb")).isEqualTo("jdbc:mysql://localhost:3306/newdb");
}

@Test
void testBuildJdbcUrlWithMySQLAndParametersAndRoutingGroupDatabase()
{
JdbcConnectionManager connectionManager = createConnectionManager("jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=Asia/Seoul");
assertThat(connectionManager.buildJdbcUrl("newdb")).isEqualTo("jdbc:mysql://localhost:3306/newdb?useSSL=false&serverTimezone=Asia/Seoul");
}

@Test
void testBuildJdbcUrlWithPostgreSQLAndNoRoutingGroupDatabase()
{
JdbcConnectionManager connectionManager = createConnectionManager("jdbc:postgresql://localhost:5432/mydb");
assertThat(connectionManager.buildJdbcUrl(null)).isEqualTo("jdbc:postgresql://localhost:5432/mydb");
}

@Test
void testBuildJdbcUrlWithPostgreSQLAndRoutingGroupDatabase()
{
JdbcConnectionManager connectionManager = createConnectionManager("jdbc:postgresql://localhost:5432/mydb");
assertThat(connectionManager.buildJdbcUrl("newdb")).isEqualTo("jdbc:postgresql://localhost:5432/newdb");
}

@Test
void testBuildJdbcUrlWithPostgreSQLAndParametersAndRoutingGroupDatabase()
{
JdbcConnectionManager connectionManager = createConnectionManager("jdbc:postgresql://localhost:5432/mydb?ssl=false&serverTimezone=Asia/Seoul");
assertThat(connectionManager.buildJdbcUrl("newdb")).isEqualTo("jdbc:postgresql://localhost:5432/newdb?ssl=false&serverTimezone=Asia/Seoul");
}

@Test
void testBuildJdbcUrlWithOracleAndNoRoutingGroupDatabase()
{
JdbcConnectionManager connectionManager = createConnectionManager("jdbc:oracle:thin:@//localhost:1521/mydb");
assertThat(connectionManager.buildJdbcUrl(null)).isEqualTo("jdbc:oracle:thin:@//localhost:1521/mydb");
}

@Test
void testBuildJdbcUrlWithOracleAndRoutingGroupDatabase()
{
JdbcConnectionManager connectionManager = createConnectionManager("jdbc:oracle:thin:@//localhost:1521/mydb");
assertThat(connectionManager.buildJdbcUrl("newdb")).isEqualTo("jdbc:oracle:thin:@//localhost:1521/newdb");
}

@Test
void testBuildJdbcUrlWithOracleAndParametersAndRoutingGroupDatabase()
{
JdbcConnectionManager connectionManager = createConnectionManager("jdbc:oracle:thin:@//localhost:1521/mydb?sessionTimeZone=Asia/Seoul");
assertThat(connectionManager.buildJdbcUrl("newdb")).isEqualTo("jdbc:oracle:thin:@//localhost:1521/newdb?sessionTimeZone=Asia/Seoul");
}

@Test
void testBuildJdbcUrlWithNullJdbcUrlThrowsException()
{
// Mock the behavior of DataStoreConfiguration.getJdbcUrl
DataStoreConfiguration dataStoreConfiguration = Mockito.mock(DataStoreConfiguration.class);
when(dataStoreConfiguration.getJdbcUrl()).thenReturn(null);

JdbcConnectionManager connectionManager = new JdbcConnectionManager(Jdbi.create("jdbc:h2:/mydb", "sa", "sa"), dataStoreConfiguration);
assertThatThrownBy(() -> connectionManager.buildJdbcUrl(null))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("JDBC URL cannot be null");
}

@Test
void testBuildJdbcUrlWithNoSlashThrowsException()
{
JdbcConnectionManager connectionManager = createConnectionManager("jdbc:h2:mem:test");
assertThatThrownBy(() -> connectionManager.buildJdbcUrl("newdb"))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Invalid JDBC URL: no '/' found in jdbc:h2:mem:test");
}

private static JdbcConnectionManager createConnectionManager(String jdbcUrl)
{
DataStoreConfiguration db = new DataStoreConfiguration(jdbcUrl, "sa", "sa", "", 4, true);
return new JdbcConnectionManager(Jdbi.create(jdbcUrl, "sa", "sa"), db);
}
}