Skip to content

Commit

Permalink
Merge pull request eclipse-kapua#4112 from Coduz/feat-deprecateDbPool…
Browse files Browse the repository at this point in the history
…Settings

♻️ [Database] Deprecate `db.pool.size.*` settings
  • Loading branch information
Coduz authored Nov 29, 2024
2 parents 4116da6 + 86528ae commit 702bd78
Show file tree
Hide file tree
Showing 16 changed files with 157 additions and 60 deletions.
8 changes: 8 additions & 0 deletions UPGRADE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ Below are described most important changes, features, additions and bugfixes tha

This report is only partial for Eclipse Kapua release 2.1.0-SNAPSHOT, since we started to maintain it mid-release development.

## Changes

#### Deprecation of `db.pool.size.min` and `db.pool.size.max` sizing options

With [#4112](https://github.com/eclipse/kapua/pull/4112) we deprecated `commons.db.pool.size.min` and `commons.db.pool.size.max` settings, switching to a new `commons.db.pool.size.fixed` setting which will configure the database connection pool to a fixed size with a default value of `5`.

Backward compatibility is granted by `db.pool.size.strategy`. Default value is `fixed`, but it can be changed to `range` to continue use of a variable database connection pool. Be aware that at some point `range` strategy will be removed and only fixed size will be available.

## DB Changes

#### New column in MFA Option table
Expand Down
86 changes: 72 additions & 14 deletions commons/src/main/java/org/eclipse/kapua/commons/jpa/DataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@
*******************************************************************************/
package org.eclipse.kapua.commons.jpa;

import org.eclipse.kapua.KapuaRuntimeException;
import org.eclipse.kapua.commons.setting.system.SystemSetting;
import org.eclipse.kapua.commons.setting.system.SystemSettingKey;

import com.zaxxer.hikari.HikariDataSource;
import org.eclipse.kapua.commons.util.log.ConfigurationPrinter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class DataSource {

private static final Logger LOG = LoggerFactory.getLogger(DataSource.class);

private static HikariDataSource hikariDataSource;

private DataSource() {
Expand All @@ -29,33 +35,85 @@ public static HikariDataSource getDataSource() {
SystemSetting config = SystemSetting.getInstance();

hikariDataSource = new HikariDataSource();
hikariDataSource.setPoolName("hikari-main");
hikariDataSource.setRegisterMbeans(true);
hikariDataSource.setAllowPoolSuspension(false);

// Connection
hikariDataSource.setDriverClassName(config.getString(SystemSettingKey.DB_JDBC_DRIVER));
hikariDataSource.setJdbcUrl(JdbcConnectionUrlResolvers.resolveJdbcUrl());
hikariDataSource.setUsername(config.getString(SystemSettingKey.DB_USERNAME));
hikariDataSource.setPassword(config.getString(SystemSettingKey.DB_PASSWORD));
hikariDataSource.setMaximumPoolSize(config.getInt(SystemSettingKey.DB_POOL_SIZE_MAX, 20));
//commented out since is not good for performances
//see official documentation https://github.com/brettwooldridge/HikariCP
//This property controls the minimum number of idle connections that HikariCP tries to maintain in the pool.
//If the idle connections dip below this value, HikariCP will make a best effort to add additional connections
//quickly and efficiently. However, for maximum performance and responsiveness to spike demands, we recommend not
//setting this value and instead allowing HikariCP to act as a fixed size connection pool.
//Default: same as maximumPoolSize
// hikariDataSource.setMinimumIdle(config.getInt(SystemSettingKey.DB_POOL_SIZE_MIN, 1));
hikariDataSource.setPoolName("hikari-main");
hikariDataSource.setRegisterMbeans(true);
hikariDataSource.setAllowPoolSuspension(false);
//fixed size so this parameter is ignored by hikari
// hikariDataSource.setIdleTimeout(config.getInt(SystemSettingKey.DB_POOL_IDLE_TIMEOUT, 180000));

// Pool
String dbConnectionPoolStrategy = config.getString(SystemSettingKey.DB_POOL_SIZE_STRATEGY, "fixed");

switch (dbConnectionPoolStrategy) {
case "fixed": {
hikariDataSource.setMaximumPoolSize(config.getInt(SystemSettingKey.DB_POOL_SIZE_FIXED, 5));
}
break;
case "range": {
LOG.warn("Using deprecated 'range' db connection pool sizing strategy. Please consider migrating to 'fixed' strategy");
hikariDataSource.setMinimumIdle(config.getInt(SystemSettingKey.DB_POOL_SIZE_MIN, 1));
hikariDataSource.setMaximumPoolSize(config.getInt(SystemSettingKey.DB_POOL_SIZE_MAX, 20));
hikariDataSource.setIdleTimeout(config.getInt(SystemSettingKey.DB_POOL_IDLE_TIMEOUT, 180000));
}
break;
default: {
throw KapuaRuntimeException.internalError("Unrecognized value for setting 'commons.db.pool.size.strategy'. Available values are 'range' and 'fixed'. Value provided: '" + dbConnectionPoolStrategy+ "'");
}
}

hikariDataSource.setKeepaliveTime(config.getInt(SystemSettingKey.DB_POOL_KEEPALIVE_TIME, 30000));
hikariDataSource.setMaxLifetime(config.getInt(SystemSettingKey.DB_POOL_MAX_LIFETIME, 1800000));
hikariDataSource.setConnectionTestQuery(config.getString(SystemSettingKey.DB_POOL_TEST_QUERY, "SELECT 1"));
hikariDataSource.setLeakDetectionThreshold(config.getInt(SystemSettingKey.DB_POOL_LEAKDETECTION_THRESHOLD, 0));

printDbPoolConfiguration(hikariDataSource, dbConnectionPoolStrategy);
}

return hikariDataSource;
}

private static void printDbPoolConfiguration(HikariDataSource hikariDataSource, String dbConnectionPoolStrategy) {
// Print Configurations
ConfigurationPrinter dbPoolConfigPrinter = ConfigurationPrinter
.create()
.withLogger(LOG)
.withLogLevel(ConfigurationPrinter.LogLevel.INFO)
.withTitle("HikariDataSource Configuration")
.addParameter("Pool Name", hikariDataSource.getPoolName())
.openSection("DB Connection info")
.addParameter("Driver", hikariDataSource.getDriverClassName())
.addParameter("JDBC URL", hikariDataSource.getJdbcUrl())
.addParameter("Username", hikariDataSource.getUsername())
.addParameter("Password", "******")
.closeSection()
.openSection("Client Pool info")
.addParameter("Sizing strategy", dbConnectionPoolStrategy);

switch (dbConnectionPoolStrategy) {
case "fixed": {
dbPoolConfigPrinter.addParameter("Size", hikariDataSource.getMaximumPoolSize());
}
break;
case "range": {
dbPoolConfigPrinter.addParameter("Min idle", hikariDataSource.getMinimumIdle());
dbPoolConfigPrinter.addParameter("Max size", hikariDataSource.getMaximumPoolSize());
dbPoolConfigPrinter.addParameter("Idle timeout", hikariDataSource.getIdleTimeout());
}
}

dbPoolConfigPrinter
.addParameter("Connection test query", hikariDataSource.getConnectionTestQuery())
.addParameter("Keepalive time", hikariDataSource.getKeepaliveTime())
.addParameter("Max lifetime", hikariDataSource.getMaxLifetime())
.addParameter("Leak detection threshold", hikariDataSource.getLeakDetectionThreshold() == 0 ? "disabled" : hikariDataSource.getLeakDetectionThreshold());

dbPoolConfigPrinter.printLog();
}

@Override
public String toString() {
return hikariDataSource.getDriverClassName() + "@" + hikariDataSource.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,17 +168,60 @@ public enum SystemSettingKey implements SettingKey {
DB_CHARACTER_WILDCARD_SINGLE("commons.db.character.wildcard.single"),

/**
* Database pool minimum pool size
* Database pool size strategy.
* <p>
* Created to maintain backward compatibility with 2.0.x release.
* 2.0.x is defaulting to {@code range} since the previous strategy was using a variable DB connection pool sizing.
* {@code range} strategy is deprecated and users should migrate to use {@code fixed} strategy
* <p>
* See official documentation <a href="https://github.com/brettwooldridge/HikariCP">Hikari CP Docs</a> about performances.
*
* @since 2.1.0
*/
DB_POOL_SIZE_STRATEGY("commons.db.pool.size.strategy"),

/**
* Database fixed pool size.
* <p>
* Taken into account when {@link #DB_POOL_SIZE_STRATEGY} is {@code fixed}
*
* @since 2.1.0
*/
DB_POOL_SIZE_FIXED("commons.db.pool.size.fixed"),

/**
* Database pool minimum pool size.
* <p>
* Taken into account when {@link #DB_POOL_SIZE_STRATEGY} is {@code range}
*
* @since 1.0.0
* @deprecated Since 2.1.0. Please make use of {@link #DB_POOL_SIZE_FIXED}
*/
@Deprecated
DB_POOL_SIZE_MIN("commons.db.pool.size.min"),

/**
* Database pool maximum pool size
* Database pool maximum pool size.
* <p>
* Taken into account when {@link #DB_POOL_SIZE_STRATEGY} is {@code range}
*
* @since 1.0.0
* @deprecated Since 2.1.0. Please make use of {@link #DB_POOL_SIZE_FIXED}
*/
@Deprecated
DB_POOL_SIZE_MAX("commons.db.pool.size.max"),

/**
* Database pool maximum time before evicting an idle connection
* <p>
* Taken into account when {@link #DB_POOL_SIZE_STRATEGY} is {@code range}
*
* @since 1.0.0
* @deprecated Since 2.1.0.
*/
@Deprecated
DB_POOL_IDLE_TIMEOUT("commons.db.pool.idle.timeout"),

/**
* Database pool keepalive query interval for idle connections
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,15 @@ commons.db.useLegacyDatetimeCode=false
commons.db.serverTimezone=UTC
commons.db.characterEncoding=UTF-8

commons.db.pool.size.initial=5
# Introduced to maintain backward compatibility with 2.0.x release. In 2.0.x the default value is `range`
commons.db.pool.size.strategy=fixed
#commons.db.pool.size.strategy=range

commons.db.pool.size.fixed=5
# Deprecated setting since 2.1.0. Use commons.db.pool.size.fixed
commons.db.pool.size.min=2
# Deprecated setting since 2.1.0. Use commons.db.pool.size.fixed
commons.db.pool.size.max=30
commons.db.pool.borrow.timeout=15000

commons.db.character.escape=\\
commons.db.character.wildcard.any=%
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public void keyTest() {
systemSettings.put(SystemSettingKey.DB_USE_LEGACY_DATETIME_CODE, "commons.db.useLegacyDatetimeCode");
systemSettings.put(SystemSettingKey.DB_SERVER_TIMEZONE, "commons.db.serverTimezone");
systemSettings.put(SystemSettingKey.DB_CHAR_ENCODING, "commons.db.characterEncoding");
systemSettings.put(SystemSettingKey.DB_POOL_SIZE_STRATEGY, "commons.db.pool.size.strategy");
systemSettings.put(SystemSettingKey.DB_POOL_SIZE_FIXED, "commons.db.pool.size.fixed");
systemSettings.put(SystemSettingKey.DB_POOL_SIZE_MIN, "commons.db.pool.size.min");
systemSettings.put(SystemSettingKey.DB_POOL_SIZE_MAX, "commons.db.pool.size.max");
systemSettings.put(SystemSettingKey.DB_POOL_IDLE_TIMEOUT, "commons.db.pool.idle.timeout");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ commons.db.useLegacyDatetimeCode=false
commons.db.serverTimezone=UTC
commons.db.characterEncoding=UTF-8

commons.db.pool.size.initial=5
commons.db.pool.size.fixed=5
# Deprecated setting since 2.1.0. Use commons.db.pool.size.fixed
commons.db.pool.size.min=2
# Deprecated setting since 2.1.0. Use commons.db.pool.size.fixed
commons.db.pool.size.max=30
commons.db.pool.borrow.timeout=15000

#
# Broker settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ commons.db.useLegacyDatetimeCode=false
commons.db.serverTimezone=UTC
commons.db.characterEncoding=UTF-8

commons.db.pool.size.initial=5
commons.db.pool.size.min=2
commons.db.pool.size.max=30
commons.db.pool.borrow.timeout=15000
commons.db.pool.size.strategy=fixed
commons.db.pool.size.fixed=5

#
# Broker settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ commons.db.useLegacyDatetimeCode=false
commons.db.serverTimezone=UTC
commons.db.characterEncoding=UTF-8

commons.db.pool.size.initial=5
commons.db.pool.size.min=2
commons.db.pool.size.max=30
commons.db.pool.borrow.timeout=15000
commons.db.pool.size.strategy=fixed
commons.db.pool.size.fixed=5

#
# Broker settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ commons.db.useLegacyDatetimeCode=false
commons.db.serverTimezone=UTC
commons.db.characterEncoding=UTF-8

commons.db.pool.size.initial=5
commons.db.pool.size.min=2
commons.db.pool.size.max=30
commons.db.pool.borrow.timeout=15000
commons.db.pool.size.strategy=fixed
commons.db.pool.size.fixed=5

#
# Broker settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ commons.db.useLegacyDatetimeCode=false
commons.db.serverTimezone=UTC
commons.db.characterEncoding=UTF-8

commons.db.pool.size.initial=5
commons.db.pool.size.min=2
commons.db.pool.size.max=30
commons.db.pool.borrow.timeout=15000
commons.db.pool.size.strategy=fixed
commons.db.pool.size.fixed=5

#
# Broker settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ commons.db.useLegacyDatetimeCode=false
commons.db.serverTimezone=UTC
commons.db.characterEncoding=UTF-8

commons.db.pool.size.initial=5
commons.db.pool.size.min=2
commons.db.pool.size.max=30
commons.db.pool.borrow.timeout=15000
commons.db.pool.size.strategy=fixed
commons.db.pool.size.fixed=5

#
# Broker settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ commons.db.useLegacyDatetimeCode=false
commons.db.serverTimezone=UTC
commons.db.characterEncoding=UTF-8

commons.db.pool.size.initial=5
commons.db.pool.size.min=2
commons.db.pool.size.max=30
commons.db.pool.borrow.timeout=15000
commons.db.pool.size.strategy=fixed
commons.db.pool.size.fixed=5

#
# Broker settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ commons.db.useLegacyDatetimeCode=false
commons.db.serverTimezone=UTC
commons.db.characterEncoding=UTF-8

commons.db.pool.size.initial=5
commons.db.pool.size.min=2
commons.db.pool.size.max=30
commons.db.pool.borrow.timeout=15000
commons.db.pool.size.strategy=fixed
commons.db.pool.size.fixed=5

#
# Broker settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ commons.db.useLegacyDatetimeCode=false
commons.db.serverTimezone=UTC
commons.db.characterEncoding=UTF-8

commons.db.pool.size.initial=5
commons.db.pool.size.min=2
commons.db.pool.size.max=30
commons.db.pool.borrow.timeout=15000
commons.db.pool.size.strategy=fixed
commons.db.pool.size.fixed=5

#
# Broker settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ commons.db.useLegacyDatetimeCode=false
commons.db.serverTimezone=UTC
commons.db.characterEncoding=UTF-8

commons.db.pool.size.initial=5
commons.db.pool.size.min=2
commons.db.pool.size.max=30
commons.db.pool.borrow.timeout=15000
commons.db.pool.size.strategy=fixed
commons.db.pool.size.fixed=5

#
# Broker settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ commons.db.useLegacyDatetimeCode=false
commons.db.serverTimezone=UTC
commons.db.characterEncoding=UTF-8

commons.db.pool.size.initial=5
commons.db.pool.size.min=2
commons.db.pool.size.max=30
commons.db.pool.borrow.timeout=15000
commons.db.pool.size.strategy=fixed
commons.db.pool.size.fixed=5

#
# Broker settings
Expand Down

0 comments on commit 702bd78

Please sign in to comment.