gnodet-bot commented on code in PR #29489:
URL: https://github.com/apache/camel/pull/29489#discussion_r4524503805


##########
components/camel-azure/camel-azure-iothub/src/main/java/org/apache/camel/component/azure/iothub/client/DeviceClientPool.java:
##########
@@ -0,0 +1,387 @@
+/*
+ * 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.Iterator;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+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.function.LongSupplier;
+
+import com.microsoft.azure.sdk.iot.device.DeviceClient;
+import com.microsoft.azure.sdk.iot.device.IotHubClientProtocol;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Thread-safe pool of Azure IoT Hub {@link DeviceClient} instances, keyed by 
the device identity in the connection
+ * string instead of only the raw device id.
+ *
+ * <p>
+ * Clients are created lazily, reused across messages, and evicted after an 
idle timeout. The Azure device SDK does not
+ * expose connection state, so broken clients are removed by the caller on 
send failure (see the send operations).
+ * </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();
+
+        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 pooled.client;
+        }
+
+        @Override
+        public void close() {
+            if (closed) {
+                return;
+            }
+            closed = true;
+            if (pooled.release()) {
+                pool.touchAndMaybeEvict(poolKey, pooled);
+            }
+        }
+    }
+
+    private final Duration idleTimeout;
+    private final Duration evictionInterval;
+    private final Map<String, PooledClient> pool = new ConcurrentHashMap<>();
+    private final AtomicBoolean shuttingDown = new AtomicBoolean(false);
+
+    private DeviceClientFactory deviceClientFactory = DeviceClient::new;
+    private LongSupplier nowMillisSupplier = System::currentTimeMillis;
+    private ScheduledExecutorService evictionExecutor;
+
+    public DeviceClientPool(Duration idleTimeout, Duration evictionInterval) {
+        this(idleTimeout, evictionInterval, null);
+    }
+
+    public DeviceClientPool(Duration idleTimeout, Duration evictionInterval, 
ScheduledExecutorService evictionExecutor) {
+        this.idleTimeout = isPositive(idleTimeout) ? idleTimeout : 
Duration.ofMinutes(5);
+        this.evictionInterval = isPositive(evictionInterval) ? 
evictionInterval : Duration.ofSeconds(30);
+        this.evictionExecutor = evictionExecutor;
+    }
+
+    private static boolean isPositive(Duration d) {
+        return d != null && !d.isZero() && !d.isNegative();
+    }
+
+    public void start() {
+        if (evictionExecutor == null) {
+            evictionExecutor = Executors.newSingleThreadScheduledExecutor(r -> 
{
+                Thread t = new Thread(r, 
"camel-azure-iothub-device-client-evictor");
+                t.setDaemon(true);
+                return t;
+            });
+        }
+        evictionExecutor.scheduleAtFixedRate(this::evictIdleClients,
+                evictionInterval.toMillis(), evictionInterval.toMillis(), 
TimeUnit.MILLISECONDS);
+        LOG.info("DeviceClient eviction enabled. idleTimeout={}s, 
interval={}s",
+                idleTimeout.getSeconds(), evictionInterval.getSeconds());
+    }
+
+    public DeviceClient getOrCreate(String deviceId, String connectionString, 
IotHubClientProtocol protocol) {
+        if (Thread.currentThread().isInterrupted() || shuttingDown.get()) {
+            throw new IllegalStateException("Skipping client creation due to 
shutdown: " + deviceId);
+        }
+
+        String poolKey = resolvePoolKey(deviceId, connectionString);
+        final long now = nowMillisSupplier.getAsLong();
+
+        PooledClient existing = pool.get(poolKey);
+        if (existing != null) {
+            existing.touch(now);
+            return existing.client;
+        }
+
+        DeviceClient client = createClient(deviceId, connectionString, 
protocol);
+        PooledClient pooled = new PooledClient(client, now);
+        PooledClient previous = pool.putIfAbsent(poolKey, pooled);
+        if (previous != null) {
+            pooled.client.close();
+            previous.touch(now);
+            return previous.client;
+        }
+        return pooled.client;
+    }
+
+    public Lease acquire(String deviceId, String connectionString, 
IotHubClientProtocol protocol) {
+        if (Thread.currentThread().isInterrupted() || shuttingDown.get()) {
+            throw new IllegalStateException("Skipping client creation due to 
shutdown: " + deviceId);
+        }
+
+        String poolKey = resolvePoolKey(deviceId, connectionString);
+        final long now = nowMillisSupplier.getAsLong();
+
+        PooledClient existing = pool.get(poolKey);
+        if (existing != null) {
+            existing.touch(now);
+            existing.acquire();
+            return new Lease(this, poolKey, existing);
+        }
+
+        DeviceClient client = createClient(deviceId, connectionString, 
protocol);
+        PooledClient pooled = new PooledClient(client, now);
+        PooledClient previous = pool.putIfAbsent(poolKey, pooled);

Review Comment:
   ⚠️ **TOCTOU race** — `createClient()` is called at L185 before 
`putIfAbsent()` at L187. If two threads race, both call `createClient()` and 
open an SDK connection; the loser closes its connection. For Azure IoT Hub 
MQTT/AMQP, opening a second connection for the same `deviceId` can trigger 
error 400027 (device already connected), which may also drop the winner's 
connection.
   
   Consider a `ConcurrentHashMap<String, Future<PooledClient>>` or a striped 
lock on `deviceId` so `createClient()` is only called once per key.



##########
components/camel-azure/camel-azure-iothub/src/main/java/org/apache/camel/component/azure/iothub/client/DeviceClientPool.java:
##########
@@ -0,0 +1,387 @@
+/*
+ * 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.Iterator;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+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.function.LongSupplier;
+
+import com.microsoft.azure.sdk.iot.device.DeviceClient;
+import com.microsoft.azure.sdk.iot.device.IotHubClientProtocol;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Thread-safe pool of Azure IoT Hub {@link DeviceClient} instances, keyed by 
the device identity in the connection
+ * string instead of only the raw device id.
+ *
+ * <p>
+ * Clients are created lazily, reused across messages, and evicted after an 
idle timeout. The Azure device SDK does not
+ * expose connection state, so broken clients are removed by the caller on 
send failure (see the send operations).
+ * </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();
+
+        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 pooled.client;
+        }
+
+        @Override
+        public void close() {
+            if (closed) {
+                return;
+            }
+            closed = true;
+            if (pooled.release()) {
+                pool.touchAndMaybeEvict(poolKey, pooled);
+            }
+        }
+    }
+
+    private final Duration idleTimeout;
+    private final Duration evictionInterval;
+    private final Map<String, PooledClient> pool = new ConcurrentHashMap<>();
+    private final AtomicBoolean shuttingDown = new AtomicBoolean(false);
+
+    private DeviceClientFactory deviceClientFactory = DeviceClient::new;
+    private LongSupplier nowMillisSupplier = System::currentTimeMillis;
+    private ScheduledExecutorService evictionExecutor;
+
+    public DeviceClientPool(Duration idleTimeout, Duration evictionInterval) {
+        this(idleTimeout, evictionInterval, null);
+    }
+
+    public DeviceClientPool(Duration idleTimeout, Duration evictionInterval, 
ScheduledExecutorService evictionExecutor) {
+        this.idleTimeout = isPositive(idleTimeout) ? idleTimeout : 
Duration.ofMinutes(5);
+        this.evictionInterval = isPositive(evictionInterval) ? 
evictionInterval : Duration.ofSeconds(30);
+        this.evictionExecutor = evictionExecutor;
+    }
+
+    private static boolean isPositive(Duration d) {
+        return d != null && !d.isZero() && !d.isNegative();
+    }
+
+    public void start() {
+        if (evictionExecutor == null) {
+            evictionExecutor = Executors.newSingleThreadScheduledExecutor(r -> 
{
+                Thread t = new Thread(r, 
"camel-azure-iothub-device-client-evictor");
+                t.setDaemon(true);
+                return t;
+            });
+        }
+        evictionExecutor.scheduleAtFixedRate(this::evictIdleClients,
+                evictionInterval.toMillis(), evictionInterval.toMillis(), 
TimeUnit.MILLISECONDS);
+        LOG.info("DeviceClient eviction enabled. idleTimeout={}s, 
interval={}s",
+                idleTimeout.getSeconds(), evictionInterval.getSeconds());
+    }
+
+    public DeviceClient getOrCreate(String deviceId, String connectionString, 
IotHubClientProtocol protocol) {
+        if (Thread.currentThread().isInterrupted() || shuttingDown.get()) {
+            throw new IllegalStateException("Skipping client creation due to 
shutdown: " + deviceId);
+        }
+
+        String poolKey = resolvePoolKey(deviceId, connectionString);
+        final long now = nowMillisSupplier.getAsLong();
+
+        PooledClient existing = pool.get(poolKey);
+        if (existing != null) {
+            existing.touch(now);
+            return existing.client;
+        }
+
+        DeviceClient client = createClient(deviceId, connectionString, 
protocol);
+        PooledClient pooled = new PooledClient(client, now);
+        PooledClient previous = pool.putIfAbsent(poolKey, pooled);
+        if (previous != null) {
+            pooled.client.close();
+            previous.touch(now);
+            return previous.client;
+        }
+        return pooled.client;
+    }
+
+    public Lease acquire(String deviceId, String connectionString, 
IotHubClientProtocol protocol) {
+        if (Thread.currentThread().isInterrupted() || shuttingDown.get()) {
+            throw new IllegalStateException("Skipping client creation due to 
shutdown: " + deviceId);
+        }
+
+        String poolKey = resolvePoolKey(deviceId, connectionString);
+        final long now = nowMillisSupplier.getAsLong();
+
+        PooledClient existing = pool.get(poolKey);
+        if (existing != null) {
+            existing.touch(now);
+            existing.acquire();
+            return new Lease(this, poolKey, existing);
+        }
+
+        DeviceClient client = createClient(deviceId, connectionString, 
protocol);
+        PooledClient pooled = new PooledClient(client, now);
+        PooledClient previous = pool.putIfAbsent(poolKey, pooled);
+        if (previous != null) {
+            pooled.client.close();
+            previous.touch(now);
+            previous.acquire();
+            return new Lease(this, poolKey, previous);
+        }
+        pooled.acquire();
+        return new Lease(this, poolKey, pooled);
+    }
+
+    public Lease lease(String deviceId, String connectionString, 
IotHubClientProtocol protocol) {
+        return acquire(deviceId, connectionString, protocol);
+    }
+
+    public void remove(String deviceId, String connectionString) {
+        String poolKey = resolvePoolKey(deviceId, connectionString);
+        PooledClient pooled = pool.remove(poolKey);
+        if (pooled != null) {
+            closeQuietly(deviceId, pooled.client, "explicit removal");
+        }
+    }
+
+    public void remove(String deviceId) {
+        if (deviceId == null || deviceId.isBlank()) {
+            return;
+        }
+
+        PooledClient pooled = pool.remove(deviceId);
+        if (pooled != null) {
+            closeQuietly(deviceId, pooled.client, "explicit removal");
+            return;
+        }
+
+        for (Iterator<Map.Entry<String, PooledClient>> it = 
pool.entrySet().iterator(); it.hasNext();) {
+            Map.Entry<String, PooledClient> entry = it.next();
+            String key = entry.getKey();
+            if (matchesDeviceId(key, deviceId)) {
+                if (pool.remove(key, entry.getValue())) {
+                    closeQuietly(deviceId, entry.getValue().client, "explicit 
removal");
+                    return;
+                }
+            }
+        }
+    }
+
+    private static boolean matchesDeviceId(String poolKey, String deviceId) {
+        if (poolKey == null) {
+            return false;
+        }
+        if (poolKey.equals(deviceId)) {
+            return true;
+        }
+        String[] parts = poolKey.split(":");
+        return parts.length >= 2 && parts[1].equals(deviceId);
+    }
+
+    private static String resolvePoolKey(String deviceId, String 
connectionString) {
+        if (deviceId == null || deviceId.isBlank()) {
+            return connectionString;
+        }
+        String[] items = connectionString == null ? new String[0] : 
connectionString.split(";");
+        String hostName = null;
+        String csDeviceId = null;
+        String moduleId = null;
+        for (String item : items) {
+            if (item == null || item.isBlank()) {
+                continue;
+            }
+            int eq = item.indexOf('=');
+            if (eq <= 0) {
+                continue;
+            }
+            String key = item.substring(0, eq).trim();
+            String value = item.substring(eq + 1).trim();
+            switch (key) {
+                case "HostName" -> hostName = value;
+                case "DeviceId" -> csDeviceId = value;
+                case "ModuleId" -> moduleId = value;
+                default -> {
+                    // ignore other connection-string fields
+                }
+            }
+        }
+        if (csDeviceId != null && !csDeviceId.equals(deviceId)) {
+            throw new IllegalArgumentException(
+                    "Device id '" + deviceId + "' does not match the 
connection string's DeviceId '" + csDeviceId + "'");
+        }
+        StringBuilder identity = new StringBuilder();
+        if (hostName != null && !hostName.isBlank()) {
+            identity.append(hostName);
+        }
+        if (csDeviceId != null && !csDeviceId.isBlank()) {
+            if (!identity.isEmpty()) {
+                identity.append(':');
+            }
+            identity.append(csDeviceId);
+        }
+        if (moduleId != null && !moduleId.isBlank()) {
+            if (!identity.isEmpty()) {
+                identity.append(':');
+            }
+            identity.append(moduleId);
+        }
+        return identity.length() > 0 ? identity.toString() : deviceId;
+    }
+
+    private DeviceClient createClient(String deviceId, String 
connectionString, IotHubClientProtocol protocol) {
+        try {
+            DeviceClient client = deviceClientFactory.create(connectionString, 
protocol);
+            client.open(true);
+            // Avoid duplicate desired-property subscriptions: subscribe only 
if not already subscribed.
+            // The Azure DeviceClient does not expose a way to check, so we 
rely on only subscribing once per client creation.
+            client.subscribeToDesiredProperties((twinUpdate, ctx) -> {
+            }, null);
+            LOG.debug("Created new DeviceClient for device: {}", deviceId);
+            return client;
+        } catch (Exception e) {
+            if (isShutdownInterruption(e)) {
+                Thread.currentThread().interrupt();
+                LOG.warn("DeviceClient creation interrupted due to shutdown 
for device: {}", deviceId);
+                return null;
+            }
+            throw new RuntimeException("DeviceClient creation failed for 
device: " + deviceId, e);
+        }
+    }
+
+    private void touchAndMaybeEvict(String poolKey, PooledClient pooled) {
+        pooled.touch(nowMillisSupplier.getAsLong());
+        if ((nowMillisSupplier.getAsLong() - pooled.lastUsed) > 
idleTimeout.toMillis() && pool.remove(poolKey, pooled)) {

Review Comment:
   🐛 **Dead eviction branch** — `touch()` sets `lastUsed = t₁`, then 
`nowMillisSupplier.getAsLong()` is called again for `t₂`. Because `t₂ − t₁ ≈ 0 
ns`, the check `(t₂ − t₁) > idleTimeout.toMillis()` is **always false** — 
eviction never fires.
   
   Capture the timestamp once before the touch:
   ```suggestion
           long now = nowMillisSupplier.getAsLong();
           long elapsed = now - pooled.lastUsed;
           pooled.touch(now);
           if (elapsed > idleTimeout.toMillis() && pool.remove(poolKey, 
pooled)) {
   ```



##########
components/camel-azure/camel-azure-iothub/src/main/java/org/apache/camel/component/azure/iothub/operations/TelemetryDeliveryStrategy.java:
##########
@@ -0,0 +1,76 @@
+/*
+ * 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.operations;
+
+import java.nio.charset.StandardCharsets;
+import java.time.Duration;
+import java.util.UUID;
+
+import com.microsoft.azure.sdk.iot.device.DeviceClient;
+import com.microsoft.azure.sdk.iot.device.Message;
+import org.apache.camel.component.azure.iothub.IoTHubDeliveryMode;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Sends the body as a device-to-cloud telemetry message. The call blocks 
until the hub acknowledges delivery or the
+ * configured timeout elapses.
+ */
+public class TelemetryDeliveryStrategy implements DeliveryStrategy {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(TelemetryDeliveryStrategy.class);
+
+    private final String contentType;
+    private final Duration sendTimeout;
+
+    public TelemetryDeliveryStrategy(String contentType, Duration sendTimeout) 
{
+        this.contentType = contentType;
+        this.sendTimeout = sendTimeout;
+    }
+
+    @Override
+    public void execute(DeviceClient client, String deviceId, DeliveryContext 
context) throws Exception {
+        byte[] payload = toBytes(context.telemetry());
+
+        Message msg = new Message(payload);
+        msg.setContentType(contentType);
+        msg.setContentEncoding(StandardCharsets.UTF_8.name());
+        msg.setMessageId(UUID.randomUUID().toString());
+
+        int timeoutMillis = Math.toIntExact(sendTimeout.toMillis());
+        LOG.debug("Dispatching telemetry for device: {} with timeout {}ms", 
deviceId, timeoutMillis);
+        client.sendEvent(msg, timeoutMillis);
+        LOG.debug("Telemetry sent for device: {}", deviceId);
+    }
+
+    private byte[] toBytes(Object body) {

Review Comment:
   ⚠️ **`InputStreamCache` not handled** — `toBytes()` only handles `byte[]` 
and `String`. Camel's stream caching converts `InputStream` bodies to 
`InputStreamCache` before producers run, so any route that sources a stream 
will throw `IllegalArgumentException` here.
   
   Use `exchange.getIn().getMandatoryBody(byte[].class)` at the call site 
instead, which routes through all TypeConverters:
   ```suggestion
           throw new IllegalArgumentException(
                   "Telemetry delivery expects a byte[] or String body but was: 
"
                                              + (body == null ? "null" : 
body.getClass().getName()));
   ```
   Actually the fix should be in the caller — replace 
`toBytes(context.telemetry())` with 
`exchange.getIn().getMandatoryBody(byte[].class)` and remove `toBytes()` 
entirely.



-- 
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]

Reply via email to