[
https://issues.apache.org/jira/browse/KNOX-3592?focusedWorklogId=1055711&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-1055711
]
ASF GitHub Bot logged work on KNOX-3592:
----------------------------------------
Author: ASF GitHub Bot
Created on: 18/Nov/26 14:49
Start Date: 18/Nov/26 14:49
Worklog Time Spent: 10m
Work Description: smolnar82 commented on code in PR #1526:
URL: https://github.com/apache/knox/pull/1526#discussion_r4615854326
##########
gateway-server/src/main/java/org/apache/knox/gateway/database/DataSourceProvider.java:
##########
@@ -39,4 +97,62 @@ public static DataSource getDataSource(GatewayConfig
gatewayConfig, AliasService
return dsFactory.createDataSource(gatewayConfig, aliasService);
}
+
+ static HikariDataSource createConnectionPool(GatewayConfig gatewayConfig,
DataSource dataSource, String poolName) throws SQLException {
+ final HikariConfig poolConfig = new HikariConfig();
+ poolConfig.setPoolName(poolName);
+ poolConfig.setDataSource(dataSource);
+
poolConfig.setMaximumPoolSize(gatewayConfig.getDatabaseConnectionPoolMaxSize());
+ if (gatewayConfig.getDatabaseConnectionPoolMinIdle() >= 0) {
+
poolConfig.setMinimumIdle(gatewayConfig.getDatabaseConnectionPoolMinIdle());
+ }
+
poolConfig.setConnectionTimeout(gatewayConfig.getDatabaseConnectionPoolConnectionTimeout());
+
poolConfig.setIdleTimeout(gatewayConfig.getDatabaseConnectionPoolIdleTimeout());
+
poolConfig.setMaxLifetime(gatewayConfig.getDatabaseConnectionPoolMaxLifetime());
+ // Connect once while building the pool and fail if that fails.
Otherwise the pool keeps retrying
+ // in the background and every caller blocks for the connection
timeout, e.g. a knoxcli command
+ // run while the gateway holds the embedded H2 database file. A failed
pool is not cached.
+ poolConfig.setInitializationFailTimeout(1);
+
+ final HikariDataSource pool;
+ try {
+ pool = new HikariDataSource(poolConfig);
+ } catch (HikariPool.PoolInitializationException e) {
+ if (e.getCause() instanceof SQLException) {
+ throw (SQLException) e.getCause();
+ }
+ throw new SQLException("Cannot create database connection pool " +
poolName + ": " + e.getMessage(), e);
Review Comment:
Good question. This is intentional, and the behavior is the same as before
pooling.
- If the pool can't open its first connection, the database is unreachable
or rejects the credentials. A non-pooled DataSource (same URL, same
credentials) would fail the same way, so falling back to one wouldn't help. We
rethrow the driver's original `SQLException`, so callers see exactly what they
saw before.
- Callers handle it as they always have:
- `JDBCTokenStateService` falls back to the embedded H2 token state
service.
- The JDBC KnoxIDF services stop gateway startup with a FATAL `Failed to
start gateway`.
- In k8s, the pod restarts (CrashLoopBackOff) and recovers on its own once
the database is reachable. The log names the cause, e.g. `PSQLException:
Connection to <host>:5432 refused`.
- At runtime, a down database or an exhausted pool gives `Connection is not
available, request timed out after Nms (total=, active=, idle=, waiting=)`. The
pool counts show which of the two it is, and the pool reconnects by itself.
- If pooling ever causes trouble, `gateway.database.pool.enabled=false`
turns it off.
Issue Time Tracking
-------------------
Worklog Id: (was: 1055711)
Time Spent: 40m (was: 0.5h)
> Use a pooled JDBC DataSource for the gateway database
> -----------------------------------------------------
>
> Key: KNOX-3592
> URL: https://issues.apache.org/jira/browse/KNOX-3592
> Project: Apache Knox
> Issue Type: Improvement
> Components: KnoxIDF
> Reporter: Sandor Molnar
> Assignee: Sandor Molnar
> Priority: Major
> Fix For: 3.1.0
>
> Time Spent: 40m
> Remaining Estimate: 0h
>
> None of the {{DataSourceFactory}} implementations pool connections:
> * Postgres uses {{PGSimpleDataSource}}.
> * MySQL and MariaDB use {{MysqlDataSource}}.
> * Oracle uses plain {{OracleDataSource}}.
> * H2 uses {{JdbcDataSource}}.
> As a result, every {{getConnection()}} opens a new TCP connection and logs in
> again (on Postgres that is SCRAM-SHA-256, plus a TLS handshake if SSL is
> enabled). {{DataSourceProvider}} also builds a separate DataSource for each
> service, e.g. token state, delegation policies, trusted OIDC issuers,
> federated identity and the remote config monitor.
> On hot paths this cost repeats on every request. For example, RFC 8693 token
> exchange does a delegation policy lookup per exchange, and
> {{JDBCTokenStateService}} runs two statements per metadata entry for each new
> token. Thread dumps under load show request threads blocked in connection
> setup and authentication. With remote databases the cost grows with network
> latency.
> *Proposal:*
> * Wrap the vendor DataSource in a connection pool, for example HikariCP
> (Apache-2.0).
> * Share one pooled DataSource across all DB-backed services.
> * Make the pool configurable in {{gateway-site.xml}}:
> {{gateway.database.pool.*}} for max size, min idle, connection timeout, idle
> timeout and max lifetime, with sensible defaults.
> * Close the pool on gateway shutdown.
> * Existing connection, credential-alias and SSL settings keep working
> unchanged.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)