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

Obtain Configuration before Raft Starts #369

Merged
merged 2 commits into from
Oct 19, 2019
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
20 changes: 20 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,23 @@ func RecoverCluster(conf *Config, fsm FSM, logs LogStore, stable StableStore,
return nil
}

// GetConfiguration returns the configuration of the Raft cluster without
// starting a Raft instance or connecting to the cluster
// This function has identical behavior to Raft.GetConfiguration
func GetConfiguration(conf *Config, fsm FSM, logs LogStore, stable StableStore,
snaps SnapshotStore, trans Transport) (Configuration, error) {
conf.skipStartup = true
r, err := NewRaft(conf, fsm, logs, stable, snaps, trans)
if err != nil {
return Configuration{}, err
}
future := r.GetConfiguration()
if err = future.Error(); err != nil {
return Configuration{}, err
}
return future.Configuration(), nil
}

// HasExistingState returns true if the server has any existing state (logs,
// knowledge of a current term, or any snapshots).
func HasExistingState(logs LogStore, stable StableStore, snaps SnapshotStore) (bool, error) {
Expand Down Expand Up @@ -542,6 +559,9 @@ func NewRaft(conf *Config, fsm FSM, logs LogStore, stable StableStore, snaps Sna
// to be called concurrently with a blocking RPC.
trans.SetHeartbeatHandler(r.processHeartbeat)

if conf.skipStartup {
return r, nil
}
// Start the background work.
r.goFunc(r.run)
r.goFunc(r.runFSM)
Expand Down
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ type Config struct {
// raft's configuration and index values. This is used in NewRaft and
// RestoreCluster.
NoSnapshotRestoreOnStart bool

// skipStartup allows NewRaft() to bypass all background work goroutines
skipStartup bool
}

// DefaultConfig returns a Config with usable defaults.
Expand Down
36 changes: 36 additions & 0 deletions raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2124,6 +2124,42 @@ func TestRaft_LeadershipTransferStopRightAway(t *testing.T) {
t.Errorf("leadership shouldn't have started, but instead it error with: %v", err)
}
}
func TestRaft_GetConfigurationNoBootstrap(t *testing.T) {
c := MakeCluster(2, t, nil)
defer c.Close()

// Should be one leader
c.Followers()
leader := c.Leader()
c.EnsureLeader(t, leader.localAddr)

// Should be able to apply
future := leader.Apply([]byte("test"), c.conf.CommitTimeout)
if err := future.Error(); err != nil {
c.FailNowf("[ERR] err: %v", err)
}
c.WaitForReplication(1)

// Get configuration via GetConfiguration of a running node
cfgf := c.rafts[0].GetConfiguration()
if err := cfgf.Error(); err != nil {
t.Fatal(err)
}
expected := cfgf.Configuration()

// Obtain the same configuration via GetConfig
logs := c.stores[0]
store := c.stores[0]
snap := c.snaps[0]
trans := c.trans[0]
observed, err := GetConfiguration(c.conf, c.fsms[0], logs, store, snap, trans)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(observed, expected) {
t.Errorf("GetConfiguration result differ from Raft.GetConfiguration: observed %+v, expected %+v", observed, expected)
}
}

// TODO: These are test cases we'd like to write for appendEntries().
// Unfortunately, it's difficult to do so with the current way this file is
Expand Down