Expire negotiation on event loop 68/102668/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 12 Oct 2022 11:54:06 +0000 (13:54 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 13 Oct 2022 13:53:24 +0000 (15:53 +0200)
Rather than having to completely synchronize state transitions, make
sure we run expiry on the event loop.

JIRA: NETCONF-827
Change-Id: I8d02c025ceaf78d547848e7e92861cb125405141
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 0df65029004fe5b4766caf2b47db23e47f955332)

netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/AbstractNetconfSessionNegotiator.java
netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/AbstractNetconfSessionNegotiatorTest.java

index 38d11af59d8e85c7493ed1cd60c3381819a86bc9..a8014974b533364e5e48f8bbce62b33059df329b 100644 (file)
@@ -25,6 +25,7 @@ import io.netty.util.concurrent.Promise;
 import java.util.concurrent.TimeUnit;
 import org.checkerframework.checker.index.qual.NonNegative;
 import org.checkerframework.checker.lock.qual.GuardedBy;
+import org.checkerframework.checker.lock.qual.Holding;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.netconf.api.NetconfDocumentedException;
 import org.opendaylight.netconf.api.NetconfMessage;
@@ -86,8 +87,8 @@ public abstract class AbstractNetconfSessionNegotiator<P extends NetconfSessionP
     private final L sessionListener;
     private final Timer timer;
 
+    @GuardedBy("this")
     private Timeout timeoutTask;
-
     @GuardedBy("this")
     private State state = State.IDLE;
 
@@ -152,15 +153,26 @@ public abstract class AbstractNetconfSessionNegotiator<P extends NetconfSessionP
         sendMessage(helloMessage);
 
         replaceHelloMessageOutboundHandler();
-        changeState(State.OPEN_WAIT);
 
-        timeoutTask = this.timer.newTimeout(this::timeoutExpired, connectionTimeoutMillis, TimeUnit.MILLISECONDS);
+        synchronized (this) {
+            lockedChangeState(State.OPEN_WAIT);
+
+            // Service the timeout on channel's eventloop, so that we do not get state transition problems
+            timeoutTask = timer.newTimeout(unused -> channel.eventLoop().execute(this::timeoutExpired),
+                connectionTimeoutMillis, TimeUnit.MILLISECONDS);
+        }
     }
 
-    private synchronized void timeoutExpired(final Timeout timeout) {
+    private synchronized void timeoutExpired() {
+        if (timeoutTask == null) {
+            // cancelTimeout() between expiry and execution on the loop
+            return;
+        }
+        timeoutTask = null;
+
         if (state != State.ESTABLISHED) {
-            LOG.debug("Connection timeout after {}, session backed by channel {} is in state {}", timeout, channel,
-                state);
+            LOG.debug("Connection timeout after {}ms, session backed by channel {} is in state {}",
+                connectionTimeoutMillis, channel, state);
 
             // Do not fail negotiation if promise is done or canceled
             // It would result in setting result of the promise second time and that throws exception
@@ -182,9 +194,10 @@ public abstract class AbstractNetconfSessionNegotiator<P extends NetconfSessionP
         }
     }
 
-    private void cancelTimeout() {
-        if (timeoutTask != null) {
-            timeoutTask.cancel();
+    private synchronized void cancelTimeout() {
+        if (timeoutTask != null && !timeoutTask.cancel()) {
+            // Late-coming cancel: make sure
+            timeoutTask = null;
         }
     }
 
@@ -258,6 +271,11 @@ public abstract class AbstractNetconfSessionNegotiator<P extends NetconfSessionP
             throws NetconfDocumentedException;
 
     private synchronized void changeState(final State newState) {
+        lockedChangeState(newState);
+    }
+
+    @Holding("this")
+    private void lockedChangeState(final State newState) {
         LOG.debug("Changing state from : {} to : {} for channel: {}", state, newState, channel);
         checkState(isStateChangePermitted(state, newState),
                 "Cannot change state from %s to %s for channel %s", state, newState, channel);
index a5a4a2395cd853c6af7d460724ae02a1e8569b0b..50db35b136f7d73af49ecda0bd86690f2ecc87d8 100644 (file)
@@ -131,6 +131,7 @@ public class AbstractNetconfSessionNegotiatorTest {
         negotiator.startNegotiation();
 
         captor.getValue().run(timeout);
+        channel.runPendingTasks();
         verify(closedDetector).close(any(), any());
     }