forked from gazette/core
-
Notifications
You must be signed in to change notification settings - Fork 2
/
retry_reader.go
188 lines (162 loc) · 5.8 KB
/
retry_reader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package client
import (
"bufio"
"context"
"errors"
"io"
"time"
pb "github.com/LiveRamp/gazette/v2/pkg/protocol"
log "github.com/sirupsen/logrus"
)
// RetryReader wraps Reader with error handling and retry behavior, as well as
// support for cancellation of an ongoing Read or Seek operation. RetryReader
// is not thread-safe, with one exception: Cancel may be called from one
// goroutine to abort an ongoing Read or Seek call in another.
type RetryReader struct {
// Reader is the current underlying Reader of the RetryReader. This instance
// may change many times over the lifetime of a RetryReader, as Read RPCs
// finish or are cancelled and then restarted.
Reader *Reader
// Cancel Read operations of the current Reader. Notably this will cause an
// ongoing blocked Read (as well as any future Reads) to return a "Cancelled"
// error. Restart may be called to re-initialize the RetryReader.
Cancel context.CancelFunc
ctx context.Context
client pb.RoutedJournalClient
}
// NewRetryReader returns a RetryReader initialized with the BrokerClient and ReadRequest.
func NewRetryReader(ctx context.Context, client pb.RoutedJournalClient, req pb.ReadRequest) *RetryReader {
var rr = &RetryReader{
ctx: ctx,
client: client,
}
rr.Restart(req)
return rr
}
// Journal being read by this RetryReader.
func (rr *RetryReader) Journal() pb.Journal {
return rr.Reader.Request.Journal
}
// Offset of the next Journal byte to be returned by Read.
func (rr *RetryReader) Offset() int64 {
return rr.Reader.Request.Offset
}
// Read returns the next bytes of journal content. It will return a non-nil
// error in the following cases:
// * Cancel is called, or the RetryReader context is cancelled.
// * The broker returns OFFSET_NOT_YET_AVAILABLE (ErrOffsetNotYetAvailable)
// for a non-blocking ReadRequest.
// * An offset jump occurred (ErrOffsetJump), in which case the client
// should inspect the new Offset may continue reading if desired.
// All other errors are retried.
func (rr *RetryReader) Read(p []byte) (n int, err error) {
for i := 0; true; i++ {
if n, err = rr.Reader.Read(p); err == nil {
return // Success.
} else if err == ErrOffsetJump {
return // Note |rr.Reader| is not invalidated by this error.
}
// Our Read failed. Since we're a retrying reader, we consume and mask
// errors (possibly logging a warning), manage our own back-off timer,
// and restart the stream when ready for another attempt.
// Restart the Reader re-using the same context (note we could be racing
// this restart with a concurrent call to |rr.Cancel|).
rr.Reader = NewReader(rr.Reader.ctx, rr.Reader.client, rr.Reader.Request)
switch err {
case context.DeadlineExceeded, context.Canceled:
return // Surface to caller.
case ErrOffsetNotYetAvailable:
if rr.Reader.Request.Block {
// |Block| was set after a non-blocking reader was started. Restart in blocking mode.
} else {
return // Surface to caller.
}
case io.EOF, ErrNotJournalBroker:
// Suppress logging for expected errors.
default:
log.WithFields(log.Fields{"journal": rr.Journal(), "offset": rr.Offset(), "err": err, "i": i}).
Warn("read failure (will retry)")
}
if n != 0 {
err = nil // Squelch from caller.
return
}
// Wait for a back-off timer, or context cancellation.
select {
case <-rr.Reader.ctx.Done():
return 0, rr.Reader.ctx.Err()
case <-time.After(backoff(i)):
}
}
panic("not reached")
}
// Seek sets the offset for the next Read. It returns an error if (and only if)
// |whence| is io.SeekEnd, which is not supported.
func (rr *RetryReader) Seek(offset int64, whence int) (int64, error) {
switch whence {
case io.SeekStart:
// |offset| is already absolute.
case io.SeekCurrent:
offset = rr.Reader.Request.Offset + offset
case io.SeekEnd:
return rr.Reader.Request.Offset, errors.New("io.SeekEnd whence is not supported")
default:
panic("invalid whence")
}
if _, err := rr.Reader.Seek(offset, io.SeekStart); err != nil {
if err != ErrSeekRequiresNewReader {
log.WithFields(log.Fields{"journal": rr.Journal(), "offset": offset, "err": err}).
Warn("failed to seek open Reader (will retry)")
}
var req = rr.Reader.Request
req.Offset = offset
rr.Cancel()
rr.Restart(req)
}
return rr.Reader.Request.Offset, nil
}
// AdjustedOffset returns the current journal offset, adjusted for content read
// by |br| (which wraps this RetryReader) but not yet consumed from |br|'s buffer.
func (rr *RetryReader) AdjustedOffset(br *bufio.Reader) int64 { return rr.Reader.AdjustedOffset(br) }
// AdjustedSeek sets the offset for the next Read, accounting for buffered data and updating
// the buffer as needed.
func (rr *RetryReader) AdjustedSeek(offset int64, whence int, br *bufio.Reader) (int64, error) {
switch whence {
case io.SeekStart:
// |offset| is already absolute.
case io.SeekCurrent:
offset = rr.AdjustedOffset(br) + offset
case io.SeekEnd:
return rr.AdjustedOffset(br), errors.New("io.SeekEnd whence is not supported")
default:
panic("invalid whence")
}
var delta = offset - rr.AdjustedOffset(br)
// Fast path: can we fulfill the seek by discarding a portion of buffered data?
if delta >= 0 && delta <= int64(br.Buffered()) {
_, _ = br.Discard(int(delta))
return offset, nil
}
// We must Seek the underlying reader, discarding and resetting the current buffer.
var n, err = rr.Seek(offset, io.SeekStart)
br.Reset(rr)
return n, err
}
// Restart the RetryReader with a new ReadRequest.
func (rr *RetryReader) Restart(req pb.ReadRequest) {
var ctx, cancel = context.WithCancel(rr.ctx)
rr.Reader = NewReader(ctx, rr.client, req)
rr.Cancel = cancel
}
func backoff(attempt int) time.Duration {
switch attempt {
case 0, 1:
return 0
case 2:
return time.Millisecond * 5
case 3, 4, 5:
return time.Second * time.Duration(attempt-1)
default:
return 5 * time.Second
}
}