diff --git a/README.md b/README.md index 3e7dbc8..5f8a7d9 100644 --- a/README.md +++ b/README.md @@ -216,37 +216,26 @@ If the `flexy-pool-spring-boot-starter` is added to the classpath your datasourc With default setting you will start getting messages about acquiring and leasing connections: ```text 2017-07-13 01:31:02.575 INFO 5432 --- [ool-1-worker-50] c.v.flexypool.FlexyPoolDataSource : Connection leased for 1500 millis, while threshold is set to 1000 in dataSource FlexyPoolDataSource -2017-07-13 01:31:03.143 WARN 5432 --- [ool-1-worker-51] PoolOnTimeoutConnectionAcquiringStrategy : Connection was acquired in 1502 millis, timeoutMillis is set to 500 -2017-07-13 01:31:03.143 INFO 5432 --- [ool-1-worker-51] PoolOnTimeoutConnectionAcquiringStrategy : Pool size changed from previous value 10 to 11 -``` -You can declare bean `MetricsFactory` and besides of JMX metrics will be exported to the metrics provider and to the logs: -```text -2017-07-13 02:07:04.265 INFO 5432 --- [rter-1-thread-1] c.v.f.metric.codahale.CodahaleMetrics : type=HISTOGRAM, name=concurrentConnectionRequestsHistogram, count=4, min=0, max=1, mean=0.5, stddev=0.5, median=1.0, p75=1.0, p95=1.0, p98=1.0, p99=1.0, p999=1.0 -2017-07-13 02:07:04.265 INFO 5432 --- [rter-1-thread-1] c.v.f.metric.codahale.CodahaleMetrics : type=HISTOGRAM, name=concurrentConnectionsHistogram, count=4, min=0, max=1, mean=0.5, stddev=0.5, median=1.0, p75=1.0, p95=1.0, p98=1.0, p99=1.0, p999=1.0 -2017-07-13 02:07:04.265 INFO 5432 --- [rter-1-thread-1] c.v.f.metric.codahale.CodahaleMetrics : type=HISTOGRAM, name=maxPoolSizeHistogram, count=1, min=10, max=10, mean=10.0, stddev=0.0, median=10.0, p75=10.0, p95=10.0, p98=10.0, p99=10.0, p999=10.0 -2017-07-13 02:07:04.265 INFO 5432 --- [rter-1-thread-1] c.v.f.metric.codahale.CodahaleMetrics : type=HISTOGRAM, name=overflowPoolSizeHistogram, count=0, min=0, max=0, mean=0.0, stddev=0.0, median=0.0, p75=0.0, p95=0.0, p98=0.0, p99=0.0, p999=0.0 -2017-07-13 02:07:04.265 INFO 5432 --- [rter-1-thread-1] c.v.f.metric.codahale.CodahaleMetrics : type=HISTOGRAM, name=retryAttemptsHistogram, count=0, min=0, max=0, mean=0.0, stddev=0.0, median=0.0, p75=0.0, p95=0.0, p98=0.0, p99=0.0, p999=0.0 -2017-07-13 02:07:04.265 INFO 5432 --- [rter-1-thread-1] c.v.f.metric.codahale.CodahaleMetrics : type=TIMER, name=connectionAcquireMillis, count=2, min=0.0, max=39.0, mean=19.5, stddev=19.5, median=39.0, p75=39.0, p95=39.0, p98=39.0, p99=39.0, p999=39.0, mean_rate=0.07135042014375073, m1=0.02490778899904623, m5=0.006288975787638508, m15=0.002179432534806779, rate_unit=events/second, duration_unit=milliseconds -2017-07-13 02:07:04.265 INFO 5432 --- [rter-1-thread-1] c.v.f.metric.codahale.CodahaleMetrics : type=TIMER, name=connectionLeaseMillis, count=2, min=3.0, max=7.0, mean=5.0, stddev=2.0, median=7.0, p75=7.0, p95=7.0, p98=7.0, p99=7.0, p999=7.0, mean_rate=0.07135743555785098, m1=0.02490778899904623, m5=0.006288975787638508, m15=0.002179432534806779, rate_unit=events/second, duration_unit=milliseconds -2017-07-13 02:07:04.265 INFO 5432 --- [rter-1-thread-1] c.v.f.metric.codahale.CodahaleMetrics : type=TIMER, name=overallConnectionAcquireMillis, count=2, min=0.0, max=39.0, mean=19.5, stddev=19.5, median=39.0, p75=39.0, p95=39.0, p98=39.0, p99=39.0, p999=39.0, mean_rate=0.07135462550886962, m1=0.02490778899904623, m5=0.006288975787638508, m15=0.002179432534806779, rate_unit=events/second, duration_unit=milliseconds +2017-07-13 01:31:03.143 WARN 5432 --- [ool-1-worker-51] PoolOnTimeoutConnectionAcquisitionStrategy : Connection was acquired in 1502 millis, timeoutMillis is set to 500 +2017-07-13 01:31:03.143 INFO 5432 --- [ool-1-worker-51] PoolOnTimeoutConnectionAcquisitionStrategy : Pool size changed from previous value 10 to 11 ``` -All beans of type `ConnectionAcquiringStrategyFactory` are used to provide `ConnectionAcquiringStrategy` for the pool. +All beans of type `ConnectionAcquisitionStrategyFactory` are used to provide `ConnectionAcquisitionStrategy` for the pool. `MetricsFactory` and `ConnectionProxyFactory` beans can be used to customize metrics and connection decorators. -`EventListener` beans can be registered to subscribe on events of flexy-pool (e.g. `ConnectionAcquireTimeThresholdExceededEvent`, `ConnectionLeaseTimeThresholdExceededEvent`). +`EventListener` beans can be registered to subscribe on events of flexy-pool (e.g. `ConnectionAcquisitionTimeThresholdExceededEvent`, `ConnectionLeaseTimeThresholdExceededEvent`). You can configure your `FlexyPoolDataSource` by using bean `FlexyPoolConfigurationBuilderCustomizer` or properties: > [!NOTE] > Configuration below indicates al possible parameters together with their default values and **does not** need to be set explicitly ```properties -# Increments pool size if connection acquire request has timed out -decorator.datasource.flexy-pool.acquiring-strategy.increment-pool.max-overflow-pool-size=15 -decorator.datasource.flexy-pool.acquiring-strategy.increment-pool.timeout-millis=500 +# Increments pool size if connection acquisition request has timed out +decorator.datasource.flexy-pool.acquisition-strategy.increment-pool.max-overgrow-pool-size=15 +decorator.datasource.flexy-pool.acquisition-strategy.increment-pool.timeout-millis=500 # Retries on getting connection -decorator.datasource.flexy-pool.acquiring-strategy.retry.attempts=2 +decorator.datasource.flexy-pool.acquisition-strategy.retry.attempts=2 # Enable metrics exporting to the JMX decorator.datasource.flexy-pool.metrics.reporter.jmx.enabled=true @@ -255,8 +244,8 @@ decorator.datasource.flexy-pool.metrics.reporter.jmx.auto-start=false # Millis between two consecutive log reports decorator.datasource.flexy-pool.metrics.reporter.log.millis=300000 -# Enable logging and publishing ConnectionAcquireTimeThresholdExceededEvent when a connection acquire request has timed out -decorator.datasource.flexy-pool.threshold.connection.acquire=50 +# Enable logging and publishing ConnectionAcquisitionTimeThresholdExceededEvent when a connection acquisition request has timed out +decorator.datasource.flexy-pool.threshold.connection.acquisition=50 # Enable logging and publishing ConnectionLeaseTimeThresholdExceededEvent when a connection lease has exceeded the given time threshold decorator.datasource.flexy-pool.threshold.connection.lease=1000 ``` diff --git a/datasource-decorator-spring-boot-autoconfigure/src/main/java/com/github/gavlyukovskiy/boot/jdbc/decorator/flexypool/FlexyPoolConfiguration.java b/datasource-decorator-spring-boot-autoconfigure/src/main/java/com/github/gavlyukovskiy/boot/jdbc/decorator/flexypool/FlexyPoolConfiguration.java index 761f23e..c1412ae 100644 --- a/datasource-decorator-spring-boot-autoconfigure/src/main/java/com/github/gavlyukovskiy/boot/jdbc/decorator/flexypool/FlexyPoolConfiguration.java +++ b/datasource-decorator-spring-boot-autoconfigure/src/main/java/com/github/gavlyukovskiy/boot/jdbc/decorator/flexypool/FlexyPoolConfiguration.java @@ -17,8 +17,6 @@ package com.github.gavlyukovskiy.boot.jdbc.decorator.flexypool; import com.github.gavlyukovskiy.boot.jdbc.decorator.DataSourceDecoratorProperties; -import com.github.gavlyukovskiy.boot.jdbc.decorator.flexypool.FlexyPoolProperties.AcquiringStrategy.IncrementPool; -import com.github.gavlyukovskiy.boot.jdbc.decorator.flexypool.FlexyPoolProperties.AcquiringStrategy.Retry; import com.vladmihalcea.flexypool.FlexyPoolDataSource; import com.vladmihalcea.flexypool.adaptor.DBCP2PoolAdapter; import com.vladmihalcea.flexypool.adaptor.HikariCPPoolAdapter; @@ -29,9 +27,9 @@ import com.vladmihalcea.flexypool.event.EventListener; import com.vladmihalcea.flexypool.metric.MetricsFactory; import com.vladmihalcea.flexypool.metric.micrometer.MicrometerMetrics; -import com.vladmihalcea.flexypool.strategy.ConnectionAcquiringStrategyFactory; -import com.vladmihalcea.flexypool.strategy.IncrementPoolOnTimeoutConnectionAcquiringStrategy; -import com.vladmihalcea.flexypool.strategy.RetryConnectionAcquiringStrategy; +import com.vladmihalcea.flexypool.strategy.ConnectionAcquisitionStrategyFactory; +import com.vladmihalcea.flexypool.strategy.IncrementPoolOnTimeoutConnectionAcquisitionStrategy; +import com.vladmihalcea.flexypool.strategy.RetryConnectionAcquisitionStrategy; import com.vladmihalcea.flexypool.util.ClassLoaderUtils; import com.zaxxer.hikari.HikariDataSource; import org.apache.commons.dbcp2.BasicDataSource; @@ -60,7 +58,7 @@ import static org.slf4j.LoggerFactory.getLogger; /** - * Configuration for integration with flexy-pool, allows to use define custom {@link ConnectionAcquiringStrategyFactory}, + * Configuration for integration with flexy-pool, allows to use define custom {@link ConnectionAcquisitionStrategyFactory}, * {@link MetricsFactory}, {@link ConnectionProxyFactory} and {@link EventListener}. * * @author Arthur Gavlyukovskiy @@ -68,9 +66,9 @@ */ public class FlexyPoolConfiguration { - static List> mergeFactories( - List> factories, FlexyPoolProperties flexyPool) { - List> newFactories = new ArrayList<>(); + static List> mergeFactories( + List> factories, FlexyPoolProperties flexyPool) { + List> newFactories = new ArrayList<>(); List> factoryClasses; if (factories != null) { factoryClasses = factories.stream().map(Object::getClass).collect(Collectors.toList()); @@ -79,17 +77,23 @@ static List> mer else { factoryClasses = Collections.emptyList(); } - if (!factoryClasses.contains(IncrementPoolOnTimeoutConnectionAcquiringStrategy.Factory.class)) { - IncrementPool incrementPool = flexyPool.getAcquiringStrategy().getIncrementPool(); - if (incrementPool.getMaxOverflowPoolSize() > 0) { - newFactories.add(new IncrementPoolOnTimeoutConnectionAcquiringStrategy.Factory<>( - incrementPool.getMaxOverflowPoolSize(), incrementPool.getTimeoutMillis())); + if (!factoryClasses.contains(IncrementPoolOnTimeoutConnectionAcquisitionStrategy.Factory.class)) { + // check deprecated properties first + if (flexyPool.getAcquiringStrategy().getIncrementPool().getMaxOverflowPoolSize() != null) { + newFactories.add(new IncrementPoolOnTimeoutConnectionAcquisitionStrategy.Factory<>( + flexyPool.getAcquiringStrategy().getIncrementPool().getMaxOverflowPoolSize(), + flexyPool.getAcquiringStrategy().getIncrementPool().getTimeoutMillis())); + } else if (flexyPool.getAcquisitionStrategy().getIncrementPool().getMaxOvergrowPoolSize() > 0) { + newFactories.add(new IncrementPoolOnTimeoutConnectionAcquisitionStrategy.Factory<>( + flexyPool.getAcquisitionStrategy().getIncrementPool().getMaxOvergrowPoolSize(), flexyPool.getAcquisitionStrategy().getIncrementPool().getTimeoutMillis())); } } - if (!factoryClasses.contains(RetryConnectionAcquiringStrategy.Factory.class)) { - Retry retry = flexyPool.getAcquiringStrategy().getRetry(); - if (retry.getAttempts() > 0) { - newFactories.add(new RetryConnectionAcquiringStrategy.Factory<>(retry.getAttempts())); + if (!factoryClasses.contains(RetryConnectionAcquisitionStrategy.Factory.class)) { + // check deprecated properties first + if (flexyPool.getAcquiringStrategy().getRetry().getAttempts() != null) { + newFactories.add(new RetryConnectionAcquisitionStrategy.Factory<>(flexyPool.getAcquiringStrategy().getRetry().getAttempts())); + } else if (flexyPool.getAcquisitionStrategy().getRetry().getAttempts() > 0) { + newFactories.add(new RetryConnectionAcquisitionStrategy.Factory<>(flexyPool.getAcquisitionStrategy().getRetry().getAttempts())); } } return newFactories; @@ -126,7 +130,7 @@ public FlexyPoolConfigurationBuilderCustomizer flexyPoolConfigurationBuilderCust builder.setMetricLogReporterMillis(flexyPool.getMetrics().getReporter().getLog().getMillis()); builder.setJmxEnabled(flexyPool.getMetrics().getReporter().getJmx().isEnabled()); builder.setJmxAutoStart(flexyPool.getMetrics().getReporter().getJmx().isAutoStart()); - builder.setConnectionAcquireTimeThresholdMillis(flexyPool.getThreshold().getConnection().getAcquire()); + builder.setConnectionAcquisitionTimeThresholdMillis(flexyPool.getThreshold().getConnection().getAcquisition()); builder.setConnectionLeaseTimeThresholdMillis(flexyPool.getThreshold().getConnection().getLease()); if (metricsFactory != null) { builder.setMetricsFactory(metricsFactory); @@ -148,14 +152,14 @@ public FlexyPoolConfigurationBuilderCustomizer flexyPoolConfigurationBuilderCust static class HikariFlexyConfiguration { @Autowired(required = false) - private List> connectionAcquiringStrategyFactories; + private List> ConnectionAcquisitionStrategyFactories; @Autowired private DataSourceDecoratorProperties dataSourceDecoratorProperties; @Bean public FlexyPoolDataSourceDecorator flexyPoolDataSourceDecorator() { return new FlexyPoolDataSourceDecorator( - mergeFactories(connectionAcquiringStrategyFactories, dataSourceDecoratorProperties.getFlexyPool()), + mergeFactories(ConnectionAcquisitionStrategyFactories, dataSourceDecoratorProperties.getFlexyPool()), HikariCPPoolAdapter.FACTORY, HikariDataSource.class); } } @@ -167,14 +171,14 @@ public FlexyPoolDataSourceDecorator flexyPoolDataSourceDecorator() { static class TomcatFlexyConfiguration { @Autowired(required = false) - private List> connectionAcquiringStrategyFactories; + private List> ConnectionAcquisitionStrategyFactories; @Autowired private DataSourceDecoratorProperties dataSourceDecoratorProperties; @Bean public FlexyPoolDataSourceDecorator flexyPoolDataSourceDecorator() { return new FlexyPoolDataSourceDecorator( - mergeFactories(connectionAcquiringStrategyFactories, dataSourceDecoratorProperties.getFlexyPool()), + mergeFactories(ConnectionAcquisitionStrategyFactories, dataSourceDecoratorProperties.getFlexyPool()), TomcatCPPoolAdapter.FACTORY, org.apache.tomcat.jdbc.pool.DataSource.class); } } @@ -186,14 +190,14 @@ public FlexyPoolDataSourceDecorator flexyPoolDataSourceDecorator() { static class Dbcp2FlexyConfiguration { @Autowired(required = false) - private List> connectionAcquiringStrategyFactories; + private List> ConnectionAcquisitionStrategyFactories; @Autowired private DataSourceDecoratorProperties dataSourceDecoratorProperties; @Bean public FlexyPoolDataSourceDecorator flexyPoolDataSourceDecorator() { return new FlexyPoolDataSourceDecorator( - mergeFactories(connectionAcquiringStrategyFactories, dataSourceDecoratorProperties.getFlexyPool()), + mergeFactories(ConnectionAcquisitionStrategyFactories, dataSourceDecoratorProperties.getFlexyPool()), DBCP2PoolAdapter.FACTORY, BasicDataSource.class); } } @@ -206,12 +210,12 @@ static class PropertyFlexyConfiguration { private static final Logger log = getLogger(PropertyFlexyConfiguration.class); @Autowired(required = false) - private List> connectionAcquiringStrategyFactories; + private List> ConnectionAcquisitionStrategyFactories; @PostConstruct public void warnIfAnyStrategyFound() { - if (connectionAcquiringStrategyFactories != null) { - log.warn("ConnectionAcquiringStrategyFactory beans found in the context will not be applied to " + + if (ConnectionAcquisitionStrategyFactories != null) { + log.warn("ConnectionAcquisitionStrategyFactory beans found in the context will not be applied to " + "FlexyDataSource due to property based configuration of FlexyPool"); } } diff --git a/datasource-decorator-spring-boot-autoconfigure/src/main/java/com/github/gavlyukovskiy/boot/jdbc/decorator/flexypool/FlexyPoolConfigurationBuilderCustomizer.java b/datasource-decorator-spring-boot-autoconfigure/src/main/java/com/github/gavlyukovskiy/boot/jdbc/decorator/flexypool/FlexyPoolConfigurationBuilderCustomizer.java index 9e3b74a..22ed919 100644 --- a/datasource-decorator-spring-boot-autoconfigure/src/main/java/com/github/gavlyukovskiy/boot/jdbc/decorator/flexypool/FlexyPoolConfigurationBuilderCustomizer.java +++ b/datasource-decorator-spring-boot-autoconfigure/src/main/java/com/github/gavlyukovskiy/boot/jdbc/decorator/flexypool/FlexyPoolConfigurationBuilderCustomizer.java @@ -16,17 +16,17 @@ package com.github.gavlyukovskiy.boot.jdbc.decorator.flexypool; -import com.vladmihalcea.flexypool.config.Configuration; +import com.vladmihalcea.flexypool.config.FlexyPoolConfiguration; import javax.sql.DataSource; /** - * Customizer for each {@link Configuration.Builder} when real {@link DataSource} is decorated using {@link FlexyPoolDataSourceDecorator}. + * Customizer for each {@link FlexyPoolConfiguration.Builder} when real {@link DataSource} is decorated using {@link FlexyPoolDataSourceDecorator}. * * @author Arthur Gavlyukovskiy * @since 1.1 */ public interface FlexyPoolConfigurationBuilderCustomizer { - void customize(String beanName, Configuration.Builder builder, Class dataSourceClass); + void customize(String beanName, FlexyPoolConfiguration.Builder builder, Class dataSourceClass); } diff --git a/datasource-decorator-spring-boot-autoconfigure/src/main/java/com/github/gavlyukovskiy/boot/jdbc/decorator/flexypool/FlexyPoolDataSourceDecorator.java b/datasource-decorator-spring-boot-autoconfigure/src/main/java/com/github/gavlyukovskiy/boot/jdbc/decorator/flexypool/FlexyPoolDataSourceDecorator.java index d6c09d2..a0beea6 100644 --- a/datasource-decorator-spring-boot-autoconfigure/src/main/java/com/github/gavlyukovskiy/boot/jdbc/decorator/flexypool/FlexyPoolDataSourceDecorator.java +++ b/datasource-decorator-spring-boot-autoconfigure/src/main/java/com/github/gavlyukovskiy/boot/jdbc/decorator/flexypool/FlexyPoolDataSourceDecorator.java @@ -19,8 +19,8 @@ import com.github.gavlyukovskiy.boot.jdbc.decorator.DataSourceDecorator; import com.vladmihalcea.flexypool.FlexyPoolDataSource; import com.vladmihalcea.flexypool.adaptor.PoolAdapterFactory; -import com.vladmihalcea.flexypool.config.Configuration; -import com.vladmihalcea.flexypool.strategy.ConnectionAcquiringStrategyFactory; +import com.vladmihalcea.flexypool.config.FlexyPoolConfiguration; +import com.vladmihalcea.flexypool.strategy.ConnectionAcquisitionStrategyFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.Ordered; @@ -35,7 +35,7 @@ */ public class FlexyPoolDataSourceDecorator implements DataSourceDecorator, Ordered { - private final ConnectionAcquiringStrategyFactory[] connectionAcquiringStrategyFactories; + private final ConnectionAcquisitionStrategyFactory[] ConnectionAcquisitionStrategyFactories; private final PoolAdapterFactory poolAdapterFactory; private final Class dataSourceClass; @@ -44,17 +44,17 @@ public class FlexyPoolDataSourceDecorator implements DataSourceDecorator, Ordere @SuppressWarnings("unchecked") FlexyPoolDataSourceDecorator( - List> connectionAcquiringStrategyFactories, + List> ConnectionAcquisitionStrategyFactories, PoolAdapterFactory poolAdapterFactory, Class dataSourceClass) { - this.connectionAcquiringStrategyFactories = (ConnectionAcquiringStrategyFactory[]) - connectionAcquiringStrategyFactories.toArray(new ConnectionAcquiringStrategyFactory[0]); + this.ConnectionAcquisitionStrategyFactories = (ConnectionAcquisitionStrategyFactory[]) + ConnectionAcquisitionStrategyFactories.toArray(new ConnectionAcquisitionStrategyFactory[0]); this.poolAdapterFactory = (PoolAdapterFactory) poolAdapterFactory; this.dataSourceClass = (Class) dataSourceClass; } FlexyPoolDataSourceDecorator() { - connectionAcquiringStrategyFactories = null; + ConnectionAcquisitionStrategyFactories = null; poolAdapterFactory = null; dataSourceClass = null; } @@ -68,7 +68,7 @@ public DataSource decorate(String beanName, DataSource dataSource) { return flexyPoolDataSource; } if (dataSourceClass.isInstance(dataSource)) { - Configuration.Builder configurationBuilder = new Configuration.Builder<>( + FlexyPoolConfiguration.Builder configurationBuilder = new FlexyPoolConfiguration.Builder<>( beanName, dataSourceClass.cast(dataSource), poolAdapterFactory @@ -76,7 +76,7 @@ public DataSource decorate(String beanName, DataSource dataSource) { if (customizers != null) { customizers.forEach(customizer -> customizer.customize(beanName, configurationBuilder, dataSourceClass)); } - FlexyPoolDataSource flexyPoolDataSource = new FlexyPoolDataSource<>(configurationBuilder.build(), connectionAcquiringStrategyFactories); + FlexyPoolDataSource flexyPoolDataSource = new FlexyPoolDataSource<>(configurationBuilder.build(), ConnectionAcquisitionStrategyFactories); flexyPoolDataSource.start(); return flexyPoolDataSource; } diff --git a/datasource-decorator-spring-boot-autoconfigure/src/main/java/com/github/gavlyukovskiy/boot/jdbc/decorator/flexypool/FlexyPoolProperties.java b/datasource-decorator-spring-boot-autoconfigure/src/main/java/com/github/gavlyukovskiy/boot/jdbc/decorator/flexypool/FlexyPoolProperties.java index 43f1466..3053ba2 100644 --- a/datasource-decorator-spring-boot-autoconfigure/src/main/java/com/github/gavlyukovskiy/boot/jdbc/decorator/flexypool/FlexyPoolProperties.java +++ b/datasource-decorator-spring-boot-autoconfigure/src/main/java/com/github/gavlyukovskiy/boot/jdbc/decorator/flexypool/FlexyPoolProperties.java @@ -16,6 +16,8 @@ package com.github.gavlyukovskiy.boot.jdbc.decorator.flexypool; +import org.springframework.boot.context.properties.DeprecatedConfigurationProperty; + /** * Properties for configuring flexy-pool. * @@ -24,13 +26,14 @@ */ public class FlexyPoolProperties { + private AcquisitionStrategy acquisitionStrategy = new AcquisitionStrategy(); private AcquiringStrategy acquiringStrategy = new AcquiringStrategy(); private Metrics metrics = new Metrics(); private Threshold threshold = new Threshold(); - public AcquiringStrategy getAcquiringStrategy() { - return this.acquiringStrategy; + public AcquisitionStrategy getAcquisitionStrategy() { + return acquisitionStrategy; } public Metrics getMetrics() { @@ -41,6 +44,16 @@ public Threshold getThreshold() { return this.threshold; } + public void setAcquisitionStrategy(AcquisitionStrategy acquisitionStrategy) { + this.acquisitionStrategy = acquisitionStrategy; + } + + @Deprecated(since = "1.10.0", forRemoval = true) + public AcquiringStrategy getAcquiringStrategy() { + return acquiringStrategy; + } + + @Deprecated(since = "1.10.0", forRemoval = true) public void setAcquiringStrategy(AcquiringStrategy acquiringStrategy) { this.acquiringStrategy = acquiringStrategy; } @@ -53,7 +66,7 @@ public void setThreshold(Threshold threshold) { this.threshold = threshold; } - public static class AcquiringStrategy { + public static class AcquisitionStrategy { private Retry retry = new Retry(); private IncrementPool incrementPool = new IncrementPool(); @@ -86,24 +99,109 @@ public void setAttempts(int attempts) { } public static class IncrementPool { - private int maxOverflowPoolSize = 15; + private int maxOvergrowPoolSize = 15; private int timeoutMillis = 500; - public int getMaxOverflowPoolSize() { - return this.maxOverflowPoolSize; + public int getMaxOvergrowPoolSize() { + return this.maxOvergrowPoolSize; } public int getTimeoutMillis() { return this.timeoutMillis; } - public void setMaxOverflowPoolSize(int maxOverflowPoolSize) { - this.maxOverflowPoolSize = maxOverflowPoolSize; + public void setMaxOvergrowPoolSize(int maxOvergrowPoolSize) { + this.maxOvergrowPoolSize = maxOvergrowPoolSize; + } + + public void setTimeoutMillis(int timeoutMillis) { + this.timeoutMillis = timeoutMillis; + } + } + } + + @Deprecated(since = "1.10.0", forRemoval = true) + public static class AcquiringStrategy { + @Deprecated(since = "1.10.0", forRemoval = true) + private Retry retry = new Retry(); + @Deprecated(since = "1.10.0", forRemoval = true) + private IncrementPool incrementPool = new IncrementPool(); + + @Deprecated(since = "1.10.0", forRemoval = true) + public Retry getRetry() { + return this.retry; + } + + @Deprecated(since = "1.10.0", forRemoval = true) + public IncrementPool getIncrementPool() { + return this.incrementPool; + } + + @Deprecated(since = "1.10.0", forRemoval = true) + public void setRetry(Retry retry) { + this.retry = retry; + } + + @Deprecated(since = "1.10.0", forRemoval = true) + public void setIncrementPool(IncrementPool incrementPool) { + this.incrementPool = incrementPool; + } + + public static class Retry { + @Deprecated(since = "1.10.0", forRemoval = true) + private Integer attempts; + + @Deprecated(since = "1.10.0", forRemoval = true) + @DeprecatedConfigurationProperty( + reason = "FlexyPool 3.0 has renamed this property", + replacement = "decorator.datasource.flexy-pool.acquisition-strategy.retry.attempts", + since = "1.10.0" + ) + public Integer getAttempts() { + return this.attempts; + } + + @Deprecated(since = "1.10.0", forRemoval = true) + public void setAttempts(int attempts) { + this.attempts = attempts; + } + } + + public static class IncrementPool { + @Deprecated(since = "1.10.0", forRemoval = true) + private Integer maxOverflowPoolSize; + @Deprecated(since = "1.10.0", forRemoval = true) + private Integer timeoutMillis; + + @Deprecated(since = "1.10.0", forRemoval = true) + @DeprecatedConfigurationProperty( + reason = "FlexyPool 3.0 has renamed this property", + replacement = "decorator.datasource.flexy-pool.acquisition-strategy.increment-pool.timeout-millis", + since = "1.10.0" + ) + public Integer getTimeoutMillis() { + return this.timeoutMillis; } + @Deprecated(since = "1.10.0", forRemoval = true) + @DeprecatedConfigurationProperty( + reason = "FlexyPool 3.0 has renamed this property", + replacement = "decorator.datasource.flexy-pool.acquisition-strategy.increment-pool.max-overgrow-pool-size", + since = "1.10.0" + ) + public Integer getMaxOverflowPoolSize() { + return this.maxOverflowPoolSize; + } + + @Deprecated(since = "1.10.0", forRemoval = true) public void setTimeoutMillis(int timeoutMillis) { this.timeoutMillis = timeoutMillis; } + + @Deprecated(since = "1.10.0", forRemoval = true) + public void setMaxOverflowPoolSize(int maxOverflowPoolSize) { + this.maxOverflowPoolSize = maxOverflowPoolSize; + } } } @@ -185,24 +283,40 @@ public void setConnection(Connection connection) { } public static class Connection { - private long acquire = 50L; + private long acquisition = 50L; private long lease = 1000L; - public long getAcquire() { - return this.acquire; + public long getAcquisition() { + return this.acquisition; } public long getLease() { return this.lease; } - public void setAcquire(long acquire) { - this.acquire = acquire; + public void setAcquisition(long acquisition) { + this.acquisition = acquisition; } public void setLease(long lease) { this.lease = lease; } + + @Deprecated(since = "1.10.0", forRemoval = true) + @DeprecatedConfigurationProperty( + reason = "FlexyPool 3.0 has renamed this property", + replacement = "decorator.datasource.flexy-pool.threshold.connection.acquisition", + since = "1.10.0" + ) + public long getAcquire() { + return this.acquisition; + } + + + @Deprecated(since = "1.10.0", forRemoval = true) + public void setAcquire(long acquire) { + this.acquisition = acquire; + } } } } diff --git a/datasource-decorator-spring-boot-autoconfigure/src/test/java/com/github/gavlyukovskiy/boot/jdbc/decorator/DataSourceDecoratorAutoConfigurationTests.java b/datasource-decorator-spring-boot-autoconfigure/src/test/java/com/github/gavlyukovskiy/boot/jdbc/decorator/DataSourceDecoratorAutoConfigurationTests.java index 74a90ce..c8e9667 100644 --- a/datasource-decorator-spring-boot-autoconfigure/src/test/java/com/github/gavlyukovskiy/boot/jdbc/decorator/DataSourceDecoratorAutoConfigurationTests.java +++ b/datasource-decorator-spring-boot-autoconfigure/src/test/java/com/github/gavlyukovskiy/boot/jdbc/decorator/DataSourceDecoratorAutoConfigurationTests.java @@ -288,6 +288,12 @@ private AbstractListAssert, Object, ObjectAssert> assertThatD return assertThat(((DecoratedDataSource) dataSource).getDecoratingChain()).extracting("dataSource").extracting("class"); } + public static void main(String[] args) { + var size = 8192; + + System.out.println(size >> 2); + } + @Configuration(proxyBeanMethods = false) static class TestDataSourceConfiguration { diff --git a/datasource-decorator-spring-boot-autoconfigure/src/test/java/com/github/gavlyukovskiy/boot/jdbc/decorator/flexypool/FlexyPoolConfigurationTests.java b/datasource-decorator-spring-boot-autoconfigure/src/test/java/com/github/gavlyukovskiy/boot/jdbc/decorator/flexypool/FlexyPoolConfigurationTests.java index 7c3cbda..d954f71 100644 --- a/datasource-decorator-spring-boot-autoconfigure/src/test/java/com/github/gavlyukovskiy/boot/jdbc/decorator/flexypool/FlexyPoolConfigurationTests.java +++ b/datasource-decorator-spring-boot-autoconfigure/src/test/java/com/github/gavlyukovskiy/boot/jdbc/decorator/flexypool/FlexyPoolConfigurationTests.java @@ -22,10 +22,10 @@ import com.vladmihalcea.flexypool.FlexyPoolDataSource; import com.vladmihalcea.flexypool.connection.ConnectionRequestContext; import com.vladmihalcea.flexypool.metric.micrometer.MicrometerMetrics; -import com.vladmihalcea.flexypool.strategy.ConnectionAcquiringStrategy; -import com.vladmihalcea.flexypool.strategy.ConnectionAcquiringStrategyFactory; -import com.vladmihalcea.flexypool.strategy.IncrementPoolOnTimeoutConnectionAcquiringStrategy; -import com.vladmihalcea.flexypool.strategy.RetryConnectionAcquiringStrategy; +import com.vladmihalcea.flexypool.strategy.ConnectionAcquisitionStrategy; +import com.vladmihalcea.flexypool.strategy.ConnectionAcquisitionStrategyFactory; +import com.vladmihalcea.flexypool.strategy.IncrementPoolOnTimeoutConnectionAcquisitionStrategy; +import com.vladmihalcea.flexypool.strategy.RetryConnectionAcquisitionStrategy; import com.zaxxer.hikari.HikariDataSource; import org.apache.commons.dbcp2.BasicDataSource; import org.junit.jupiter.api.Test; @@ -96,14 +96,14 @@ void testDecoratingHikariDataSourceWithDefaultStrategies() { DataSource dataSource = context.getBean(DataSource.class); assertDataSourceOfType(dataSource, HikariDataSource.class); FlexyPoolDataSource flexyPoolDataSource = assertDataSourceOfType(dataSource, HikariDataSource.class); - IncrementPoolOnTimeoutConnectionAcquiringStrategy strategy1 = - findStrategy(flexyPoolDataSource, IncrementPoolOnTimeoutConnectionAcquiringStrategy.class); + IncrementPoolOnTimeoutConnectionAcquisitionStrategy strategy1 = + findStrategy(flexyPoolDataSource, IncrementPoolOnTimeoutConnectionAcquisitionStrategy.class); assertThat(strategy1).isNotNull(); - assertThat(strategy1).hasFieldOrPropertyWithValue("maxOverflowPoolSize", 15); + assertThat(strategy1).hasFieldOrPropertyWithValue("maxOvergrowPoolSize", 15); assertThat(strategy1).hasFieldOrPropertyWithValue("timeoutMillis", 500); - RetryConnectionAcquiringStrategy strategy2 = - findStrategy(flexyPoolDataSource, RetryConnectionAcquiringStrategy.class); + RetryConnectionAcquisitionStrategy strategy2 = + findStrategy(flexyPoolDataSource, RetryConnectionAcquisitionStrategy.class); assertThat(strategy2).isNotNull(); assertThat(strategy2).hasFieldOrPropertyWithValue("retryAttempts", 2); }); @@ -113,22 +113,47 @@ void testDecoratingHikariDataSourceWithDefaultStrategies() { @SuppressWarnings("unchecked") void testDecoratingHikariDataSourceWithCustomPropertyStrategies() { ApplicationContextRunner contextRunner = this.contextRunner.withPropertyValues("spring.datasource.type:" + HikariDataSource.class.getName(), - "decorator.datasource.flexy-pool.acquiring-strategy.increment-pool.max-overflow-pool-size:15", - "decorator.datasource.flexy-pool.acquiring-strategy.increment-pool.timeout-millis:500", + "decorator.datasource.flexy-pool.acquisition-strategy.increment-pool.max-overgrow-pool-size:35", + "decorator.datasource.flexy-pool.acquisition-strategy.increment-pool.timeout-millis:10000", + "decorator.datasource.flexy-pool.acquisition-strategy.retry.attempts:5") + .withUserConfiguration(FlexyPoolHikariConfiguration.class); + + contextRunner.run(context -> { + DataSource dataSource = context.getBean(DataSource.class); + FlexyPoolDataSource flexyPoolDataSource = assertDataSourceOfType(dataSource, HikariDataSource.class); + IncrementPoolOnTimeoutConnectionAcquisitionStrategy strategy1 = + findStrategy(flexyPoolDataSource, IncrementPoolOnTimeoutConnectionAcquisitionStrategy.class); + assertThat(strategy1).isNotNull(); + assertThat(strategy1).hasFieldOrPropertyWithValue("maxOvergrowPoolSize", 35); + assertThat(strategy1).hasFieldOrPropertyWithValue("timeoutMillis", 10000); + + RetryConnectionAcquisitionStrategy strategy2 = + findStrategy(flexyPoolDataSource, RetryConnectionAcquisitionStrategy.class); + assertThat(strategy2).isNotNull(); + assertThat(strategy2).hasFieldOrPropertyWithValue("retryAttempts", 5); + }); + } + + @Test + @SuppressWarnings("unchecked") + void testDecoratingHikariDataSourceWithDeprecatedProperties() { + ApplicationContextRunner contextRunner = this.contextRunner.withPropertyValues("spring.datasource.type:" + HikariDataSource.class.getName(), + "decorator.datasource.flexy-pool.acquiring-strategy.increment-pool.max-overflow-pool-size:35", + "decorator.datasource.flexy-pool.acquiring-strategy.increment-pool.timeout-millis:10000", "decorator.datasource.flexy-pool.acquiring-strategy.retry.attempts:5") - .withUserConfiguration(HikariConfiguration.class); + .withUserConfiguration(FlexyPoolHikariConfiguration.class); contextRunner.run(context -> { DataSource dataSource = context.getBean(DataSource.class); FlexyPoolDataSource flexyPoolDataSource = assertDataSourceOfType(dataSource, HikariDataSource.class); - IncrementPoolOnTimeoutConnectionAcquiringStrategy strategy1 = - findStrategy(flexyPoolDataSource, IncrementPoolOnTimeoutConnectionAcquiringStrategy.class); + IncrementPoolOnTimeoutConnectionAcquisitionStrategy strategy1 = + findStrategy(flexyPoolDataSource, IncrementPoolOnTimeoutConnectionAcquisitionStrategy.class); assertThat(strategy1).isNotNull(); - assertThat(strategy1).hasFieldOrPropertyWithValue("maxOverflowPoolSize", 35); + assertThat(strategy1).hasFieldOrPropertyWithValue("maxOvergrowPoolSize", 35); assertThat(strategy1).hasFieldOrPropertyWithValue("timeoutMillis", 10000); - RetryConnectionAcquiringStrategy strategy2 = - findStrategy(flexyPoolDataSource, RetryConnectionAcquiringStrategy.class); + RetryConnectionAcquisitionStrategy strategy2 = + findStrategy(flexyPoolDataSource, RetryConnectionAcquisitionStrategy.class); assertThat(strategy2).isNotNull(); assertThat(strategy2).hasFieldOrPropertyWithValue("retryAttempts", 5); }); @@ -138,19 +163,19 @@ void testDecoratingHikariDataSourceWithCustomPropertyStrategies() { @SuppressWarnings("unchecked") void testDecoratingHikariDataSourceWithCustomBeanStrategies() { ApplicationContextRunner contextRunner = this.contextRunner.withPropertyValues("spring.datasource.type:" + HikariDataSource.class.getName()) - .withConfiguration(AutoConfigurations.of(HikariConfiguration.class)); + .withConfiguration(AutoConfigurations.of(FlexyPoolHikariConfiguration.class, FlexyPoolCustomFactoriesHikariConfiguration.class)); contextRunner.run(context -> { DataSource dataSource = context.getBean(DataSource.class); FlexyPoolDataSource flexyPoolDataSource = assertDataSourceOfType(dataSource, HikariDataSource.class); - IncrementPoolOnTimeoutConnectionAcquiringStrategy strategy1 = - findStrategy(flexyPoolDataSource, IncrementPoolOnTimeoutConnectionAcquiringStrategy.class); + IncrementPoolOnTimeoutConnectionAcquisitionStrategy strategy1 = + findStrategy(flexyPoolDataSource, IncrementPoolOnTimeoutConnectionAcquisitionStrategy.class); assertThat(strategy1).isNotNull(); - assertThat(strategy1).hasFieldOrPropertyWithValue("maxOverflowPoolSize", 35); + assertThat(strategy1).hasFieldOrPropertyWithValue("maxOvergrowPoolSize", 35); assertThat(strategy1).hasFieldOrPropertyWithValue("timeoutMillis", 10000); - RetryConnectionAcquiringStrategy strategy2 = - findStrategy(flexyPoolDataSource, RetryConnectionAcquiringStrategy.class); + RetryConnectionAcquisitionStrategy strategy2 = + findStrategy(flexyPoolDataSource, RetryConnectionAcquisitionStrategy.class); assertThat(strategy2).isNotNull(); assertThat(strategy2).hasFieldOrPropertyWithValue("retryAttempts", 5); @@ -183,42 +208,46 @@ private FlexyPoolDataSource assertDataSourceOfType(Dat } @SuppressWarnings("unchecked") - private T findStrategy(FlexyPoolDataSource flexyPoolDataSource, + private T findStrategy(FlexyPoolDataSource flexyPoolDataSource, Class factoryClass) { Field field = ReflectionUtils.findField(FlexyPoolDataSource.class, "connectionAcquiringStrategies"); Objects.requireNonNull(field); ReflectionUtils.makeAccessible(field); - Set strategies = - (Set) ReflectionUtils.getField(field, flexyPoolDataSource); + Set strategies = + (Set) ReflectionUtils.getField(field, flexyPoolDataSource); Objects.requireNonNull(strategies); return (T) strategies.stream().filter(factoryClass::isInstance).findFirst().orElse(null); } @Configuration(proxyBeanMethods = false) - static class HikariConfiguration { + static class FlexyPoolHikariConfiguration { @Bean - public IncrementPoolOnTimeoutConnectionAcquiringStrategy.Factory incrementPoolOnTimeoutConnectionAcquiringStrategyFactory() { - return new IncrementPoolOnTimeoutConnectionAcquiringStrategy.Factory<>(35, 10000); + public ConnectionAcquisitionStrategyFactory hikariConnectionAcquisitionStrategyForHikari() { + return configurationProperties -> new HikariConnectionAcquiringFactory(); } @Bean - public RetryConnectionAcquiringStrategy.Factory retryConnectionAcquiringStrategy() { - return new RetryConnectionAcquiringStrategy.Factory<>(5); + public ConnectionAcquisitionStrategyFactory dbcp2ConnectionAcquiringFactory() { + return configurationProperties -> new Dbcp2ConnectionAcquiringFactory(); } + } + + @Configuration(proxyBeanMethods = false) + static class FlexyPoolCustomFactoriesHikariConfiguration { @Bean - public ConnectionAcquiringStrategyFactory hikariConnectionAcquiringStrategyForHikari() { - return configurationProperties -> new HikariConnectionAcquiringFactory(); + public IncrementPoolOnTimeoutConnectionAcquisitionStrategy.Factory incrementPoolOnTimeoutConnectionAcquisitionStrategyFactory() { + return new IncrementPoolOnTimeoutConnectionAcquisitionStrategy.Factory<>(35, 10000); } @Bean - public ConnectionAcquiringStrategyFactory dbcp2ConnectionAcquiringFactory() { - return configurationProperties -> new Dbcp2ConnectionAcquiringFactory(); + public RetryConnectionAcquisitionStrategy.Factory retryConnectionAcquisitionStrategy() { + return new RetryConnectionAcquisitionStrategy.Factory<>(5); } } - static class HikariConnectionAcquiringFactory implements ConnectionAcquiringStrategy { + static class HikariConnectionAcquiringFactory implements ConnectionAcquisitionStrategy { @Override public Connection getConnection(ConnectionRequestContext requestContext) { @@ -226,7 +255,7 @@ public Connection getConnection(ConnectionRequestContext requestContext) { } } - static class Dbcp2ConnectionAcquiringFactory implements ConnectionAcquiringStrategy { + static class Dbcp2ConnectionAcquiringFactory implements ConnectionAcquisitionStrategy { @Override public Connection getConnection(ConnectionRequestContext requestContext) { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 5499959..5f231ac 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -2,7 +2,7 @@ spring-boot = "3.3.5" p6Spy = "3.9.1" datasource-proxy = "1.10" -flexy-pool = "2.2.3" +flexy-pool = "3.0.2" [libraries] spring-boot-dependencies = { group = "org.springframework.boot", name = "spring-boot-dependencies", version.ref = "spring-boot" }