Skip to content

Commit

Permalink
Make mutiny version of pool use the already configured vertx pool
Browse files Browse the repository at this point in the history
Before this, the mutiny version would recreate the delegate instead
of reusing the existing bean

Fixes: quarkusio#39047
(cherry picked from commit d894149)
  • Loading branch information
geoand authored and gsmet committed Mar 15, 2024
1 parent 455ab13 commit 9655a08
Show file tree
Hide file tree
Showing 14 changed files with 249 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,16 @@

class ReactiveDB2ClientProcessor {

private static final ParameterizedType POOL_INJECTION_TYPE = ParameterizedType.create(DotName.createSimple(Instance.class),
private static final ParameterizedType POOL_CREATOR_INJECTION_TYPE = ParameterizedType.create(
DotName.createSimple(Instance.class),
new Type[] { ClassType.create(DotName.createSimple(DB2PoolCreator.class.getName())) }, null);
private static final AnnotationInstance[] EMPTY_ANNOTATIONS = new AnnotationInstance[0];

private static final DotName REACTIVE_DATASOURCE = DotName.createSimple(ReactiveDataSource.class);

private static final DotName VERTX_DB2_POOL = DotName.createSimple(DB2Pool.class);
private static final Type VERTX_DB2_POOL_TYPE = Type.create(VERTX_DB2_POOL, Type.Kind.CLASS);

@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
ServiceStartBuildItem build(BuildProducer<FeatureBuildItem> feature,
Expand Down Expand Up @@ -208,7 +212,7 @@ private void createPoolIfDefined(DB2PoolRecorder recorder,
.defaultBean()
.addType(Pool.class)
.scope(ApplicationScoped.class)
.addInjectionPoint(POOL_INJECTION_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(POOL_CREATOR_INJECTION_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(ClassType.create(DataSourceSupport.class))
.createWith(poolFunction)
.unremovable()
Expand All @@ -223,9 +227,9 @@ private void createPoolIfDefined(DB2PoolRecorder recorder,
.defaultBean()
.addType(io.vertx.mutiny.sqlclient.Pool.class)
.scope(ApplicationScoped.class)
.addInjectionPoint(POOL_INJECTION_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(VERTX_DB2_POOL_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(ClassType.create(DataSourceSupport.class))
.createWith(recorder.mutinyDB2Pool(poolFunction))
.createWith(recorder.mutinyDB2Pool(dataSourceName))
.unremovable()
.setRuntimeInit();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
import static io.quarkus.vertx.core.runtime.SSLConfigHelper.configurePfxKeyCertOptions;
import static io.quarkus.vertx.core.runtime.SSLConfigHelper.configurePfxTrustOptions;

import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;

import jakarta.enterprise.inject.Default;
import jakarta.enterprise.inject.Instance;
import jakarta.enterprise.util.TypeLiteral;

Expand Down Expand Up @@ -47,7 +49,7 @@
public class DB2PoolRecorder {

private static final Logger log = Logger.getLogger(DB2PoolRecorder.class);
private static final TypeLiteral<Instance<DB2PoolCreator>> TYPE_LITERAL = new TypeLiteral<>() {
private static final TypeLiteral<Instance<DB2PoolCreator>> POOL_CREATOR_TYPE_LITERAL = new TypeLiteral<>() {
};

public Function<SyntheticCreationalContext<DB2Pool>, DB2Pool> configureDB2Pool(RuntimeValue<Vertx> vertx,
Expand Down Expand Up @@ -75,16 +77,30 @@ public DB2Pool apply(SyntheticCreationalContext<DB2Pool> context) {
}

public Function<SyntheticCreationalContext<io.vertx.mutiny.db2client.DB2Pool>, io.vertx.mutiny.db2client.DB2Pool> mutinyDB2Pool(
Function<SyntheticCreationalContext<DB2Pool>, DB2Pool> function) {
String dataSourceName) {
return new Function<>() {
@SuppressWarnings("unchecked")
@Override
public io.vertx.mutiny.db2client.DB2Pool apply(SyntheticCreationalContext context) {
return io.vertx.mutiny.db2client.DB2Pool.newInstance(function.apply(context));
DataSourceSupport datasourceSupport = (DataSourceSupport) context.getInjectedReference(DataSourceSupport.class);
if (datasourceSupport.getInactiveNames().contains(dataSourceName)) {
throw DataSourceUtil.dataSourceInactive(dataSourceName);
}
DB2Pool db2Pool = (DB2Pool) context.getInjectedReference(DB2Pool.class,
getReactiveDataSourceQualifier(dataSourceName));
return io.vertx.mutiny.db2client.DB2Pool.newInstance(db2Pool);
}
};
}

private static Annotation getReactiveDataSourceQualifier(String dataSourceName) {
if (DataSourceUtil.isDefault(dataSourceName)) {
return Default.Literal.INSTANCE;
}

return new ReactiveDataSource.ReactiveDataSourceLiteral(dataSourceName);
}

private DB2Pool initialize(VertxInternal vertx,
Integer eventLoopCount,
String dataSourceName,
Expand Down Expand Up @@ -240,9 +256,9 @@ private DB2Pool createPool(Vertx vertx, PoolOptions poolOptions, DB2ConnectOptio
SyntheticCreationalContext<DB2Pool> context) {
Instance<DB2PoolCreator> instance;
if (DataSourceUtil.isDefault(dataSourceName)) {
instance = context.getInjectedReference(TYPE_LITERAL);
instance = context.getInjectedReference(POOL_CREATOR_TYPE_LITERAL);
} else {
instance = context.getInjectedReference(TYPE_LITERAL,
instance = context.getInjectedReference(POOL_CREATOR_TYPE_LITERAL,
new ReactiveDataSource.ReactiveDataSourceLiteral(dataSourceName));
}
if (instance.isResolvable()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,13 @@

class ReactiveMSSQLClientProcessor {

private static final ParameterizedType POOL_INJECTION_TYPE = ParameterizedType.create(DotName.createSimple(Instance.class),
private static final ParameterizedType POOL_CREATOR_INJECTION_TYPE = ParameterizedType.create(
DotName.createSimple(Instance.class),
new Type[] { ClassType.create(DotName.createSimple(MSSQLPoolCreator.class.getName())) }, null);
private static final AnnotationInstance[] EMPTY_ANNOTATIONS = new AnnotationInstance[0];
private static final DotName REACTIVE_DATASOURCE = DotName.createSimple(ReactiveDataSource.class);
private static final DotName VERTX_MSSQL_POOL = DotName.createSimple(MSSQLPool.class);
private static final Type VERTX_MSSQL_POOL_TYPE = Type.create(VERTX_MSSQL_POOL, Type.Kind.CLASS);

@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
Expand Down Expand Up @@ -207,7 +210,7 @@ private void createPoolIfDefined(MSSQLPoolRecorder recorder,
.defaultBean()
.addType(Pool.class)
.scope(ApplicationScoped.class)
.addInjectionPoint(POOL_INJECTION_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(POOL_CREATOR_INJECTION_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(ClassType.create(DataSourceSupport.class))
.createWith(poolFunction)
.unremovable()
Expand All @@ -222,9 +225,9 @@ private void createPoolIfDefined(MSSQLPoolRecorder recorder,
.defaultBean()
.addType(io.vertx.mutiny.sqlclient.Pool.class)
.scope(ApplicationScoped.class)
.addInjectionPoint(POOL_INJECTION_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(VERTX_MSSQL_POOL_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(ClassType.create(DataSourceSupport.class))
.createWith(recorder.mutinyMSSQLPool(poolFunction))
.createWith(recorder.mutinyMSSQLPool(dataSourceName))
.unremovable()
.setRuntimeInit();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.quarkus.reactive.mssql.client;

import jakarta.inject.Inject;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.ClientProxy;
import io.quarkus.test.QuarkusUnitTest;

public class SamePoolInstanceTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.overrideConfigKey("quarkus.devservices.enabled", "false");

@Inject
io.vertx.mutiny.mssqlclient.MSSQLPool mutinyPool;
@Inject
io.vertx.mssqlclient.MSSQLPool pool;

@Test
public void test() {
Assertions.assertThat(ClientProxy.unwrap(pool)).isSameAs(ClientProxy.unwrap(mutinyPool.getDelegate()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
import static io.quarkus.vertx.core.runtime.SSLConfigHelper.configurePfxKeyCertOptions;
import static io.quarkus.vertx.core.runtime.SSLConfigHelper.configurePfxTrustOptions;

import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;

import jakarta.enterprise.inject.Default;
import jakarta.enterprise.inject.Instance;
import jakarta.enterprise.util.TypeLiteral;

Expand Down Expand Up @@ -46,7 +48,7 @@
@Recorder
public class MSSQLPoolRecorder {

private static final TypeLiteral<Instance<MSSQLPoolCreator>> TYPE_LITERAL = new TypeLiteral<>() {
private static final TypeLiteral<Instance<MSSQLPoolCreator>> POOL_CREATOR_TYPE_LITERAL = new TypeLiteral<>() {
};

private static final Logger log = Logger.getLogger(MSSQLPoolRecorder.class);
Expand Down Expand Up @@ -80,16 +82,31 @@ public MSSQLPool apply(SyntheticCreationalContext<MSSQLPool> context) {
}

public Function<SyntheticCreationalContext<io.vertx.mutiny.mssqlclient.MSSQLPool>, io.vertx.mutiny.mssqlclient.MSSQLPool> mutinyMSSQLPool(
Function<SyntheticCreationalContext<MSSQLPool>, MSSQLPool> function) {
String dataSourceName) {
return new Function<SyntheticCreationalContext<io.vertx.mutiny.mssqlclient.MSSQLPool>, io.vertx.mutiny.mssqlclient.MSSQLPool>() {
@Override
@SuppressWarnings("unchecked")
public io.vertx.mutiny.mssqlclient.MSSQLPool apply(SyntheticCreationalContext context) {
return io.vertx.mutiny.mssqlclient.MSSQLPool.newInstance(function.apply(context));
DataSourceSupport datasourceSupport = (DataSourceSupport) context.getInjectedReference(DataSourceSupport.class);
if (datasourceSupport.getInactiveNames().contains(dataSourceName)) {
throw DataSourceUtil.dataSourceInactive(dataSourceName);
}

return io.vertx.mutiny.mssqlclient.MSSQLPool.newInstance(
(MSSQLPool) context.getInjectedReference(MSSQLPool.class,
getReactiveDataSourceQualifier(dataSourceName)));
}
};
}

private static Annotation getReactiveDataSourceQualifier(String dataSourceName) {
if (DataSourceUtil.isDefault(dataSourceName)) {
return Default.Literal.INSTANCE;
}

return new ReactiveDataSource.ReactiveDataSourceLiteral(dataSourceName);
}

private MSSQLPool initialize(VertxInternal vertx,
Integer eventLoopCount,
String dataSourceName, DataSourceRuntimeConfig dataSourceRuntimeConfig,
Expand Down Expand Up @@ -243,9 +260,9 @@ private MSSQLPool createPool(Vertx vertx, PoolOptions poolOptions, MSSQLConnectO
SyntheticCreationalContext<MSSQLPool> context) {
Instance<MSSQLPoolCreator> instance;
if (DataSourceUtil.isDefault(dataSourceName)) {
instance = context.getInjectedReference(TYPE_LITERAL);
instance = context.getInjectedReference(POOL_CREATOR_TYPE_LITERAL);
} else {
instance = context.getInjectedReference(TYPE_LITERAL,
instance = context.getInjectedReference(POOL_CREATOR_TYPE_LITERAL,
new ReactiveDataSource.ReactiveDataSourceLiteral(dataSourceName));
}
if (instance.isResolvable()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,13 @@

class ReactiveMySQLClientProcessor {

private static final ParameterizedType POOL_INJECTION_TYPE = ParameterizedType.create(DotName.createSimple(Instance.class),
private static final ParameterizedType POOL_CREATOR_INJECTION_TYPE = ParameterizedType.create(
DotName.createSimple(Instance.class),
new Type[] { ClassType.create(DotName.createSimple(MySQLPoolCreator.class.getName())) }, null);
private static final AnnotationInstance[] EMPTY_ANNOTATIONS = new AnnotationInstance[0];
private static final DotName REACTIVE_DATASOURCE = DotName.createSimple(ReactiveDataSource.class);
private static final DotName VERTX_MYSQL_POOL = DotName.createSimple(MySQLPool.class);
private static final Type VERTX_MYSQL_POOL_TYPE = Type.create(VERTX_MYSQL_POOL, Type.Kind.CLASS);

@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
Expand Down Expand Up @@ -208,7 +211,7 @@ private void createPoolIfDefined(MySQLPoolRecorder recorder,
.defaultBean()
.addType(Pool.class)
.scope(ApplicationScoped.class)
.addInjectionPoint(POOL_INJECTION_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(POOL_CREATOR_INJECTION_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(ClassType.create(DataSourceSupport.class))
.createWith(poolFunction)
.unremovable()
Expand All @@ -223,9 +226,9 @@ private void createPoolIfDefined(MySQLPoolRecorder recorder,
.defaultBean()
.addType(io.vertx.mutiny.sqlclient.Pool.class)
.scope(ApplicationScoped.class)
.addInjectionPoint(POOL_INJECTION_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(VERTX_MYSQL_POOL_TYPE, injectionPointAnnotations(dataSourceName))
.addInjectionPoint(ClassType.create(DataSourceSupport.class))
.createWith(recorder.mutinyMySQLPool(poolFunction))
.createWith(recorder.mutinyMySQLPool(dataSourceName))
.unremovable()
.setRuntimeInit();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.quarkus.reactive.mysql.client;

import jakarta.inject.Inject;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.ClientProxy;
import io.quarkus.test.QuarkusUnitTest;

public class SamePoolInstanceTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest();

@Inject
io.vertx.mutiny.mysqlclient.MySQLPool mutinyPool;
@Inject
io.vertx.mysqlclient.MySQLPool pool;

@Test
public void test() {
Assertions.assertThat(ClientProxy.unwrap(pool)).isSameAs(ClientProxy.unwrap(mutinyPool.getDelegate()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
import static io.quarkus.vertx.core.runtime.SSLConfigHelper.configurePfxKeyCertOptions;
import static io.quarkus.vertx.core.runtime.SSLConfigHelper.configurePfxTrustOptions;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Supplier;

import jakarta.enterprise.inject.Default;
import jakarta.enterprise.inject.Instance;
import jakarta.enterprise.util.TypeLiteral;

Expand Down Expand Up @@ -47,7 +49,7 @@
@Recorder
public class MySQLPoolRecorder {

private static final TypeLiteral<Instance<MySQLPoolCreator>> TYPE_LITERAL = new TypeLiteral<>() {
private static final TypeLiteral<Instance<MySQLPoolCreator>> POOL_CREATOR_TYPE_LITERAL = new TypeLiteral<>() {
};

public Function<SyntheticCreationalContext<MySQLPool>, MySQLPool> configureMySQLPool(RuntimeValue<Vertx> vertx,
Expand Down Expand Up @@ -75,16 +77,30 @@ public MySQLPool apply(SyntheticCreationalContext<MySQLPool> context) {
}

public Function<SyntheticCreationalContext<io.vertx.mutiny.mysqlclient.MySQLPool>, io.vertx.mutiny.mysqlclient.MySQLPool> mutinyMySQLPool(
Function<SyntheticCreationalContext<MySQLPool>, MySQLPool> function) {
String dataSourceName) {
return new Function<>() {
@Override
@SuppressWarnings("unchecked")
public io.vertx.mutiny.mysqlclient.MySQLPool apply(SyntheticCreationalContext context) {
return io.vertx.mutiny.mysqlclient.MySQLPool.newInstance(function.apply(context));
DataSourceSupport datasourceSupport = (DataSourceSupport) context.getInjectedReference(DataSourceSupport.class);
if (datasourceSupport.getInactiveNames().contains(dataSourceName)) {
throw DataSourceUtil.dataSourceInactive(dataSourceName);
}
return io.vertx.mutiny.mysqlclient.MySQLPool.newInstance(
(MySQLPool) context.getInjectedReference(MySQLPool.class,
getReactiveDataSourceQualifier(dataSourceName)));
}
};
}

private static Annotation getReactiveDataSourceQualifier(String dataSourceName) {
if (DataSourceUtil.isDefault(dataSourceName)) {
return Default.Literal.INSTANCE;
}

return new ReactiveDataSource.ReactiveDataSourceLiteral(dataSourceName);
}

private MySQLPool initialize(VertxInternal vertx,
Integer eventLoopCount,
String dataSourceName,
Expand Down Expand Up @@ -262,9 +278,9 @@ private MySQLPool createPool(Vertx vertx, PoolOptions poolOptions, List<MySQLCon
SyntheticCreationalContext<MySQLPool> context) {
Instance<MySQLPoolCreator> instance;
if (DataSourceUtil.isDefault(dataSourceName)) {
instance = context.getInjectedReference(TYPE_LITERAL);
instance = context.getInjectedReference(POOL_CREATOR_TYPE_LITERAL);
} else {
instance = context.getInjectedReference(TYPE_LITERAL,
instance = context.getInjectedReference(POOL_CREATOR_TYPE_LITERAL,
new ReactiveDataSource.ReactiveDataSourceLiteral(dataSourceName));
}
if (instance.isResolvable()) {
Expand Down
Loading

0 comments on commit 9655a08

Please sign in to comment.