Skip to content

fix: [OCISDEV-1528] skip a not-ready node before reusing a cached grpc client - #766

Merged
2403905 merged 1 commit into
stable-8.0from
fix/8.0/OCISDEV-1528
Oct 14, 2026
Merged

2403905 merged 1 commit into
stable-8.0from
fix/8.0/OCISDEV-1528

Conversation

@2403905

@2403905 2403905 commented Oct 12, 2026 •

Copy link
Copy Markdown

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.go now stores the connection together with the
client. 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, 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.

@kw-security

kw-security commented Oct 12, 2026 •

Copy link
Copy Markdown

✅ Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
✅ Open Source Security 0 0 0 0 0 issues
✅ Licenses 0 0 0 0 0 issues
✅ Code Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@2403905
2403905 force-pushed the fix/8.0/OCISDEV-1528 branch 3 times, most recently from 08bd020 to e36ae74 Compare October 13, 2026 17:34
@2403905
2403905 requested review from a team, jvillafanez and kobergj October 13, 2026 20:13
@2403905
2403905 force-pushed the fix/8.0/OCISDEV-1528 branch from e36ae74 to a12f19e Compare October 14, 2026 06:32
@2403905 2403905 changed the title fix: [OCISDEV-1528] skip a known-dead node before reusing a cached grpc client fix: [OCISDEV-1528] skip a not-ready node before reusing a cached grpc client Oct 14, 2026
@2403905
2403905 requested a review from mklos-kw October 14, 2026 07:31

@kobergj kobergj left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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 since WaitForReady is set nowhere)
  • stuck SYN -> state parks in Connecting (covered, and this is the real win - note the doc comment above does not mention Connecting at 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.

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.

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:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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-dial

and 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.

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.

  1. 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.
  2. 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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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)
}

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.

We did it before.
if len(targets) == 0 {
return *new(T), fmt.Errorf("%s: no nodes found in registry", s.id)
}

Comment on lines +154 to +158
// 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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

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.

With the keepalive.ClientParameters, it should not be a problem

Comment on lines +163 to +164
if !ok {
return s.newClient(target, opts)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Comment thread pkg/rgrpc/todo/pool/selector.go Outdated

newClient := s.clientFactory(conn)
newClient := cachedClient[T]{conn: conn, client: s.clientFactory(conn)}
s.clientMap.Store(target, newClient)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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, nil

The 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 kobergj left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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]>
@2403905
2403905 force-pushed the fix/8.0/OCISDEV-1528 branch from a12f19e to 32adc6b Compare October 14, 2026 10:15
@2403905
2403905 requested a review from kobergj October 14, 2026 10:36
@2403905
2403905 merged commit e71b24a into stable-8.0 Oct 14, 2026
@2403905
2403905 deleted the fix/8.0/OCISDEV-1528 branch October 14, 2026 12:56
2403905 added a commit that referenced this pull request Oct 16, 2026
… 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]>
2403905 added a commit that referenced this pull request Oct 19, 2026
…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]>
2403905 added a commit that referenced this pull request Oct 19, 2026
…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]>
2403905 added a commit that referenced this pull request Oct 19, 2026
…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]>
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.

4 participants