Skip to content

Commit

Permalink
kvserver: rlock replica mu when reading follower state info
Browse files Browse the repository at this point in the history
Goroutines can mutate the Raft node state without acquiring raftMu,
however must acquire replica mu.

Acquire a read lock on replica mu when reading the tracker progress
state from the raw node in `FollowerStateRaftMuLocked` to prevent races.

Part of: #130187
Release note: None
  • Loading branch information
kvoli committed Sep 20, 2024
1 parent 8b17cd7 commit 754a1de
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions pkg/kv/kvserver/kvflowcontrol/replica_rac2/raft_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ import (
)

type raftNodeForRACv2 struct {
Replica
*raft.RawNode
}

// NewRaftNode creates a RaftNode implementation from the given RawNode.
func NewRaftNode(rn *raft.RawNode) RaftNode {
return raftNodeForRACv2{RawNode: rn}
func NewRaftNode(rn *raft.RawNode, r Replica) RaftNode {
return raftNodeForRACv2{RawNode: rn, Replica: r}
}

func (rn raftNodeForRACv2) TermLocked() uint64 {
Expand All @@ -47,6 +48,9 @@ func (rn raftNodeForRACv2) FollowerStateRaftMuLocked(
replicaID roachpb.ReplicaID,
) rac2.FollowerStateInfo {
// TODO(pav-kv): this is a temporary implementation.
rn.MuRLock()
defer rn.MuRUnlock()

status := rn.Status()
if progress, ok := status.Progress[raftpb.PeerID(replicaID)]; ok {
return rac2.FollowerStateInfo{
Expand Down
2 changes: 1 addition & 1 deletion pkg/kv/kvserver/replica_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func (r *Replica) initRaftGroupRaftMuLockedReplicaMuLocked() error {
return err
}
r.mu.internalRaftGroup = rg
r.flowControlV2.InitRaftLocked(ctx, replica_rac2.NewRaftNode(rg))
r.flowControlV2.InitRaftLocked(ctx, replica_rac2.NewRaftNode(rg, (*replicaForRACv2)(r)))
return nil
}

Expand Down

0 comments on commit 754a1de

Please sign in to comment.