Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added in memory based raft tests. Updated some raft comments. #5515

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions server/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,25 +156,27 @@ type raft struct {
llqrt time.Time // Last quorum lost time
lsut time.Time // Last scale-up time

term uint64 // The current vote term
pterm uint64 // Previous term from the last snapshot
pindex uint64 // Previous index from the last snapshot
commit uint64 // Sequence number of the most recent commit
applied uint64 // Sequence number of the most recently applied commit
hcbehind bool // Were we falling behind at the last health check? (see: isCurrent)
term uint64 // The current vote term
pterm uint64 // Previous term from the last snapshot
pindex uint64 // Previous index from the last snapshot
commit uint64 // Index of the most recent commit
applied uint64 // Index of the most recently applied commit

leader string // The ID of the leader
vote string // Our current vote state
lxfer bool // Are we doing a leadership transfer?

hcbehind bool // Were we falling behind at the last health check? (see: isCurrent)

s *Server // Reference to top-level server
c *client // Internal client for subscriptions
js *jetStream // JetStream, if running, to see if we are out of resources

dflag bool // Debug flag
pleader bool // Has the group ever had a leader?
observer bool // The node is observing, i.e. not participating in voting
extSt extensionState // Extension state
dflag bool // Debug flag
pleader bool // Has the group ever had a leader?
observer bool // The node is observing, i.e. not participating in voting

extSt extensionState // Extension state

psubj string // Proposals subject
rpsubj string // Remove peers subject
Expand Down
45 changes: 34 additions & 11 deletions server/raft_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,26 @@ func (sg smGroup) unlockAll() {
}

// Create a raft group and place on numMembers servers at random.
// Filestore based.
func (c *cluster) createRaftGroup(name string, numMembers int, smf smFactory) smGroup {
return c.createRaftGroupEx(name, numMembers, smf, FileStorage)
}

func (c *cluster) createMemRaftGroup(name string, numMembers int, smf smFactory) smGroup {
return c.createRaftGroupEx(name, numMembers, smf, MemoryStorage)
}

func (c *cluster) createRaftGroupEx(name string, numMembers int, smf smFactory, st StorageType) smGroup {
c.t.Helper()
if numMembers > len(c.servers) {
c.t.Fatalf("Members > Peers: %d vs %d", numMembers, len(c.servers))
}
servers := append([]*Server{}, c.servers...)
rand.Shuffle(len(servers), func(i, j int) { servers[i], servers[j] = servers[j], servers[i] })
return c.createRaftGroupWithPeers(name, servers[:numMembers], smf)
return c.createRaftGroupWithPeers(name, servers[:numMembers], smf, st)
}

func (c *cluster) createRaftGroupWithPeers(name string, servers []*Server, smf smFactory) smGroup {
func (c *cluster) createRaftGroupWithPeers(name string, servers []*Server, smf smFactory, st StorageType) smGroup {
c.t.Helper()

var sg smGroup
Expand All @@ -122,12 +131,19 @@ func (c *cluster) createRaftGroupWithPeers(name string, servers []*Server, smf s
}

for _, s := range servers {
fs, err := newFileStore(
FileStoreConfig{StoreDir: c.t.TempDir(), BlockSize: defaultMediumBlockSize, AsyncFlush: false, SyncInterval: 5 * time.Minute},
StreamConfig{Name: name, Storage: FileStorage},
)
require_NoError(c.t, err)
cfg := &RaftConfig{Name: name, Store: c.t.TempDir(), Log: fs}
var cfg *RaftConfig
if st == FileStorage {
fs, err := newFileStore(
FileStoreConfig{StoreDir: c.t.TempDir(), BlockSize: defaultMediumBlockSize, AsyncFlush: false, SyncInterval: 5 * time.Minute},
StreamConfig{Name: name, Storage: FileStorage},
)
require_NoError(c.t, err)
cfg = &RaftConfig{Name: name, Store: c.t.TempDir(), Log: fs}
} else {
ms, err := newMemStore(&StreamConfig{Name: name, Storage: MemoryStorage})
require_NoError(c.t, err)
cfg = &RaftConfig{Name: name, Store: c.t.TempDir(), Log: ms}
}
s.bootstrapRaftNode(cfg, peers, true)
n, err := s.startRaftNode(globalAccountName, cfg, pprofLabels{})
require_NoError(c.t, err)
Expand Down Expand Up @@ -243,13 +259,20 @@ func (a *stateAdder) restart() {

// The filestore is stopped as well, so need to extract the parts to recreate it.
rn := a.n.(*raft)
fs := rn.wal.(*fileStore)

var err error
a.cfg.Log, err = newFileStore(fs.fcfg, fs.cfg.StreamConfig)

switch rn.wal.(type) {
case *fileStore:
fs := rn.wal.(*fileStore)
a.cfg.Log, err = newFileStore(fs.fcfg, fs.cfg.StreamConfig)
case *memStore:
ms := rn.wal.(*memStore)
a.cfg.Log, err = newMemStore(&ms.cfg)
}
if err != nil {
panic(err)
}

a.n, err = a.s.startRaftNode(globalAccountName, a.cfg, pprofLabels{})
if err != nil {
panic(err)
Expand Down
27 changes: 24 additions & 3 deletions server/raft_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2023 The NATS Authors
// Copyright 2021-2024 The NATS 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
Expand Down Expand Up @@ -168,7 +168,7 @@ func TestNRGRecoverFromFollowingNoLeader(t *testing.T) {
for _, n := range rg {
rn := n.node().(*raft)
rn.ApplyQ().drain()
rn.switchToFollower("")
rn.switchToFollower(noLeader)
}

// Resume the nodes.
Expand Down Expand Up @@ -262,7 +262,7 @@ func TestNRGSimpleElection(t *testing.T) {
msg := require_ChanRead(t, voteReqs, time.Second)
vr := decodeVoteRequest(msg.Data, msg.Reply)
require_True(t, vr != nil)
require_NotEqual(t, vr.candidate, "")
require_NotEqual(t, vr.candidate, _EMPTY_)

// The leader should have bumped their term in order to start
// an election.
Expand Down Expand Up @@ -468,3 +468,24 @@ func TestNRGCandidateStepsDownAfterAE(t *testing.T) {
return nil
})
}

// Test to make sure this does not cause us to truncate our wal or enter catchup state.
func TestNRGHeartbeatOnLeaderChange(t *testing.T) {
c := createJetStreamClusterExplicit(t, "R3S", 3)
defer c.shutdown()

rg := c.createMemRaftGroup("TEST", 3, newStateAdder)
rg.waitOnLeader()

for i := 0; i < 10; i++ {
// Restart the leader.
leader := rg.leader().(*stateAdder)
leader.proposeDelta(22)
leader.proposeDelta(-11)
leader.proposeDelta(-11)
rg.waitOnTotal(t, 0)
leader.stop()
leader.restart()
rg.waitOnLeader()
}
}