Skip to content

Commit bc9c804

Browse files
craig[bot]pav-kv
andcommitted
Merge #152820
152820: kvserver: clarify apply batch zeroing behaviour r=tbg a=pav-kv Epic: none Release note: none Co-authored-by: Pavel Kalinnikov <[email protected]>
2 parents ea6772d + 2735485 commit bc9c804

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

pkg/kv/kvserver/apply/task.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ type StateMachine interface {
3030
// AckCommittedEntriesBeforeApplication.
3131
//
3232
// There must only be a single EphemeralBatch *or* Batch open at any given
33-
// point in time.
33+
// point in time. The caller must Close the batch to clear resources.
3434
NewEphemeralBatch() EphemeralBatch
3535
// NewBatch creates a new batch that is suitable for accumulating the effects
3636
// that a group of Commands will have on the replicated state machine.
3737
// Commands are staged in the batch one-by-one and then the entire batch is
3838
// applied to the StateMachine at once via its ApplyToStateMachine method.
3939
//
4040
// There must only be a single EphemeralBatch *or* Batch open at any given
41-
// point in time.
41+
// point in time. The caller must Close the batch to clear resources.
4242
NewBatch() Batch
4343
// ApplySideEffects applies the in-memory side-effects of a Command to
4444
// the replicated state machine. The method will be called in the order

pkg/kv/kvserver/replica_application_state_machine.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@ type applyCommittedEntriesStats struct {
5757
// side-effects of each command is applied to the Replica's in-memory state.
5858
type replicaStateMachine struct {
5959
r *Replica
60-
// batch is returned from NewBatch.
60+
// batch is returned from NewBatch. It is non-zero between NewBatch() and the
61+
// corresponding Close() call.
6162
batch replicaAppBatch
62-
// ephemeralBatch is returned from NewEphemeralBatch.
63+
// ephemeralBatch is returned from NewEphemeralBatch. It is non-zero between
64+
// NewEphemeralBatch() and the corresponding Close() call.
6365
ephemeralBatch ephemeralReplicaAppBatch
6466
// stats are updated during command application and reset by moveStats.
6567
applyStats applyCommittedEntriesStats
@@ -128,6 +130,8 @@ func replicaApplyTestingFilters(
128130
func (sm *replicaStateMachine) NewEphemeralBatch() apply.EphemeralBatch {
129131
r := sm.r
130132
mb := &sm.ephemeralBatch
133+
// NB: the batch struct is zero-initialized, which is guaranteed by the fact
134+
// that its previous use ended with ephemeralReplicaAppBatch.Close().
131135
mb.r = r
132136
r.raftMu.AssertHeld()
133137
mb.state = r.shMu.state
@@ -138,9 +142,8 @@ func (sm *replicaStateMachine) NewEphemeralBatch() apply.EphemeralBatch {
138142
func (sm *replicaStateMachine) NewBatch() apply.Batch {
139143
r := sm.r
140144
b := &sm.batch
141-
// TODO(pav-kv): replicaAppBatch initialization below is bug-prone, we need to
142-
// not forget resetting the fields that are local to one batch. Find a way to
143-
// make it safer.
145+
// NB: the batch struct is zero-initialized, which is guaranteed by the fact
146+
// that its previous use ended with replicaAppBatch.Close().
144147
b.r = r
145148
b.applyStats = &sm.applyStats
146149
// TODO(#144627): most commands do not need to read. Use NewWriteBatch because
@@ -154,9 +157,6 @@ func (sm *replicaStateMachine) NewBatch() apply.Batch {
154157
*b.state.Stats = *r.shMu.state.Stats
155158
b.closedTimestampSetter = r.mu.closedTimestampSetter
156159
r.mu.RUnlock()
157-
b.changeRemovesReplica = false
158-
b.changeTruncatesSideloadedFiles = false
159-
// TODO(pav-kv): what about b.ab and b.followerStoreWriteBytes?
160160
b.start = timeutil.Now()
161161
return b
162162
}

0 commit comments

Comments
 (0)