-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathresult.go
374 lines (335 loc) · 12.4 KB
/
result.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
// Copyright 2014 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package result
import (
"context"
"fmt"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvserverpb"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/errors"
"github.com/kr/pretty"
)
// LocalResult is data belonging to an evaluated command that is
// only used on the node on which the command was proposed. Note that
// the proposing node may die before the local results are processed,
// so any side effects here are only best-effort.
type LocalResult struct {
Reply *roachpb.BatchResponse
// EncounteredIntents stores any intents from other transactions that the
// request encountered but did not conflict with. They should be handed off
// to asynchronous intent processing on the proposer, so that an attempt to
// resolve them is made.
EncounteredIntents []roachpb.Intent
// AcquiredLocks stores any newly acquired or re-acquired locks.
AcquiredLocks []roachpb.LockAcquisition
// ResolvedLocks stores any resolved lock spans, either with finalized or
// pending statuses. Unlike AcquiredLocks and EncounteredIntents, values in
// this slice will represent spans of locks that were resolved.
ResolvedLocks []roachpb.LockUpdate
// UpdatedTxns stores transaction records that have been updated by
// calls to EndTxn, PushTxn, and RecoverTxn.
UpdatedTxns []*roachpb.Transaction
// EndTxns stores completed transactions. If the transaction
// contains unresolved intents, they should be handed off for
// asynchronous intent resolution. A bool in each EndTxnIntents
// indicates whether or not the intents must be left alone if the
// corresponding command/proposal didn't succeed. For example,
// resolving intents of a committing txn should not happen if the
// commit fails, or we may accidentally make uncommitted values
// live.
EndTxns []EndTxnIntents
// When set (in which case we better be the first range), call
// GossipFirstRange if the Replica holds the lease.
GossipFirstRange bool
// Call MaybeGossipSystemConfig.
MaybeGossipSystemConfig bool
// Call MaybeGossipSystemConfigIfHaveFailure
MaybeGossipSystemConfigIfHaveFailure bool
// Call MaybeAddToSplitQueue.
MaybeAddToSplitQueue bool
// Call MaybeGossipNodeLiveness with the specified Span, if set.
MaybeGossipNodeLiveness *roachpb.Span
// FreezeStart indicates the high water mark timestamp beyond which the
// range is guaranteed to not have served any requests, if set call
// maybeWatchForMerge.
FreezeStart hlc.Timestamp
// Metrics contains counters which are to be passed to the
// metrics subsystem.
Metrics *Metrics
}
// IsZero reports whether lResult is the zero value.
func (lResult *LocalResult) IsZero() bool {
// NB: keep in order.
return lResult.Reply == nil &&
lResult.EncounteredIntents == nil &&
lResult.AcquiredLocks == nil &&
lResult.ResolvedLocks == nil &&
lResult.UpdatedTxns == nil &&
lResult.EndTxns == nil &&
!lResult.GossipFirstRange &&
!lResult.MaybeGossipSystemConfig &&
!lResult.MaybeGossipSystemConfigIfHaveFailure &&
lResult.MaybeGossipNodeLiveness == nil &&
lResult.FreezeStart.IsEmpty() &&
lResult.Metrics == nil
}
func (lResult *LocalResult) String() string {
if lResult == nil {
return "LocalResult: nil"
}
return fmt.Sprintf("LocalResult (reply: %v, "+
"#encountered intents: %d, #acquired locks: %d, #resolved locks: %d"+
"#updated txns: %d #end txns: %d, "+
"GossipFirstRange:%t MaybeGossipSystemConfig:%t "+
"MaybeGossipSystemConfigIfHaveFailure:%t MaybeAddToSplitQueue:%t "+
"MaybeGossipNodeLiveness:%s FreezeStart:%s",
lResult.Reply,
len(lResult.EncounteredIntents), len(lResult.AcquiredLocks), len(lResult.ResolvedLocks),
len(lResult.UpdatedTxns), len(lResult.EndTxns),
lResult.GossipFirstRange, lResult.MaybeGossipSystemConfig,
lResult.MaybeGossipSystemConfigIfHaveFailure, lResult.MaybeAddToSplitQueue,
lResult.MaybeGossipNodeLiveness, lResult.FreezeStart)
}
// DetachEncounteredIntents returns (and removes) those encountered
// intents from the LocalEvalResult which are supposed to be handled.
func (lResult *LocalResult) DetachEncounteredIntents() []roachpb.Intent {
if lResult == nil {
return nil
}
r := lResult.EncounteredIntents
lResult.EncounteredIntents = nil
return r
}
// DetachEndTxns returns (and removes) the EndTxnIntent objects from
// the local result. If alwaysOnly is true, the slice is filtered to
// include only those which have specified returnAlways=true, meaning
// the intents should be resolved regardless of whether the
// EndTxn command succeeded.
func (lResult *LocalResult) DetachEndTxns(alwaysOnly bool) []EndTxnIntents {
if lResult == nil {
return nil
}
r := lResult.EndTxns
if alwaysOnly {
// If alwaysOnly, filter away any !Always EndTxnIntents.
r = r[:0]
for _, eti := range lResult.EndTxns {
if eti.Always {
r = append(r, eti)
}
}
}
lResult.EndTxns = nil
return r
}
// Result is the result of evaluating a KV request. That is, the
// proposer (which holds the lease, at least in the case in which the command
// will complete successfully) has evaluated the request and is holding on to:
//
// a) changes to be written to disk when applying the command
// b) changes to the state which may require special handling (i.e. code
// execution) on all Replicas
// c) data which isn't sent to the followers but the proposer needs for tasks
// it must run when the command has applied (such as resolving intents).
type Result struct {
Local LocalResult
Replicated kvserverpb.ReplicatedEvalResult
WriteBatch *kvserverpb.WriteBatch
LogicalOpLog *kvserverpb.LogicalOpLog
}
// IsZero reports whether p is the zero value.
func (p *Result) IsZero() bool {
if !p.Local.IsZero() {
return false
}
if !p.Replicated.Equal(kvserverpb.ReplicatedEvalResult{}) {
return false
}
if p.WriteBatch != nil {
return false
}
if p.LogicalOpLog != nil {
return false
}
return true
}
// coalesceBool ORs rhs into lhs and then zeroes rhs.
func coalesceBool(lhs *bool, rhs *bool) {
*lhs = *lhs || *rhs
*rhs = false
}
// MergeAndDestroy absorbs the supplied EvalResult while validating that the
// resulting EvalResult makes sense. For example, it is forbidden to absorb
// two lease updates or log truncations, or multiple splits and/or merges.
//
// The passed EvalResult must not be used once passed to Merge.
func (p *Result) MergeAndDestroy(q Result) error {
if q.Replicated.State != nil {
if q.Replicated.State.RaftAppliedIndex != 0 {
return errors.New("must not specify RaftApplyIndex")
}
if q.Replicated.State.LeaseAppliedIndex != 0 {
return errors.New("must not specify RaftApplyIndex")
}
if p.Replicated.State == nil {
p.Replicated.State = &kvserverpb.ReplicaState{}
}
if p.Replicated.State.Desc == nil {
p.Replicated.State.Desc = q.Replicated.State.Desc
} else if q.Replicated.State.Desc != nil {
return errors.New("conflicting RangeDescriptor")
}
q.Replicated.State.Desc = nil
if p.Replicated.State.Lease == nil {
p.Replicated.State.Lease = q.Replicated.State.Lease
} else if q.Replicated.State.Lease != nil {
return errors.New("conflicting Lease")
}
q.Replicated.State.Lease = nil
if p.Replicated.State.TruncatedState == nil {
p.Replicated.State.TruncatedState = q.Replicated.State.TruncatedState
} else if q.Replicated.State.TruncatedState != nil {
return errors.New("conflicting TruncatedState")
}
q.Replicated.State.TruncatedState = nil
if q.Replicated.State.GCThreshold != nil {
if p.Replicated.State.GCThreshold == nil {
p.Replicated.State.GCThreshold = q.Replicated.State.GCThreshold
} else {
p.Replicated.State.GCThreshold.Forward(*q.Replicated.State.GCThreshold)
}
q.Replicated.State.GCThreshold = nil
}
if q.Replicated.State.Stats != nil {
return errors.New("must not specify Stats")
}
if (*q.Replicated.State != kvserverpb.ReplicaState{}) {
log.Fatalf(context.TODO(), "unhandled EvalResult: %s",
pretty.Diff(*q.Replicated.State, kvserverpb.ReplicaState{}))
}
q.Replicated.State = nil
}
if p.Replicated.Split == nil {
p.Replicated.Split = q.Replicated.Split
} else if q.Replicated.Split != nil {
return errors.New("conflicting Split")
}
q.Replicated.Split = nil
if p.Replicated.Merge == nil {
p.Replicated.Merge = q.Replicated.Merge
} else if q.Replicated.Merge != nil {
return errors.New("conflicting Merge")
}
q.Replicated.Merge = nil
if p.Replicated.ChangeReplicas == nil {
p.Replicated.ChangeReplicas = q.Replicated.ChangeReplicas
} else if q.Replicated.ChangeReplicas != nil {
return errors.New("conflicting ChangeReplicas")
}
q.Replicated.ChangeReplicas = nil
if p.Replicated.ComputeChecksum == nil {
p.Replicated.ComputeChecksum = q.Replicated.ComputeChecksum
} else if q.Replicated.ComputeChecksum != nil {
return errors.New("conflicting ComputeChecksum")
}
q.Replicated.ComputeChecksum = nil
if p.Replicated.RaftLogDelta == 0 {
p.Replicated.RaftLogDelta = q.Replicated.RaftLogDelta
} else if q.Replicated.RaftLogDelta != 0 {
return errors.New("conflicting RaftLogDelta")
}
q.Replicated.RaftLogDelta = 0
if p.Replicated.AddSSTable == nil {
p.Replicated.AddSSTable = q.Replicated.AddSSTable
} else if q.Replicated.AddSSTable != nil {
return errors.New("conflicting AddSSTable")
}
q.Replicated.AddSSTable = nil
if q.Replicated.SuggestedCompactions != nil {
if p.Replicated.SuggestedCompactions == nil {
p.Replicated.SuggestedCompactions = q.Replicated.SuggestedCompactions
} else {
p.Replicated.SuggestedCompactions = append(p.Replicated.SuggestedCompactions, q.Replicated.SuggestedCompactions...)
}
}
q.Replicated.SuggestedCompactions = nil
if p.Replicated.PrevLeaseProposal == nil {
p.Replicated.PrevLeaseProposal = q.Replicated.PrevLeaseProposal
} else if q.Replicated.PrevLeaseProposal != nil {
return errors.New("conflicting lease expiration")
}
q.Replicated.PrevLeaseProposal = nil
if p.Local.EncounteredIntents == nil {
p.Local.EncounteredIntents = q.Local.EncounteredIntents
} else {
p.Local.EncounteredIntents = append(p.Local.EncounteredIntents, q.Local.EncounteredIntents...)
}
q.Local.EncounteredIntents = nil
if p.Local.AcquiredLocks == nil {
p.Local.AcquiredLocks = q.Local.AcquiredLocks
} else {
p.Local.AcquiredLocks = append(p.Local.AcquiredLocks, q.Local.AcquiredLocks...)
}
q.Local.AcquiredLocks = nil
if p.Local.ResolvedLocks == nil {
p.Local.ResolvedLocks = q.Local.ResolvedLocks
} else {
p.Local.ResolvedLocks = append(p.Local.ResolvedLocks, q.Local.ResolvedLocks...)
}
q.Local.ResolvedLocks = nil
if p.Local.UpdatedTxns == nil {
p.Local.UpdatedTxns = q.Local.UpdatedTxns
} else {
p.Local.UpdatedTxns = append(p.Local.UpdatedTxns, q.Local.UpdatedTxns...)
}
q.Local.UpdatedTxns = nil
if p.Local.EndTxns == nil {
p.Local.EndTxns = q.Local.EndTxns
} else {
p.Local.EndTxns = append(p.Local.EndTxns, q.Local.EndTxns...)
}
q.Local.EndTxns = nil
if p.Local.MaybeGossipNodeLiveness == nil {
p.Local.MaybeGossipNodeLiveness = q.Local.MaybeGossipNodeLiveness
} else if q.Local.MaybeGossipNodeLiveness != nil {
return errors.New("conflicting MaybeGossipNodeLiveness")
}
q.Local.MaybeGossipNodeLiveness = nil
if p.Local.FreezeStart.IsEmpty() {
p.Local.FreezeStart = q.Local.FreezeStart
} else if !q.Local.FreezeStart.IsEmpty() {
return errors.New("conflicting FreezeStart")
}
q.Local.FreezeStart = hlc.Timestamp{}
coalesceBool(&p.Local.GossipFirstRange, &q.Local.GossipFirstRange)
coalesceBool(&p.Local.MaybeGossipSystemConfig, &q.Local.MaybeGossipSystemConfig)
coalesceBool(&p.Local.MaybeGossipSystemConfigIfHaveFailure, &q.Local.MaybeGossipSystemConfigIfHaveFailure)
coalesceBool(&p.Local.MaybeAddToSplitQueue, &q.Local.MaybeAddToSplitQueue)
if p.Local.Metrics == nil {
p.Local.Metrics = q.Local.Metrics
} else if q.Local.Metrics != nil {
p.Local.Metrics.Add(*q.Local.Metrics)
}
q.Local.Metrics = nil
if q.LogicalOpLog != nil {
if p.LogicalOpLog == nil {
p.LogicalOpLog = q.LogicalOpLog
} else {
p.LogicalOpLog.Ops = append(p.LogicalOpLog.Ops, q.LogicalOpLog.Ops...)
}
}
q.LogicalOpLog = nil
if !q.IsZero() {
log.Fatalf(context.TODO(), "unhandled EvalResult: %s", pretty.Diff(q, Result{}))
}
return nil
}