Skip to content
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

feat: Java API to configure named connections #1604

Merged
merged 1 commit into from
Nov 7, 2023

Conversation

hessjcg
Copy link
Collaborator

@hessjcg hessjcg commented Oct 10, 2023

In real usage, an application would first call Connector.register() to register the configuration for a named connection.

ConnectorConfig config =  new ConnectorConfig.Builder()
            .withTargetPrincipal("example@project.iam.googleapis.com")
            .withDelegates(Arrays.asList("delegate@project.iam.googleapis.com"))
            .build();
ConnectorRegistry.register("my-connector", config);

Then the application would use the connector by adding the cloudSqlNamedConnector parameter to the JDBC URL:

String jdbcUrl = "jdbc:mysql:///<DATABASE_NAME>?"+
  + "cloudSqlInstance=project:region:instance"
  + "&cloudSqlNamedConnector=my-connector"
  + "&socketFactory=com.google.cloud.sql.mysql.SocketFactory"
  + "&user=<DB_USER>&password=<PASSWORD>";

Or using JDBC connection properties, by adding the cloudSqlNamedConnector property:

// Set up URL parameters
Properties connProps=new Properties();
connProps.setProperty("user","<DB_USER>");
connProps.setProperty("password","<PASSWORD>");
connProps.setProperty("sslmode","disable");
connProps.setProperty("socketFactory","<DRIVER_CLASS>");
connProps.setProperty("cloudSqlInstance","my-connection");
connProps.setProperty("cloudSqlNamedConnector","[my-connection](project:region:instance)");

// Initialize connection pool
HikariConfig config=new HikariConfig();
config.setJdbcUrl("jdbc:mysql:///<DB_NAME>");
config.setDataSourceProperties(connProps);
config.setConnectionTimeout(10000); // 10s

HikariDataSource connectionPool=new HikariDataSource(config);

The application can stop individual named connectors, or all connectors:

ConnectorRegistry.close("my-connector");

The application can shutdown the connector, preventing any new connections
and stoping all connector threads.

ConnectorRegistry.shutdown();

Fixes #1226

@hessjcg hessjcg requested a review from a team as a code owner October 10, 2023 17:25
@hessjcg hessjcg marked this pull request as draft October 10, 2023 17:25
@hessjcg hessjcg force-pushed the gh-1226-named-connection-java-api branch 4 times, most recently from f09138c to 009f14e Compare October 10, 2023 19:30
@hessjcg hessjcg force-pushed the gh-1226-named-connection-java-api branch from 009f14e to dc22b8e Compare October 10, 2023 21:14
@github-actions github-actions bot removed the size: l label Oct 10, 2023
@hessjcg hessjcg force-pushed the gh-1226-named-connection-java-api branch from dc22b8e to 73d9d3c Compare October 12, 2023 18:12
@hessjcg hessjcg force-pushed the gh-1226-named-connection-java-api branch from 73d9d3c to c8926b2 Compare October 12, 2023 22:31
@github-actions github-actions bot removed the size: l label Oct 13, 2023
@hessjcg hessjcg force-pushed the gh-1587-config-value-object branch 3 times, most recently from 116886f to e8b778d Compare October 13, 2023 01:48
@hessjcg hessjcg force-pushed the gh-1226-named-connection-java-api branch from c8926b2 to 7f2de8b Compare October 13, 2023 01:48
@hessjcg hessjcg force-pushed the gh-1226-named-connection-java-api branch 3 times, most recently from e4ecf72 to 1bb166a Compare November 6, 2023 23:03
@hessjcg hessjcg force-pushed the gh-1226-named-connection-java-api branch 2 times, most recently from ef9adf1 to 420e7e1 Compare November 7, 2023 17:40
@hessjcg hessjcg changed the base branch from main to gh-1226-InternalConnectorRegsitryTest November 7, 2023 17:42
@hessjcg hessjcg marked this pull request as draft November 7, 2023 17:42
@hessjcg hessjcg force-pushed the gh-1226-named-connection-java-api branch from 420e7e1 to 666e768 Compare November 7, 2023 17:55
@hessjcg hessjcg force-pushed the gh-1226-InternalConnectorRegsitryTest branch from c7b599a to c0827e1 Compare November 7, 2023 17:55
@hessjcg hessjcg force-pushed the gh-1226-named-connection-java-api branch from 666e768 to d5f6ca1 Compare November 7, 2023 17:56
@hessjcg hessjcg force-pushed the gh-1226-InternalConnectorRegsitryTest branch 3 times, most recently from a173501 to 2b7800a Compare November 7, 2023 19:06
@hessjcg hessjcg force-pushed the gh-1226-named-connection-java-api branch from d5f6ca1 to ff6454f Compare November 7, 2023 19:10
Base automatically changed from gh-1226-InternalConnectorRegsitryTest to main November 7, 2023 19:16
@hessjcg hessjcg force-pushed the gh-1226-named-connection-java-api branch 3 times, most recently from 016381c to d52c0f4 Compare November 7, 2023 19:37
@hessjcg hessjcg marked this pull request as ready for review November 7, 2023 19:43
Copy link
Member

@enocom enocom left a comment

Choose a reason for hiding this comment

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

Two things and then we can merge this:

  • Let's make sure to close all unnamed connectors
  • The tests of named connectors aren't clear in how they're confirming we're using a named connector.

if (config.getNamedConnection() != null) {
NamedConnector namedConnector = getNamedConnector(config);
return namedConnector.connector.connect(namedConnector.config);
} else {
Copy link
Member

Choose a reason for hiding this comment

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

nit else is unnecessary after a return

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

return connector
.getConnection(config.withConnectorConfig(connector.getConfig()))
.getConnectionMetadata(refreshTimeoutMs);
} else {
Copy link
Member

Choose a reason for hiding this comment

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

same nit here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

/** Shutdown all connectors and remove the singleton instance. */
public void shutdown() {
this.unnamedConnectors.forEach(
(key, c) -> {
Copy link
Member

Choose a reason for hiding this comment

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

nit: could this be all in one line?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

(key, c) -> {
c.close();
});
this.unnamedConnectors.clear();
Copy link
Member

Choose a reason for hiding this comment

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

We need to close all the unnamed connectors in the connectors map.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

ConnectionConfig namedConfig = ConnectionConfig.fromConnectionProperties(connProps);
Socket socket = coreSocketFactory.connect(namedConfig);
// Assert that the socket opens correctly.
assertThat(readLine(socket)).isEqualTo(SERVER_MESSAGE);
Copy link
Member

Choose a reason for hiding this comment

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

Sorry I still don't understand -- how do we know the registered connector was used and not simply an unnamed connector?

@hessjcg hessjcg force-pushed the gh-1226-named-connection-java-api branch from d52c0f4 to 9d12ed4 Compare November 7, 2023 21:43
@hessjcg hessjcg requested a review from enocom November 7, 2023 21:43
@hessjcg hessjcg merged commit 310624d into main Nov 7, 2023
16 checks passed
@hessjcg hessjcg deleted the gh-1226-named-connection-java-api branch November 7, 2023 23:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow callers to configure multiple "connectors" per socket factory
3 participants