Repository navigation
fix: [OCISDEV-1528] skip a not-ready node before reusing a cached grpc client - #766
Conversation
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
08bd020 to
e36ae74
Compare
e36ae74 to
a12f19e
Compare
kobergj
left a comment
There was a problem hiding this comment.
Reviewed the selector change against connection.go and pkg/registry. The diagnosis is right and the implementation is clean - the old code really did hand back a cached client with zero state inspection, and shuffledTargets + first-healthy-wins preserves the uniform-random spread that registry.GetNodeAddress (selector.Random) gave before, so the changelog's "load still spreads" claim holds.
Six comments below: one on a failure mode I do not think this covers yet (client keepalive), and five smaller correctness/robustness points. Dependency changes deliberately not covered here.
| case connectivity.TransientFailure, connectivity.Connecting, connectivity.Shutdown: | ||
| return false | ||
| default: | ||
| return true |
There was a problem hiding this comment.
Ready falls into default, so a connection whose peer went away silently is still handed out as healthy.
NewConn sets no client-side keepalive: grpc.WithKeepaliveParams appears nowhere in pkg/rgrpc/todo/pool/, and the only keepalive config in the repo is server-side (pkg/rgrpc/rgrpc.go:255). grpc-go's client keepalive is off by default, so nothing ever probes an established-but-unused connection.
Consequence: when a peer is blackholed (node goes NotReady, network partition, no RST), the conn stays Ready indefinitely, healthy() returns true, and the caller stalls exactly as it did before this patch.
What the patch does and does not cover:
- connection refused ->
TransientFailure(covered, though fail-fast already errors quickly here sinceWaitForReadyis set nowhere) - stuck SYN -> state parks in
Connecting(covered, and this is the real win - note the doc comment above does not mentionConnectingat all, even though it is the most consequential of the three states listed below) - silent blackhole on an established conn -> not covered
Adding something like
grpc.WithKeepaliveParams(keepalive.ClientParameters{
Time: 10 * time.Second,
Timeout: 3 * time.Second,
PermitWithoutStream: true,
})to NewConn is what makes the connectivity state machine reflect reality. Without it, the new state check mostly fires in the cases grpc already fails fast on. I would like that in this PR; failing that, please spell out in the changelog which failure modes are actually fixed, because "when a node died, every caller kept reusing the same cached client" reads as covering the blackhole case too.
There was a problem hiding this comment.
The WithKeepaliveParams is configurable PR OCISDEV-1433.
GRPC_CLIENT_KEEPALIVE_TIME / GRPC_CLIENT_KEEPALIVE_TIMEUOT
| // reachability without using it). | ||
| func (c cachedClient[T]) healthy() bool { | ||
| switch c.conn.GetState() { | ||
| case connectivity.TransientFailure, connectivity.Connecting, connectivity.Shutdown: |
There was a problem hiding this comment.
Shutdown is terminal - that conn never recovers - so skipping it is not sufficient. The cache entry should be evicted and the target re-dialed, otherwise if every node's conn ends up Shutdown the fallback on line 176 returns a permanently dead client with no path back.
Suggest splitting it out:
case connectivity.Shutdown:
// terminal: the conn will never recover, so drop it and re-dialand having the caller s.clientMap.Delete(target) then fall through to newClient.
Priority caveat: nothing in reva calls conn.Close() today, so Shutdown should be unreachable in production. That makes this cheap to defer - but it also means the state is worth an explicit comment rather than being quietly lumped in with the two recoverable ones.
There was a problem hiding this comment.
- The Shutdown state never happened without conn.Close(), so we just set shutdown here formally.
If something is wrong, the connection is always in a loop TransientFailure -> Connecting -> TransientFailure, and then if the node has already repaired itself, the connection goes back to Ready. - We cannot just s.clientMap.Delete(target) because the registry has a 30-second delay before it removes the dead node.
| } | ||
|
|
||
| conn, err := NewConn(target, allOpts...) | ||
| return fallback.client, nil |
There was a problem hiding this comment.
Nil deref. fallback is still nil if the loop body never reaches the assignment on line 172 - i.e. when targets is empty - and this panics.
It cannot fire today because the only caller guards it (len(targets) == 0, line 144). But nextHealthy otherwise reads as a self-contained helper, and the invariant that keeps it safe lives in a different function. That is one refactor away from a panic in the gRPC hot path, in the code that is supposed to make failures graceful.
Cheap to make local:
if fallback == nil {
return *new(T), fmt.Errorf("%s: no nodes found in registry", s.id)
}There was a problem hiding this comment.
We did it before.
if len(targets) == 0 {
return *new(T), fmt.Errorf("%s: no nodes found in registry", s.id)
}
| // nextHealthy walks targets in the given order. It returns the client for | ||
| // the first target that is ready. A target with no cache entry is also a | ||
| // candidate, since Next has no way to know its state without dialing it. If | ||
| // no target is ready, nextHealthy reuses the first one found, so the caller | ||
| // gets a real error from a real attempt instead of a fabricated one. |
There was a problem hiding this comment.
If no target is ready, nextHealthy reuses the first one found, so the caller gets a real error from a real attempt instead of a fabricated one.
Handing back a client known to be in TransientFailure or Connecting reproduces the exact stall this PR exists to fix. For the Connecting-on-a-blackholed-node case in particular, the "real attempt" is not a real error - it is a block until the caller's context deadline, which is the original symptom.
Returning Unavailable straight away when no known node is usable would be faster and more honest.
If the fallback is deliberate - e.g. to avoid hard-failing while all nodes are mid-restart and merely Connecting - then the two states want different handling: fall back on Connecting, fail fast on TransientFailure. Either way the stall risk should be stated in this comment, since the current wording claims the opposite.
There was a problem hiding this comment.
With the keepalive.ClientParameters, it should not be a problem
| if !ok { | ||
| return s.newClient(target, opts) |
There was a problem hiding this comment.
If newClient fails, nextHealthy returns that error and never examines the remaining targets - including cached healthy ones further down the shuffled list. One uncached target can fail a call that had a working node available, and because the list is shuffled it will do so non-deterministically.
Low priority: grpc.NewClient is lazy, so NewConn realistically only fails on TLS/config errors, which would fail for every target anyway. But remembering the error and continue-ing reads better than return and costs nothing.
|
|
||
| newClient := s.clientFactory(conn) | ||
| newClient := cachedClient[T]{conn: conn, client: s.clientFactory(conn)} | ||
| s.clientMap.Store(target, newClient) |
There was a problem hiding this comment.
Store rather than LoadOrStore: two goroutines racing on the same uncached target each build a *grpc.ClientConn, and the loser is overwritten in the map and never Close()d - leaking an fd, a resolver goroutine and a transport every time.
This is pre-existing, but this PR is the right place to fix it. Before, the discarded value was just a client struct and the conn was unreachable; now the selector explicitly holds the conn and reasons about its state, so conn lifecycle is this type's responsibility.
newClient := cachedClient[T]{conn: conn, client: s.clientFactory(conn)}
if existing, loaded := s.clientMap.LoadOrStore(target, newClient); loaded {
_ = conn.Close()
return existing.(cachedClient[T]).client, nil
}
return newClient.client, nilThe shuffle helps a little by spreading concurrent lookups across targets, but a request burst against a fresh selector can still have N goroutines land on the same unseen target.
kobergj
left a comment
There was a problem hiding this comment.
The keepalive makes sense I think. Rest is informative/robustness.
…c client The grpc client selector cached one client per resolved node address. It returned that client without a health check. When a node died, every caller kept reusing the same cached client against it. pkg/rgrpc/todo/pool/selector.go now caches the connection together with the client. The selector shuffles the known node addresses on each lookup. It skips any cached connection in the TransientFailure, Connecting, or Shutdown state. It returns the first node that is ready, or dials a node it has not tried yet. If no known node is ready, the selector reuses the first node it found. The caller then gets a fast, real error instead of a long stall. Co-Authored-By: Claude Sonnet 5 <[email protected]>
a12f19e to
32adc6b
Compare
… a cached grpc client (#770) Backport of #766. The grpc client selector cached one client per resolved node address. It returned that client without a health check. When a node died, every caller kept reusing the same cached client against it. pkg/rgrpc/todo/pool/selector.go now caches the connection together with the client. The selector shuffles the known node addresses on each lookup. It skips any cached connection in the TransientFailure, Connecting, or Shutdown state. It returns the first node that is ready, or dials a node it has not tried yet. If no known node is ready, the selector reuses the first node it found. Co-authored-by: Claude Sonnet 5.5 <[email protected]>
…a cached grpc client (#767) Backport of #766. The grpc client selector cached one client per resolved node address. It returned that client without a health check. When a node died, every caller kept reusing the same cached client against it. pkg/rgrpc/todo/pool/selector.go now caches the connection together with the client. The selector shuffles the known node addresses on each lookup. It skips any cached connection in the TransientFailure, Connecting, or Shutdown state. It returns the first node that is ready, or dials a node it has not tried yet. If no known node is ready, the selector reuses the first node it found. Co-authored-by: Claude Sonnet 5.5 <[email protected]>
…a cached grpc client (#768) Backport of #766. The grpc client selector cached one client per resolved node address. It returned that client without a health check. When a node died, every caller kept reusing the same cached client against it. pkg/rgrpc/todo/pool/selector.go now caches the connection together with the client. The selector shuffles the known node addresses on each lookup. It skips any cached connection in the TransientFailure, Connecting, or Shutdown state. It returns the first node that is ready, or dials a node it has not tried yet. If no known node is ready, the selector reuses the first node it found. Co-authored-by: Claude Sonnet 5.5 <[email protected]>
…a cached grpc client (#769) Backport of #766. The grpc client selector cached one client per resolved node address. It returned that client without a health check. When a node died, every caller kept reusing the same cached client against it. pkg/rgrpc/todo/pool/selector.go now caches the connection together with the client. The selector shuffles the known node addresses on each lookup. It skips any cached connection in the TransientFailure, Connecting, or Shutdown state. It returns the first node that is ready, or dials a node it has not tried yet. If no known node is ready, the selector reuses the first node it found. Co-authored-by: Claude Sonnet 5.5 <[email protected]>
Skip a not-ready node before reusing a cached grpc client
The grpc client selector cached one client per resolved node address. It returned
that client without a health check. When a node died, every caller kept reusing
the same cached client. Every call against that node waited for grpc's default
connect timeout before it failed.
pkg/rgrpc/todo/pool/selector.gonow stores the connection together with theclient. The selector shuffles the known node addresses on each lookup. This way,
load still spreads across nodes. The selector then walks the addresses and skips
any cached connection in the
TransientFailure,Connecting, orShutdownstate. It returns the first node that is ready, or dials a node it has not tried
yet. If no known node is ready, the selector reuses the first node it found. The
caller then gets a fast, real error instead of a long stall.