psalagnac commented on code in PR #5301:
URL: https://github.com/apache/solr/pull/5301#discussion_r4584385634


##########
solr/solrj-zookeeper/src/java/org/apache/solr/common/cloud/ConnectionManager.java:
##########
@@ -102,24 +112,99 @@ public ConnectionManager(
     this.isClosedCheck = isClosed;
   }
 
-  private synchronized void connected() {
+  /**
+   * Receives the events of a single ZooKeeper instance. An expiration is 
ignored once a newer
+   * instance has been created, since it must not undo the recovery from the 
session it was for.
+   */
+  final class SessionWatcher implements Watcher {
+    private final long generation = sessionGeneration.incrementAndGet();
+
+    long nextEventSequence() {
+      return ConnectionManager.this.nextEventSequence();
+    }
+
+    void process(WatchedEvent event, long sequence) {
+      ConnectionManager.this.process(event, sequence, generation);
+    }
+
+    @Override
+    public void process(WatchedEvent event) {
+      process(event, nextEventSequence());
+    }
+  }
+
+  SessionWatcher newSessionWatcher() {
+    return new SessionWatcher();
+  }
+
+  /**
+   * Numbers an event in delivery order. Must be called from the thread that 
ZooKeeper delivers
+   * events on, before the event is handed off to another thread.
+   */
+  long nextEventSequence() {
+    return eventSequence.incrementAndGet();
+  }
+
+  /** Returns false, without changing state, if a newer state event has 
already been applied. */
+  private synchronized boolean connected(long sequence) {
+    if (!acceptStateEvent(sequence)) {
+      return false;
+    }
     connected = true;
     likelyExpiredState = LikelyExpiredState.NOT_EXPIRED;
     notifyAll();
+    return true;
   }
 
-  private synchronized void disconnected() {
+  /** Returns false, without changing state, if a newer state event has 
already been applied. */
+  private synchronized boolean disconnected(long sequence) {
+    if (!acceptStateEvent(sequence)) {
+      return false;
+    }
     connected = false;
     // record the time we expired unless we are already likely expired
     if (!likelyExpiredState.isLikelyExpired(0)) {
       likelyExpiredState =
           new LikelyExpiredState(LikelyExpiredState.StateType.TRACKING_TIME, 
System.nanoTime());
     }
     notifyAll();
+    return true;
+  }
+
+  /**
+   * Returns false, without changing state, if a newer ZooKeeper instance has 
replaced the one that
+   * expired. Events from the expired session that are numbered before this 
one are ignored from now
+   * on.
+   */
+  private synchronized boolean expired(long sequence, long generation) {
+    if (generation < sessionGeneration.get()) {
+      return false;
+    }
+    lastStateEventSequence = Math.max(lastStateEventSequence, sequence);
+    // we don't call disconnected here, because we know we are expired
+    connected = false;
+    likelyExpiredState = LikelyExpiredState.EXPIRED;
+    return true;
+  }
+
+  private synchronized boolean acceptStateEvent(long sequence) {
+    if (sequence < lastStateEventSequence) {
+      return false;
+    }
+    lastStateEventSequence = sequence;
+    return true;
   }
 
   @Override
   public void process(WatchedEvent event) {
+    process(event, nextEventSequence());
+  }
+
+  void process(WatchedEvent event, long sequence) {
+    process(event, sequence, sessionGeneration.get());

Review Comment:
   My understanding is a race condition is still possible here since we 
retrieve the session generation when processing the watch.
   
   Actually, this watch is not used anymore. In other words, class 
`ConnectionManager` doesn't need anymore to implement `Watcher`. This could be 
removed along with these two `process()` methods, to make things much clearer. 
(adapting some legacy tests will be required).



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to