Repository navigation
Conversation
snaury
left a comment
There was a problem hiding this comment.
LGTM, but I'm not part of YDB at the moment.
| // close() can run after the count is reserved but before this offer. | ||
| // In that case clear() cannot see the new entry, so clean it here. | ||
| if (stopped && waitingAcquires.remove(acquire)) { | ||
| waitingAcqueireCount.decrementAndGet(); |
There was a problem hiding this comment.
Maybe fix a typo? waitingAcqueireCount -> waitingAcquireAcount.
There was a problem hiding this comment.
Thanks for catching this! Fixed.
| } | ||
|
|
||
| waitingAcquires.offer(acquire); // ConcurrentLinkedQueue always return true | ||
| acquire.whenComplete((ignored, th) -> removeWaitingAcquire(acquire)); |
There was a problem hiding this comment.
To be honest I'm a little worried that this call happens in all cases, both when the call is cancelled and when successful. In the case of success, since the future is removed immediately before completion, this operation is always O(n) on the number of awaiters, with potentially very expensive worst-case walk over the concurrent list, since the future is guaranteed to not be in the list. The total complexity grows quadratically, and completing 1000 awaiters will take on the order of 1 million operations. Historically things like that were enough to cause clients problems in production, even with modest spikes in concurrent requests.
Ideally this should be amortized somehow. At the minimum awaiters which are already dequeued shouldn't incur this cost at all when completing. Would be best if this cost is never quadratic, because a spike in concurrent requests with many such requests cancelled at roughly the same time is also very much possible to cause problems. I'm not sure how to solve the latter with standard java ConcurrentLinkedDeque though.
There was a problem hiding this comment.
Thanks for calling this out. You were right: ConcurrentLinkedDeque.remove could walk the entire waiter queue even after a waiter had been dequeued. I replaced it with WaitingAcquires, which removes a waiter through its node reference, so completing a dequeued waiter no longer searches the queue.
I compared the first fix with the current version against a local YDB container. Each run used 1,000 waiters, a pool of 100 real YDB sessions, and 8 callback workers. These are median times in milliseconds:
| JDK | Workload | First fix | Current |
|---|---|---|---|
| 8 | Hand off 1,000 sessions | 23.4 | 6.6 |
| 8 | Cancel 1,000 waiters | 16.5 | 11.8 |
| 21 | Hand off 1,000 sessions | 27.1 | 6.5 |
| 21 | Cancel 1,000 waiters | 24.4 | 15.7 |
There was a problem hiding this comment.
Looks good to me (I have minor nitpicks, but it's not really about the new code, so I'm not going to voice them).
There was a problem hiding this comment.
Thanks for the review and the helpful feedback!
|
@alex268 @KirillKurdyukov a gentle reminder to look at this PR, please don't forget to communicate with your external contributors! Could you please assign a current YDB employee as a reviewer and maybe leave a comment on how long the review is expected to take, and what needs to be done before that? |
|
Thanks for looking into the leak of cancelled waiters — it's a real issue. But I'm against making The leak can be fixed without a lock, e.g. purge completed futures from the |
Set latency bucket boundaries for duration histograms
- After a failed refresh, the next getToken() starts another login immediately. - If the auth service is down, logins repeat back-to-back at the request rate. A failed background token refresh now retries after 1 s, 2 s, 4 s… up to 1 min. An expired token still triggers a blocking login as before.
|
I don't have a stake here, but I'd argue that in my experience mutex guarded data structures are usually enough for millions of operations per second and only pose a problem when you're close to that throughput. Since this data structure is used for a session pool, and sessions are used for making YDB calls, which always go over the network in one way or the other (with each transaction on the order of milliseconds, rarely less even in single-dc clusters), current code looks complicated and could be simplified to make more decisions under a mutex instead of spinning in complicated compare-and-swap loops. Also correct me if I'm wrong, but carrier pinning is mostly harmful when performing i/o (or generally parking) within synchronized methods. When entering a synchronized method it might block the carrier thread temporarily (like any other thread), but the actual harm depends on the use case. Benchmarks are a good idea. Without them discussions and decisions tend to become theoretical, and implementations are ripe for premature optimizations, which could unintentionally bring worse scalability and performance. |
Release each waiting slot once, including when resize has detached the waiter. Skip removal on normal handoff and cover both concurrent cases.
Set latency bucket boundaries for duration histograms
Release each waiting slot once, including when resize has detached the waiter. Skip removal on normal handoff and cover both concurrent cases.
# Conflicts: # CHANGELOG.md
Propagate cancellation and timeouts in the Table and Query pools. Use an ordered concurrent map for key-based waiter removal and test immediate slot reuse during parallel cancellation and resize.
|
Thanks for the feedback on the release path and the benchmark suggestions. The revised queue uses The regression tests cover slot reuse while a session is still held, cancellation while resize has detached the waiter, and parallel cancellation and replacement without retries. JMH with 16 workers
In a short check at the same queue depth, upstream exhausted the 1,024-attempt retry budget on both JDKs while the only resource was held. The virtual-worker runs used Java 21. Point reads against local YDB
Upstream rejected the replacement attempts with Could you take another look at the queue and the regression tests? |
Changes
WaitingQueueTest.canceledWaitingTestto assert the intentional behavior change: a cancelled waiter now frees its waiting slot immediately, rather than remaining counted until the next session release.Regression tests
Performance note
Removing a cancelled waiter from
ConcurrentLinkedDequeis linear in the queue length.Fixes #794