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 e237dbbe6677 CAMEL-27503: camel-milo - a value queued on a replaced 
connection no longer ends the loss report of its replacement (#30817)
e237dbbe6677 is described below

commit e237dbbe6677461d067e0783705c6ffc560f045b
Author: allthingssecurity <[email protected]>
AuthorDate: Thu Nov 26 13:29:50 2026 +0530

    CAMEL-27503: camel-milo - a value queued on a replaced connection no longer 
ends the loss report of its replacement (#30817)
    
    Each connection of the milo-client SubscriptionManager has its own Milo
    delivery queue, and a connection torn down after a failed service call
    queues its loss report behind the values it still has queued. With a
    slow route on the old queue, the old connection's value could arrive
    after the new connection had delivered its own value and then reported
    the loss of its session: the node ended with a Good value while
    communication was lost, and the old loss report was dropped because the
    new connection had delivered a value. The generation check of
    CAMEL-25835 guarded the loss reports, not the values.
    
    The node now keeps the generation of the newest connection whose loss
    report it delivered and drops a value of an older connection. An old
    value that arrives after the first value of the new connection, but
    before a newer loss report, is still delivered, as documented. The
    component page says so.
    
    Test: a new CommunicationLossTest method,
    valueOfAReplacedConnectionDoesNotEndTheLossReportOfItsReplacement, holds
    the old delivery queue with a slow route of another node, rebuilds the
    connection through a silent relay, lets the new connection deliver and
    lose its session, then releases the old queue: the node must end with
    Bad_NoCommunication (it ended Good before).
    
    Not changed: an Error during a reconnect or a retry of refused nodes
    ends those attempts for good, and the discovery of the endpoints
    ignores requestTimeout; both get their own tickets.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../camel/catalog/docs/milo-client-component.adoc  |   3 +-
 .../src/main/docs/milo-client-component.adoc       |   3 +-
 .../milo/client/internal/SubscriptionManager.java  |  17 +++-
 .../component/milo/CommunicationLossTest.java      | 105 +++++++++++++++++++++
 4 files changed, 124 insertions(+), 4 deletions(-)

diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/milo-client-component.adoc
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/milo-client-component.adoc
index 981b4434f4ff..01891326dc75 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/milo-client-component.adoc
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/milo-client-component.adoc
@@ -237,7 +237,8 @@ Once communication is restored, every node delivers its 
current value, also when
 existing session, Camel asks the server to send the current values again with 
the `ResendData` method; when the
 server does not provide it, Camel creates the subscription again, and the 
values the server queued during the loss
 are not delivered. Values can arrive more than once after a loss, and a value 
received before Camel rebuilt the
-connection can arrive after the first value of the new connection. A route 
that counts values, computes differences
+connection can arrive after the first value of the new connection, but not 
after a loss report of the new connection:
+the node keeps that status until the new connection delivers a value. A route 
that counts values, computes differences
 or detects changes must tolerate this. A value that did not change during the 
loss keeps its earlier timestamps.
 
 Routes run while communication is lost. An OPC UA request that a route makes 
while the client reconnects, for example
diff --git a/components/camel-milo/src/main/docs/milo-client-component.adoc 
b/components/camel-milo/src/main/docs/milo-client-component.adoc
index 981b4434f4ff..01891326dc75 100644
--- a/components/camel-milo/src/main/docs/milo-client-component.adoc
+++ b/components/camel-milo/src/main/docs/milo-client-component.adoc
@@ -237,7 +237,8 @@ Once communication is restored, every node delivers its 
current value, also when
 existing session, Camel asks the server to send the current values again with 
the `ResendData` method; when the
 server does not provide it, Camel creates the subscription again, and the 
values the server queued during the loss
 are not delivered. Values can arrive more than once after a loss, and a value 
received before Camel rebuilt the
-connection can arrive after the first value of the new connection. A route 
that counts values, computes differences
+connection can arrive after the first value of the new connection, but not 
after a loss report of the new connection:
+the node keeps that status until the new connection delivers a value. A route 
that counts values, computes differences
 or detects changes must tolerate this. A value that did not change during the 
loss keeps its earlier timestamps.
 
 Routes run while communication is lost. An OPC UA request that a route makes 
while the client reconnects, for example
diff --git 
a/components/camel-milo/src/main/java/org/apache/camel/component/milo/client/internal/SubscriptionManager.java
 
b/components/camel-milo/src/main/java/org/apache/camel/component/milo/client/internal/SubscriptionManager.java
index 4c7d45fe8385..b81a87566d44 100644
--- 
a/components/camel-milo/src/main/java/org/apache/camel/component/milo/client/internal/SubscriptionManager.java
+++ 
b/components/camel-milo/src/main/java/org/apache/camel/component/milo/client/internal/SubscriptionManager.java
@@ -267,6 +267,11 @@ public class SubscriptionManager {
          */
         private boolean reported;
 
+        /**
+         * The highest generation of the connections whose loss report was 
delivered, guarded by this
+         */
+        private long lossGeneration;
+
         /**
          * Whether the server refused to monitor the node: its status is that 
refusal, whatever the communication, until
          * its item is created. Set under the lock of the manager when a call 
hands the refusal out for delivery, and
@@ -297,11 +302,18 @@ public class SubscriptionManager {
         }
 
         /**
-         * Delivers a value of the node to the consumer, which ends a loss 
report.
+         * Delivers a value of the node to the consumer, which ends a loss 
report, unless a later connection has
+         * delivered a loss report: the value was then queued on a connection 
that has been replaced, and the report
+         * stays the status of the node until the current connection delivers 
a value.
          *
          * @param generation the generation of the connection that received 
the value
          */
         synchronized void deliver(final long generation, final DataValue 
value) {
+            if (generation < this.lossGeneration) {
+                LOG.debug("Dropping a value of node {} from connection {}: 
connection {} reported a loss since",
+                        nodeId, generation, this.lossGeneration);
+                return;
+            }
             this.generation = Math.max(this.generation, generation);
             this.reported = false;
             this.valueConsumer.accept(value);
@@ -320,7 +332,7 @@ public class SubscriptionManager {
         /**
          * Delivers a loss report to the consumer, unless the node is reported 
or refused already, or a later connection
          * has delivered a value. The report of a replaced connection is 
queued behind its last values, which may race
-         * the first values of its replacement.
+         * the first values of its replacement, and those values are dropped 
once a later connection reports a loss.
          *
          * @param generation the generation of the connection whose 
communication was lost
          */
@@ -329,6 +341,7 @@ public class SubscriptionManager {
                 return;
             }
             this.reported = true;
+            this.lossGeneration = Math.max(this.lossGeneration, generation);
             this.valueConsumer.accept(value);
         }
 
diff --git 
a/components/camel-milo/src/test/java/org/apache/camel/component/milo/CommunicationLossTest.java
 
b/components/camel-milo/src/test/java/org/apache/camel/component/milo/CommunicationLossTest.java
index 467f5543d90a..c2533c4871c0 100644
--- 
a/components/camel-milo/src/test/java/org/apache/camel/component/milo/CommunicationLossTest.java
+++ 
b/components/camel-milo/src/test/java/org/apache/camel/component/milo/CommunicationLossTest.java
@@ -42,12 +42,14 @@ import 
org.apache.camel.component.milo.server.MiloServerComponent;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.test.AvailablePortFinder;
 import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
+import org.eclipse.milo.opcua.sdk.client.subscriptions.OpcUaSubscription;
 import org.eclipse.milo.opcua.stack.core.StatusCodes;
 import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue;
 import org.eclipse.milo.opcua.stack.core.types.builtin.ExpandedNodeId;
 import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
 import org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode;
 import org.eclipse.milo.opcua.stack.core.types.builtin.Variant;
+import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.Timeout;
 import org.junit.jupiter.api.extension.RegisterExtension;
@@ -72,6 +74,10 @@ public class CommunicationLossTest extends 
AbstractMiloServerTest {
 
     private static final String MILO_SERVER_ITEM_1 = "milo-server:myitem1";
 
+    private static final String DIRECT_START_2 = "direct:start2";
+
+    private static final String MILO_SERVER_ITEM_2 = "milo-server:myitem2";
+
     private static final String MILO_CLIENT_BASE = 
"milo-client:opc.tcp://foo:bar@localhost:@@relay@@"
                                                    + 
"?allowedSecurityPolicies=None&overrideHost=true&overridePort=true"
                                                    + 
"&samplingInterval=50&requestedPublishingInterval=100";
@@ -79,6 +85,9 @@ public class CommunicationLossTest extends 
AbstractMiloServerTest {
     private static final String MILO_CLIENT_ITEM_1
             = MILO_CLIENT_BASE + "&node=" + 
NodeIds.nodeValue(MiloServerComponent.DEFAULT_NAMESPACE_URI, "myitem1");
 
+    private static final String MILO_CLIENT_ITEM_2
+            = MILO_CLIENT_BASE + "&node=" + 
NodeIds.nodeValue(MiloServerComponent.DEFAULT_NAMESPACE_URI, "myitem2");
+
     private static final String MOCK_ITEM_1 = "mock:item1";
 
     private static final String MOCK_LATE = "mock:late";
@@ -105,6 +114,9 @@ public class CommunicationLossTest extends 
AbstractMiloServerTest {
     @Produce(DIRECT_START_1)
     protected ProducerTemplate producer1;
 
+    @Produce(DIRECT_START_2)
+    protected ProducerTemplate producer2;
+
     @RegisterExtension
     AvailablePortFinder.Port relayPort = AvailablePortFinder.find();
 
@@ -136,6 +148,7 @@ public class CommunicationLossTest extends 
AbstractMiloServerTest {
             @Override
             public void configure() {
                 from(DIRECT_START_1).to(MILO_SERVER_ITEM_1);
+                from(DIRECT_START_2).to(MILO_SERVER_ITEM_2);
 
                 // the consumers deliver the current values while they start, 
so the tests start these routes
                 
from(throughRelay(MILO_CLIENT_ITEM_1)).routeId("item1").autoStartup(false).to(MOCK_ITEM_1);
@@ -401,6 +414,98 @@ public class CommunicationLossTest extends 
AbstractMiloServerTest {
         }
     }
 
+    /**
+     * A value that a connection still had queued for delivery when it was 
replaced does not end the loss report of the
+     * connection that replaced it: the node keeps the status of the current 
connection, whose communication is lost.
+     * Another node holds the delivery queue of the first connection (a slow 
route), so the value of the node waits
+     * behind it until the second connection has delivered its values and lost 
its session.
+     */
+    @Test
+    @Timeout(value = 90, unit = TimeUnit.SECONDS)
+    void valueOfAReplacedConnectionDoesNotEndTheLossReportOfItsReplacement() 
throws Exception {
+        producer1.sendBody("Foo");
+        producer2.sendBody("Slow");
+
+        // a connection of its own through the relay, whose requests time out 
after a second
+        final MiloClientEndpoint endpoint = context.getEndpoint(
+                throughRelay(MILO_CLIENT_ITEM_1) + "&requestTimeout=1000", 
MiloClientEndpoint.class);
+        final MiloClientEndpoint slowEndpoint = context.getEndpoint(
+                throughRelay(MILO_CLIENT_ITEM_2) + "&requestTimeout=1000", 
MiloClientEndpoint.class);
+        final ScheduledExecutorService reconnectExecutor = 
Executors.newSingleThreadScheduledExecutor();
+        final ExecutorService executor = Executors.newSingleThreadExecutor();
+        final SubscriptionManager manager
+                = new SubscriptionManager(endpoint.getConfiguration(), 
reconnectExecutor, executor, 1000);
+        final CountDownLatch slowRouteBusy = new CountDownLatch(1);
+        final CountDownLatch slowRouteDone = new CountDownLatch(1);
+        Thread registering = null;
+        try {
+            final ExpandedNodeId item = 
ExpandedNodeId.parse(endpoint.getNode());
+            final ExpandedNodeId slowItem = 
ExpandedNodeId.parse(slowEndpoint.getNode());
+            final List<DataValue> values = new CopyOnWriteArrayList<>();
+            manager.registerItems(Map.of(item, values::add), 50.0, null, new 
MonitorFilterConfiguration());
+            final List<UInteger> slowHandles = 
manager.registerItems(Map.of(slowItem, value -> {
+                if ("Busy".equals(value.getValue().getValue())) {
+                    slowRouteBusy.countDown();
+                    try {
+                        slowRouteDone.await(60, TimeUnit.SECONDS);
+                    } catch (final InterruptedException e) {
+                        Thread.currentThread().interrupt();
+                    }
+                }
+            }), 50.0, null, new MonitorFilterConfiguration());
+            await().atMost(10, TimeUnit.SECONDS).until(() -> 
!values.isEmpty());
+            final OpcUaClient client = manager.getOpcUaClient();
+            final OpcUaSubscription subscription = 
client.getSubscriptions().get(0);
+
+            // the slow route holds the delivery queue of the first 
connection, and the new value of the node is queued
+            // behind it
+            producer2.sendBody("Busy");
+            assertTrue(slowRouteBusy.await(10, TimeUnit.SECONDS), "The slow 
route did not receive its value");
+            producer1.sendBody("Bar");
+            await().atMost(10, TimeUnit.SECONDS).until(() -> 
subscription.getDeliveryQueue().getQueueSize() >= 1);
+            // the slow consumer stops, so the second connection does not wait 
for its route
+            manager.unregisterItems(slowHandles);
+
+            // the link drops silently: the CreateMonitoredItems call of the 
next consumer times out, which tears the
+            // first connection down (its loss report is queued behind the 
value) and builds the second one
+            relay.silence();
+            final int before = values.size();
+            registering = new Thread(
+                    () -> manager.registerItems(Map.of(slowItem, value -> {
+                    }), 50.0, null, new MonitorFilterConfiguration()),
+                    "register-item");
+            registering.start();
+            await().atMost(30, TimeUnit.SECONDS)
+                    .until(manager::getOpcUaClient, current -> current != null 
&& current != client);
+            await().atMost(10, TimeUnit.SECONDS).until(() -> values.size() > 
before);
+            assertGoodValue("Bar").accept(values.get(values.size() - 1));
+
+            // the second connection loses its session and gets no new one: 
the node reports the loss
+            relay.stop();
+            await().atMost(30, TimeUnit.SECONDS).until(() -> 
isNoCommunication(values.get(values.size() - 1)));
+
+            // the slow route completes, and the first connection delivers 
what it still had queued
+            slowRouteDone.countDown();
+            await().atMost(10, TimeUnit.SECONDS).until(() -> 
subscription.getDeliveryQueue().getQueueSize() == 0
+                    && subscription.getDeliveryQueue().getPending() == 0);
+
+            assertTrue(isNoCommunication(values.get(values.size() - 1)),
+                    "The node ends with a value of the replaced connection 
while communication is lost: " + values);
+        } finally {
+            slowRouteDone.countDown();
+            manager.dispose();
+            reconnectExecutor.shutdownNow();
+            executor.shutdownNow();
+            if (registering != null) {
+                registering.join(10000);
+            }
+        }
+    }
+
+    private static boolean isNoCommunication(final DataValue value) {
+        return value.getStatusCode().getValue() == 
StatusCodes.Bad_NoCommunication;
+    }
+
     /**
      * The report of a consumer that starts while the server cannot be reached 
runs its route, which can wait for a
      * request of the same connection, here a write once the connection is up, 
as a redelivery does. The outcome of the

Reply via email to