|
| 1 | +// Copyright 2026 Redpanda Data, Inc. |
| 2 | +// |
| 3 | +// Use of this software is governed by the Business Source License |
| 4 | +// included in the file licenses/BSL.md |
| 5 | +// |
| 6 | +// As of the Change Date specified in that file, in accordance with |
| 7 | +// the Business Source License, use of this software will be governed |
| 8 | +// by the Apache License, Version 2.0 |
| 9 | + |
| 10 | +package kafka |
| 11 | + |
| 12 | +import ( |
| 13 | + "context" |
| 14 | + "errors" |
| 15 | + "time" |
| 16 | + |
| 17 | + "github.com/twmb/franz-go/pkg/kadm" |
| 18 | + "github.com/twmb/franz-go/pkg/kerr" |
| 19 | +) |
| 20 | + |
| 21 | +const ( |
| 22 | + // offsetListRetryTimeout bounds how long an offset listing retries. It |
| 23 | + // matches the client RetryTimeout so offset listing does not retry any |
| 24 | + // longer than the client would for an ordinary request. |
| 25 | + offsetListRetryTimeout = 11 * time.Second |
| 26 | + offsetListRetryBackoff = 250 * time.Millisecond |
| 27 | +) |
| 28 | + |
| 29 | +// ListStartOffsetsWithRetries wraps (*kadm.Client).ListStartOffsets, retrying |
| 30 | +// transient errors; see retryListOffsets for the retry semantics. |
| 31 | +func ListStartOffsetsWithRetries( |
| 32 | + ctx context.Context, adm *kadm.Client, topics ...string, |
| 33 | +) (kadm.ListedOffsets, error) { |
| 34 | + return retryListOffsets(ctx, func(ctx context.Context) (kadm.ListedOffsets, error) { |
| 35 | + return adm.ListStartOffsets(ctx, topics...) |
| 36 | + }) |
| 37 | +} |
| 38 | + |
| 39 | +// ListEndOffsetsWithRetries wraps (*kadm.Client).ListEndOffsets, retrying |
| 40 | +// transient errors; see retryListOffsets for the retry semantics. |
| 41 | +func ListEndOffsetsWithRetries( |
| 42 | + ctx context.Context, adm *kadm.Client, topics ...string, |
| 43 | +) (kadm.ListedOffsets, error) { |
| 44 | + return retryListOffsets(ctx, func(ctx context.Context) (kadm.ListedOffsets, error) { |
| 45 | + return adm.ListEndOffsets(ctx, topics...) |
| 46 | + }) |
| 47 | +} |
| 48 | + |
| 49 | +// ListOffsetsAfterMilliWithRetries wraps (*kadm.Client).ListOffsetsAfterMilli, |
| 50 | +// retrying transient errors; see retryListOffsets for the retry semantics. |
| 51 | +func ListOffsetsAfterMilliWithRetries( |
| 52 | + ctx context.Context, adm *kadm.Client, milli int64, topics ...string, |
| 53 | +) (kadm.ListedOffsets, error) { |
| 54 | + return retryListOffsets(ctx, func(ctx context.Context) (kadm.ListedOffsets, error) { |
| 55 | + return adm.ListOffsetsAfterMilli(ctx, milli, topics...) |
| 56 | + }) |
| 57 | +} |
| 58 | + |
| 59 | +// retryListOffsets runs a kadm offset-listing call, retrying for a bounded time |
| 60 | +// while it fails with only retriable errors. A broker can briefly return a |
| 61 | +// retriable error such as LEADER_NOT_AVAILABLE for a partition whose leader it |
| 62 | +// has not yet learned -- for example right after topic creation or while |
| 63 | +// leadership moves between brokers -- which a one-shot listing would otherwise |
| 64 | +// surface as an outright failure. The error may be a top-level shard error or a |
| 65 | +// per-partition error in the returned offsets; both are retried. |
| 66 | +func retryListOffsets( |
| 67 | + ctx context.Context, |
| 68 | + list func(context.Context) (kadm.ListedOffsets, error), |
| 69 | +) (kadm.ListedOffsets, error) { |
| 70 | + deadline := time.Now().Add(offsetListRetryTimeout) |
| 71 | + for { |
| 72 | + l, err := list(ctx) |
| 73 | + if err == nil { |
| 74 | + err = l.Error() |
| 75 | + } |
| 76 | + if err == nil || time.Now().After(deadline) || !offsetListRetriable(err) { |
| 77 | + return l, err |
| 78 | + } |
| 79 | + select { |
| 80 | + case <-ctx.Done(): |
| 81 | + return l, err |
| 82 | + case <-time.After(offsetListRetryBackoff): |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// offsetListRetriable reports whether an offset-listing failure is composed |
| 88 | +// entirely of retriable Kafka errors. |
| 89 | +func offsetListRetriable(err error) bool { |
| 90 | + var se *kadm.ShardErrors |
| 91 | + if errors.As(err, &se) { |
| 92 | + if len(se.Errs) == 0 { |
| 93 | + return false |
| 94 | + } |
| 95 | + for _, e := range se.Errs { |
| 96 | + if !kerr.IsRetriable(e.Err) { |
| 97 | + return false |
| 98 | + } |
| 99 | + } |
| 100 | + return true |
| 101 | + } |
| 102 | + return kerr.IsRetriable(err) |
| 103 | +} |
0 commit comments