-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathreplica_state.go
410 lines (370 loc) · 13.5 KB
/
replica_state.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
// 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: Tobias Schottdorf ([email protected])
package storage
import (
"github.com/cockroachdb/cockroach/keys"
"github.com/cockroachdb/cockroach/roachpb"
"github.com/cockroachdb/cockroach/storage/engine"
"github.com/cockroachdb/cockroach/storage/engine/enginepb"
"github.com/cockroachdb/cockroach/storage/storagebase"
"github.com/cockroachdb/cockroach/util/hlc"
"github.com/cockroachdb/cockroach/util/protoutil"
"github.com/coreos/etcd/raft/raftpb"
"github.com/pkg/errors"
"golang.org/x/net/context"
)
// loadState loads a ReplicaState from disk. The exception is the Desc field,
// which is updated transactionally, and is populated from the supplied
// RangeDescriptor under the convention that that is the latest committed
// version.
func loadState(
reader engine.Reader, desc *roachpb.RangeDescriptor,
) (storagebase.ReplicaState, error) {
var s storagebase.ReplicaState
// TODO(tschottdorf): figure out whether this is always synchronous with
// on-disk state (likely iffy during Split/ChangeReplica triggers).
s.Desc = protoutil.Clone(desc).(*roachpb.RangeDescriptor)
// Read the leader lease.
var err error
if s.Lease, err = loadLease(reader, desc.RangeID); err != nil {
return storagebase.ReplicaState{}, err
}
if s.Frozen, err = loadFrozenStatus(reader, desc.RangeID); err != nil {
return storagebase.ReplicaState{}, err
}
if s.GCThreshold, err = loadGCThreshold(reader, desc.RangeID); err != nil {
return storagebase.ReplicaState{}, err
}
if s.RaftAppliedIndex, s.LeaseAppliedIndex, err = loadAppliedIndex(
reader,
desc.RangeID,
); err != nil {
return storagebase.ReplicaState{}, err
}
if s.Stats, err = loadMVCCStats(reader, desc.RangeID); err != nil {
return storagebase.ReplicaState{}, err
}
// The truncated state should not be optional (i.e. the pointer is
// pointless), but it is and the migration is not worth it.
truncState, err := loadTruncatedState(reader, desc.RangeID)
if err != nil {
return storagebase.ReplicaState{}, err
}
s.TruncatedState = &truncState
return s, nil
}
// saveState persists the given ReplicaState to disk. It assumes that the
// contained Stats are up-to-date and returns the stats which result from
// writing the updated State.
// As an exception to the rule, the Desc field (whose on-disk state is special
// in that it's a full MVCC value and updated transactionally) is only used for
// its RangeID.
//
// TODO(tschottdorf): consolidate direct permutation and persistence of
// state throughout the Raft path in favor of a more organized approach.
func saveState(
eng engine.ReadWriter, state storagebase.ReplicaState,
) (enginepb.MVCCStats, error) {
ms, rangeID := &state.Stats, state.Desc.RangeID
if err := setLease(eng, ms, rangeID, state.Lease); err != nil {
return enginepb.MVCCStats{}, err
}
if err := setAppliedIndex(
eng, ms, rangeID, state.RaftAppliedIndex, state.LeaseAppliedIndex,
); err != nil {
return enginepb.MVCCStats{}, err
}
if err := setFrozenStatus(eng, ms, rangeID, state.Frozen); err != nil {
return enginepb.MVCCStats{}, err
}
if err := setGCThreshold(eng, ms, rangeID, &state.GCThreshold); err != nil {
return enginepb.MVCCStats{}, err
}
if err := setTruncatedState(eng, ms, rangeID, *state.TruncatedState); err != nil {
return enginepb.MVCCStats{}, err
}
if err := setMVCCStats(eng, rangeID, state.Stats); err != nil {
return enginepb.MVCCStats{}, err
}
return state.Stats, nil
}
func loadLease(reader engine.Reader, rangeID roachpb.RangeID) (*roachpb.Lease, error) {
lease := &roachpb.Lease{}
_, err := engine.MVCCGetProto(context.Background(), reader,
keys.RangeLeaderLeaseKey(rangeID), hlc.ZeroTimestamp,
true, nil, lease)
if err != nil {
return nil, err
}
return lease, nil
}
func setLease(
eng engine.ReadWriter,
ms *enginepb.MVCCStats,
rangeID roachpb.RangeID,
lease *roachpb.Lease, // TODO(tschottdorf): better if this is never nil
) error {
if lease == nil {
return nil
}
return engine.MVCCPutProto(
context.Background(), eng, ms,
keys.RangeLeaderLeaseKey(rangeID),
hlc.ZeroTimestamp, nil, lease)
}
func loadAppliedIndex(reader engine.Reader, rangeID roachpb.RangeID) (uint64, uint64, error) {
var appliedIndex uint64
v, _, err := engine.MVCCGet(context.Background(), reader, keys.RaftAppliedIndexKey(rangeID),
hlc.ZeroTimestamp, true, nil)
if err != nil {
return 0, 0, err
}
if v != nil {
int64AppliedIndex, err := v.GetInt()
if err != nil {
return 0, 0, err
}
appliedIndex = uint64(int64AppliedIndex)
}
// TODO(tschottdorf): code duplication.
var leaseAppliedIndex uint64
v, _, err = engine.MVCCGet(context.Background(), reader, keys.LeaseAppliedIndexKey(rangeID),
hlc.ZeroTimestamp, true, nil)
if err != nil {
return 0, 0, err
}
if v != nil {
int64LeaseAppliedIndex, err := v.GetInt()
if err != nil {
return 0, 0, err
}
leaseAppliedIndex = uint64(int64LeaseAppliedIndex)
}
return appliedIndex, leaseAppliedIndex, nil
}
func setAppliedIndex(eng engine.ReadWriter, ms *enginepb.MVCCStats, rangeID roachpb.RangeID, appliedIndex, leaseAppliedIndex uint64) error {
var value roachpb.Value
value.SetInt(int64(appliedIndex))
if err := engine.MVCCPut(context.Background(), eng, ms,
keys.RaftAppliedIndexKey(rangeID),
hlc.ZeroTimestamp,
value,
nil /* txn */); err != nil {
return err
}
value.SetInt(int64(leaseAppliedIndex))
return engine.MVCCPut(context.Background(), eng, ms,
keys.LeaseAppliedIndexKey(rangeID),
hlc.ZeroTimestamp,
value,
nil /* txn */)
}
func loadTruncatedState(
reader engine.Reader, rangeID roachpb.RangeID,
) (roachpb.RaftTruncatedState, error) {
var truncState roachpb.RaftTruncatedState
if _, err := engine.MVCCGetProto(context.Background(), reader,
keys.RaftTruncatedStateKey(rangeID), hlc.ZeroTimestamp, true,
nil, &truncState); err != nil {
return roachpb.RaftTruncatedState{}, err
}
return truncState, nil
}
func setTruncatedState(
eng engine.ReadWriter,
ms *enginepb.MVCCStats,
rangeID roachpb.RangeID,
truncState roachpb.RaftTruncatedState,
) error {
return engine.MVCCPutProto(context.Background(), eng, ms,
keys.RaftTruncatedStateKey(rangeID), hlc.ZeroTimestamp, nil, &truncState)
}
func loadGCThreshold(reader engine.Reader, rangeID roachpb.RangeID) (hlc.Timestamp, error) {
var t hlc.Timestamp
_, err := engine.MVCCGetProto(context.Background(), reader, keys.RangeLastGCKey(rangeID),
hlc.ZeroTimestamp, true, nil, &t)
return t, err
}
func setGCThreshold(
eng engine.ReadWriter, ms *enginepb.MVCCStats, rangeID roachpb.RangeID, threshold *hlc.Timestamp,
) error {
return engine.MVCCPutProto(context.Background(), eng, ms,
keys.RangeLastGCKey(rangeID), hlc.ZeroTimestamp, nil, threshold)
}
func loadMVCCStats(reader engine.Reader, rangeID roachpb.RangeID) (enginepb.MVCCStats, error) {
var ms enginepb.MVCCStats
if err := engine.MVCCGetRangeStats(context.Background(), reader, rangeID, &ms); err != nil {
return enginepb.MVCCStats{}, err
}
return ms, nil
}
func setMVCCStats(eng engine.ReadWriter, rangeID roachpb.RangeID, newMS enginepb.MVCCStats) error {
return engine.MVCCSetRangeStats(context.Background(), eng, rangeID, &newMS)
}
func setFrozenStatus(
eng engine.ReadWriter, ms *enginepb.MVCCStats, rangeID roachpb.RangeID, frozen bool,
) error {
var val roachpb.Value
val.SetBool(frozen)
return engine.MVCCPut(context.Background(), eng, ms,
keys.RangeFrozenStatusKey(rangeID), hlc.ZeroTimestamp, val, nil)
}
func loadFrozenStatus(reader engine.Reader, rangeID roachpb.RangeID) (bool, error) {
val, _, err := engine.MVCCGet(context.Background(), reader, keys.RangeFrozenStatusKey(rangeID),
hlc.ZeroTimestamp, true, nil)
if err != nil {
return false, err
}
if val == nil {
return false, nil
}
return val.GetBool()
}
// The rest is not technically part of ReplicaState.
// TODO(tschottdorf): more consolidation of ad-hoc structures: last index and
// hard state. These are closely coupled with ReplicaState (and in particular
// with its TruncatedState) but are different in that they are not consistently
// updated through Raft.
func loadLastIndex(reader engine.Reader, rangeID roachpb.RangeID) (uint64, error) {
lastIndex := uint64(0)
v, _, err := engine.MVCCGet(context.Background(), reader,
keys.RaftLastIndexKey(rangeID),
hlc.ZeroTimestamp, true /* consistent */, nil)
if err != nil {
return 0, err
}
if v != nil {
int64LastIndex, err := v.GetInt()
if err != nil {
return 0, err
}
lastIndex = uint64(int64LastIndex)
} else {
// The log is empty, which means we are either starting from scratch
// or the entire log has been truncated away.
lastEnt, err := raftTruncatedState(reader, rangeID)
if err != nil {
return 0, err
}
lastIndex = lastEnt.Index
}
return lastIndex, nil
}
func setLastIndex(eng engine.ReadWriter, rangeID roachpb.RangeID, lastIndex uint64) error {
var value roachpb.Value
value.SetInt(int64(lastIndex))
return engine.MVCCPut(context.Background(), eng, nil, keys.RaftLastIndexKey(rangeID),
hlc.ZeroTimestamp,
value,
nil /* txn */)
}
func loadHardState(
reader engine.Reader, rangeID roachpb.RangeID,
) (raftpb.HardState, error) {
var hs raftpb.HardState
found, err := engine.MVCCGetProto(context.Background(), reader,
keys.RaftHardStateKey(rangeID), hlc.ZeroTimestamp, true, nil, &hs)
if !found || err != nil {
return raftpb.HardState{}, err
}
return hs, nil
}
func setHardState(
batch engine.ReadWriter, rangeID roachpb.RangeID, st raftpb.HardState,
) error {
return engine.MVCCPutProto(context.Background(), batch, nil,
keys.RaftHardStateKey(rangeID),
hlc.ZeroTimestamp, nil, &st)
}
// synthesizeHardState synthesizes a HardState from the given ReplicaState and
// any existing on-disk HardState in the context of a snapshot, while verifying
// that the application of the snapshot does not violate Raft invariants.
//
// The HardState is Raft's view of term and acknowledged log entries, and so we
// must be very careful touching it manually. Normally, Raft itself produces
// valid HardStates, but we don't have that luxury during preemptive snapshots.
// In particular, the snapshot we're applying might hit upon some previous
// state. For example, a preemptive snapshot might arrive late, after the Range
// has already been created and caught up by vanilla Raft. In that case it's
// very likely that we must _refuse_ the snapshot at this point since it would
// otherwise violate Raft invariants. On the other hand, when the existing
// HardState is simply a replica which hasn't ever received any log entries but
// only managed to cast a vote or take a note of a Term compatible with the
// snapshot, we want to apply it - this is the case during splits, when the
// right-hand side of a split can be created early by other Replicas' messages
// to it, but which is unable to make any progress (since it would need to
// receive a snapshot, which we prevent due to overlap with the still-existing
// left-hand side).
func synthesizeHardState(eng engine.ReadWriter, s storagebase.ReplicaState) error {
oldHS, err := loadHardState(eng, s.Desc.RangeID)
if err != nil {
return err
}
newHS := raftpb.HardState{
Term: s.TruncatedState.Term,
// Note that when applying a Raft snapshot, the applied index is
// equal to the Commit index represented by the snapshot.
Commit: s.RaftAppliedIndex,
}
if oldHS.Commit > newHS.Commit {
return errors.Errorf("can't decrease HardState.Commit from %d to %d",
oldHS.Commit, newHS.Commit)
}
if oldHS.Term > newHS.Term {
// The existing HardState is allowed to be ahead of us (and in fact we
// need this). We already checked above that we're not rewinding the
// acknowledged index, and we haven't updated votes yet.
newHS.Term = oldHS.Term
}
// If the existing HardState voted, remember that.
if oldHS.Term == newHS.Term {
newHS.Vote = oldHS.Vote
}
return errors.Wrapf(setHardState(eng, s.Desc.RangeID, newHS), "writing HardState %+v", &newHS)
}
// writeInitialState bootstraps a new Raft group (i.e. it is called when we
// bootstrap a Range, or when setting up the right hand side of a split).
// Its main task is to persist a consistent Raft (and associated Replica) state
// which does not start from zero but presupposes a few entries already having
// applied.
// The supplied MVCCStats are used for the Stats field after adjusting for
// persisting the state itself, and the updated stats are returned.
func writeInitialState(
eng engine.ReadWriter, ms enginepb.MVCCStats, desc roachpb.RangeDescriptor,
) (enginepb.MVCCStats, error) {
rangeID := desc.RangeID
var s storagebase.ReplicaState
s.TruncatedState = &roachpb.RaftTruncatedState{
Term: raftInitialLogTerm,
Index: raftInitialLogIndex,
}
s.RaftAppliedIndex = s.TruncatedState.Index
s.Desc = &roachpb.RangeDescriptor{
RangeID: rangeID,
}
s.Stats = ms
newMS, err := saveState(eng, s)
if err != nil {
return enginepb.MVCCStats{}, err
}
if err := synthesizeHardState(eng, s); err != nil {
return enginepb.MVCCStats{}, err
}
if err := setLastIndex(eng, rangeID, s.TruncatedState.Index); err != nil {
return enginepb.MVCCStats{}, err
}
return newMS, nil
}