Skip to content

Commit

Permalink
Dynamic pool configuration for DB2 Client
Browse files Browse the repository at this point in the history
  • Loading branch information
tsegismont committed Mar 21, 2023
1 parent 1040280 commit 91e118f
Showing 1 changed file with 51 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
import static io.quarkus.vertx.core.runtime.SSLConfigHelper.configurePfxKeyCertOptions;
import static io.quarkus.vertx.core.runtime.SSLConfigHelper.configurePfxTrustOptions;

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

import jakarta.enterprise.inject.Instance;

import org.jboss.logging.Logger;

import io.quarkus.arc.Arc;
Expand All @@ -26,14 +25,21 @@
import io.quarkus.reactive.datasource.ReactiveDataSource;
import io.quarkus.reactive.datasource.runtime.DataSourceReactiveRuntimeConfig;
import io.quarkus.reactive.datasource.runtime.DataSourcesReactiveRuntimeConfig;
import io.quarkus.reactive.datasource.runtime.PoolCloseFutureFactory;
import io.quarkus.reactive.datasource.runtime.ReactiveDatasourceConnectionProvider;
import io.quarkus.reactive.datasource.runtime.ReactiveDatasourceCredentialsProvider;
import io.quarkus.reactive.db2.client.DB2PoolCreator;
import io.quarkus.runtime.RuntimeValue;
import io.quarkus.runtime.ShutdownContext;
import io.quarkus.runtime.annotations.Recorder;
import io.vertx.core.Vertx;
import io.vertx.core.impl.CloseFuture;
import io.vertx.core.impl.VertxInternal;
import io.vertx.db2client.DB2ConnectOptions;
import io.vertx.db2client.DB2Pool;
import io.vertx.db2client.spi.DB2Driver;
import io.vertx.sqlclient.PoolOptions;
import jakarta.enterprise.inject.Instance;

