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 8c8028a87345 CAMEL-26969: camel-azure-servicebus - the Camel lock 
renewer stops renewing a message lock after one transient renewal error (#30134)
8c8028a87345 is described below

commit 8c8028a873451a67d879a8a36025c4874040d31a
Author: allthingssecurity <[email protected]>
AuthorDate: Mon Nov 16 18:59:59 2026 +0530

    CAMEL-26969: camel-azure-servicebus - the Camel lock renewer stops renewing 
a message lock after one transient renewal error (#30134)
    
    Follow-up of CAMEL-23937 and CAMEL-25701 (the Camel lock renewer). Found by 
the DST world in #30130 (CAMEL-26956): its default set accepts this case, and 
`-Ddst.servicebus.strictRenew=true` fails 27 of 1000 seeds. Reproduced here 
with a plain unit test, outside the DST harness.
    
    The lock renewer of `ServiceBusConsumer` (peek-lock, 
`maxAutoLockRenewDuration` > 0) renews a lock that expires within its next two 
runs. When `renewalClient.renewMessageLock(message)` failed, the error callback 
removed the exchange from the renewer, whatever the error: the lock was never 
renewed again. In azure-messaging-servicebus 7.17 that call makes one request 
(javap of 7.17.16: no retry policy around `renewMessageLock`), so one transient 
error, such as a busy server or a connect [...]
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../azure/servicebus/ServiceBusConsumer.java       | 22 ++++++--
 .../azure/servicebus/ServiceBusConsumerTest.java   | 61 ++++++++++++++++++++++
 2 files changed, 80 insertions(+), 3 deletions(-)

diff --git 
a/components/camel-azure/camel-azure-servicebus/src/main/java/org/apache/camel/component/azure/servicebus/ServiceBusConsumer.java
 
b/components/camel-azure/camel-azure-servicebus/src/main/java/org/apache/camel/component/azure/servicebus/ServiceBusConsumer.java
index 147f9da3902f..edc241df0828 100644
--- 
a/components/camel-azure/camel-azure-servicebus/src/main/java/org/apache/camel/component/azure/servicebus/ServiceBusConsumer.java
+++ 
b/components/camel-azure/camel-azure-servicebus/src/main/java/org/apache/camel/component/azure/servicebus/ServiceBusConsumer.java
@@ -28,6 +28,7 @@ import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Collectors;
 
 import com.azure.messaging.servicebus.ServiceBusErrorContext;
+import com.azure.messaging.servicebus.ServiceBusException;
 import com.azure.messaging.servicebus.ServiceBusProcessorClient;
 import com.azure.messaging.servicebus.ServiceBusReceivedMessage;
 import com.azure.messaging.servicebus.ServiceBusReceivedMessageContext;
@@ -402,14 +403,29 @@ public class ServiceBusConsumer extends DefaultConsumer 
implements ShutdownAware
                                                 (k, e) -> new 
LockRenewerEntry(e.message, newLockedUntil, e.startTime));
                                     },
                                     error -> {
-                                        LOG.warn("Failed to renew lock for 
exchangeId {}: {}", exchangeId,
-                                                error.getMessage());
-                                        entries.remove(exchangeId);
+                                        if (isTransient(error)) {
+                                            // the SDK makes one attempt: keep 
the entry, so the next run renews
+                                            // the lock again while it is 
still held
+                                            LOG.warn("Failed to renew lock for 
exchangeId {}, will try again: {}",
+                                                    exchangeId, 
error.getMessage());
+                                        } else {
+                                            LOG.warn("Failed to renew lock for 
exchangeId {}: {}", exchangeId,
+                                                    error.getMessage());
+                                            entries.remove(exchangeId);
+                                        }
                                     });
                 }
             }
         }
 
+        /**
+         * A transient error (such as a busy server or a connection that was 
lost) can be retried, as the processor of
+         * the Service Bus .NET SDK does for its own lock renewal; another 
error (such as a lost lock) ends the renewal.
+         */
+        private static boolean isTransient(Throwable error) {
+            return error instanceof ServiceBusException sbe && 
sbe.isTransient();
+        }
+
         private record LockRenewerEntry(ServiceBusReceivedMessage message, 
OffsetDateTime lockedUntil, Instant startTime) {
         }
     }
diff --git 
a/components/camel-azure/camel-azure-servicebus/src/test/java/org/apache/camel/component/azure/servicebus/ServiceBusConsumerTest.java
 
