Skip to content

Commit

Permalink
nomad: using Raft StartAsLeader to make tests faster
Browse files Browse the repository at this point in the history
  • Loading branch information
armon committed Sep 7, 2015
1 parent e5c7eff commit c2b7ce9
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
2 changes: 2 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func testServer(t *testing.T, cb func(*nomad.Config)) (*nomad.Server, string) {
config.RaftConfig.LeaderLeaseTimeout = 20 * time.Millisecond
config.RaftConfig.HeartbeatTimeout = 40 * time.Millisecond
config.RaftConfig.ElectionTimeout = 40 * time.Millisecond
config.RaftConfig.StartAsLeader = true
config.RaftTimeout = 500 * time.Millisecond

// Invoke the callback if any
if cb != nil {
Expand Down
1 change: 1 addition & 0 deletions command/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func makeAgent(t *testing.T, cb func(*Config)) (string, *Agent) {
config.RaftConfig.LeaderLeaseTimeout = 20 * time.Millisecond
config.RaftConfig.HeartbeatTimeout = 40 * time.Millisecond
config.RaftConfig.ElectionTimeout = 40 * time.Millisecond
config.RaftConfig.StartAsLeader = true
config.RaftTimeout = 500 * time.Millisecond

if cb != nil {
Expand Down
3 changes: 1 addition & 2 deletions nomad/leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ import (
// as the leader in the Raft cluster. There is some work the leader is
// expected to do, so we must react to changes
func (s *Server) monitorLeadership() {
leaderCh := s.raft.LeaderCh()
var stopCh chan struct{}
for {
select {
case isLeader := <-leaderCh:
case isLeader := <-s.leaderCh:
if isLeader {
stopCh = make(chan struct{})
go s.leaderLoop(stopCh)
Expand Down
18 changes: 13 additions & 5 deletions nomad/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type Server struct {

// The raft instance is used among Nomad nodes within the
// region to protect operations that require strong consistency
leaderCh <-chan bool
raft *raft.Raft
raftLayer *RaftLayer
raftPeers raft.PeerStore
Expand Down Expand Up @@ -197,14 +198,19 @@ func NewServer(config *Config) (*Server, error) {
s.Shutdown()
return nil, fmt.Errorf("Failed to start serf: %v", err)
}
go s.serfEventHandler()

// Intialize the scheduling workers
if err := s.setupWorkers(); err != nil {
s.Shutdown()
return nil, fmt.Errorf("Failed to start workers: %v", err)
}

// Monitor leadership changes
go s.monitorLeadership()

// Start ingesting events for Serf
go s.serfEventHandler()

// Start the RPC listeners
go s.listen()

Expand Down Expand Up @@ -471,6 +477,11 @@ func (s *Server) setupRaft() error {
// Make sure we set the LogOutput
s.config.RaftConfig.LogOutput = s.config.LogOutput

// Setup the leader channel
leaderCh := make(chan bool, 1)
s.config.RaftConfig.NotifyCh = leaderCh
s.leaderCh = leaderCh

// Setup the Raft store
s.raft, err = raft.NewRaft(s.config.RaftConfig, s.fsm, log, stable,
snap, peers, trans)
Expand All @@ -481,9 +492,6 @@ func (s *Server) setupRaft() error {
trans.Close()
return err
}

// Start monitoring leadership
go s.monitorLeadership()
return nil
}

Expand All @@ -499,7 +507,7 @@ func (s *Server) setupSerf(conf *serf.Config, ch chan serf.Event, path string) (
conf.Tags["vsn_max"] = fmt.Sprintf("%d", ProtocolVersionMax)
conf.Tags["build"] = s.config.Build
conf.Tags["port"] = fmt.Sprintf("%d", s.rpcAdvertise.(*net.TCPAddr).Port)
if s.config.Bootstrap {
if s.config.Bootstrap || (s.config.DevMode && !s.config.DevDisableBootstrap) {
conf.Tags["bootstrap"] = "1"
}
if s.config.BootstrapExpect != 0 {
Expand Down
3 changes: 3 additions & 0 deletions nomad/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func testServer(t *testing.T, cb func(*Config)) *Server {
cb(config)
}

// Enable raft as leader if we have bootstrap on
config.RaftConfig.StartAsLeader = !config.DevDisableBootstrap

// Create server
server, err := NewServer(config)
if err != nil {
Expand Down

0 comments on commit c2b7ce9

Please sign in to comment.