@Recorder
public class DB2PoolRecorder {
Expand All @@ -48,7 +54,7 @@ public RuntimeValue<DB2Pool> configureDB2Pool(RuntimeValue<Vertx> vertx,
DataSourcesReactiveDB2Config dataSourcesReactiveDB2Config,
ShutdownContext shutdown) {

DB2Pool db2Pool = initialize(vertx.getValue(),
DB2Pool db2Pool = initialize((VertxInternal) vertx.getValue(),
eventLoopCount.get(),
dataSourceName,
dataSourcesRuntimeConfig.getDataSourceRuntimeConfig(dataSourceName),
Expand All @@ -63,7 +69,7 @@ public RuntimeValue<io.vertx.mutiny.db2client.DB2Pool> mutinyDB2Pool(RuntimeValu
return new RuntimeValue<>(io.vertx.mutiny.db2client.DB2Pool.newInstance(db2Pool.getValue()));
}

private DB2Pool initialize(Vertx vertx,
private DB2Pool initialize(VertxInternal vertx,
Integer eventLoopCount,
String dataSourceName,
DataSourceRuntimeConfig dataSourceRuntimeConfig,
Expand All @@ -73,14 +79,46 @@ private DB2Pool initialize(Vertx vertx,
dataSourceReactiveDB2Config);
DB2ConnectOptions connectOptions = toConnectOptions(dataSourceRuntimeConfig, dataSourceReactiveRuntimeConfig,
dataSourceReactiveDB2Config);

// Use the convention defined by Quarkus Micrometer Vert.x metrics to create metrics prefixed with db2. and
CloseFuture poolCloseFuture = PoolCloseFutureFactory.create(vertx);
ReactiveDatasourceConnectionProvider<DB2ConnectOptions> connectionProvider = toConnectionProvider(vertx,
connectOptions, dataSourceRuntimeConfig, poolCloseFuture); // Use the convention defined by Quarkus Micrometer Vert.x metrics to create metrics prefixed with db2. and
// the client_name as tag.
// See io.quarkus.micrometer.runtime.binder.vertx.VertxMeterBinderAdapter.extractPrefix and
// io.quarkus.micrometer.runtime.binder.vertx.VertxMeterBinderAdapter.extractClientName
connectOptions.setMetricsName("db2|" + dataSourceName);

return createPool(vertx, poolOptions, connectOptions, dataSourceName);
return createPool(vertx, poolOptions, connectOptions, dataSourceName, connectionProvider, poolCloseFuture);
}

private ReactiveDatasourceConnectionProvider<DB2ConnectOptions> toConnectionProvider(VertxInternal vertx,
DB2ConnectOptions connectOptions, DataSourceRuntimeConfig dataSourceRuntimeConfig, CloseFuture poolCloseFuture) {
ReactiveDatasourceCredentialsProvider reactiveDatasourceCredentialsProvider;
if (dataSourceRuntimeConfig.credentialsProvider.isPresent()) {
String beanName = dataSourceRuntimeConfig.credentialsProviderName.orElse(null);
CredentialsProvider credentialsProvider = CredentialsProviderFinder.find(beanName);
String name = dataSourceRuntimeConfig.credentialsProvider.get();
Map<String, String> credentials = credentialsProvider.getCredentials(name);
String user = credentials.get(USER_PROPERTY_NAME);
String password = credentials.get(PASSWORD_PROPERTY_NAME);
if (user != null) {
connectOptions.setUser(user);
}
if (password != null) {
connectOptions.setPassword(password);
}
reactiveDatasourceCredentialsProvider = new ReactiveDatasourceCredentialsProvider(credentialsProvider, name);
} else {
reactiveDatasourceCredentialsProvider = new ReactiveDatasourceCredentialsProvider(new CredentialsProvider() {
@Override
public Map<String, String> getCredentials(String credentialsProviderName) {
return Map.of(USER_PROPERTY_NAME, connectOptions.getUser(), PASSWORD_PROPERTY_NAME,
connectOptions.getPassword());
}
}, null);
}

return new ReactiveDatasourceConnectionProvider<>(vertx, DB2Driver.INSTANCE, reactiveDatasourceCredentialsProvider,
connectOptions, DB2ConnectOptions::new, poolCloseFuture);
}

private PoolOptions toPoolOptions(Integer eventLoopCount,
Expand Down Expand Up @@ -137,22 +175,6 @@ private DB2ConnectOptions toConnectOptions(DataSourceRuntimeConfig dataSourceRun
connectOptions.setPassword(dataSourceRuntimeConfig.password.get());
}

// credentials provider
if (dataSourceRuntimeConfig.credentialsProvider.isPresent()) {
String beanName = dataSourceRuntimeConfig.credentialsProviderName.orElse(null);
CredentialsProvider credentialsProvider = CredentialsProviderFinder.find(beanName);
String name = dataSourceRuntimeConfig.credentialsProvider.get();
Map<String, String> credentials = credentialsProvider.getCredentials(name);
String user = credentials.get(USER_PROPERTY_NAME);
String password = credentials.get(PASSWORD_PROPERTY_NAME);
if (user != null) {
connectOptions.setUser(user);
}
if (password != null) {
connectOptions.setPassword(user);
}
}

connectOptions.setCachePreparedStatements(dataSourceReactiveRuntimeConfig.cachePreparedStatements);

connectOptions.setSsl(dataSourceReactiveDB2Config.ssl);
Expand Down Expand Up @@ -182,7 +204,8 @@ private DB2ConnectOptions toConnectOptions(DataSourceRuntimeConfig dataSourceRun
}

private DB2Pool createPool(Vertx vertx, PoolOptions poolOptions, DB2ConnectOptions dB2ConnectOptions,
String dataSourceName) {
String dataSourceName, ReactiveDatasourceConnectionProvider<DB2ConnectOptions> connectionProvider,
CloseFuture poolCloseFuture) {
Instance<DB2PoolCreator> instance;
if (DataSourceUtil.isDefault(dataSourceName)) {
instance = Arc.container().select(DB2PoolCreator.class);
Expand All @@ -194,7 +217,10 @@ private DB2Pool createPool(Vertx vertx, PoolOptions poolOptions, DB2ConnectOptio
DB2PoolCreator.Input input = new DefaultInput(vertx, poolOptions, dB2ConnectOptions);
return instance.get().create(input);
}
return DB2Pool.pool(vertx, dB2ConnectOptions, poolOptions);
DB2Pool pool = DB2Driver.INSTANCE
.newPool(vertx, Collections.singletonList(dB2ConnectOptions), poolOptions, poolCloseFuture)
.connectionProvider(connectionProvider);
return pool;
}

private static class DefaultInput implements DB2PoolCreator.Input {
Expand Down

0 comments on commit 91e118f

Please sign in to comment.