-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathkv_batch_fetcher.go
442 lines (400 loc) · 13.9 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// 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/kv"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/concurrency/lock"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkeys"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/mon"
"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
// lockStrength represents the locking mode to use when fetching KVs.
lockStrength descpb.ScanLockingStrength
// lockWaitPolicy represents the policy to be used for handling conflicting
// locks held by other active transactions.
lockWaitPolicy descpb.ScanLockingWaitPolicy
needValueCols 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
origSpan roachpb.Span
remainingBatches [][]byte
mon *mon.BytesMonitor
}
var _ kvBatchFetcher = &txnKVFetcher{}
// 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
}
}
// getKeyLockingStrength returns the configured per-key locking strength to use
// for key-value scans.
func (f *txnKVFetcher) getKeyLockingStrength() lock.Strength {
switch f.lockStrength {
case descpb.ScanLockingStrength_FOR_NONE:
return lock.None
case descpb.ScanLockingStrength_FOR_KEY_SHARE:
// Promote to FOR_SHARE.
fallthrough
case descpb.ScanLockingStrength_FOR_SHARE:
// We currently perform no per-key locking when FOR_SHARE is used
// because Shared locks have not yet been implemented.
return lock.None
case descpb.ScanLockingStrength_FOR_NO_KEY_UPDATE:
// Promote to FOR_UPDATE.
fallthrough
case descpb.ScanLockingStrength_FOR_UPDATE:
// We currently perform exclusive per-key locking when FOR_UPDATE is
// used because Upgrade locks have not yet been implemented.
return lock.Exclusive
default:
panic(errors.AssertionFailedf("unknown locking strength %s", f.lockStrength))
}
}
// getWaitPolicy returns the configured lock wait policy to use for key-value
// scans.
func (f *txnKVFetcher) getWaitPolicy() lock.WaitPolicy {
switch f.lockWaitPolicy {
case descpb.ScanLockingWaitPolicy_BLOCK:
return lock.WaitPolicy_Block
case descpb.ScanLockingWaitPolicy_SKIP:
// Should not get here. Query should be rejected during planning.
panic(errors.AssertionFailedf("unsupported wait policy %s", f.lockWaitPolicy))
case descpb.ScanLockingWaitPolicy_ERROR:
return lock.WaitPolicy_Error
default:
panic(errors.AssertionFailedf("unknown wait policy %s", f.lockWaitPolicy))
}
}
// 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 *kv.Txn,
spans roachpb.Spans,
reverse bool,
useBatchLimit bool,
firstBatchLimit int64,
lockStrength descpb.ScanLockingStrength,
lockWaitPolicy descpb.ScanLockingWaitPolicy,
mon *mon.BytesMonitor,
needValueCols 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, lockStrength, lockWaitPolicy, mon, needValueCols,
)
}
// makeKVBatchFetcherWithSendFunc is like makeKVBatchFetcher but uses a custom
// send function.
func makeKVBatchFetcherWithSendFunc(
sendFn sendFunc,
spans roachpb.Spans,
reverse bool,
useBatchLimit bool,
firstBatchLimit int64,
lockStrength descpb.ScanLockingStrength,
lockWaitPolicy descpb.ScanLockingWaitPolicy,
mon *mon.BytesMonitor,
needValueCols 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,
lockStrength: lockStrength,
lockWaitPolicy: lockWaitPolicy,
mon: mon,
needValueCols: needValueCols,
}, nil
}
// maxScanResponseBytes is the maximum number of bytes a scan request can
// return.
const maxScanResponseBytes = 10 * (1 << 20)
// fetch retrieves spans from the kv layer.
func (f *txnKVFetcher) fetch(ctx context.Context) error {
var ba roachpb.BatchRequest
ba.Header.WaitPolicy = f.getWaitPolicy()
ba.Header.MaxSpanRequestKeys = f.getBatchSize()
if ba.Header.MaxSpanRequestKeys > 0 {
// If this kvfetcher limits the number of rows returned, also use
// target bytes to guard against the case in which the average row
// is very large.
// If no limit is set, the assumption is that SQL *knows* that there
// is only a "small" amount of data to be read, and wants to preserve
// concurrency for this request inside of DistSender, which setting
// TargetBytes would interfere with.
ba.Header.TargetBytes = maxScanResponseBytes
}
ba.Requests = make([]roachpb.RequestUnion, len(f.spans))
keyLocking := f.getKeyLockingStrength()
if f.reverse {
scans := make([]struct {
req roachpb.ReverseScanRequest
union roachpb.RequestUnion_ReverseScan
}, len(f.spans))
for i := range f.spans {
scans[i].req.SetSpan(f.spans[i])
scans[i].req.ScanFormat = roachpb.BATCH_RESPONSE
scans[i].req.KeyLocking = keyLocking
scans[i].union.ReverseScan = &scans[i].req
ba.Requests[i].Value = &scans[i].union
}
} else {
if f.needValueCols {
scans := make([]struct {
req roachpb.ScanRequest
union roachpb.RequestUnion_Scan
}, len(f.spans))
for i := range f.spans {
scans[i].req.SetSpan(f.spans[i])
scans[i].req.ScanFormat = roachpb.BATCH_RESPONSE
scans[i].req.KeyLocking = keyLocking
scans[i].union.Scan = &scans[i].req
ba.Requests[i].Value = &scans[i].union
}
} else {
scans := make([]roachpb.CheckExistsRequest, len(f.spans))
for i := range f.spans {
scans[i].SetSpan(f.spans[i])
ba.Requests[i].MustSetInner(&scans[i])
}
ba.Header.MaxSpanRequestKeys = 0
ba.Header.TargetBytes = 0
}
}
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) {
var buf bytes.Buffer
for i, span := range f.spans {
if i != 0 {
buf.WriteString(", ")
}
buf.WriteString(span.String())
}
log.VEventf(ctx, 2, "Scan %s", 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",
catalogkeys.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
}
}
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
case *roachpb.CheckExistsResponse:
if t.Exists {
kvs = []roachpb.KeyValue{{
Key: origSpan.Key,
}}
}
return true, kvs, 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)
}