JackieTien97 opened a new pull request, #19173:
URL: https://github.com/apache/iotdb/pull/19173

   ## Description
   
   `SessionSeedNodeFallbackTest.testSessionPoolBufferReuseThreshold`, added in 
#19148, fails intermittently on `unit-test (17, windows-latest, others)`:
   
   - https://github.com/apache/iotdb/actions/runs/42192797529/job/127810615290
     `assertDiscoveryBufferThreshold:581->assertTransportBufferThreshold:589 
IllegalArgument The object containing the field cannot be null`
   - https://github.com/apache/iotdb/actions/runs/42191068700/job/127802924439
     `assertDiscoveryBufferThreshold:583 expected:<32> but was:<28>`
   
   Neither failing PR touched the session module. Since #19148 was merged, the 
test has failed in 2 of 14 Windows `others` jobs and in none of 18 Ubuntu 
`others` jobs.
   
   ### Root cause
   
   `SessionPool` runs its `NodesSupplier` on a real single-thread scheduled 
executor, and `scheduleAtFixedRate(nodesSupplier, 0, 60, SECONDS)` starts the 
first discovery refresh immediately. `assertDiscoveryBufferThreshold()` called 
`run()` and then inspected the discovery connection. `run()` returns without 
waiting when another refresh holds `refreshLock`, so the assertions could see 
the background refresh half done:
   
   - If the background thread held `refreshLock` but had not published `client` 
yet, `client` was `null` (line 589).
   - If the background thread was serializing the `openSession` request, the 
write transport still held the temporary frame buffer. With a zero reuse 
threshold, that buffer has 28 bytes at that point of `openSession`, which 
caused the line 583 failure.
   
   Skipping a refresh while another one is in flight is intended 
`NodesSupplier` behavior, so only the test changes. `awaitSnapshot()` also 
calls `run()` directly, but it polls until the expected snapshot appears, so it 
is not affected.
   
   ### Fix
   
   Hold `refreshLock` while refreshing and inspecting the discovery connection. 
Because the lock is reentrant, `run()` still refreshes on the test thread. Any 
in-flight background refresh finishes first and cannot use the connection 
during the buffer assertions. The wait is bounded so that a stuck refresh fails 
the test instead of hanging the build. 
`testTableSessionPoolBufferReuseThreshold` uses the same helper, so it is fixed 
too.
   
   ### Verification
   
   - `mvn test -pl iotdb-client/session -am` passes locally on Corretto 17: 119 
Service-RPC tests and 291 Session tests.
   - To reproduce the race deterministically, I ran the tests against a locally 
patched build that delays only the pool's `PeriodicalUpdateDNList` refresh 
thread by 200 ms:
     - With the delay right after the thread acquires `refreshLock`, the 
original test fails 5/5 with the same `IllegalArgumentException` at lines 
589/581/523.
     - With the delay while the `openSession` frame buffer has 28 bytes, the 
original test fails 5/5 with `expected:<32> but was:<28>` at lines 583/523.
     - With this change, `testSessionPoolBufferReuseThreshold` and 
`testTableSessionPoolBufferReuseThreshold` each pass 10/10 under both delays.
   - Without injected delays, both tests also pass in a local loop (235 and 161 
consecutive runs).
   
   <hr>
   
   This PR has:
   - [x] been self-reviewed.
   - [x] added comments explaining the "why" and the intent of the code 
wherever would not be obvious
     for an unfamiliar reader.
   - [x] added unit tests or modified existing tests to cover new code paths, 
ensuring the threshold
     for code coverage.
   
   <hr>
   
   ##### Key changed/added classes (or packages if there are too many classes) 
in this PR
   
   - `SessionSeedNodeFallbackTest` (test only)
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


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