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. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
