forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kvfetcher.go
302 lines (270 loc) · 8.51 KB
/
kvfetcher.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
// Copyright 2016 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.
//
// Author: Peter Mattis (peter@cockroachlabs.com)
// Author: Radu Berinde (radu@cockroachlabs.com)
package sqlbase
import (
"bytes"
"fmt"
"strings"
"github.com/pkg/errors"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/internal/client"
"github.com/cockroachdb/cockroach/pkg/roachpb"
)
// PrettyKey pretty-prints the specified key, skipping over the first `skip`
// fields. The pretty printed key looks like:
//
// /Table/<tableID>/<indexID>/...
//
// We always strip off the /Table prefix and then `skip` more fields. Note that
// this assumes that the fields themselves do not contain '/', but that is
// currently true for the fields we care about stripping (the table and index
// ID).
func PrettyKey(key roachpb.Key, skip int) string {
p := key.String()
for i := 0; i <= skip; i++ {
n := strings.IndexByte(p[1:], '/')
if n == -1 {
return ""
}
p = p[n+1:]
}
return p
}
// PrettySpan returns a human-readable representation of a span.
func PrettySpan(span roachpb.Span, skip int) string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "%s-%s", PrettyKey(span.Key, skip), PrettyKey(span.EndKey, skip))
return buf.String()
}
// PrettySpans returns a human-readable description of the spans.
func PrettySpans(spans []roachpb.Span, skip int) string {
var buf bytes.Buffer
for i, span := range spans {
if i > 0 {
buf.WriteString(" ")
}
buf.WriteString(PrettySpan(span, skip))
}
return buf.String()
}
// 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
// SetKVBatchSize changes the kvFetcher batch size, and returns a function that restores it.
func SetKVBatchSize(val int64) func() {
oldVal := kvBatchSize
kvBatchSize = val
return func() { kvBatchSize = oldVal }
}
// kvFetcher handles retrieval of key/values.
type kvFetcher struct {
// "Constant" fields, provided by the caller.
txn *client.Txn
spans roachpb.Spans
reverse bool
useBatchLimit bool
firstBatchLimit int64
// Fields used to avoid allocations.
batch client.Batch
batchIdx int
fetchEnd bool
kvs []client.KeyValue
kvIndex int
totalFetched int64
// returnRangeInfo, is set, causes the kvFetcher to populate rangeInfos.
// See also rowFetcher.returnRangeInfo.
returnRangeInfo bool
// As the kvFetcher 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 kvFetcher.spans in any particular way.
rangeInfos []roachpb.RangeInfo
}
func (f *kvFetcher) getRangesInfo() []roachpb.RangeInfo {
if !f.returnRangeInfo {
panic("GetRangeInfo() called on kvFetcher that wasn't configured with returnRangeInfo")
}
return f.rangeInfos
}
// getBatchSize returns the max size of the next batch.
func (f *kvFetcher) getBatchSize() 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 f.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
}
}
// makeKVFetcher initializes a kvFetcher 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 makeKVFetcher(
txn *client.Txn,
spans roachpb.Spans,
reverse bool,
useBatchLimit bool,
firstBatchLimit int64,
returnRangeInfo bool,
) (kvFetcher, error) {
if firstBatchLimit < 0 || (!useBatchLimit && firstBatchLimit != 0) {
return kvFetcher{}, 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 kvFetcher{}, errors.Errorf("unordered 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 kvFetcher{
txn: txn,
spans: copySpans,
reverse: reverse,
useBatchLimit: useBatchLimit,
firstBatchLimit: firstBatchLimit,
returnRangeInfo: returnRangeInfo,
}, nil
}
// fetch retrieves spans from the kv
func (f *kvFetcher) fetch(ctx context.Context) error {
batchSize := f.getBatchSize()
b := &f.batch
*b = client.Batch{}
b.Header.MaxSpanRequestKeys = batchSize
b.Header.ReturnRangeInfo = f.returnRangeInfo
for _, span := range f.spans {
if f.reverse {
b.ReverseScan(span.Key, span.EndKey)
} else {
b.Scan(span.Key, span.EndKey)
}
}
// Reset spans and add resume-spans later.
f.spans = f.spans[:0]
if err := f.txn.Run(ctx, b); err != nil {
return err
}
if f.kvs == nil {
numResults := 0
for _, result := range b.Results {
numResults += len(result.Rows)
}
f.kvs = make([]client.KeyValue, 0, numResults)
} else {
f.kvs = f.kvs[:0]
}
// Set end to true until disproved.
f.fetchEnd = true
var sawResumeSpan bool
for _, result := range b.Results {
if result.ResumeSpan.Key != nil {
// A span needs to be resumed.
f.fetchEnd = false
f.spans = append(f.spans, result.ResumeSpan)
}
if len(result.Rows) > 0 {
if sawResumeSpan {
return errors.Errorf(
"span with results after resume span; new spans: %s",
PrettySpans(f.spans, 0))
}
f.kvs = append(f.kvs, result.Rows...)
}
if result.ResumeSpan.Key != nil {
// Verify we don't receive results for any remaining spans.
sawResumeSpan = true
}
if f.returnRangeInfo {
for _, ri := range result.RangeInfos {
f.rangeInfos = roachpb.InsertRangeInfo(f.rangeInfos, ri)
}
}
}
f.batchIdx++
f.totalFetched += int64(len(f.kvs))
f.kvIndex = 0
// 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
}
// nextKV returns the next key/value (initiating fetches as necessary). When there are no more keys,
// returns false and an empty key/value.
func (f *kvFetcher) nextKV(ctx context.Context) (bool, client.KeyValue, error) {
if f.kvIndex == len(f.kvs) {
if f.fetchEnd {
return false, client.KeyValue{}, nil
}
err := f.fetch(ctx)
if err != nil {
return false, client.KeyValue{}, err
}
if len(f.kvs) == 0 {
return false, client.KeyValue{}, nil
}
}
f.kvIndex++
return true, f.kvs[f.kvIndex-1], nil
}