Skip to content

Recover from broken region connections: purge dead clients, serialise discovery with retirement - #20

Merged
jcornejo86 merged 11 commits into
masterfrom
jc/20261113/pybase-discovery-retirement-race
Nov 13, 2026
Merged

jcornejo86 merged 11 commits into
masterfrom
jc/20261113/pybase-discovery-retirement-race

Conversation

@jcornejo86

Copy link
Copy Markdown

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_lock section, so _purge_client can 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 BrokenPipeError for 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

  • Commits d1bc2a1..d5f9ba3 are Recover from broken region connections: purge dead clients, release futures, raise the retry bound #19 as reviewed: key normalisation via region_cache_key, identity check in _purge_client, Future release in _send_request, executor shutdown in Client.close(), atomic bucket claim and bucket release, RuntimeError from a retired pool mapped to RegionServerException, MAX_REQUEST_ATTEMPTS = 100.
  • 7323882 pybase/client.py: new _attach_region_to_live_client does the select-or-install plus insert under _cache_lock, treating a client that is shutting_down as 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_cache keeps 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 old TODO: 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_regions change.
  • 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). flake8 clean on the new file; client.py unchanged apart from the pre-existing E128/F821/W293.
  • The integration tests need a live HBase and were not run.
  • Not yet exercised in production. Rollout plan is in Flipboard/ops#2542: bump the pin in flavour requirements-source.txt, roll the extractor image and the flavour EC2 payload, then confirm a region server restart produces Purging cache, sleeping, retrying log lines and writes recover without a service restart.

Refs Flipboard/ops#2542.

🤖 Generated with Claude Code

rockhunters08 and others added 10 commits October 14, 2026 14:31
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>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 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

Comment thread pybase/client.py Resolved
_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>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are blocking approval of the connection-recovery changes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@jcornejo86
jcornejo86 merged commit aad605b into master Nov 13, 2026
1 check passed
@jcornejo86
jcornejo86 deleted the jc/20261113/pybase-discovery-retirement-race branch November 13, 2026 07:35
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.

3 participants