Skip to content

Commit b8f7a55

Browse files
committed
stateloader: rm trunc state from WriteInitialReplicaState
Epic: none Release note: none
1 parent 10405b5 commit b8f7a55

File tree

5 files changed

+59
-30
lines changed

5 files changed

+59
-30
lines changed

pkg/kv/kvserver/batcheval/cmd_end_transaction.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,11 +1460,14 @@ func splitTriggerHelper(
14601460
if err != nil {
14611461
return enginepb.MVCCStats{}, result.Result{}, errors.Wrap(err, "unable to load replica version")
14621462
}
1463-
*h.AbsPostSplitRight(), err = stateloader.WriteInitialReplicaState(
1463+
if *h.AbsPostSplitRight(), err = stateloader.WriteInitialReplicaState(
14641464
ctx, batch, *h.AbsPostSplitRight(), split.RightDesc, rightLease,
14651465
*gcThreshold, *gcHint, replicaVersion,
1466-
)
1467-
if err != nil {
1466+
); err != nil {
1467+
return enginepb.MVCCStats{}, result.Result{}, errors.Wrap(err, "unable to write initial Replica state")
1468+
}
1469+
// TODO(arulajmani): remove WriteInitialTruncState.
1470+
if err := stateloader.WriteInitialTruncState(ctx, batch, split.RightDesc.RangeID); err != nil {
14681471
return enginepb.MVCCStats{}, result.Result{}, errors.Wrap(err, "unable to write initial Replica state")
14691472
}
14701473
}

pkg/kv/kvserver/stateloader/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ go_library(
1212
"//pkg/kv/kvpb",
1313
"//pkg/kv/kvserver/kvserverpb",
1414
"//pkg/kv/kvserver/logstore",
15+
"//pkg/raft/raftpb",
1516
"//pkg/roachpb",
1617
"//pkg/storage",
1718
"//pkg/storage/enginepb",
@@ -34,7 +35,7 @@ go_test(
3435
embed = [":stateloader"],
3536
deps = [
3637
"//pkg/kv/kvpb",
37-
"//pkg/kv/kvserver/kvserverpb",
38+
"//pkg/kv/kvserver/logstore",
3839
"//pkg/kv/kvserver/print",
3940
"//pkg/raft/raftpb",
4041
"//pkg/roachpb",

pkg/kv/kvserver/stateloader/initial.go

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"context"
1010

1111
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvserverpb"
12+
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/logstore"
13+
"github.com/cockroachdb/cockroach/pkg/raft/raftpb"
1214
"github.com/cockroachdb/cockroach/pkg/roachpb"
1315
"github.com/cockroachdb/cockroach/pkg/storage"
1416
"github.com/cockroachdb/cockroach/pkg/storage/enginepb"
@@ -56,13 +58,9 @@ func WriteInitialReplicaState(
5658
gcHint roachpb.GCHint,
5759
replicaVersion roachpb.Version,
5860
) (enginepb.MVCCStats, error) {
59-
truncState := &kvserverpb.RaftTruncatedState{
60-
Term: RaftInitialLogTerm,
61-
Index: RaftInitialLogIndex,
62-
}
6361
s := kvserverpb.ReplicaState{
64-
RaftAppliedIndex: truncState.Index,
65-
RaftAppliedIndexTerm: truncState.Term,
62+
RaftAppliedIndex: RaftInitialLogIndex,
63+
RaftAppliedIndexTerm: RaftInitialLogTerm,
6664
LeaseAppliedIndex: InitialLeaseAppliedIndex,
6765
Desc: &roachpb.RangeDescriptor{
6866
RangeID: desc.RangeID,
@@ -101,11 +99,6 @@ func WriteInitialReplicaState(
10199
log.Fatalf(ctx, "expected trivial version, but found %+v", existingVersion)
102100
}
103101

104-
// TODO(sep-raft-log): SetRaftTruncatedState will be in a separate batch when
105-
// the Raft log engine is separated. Figure out the ordering required here.
106-
if err := rsl.SetRaftTruncatedState(ctx, readWriter, truncState); err != nil {
107-
return enginepb.MVCCStats{}, err
108-
}
109102
newMS, err := rsl.Save(ctx, readWriter, s)
110103
if err != nil {
111104
return enginepb.MVCCStats{}, err
@@ -114,6 +107,16 @@ func WriteInitialReplicaState(
114107
return newMS, nil
115108
}
116109

110+
// WriteInitialTruncState writes the initial RaftTruncatedState.
111+
// TODO(arulajmani): remove this.
112+
func WriteInitialTruncState(ctx context.Context, w storage.Writer, rangeID roachpb.RangeID) error {
113+
return logstore.NewStateLoader(rangeID).SetRaftTruncatedState(ctx, w,
114+
&kvserverpb.RaftTruncatedState{
115+
Index: RaftInitialLogIndex,
116+
Term: RaftInitialLogTerm,
117+
})
118+
}
119+
117120
// WriteInitialRangeState writes the initial range state. It's called during
118121
// bootstrap.
119122
func WriteInitialRangeState(
@@ -134,17 +137,35 @@ func WriteInitialRangeState(
134137
); err != nil {
135138
return err
136139
}
137-
138-
// TODO(sep-raft-log): when the log storage is separated, the below can't be
139-
// written in the same batch. Figure out the ordering required here.
140-
sl := Make(desc.RangeID)
141-
if err := sl.SynthesizeRaftState(ctx, readWriter); err != nil {
142-
return err
143-
}
144140
// Maintain the invariant that any replica (uninitialized or initialized),
145141
// with persistent state, has a RaftReplicaID.
146-
if err := sl.SetRaftReplicaID(ctx, readWriter, replicaID); err != nil {
142+
if err := Make(desc.RangeID).SetRaftReplicaID(ctx, readWriter, replicaID); err != nil {
147143
return err
148144
}
149-
return nil
145+
146+
// TODO(sep-raft-log): when the log storage is separated, raft state must be
147+
// written separately.
148+
return WriteInitialRaftState(ctx, readWriter, desc.RangeID)
149+
}
150+
151+
// WriteInitialRaftState writes raft state for an initialized replica created
152+
// during cluster bootstrap.
153+
func WriteInitialRaftState(
154+
ctx context.Context, writer storage.Writer, rangeID roachpb.RangeID,
155+
) error {
156+
sl := logstore.NewStateLoader(rangeID)
157+
// Initialize the HardState with the term and commit index matching the
158+
// initial applied state of the replica.
159+
if err := sl.SetHardState(ctx, writer, raftpb.HardState{
160+
Term: RaftInitialLogTerm,
161+
Commit: RaftInitialLogIndex,
162+
}); err != nil {
163+
return err
164+
}
165+
// The raft log is initialized empty, with the truncated state matching the
166+
// committed / applied initial state of the replica.
167+
return sl.SetRaftTruncatedState(ctx, writer, &kvserverpb.RaftTruncatedState{
168+
Index: RaftInitialLogIndex,
169+
Term: RaftInitialLogTerm,
170+
})
150171
}

pkg/kv/kvserver/stateloader/stateloader.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,10 +442,14 @@ func UninitializedReplicaState(rangeID roachpb.RangeID) kvserverpb.ReplicaState
442442

443443
// The rest is not technically part of ReplicaState.
444444

445-
// SynthesizeRaftState creates a Raft state which synthesizes both a HardState
446-
// and a lastIndex from pre-seeded data in the engine (typically created via
447-
// WriteInitialReplicaState and, on a split, perhaps the activity of an
448-
// uninitialized Raft group)
445+
// SynthesizeRaftState creates a Raft state which synthesizes HardState from
446+
// pre-seeded data in the engine: the state machine state created by
447+
// WriteInitialReplicaState on a split, and the existing HardState of an
448+
// uninitialized replica.
449+
//
450+
// TODO(sep-raft-log): this is now only used in splits, when initializing a
451+
// replica. Make the implementation straightforward, most of the stuff here is
452+
// constant except the existing HardState.
449453
func (rsl StateLoader) SynthesizeRaftState(
450454
ctx context.Context, readWriter storage.ReadWriter,
451455
) error {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
echo
22
----
3-
Put: 0,0 /Local/RangeID/5/u/RaftTruncatedState (0x01698d757266747400): index:10 term:5
43
Put: 0,0 /Local/RangeID/5/r/RangeLease (0x01698d72726c6c2d00): <empty>
54
Put: 0,0 /Local/RangeID/5/r/RangeGCThreshold (0x01698d726c67632d00): 0,0
65
Put: 0,0 /Local/RangeID/5/r/RangeGCHint (0x01698d727267636800): latest_range_delete_timestamp:<> gc_timestamp:<> gc_timestamp_next:<>
76
Put: 0,0 /Local/RangeID/5/r/RangeVersion (0x01698d727276657200): 10.2
87
Put: 0,0 /Local/RangeID/5/r/RangeAppliedState (0x01698d727261736b00): raft_applied_index:10 lease_applied_index:10 range_stats:<sys_bytes:142 sys_count:4 > raft_closed_timestamp:<> raft_applied_index_term:5
9-
Put: 0,0 /Local/RangeID/5/u/RaftHardState (0x01698d757266746800): term:5 vote:0 commit:10 lead:0 lead_epoch:0
108
Put: 0,0 /Local/RangeID/5/u/RaftReplicaID (0x01698d757266747200): replica_id:3
9+
Put: 0,0 /Local/RangeID/5/u/RaftHardState (0x01698d757266746800): term:5 vote:0 commit:10 lead:0 lead_epoch:0
10+
Put: 0,0 /Local/RangeID/5/u/RaftTruncatedState (0x01698d757266747400): index:10 term:5

0 commit comments

Comments
 (0)