This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 2457452faeee CAMEL-26971: camel-alibaba-ots - create the client once 
and without leaking SDK threads (#30066)
2457452faeee is described below

commit 2457452faeee244fb960b9c6c276ec430cd169e7
Author: Saravanakumar Selvaraj <[email protected]>
AuthorDate: Sun Nov 15 18:04:16 2026 +0000

    CAMEL-26971: camel-alibaba-ots - create the client once and without leaking 
SDK threads (#30066)
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 components/camel-alibaba/camel-alibaba-ots/pom.xml |   7 ++
 .../component/alibaba/ots/AlibabaOtsEndpoint.java  |  49 +++++++--
 .../component/alibaba/ots/AlibabaOtsUtils.java     |  27 ++++-
 .../alibaba/ots/AlibabaOtsEndpointTest.java        | 120 +++++++++++++++++++++
 4 files changed, 189 insertions(+), 14 deletions(-)

diff --git a/components/camel-alibaba/camel-alibaba-ots/pom.xml 
b/components/camel-alibaba/camel-alibaba-ots/pom.xml
index b3572fc94be8..89c378a1e9b8 100644
--- a/components/camel-alibaba/camel-alibaba-ots/pom.xml
+++ b/components/camel-alibaba/camel-alibaba-ots/pom.xml
@@ -71,6 +71,13 @@
             <version>${mockito-version}</version>
             <scope>test</scope>
         </dependency>
+
+        <dependency>
+            <groupId>org.awaitility</groupId>
+            <artifactId>awaitility</artifactId>
+            <version>${awaitility-version}</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
 </project>
diff --git 
a/components/camel-alibaba/camel-alibaba-ots/src/main/java/org/apache/camel/component/alibaba/ots/AlibabaOtsEndpoint.java
 
b/components/camel-alibaba/camel-alibaba-ots/src/main/java/org/apache/camel/component/alibaba/ots/AlibabaOtsEndpoint.java
index 5642306fa78e..f370e1d966f1 100644
--- 
a/components/camel-alibaba/camel-alibaba-ots/src/main/java/org/apache/camel/component/alibaba/ots/AlibabaOtsEndpoint.java
+++ 
b/components/camel-alibaba/camel-alibaba-ots/src/main/java/org/apache/camel/component/alibaba/ots/AlibabaOtsEndpoint.java
@@ -106,7 +106,7 @@ public class AlibabaOtsEndpoint extends DefaultEndpoint {
                             + " Camel endpoint stops.",
               displayName = "OTS Client", label = "advanced")
     @Metadata(autowired = true)
-    private SyncClient otsClient;
+    private volatile SyncClient otsClient;
 
     private boolean autowiredOtsClient;
 
@@ -128,19 +128,43 @@ public class AlibabaOtsEndpoint extends DefaultEndpoint {
         throw new UnsupportedOperationException("You cannot consume from this 
endpoint");
     }
 
+    /**
+     * Returns the client that the producer sends its requests with. If no 
client is set or autowired, the first call
+     * creates it from the options of this endpoint, while it holds the lock 
of this endpoint: the calls made at the
+     * same time wait for this creation, and the calls made once the client 
exists do not take the lock. A failed
+     * creation keeps no client, so each call that waited for it, and each 
later call, tries again.
+     */
     public SyncClient initClient() throws Exception {
-        if (otsClient != null) {
-            return otsClient;
+        SyncClient answer = otsClient;
+        if (answer == null) {
+            lock.lock();
+            try {
+                answer = otsClient;
+                if (answer == null) {
+                    answer = createClient();
+                    otsClient = answer;
+                }
+            } finally {
+                lock.unlock();
+            }
         }
-        otsClient = AlibabaOtsUtils.createClient(this);
-        return otsClient;
+        return answer;
+    }
+
+    SyncClient createClient() {
+        return AlibabaOtsUtils.createClient(this);
     }
 
     @Override
     protected void doStop() throws Exception {
-        if (otsClient != null && !autowiredOtsClient) {
-            otsClient.shutdown();
-            otsClient = null;
+        lock.lock();
+        try {
+            if (otsClient != null && !autowiredOtsClient) {
+                otsClient.shutdown();
+                otsClient = null;
+            }
+        } finally {
+            lock.unlock();
         }
         super.doStop();
     }
@@ -198,7 +222,12 @@ public class AlibabaOtsEndpoint extends DefaultEndpoint {
     }
 
     public void setOtsClient(SyncClient otsClient) {
-        this.otsClient = otsClient;
-        this.autowiredOtsClient = otsClient != null;
+        lock.lock();
+        try {
+            this.otsClient = otsClient;
+            this.autowiredOtsClient = otsClient != null;
+        } finally {
+            lock.unlock();
+        }
     }
 }
diff --git 
a/components/camel-alibaba/camel-alibaba-ots/src/main/java/org/apache/camel/component/alibaba/ots/AlibabaOtsUtils.java
 
b/components/camel-alibaba/camel-alibaba-ots/src/main/java/org/apache/camel/component/alibaba/ots/AlibabaOtsUtils.java
index 5b22737f1ab1..08b2a2e8ed1d 100644
--- 
a/components/camel-alibaba/camel-alibaba-ots/src/main/java/org/apache/camel/component/alibaba/ots/AlibabaOtsUtils.java
+++ 
b/components/camel-alibaba/camel-alibaba-ots/src/main/java/org/apache/camel/component/alibaba/ots/AlibabaOtsUtils.java
@@ -22,7 +22,11 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import com.alicloud.openservices.tablestore.ClientConfiguration;
 import com.alicloud.openservices.tablestore.SyncClient;
+import com.alicloud.openservices.tablestore.core.ResourceManager;
+import com.alicloud.openservices.tablestore.core.auth.CredentialsProvider;
+import 
com.alicloud.openservices.tablestore.core.auth.CredentialsProviderFactory;
 import com.alicloud.openservices.tablestore.model.CapacityUnit;
 import com.alicloud.openservices.tablestore.model.Column;
 import com.alicloud.openservices.tablestore.model.ConsumedCapacity;
@@ -54,11 +58,26 @@ public final class AlibabaOtsUtils {
             throw new IllegalArgumentException("Instance name is required");
         }
 
-        return new SyncClient(
-                endpoint.getEndpoint(),
+        // created first, as it rejects an invalid AccessKey pair
+        CredentialsProvider credentialsProvider = 
CredentialsProviderFactory.newDefaultCredentialProvider(
                 OpenApiClientSupport.resolveAccessKey(endpoint.getAccessKey(), 
endpoint.getServiceKeys()),
-                OpenApiClientSupport.resolveSecretKey(endpoint.getSecretKey(), 
endpoint.getServiceKeys()),
-                endpoint.getInstanceName());
+                OpenApiClientSupport.resolveSecretKey(endpoint.getSecretKey(), 
endpoint.getServiceKeys()));
+        ClientConfiguration configuration = new ClientConfiguration();
+        // The ResourceManager starts the threads of the HTTP client of the 
SDK, and the SDK creates it before it rejects
+        // an endpoint or an instance name. Created here, it is shut down when 
the SyncClient constructor fails.
+        // Otherwise the client owns it, and SyncClient.shutdown shuts it down.
+        ResourceManager resourceManager = new ResourceManager(configuration);
+        try {
+            return new SyncClient(
+                    endpoint.getEndpoint(), credentialsProvider, 
endpoint.getInstanceName(), configuration, resourceManager);
+        } catch (RuntimeException e) {
+            try {
+                resourceManager.shutdown();
+            } catch (RuntimeException shutdownFailure) {
+                e.addSuppressed(shutdownFailure);
+            }
+            throw e;
+        }
     }
 
     public static ClientConfigurations 
createClientConfigurations(AlibabaOtsEndpoint endpoint, Exchange exchange) {
diff --git 
a/components/camel-alibaba/camel-alibaba-ots/src/test/java/org/apache/camel/component/alibaba/ots/AlibabaOtsEndpointTest.java
 
b/components/camel-alibaba/camel-alibaba-ots/src/test/java/org/apache/camel/component/alibaba/ots/AlibabaOtsEndpointTest.java
index 76a9ead32c03..cf478620eeb5 100644
--- 
a/components/camel-alibaba/camel-alibaba-ots/src/test/java/org/apache/camel/component/alibaba/ots/AlibabaOtsEndpointTest.java
+++ 
b/components/camel-alibaba/camel-alibaba-ots/src/test/java/org/apache/camel/component/alibaba/ots/AlibabaOtsEndpointTest.java
@@ -16,10 +16,30 @@
  */
 package org.apache.camel.component.alibaba.ots;
 
+import java.lang.management.ManagementFactory;
+import java.lang.management.ThreadInfo;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
 import com.alicloud.openservices.tablestore.SyncClient;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.awaitility.Awaitility.await;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.verify;
@@ -67,4 +87,104 @@ class AlibabaOtsEndpointTest {
         verify(client).shutdown();
         assertThat(endpoint.getOtsClient()).isNull();
     }
+
+    @Test
+    void concurrentInitClientCreatesOneClient() throws Exception {
+        int callers = 8;
+        Set<Thread> creators = ConcurrentHashMap.newKeySet();
+        List<SyncClient> created = new CopyOnWriteArrayList<>();
+        CountDownLatch release = new CountDownLatch(1);
+        AlibabaOtsEndpoint endpoint = new AlibabaOtsEndpoint() {
+            @Override
+            SyncClient createClient() {
+                creators.add(Thread.currentThread());
+                try {
+                    release.await();
+                } catch (InterruptedException e) {
+                    Thread.currentThread().interrupt();
+                    throw new IllegalStateException(e);
+                }
+                SyncClient client = mock(SyncClient.class);
+                created.add(client);
+                return client;
+            }
+        };
+
+        Set<Thread> threads = ConcurrentHashMap.newKeySet();
+        ExecutorService executor = Executors.newFixedThreadPool(callers);
+        try {
+            List<Future<SyncClient>> results = new ArrayList<>();
+            for (int i = 0; i < callers; i++) {
+                results.add(executor.submit(() -> {
+                    threads.add(Thread.currentThread());
+                    return endpoint.initClient();
+                }));
+            }
+            // a creation is held until each caller has started one too, or 
waits for the lock of a caller that has,
+            // so the result does not depend on how the threads are scheduled
+            await().atMost(10, TimeUnit.SECONDS).until(() -> threads.size() == 
callers
+                    && threads.stream().allMatch(thread -> 
creators.contains(thread) || waitsForLockOf(thread, creators)));
+            release.countDown();
+
+            List<SyncClient> clients = new ArrayList<>();
+            for (Future<SyncClient> result : results) {
+                clients.add(result.get(10, TimeUnit.SECONDS));
+            }
+            assertThat(created).hasSize(1);
+            assertThat(clients).hasSize(callers).allSatisfy(client -> 
assertThat(client).isSameAs(created.get(0)));
+            assertThat(endpoint.getOtsClient()).isSameAs(created.get(0));
+
+            endpoint.doStop();
+            verify(created.get(0)).shutdown();
+        } finally {
+            release.countDown();
+            executor.shutdownNow();
+        }
+    }
+
+    @ParameterizedTest
+    @ValueSource(strings = {
+            // InternalClient rejects it in its first checks, as it does not 
start with http:// or https://
+            "my-instance.cn-hangzhou.ots.aliyuncs.com",
+            // OTSUri rejects it after these checks, as it has a path
+            "https://my-instance.cn-hangzhou.ots.aliyuncs.com/path"; })
+    void failedClientCreationLeavesNoSdkThread(String endpointValue) {
+        Set<Thread> threadsBefore = sdkThreads();
+        AlibabaOtsEndpoint endpoint = new AlibabaOtsEndpoint();
+        endpoint.setEndpoint(endpointValue);
+        endpoint.setInstanceName("my-instance");
+        endpoint.setAccessKey("my-access-key");
+        endpoint.setSecretKey("my-secret-key");
+
+        // the endpoint keeps no client when the creation fails, so each 
message creates one again
+        for (int i = 0; i < 3; i++) {
+            
assertThatThrownBy(endpoint::initClient).isInstanceOf(IllegalArgumentException.class);
+        }
+
+        assertThat(endpoint.getOtsClient()).isNull();
+        await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> 
assertThat(sdkThreads()).isSubsetOf(threadsBefore));
+    }
+
+    private static boolean waitsForLockOf(Thread thread, Set<Thread> owners) {
+        ThreadInfo info = 
ManagementFactory.getThreadMXBean().getThreadInfo(thread.threadId());
+        return info != null && owners.stream().anyMatch(owner -> 
owner.threadId() == info.getLockOwnerId());
+    }
+
+    /**
+     * The live threads of the Tablestore SDK: they run code of the SDK, or of 
the HTTP client that the SDK shades.
+     */
+    private static Set<Thread> sdkThreads() {
+        Set<Thread> threads = new HashSet<>();
+        for (Map.Entry<Thread, StackTraceElement[]> entry : 
Thread.getAllStackTraces().entrySet()) {
+            if (isSdkClass(entry.getKey().getClass().getName())
+                    || Arrays.stream(entry.getValue()).anyMatch(frame -> 
isSdkClass(frame.getClassName()))) {
+                threads.add(entry.getKey());
+            }
+        }
+        return threads;
+    }
+
+    private static boolean isSdkClass(String className) {
+        return className.startsWith("com.alicloud.openservices.tablestore.") 
|| className.startsWith("com.aliyun.ots.");
+    }
 }

Reply via email to