Skip to content
Snippets Groups Projects
Commit e2b76a22 authored by ale's avatar ale
Browse files

remove unnecessary Get

The etcd server now returns the current index on a failed Create()
call, so we can use that to start the slave Watch.
parent 16767fbd
No related branches found
No related tags found
No related merge requests found
...@@ -156,19 +156,8 @@ func (m *MasterElection) Run() { ...@@ -156,19 +156,8 @@ func (m *MasterElection) Run() {
// Start as a slave. // Start as a slave.
m.setState(STATE_SLAVE) m.setState(STATE_SLAVE)
var watchIndex uint64
for !m.stopped { for !m.stopped {
// Since a failed Create does not return the
// RAFT index, let's optimistically query the lock
// before starting just to set a baseline for the
// following Watch().
if iresponse, err := m.client.Get(m.Path, false, false); err == nil {
log.Printf("lock already exists: %+v", iresponse)
watchIndex = iresponse.EtcdIndex
}
// Try to acquire the lock. This call will only succeed // Try to acquire the lock. This call will only succeed
// if the lockfile does not exist (either because it // if the lockfile does not exist (either because it
// expired, or the previous master exited gracefully and // expired, or the previous master exited gracefully and
...@@ -180,11 +169,15 @@ func (m *MasterElection) Run() { ...@@ -180,11 +169,15 @@ func (m *MasterElection) Run() {
// and renew our TTL. // and renew our TTL.
log.Printf("masterelection: we are the master") log.Printf("masterelection: we are the master")
m.runMaster(response.EtcdIndex) m.runMaster(response.EtcdIndex)
} else { } else if etcdErr, ok := err.(*etcd.EtcdError); ok {
// We're not the master. Wait until the lock // We're not the master. Wait until the lock
// is deleted or expires. // is deleted or expires.
log.Printf("masterelection: running as slave (%s)", err) log.Printf("masterelection: running as slave (%v)", etcdErr)
m.runSlave(watchIndex) m.runSlave(etcdErr.Index)
} else {
// An error of some other sort! Retry.
log.Printf("masterelection: unexpected error: %v", err)
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment