Skip to content

Fix canceled session pool waiters and add acquire metrics - #797

Open
lebmax wants to merge 4 commits into
ydb-platform:masterfrom
lebmax:fix/794-issue
Open

lebmax wants to merge 4 commits into
ydb-platform:masterfrom
lebmax:fix/794-issue

Conversation

@lebmax

@lebmax lebmax commented Nov 24, 2026 •

Copy link
Copy Markdown

Changes

  • Propagate completion of the caller-facing acquire future to the corresponding internal acquire in both the Table and Query session pools.
  • Remove completed waiters promptly and keep the waiting count consistent during cancellation, requeueing, and pool shutdown.
  • Preserve the reservation for a session creation that is already in progress. If creation succeeds after the caller cancels, offer the session to another waiter or retain it as idle.
  • Update WaitingQueueTest.canceledWaitingTest to assert the intentional behavior change: a cancelled waiter now frees its waiting slot immediately, rather than remaining counted until the next session release.
  • Add regression tests for timeouts, cancellation, queue admission after cancellation at the waiting limit, requeueing, shutdown, and acquire metrics.

Metrics

  • Add ydb.table.session.acquire_time and ydb.query.session.acquire_time histograms, recorded in seconds for completed acquire attempts.
  • Add ydb.table.session.acquire_failures and ydb.query.session.acquire_failures counters. The ydb.table.session.reason and ydb.query.session.reason attributes use the bounded values timeout, cancelled, rejected, and other.
  • Bump the SDK metrics version reported in discovery metadata to 0.4.0.

Tests

  • Cover timeout and cancellation freeing a Table pool waiting slot before a session is released, so a subsequent acquire can proceed.
  • Cover immediate waiter removal, admitting a new acquire after cancellation in a full queue, cancellation after requeueing, and waiter-count cleanup on pool close.
  • Verify acquire duration and failure metrics, including failure reason values, for the Table and Query pools.

Performance note

Removing a cancelled waiter from ConcurrentLinkedDeque is linear in the queue length.

Fixes #794

@lebmax lebmax changed the title Fix/794 issue Fix canceled session pool waiters and add acquire metrics Nov 24, 2026
@lebmax

lebmax commented Nov 25, 2026

Copy link
Copy Markdown
Author

@snaury @shadchin @jamel
Hi, everyone!
I’d appreciate a review and any feedback on this approach.

@snaury snaury left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe fix a typo? waitingAcqueireCount -> waitingAcquireAcount.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this! Fixed.

}

waitingAcquires.offer(acquire); // ConcurrentLinkedQueue always return true
acquire.whenComplete((ignored, th) -> removeWaitingAcquire(acquire));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cancel queued session acquisition when SessionPool.acquire() times out or is cancelled

2 participants