Repository navigation
Recover from broken region connections: purge dead clients, serialise discovery with retirement - #20
Merged
jcornejo86 merged 11 commits intoNov 13, 2026
Conversation
Ten was picked arbitrarily when the retry loop replaced recursion. Memory is flat per attempt once frames are gone, so the bound costs nothing and should be set for time, not memory. Ten sat below the attempts a region reconnect needs, turning recoverable broken pipes into failed writes in production. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Six lines of prose on the bound and five-line docstrings per test restated what names and code already say. Keep one line of non-obvious why each, and drop the step-by-step narration in get() that tracked the code line for line. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
A failed Future holds its exception, whose traceback references the sender frame holding the Future and the request, a cycle only the collector breaks. Measured 20 of 20 requests retained with gc off; dropping the local gives 0. Also drops a 3.6-only numeric literal, since setup.py still claims 2.7. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Checking weakrefs once _mutate unwound would pass against the recursion it is meant to rule out, which holds every attempt at peak and frees them all on the way out. Sampling at each build shows the loop keeps at most one live; the recursive shape peaks at the attempt count. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
result() can return before the worker releases its work item, which still holds the request arguments, so the count could see unfinished cleanup and fail on scheduling alone. Shut the pool down with cyclic gc still disabled. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Clients are cached under the protobuf bytes spelling while Client decodes host and port, so the handler's lookup always missed and a dead client was never evicted or slept on. Production shows 496 broken pipes in an hour and zero purges. Key on one str form everywhere; _purge_client also raised TypeError mixing a str host with a bytes separator. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Working eviction exposed three leaks it had been hiding. A late failure from a retired client could evict the healthy replacement sharing its address, so purge only when the cached entry is that client. close() left the executor's workers running, and the bucket key held the client itself. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Popping the bucket left a window: a caller could look one up, have it retired, then claim the dead semaphore and later release a different one. Claim under the map lock. Submitting to a shut-down pool also raised RuntimeError, which the retry handlers do not catch, so translate it. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Discovery selected a region client and inserted the region into the cache without holding _cache_lock, while _purge_client retired clients under it. Eviction between the membership check and the indexed lookup raised KeyError, and eviction between selection and insertion cached a region against a closed client with no reverse-cache entry, so the recovery handler skipped it and every retry went back to the dead client until the attempt bound failed the request. _attach_region_to_live_client now selects or installs the client and inserts the region in one locked section, treating a client that is shutting down as absent. Connecting stays outside the lock: a second pass adopts a client another thread installed meanwhile and closes the spare, or installs ours if the cached one was retired while we connected. _add_to_region_cache keeps its public shape and delegates to a _locked variant for callers that already hold the lock. _close_old_regions closed the shared client whenever a cached region overlapped a newly discovered one (a split, or a stale entry), which left every other region on that client pointing at a closed client. It is now _forget_old_regions and only detaches the stale regions. Eight offline tests in tests/test_discovery_retirement.py cover reuse, replacement of a retired entry, retirement and competing discovery during connect, the overlap case, a failed connect, a purge issued inside the locked section, and a threaded stress run that asserts no cached region ever points at a closed or evicted client. The stress run fails without the _forget_old_regions change. Version 4.3.0. Refs Flipboard/ops#2542. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
There was a problem hiding this comment.
🟡 Changes recommended
Replacing overlapping regions across servers can leave displaced, regionless clients cached with open sockets and executors, causing idle connections and worker threads to accumulate until the main client closes.
1 open issue
What changed in this PR
This PR improves the HBase client’s recovery from broken region-server connections, carrying forward the fixes from #19 and coordinating region discovery with client retirement to prevent stale cache entries.
Changes:
- Normalize server cache keys and make region attachment atomic with retirement, while preserving clients shared by other regions.
- Increase the retry bound and improve cleanup of failed requests, executors, and recovery semaphores.
- Add offline regression coverage and bump the package version to 4.3.0.
| File | Description |
|---|---|
tests/test_retry_bounds.py |
Tests retry headroom and request/Future memory release. |
tests/test_region_cache_key.py |
Tests consistent cache keys and client purging. |
tests/test_discovery_retirement.py |
Tests discovery/retirement races and shared-client preservation. |
tests/test_client_retirement.py |
Tests retirement, semaphore cleanup, and retired-pool errors. |
setup.py |
Bumps the version to 4.3.0. |
pybase/region/client.py |
Releases synchronous Futures, shuts down executors, and translates retired-pool errors. |
pybase/helpers/__init__.py |
Adds normalized region-server cache keys. |
pybase/exceptions.py |
Uses normalized keys and coordinates semaphore claim/release. |
pybase/client.py |
Raises the retry bound, synchronizes attachment and retirement, and detaches stale regions. |
Review effort: Balanced
_forget_old_regions stopped closing the client of an overlapping stale region, which fixed the shared-client breakage but left a client whose only cached region moved to another server sitting in reverse_client_cache with its sockets and executor open until MainClient.close(). It now returns the clients it touched and _add_to_region_cache_locked retires any that is regionless and not the client just selected, evicting the reverse-cache entry only when it is still that instance. Both cases are covered by tests. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

