From 1ef36c3a47caf49ffc824d8f74fea37b9d85271b Mon Sep 17 00:00:00 2001 From: Silvestre Zabala Date: Thu, 2 Nov 2023 11:43:28 +0100 Subject: [PATCH] Replace DBCP2 database connection pool implementation with HikariCP HikariCP is the default Spring Boot database connection pool implementation and the current implementation of choice. --- src/scheduler/pom.xml | 5 --- .../scheduler/conf/DataSourceConfig.java | 6 ++-- .../scheduler/health/DbStatusCollector.java | 31 ++++++------------- .../scheduler/SchedulerApplicationTest.java | 7 ++--- 4 files changed, 16 insertions(+), 33 deletions(-) diff --git a/src/scheduler/pom.xml b/src/scheduler/pom.xml index fc121612cc..d36dd2e4ef 100644 --- a/src/scheduler/pom.xml +++ b/src/scheduler/pom.xml @@ -85,11 +85,6 @@ mysql-connector-j 8.2.0 - - org.apache.commons - commons-dbcp2 - 2.10.0 - org.apache.httpcomponents.client5 httpclient5 diff --git a/src/scheduler/src/main/java/org/cloudfoundry/autoscaler/scheduler/conf/DataSourceConfig.java b/src/scheduler/src/main/java/org/cloudfoundry/autoscaler/scheduler/conf/DataSourceConfig.java index cf2531d96d..ae42d2eefa 100644 --- a/src/scheduler/src/main/java/org/cloudfoundry/autoscaler/scheduler/conf/DataSourceConfig.java +++ b/src/scheduler/src/main/java/org/cloudfoundry/autoscaler/scheduler/conf/DataSourceConfig.java @@ -1,8 +1,8 @@ package org.cloudfoundry.autoscaler.scheduler.conf; +import com.zaxxer.hikari.HikariDataSource; import java.util.Properties; import javax.sql.DataSource; -import org.apache.commons.dbcp2.BasicDataSource; import org.cloudfoundry.autoscaler.scheduler.beanPostProcessor.DatasourceBeanPostProcessor; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; @@ -24,7 +24,7 @@ public class DataSourceConfig { @Qualifier("primary") @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource(@Qualifier("primary") DataSourceProperties properties) { - return properties.initializeDataSourceBuilder().type(BasicDataSource.class).build(); + return properties.initializeDataSourceBuilder().type(HikariDataSource.class).build(); } @Bean @@ -45,7 +45,7 @@ public DataSourceProperties policyDbDataSourceProperties() { @Qualifier("policy") @ConfigurationProperties("spring.policy-db-datasource") public DataSource policyDbDataSource(@Qualifier("policy") DataSourceProperties properties) { - return properties.initializeDataSourceBuilder().type(BasicDataSource.class).build(); + return properties.initializeDataSourceBuilder().type(HikariDataSource.class).build(); } @Bean diff --git a/src/scheduler/src/main/java/org/cloudfoundry/autoscaler/scheduler/health/DbStatusCollector.java b/src/scheduler/src/main/java/org/cloudfoundry/autoscaler/scheduler/health/DbStatusCollector.java index 097c05aa6e..491a24e715 100644 --- a/src/scheduler/src/main/java/org/cloudfoundry/autoscaler/scheduler/health/DbStatusCollector.java +++ b/src/scheduler/src/main/java/org/cloudfoundry/autoscaler/scheduler/health/DbStatusCollector.java @@ -1,11 +1,11 @@ package org.cloudfoundry.autoscaler.scheduler.health; +import com.zaxxer.hikari.HikariDataSource; import io.prometheus.client.Collector; import io.prometheus.client.GaugeMetricFamily; import java.util.ArrayList; import java.util.List; import javax.sql.DataSource; -import org.apache.commons.dbcp2.BasicDataSource; public class DbStatusCollector extends Collector { @@ -24,53 +24,42 @@ public void setPolicyDbDataSource(DataSource policyDbDataSource) { private DataSource policyDbDataSource; - private List collectForDataSource(BasicDataSource dataSource, String name) { - List mfs = new ArrayList(); - mfs.add( - new GaugeMetricFamily( - namespace + "_" + subSystem + name + "_initial_size", - "The initial number of connections that are created when the pool is started", - dataSource.getInitialSize())); + private List collectForDataSource(HikariDataSource dataSource, String name) { + List mfs = new ArrayList<>(); mfs.add( new GaugeMetricFamily( - namespace + "_" + subSystem + name + "_max_active", + namespace + "_" + subSystem + name + "_max_size", "The maximum number of active connections that can be allocated from this pool at the" + " same time, or negative for no limit", - dataSource.getMaxTotal())); - mfs.add( - new GaugeMetricFamily( - namespace + "_" + subSystem + name + "_max_idle", - "The maximum number of connections that can remain idle in the pool, without extra ones" - + " being released, or negative for no limit.", - dataSource.getMaxIdle())); + dataSource.getMaximumPoolSize())); mfs.add( new GaugeMetricFamily( namespace + "_" + subSystem + name + "_min_idle", "The minimum number of active connections that can remain idle in the pool, without" + " extra ones being created, or 0 to create none.", - dataSource.getMinIdle())); + dataSource.getMinimumIdle())); mfs.add( new GaugeMetricFamily( namespace + "_" + subSystem + name + "_active_connections_number", "The current number of active connections that have been allocated from this data" + " source", - dataSource.getNumActive())); + dataSource.getHikariPoolMXBean().getActiveConnections())); mfs.add( new GaugeMetricFamily( namespace + "_" + subSystem + name + "_idle_connections_number", "The current number of idle connections that are waiting to be allocated from this data" + " source", - dataSource.getNumIdle())); + dataSource.getHikariPoolMXBean().getIdleConnections())); return mfs; } @Override public List collect() { List mfs = new ArrayList(); - BasicDataSource basicDataSource = (BasicDataSource) this.dataSource; + HikariDataSource basicDataSource = (HikariDataSource) this.dataSource; mfs.addAll(collectForDataSource(basicDataSource, "_data_source")); - BasicDataSource policyBasicDataSource = (BasicDataSource) this.policyDbDataSource; + HikariDataSource policyBasicDataSource = (HikariDataSource) this.policyDbDataSource; mfs.addAll(collectForDataSource(policyBasicDataSource, "_policy_db_data_source")); return mfs; } diff --git a/src/scheduler/src/test/java/org/cloudfoundry/autoscaler/scheduler/SchedulerApplicationTest.java b/src/scheduler/src/test/java/org/cloudfoundry/autoscaler/scheduler/SchedulerApplicationTest.java index 7af8071896..1fe406f309 100644 --- a/src/scheduler/src/test/java/org/cloudfoundry/autoscaler/scheduler/SchedulerApplicationTest.java +++ b/src/scheduler/src/test/java/org/cloudfoundry/autoscaler/scheduler/SchedulerApplicationTest.java @@ -3,7 +3,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalToIgnoringCase; -import org.apache.commons.dbcp2.BasicDataSource; +import com.zaxxer.hikari.HikariDataSource; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -14,13 +14,12 @@ @RunWith(SpringRunner.class) @SpringBootTest public class SchedulerApplicationTest { - @Autowired private BasicDataSource dataSource; + @Autowired private HikariDataSource dataSource; @Test public void testTomcatConnectionPoolNameCorrect() { assertThat( - dataSource.getClass().getName(), - equalToIgnoringCase("org.apache.commons.dbcp2.BasicDataSource")); + dataSource.getClass().getName(), equalToIgnoringCase("com.zaxxer.hikari.HikariDataSource")); } @Test