-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
dist_sender_rangefeed.go
280 lines (259 loc) · 8.66 KB
/
dist_sender_rangefeed.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright 2018 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package kv
import (
"context"
"fmt"
"io"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/util/ctxgroup"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/retry"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
)
type singleRangeInfo struct {
desc *roachpb.RangeDescriptor
rs roachpb.RSpan
ts hlc.Timestamp
token *EvictionToken
}
// RangeFeed divides a RangeFeed request on range boundaries and establishes a
// RangeFeed to each of the individual ranges. It streams back results on the
// provided channel.
//
// Note that the timestamps in RangeFeedCheckpoint events that are streamed back
// may be lower than the timestamp given here.
func (ds *DistSender) RangeFeed(
ctx context.Context, span roachpb.Span, ts hlc.Timestamp, eventCh chan<- *roachpb.RangeFeedEvent,
) error {
ctx = ds.AnnotateCtx(ctx)
ctx, sp := tracing.EnsureChildSpan(ctx, ds.AmbientContext.Tracer, "dist sender")
defer sp.Finish()
startRKey, err := keys.Addr(span.Key)
if err != nil {
return err
}
endRKey, err := keys.Addr(span.EndKey)
if err != nil {
return err
}
rs := roachpb.RSpan{Key: startRKey, EndKey: endRKey}
g := ctxgroup.WithContext(ctx)
// Goroutine that processes subdivided ranges and creates a rangefeed for
// each.
rangeCh := make(chan singleRangeInfo, 16)
g.GoCtx(func(ctx context.Context) error {
for {
select {
case sri := <-rangeCh:
// Spawn a child goroutine to process this feed.
g.GoCtx(func(ctx context.Context) error {
return ds.partialRangeFeed(ctx, &sri, rangeCh, eventCh)
})
case <-ctx.Done():
return ctx.Err()
}
}
})
// Kick off the initial set of ranges.
g.GoCtx(func(ctx context.Context) error {
return ds.divideAndSendRangeFeedToRanges(ctx, rs, ts, rangeCh)
})
return g.Wait()
}
func (ds *DistSender) divideAndSendRangeFeedToRanges(
ctx context.Context, rs roachpb.RSpan, ts hlc.Timestamp, rangeCh chan<- singleRangeInfo,
) error {
// As RangeIterator iterates, it can return overlapping descriptors (and
// during splits, this happens frequently), but divideAndSendRangeFeedToRanges
// intends to split up the input into non-overlapping spans aligned to range
// boundaries. So, as we go, keep track of the remaining uncovered part of
// `rs` in `nextRS`.
nextRS := rs
ri := NewRangeIterator(ds)
for ri.Seek(ctx, nextRS.Key, Ascending); ri.Valid(); ri.Next(ctx) {
desc := ri.Desc()
partialRS, err := nextRS.Intersect(desc)
if err != nil {
return err
}
nextRS.Key = partialRS.EndKey
select {
case rangeCh <- singleRangeInfo{
desc: desc,
rs: partialRS,
ts: ts,
token: ri.Token(),
}:
case <-ctx.Done():
return ctx.Err()
}
if !ri.NeedAnother(nextRS) {
break
}
}
return ri.Error().GoError()
}
// partialRangeFeed establishes a RangeFeed to the range specified by desc. It
// manages lifecycle events of the range in order to maintain the RangeFeed
// connection; this may involve instructing higher-level functions to retry
// this rangefeed, or subdividing the range further in the event of a split.
func (ds *DistSender) partialRangeFeed(
ctx context.Context,
rangeInfo *singleRangeInfo,
rangeCh chan<- singleRangeInfo,
eventCh chan<- *roachpb.RangeFeedEvent,
) error {
// Bound the partial rangefeed to the partial span.
span := rangeInfo.rs.AsRawSpanWithNoLocals()
ts := rangeInfo.ts
// Start a retry loop for sending the batch to the range.
for r := retry.StartWithCtx(ctx, ds.rpcRetryOptions); r.Next(); {
// If we've cleared the descriptor on a send failure, re-lookup.
if rangeInfo.desc == nil {
var err error
rangeInfo.desc, rangeInfo.token, err = ds.getDescriptor(ctx, rangeInfo.rs.Key, nil, false)
if err != nil {
log.VErrEventf(ctx, 1, "range descriptor re-lookup failed: %s", err)
continue
}
}
// Establish a RangeFeed for a single Range.
maxTS, pErr := ds.singleRangeFeed(ctx, span, ts, rangeInfo.desc, eventCh)
// Forward the timestamp in case we end up sending it again.
ts.Forward(maxTS)
if pErr != nil {
if log.V(1) {
log.Infof(ctx, "RangeFeed %s disconnected with last checkpoint %s ago: %v",
span, timeutil.Since(ts.GoTime()), pErr)
}
switch t := pErr.GetDetail().(type) {
case *roachpb.StoreNotFoundError, *roachpb.NodeUnavailableError:
// These errors are likely to be unique to the replica that
// reported them, so no action is required before the next
// retry.
case *roachpb.SendError, *roachpb.RangeNotFoundError:
// Evict the decriptor from the cache and reload on next attempt.
if err := rangeInfo.token.Evict(ctx); err != nil {
return err
}
rangeInfo.desc = nil
continue
case *roachpb.RangeKeyMismatchError:
// Evict the decriptor from the cache.
if err := rangeInfo.token.Evict(ctx); err != nil {
return err
}
return ds.divideAndSendRangeFeedToRanges(ctx, rangeInfo.rs, ts, rangeCh)
case *roachpb.RangeFeedRetryError:
switch t.Reason {
case roachpb.RangeFeedRetryError_REASON_REPLICA_REMOVED,
roachpb.RangeFeedRetryError_REASON_RAFT_SNAPSHOT,
roachpb.RangeFeedRetryError_REASON_LOGICAL_OPS_MISSING,
roachpb.RangeFeedRetryError_REASON_SLOW_CONSUMER:
// Try again with same descriptor. These are transient
// errors that should not show up again.
continue
case roachpb.RangeFeedRetryError_REASON_RANGE_SPLIT,
roachpb.RangeFeedRetryError_REASON_RANGE_MERGED:
// Evict the decriptor from the cache.
if err := rangeInfo.token.Evict(ctx); err != nil {
return err
}
return ds.divideAndSendRangeFeedToRanges(ctx, rangeInfo.rs, ts, rangeCh)
default:
log.Fatalf(ctx, "unexpected RangeFeedRetryError reason %v", t.Reason)
}
default:
return t
}
}
}
return nil
}
// singleRangeFeed gathers and rearranges the replicas, and makes a RangeFeed
// RPC call. Results will be send on the provided channel. Returns the timestamp
// of the maximum rangefeed checkpoint seen, which can be used to re-establish
// the rangefeed with a larger starting timestamp, reflecting the fact that all
// values up to the last checkpoint have already been observed. Returns the
// request's timestamp if not checkpoints are seen.
func (ds *DistSender) singleRangeFeed(
ctx context.Context,
span roachpb.Span,
ts hlc.Timestamp,
desc *roachpb.RangeDescriptor,
eventCh chan<- *roachpb.RangeFeedEvent,
) (hlc.Timestamp, *roachpb.Error) {
args := roachpb.RangeFeedRequest{
Span: span,
Header: roachpb.Header{
Timestamp: ts,
RangeID: desc.RangeID,
},
}
var latencyFn LatencyFunc
if ds.rpcContext != nil {
latencyFn = ds.rpcContext.RemoteClocks.Latency
}
replicas := NewReplicaSlice(ds.gossip, desc)
replicas.OptimizeReplicaOrder(ds.getNodeDescriptor(), latencyFn)
transport, err := ds.transportFactory(SendOptions{}, ds.nodeDialer, replicas)
if err != nil {
return args.Timestamp, roachpb.NewError(err)
}
for {
if transport.IsExhausted() {
return args.Timestamp, roachpb.NewError(roachpb.NewSendError(
fmt.Sprintf("sending to all %d replicas failed", len(replicas)),
))
}
args.Replica = transport.NextReplica()
clientCtx, client, err := transport.NextInternalClient(ctx)
if err != nil {
log.VErrEventf(ctx, 2, "RPC error: %s", err)
continue
}
stream, err := client.RangeFeed(clientCtx, &args)
if err != nil {
log.VErrEventf(ctx, 2, "RPC error: %s", err)
continue
}
for {
event, err := stream.Recv()
if err == io.EOF {
return args.Timestamp, nil
}
if err != nil {
return args.Timestamp, roachpb.NewError(err)
}
switch t := event.GetValue().(type) {
case *roachpb.RangeFeedCheckpoint:
if t.Span.Contains(args.Span) {
args.Timestamp.Forward(t.ResolvedTS)
}
case *roachpb.RangeFeedError:
log.VErrEventf(ctx, 2, "RangeFeedError: %s", t.Error.GoError())
return args.Timestamp, &t.Error
}
select {
case eventCh <- event:
case <-ctx.Done():
return args.Timestamp, roachpb.NewError(ctx.Err())
}
}
}
}