9
9
"context"
10
10
11
11
"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"
12
14
"github.com/cockroachdb/cockroach/pkg/roachpb"
13
15
"github.com/cockroachdb/cockroach/pkg/storage"
14
16
"github.com/cockroachdb/cockroach/pkg/storage/enginepb"
@@ -56,13 +58,9 @@ func WriteInitialReplicaState(
56
58
gcHint roachpb.GCHint ,
57
59
replicaVersion roachpb.Version ,
58
60
) (enginepb.MVCCStats , error ) {
59
- truncState := & kvserverpb.RaftTruncatedState {
60
- Term : RaftInitialLogTerm ,
61
- Index : RaftInitialLogIndex ,
62
- }
63
61
s := kvserverpb.ReplicaState {
64
- RaftAppliedIndex : truncState . Index ,
65
- RaftAppliedIndexTerm : truncState . Term ,
62
+ RaftAppliedIndex : RaftInitialLogIndex ,
63
+ RaftAppliedIndexTerm : RaftInitialLogTerm ,
66
64
LeaseAppliedIndex : InitialLeaseAppliedIndex ,
67
65
Desc : & roachpb.RangeDescriptor {
68
66
RangeID : desc .RangeID ,
@@ -101,11 +99,6 @@ func WriteInitialReplicaState(
101
99
log .Fatalf (ctx , "expected trivial version, but found %+v" , existingVersion )
102
100
}
103
101
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
- }
109
102
newMS , err := rsl .Save (ctx , readWriter , s )
110
103
if err != nil {
111
104
return enginepb.MVCCStats {}, err
@@ -114,6 +107,16 @@ func WriteInitialReplicaState(
114
107
return newMS , nil
115
108
}
116
109
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
+
117
120
// WriteInitialRangeState writes the initial range state. It's called during
118
121
// bootstrap.
119
122
func WriteInitialRangeState (
@@ -134,17 +137,35 @@ func WriteInitialRangeState(
134
137
); err != nil {
135
138
return err
136
139
}
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
- }
144
140
// Maintain the invariant that any replica (uninitialized or initialized),
145
141
// 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 {
147
143
return err
148
144
}
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
+ })
150
171
}
0 commit comments