-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.go
610 lines (558 loc) · 19.6 KB
/
db.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
// Copyright 2015 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 client
import (
"bytes"
"fmt"
"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"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/tracing"
)
// KeyValue represents a single key/value pair. This is similar to
// roachpb.KeyValue except that the value may be nil.
type KeyValue struct {
Key roachpb.Key
Value *roachpb.Value // Timestamp will always be zero
}
func (kv *KeyValue) String() string {
return kv.Key.String() + "=" + kv.PrettyValue()
}
// Exists returns true iff the value exists.
func (kv *KeyValue) Exists() bool {
return kv.Value != nil
}
// PrettyValue returns a human-readable version of the value as a string.
func (kv *KeyValue) PrettyValue() string {
if kv.Value == nil {
return "nil"
}
switch kv.Value.GetTag() {
case roachpb.ValueType_INT:
v, err := kv.Value.GetInt()
if err != nil {
return fmt.Sprintf("%v", err)
}
return fmt.Sprintf("%d", v)
case roachpb.ValueType_FLOAT:
v, err := kv.Value.GetFloat()
if err != nil {
return fmt.Sprintf("%v", err)
}
return fmt.Sprintf("%v", v)
case roachpb.ValueType_BYTES:
v, err := kv.Value.GetBytes()
if err != nil {
return fmt.Sprintf("%v", err)
}
return fmt.Sprintf("%q", v)
case roachpb.ValueType_TIME:
v, err := kv.Value.GetTime()
if err != nil {
return fmt.Sprintf("%v", err)
}
return v.String()
}
return fmt.Sprintf("%x", kv.Value.RawBytes)
}
// ValueBytes returns the value as a byte slice. This method will panic if the
// value's type is not a byte slice.
func (kv *KeyValue) ValueBytes() []byte {
if kv.Value == nil {
return nil
}
bytes, err := kv.Value.GetBytes()
if err != nil {
panic(err)
}
return bytes
}
// ValueInt returns the value decoded as an int64. This method will panic if
// the value cannot be decoded as an int64.
func (kv *KeyValue) ValueInt() int64 {
if kv.Value == nil {
return 0
}
i, err := kv.Value.GetInt()
if err != nil {
panic(err)
}
return i
}
// ValueProto parses the byte slice value into msg.
func (kv *KeyValue) ValueProto(msg proto.Message) error {
if kv.Value == nil {
msg.Reset()
return nil
}
return kv.Value.GetProto(msg)
}
// Result holds the result for a single DB or Txn operation (e.g. Get, Put,
// etc).
type Result struct {
calls int
// Err contains any error encountered when performing the operation.
Err error
// Rows contains the key/value pairs for the operation. The number of rows
// returned varies by operation. For Get, Put, CPut, Inc and Del the number
// of rows returned is the number of keys operated on. For Scan the number of
// rows returned is the number or rows matching the scan capped by the
// maxRows parameter. For DelRange Rows is nil.
Rows []KeyValue
// Keys is set by some operations instead of returning the rows themselves.
Keys []roachpb.Key
// ResumeSpan is the the span to be used on the next operation in a
// sequence of operations. It is returned whenever an operation over a
// span of keys is bounded and the operation returns before completely
// running over the span. It allows the operation to be called again with
// a new shorter span of keys. An empty span is returned when the
// operation has successfully completed running through the span.
ResumeSpan roachpb.Span
// RangeInfos contains information about the replicas that produced this
// result.
// This is only populated if Err == nil and if ReturnRangeInfo has been set on
// the request.
RangeInfos []roachpb.RangeInfo
}
func (r Result) String() string {
if r.Err != nil {
return r.Err.Error()
}
var buf bytes.Buffer
for i, row := range r.Rows {
if i > 0 {
buf.WriteString("\n")
}
fmt.Fprintf(&buf, "%d: %s", i, &row)
}
return buf.String()
}
// DBContext contains configuration parameters for DB.
type DBContext struct {
// UserPriority is the default user priority to set on API calls. If
// userPriority is set to any value except 1 in call arguments, this
// value is ignored.
UserPriority roachpb.UserPriority
}
// DefaultDBContext returns (a copy of) the default options for
// NewDBWithContext.
func DefaultDBContext() DBContext {
return DBContext{
UserPriority: roachpb.NormalUserPriority,
}
}
// DB is a database handle to a single cockroach cluster. A DB is safe for
// concurrent use by multiple goroutines.
type DB struct {
sender Sender
clock *hlc.Clock
ctx DBContext
}
// GetSender returns the underlying Sender. Only exported for tests.
func (db *DB) GetSender() Sender {
return db.sender
}
// NewDB returns a new DB.
func NewDB(sender Sender, clock *hlc.Clock) *DB {
return NewDBWithContext(sender, clock, DefaultDBContext())
}
// NewDBWithContext returns a new DB with the given parameters.
func NewDBWithContext(sender Sender, clock *hlc.Clock, ctx DBContext) *DB {
return &DB{
sender: sender,
clock: clock,
ctx: ctx,
}
}
// Get retrieves the value for a key, returning the retrieved key/value or an
// error. It is not considered an error for the key not to exist.
//
// r, err := db.Get("a")
// // string(r.Key) == "a"
//
// key can be either a byte slice or a string.
func (db *DB) Get(ctx context.Context, key interface{}) (KeyValue, error) {
b := &Batch{}
b.Get(key)
return getOneRow(db.Run(ctx, b), b)
}
// GetProto retrieves the value for a key and decodes the result as a proto
// message. If the key doesn't exist, the proto will simply be reset.
//
// key can be either a byte slice or a string.
func (db *DB) GetProto(ctx context.Context, key interface{}, msg proto.Message) error {
r, err := db.Get(ctx, key)
if err != nil {
return err
}
return r.ValueProto(msg)
}
// Put sets the value for a key.
//
// key can be either a byte slice or a string. value can be any key type, a
// proto.Message or any Go primitive type (bool, int, etc).
func (db *DB) Put(ctx context.Context, key, value interface{}) error {
b := &Batch{}
b.Put(key, value)
return getOneErr(db.Run(ctx, b), b)
}
// PutInline sets the value for a key, but does not maintain
// multi-version values. The most recent value is always overwritten.
// Inline values cannot be mutated transactionally and should be used
// with caution.
//
// key can be either a byte slice or a string. value can be any key type, a
// proto.Message or any Go primitive type (bool, int, etc).
func (db *DB) PutInline(ctx context.Context, key, value interface{}) error {
b := &Batch{}
b.PutInline(key, value)
return getOneErr(db.Run(ctx, b), b)
}
// CPut conditionally sets the value for a key if the existing value is equal
// to expValue. To conditionally set a value only if there is no existing entry
// pass nil for expValue. Note that this must be an interface{}(nil), not a
// typed nil value (e.g. []byte(nil)).
//
// Returns an error if the existing value is not equal to expValue.
//
// key can be either a byte slice or a string. value can be any key type, a
// proto.Message or any Go primitive type (bool, int, etc).
func (db *DB) CPut(ctx context.Context, key, value, expValue interface{}) error {
b := &Batch{}
b.CPut(key, value, expValue)
return getOneErr(db.Run(ctx, b), b)
}
// InitPut sets the first value for a key to value. A ConditionFailedError is
// reported if a value already exists for the key and it's not equal to the
// value passed in. If failOnTombstones is set to true, tombstones count as
// mismatched values and will cause a ConditionFailedError.
//
// key can be either a byte slice or a string. value can be any key type, a
// proto.Message or any Go primitive type (bool, int, etc). It is illegal to
// set value to nil.
func (db *DB) InitPut(ctx context.Context, key, value interface{}, failOnTombstones bool) error {
b := &Batch{}
b.InitPut(key, value, failOnTombstones)
return getOneErr(db.Run(ctx, b), b)
}
// Inc increments the integer value at key. If the key does not exist it will
// be created with an initial value of 0 which will then be incremented. If the
// key exists but was set using Put or CPut an error will be returned.
//
// key can be either a byte slice or a string.
func (db *DB) Inc(ctx context.Context, key interface{}, value int64) (KeyValue, error) {
b := &Batch{}
b.Inc(key, value)
return getOneRow(db.Run(ctx, b), b)
}
func (db *DB) scan(
ctx context.Context,
begin, end interface{},
maxRows int64,
isReverse bool,
readConsistency roachpb.ReadConsistencyType,
) ([]KeyValue, error) {
b := &Batch{}
b.Header.ReadConsistency = readConsistency
if maxRows > 0 {
b.Header.MaxSpanRequestKeys = maxRows
}
if !isReverse {
b.Scan(begin, end)
} else {
b.ReverseScan(begin, end)
}
r, err := getOneResult(db.Run(ctx, b), b)
return r.Rows, err
}
// Scan retrieves the rows between begin (inclusive) and end (exclusive) in
// ascending order.
//
// The returned []KeyValue will contain up to maxRows elements.
//
// key can be either a byte slice or a string.
func (db *DB) Scan(ctx context.Context, begin, end interface{}, maxRows int64) ([]KeyValue, error) {
return db.scan(ctx, begin, end, maxRows, false, roachpb.CONSISTENT)
}
// ReverseScan retrieves the rows between begin (inclusive) and end (exclusive)
// in descending order.
//
// The returned []KeyValue will contain up to maxRows elements.
//
// key can be either a byte slice or a string.
func (db *DB) ReverseScan(
ctx context.Context, begin, end interface{}, maxRows int64,
) ([]KeyValue, error) {
return db.scan(ctx, begin, end, maxRows, true, roachpb.CONSISTENT)
}
// Del deletes one or more keys.
//
// key can be either a byte slice or a string.
func (db *DB) Del(ctx context.Context, keys ...interface{}) error {
b := &Batch{}
b.Del(keys...)
return getOneErr(db.Run(ctx, b), b)
}
// DelRange deletes the rows between begin (inclusive) and end (exclusive).
//
// TODO(pmattis): Perhaps the result should return which rows were deleted.
//
// key can be either a byte slice or a string.
func (db *DB) DelRange(ctx context.Context, begin, end interface{}) error {
b := &Batch{}
b.DelRange(begin, end, false)
return getOneErr(db.Run(ctx, b), b)
}
// AdminMerge merges the range containing key and the subsequent
// range. After the merge operation is complete, the range containing
// key will contain all of the key/value pairs of the subsequent range
// and the subsequent range will no longer exist.
//
// key can be either a byte slice or a string.
func (db *DB) AdminMerge(ctx context.Context, key interface{}) error {
b := &Batch{}
b.adminMerge(key)
return getOneErr(db.Run(ctx, b), b)
}
// AdminSplit splits the range at splitkey.
//
// spanKey is a key within the range that should be split, and splitKey is the
// key at which that range should be split. splitKey is not used exactly as
// provided--it is first mutated by keys.EnsureSafeSplitKey. Accounting for
// this mutation sometimes requires constructing a key that falls in a
// different range, hence the separation between spanKey and splitKey. See
// #16008 for details, and #16344 for the tracking issue to clean this mess up
// properly.
//
// keys can be either a byte slice or a string.
func (db *DB) AdminSplit(ctx context.Context, spanKey, splitKey interface{}) error {
b := &Batch{}
b.adminSplit(spanKey, splitKey)
return getOneErr(db.Run(ctx, b), b)
}
// AdminTransferLease transfers the lease for the range containing key to the
// specified target. The target replica for the lease transfer must be one of
// the existing replicas of the range.
//
// key can be either a byte slice or a string.
//
// When this method returns, it's guaranteed that the old lease holder has
// applied the new lease, but that's about it. It's not guaranteed that the new
// lease holder has applied it (so it might not know immediately that it is the
// new lease holder).
func (db *DB) AdminTransferLease(
ctx context.Context, key interface{}, target roachpb.StoreID,
) error {
b := &Batch{}
b.adminTransferLease(key, target)
return getOneErr(db.Run(ctx, b), b)
}
// AdminChangeReplicas adds or removes a set of replicas for a range.
func (db *DB) AdminChangeReplicas(
ctx context.Context,
key interface{},
changeType roachpb.ReplicaChangeType,
targets []roachpb.ReplicationTarget,
) error {
b := &Batch{}
b.adminChangeReplicas(key, changeType, targets)
return getOneErr(db.Run(ctx, b), b)
}
// CheckConsistency runs a consistency check on all the ranges containing
// the key span. It logs a diff of all the keys that are inconsistent
// when withDiff is set to true.
func (db *DB) CheckConsistency(ctx context.Context, begin, end interface{}, withDiff bool) error {
b := &Batch{}
b.CheckConsistency(begin, end, withDiff)
return getOneErr(db.Run(ctx, b), b)
}
// WriteBatch applies the operations encoded in a BatchRepr, which is the
// serialized form of a RocksDB Batch. The command cannot span Ranges and must
// be run on an empty keyrange.
func (db *DB) WriteBatch(ctx context.Context, begin, end interface{}, data []byte) error {
b := &Batch{}
b.writeBatch(begin, end, data)
return getOneErr(db.Run(ctx, b), b)
}
// AddSSTable links a file into the RocksDB log-structured merge-tree. Existing
// data in the range is cleared.
func (db *DB) AddSSTable(ctx context.Context, begin, end interface{}, data []byte) error {
b := &Batch{}
b.addSSTable(begin, end, data)
return getOneErr(db.Run(ctx, b), b)
}
// sendAndFill is a helper which sends the given batch and fills its results,
// returning the appropriate error which is either from the first failing call,
// or an "internal" error.
func sendAndFill(ctx context.Context, send SenderFunc, b *Batch) error {
// Errors here will be attached to the results, so we will get them from
// the call to fillResults in the regular case in which an individual call
// fails. But send() also returns its own errors, so there's some dancing
// here to do because we want to run fillResults() so that the individual
// result gets initialized with an error from the corresponding call.
var ba roachpb.BatchRequest
ba.Requests = b.reqs
ba.Header = b.Header
b.response, b.pErr = send(ctx, ba)
if b.pErr != nil {
// Discard errors from fillResults.
_ = b.fillResults()
return b.pErr.GoError()
}
if err := b.fillResults(); err != nil {
b.pErr = roachpb.NewError(err)
return err
}
return nil
}
// Run executes the operations queued up within a batch. Before executing any
// of the operations the batch is first checked to see if there were any errors
// during its construction (e.g. failure to marshal a proto message).
//
// The operations within a batch are run in parallel and the order is
// non-deterministic. It is an unspecified behavior to modify and retrieve the
// same key within a batch.
//
// Upon completion, Batch.Results will contain the results for each
// operation. The order of the results matches the order the operations were
// added to the batch.
func (db *DB) Run(ctx context.Context, b *Batch) error {
if err := b.prepare(); err != nil {
return err
}
return sendAndFill(ctx, db.send, b)
}
// Txn executes retryable in the context of a distributed transaction. The
// transaction is automatically aborted if retryable returns any error aside
// from recoverable internal errors, and is automatically committed
// otherwise. The retryable function should have no side effects which could
// cause problems in the event it must be run more than once.
//
// If you need more control over how the txn is executed, check out txn.Exec().
func (db *DB) Txn(ctx context.Context, retryable func(context.Context, *Txn) error) error {
// TODO(radu): we should open a tracing Span here (we need to figure out how
// to use the correct tracer).
// TODO(andrei): revisit this when TxnCoordSender is moved to the client
// (https://github.com/cockroachdb/cockroach/issues/10511).
ctx, cancel := context.WithCancel(ctx)
defer cancel()
// TODO(andrei): Plumb a gatewayNodeID here. If we pass 0, then the gateway
// field will be filled in by the DistSender which will assume that the
// current node is the gateway.
txn := NewTxn(db, 0 /* gatewayNodeID */)
txn.SetDebugName("unnamed")
opts := TxnExecOptions{
AutoCommit: true,
AutoRetry: true,
}
err := txn.Exec(ctx, opts, func(ctx context.Context, txn *Txn, _ *TxnExecOptions) error {
return retryable(ctx, txn)
})
if err != nil {
txn.CleanupOnError(ctx, err)
}
// Terminate HandledRetryableTxnError here, so it doesn't cause a higher-level
// txn to be retried. We don't do this in any of the other functions in DB; I
// guess we should.
if _, ok := err.(*roachpb.HandledRetryableTxnError); ok {
return errors.Wrapf(err, "terminated retryable error")
}
return err
}
// send runs the specified calls synchronously in a single batch and returns
// any errors. Returns (nil, nil) for an empty batch.
func (db *DB) send(
ctx context.Context, ba roachpb.BatchRequest,
) (*roachpb.BatchResponse, *roachpb.Error) {
if len(ba.Requests) == 0 {
return nil, nil
}
if ba.UserPriority == 0 && db.ctx.UserPriority != 1 {
ba.UserPriority = db.ctx.UserPriority
}
if ba.ReadConsistency == roachpb.INCONSISTENT {
for _, ru := range ba.Requests {
m := ru.GetInner().Method()
switch m {
case roachpb.Get, roachpb.Scan, roachpb.ReverseScan:
default:
return nil, roachpb.NewErrorf("method %s not allowed with INCONSISTENT batch", m)
}
}
}
tracing.AnnotateTrace()
br, pErr := db.sender.Send(ctx, ba)
if pErr != nil {
if log.V(1) {
log.Infof(ctx, "failed batch: %s", pErr)
}
return nil, pErr
}
return br, nil
}
// getOneErr returns the error for a single-request Batch that was run.
// runErr is the error returned by Run, b is the Batch that was passed to Run.
func getOneErr(runErr error, b *Batch) error {
if runErr != nil && len(b.Results) > 0 {
return b.Results[0].Err
}
return runErr
}
// getOneResult returns the result for a single-request Batch that was run.
// runErr is the error returned by Run, b is the Batch that was passed to Run.
func getOneResult(runErr error, b *Batch) (Result, error) {
if runErr != nil {
if len(b.Results) > 0 {
return b.Results[0], b.Results[0].Err
}
return Result{Err: runErr}, runErr
}
res := b.Results[0]
if res.Err != nil {
panic("run succeeded even through the result has an error")
}
return res, nil
}
// getOneRow returns the first row for a single-request Batch that was run.
// runErr is the error returned by Run, b is the Batch that was passed to Run.
func getOneRow(runErr error, b *Batch) (KeyValue, error) {
res, err := getOneResult(runErr, b)
if err != nil {
return KeyValue{}, err
}
return res.Rows[0], nil
}
// IncrementValRetryable increments a key's value by a specified amount and
// returns the new value.
//
// It performs the increment as a retryable non-transactional increment. The key
// might be incremented multiple times because of the retries.
func IncrementValRetryable(ctx context.Context, db *DB, key roachpb.Key, inc int64) (int64, error) {
var err error
var res KeyValue
for r := retry.Start(base.DefaultRetryOptions()); r.Next(); {
res, err = db.Inc(ctx, key, inc)
switch err.(type) {
case *roachpb.UnhandledRetryableError, *roachpb.AmbiguousResultError:
continue
}
break
}
return res.ValueInt(), err
}