What is this
Picks up the five fixes from #19 (closed unmerged, see Flipboard/ops#2542) unchanged, and resolves the discovery/retirement race that stopped that PR. Region discovery now selects or installs a region client and inserts the region in one
_cache_locksection, so_purge_clientcan no longer retire a client between those steps and leave a cached region pointing at a closed client. Overlapping stale regions no longer close the client they share with every other region on that server.Production evidence that the headline bug is live: on 2026-11-13 a hbase-prod-1 region server aborted at 04:39Z (EMRFS flush failure); the flavour topic-writer on flavour01-notifications then failed every write routed to that server with
BrokenPipeErrorfor 2.5 h until restarted, with 8,764 errors and 2,180 dropped documents, and zero reconnect attempts recorded in the region server's audit log. Details and timeline in Flipboard/ops#2542.Summary
d1bc2a1..d5f9ba3are Recover from broken region connections: purge dead clients, release futures, raise the retry bound #19 as reviewed: key normalisation viaregion_cache_key, identity check in_purge_client, Future release in_send_request, executor shutdown inClient.close(), atomic bucket claim and bucket release,RuntimeErrorfrom a retired pool mapped toRegionServerException,MAX_REQUEST_ATTEMPTS = 100.7323882pybase/client.py: new_attach_region_to_live_clientdoes the select-or-install plus insert under_cache_lock, treating a client that isshutting_downas absent;NewClient(the socket connects) stays outside the lock and a second pass adopts a client another thread installed meanwhile or installs ours if the cached one was retired while we connected._add_to_region_cachekeeps its shape and delegates to_add_to_region_cache_locked._close_old_regions→_forget_old_regions: a stale overlapping region only detaches itself from its client; it no longer closes the client (the oldTODO: should we really be killing a client unnecessarily?).tests/test_discovery_retirement.py: eight offline tests covering reuse, replacement of a retired entry, retirement and competing discovery during connect, the overlap case, a failed connect, a purge issued inside the locked section (it blocks, then evicts), and a 0.5 s threaded stress run asserting no cached region ever points at a closed or evicted client. The stress run fails without the_forget_old_regionschange.setup.py: 4.2.0 → 4.3.0.Verification
python -m unittest tests.test_retry_bounds tests.test_region_cache_key tests.test_client_retirement tests.test_discovery_retirement: 24 tests OK, three consecutive runs (Python 3.9, intervaltree 3.x, protobuf 3.x).flake8clean on the new file;client.pyunchanged apart from the pre-existing E128/F821/W293.requirements-source.txt, roll the extractor image and the flavour EC2 payload, then confirm a region server restart producesPurging cache, sleeping, retryinglog lines and writes recover without a service restart.Refs Flipboard/ops#2542.
🤖 Generated with Claude Code