b/components/camel-azure/camel-azure-servicebus/src/test/java/org/apache/camel/component/azure/servicebus/ServiceBusConsumerTest.java
index 005ecc5ffbc7..6a57138b418b 100644
--- 
a/components/camel-azure/camel-azure-servicebus/src/test/java/org/apache/camel/component/azure/servicebus/ServiceBusConsumerTest.java
+++ 
b/components/camel-azure/camel-azure-servicebus/src/test/java/org/apache/camel/component/azure/servicebus/ServiceBusConsumerTest.java
@@ -24,6 +24,8 @@ import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.function.Consumer;
 
+import com.azure.core.amqp.exception.AmqpErrorCondition;
+import com.azure.core.amqp.exception.AmqpException;
 import com.azure.core.amqp.models.AmqpAnnotatedMessage;
 import com.azure.core.util.BinaryData;
 import com.azure.messaging.servicebus.*;
@@ -729,6 +731,65 @@ public class ServiceBusConsumerTest {
         }
     }
 
+    @Test
+    void lockRenewerRenewsAgainAfterATransientError() throws Exception {
+        
when(configuration.getServiceBusReceiveMode()).thenReturn(ServiceBusReceiveMode.PEEK_LOCK);
+        when(configuration.getMaxAutoLockRenewDuration()).thenReturn(300000L);
+        when(messageContext.getMessage()).thenReturn(message);
+        // the lock expires before the next runs of the renewer
+        
when(message.getLockedUntil()).thenReturn(OffsetDateTime.now().plusSeconds(15));
+        // as the SDK reports a busy server: a transient error, which can be 
retried
+        ServiceBusException busy = new ServiceBusException(
+                new AmqpException(true, AmqpErrorCondition.SERVER_BUSY_ERROR, 
"server busy", null),
+                ServiceBusErrorSource.RENEW_LOCK);
+        
when(renewalClient.renewMessageLock(any(ServiceBusReceivedMessage.class)))
+                .thenReturn(Mono.error(busy))
+                .thenReturn(Mono.just(OffsetDateTime.now().plusMinutes(1)));
+        try (ServiceBusConsumer consumer = new ServiceBusConsumer(endpoint, 
processor)) {
+            consumer.doStart();
+
+            ArgumentCaptor<Runnable> taskCaptor = ArgumentCaptor.captor();
+            
verify(periodTaskScheduler).schedulePeriodTask(taskCaptor.capture(), anyLong());
+            processMessageCaptor.getValue().accept(messageContext);
+
+            taskCaptor.getValue().run();
+            verify(renewalClient, times(1)).renewMessageLock(message);
+
+            // the next run renews the lock it still holds
+            taskCaptor.getValue().run();
+            verify(renewalClient, times(2)).renewMessageLock(message);
+
+            // the lock was renewed for another minute: the run after does not 
renew it
+            taskCaptor.getValue().run();
+            verify(renewalClient, times(2)).renewMessageLock(message);
+        }
+    }
+
+    @Test
+    void lockRenewerStopsAfterTheLockIsLost() throws Exception {
+        
when(configuration.getServiceBusReceiveMode()).thenReturn(ServiceBusReceiveMode.PEEK_LOCK);
+        when(configuration.getMaxAutoLockRenewDuration()).thenReturn(300000L);
+        when(messageContext.getMessage()).thenReturn(message);
+        
when(message.getLockedUntil()).thenReturn(OffsetDateTime.now().plusSeconds(15));
+        // as the SDK reports a lock that expired or a message that was 
settled: not transient
+        ServiceBusException lockLost = new ServiceBusException(
+                new AmqpException(false, AmqpErrorCondition.MESSAGE_LOCK_LOST, 
"lock lost", null),
+                ServiceBusErrorSource.RENEW_LOCK);
+        
when(renewalClient.renewMessageLock(any(ServiceBusReceivedMessage.class))).thenReturn(Mono.error(lockLost));
+        try (ServiceBusConsumer consumer = new ServiceBusConsumer(endpoint, 
processor)) {
+            consumer.doStart();
+
+            ArgumentCaptor<Runnable> taskCaptor = ArgumentCaptor.captor();
+            
verify(periodTaskScheduler).schedulePeriodTask(taskCaptor.capture(), anyLong());
+            processMessageCaptor.getValue().accept(messageContext);
+
+            taskCaptor.getValue().run();
+            taskCaptor.getValue().run();
+
+            verify(renewalClient, times(1)).renewMessageLock(message);
+        }
+    }
+
     @Test
     void lockRenewerDoesNotRenewALockThatOutlastsTheNextRuns() throws 
Exception {
         
when(configuration.getServiceBusReceiveMode()).thenReturn(ServiceBusReceiveMode.PEEK_LOCK);

Reply via email to