JackieTien97 commented on code in PR #18921:
URL: https://github.com/apache/iotdb/pull/18921#discussion_r4321153127


##########
iotdb-client/session/src/main/java/org/apache/iotdb/session/Session.java:
##########
@@ -599,11 +605,7 @@ private static List<String> shuffleNodeUrls(List<String> 
endPoints) {
   }
 
   private List<TEndPoint> getNodeUrls() {
-    if (defaultEndPoint != null) {
-      return Collections.singletonList(defaultEndPoint);
-    } else {
-      return SessionUtils.parseSeedNodeUrls(nodeUrls);
-    }
+    return SessionUtils.parseSeedNodeUrls(initialNodeUrls);

Review Comment:
   Confirmed with an A/B run: the same test class against base `bacbd5a6dc` and 
head `c600593`, reusing the real-Thrift `TestNode` harness from 
`SessionSeedNodeFallbackTest`.
   
   Scenario: `nodeUrls = [A]`; discovery returns `[A, B]`; A stops and the 
server now advertises only B; the default connection's `reconnect()` moves to 
B; then `close()`, `open()` and 10 `executeQueryStatement` calls.
   
   | | base | head |
   |---|---|---|
   | new supplier's list after reopen | `[B]` | `[A]` (A is unreachable, so it 
never refreshes) |
   | connection attempts to A during the 10 queries | 0 | 10 |
   
   On head, `getQueryEndPoint()` returns A for every query, so each query first 
tries to open a connection to A before falling back to the default connection. 
Against a host that is really down, each of those attempts is a full connect 
timeout. That is the #18633 symptom, now for every query instead of 1/N. 
Host/port sessions that failed over or were redirected behave the same way. 
`SessionPool` is not affected because pooled sessions share the pool's supplier.
   
   Suggested fix: bootstrap discovery from `defaultEndPoint` (when set) 
followed by the initial URLs, and keep only the initial URLs as permanent 
seeds. `testSessionRetainsInitialUrlsAfterDefaultEndpointChanges` currently 
asserts the regressing behaviour, so it needs to change. A 
close/reopen-after-failover test would lock the fix in.



##########
iotdb-client/session/src/main/java/org/apache/iotdb/session/NodesSupplier.java:
##########
@@ -172,24 +186,54 @@ public List<TEndPoint> get() {
   }
 
   @Override
-  public void run() {
+  public List<TEndPoint> getSeedNodes() {
+    return seedNodes;
+  }
+
+  @Override
+  public void requestRefresh() {
+    if (!closed && refreshRequested.compareAndSet(false, true)) {
+      try {
+        executorService.execute(
+            () -> {
+              try {
+                run();
+              } finally {
+                refreshRequested.set(false);
+              }
+            });
+      } catch (RejectedExecutionException e) {
+        // The session or pool may have shut down its executor concurrently.
+        refreshRequested.set(false);
+      }
+    }
+  }
+
+  @Override
+  public synchronized void run() {

Review Comment:
   Confirmed, with one nuance about how much of this is new. Measured with 
`close()` called 300 ms into a refresh (A/B, same harness as above):
   
   | candidates | base `close()` | head `close()` |
   |---|---|---|
   | 3 hosts that accept TCP but never answer, `connectionTimeoutInMs = 1500` | 
1251 ms | 4274 ms |
   | 3 black-holed hosts (SYN dropped), `connectionTimeoutInMs = 2000` | 0 ms | 
5726 ms |
   
   - **Base** already waits for an in-flight attempt whose TCP connection has 
been accepted. `ThriftConnection.close()` sends `closeSession` through the 
synchronized client, so it waits for the pending `openSession`. While the TCP 
connect itself is still pending, `TSocket.isOpen()` is false, `close()` is a 
no-op, and base returns immediately. The refresh then carries on in the 
background: about 6 s in both versions.
   - **Head** waits for every remaining candidate (discovered + seeds) plus the 
final `SHOW AVAILABLE URLS`. `run()` holds the monitor for the whole loop, and 
the loop never rechecks `closed`.
   
   With the default `connectionTimeoutInMs = 0`, a peer that accepts but never 
answers blocks `close()` forever on both base and head; that part is 
pre-existing. Head extends the wait to every candidate. With timeout 0, each 
black-holed host costs the OS SYN timeout: about 21 s on Windows, about 127 s 
on Linux. `Session.close()`, re-`open()` and `SessionPool.close()` all go 
through `supplier.close()`. `Session.close()` and `SessionPool.close()` are 
`synchronized`, so their callers are blocked as well.
   
   Suggestion:
   - Don't hold the monitor that `close()` needs across network I/O. Serialize 
refreshes with a separate lock or `tryLock`.
   - Check `closed` before each candidate and again after a successful connect.
   - Let `close()` close the in-flight transport directly so a blocked attempt 
is aborted.
   
   Please keep the race this `synchronized` fixes as a side effect: on base, 
`run()` can still create and leak a connection after `close()`.



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

Reply via email to