From e2b76a228ba183287c3e25aca5d40bdef79d4511 Mon Sep 17 00:00:00 2001
From: ale <ale@incal.net>
Date: Tue, 14 Oct 2014 23:14:11 +0100
Subject: [PATCH] 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.
---
 masterelection/masterelection.go | 21 +++++++--------------
 1 file changed, 7 insertions(+), 14 deletions(-)

diff --git a/masterelection/masterelection.go b/masterelection/masterelection.go
index da2f9fd4..a979db42 100644
--- a/masterelection/masterelection.go
+++ b/masterelection/masterelection.go
@@ -156,19 +156,8 @@ func (m *MasterElection) Run() {
 	// Start as a slave.
 	m.setState(STATE_SLAVE)
 
-	var watchIndex uint64
-
 	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
 		// if the lockfile does not exist (either because it
 		// expired, or the previous master exited gracefully and
@@ -180,11 +169,15 @@ func (m *MasterElection) Run() {
 			// and renew our TTL.
 			log.Printf("masterelection: we are the master")
 			m.runMaster(response.EtcdIndex)
-		} else {
+		} else if etcdErr, ok := err.(*etcd.EtcdError); ok {
 			// We're not the master. Wait until the lock
 			// is deleted or expires.
-			log.Printf("masterelection: running as slave (%s)", err)
-			m.runSlave(watchIndex)
+			log.Printf("masterelection: running as slave (%v)", etcdErr)
+			m.runSlave(etcdErr.Index)
+		} else {
+			// An error of some other sort! Retry.
+			log.Printf("masterelection: unexpected error: %v", err)
 		}
+
 	}
 }
-- 
GitLab