gRPC Channel Management in the HStore Client#
The HStore client manages gRPC connectivity through a layered system involving three core components: AbstractGrpcClient (channel pool), HgStoreNodeManager (node registry and eviction), and NotifyingExecutor (error-to-eviction bridge). Together they handle channel creation, pooling, and teardown on node failure.
Channel Pool: Lazy Creation and Round-Robin Selection#
AbstractGrpcClient owns a single static ConcurrentHashMap<String, ManagedChannel[]> shared across all subclass instances . Channels are created lazily and in bulk.
- Pool size:
concurrency = 1 << 5 = 32channels per target address . - Creation:
getChannels(target)uses double-checked locking. On first access, it spawns all 32 channels in parallel via an internalThreadPoolExecutor, waits on aCountDownLatch, then stores the array atomically . - Stub caching:
getBlockingStub(target)andgetAsyncStub(target)each maintain their ownConcurrentHashMap<String, HgPair<ManagedChannel, Stub>[]>. A stub array is valid only while its backing channel array is the same object reference (usesChannels()). If channels have been rotated out (after eviction), stubs are rebuilt . - Selection:
nextStubIndex()uses a globalAtomicLongcounter masked to the pool size, giving approximate round-robin selection across 32 channels. - Blocking stub timeouts are applied per-call via
withDeadlineAfter, usingHgStoreClientConfig.getGrpcTimeoutSeconds(). Async stubs get message-size limits but no per-call deadline .
Channel Eviction on Node Failure#
HgStoreNodeManager.evictNode() is the sole entry point for tearing down a channel :
- Removes the
HgStoreNodefrom thenodeIdMapandaddressMap(identity-checked to avoid evicting a replacement node). - Calls
AbstractGrpcClient.closeChannel(address), which removes the channel array from the static map and callsManagedChannel.shutdown()on each channel. - Removes the node from every graph's node list in
graphNodesMap.
Eviction is triggered by notifying(graphName, notice, expectedNode) when the node status is NOT_ONLINE or NOT_WORK .
Error-to-eviction path (unary/blocking RPCs): NotifyingExecutor.notifyErrConsumer() catches Throwable from any RPC supplier. If gRPC reports Status.UNAVAILABLE, it calls nodeManager.notifying(..., notice, storeNode) with the expected node reference โ triggering eviction. All other errors only publish the notice without evicting .
Gap: Streaming Scan Observers Do Not Trigger Eviction#
The onError callbacks in streaming scanners do not call back into HgStoreNodeManager.
KvPageScanner.ServeObserverImpl.onError()setscompleted = true, callsobserver.onCompleted(), and propagates the error into theHgBufferProxyโ but makes no call tonodeManager.notifying()orAbstractGrpcClient.closeChannel().KvBatchScanner5.OrderAgent.onError()callskeeper.shout(t), which queues a null sentinel and stores the error for later re-throw onpickUp()โ again without any eviction side-effect .
Consequence: A node that fails mid-stream leaves its 32-channel pool intact in the static map. Subsequent requests to the same address will attempt to reuse those channels and will likely fail again until an unary RPC triggers eviction through NotifyingExecutor, or the application restarts.
Key Files#
| File | Role |
|---|---|
AbstractGrpcClient | Channel pool creation, caching, round-robin stub selection, shutdown |
HgStoreNodeManager | Node registry, eviction coordination, notifying() dispatch |
NotifyingExecutor | Wraps unary RPC calls; maps gRPC UNAVAILABLE to channel eviction |
KvPageScanner | Streaming iterator (single-node); onError does not evict |
KvBatchScanner5 | Batch streaming iterator; onError does not evict |