Skip to content

Commit

Permalink
Update to FlexyPool 3.0.2 (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
gavlyukovskiy authored Nov 8, 2024
1 parent 4c773b4 commit d1aca35
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 112 deletions.
31 changes: 10 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends Event>` beans can be registered to subscribe on events of flexy-pool (e.g. `ConnectionAcquireTimeThresholdExceededEvent`, `ConnectionLeaseTimeThresholdExceededEvent`).
`EventListener<? extends Event>` 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
Expand All @@ -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
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -60,17 +58,17 @@
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
* @since 1.2
*/
public class FlexyPoolConfiguration {

static <T extends DataSource> List<ConnectionAcquiringStrategyFactory<?, T>> mergeFactories(
List<ConnectionAcquiringStrategyFactory<?, T>> factories, FlexyPoolProperties flexyPool) {
List<ConnectionAcquiringStrategyFactory<?, T>> newFactories = new ArrayList<>();
static <T extends DataSource> List<ConnectionAcquisitionStrategyFactory<?, T>> mergeFactories(
List<ConnectionAcquisitionStrategyFactory<?, T>> factories, FlexyPoolProperties flexyPool) {
List<ConnectionAcquisitionStrategyFactory<?, T>> newFactories = new ArrayList<>();
List<? extends Class<?>> factoryClasses;
if (factories != null) {
factoryClasses = factories.stream().map(Object::getClass).collect(Collectors.toList());
Expand All @@ -79,17 +77,23 @@ static <T extends DataSource> List<ConnectionAcquiringStrategyFactory<?, T>> 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;
Expand Down Expand Up @@ -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);
Expand All @@ -148,14 +152,14 @@ public FlexyPoolConfigurationBuilderCustomizer flexyPoolConfigurationBuilderCust
static class HikariFlexyConfiguration {

@Autowired(required = false)
private List<ConnectionAcquiringStrategyFactory<?, HikariDataSource>> connectionAcquiringStrategyFactories;
private List<ConnectionAcquisitionStrategyFactory<?, HikariDataSource>> ConnectionAcquisitionStrategyFactories;
@Autowired
private DataSourceDecoratorProperties dataSourceDecoratorProperties;

@Bean
public FlexyPoolDataSourceDecorator flexyPoolDataSourceDecorator() {
return new FlexyPoolDataSourceDecorator(
mergeFactories(connectionAcquiringStrategyFactories, dataSourceDecoratorProperties.getFlexyPool()),
mergeFactories(ConnectionAcquisitionStrategyFactories, dataSourceDecoratorProperties.getFlexyPool()),
HikariCPPoolAdapter.FACTORY, HikariDataSource.class);
}
}
Expand All @@ -167,14 +171,14 @@ public FlexyPoolDataSourceDecorator flexyPoolDataSourceDecorator() {
static class TomcatFlexyConfiguration {

@Autowired(required = false)
private List<ConnectionAcquiringStrategyFactory<?, org.apache.tomcat.jdbc.pool.DataSource>> connectionAcquiringStrategyFactories;
private List<ConnectionAcquisitionStrategyFactory<?, org.apache.tomcat.jdbc.pool.DataSource>> 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);
}
}
Expand All @@ -186,14 +190,14 @@ public FlexyPoolDataSourceDecorator flexyPoolDataSourceDecorator() {
static class Dbcp2FlexyConfiguration {

@Autowired(required = false)
private List<ConnectionAcquiringStrategyFactory<?, BasicDataSource>> connectionAcquiringStrategyFactories;
private List<ConnectionAcquisitionStrategyFactory<?, BasicDataSource>> ConnectionAcquisitionStrategyFactories;
@Autowired
private DataSourceDecoratorProperties dataSourceDecoratorProperties;

@Bean
public FlexyPoolDataSourceDecorator flexyPoolDataSourceDecorator() {
return new FlexyPoolDataSourceDecorator(
mergeFactories(connectionAcquiringStrategyFactories, dataSourceDecoratorProperties.getFlexyPool()),
mergeFactories(ConnectionAcquisitionStrategyFactories, dataSourceDecoratorProperties.getFlexyPool()),
DBCP2PoolAdapter.FACTORY, BasicDataSource.class);
}
}
Expand All @@ -206,12 +210,12 @@ static class PropertyFlexyConfiguration {
private static final Logger log = getLogger(PropertyFlexyConfiguration.class);

@Autowired(required = false)
private List<ConnectionAcquiringStrategyFactory<?, javax.sql.DataSource>> connectionAcquiringStrategyFactories;
private List<ConnectionAcquisitionStrategyFactory<?, javax.sql.DataSource>> 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");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Loading

0 comments on commit d1aca35

Please sign in to comment.