forked from cockroachdb/cockroach
/
batch.go
321 lines (290 loc) · 9.62 KB
/
batch.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
// 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: Tobias Schottdorf
package roachpb
import (
"errors"
"fmt"
"strings"
)
// IsAdmin returns true iff the BatchRequest contains an admin request.
func (ba *BatchRequest) IsAdmin() bool {
return ba.flags()&isAdmin != 0
}
// IsWrite returns true iff the BatchRequest contains a write.
func (ba *BatchRequest) IsWrite() bool {
return (ba.flags() & isWrite) != 0
}
// IsReadOnly returns true if all requests within are read-only.
func (ba *BatchRequest) IsReadOnly() bool {
flags := ba.flags()
return (flags&isRead) != 0 && (flags&isWrite) == 0
}
// IsReverse returns true iff the BatchRequest contains a reverse request.
func (ba *BatchRequest) IsReverse() bool {
return (ba.flags() & isReverse) != 0
}
// IsPossibleTransaction returns true iff the BatchRequest contains
// requests that can be part of a transaction.
func (ba *BatchRequest) IsPossibleTransaction() bool {
return (ba.flags() & isTxn) != 0
}
// IsTransactionWrite returns true iff the BatchRequest contains a txn write.
func (ba *BatchRequest) IsTransactionWrite() bool {
return (ba.flags() & isTxnWrite) != 0
}
// IsRange returns true iff the BatchRequest contains a range request.
func (ba *BatchRequest) IsRange() bool {
return (ba.flags() & isRange) != 0
}
// GetArg returns the first request of the given type, if possible.
func (ba *BatchRequest) GetArg(method Method) (Request, bool) {
// TODO(tschottdorf): when looking for EndTransaction, just look at the
// last entry.
for _, arg := range ba.Requests {
if req := arg.GetInner(); req.Method() == method {
return req, true
}
}
return nil, false
}
func (br *BatchResponse) String() string {
var str []string
for _, union := range br.Responses {
str = append(str, fmt.Sprintf("%T", union.GetInner()))
}
return strings.Join(str, ", ")
}
// First returns the first response of the given type, if possible.
func (br *BatchResponse) First() Response {
if len(br.Responses) > 0 {
return br.Responses[0].GetInner()
}
return nil
}
// Header returns a pointer to the header.
func (br *BatchResponse) Header() *BatchResponse_Header {
return &br.BatchResponse_Header
}
// GetIntents returns a slice of key pairs corresponding to transactional writes
// contained in the batch.
// TODO(tschottdorf): use roachpb.Span here instead of []Intent. Actually
// Intent should be Intents = {Txn, []Span} so that a []Span can
// be turned into Intents easily by just adding a Txn.
func (ba *BatchRequest) GetIntents() []Intent {
var intents []Intent
for _, arg := range ba.Requests {
req := arg.GetInner()
if !IsTransactionWrite(req) {
continue
}
h := req.Header()
intents = append(intents, Intent{Key: h.Key, EndKey: h.EndKey})
}
return intents
}
// ResetAll resets all the contained requests to their original state.
func (br *BatchResponse) ResetAll() {
if br == nil {
return
}
for _, rsp := range br.Responses {
// TODO(tschottdorf) `rsp.Reset()` isn't enough because rsp
// isn't a pointer.
rsp.GetInner().Reset()
}
}
// Combine implements the Combinable interface. It combines each slot of the
// given request into the corresponding slot of the base response. The number
// of slots must be equal and the respective slots must be combinable.
// On error, the receiver BatchResponse is in an invalid state.
// TODO(tschottdorf): write tests.
func (br *BatchResponse) Combine(otherBatch *BatchResponse) error {
if len(otherBatch.Responses) != len(br.Responses) {
return errors.New("unable to combine batch responses of different length")
}
for i, l := 0, len(br.Responses); i < l; i++ {
valLeft := br.Responses[i].GetInner()
valRight := otherBatch.Responses[i].GetInner()
args, lOK := valLeft.(Combinable)
reply, rOK := valRight.(Combinable)
if lOK && rOK {
if err := args.Combine(reply.(Response)); err != nil {
return err
}
continue
}
// If our slot is a NoopResponse, then whatever the other batch has is
// the result. Note that the result can still be a NoopResponse, to be
// filled in by a future Combine().
if _, ok := valLeft.(*NoopResponse); ok {
br.Responses[i] = otherBatch.Responses[i]
}
}
br.Timestamp.Forward(otherBatch.Timestamp)
br.Txn.Update(otherBatch.Txn)
return nil
}
// Add adds a request to the batch request.
func (ba *BatchRequest) Add(requests ...Request) {
for _, args := range requests {
union := RequestUnion{}
if !union.SetValue(args) {
panic(fmt.Sprintf("unable to add %T to batch request", args))
}
ba.Requests = append(ba.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)
}
// Methods returns a slice of the contained methods.
func (ba *BatchRequest) Methods() []Method {
var res []Method
for _, arg := range ba.Requests {
res = append(res, arg.GetInner().Method())
}
return res
}
// CreateReply implements the Request interface. It's slightly different from
// the other implementations: It creates replies for each of the contained
// requests, wrapped in a BatchResponse.
func (ba *BatchRequest) CreateReply() *BatchResponse {
br := &BatchResponse{}
for _, union := range ba.Requests {
br.Add(union.GetInner().CreateReply())
}
return br
}
func (ba *BatchRequest) flags() int {
var flags int
for _, union := range ba.Requests {
flags |= union.GetInner().flags()
}
return flags
}
// Split separates the requests contained in a batch so that each subset of
// requests can be executed by a Store (without changing order). In particular,
// Admin and EndTransaction requests are always singled out and mutating
// requests separated from reads.
func (ba BatchRequest) Split() [][]RequestUnion {
compatible := func(exFlags, newFlags int) bool {
// If no flags are set so far, everything goes.
if exFlags == 0 {
return true
}
if (newFlags & isAlone) != 0 {
return false
}
// Otherwise, the flags below must remain the same with the new
// request added.
//
// Note that we're not checking isRead: The invariants we're
// enforcing are that a batch can't mix non-writes with writes.
// Checking isRead would cause ConditionalPut and Put to conflict,
// which is not what we want.
const mask = isWrite | isAdmin | isReverse
return (mask & exFlags) == (mask & newFlags)
}
var parts [][]RequestUnion
for len(ba.Requests) > 0 {
part := ba.Requests
var gFlags int
for i, union := range ba.Requests {
flags := union.GetInner().flags()
if !compatible(gFlags, flags) {
part = ba.Requests[:i]
break
}
gFlags |= flags
}
parts = append(parts, part)
ba.Requests = ba.Requests[len(part):]
}
return parts
}
// String gives a brief summary of the contained requests and keys in the batch.
// TODO(tschottdorf): the key range is useful information, but requires `keys`.
// See #2198.
func (ba BatchRequest) String() string {
var str []string
for _, arg := range ba.Requests {
req := arg.GetInner()
h := req.Header()
str = append(str, fmt.Sprintf("%s [%s,%s)", req.Method(), h.Key, h.EndKey))
}
return strings.Join(str, ", ")
}
// Short gives gives a short version of the batch, printing the flags and
// timestamp only.
func (ba BatchRequest) Short() string {
return flagsToStr(ba.flags()) + "@" + ba.Timestamp.String()
}
// TraceID implements tracer.Traceable by returning the TraceID of the Transaction or,
// if not transactional, a digest of the batch.
func (ba BatchRequest) TraceID() string {
if r := ba.Txn.TraceID(); r != "" {
return r
}
return "c" + ba.Short()
}
// TraceName implements tracer.Traceable and behaves like TraceID, but using
// the TraceName of the object delegated to.
func (ba BatchRequest) TraceName() string {
if r := ba.Txn.TraceID(); r != "" {
return ba.Txn.TraceName()
}
return ba.Short()
}
// TODO(marc): we should assert
// var _ security.RequestWithUser = &BatchRequest{}
// here, but we need to break cycles first.
// GetUser implements security.RequestWithUser.
// KV messages are always sent by the node user.
func (*BatchRequest) GetUser() string {
// TODO(marc): we should use security.NodeUser here, but we need to break cycles first.
return "node"
}
// SetNewRequest increases the internal sequence counter of this batch request.
// The sequence counter is used for replay and reordering protection. At the
// Store, a sequence counter less than or equal to the last observed one incurs
// a transaction restart (if the request is transactional).
func (ba *BatchRequest) SetNewRequest() {
if ba.Txn == nil {
return
}
ba.Txn.Sequence++
}
// GoError returns the non-nil error from the proto.Error union.
func (br *BatchResponse) GoError() error {
return br.Error.GoError()
}
// SetGoError converts the specified type into either one of the proto-
// defined error types or into an Error for all other Go errors.
func (br *BatchResponse) SetGoError(err error) {
if err == nil {
br.Error = nil
return
}
br.Error = &Error{}
br.Error.SetGoError(err)
}