forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.go
516 lines (429 loc) · 16.9 KB
/
api.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
// Copyright 2014 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. See the AUTHORS file
// for names of contributors.
//
// Author: Spencer Kimball (spencer.kimball@gmail.com)
package proto
import (
"fmt"
"math/rand"
"github.com/cockroachdb/cockroach/util/retry"
gogoproto "github.com/gogo/protobuf/proto"
)
// A RangeID is a unique ID associated to a Raft consensus group.
type RangeID int64
// IsEmpty returns true if the client command ID has zero values.
func (ccid ClientCmdID) IsEmpty() bool {
return ccid.WallTime == 0 && ccid.Random == 0
}
// TraceID implements tracer.Traceable and returns the ClientCmdID in the
// format "c<WallTime>.<Random>".
func (ccid ClientCmdID) TraceID() string {
return fmt.Sprint("c", ccid.WallTime, ".", ccid.Random)
}
// TraceName implements tracer.Traceable.
func (ccid ClientCmdID) TraceName() string {
// 1 day = 8.64*10E13ns
return fmt.Sprint("c", ccid.WallTime%10E14, ".", ccid.Random%100)
}
const (
isAdmin = 1 << iota // admin cmds don't go through raft, but run on leader
isRead // read-only cmds don't go through raft, but may run on leader
isWrite // write cmds go through raft and must be proposed on leader
isTxnWrite // txn write cmds start heartbeat and are marked for intent resolution
isRange // range commands may span multiple keys
)
// IsAdmin returns true if the request is an admin request.
func IsAdmin(args Request) bool {
return (args.flags() & isAdmin) != 0
}
// IsRead returns true if the request is a read request.
func IsRead(args Request) bool {
return (args.flags() & isRead) != 0
}
// IsWrite returns true if the request is a write request.
func IsWrite(args Request) bool {
return (args.flags() & isWrite) != 0
}
// IsReadOnly returns true if the request is read-only.
func IsReadOnly(args Request) bool {
return IsRead(args) && !IsWrite(args)
}
// IsTransactionWrite returns true if the request produces write
// intents when used within a transaction.
func IsTransactionWrite(args Request) bool {
return (args.flags() & isTxnWrite) != 0
}
// IsRange returns true if the operation is range-based and must include
// a start and an end key.
func IsRange(args Request) bool {
return (args.flags() & isRange) != 0
}
// Request is an interface for RPC requests.
type Request interface {
gogoproto.Message
// Header returns the request header.
Header() *RequestHeader
// Method returns the request method.
Method() Method
// CreateReply creates a new response object.
CreateReply() Response
flags() int
}
// Response is an interface for RPC responses.
type Response interface {
gogoproto.Message
// Header returns the response header.
Header() *ResponseHeader
// Verify verifies response integrity, as applicable.
Verify(req Request) error
}
// Combinable is implemented by response types whose corresponding
// requests may cross range boundaries, such as Scan or DeleteRange.
// Combine() allows responses from individual ranges to be aggregated
// into a single one.
// It is not expected that Combine() perform any error checking; this
// should be done by the caller instead.
type Combinable interface {
Combine(Response)
}
// GetUser implements userRequest.
// KV messages are always sent by the node user.
func (rh *RequestHeader) GetUser() string {
// TODO(marc): we should use security.NodeUser here, but we need to break cycles first.
return "node"
}
// GetOrCreateCmdID returns the request header's command ID if available.
// Otherwise, creates a new ClientCmdID, initialized with current time
// and random salt.
func (rh *RequestHeader) GetOrCreateCmdID(walltime int64) (cmdID ClientCmdID) {
if !rh.CmdID.IsEmpty() {
cmdID = rh.CmdID
} else {
cmdID = ClientCmdID{
WallTime: walltime,
Random: rand.Int63(),
}
}
return
}
// TraceID implements tracer.Traceable by returning the first nontrivial
// TraceID of the Transaction and CmdID.
func (rh *RequestHeader) TraceID() string {
if r := rh.Txn.TraceID(); r != "" {
return r
}
return rh.CmdID.TraceID()
}
// TraceName implements tracer.Traceable and behaves like TraceID, but using
// the TraceName of the object delegated to.
func (rh *RequestHeader) TraceName() string {
if r := rh.Txn.TraceID(); r != "" {
return rh.Txn.TraceName()
}
return rh.CmdID.TraceName()
}
// Combine is used by range-spanning Response types (e.g. Scan or DeleteRange)
// to merge their headers.
func (rh *ResponseHeader) Combine(otherRH *ResponseHeader) {
if rh != nil {
if ts := otherRH.GetTimestamp(); rh.Timestamp.Less(ts) {
rh.Timestamp = ts
}
if rh.Txn != nil && otherRH.GetTxn() == nil {
rh.Txn = nil
}
}
}
// Combine implements the Combinable interface.
func (sr *ScanResponse) Combine(c Response) {
otherSR := c.(*ScanResponse)
if sr != nil {
sr.Rows = append(sr.Rows, otherSR.GetRows()...)
sr.Header().Combine(otherSR.Header())
}
}
// Combine implements the Combinable interface.
func (sr *ReverseScanResponse) Combine(c Response) {
otherSR := c.(*ReverseScanResponse)
if sr != nil {
sr.Rows = append(sr.Rows, otherSR.GetRows()...)
sr.Header().Combine(otherSR.Header())
}
}
// Combine implements the Combinable interface.
func (dr *DeleteRangeResponse) Combine(c Response) {
otherDR := c.(*DeleteRangeResponse)
if dr != nil {
dr.NumDeleted += otherDR.GetNumDeleted()
dr.Header().Combine(otherDR.Header())
}
}
// Combine implements the Combinable interface.
func (rr *ResolveIntentRangeResponse) Combine(c Response) {
otherRR := c.(*ResolveIntentRangeResponse)
if rr != nil {
rr.Header().Combine(otherRR.Header())
}
}
// Header implements the Request interface for RequestHeader.
func (rh *RequestHeader) Header() *RequestHeader {
return rh
}
// Header implements the Response interface for ResponseHeader.
func (rh *ResponseHeader) Header() *ResponseHeader {
return rh
}
// Verify implements the Response interface for ResopnseHeader with a
// default noop. Individual response types should override this method
// if they contain checksummed data which can be verified.
func (rh *ResponseHeader) Verify(req Request) error {
return nil
}
// GoError returns the non-nil error from the proto.Error union.
func (rh *ResponseHeader) GoError() error {
if rh.Error == nil {
return nil
}
if rh.Error.Detail == nil {
return rh.Error
}
errVal := rh.Error.Detail.GetValue()
if errVal == nil {
// Unknown error detail; return the generic error.
return rh.Error
}
err := errVal.(error)
// Make sure that the flags in the generic portion of the error
// match the methods of the specific error type.
if rh.Error.Retryable {
if r, ok := err.(retry.Retryable); !ok || !r.CanRetry() {
panic(fmt.Sprintf("inconsistent error proto; expected %T to be retryable", err))
}
}
if r, ok := err.(TransactionRestartError); ok {
if r.CanRestartTransaction() != rh.Error.TransactionRestart {
panic(fmt.Sprintf("inconsistent error proto; expected %T to have restart mode %v",
err, rh.Error.TransactionRestart))
}
} else {
// Error type doesn't implement TransactionRestartError, so expect it to have the default.
if rh.Error.TransactionRestart != TransactionRestart_ABORT {
panic(fmt.Sprintf("inconsistent error proto; expected %T to have restart mode ABORT", err))
}
}
return err
}
// SetGoError converts the specified type into either one of the proto-
// defined error types or into a Error for all other Go errors.
func (rh *ResponseHeader) SetGoError(err error) {
if err == nil {
rh.Error = nil
return
}
rh.Error = &Error{}
rh.Error.SetResponseGoError(err)
}
// Verify verifies the integrity of the get response value.
func (gr *GetResponse) Verify(req Request) error {
if gr.Value != nil {
return gr.Value.Verify(req.Header().Key)
}
return nil
}
// Verify verifies the integrity of every value returned in the scan.
func (sr *ScanResponse) Verify(req Request) error {
for _, kv := range sr.Rows {
if err := kv.Value.Verify(kv.Key); err != nil {
return err
}
}
return nil
}
// Verify verifies the integrity of every value returned in the reverse scan.
func (sr *ReverseScanResponse) Verify(req Request) error {
for _, kv := range sr.Rows {
if err := kv.Value.Verify(kv.Key); err != nil {
return err
}
}
return nil
}
// Add adds a request to the batch request. The batch inherits
// the key range of the first request added to it.
//
// TODO(spencer): batches should include a list of key ranges
// representing the constituent requests.
func (br *BatchRequest) Add(args Request) {
union := RequestUnion{}
if !union.SetValue(args) {
panic(fmt.Sprintf("unable to add %T to batch request", args))
}
if br.Key == nil {
br.Key = args.Header().Key
br.EndKey = args.Header().EndKey
}
br.Requests = append(br.Requests, union)
}
// Add adds a response to the batch response.
func (br *BatchResponse) Add(reply Response) {
union := ResponseUnion{}
if !union.SetValue(reply) {
// TODO(tschottdorf) evaluate whether this should return an error.
panic(fmt.Sprintf("unable to add %T to batch response", reply))
}
br.Responses = append(br.Responses, union)
}
// Bounded is implemented by request types which have a bounded number of
// result rows, such as Scan.
type Bounded interface {
GetBound() int64
SetBound(bound int64)
}
// GetBound returns the MaxResults field in ScanRequest.
func (sr *ScanRequest) GetBound() int64 {
return sr.GetMaxResults()
}
// SetBound sets the MaxResults field in ScanRequest.
func (sr *ScanRequest) SetBound(bound int64) {
sr.MaxResults = bound
}
// GetBound returns the MaxResults field in ReverseScanRequest.
func (sr *ReverseScanRequest) GetBound() int64 {
return sr.GetMaxResults()
}
// SetBound sets the MaxResults field in ReverseScanRequest.
func (sr *ReverseScanRequest) SetBound(bound int64) {
sr.MaxResults = bound
}
// Countable is implemented by response types which have a number of
// result rows, such as Scan.
type Countable interface {
Count() int64
}
// Count returns the number of rows in ScanResponse.
func (sr *ScanResponse) Count() int64 {
return int64(len(sr.Rows))
}
// Count returns the number of rows in ReverseScanResponse.
func (sr *ReverseScanResponse) Count() int64 {
return int64(len(sr.Rows))
}
// Method implements the Request interface.
func (*GetRequest) Method() Method { return Get }
// Method implements the Request interface.
func (*PutRequest) Method() Method { return Put }
// Method implements the Request interface.
func (*ConditionalPutRequest) Method() Method { return ConditionalPut }
// Method implements the Request interface.
func (*IncrementRequest) Method() Method { return Increment }
// Method implements the Request interface.
func (*DeleteRequest) Method() Method { return Delete }
// Method implements the Request interface.
func (*DeleteRangeRequest) Method() Method { return DeleteRange }
// Method implements the Request interface.
func (*ScanRequest) Method() Method { return Scan }
// Method implements the Request interface.
func (*ReverseScanRequest) Method() Method { return ReverseScan }
// Method implements the Request interface.
func (*EndTransactionRequest) Method() Method { return EndTransaction }
// Method implements the Request interface.
func (*AdminSplitRequest) Method() Method { return AdminSplit }
// Method implements the Request interface.
func (*AdminMergeRequest) Method() Method { return AdminMerge }
// Method implements the Request interface.
func (*HeartbeatTxnRequest) Method() Method { return HeartbeatTxn }
// Method implements the Request interface.
func (*GCRequest) Method() Method { return GC }
// Method implements the Request interface.
func (*PushTxnRequest) Method() Method { return PushTxn }
// Method implements the Request interface.
func (*RangeLookupRequest) Method() Method { return RangeLookup }
// Method implements the Request interface.
func (*ResolveIntentRequest) Method() Method { return ResolveIntent }
// Method implements the Request interface.
func (*ResolveIntentRangeRequest) Method() Method { return ResolveIntentRange }
// Method implements the Request interface.
func (*MergeRequest) Method() Method { return Merge }
// Method implements the Request interface.
func (*TruncateLogRequest) Method() Method { return TruncateLog }
// Method implements the Request interface.
func (*LeaderLeaseRequest) Method() Method { return LeaderLease }
// Method implements the Request interface.
func (*BatchRequest) Method() Method { return Batch }
// CreateReply implements the Request interface.
func (*GetRequest) CreateReply() Response { return &GetResponse{} }
// CreateReply implements the Request interface.
func (*PutRequest) CreateReply() Response { return &PutResponse{} }
// CreateReply implements the Request interface.
func (*ConditionalPutRequest) CreateReply() Response { return &ConditionalPutResponse{} }
// CreateReply implements the Request interface.
func (*IncrementRequest) CreateReply() Response { return &IncrementResponse{} }
// CreateReply implements the Request interface.
func (*DeleteRequest) CreateReply() Response { return &DeleteResponse{} }
// CreateReply implements the Request interface.
func (*DeleteRangeRequest) CreateReply() Response { return &DeleteRangeResponse{} }
// CreateReply implements the Request interface.
func (*ScanRequest) CreateReply() Response { return &ScanResponse{} }
// CreateReply implements the Request interface.
func (*ReverseScanRequest) CreateReply() Response { return &ReverseScanResponse{} }
// CreateReply implements the Request interface.
func (*EndTransactionRequest) CreateReply() Response { return &EndTransactionResponse{} }
// CreateReply implements the Request interface.
func (*AdminSplitRequest) CreateReply() Response { return &AdminSplitResponse{} }
// CreateReply implements the Request interface.
func (*AdminMergeRequest) CreateReply() Response { return &AdminMergeResponse{} }
// CreateReply implements the Request interface.
func (*HeartbeatTxnRequest) CreateReply() Response { return &HeartbeatTxnResponse{} }
// CreateReply implements the Request interface.
func (*GCRequest) CreateReply() Response { return &GCResponse{} }
// CreateReply implements the Request interface.
func (*PushTxnRequest) CreateReply() Response { return &PushTxnResponse{} }
// CreateReply implements the Request interface.
func (*RangeLookupRequest) CreateReply() Response { return &RangeLookupResponse{} }
// CreateReply implements the Request interface.
func (*ResolveIntentRequest) CreateReply() Response { return &ResolveIntentResponse{} }
// CreateReply implements the Request interface.
func (*ResolveIntentRangeRequest) CreateReply() Response {
return &ResolveIntentRangeResponse{}
}
// CreateReply implements the Request interface.
func (*MergeRequest) CreateReply() Response { return &MergeResponse{} }
// CreateReply implements the Request interface.
func (*TruncateLogRequest) CreateReply() Response { return &TruncateLogResponse{} }
// CreateReply implements the Request interface.
func (*LeaderLeaseRequest) CreateReply() Response { return &LeaderLeaseResponse{} }
// CreateReply implements the Request interface.
func (*BatchRequest) CreateReply() Response { return &BatchResponse{} }
func (*GetRequest) flags() int { return isRead }
func (*PutRequest) flags() int { return isWrite | isTxnWrite }
func (*ConditionalPutRequest) flags() int { return isRead | isWrite | isTxnWrite }
func (*IncrementRequest) flags() int { return isRead | isWrite | isTxnWrite }
func (*DeleteRequest) flags() int { return isWrite | isTxnWrite }
func (*DeleteRangeRequest) flags() int { return isWrite | isTxnWrite | isRange }
func (*ScanRequest) flags() int { return isRead | isRange }
func (*ReverseScanRequest) flags() int { return isRead | isRange }
func (*EndTransactionRequest) flags() int { return isWrite }
func (*AdminSplitRequest) flags() int { return isAdmin }
func (*AdminMergeRequest) flags() int { return isAdmin }
func (*HeartbeatTxnRequest) flags() int { return isWrite }
func (*GCRequest) flags() int { return isWrite | isRange }
func (*PushTxnRequest) flags() int { return isWrite }
func (*RangeLookupRequest) flags() int { return isRead }
func (*ResolveIntentRequest) flags() int { return isWrite }
func (*ResolveIntentRangeRequest) flags() int { return isWrite | isRange }
func (*MergeRequest) flags() int { return isWrite }
func (*TruncateLogRequest) flags() int { return isWrite }
func (*LeaderLeaseRequest) flags() int { return isWrite }
func (*BatchRequest) flags() int { return isWrite }