[
https://issues.apache.org/jira/browse/KNOX-3592?focusedWorklogId=1055662&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-1055662
]
ASF GitHub Bot logged work on KNOX-3592:
----------------------------------------
Author: ASF GitHub Bot
Created on: 18/Nov/26 13:09
Start Date: 18/Nov/26 13:09
Worklog Time Spent: 10m
Work Description: smolnar82 opened a new pull request, #1526:
URL: https://github.com/apache/knox/pull/1526
[KNOX-3592](https://issues.apache.org/jira/browse/KNOX-3592) - Use a pooled
JDBC DataSource for the gateway database
## What changes were proposed in this pull request?
None of the `DataSourceFactory` implementations pool connections. Every
`getConnection()` opens a new TCP connection and logs in again (on Postgres
that is SCRAM-SHA-256, plus a TLS handshake when SSL is on).
`DataSourceProvider` also built a separate DataSource for each DB-backed
service. On hot paths, such as the per-exchange delegation policy lookup and
the token state writes, this cost repeated on every request.
This PR:
- **Pools connections.** `DataSourceProvider` wraps the vendor DataSource in
a HikariCP pool (`HikariConfig.setDataSource`), so the existing connection,
credential-alias and SSL settings work unchanged.
- **Shares one pool per configuration.** All services with the same database
settings use one pool. The pool key covers every setting the data source
factories read, plus a SHA-256 hash of the alias credentials, so services
configured differently get separate pools.
- **Connects lazily.** The pool doesn't connect until first use
(`initializationFailTimeout=-1`), so the gateway starts when the database is
unavailable, as before.
- **Closes pools on shutdown.** `GatewayServer.stop()` closes them after
`services.stop()`.
- **Adds `gateway-site.xml` settings:**
| Property | Default |
|---|---|
| `gateway.database.pool.enabled` | `true` |
| `gateway.database.pool.max.size` | `10` |
| `gateway.database.pool.min.idle` | `-1` (HikariCP default: same as
`max.size`, i.e. a fixed pool) |
| `gateway.database.pool.connection.timeout.ms` | `30000` |
| `gateway.database.pool.idle.timeout.ms` | `600000` |
| `gateway.database.pool.max.lifetime.ms` | `1800000` |
- **Uses a HikariCP version Knox already ships.** It is pinned to 7.0.2, the
version `pac4j-config` already brings in, so the release dependency set doesn't
change.
## How was this patch tested?
- **New unit tests in `DataSourceProviderTest`.** They check that:
- the pool wraps the vendor DataSource with the configured settings and
runs a query (H2);
- identical settings share one pool, and different settings get separate
pools;
- `closeConnectionPools()` closes and forgets the pools;
- the pool doesn't connect until first use;
- pooling can be turned off.
- **New unit tests in `GatewayConfigImplTest`** for the defaults and
configured values.
- **Module test runs:** all `gateway-server` tests pass.
`JDBCTokenStateServiceTest`, `JdbcDelegationPolicyServiceTest` and
`JdbcTrustedOidcIssuerServiceTest` also pass.
- **Local deployment against Postgres (in Docker):**
- The gateway logs `Created database connection pool KnoxDatabasePool-1
for postgresql with at most 10 connections`, once for all services.
- An RFC 8693 token-exchange load test (delegation depth 3, 1 and 20
threads) ran with managed tokens on and off, with 0 errors.
- With managed tokens, throughput rose from 10.8 to 149 req/s (1 thread)
and from 15.7 to 528 req/s (20 threads). These figures also include KNOX-3587
and KNOX-3591, which were deployed together, so they aren't the pool's gain
alone.
## Integration Tests
No new integration tests. Pooling is on by default, so the existing Docker
Compose suites that use the database (token state, KnoxIDF delegation policies
and trusted issuers) now run through the pool.
Issue Time Tracking
-------------------
Worklog Id: (was: 1055662)
Remaining Estimate: 0h
Time Spent: 10m
> 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: 10m
> 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)