davsclaus commented on code in PR #26845: URL: https://github.com/apache/camel/pull/26845#discussion_r4134214424
########## components/camel-sql/src/test/java/org/apache/camel/component/sql/DataSourceHelperAgroalIntegrationTest.java: ########## @@ -0,0 +1,101 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.sql; + +import java.sql.Connection; +import java.util.concurrent.TimeUnit; + +import io.agroal.api.AgroalDataSource; +import io.agroal.api.AgroalDataSourceMetrics; +import io.agroal.api.configuration.supplier.AgroalDataSourceConfigurationSupplier; +import io.agroal.api.security.NamePrincipal; +import io.agroal.api.security.SimplePassword; +import org.apache.camel.support.DataSourceHelper; +import org.junit.jupiter.api.Test; + +import static org.awaitility.Awaitility.await; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Integration test for {@link DataSourceHelper#evictDataSourceConnections} against a real {@link AgroalDataSource} + * backed by h2. Verifies that Agroal's {@code flush(GRACEFUL)} is correctly detected and invoked via reflection. + */ +class DataSourceHelperAgroalIntegrationTest { + + private static AgroalDataSource createDataSource(String dbName) throws Exception { + return AgroalDataSource.from(new AgroalDataSourceConfigurationSupplier() + .metricsEnabled() + .connectionPoolConfiguration(pool -> pool + .maxSize(1) + .minSize(1) + .connectionFactoryConfiguration(factory -> factory + .jdbcUrl("jdbc:h2:mem:" + dbName + ";DB_CLOSE_DELAY=-1") + .credential(new NamePrincipal("sa")) + .credential(new SimplePassword(""))))); + } + + @Test + void evict_startedPool_evictsConnections() throws Exception { + try (AgroalDataSource ds = createDataSource("agroal_evict_started")) { + AgroalDataSourceMetrics metrics = ds.getMetrics(); + + // Grab a connection and remember the underlying physical connection + Connection physicalBefore; + try (Connection conn = ds.getConnection()) { + physicalBefore = conn.unwrap(Connection.class); + assertNotNull(physicalBefore); + } + + long flushCountBefore = metrics.flushCount(); + + // Evict — flush(GRACEFUL) via reflection + DataSourceHelper.evictDataSourceConnections(ds, "test-rotation"); + + // GRACEFUL flush hands a FlushTask to the housekeeping executor, so the + // actual eviction is async. Use Awaitility instead of Thread.sleep to + // avoid flakiness and comply with the project's no-Thread.sleep rule. Review Comment: Nit: the reference to the project's no-Thread.sleep rule reads a bit odd in source; the first sentence (flush is async) is enough on its own. ########## core/camel-support/src/main/java/org/apache/camel/support/DataSourceHelper.java: ########## @@ -0,0 +1,170 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.support; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Collections; +import java.util.IdentityHashMap; +import java.util.Set; +import java.util.function.Function; + +import javax.sql.DataSource; + +import org.apache.camel.Component; +import org.apache.camel.Endpoint; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Utility methods for working with JDBC {@link DataSource} instances. + */ +public final class DataSourceHelper { + + private static final Logger LOG = LoggerFactory.getLogger(DataSourceHelper.class); + + private DataSourceHelper() { + } + + /** + * Collects and evicts stale connections from all DataSources used by a component: the component's own + * {@code dataSource} field plus any DataSources held by the component's active endpoints. + * <p/> + * Identity-based deduplication ensures each DataSource is evicted at most once, even if the same instance is shared + * between the component and one or more endpoints. + * + * @param componentDataSource the component-level DataSource (may be null) + * @param endpoints the component's active endpoints (from {@code getCamelContext().getEndpoints()}) + * @param componentInstance the component instance, used to filter endpoints + * ({@code endpoint.getComponent() == this}) + * @param endpointDsExtractor a function to extract the DataSource from an endpoint (may return null) + * @param source an opaque label for the rotation event (used in log messages only) + */ + public static void evictComponentDataSources( + DataSource componentDataSource, + Iterable<Endpoint> endpoints, + Component componentInstance, + Function<Endpoint, DataSource> endpointDsExtractor, + Object source) { + + Set<DataSource> dataSources = Collections.newSetFromMap(new IdentityHashMap<>()); + if (componentDataSource != null) { + dataSources.add(componentDataSource); + } + if (endpoints != null) { + for (Endpoint ep : endpoints) { + if (ep.getComponent() == componentInstance) { + DataSource ds = endpointDsExtractor.apply(ep); + if (ds != null) { + dataSources.add(ds); + } + } + } + } + for (DataSource ds : dataSources) { + evictDataSourceConnections(ds, source); + } + } + + /** + * Evicts stale connections from the given DataSource's connection pool. + * <p/> + * The following pool implementations are supported (detected via reflection — no compile-time dependency required): + * <ul> + * <li><b>HikariCP</b>: {@code softEvictConnections()} is called via {@code HikariPoolMXBean}. Idle connections are + * evicted immediately; borrowed connections are evicted when returned.</li> + * <li><b>Agroal</b> (Quarkus default): {@code flush(GRACEFUL)} is called on {@code AgroalDataSource}. Idle + * connections are closed immediately; active connections are closed when returned to the pool.</li> + * </ul> + * Any DataSource not recognised as one of the above is left untouched — a log message is emitted and the pool will + * naturally replace connections as they expire or are validated. + * <p/> + * <b>Important:</b> this method only evicts existing connections from the pool. It does <em>not</em> update the + * pool's credentials. For pools configured with a static password (e.g. Spring Boot + * {@code spring.datasource.password}), the pool will re-open connections using the <em>old</em> credentials unless + * the credentials are resolved dynamically (e.g. {@code HikariCredentialsProvider}, the AWS JDBC wrapper secrets + * plugin, or a custom {@code DataSource} that fetches credentials from a vault at connect time). + * + * @param ds the DataSource whose connections should be evicted + * @param source an opaque label for the rotation event (used in log messages only) + */ + public static void evictDataSourceConnections(DataSource ds, Object source) { + // HikariCP: softEvictConnections() is defined on HikariPoolMXBean, not on HikariDataSource directly. + // We retrieve the MXBean via getHikariPoolMXBean() (a public method on HikariDataSource) using reflection + // so that the caller does not need a compile-time dependency on HikariCP. + try { + Method getPoolMXBean = ds.getClass().getMethod("getHikariPoolMXBean"); + Object poolMXBean = getPoolMXBean.invoke(ds); + if (poolMXBean != null) { + Method softEvict = poolMXBean.getClass().getMethod("softEvictConnections"); + softEvict.invoke(poolMXBean); + LOG.info("Secret rotation (source={}): HikariCP softEvictConnections() called on {}", source, ds); + } else { + LOG.debug("Secret rotation (source={}): HikariCP pool on {} is not started yet; nothing to evict", + source, ds); + } + return; + } catch (NoSuchMethodException e) { + // Not a HikariCP DataSource — fall through to next pool check + } catch (InvocationTargetException e) { + LOG.warn("Secret rotation (source={}): HikariCP softEvictConnections() failed on {}", source, ds, + e.getCause() != null ? e.getCause() : e); + return; + } catch (Exception e) { + LOG.warn("Secret rotation (source={}): HikariCP softEvictConnections() failed on {}", source, ds, e); + return; + } + + // Agroal (Quarkus default pool): AgroalDataSource.flush(FlushMode.GRACEFUL). + // GRACEFUL closes idle connections immediately and active connections when returned to the pool. + // Resolved through the public AgroalDataSource interface (not ds.getClass()) so that proxy + // or wrapper classes work correctly. No compile-time dependency on agroal-api. + try { + Class<?> agroalDsClass = Class.forName("io.agroal.api.AgroalDataSource"); Review Comment: `Class.forName(String)` resolves against camel-support's own classloader. If Agroal lives in a different (child) classloader than camel-support, this throws `ClassNotFoundException` and silently falls through to the generic fallback. Resolving it via the DataSource's classloader would be sturdier: ```java Class<?> agroalDsClass = Class.forName("io.agroal.api.AgroalDataSource", false, ds.getClass().getClassLoader()); ``` -- 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]
