-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathkv_batch_fetcher.go
355 lines (322 loc) · 11.1 KB
/
kv_batch_fetcher.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// Copyright 2016 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package row
import (
"bytes"
"context"
"github.com/cockroachdb/cockroach/pkg/internal/client"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/errors"
)
// kvBatchSize is the number of keys we request at a time.
// On a single node, 1000 was enough to avoid any performance degradation. On
// multi-node clusters, we want bigger chunks to make up for the higher latency.
// TODO(radu): parameters like this should be configurable
var kvBatchSize int64 = 10000
// TestingSetKVBatchSize changes the kvBatchFetcher batch size, and returns a function that restores it.
// This is to be used only in tests - we have no test coverage for arbitrary kv batch sizes at this time.
func TestingSetKVBatchSize(val int64) func() {
oldVal := kvBatchSize
kvBatchSize = val
return func() { kvBatchSize = oldVal }
}
// sendFunc is the function used to execute a KV batch; normally
// wraps (*client.Txn).Send.
type sendFunc func(
ctx context.Context, ba roachpb.BatchRequest,
) (*roachpb.BatchResponse, error)
// txnKVFetcher handles retrieval of key/values.
type txnKVFetcher struct {
// "Constant" fields, provided by the caller.
sendFn sendFunc
spans roachpb.Spans
// If useBatchLimit is true, batches are limited to kvBatchSize. If
// firstBatchLimit is also set, the first batch is limited to that value.
// Subsequent batches are larger, up to kvBatchSize.
firstBatchLimit int64
useBatchLimit bool
reverse bool
// returnRangeInfo, if set, causes the kvBatchFetcher to populate rangeInfos.
// See also rowFetcher.returnRangeInfo.
returnRangeInfo bool
fetchEnd bool
batchIdx int
// requestSpans contains the spans that were requested in the last request,
// and is one to one with responses. This field is kept separately from spans
// so that the fetcher can keep track of which response was produced for each
// input span.
requestSpans roachpb.Spans
responses []roachpb.ResponseUnion
// As the kvBatchFetcher fetches batches of kvs, it accumulates information on the
// replicas where the batches came from. This info can be retrieved through
// getRangeInfo(), to be used for updating caches.
// rangeInfos are deduped, so they're not ordered in any particular way and
// they don't map to kvBatchFetcher.spans in any particular way.
rangeInfos []roachpb.RangeInfo
origSpan roachpb.Span
remainingBatches [][]byte
}
var _ kvBatchFetcher = &txnKVFetcher{}
func (f *txnKVFetcher) GetRangesInfo() []roachpb.RangeInfo {
if !f.returnRangeInfo {
panic(errors.AssertionFailedf("GetRangesInfo() called on kvBatchFetcher that wasn't configured with returnRangeInfo"))
}
return f.rangeInfos
}
// getBatchSize returns the max size of the next batch.
func (f *txnKVFetcher) getBatchSize() int64 {
return f.getBatchSizeForIdx(f.batchIdx)
}
func (f *txnKVFetcher) getBatchSizeForIdx(batchIdx int) int64 {
if !f.useBatchLimit {
return 0
}
if f.firstBatchLimit == 0 || f.firstBatchLimit >= kvBatchSize {
return kvBatchSize
}
// We grab the first batch according to the limit. If it turns out that we
// need another batch, we grab a bigger batch. If that's still not enough,
// we revert to the default batch size.
switch batchIdx {
case 0:
return f.firstBatchLimit
case 1:
// Make the second batch 10 times larger (but at most the default batch
// size and at least 1/10 of the default batch size). Sample
// progressions of batch sizes:
//
// First batch | Second batch | Subsequent batches
// -----------------------------------------------
// 1 | 1,000 | 10,000
// 100 | 1,000 | 10,000
// 500 | 5,000 | 10,000
// 1000 | 10,000 | 10,000
secondBatch := f.firstBatchLimit * 10
switch {
case secondBatch < kvBatchSize/10:
return kvBatchSize / 10
case secondBatch > kvBatchSize:
return kvBatchSize
default:
return secondBatch
}
default:
return kvBatchSize
}
}
// makeKVBatchFetcher initializes a kvBatchFetcher for the given spans.
//
// If useBatchLimit is true, batches are limited to kvBatchSize. If
// firstBatchLimit is also set, the first batch is limited to that value.
// Subsequent batches are larger, up to kvBatchSize.
//
// Batch limits can only be used if the spans are ordered.
func makeKVBatchFetcher(
txn *client.Txn,
spans roachpb.Spans,
reverse bool,
useBatchLimit bool,
firstBatchLimit int64,
returnRangeInfo bool,
) (txnKVFetcher, error) {
sendFn := func(ctx context.Context, ba roachpb.BatchRequest) (*roachpb.BatchResponse, error) {
res, err := txn.Send(ctx, ba)
if err != nil {
return nil, err.GoError()
}
return res, nil
}
return makeKVBatchFetcherWithSendFunc(
sendFn, spans, reverse, useBatchLimit, firstBatchLimit, returnRangeInfo,
)
}
// makeKVBatchFetcherWithSendFunc is like makeKVBatchFetcher but uses a custom
// send function.
func makeKVBatchFetcherWithSendFunc(
sendFn sendFunc,
spans roachpb.Spans,
reverse bool,
useBatchLimit bool,
firstBatchLimit int64,
returnRangeInfo bool,
) (txnKVFetcher, error) {
if firstBatchLimit < 0 || (!useBatchLimit && firstBatchLimit != 0) {
return txnKVFetcher{}, errors.Errorf("invalid batch limit %d (useBatchLimit: %t)",
firstBatchLimit, useBatchLimit)
}
if useBatchLimit {
// Verify the spans are ordered if a batch limit is used.
for i := 1; i < len(spans); i++ {
if spans[i].Key.Compare(spans[i-1].EndKey) < 0 {
return txnKVFetcher{}, errors.Errorf("unordered spans (%s %s)", spans[i-1], spans[i])
}
}
} else if util.RaceEnabled {
// Otherwise, just verify the spans don't contain consecutive overlapping
// spans.
for i := 1; i < len(spans); i++ {
if spans[i].Key.Compare(spans[i-1].EndKey) >= 0 {
// Current span's start key is greater than or equal to the last span's
// end key - we're good.
continue
} else if spans[i].EndKey.Compare(spans[i-1].Key) < 0 {
// Current span's end key is less than or equal to the last span's start
// key - also good.
continue
}
// Otherwise, the two spans overlap, which isn't allowed - it leaves us at
// risk of incorrect results, since the row fetcher can't distinguish
// between identical rows in two different batches.
return txnKVFetcher{}, errors.Errorf("overlapping neighbor spans (%s %s)", spans[i-1], spans[i])
}
}
// Make a copy of the spans because we update them.
copySpans := make(roachpb.Spans, len(spans))
for i := range spans {
if reverse {
// Reverse scans receive the spans in decreasing order.
copySpans[len(spans)-i-1] = spans[i]
} else {
copySpans[i] = spans[i]
}
}
return txnKVFetcher{
sendFn: sendFn,
spans: copySpans,
reverse: reverse,
useBatchLimit: useBatchLimit,
firstBatchLimit: firstBatchLimit,
returnRangeInfo: returnRangeInfo,
}, nil
}
// fetch retrieves spans from the kv
func (f *txnKVFetcher) fetch(ctx context.Context) error {
var ba roachpb.BatchRequest
ba.Header.MaxSpanRequestKeys = f.getBatchSize()
ba.Header.ReturnRangeInfo = f.returnRangeInfo
ba.Requests = make([]roachpb.RequestUnion, len(f.spans))
if f.reverse {
scans := make([]roachpb.ReverseScanRequest, len(f.spans))
for i := range f.spans {
scans[i].ScanFormat = roachpb.BATCH_RESPONSE
scans[i].SetSpan(f.spans[i])
ba.Requests[i].MustSetInner(&scans[i])
}
} else {
scans := make([]roachpb.ScanRequest, len(f.spans))
for i := range f.spans {
scans[i].ScanFormat = roachpb.BATCH_RESPONSE
scans[i].SetSpan(f.spans[i])
ba.Requests[i].MustSetInner(&scans[i])
}
}
if cap(f.requestSpans) < len(f.spans) {
f.requestSpans = make(roachpb.Spans, len(f.spans))
} else {
f.requestSpans = f.requestSpans[:len(f.spans)]
}
copy(f.requestSpans, f.spans)
if log.ExpensiveLogEnabled(ctx, 2) {
buf := bytes.NewBufferString("Scan ")
for i, span := range f.spans {
if i != 0 {
buf.WriteString(", ")
}
buf.WriteString(span.String())
}
log.VEvent(ctx, 2, buf.String())
}
// Reset spans in preparation for adding resume-spans below.
f.spans = f.spans[:0]
br, err := f.sendFn(ctx, ba)
if err != nil {
return err
}
if br != nil {
f.responses = br.Responses
} else {
f.responses = nil
}
// Set end to true until disproved.
f.fetchEnd = true
var sawResumeSpan bool
for _, resp := range f.responses {
reply := resp.GetInner()
header := reply.Header()
if header.NumKeys > 0 && sawResumeSpan {
return errors.Errorf(
"span with results after resume span; it shouldn't happen given that "+
"we're only scanning non-overlapping spans. New spans: %s",
sqlbase.PrettySpans(nil, f.spans, 0 /* skip */))
}
if resumeSpan := header.ResumeSpan; resumeSpan != nil {
// A span needs to be resumed.
f.fetchEnd = false
f.spans = append(f.spans, *resumeSpan)
// Verify we don't receive results for any remaining spans.
sawResumeSpan = true
}
// Fill up the RangeInfos, in case we got any.
if f.returnRangeInfo {
for _, ri := range header.RangeInfos {
f.rangeInfos = roachpb.InsertRangeInfo(f.rangeInfos, ri)
}
}
}
f.batchIdx++
// TODO(radu): We should fetch the next chunk in the background instead of waiting for the next
// call to fetch(). We can use a pool of workers to issue the KV ops which will also limit the
// total number of fetches that happen in parallel (and thus the amount of resources we use).
return nil
}
// nextBatch returns the next batch of key/value pairs. If there are none
// available, a fetch is initiated. When there are no more keys, ok is false.
// origSpan returns the span that batch was fetched from, and bounds all of the
// keys returned.
func (f *txnKVFetcher) nextBatch(
ctx context.Context,
) (ok bool, kvs []roachpb.KeyValue, batchResponse []byte, origSpan roachpb.Span, err error) {
if len(f.remainingBatches) > 0 {
batch := f.remainingBatches[0]
f.remainingBatches = f.remainingBatches[1:]
return true, nil, batch, f.origSpan, nil
}
if len(f.responses) > 0 {
reply := f.responses[0].GetInner()
f.responses = f.responses[1:]
origSpan := f.requestSpans[0]
f.requestSpans = f.requestSpans[1:]
var batchResp []byte
switch t := reply.(type) {
case *roachpb.ScanResponse:
if len(t.BatchResponses) > 0 {
batchResp = t.BatchResponses[0]
f.remainingBatches = t.BatchResponses[1:]
}
return true, t.Rows, batchResp, origSpan, nil
case *roachpb.ReverseScanResponse:
if len(t.BatchResponses) > 0 {
batchResp = t.BatchResponses[0]
f.remainingBatches = t.BatchResponses[1:]
}
return true, t.Rows, batchResp, origSpan, nil
}
}
if f.fetchEnd {
return false, nil, nil, roachpb.Span{}, nil
}
if err := f.fetch(ctx); err != nil {
return false, nil, nil, roachpb.Span{}, err
}
return f.nextBatch(ctx)
}