segmentwriter/client: circuit breaker never counts writer upload timeouts (os.ErrDeadlineExceeded never matches a gRPC status), so a writer that can't reach object storage keeps receiving pushes
Summary
When a segment-writer can't upload to object storage, the distributors keep sending its shards to it for the whole outage. The segment writer client's circuit breaker never counts these failures. Pushes routed to that writer wait for the failed flush. They are then retried on another writer only if the returned status is retryable and the request deadline still allows it.
The writer reports a failed flush with one of two codes (pkg/segmentwriter/service.go:283-290):
case errors.Is(err, context.DeadlineExceeded):
...
level.Error(i.logger).Log("msg", "flush timeout", "err", err)
return nil, status.FromContextError(err).Err()
default:
level.Error(i.logger).Log("msg", "flush err", "err", err)
return nil, status.Error(codes.Unknown, err.Error())The breaker's IsSuccessful function (pkg/segmentwriter/client/client.go:122-133, from #3505) only treats os.ErrDeadlineExceeded and codes.Unavailable as failures:
func shouldBeHandledByCaller(err error) bool {
if errors.Is(err, os.ErrDeadlineExceeded) {
return false
}
if status.Code(err) == codes.Unavailable {
return false
}
return true
}The interceptor in pkg/util/circuitbreaker/cirquit_breaker.go passes the invoker's gRPC status error to this function, and a gRPC status error never matches os.ErrDeadlineExceeded. So when a writer answers with DeadlineExceeded or Unknown, the breaker records a success, which also resets the consecutive-failure count. Yet the comment above the function says the breaker should stop traffic to instances that don't respond in a timely fashion.
We understand Unknown is excluded on purpose, because such errors can be cluster-wide. The problem is that a failure to reach object storage from one instance only ever reaches the client as codes the breaker ignores.
Separately, isRetryable (client.go:51-61) does not retry codes.Unknown. If an upload failure reaches the client as Unknown, the push fails with Unavailable (client.go:275-278) without trying another writer. We found this by reading the code; our test only produced timeouts.
Impact
Test setup:
- 3 segment-writers with
segment-writer.num-tokens=1,segment-writer.segment-duration=4sand default upload settings. - 2 distributors with
write-path.segment-writer-timeout=9s. - About 100 pushes/s.
- For 4 minutes, we dropped one writer's egress to object storage (TCP 443) with a network policy on that pod.
Results:
- The distributors logged 7,638
failed attempt to push data to segment writerwarnings, 1.5k to 2.2k per minute throughout.- All of them were for the blocked writer:
code = DeadlineExceeded desc = failed to upload block <id>: upload s3 object: .... - There were no circuit breaker errors.
- The blocked writer logged
flush timeoutfor each waiting request.
- All of them were for the blocked writer:
- Mean distributor request duration (
pyroscope_request_duration_seconds, ingest and push routes) went from about 2.1 s to 3.7-4.3 s. About a third of requests took more than 5 s, and about 1% took more than 10 s. - The distributors returned 28 non-2xx responses (0.12%): 18 HTTP 504 and 10 HTTP 499.
- With 3 s segments, the same test gave no non-2xx responses, but about a third of requests still took more than 5 s.
Reproduction
- Run v2 with 3 segment-writers under steady ingest. Observed on 2.3.1 (revision 7aeaa0f); the code above is unchanged on main.
- Block one writer's egress to the object store for a few minutes, for example with this Cilium policy selecting that pod:
egressDeny:
- toEntities: [world]
toPorts:
- ports: [{port: "443", protocol: TCP}]- Watch
failed attempt to push data to segment writerin the distributor logs, and watch the distributor request duration.
Environment: Kubernetes (EKS 1.35), helm chart 2.3.1, image 2.3.1, microservices mode, v2 storage, S3.
Suggested fix
- Classify upload failures that are local to one instance (for example, upload timeouts reported by the writer) so the existing retry and breaker logic can skip that writer, with bounded recovery attempts.
- Revisit the
os.ErrDeadlineExceededcheck if it was meant to catch slow instances. Counting everyDeadlineExceededwould also count the caller's own expired deadline, so the two cases need to be told apart.
A client test would lock this in: use a fake writer that keeps answering upload failures with a server-side DeadlineExceeded, and assert that the breaker opens and pushes move to another instance.
Source: grafana/pyroscope