gnodet-bot commented on code in PR #29489: URL: https://github.com/apache/camel/pull/29489#discussion_r4540995434
########## components/camel-azure/camel-azure-iothub/src/main/java/org/apache/camel/component/azure/iothub/client/DeviceClientPool.java: ########## @@ -0,0 +1,602 @@ +/* + * 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.azure.iothub.client; + +import java.time.Duration; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; +import java.util.function.LongSupplier; + +import com.microsoft.azure.sdk.iot.device.ConnectionStatusChangeContext; +import com.microsoft.azure.sdk.iot.device.DeviceClient; +import com.microsoft.azure.sdk.iot.device.IotHubClientProtocol; +import com.microsoft.azure.sdk.iot.device.IotHubConnectionStatusChangeReason; +import com.microsoft.azure.sdk.iot.device.transport.IotHubConnectionStatus; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Thread-safe pool of Azure IoT Hub {@link DeviceClient} instances, keyed by the resolved hub/device/module identity + * derived from the connection string instead of only the raw device id. + * + * <p> + * Clients are created lazily, reused across messages, and evicted after an idle timeout. Connection status changes are + * monitored through the Azure SDK callback, and disconnected clients are retired automatically; any in-use client is + * closed only when the final lease is released. Callers may also remove a client from the pool explicitly during + * cleanup. + * </p> + */ +public class DeviceClientPool { + + private static final Logger LOG = LoggerFactory.getLogger(DeviceClientPool.class); + + @FunctionalInterface + public interface DeviceClientFactory { + DeviceClient create(String connectionString, IotHubClientProtocol protocol) throws Exception; + } + + private static final class PooledClient { + final DeviceClient client; + volatile long lastUsed; + final AtomicInteger inUse = new AtomicInteger(); + volatile boolean retired; + final AtomicBoolean closed = new AtomicBoolean(); + + PooledClient(DeviceClient client, long nowMillis) { + this.client = client; + this.lastUsed = nowMillis; + } + + void touch(long nowMillis) { + this.lastUsed = nowMillis; + } + + void acquire() { + inUse.incrementAndGet(); + } + + boolean release() { + return inUse.decrementAndGet() == 0; + } + } + + public static final class Lease implements AutoCloseable { + private final DeviceClientPool pool; + private final String poolKey; + private final PooledClient pooled; + private boolean closed; + + private Lease(DeviceClientPool pool, String poolKey, PooledClient pooled) { + this.pool = pool; + this.poolKey = poolKey; + this.pooled = pooled; + } + + public DeviceClient getClient() { + return pooled.client; + } + + public DeviceClient client() { + return getClient(); + } + + @Override + public void close() { + if (closed) { + return; + } + closed = true; + if (pooled.release()) { + if (pooled.retired) { + pool.closeRetired(pooled); + } else { + pool.touchAndMaybeEvict(poolKey); + } + } + } + } + + private final Duration idleTimeout; + private final Duration evictionInterval; + private final Map<String, PooledClient> pool = new ConcurrentHashMap<>(); + private final Map<String, CompletableFuture<DeviceClient>> creationFutures = new ConcurrentHashMap<>(); + private final AtomicBoolean shuttingDown = new AtomicBoolean(false); + + private DeviceClientFactory deviceClientFactory = DeviceClient::new; + private LongSupplier nowMillisSupplier = System::currentTimeMillis; + private ScheduledExecutorService evictionExecutor; Review Comment: 🚨 **Missing `volatile` — broken double-checked locking** `getOrCreateEvictionExecutor()` reads this field _outside_ the `synchronized` block (see below). The JMM requires `volatile` on the guarded field for DCL to be safe — without it, a concurrent reader may see `null` despite the executor being initialized, or worse, may see a partially-constructed object. The same unguarded read in `shutdown()` creates an executor-leak window: if a disconnect callback creates the executor after `shutdown()` has already passed the `if (evictionExecutor != null)` check, the executor thread never terminates. ```suggestion private volatile ScheduledExecutorService evictionExecutor; ``` -- 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]
