forked from cockroachdb/cockroach
/
kvfetcher.go
282 lines (251 loc) · 7.96 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
// 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 sql
import (
"bytes"
"fmt"
"strings"
"github.com/cockroachdb/cockroach/client"
"github.com/cockroachdb/cockroach/roachpb"
"github.com/cockroachdb/cockroach/sql/parser"
)
type span struct {
start roachpb.Key // inclusive key
end roachpb.Key // exclusive key
count int64 // max # of keys for this span
}
type spans []span
// implement Sort.Interface
func (a spans) Len() int { return len(a) }
func (a spans) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a spans) Less(i, j int) bool { return a[i].start.Compare(a[j].start) < 0 }
// 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
}
func prettyDatums(vals []parser.Datum) string {
var buf bytes.Buffer
for _, v := range vals {
fmt.Fprintf(&buf, "/%v", v)
}
return buf.String()
}
func prettySpan(span span, skip int) string {
var buf bytes.Buffer
if span.count != 0 {
fmt.Fprintf(&buf, "%d:", span.count)
}
fmt.Fprintf(&buf, "%s-%s", prettyKey(span.start, skip), prettyKey(span.end, skip))
return buf.String()
}
func prettySpans(spans []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 spans
reverse bool
firstBatchLimit int64
batchIdx int
fetchEnd bool
kvs []client.KeyValue
kvIndex int
totalFetched int64
}
// getBatchSize returns the max size of the next batch.
func (f *kvFetcher) getBatchSize() int64 {
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 non-zero, firstBatchLimit limits
// the size of the first batch (subsequent batches use the default size).
func makeKVFetcher(txn *client.Txn, spans spans, reverse bool, firstBatchLimit int64) kvFetcher {
if firstBatchLimit < 0 {
panic(fmt.Sprintf("invalid batch limit %d", firstBatchLimit))
}
return kvFetcher{txn: txn, spans: spans, reverse: reverse, firstBatchLimit: firstBatchLimit}
}
// fetch retrieves spans from the kv
func (f *kvFetcher) fetch() *roachpb.Error {
batchSize := f.getBatchSize()
b := &client.Batch{MaxScanResults: batchSize}
var resumeKey roachpb.Key
if len(f.kvs) > 0 {
resumeKey = f.kvs[len(f.kvs)-1].Key
// To resume forward scans we will set the (inclusive) scan start to the Next of the last
// received key. To resume reverse scans we will set the (exclusive) scan end to the last
// received key.
if !f.reverse {
resumeKey = resumeKey.ShallowNext()
}
}
atEnd := true
if !f.reverse {
for i := 0; i < len(f.spans); i++ {
start := f.spans[i].start
if resumeKey != nil {
if resumeKey.Compare(f.spans[i].end) >= 0 {
// We are resuming from a key after this span.
continue
}
if resumeKey.Compare(start) > 0 {
// We are resuming from a key inside this span.
// In this case we should technically reduce the max count for the span; but
// since this count is only an optimization it's not incorrect to retrieve more
// keys for the span.
start = resumeKey
}
}
atEnd = false
b.Scan(start, f.spans[i].end, f.spans[i].count)
}
} else {
for i := len(f.spans) - 1; i >= 0; i-- {
end := f.spans[i].end
if resumeKey != nil {
if resumeKey.Compare(f.spans[i].start) <= 0 {
// We are resuming from a key before this span.
continue
}
if resumeKey.Compare(end) < 0 {
// We are resuming from a key inside this span.
// In this case we should technically reduce the max count for the span; but
// since this count is only an optimization it's not incorrect to retrieve more
// keys for the span.
end = resumeKey
}
}
atEnd = false
b.ReverseScan(f.spans[i].start, end, f.spans[i].count)
}
}
if atEnd {
// The last scan happened to finish just at the end of the last span.
f.kvs = nil
f.fetchEnd = true
return nil
}
if pErr := f.txn.Run(b); pErr != nil {
return pErr
}
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]
}
for _, result := range b.Results {
f.kvs = append(f.kvs, result.Rows...)
}
f.batchIdx++
f.totalFetched += int64(len(f.kvs))
f.kvIndex = 0
if int64(len(f.kvs)) < batchSize {
f.fetchEnd = true
}
// 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() (bool, client.KeyValue, *roachpb.Error) {
if f.kvIndex == len(f.kvs) {
if f.fetchEnd {
return false, client.KeyValue{}, nil
}
pErr := f.fetch()
if pErr != nil {
return false, client.KeyValue{}, pErr
}
if len(f.kvs) == 0 {
return false, client.KeyValue{}, nil
}
}
f.kvIndex++
return true, f.kvs[f.kvIndex-1], nil
}