-
Notifications
You must be signed in to change notification settings - Fork 98
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
seoyoniee
wants to merge
5
commits into
trinodb:main
Choose a base branch
from
seoyoniee:fix-build-jdbc-url
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+168
−4
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
993cec5
Fix buildJdbcUrl to apply connection properties
seoyoniee 7e1b2e8
Add null check for JDBC URL
seoyoniee a364ee8
Add check for index
seoyoniee 9c8434e
Add fail test cases in TestJdbcConnectionManager
seoyoniee 9dac008
Extract method to getUriWithRoutingGroupDatabase
seoyoniee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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; | ||||||||||||||
|
@@ -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; | ||||||||||||||
Chaho12 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
if (index == 0) { | ||||||||||||||
throw new IllegalArgumentException("Invalid JDBC URL: no '/' found in " + jdbcUrl); | ||||||||||||||
} | ||||||||||||||
seoyoniee marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
|
||||||||||||||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey @ebyhr WDYT of catching more broder exception like RuntimeException or Exception
Suggested change
|
||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
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() | ||||||||||||||
|
131 changes: 131 additions & 0 deletions
131
gateway-ha/src/test/java/io/trino/gateway/ha/persistence/TestJdbcConnectionManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
ebyhr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
@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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.