From b43bf3b9252fcb1de407afa139f81a8a9a43f31e Mon Sep 17 00:00:00 2001
From: Vincent Pelletier <vincent@nexedi.com>
Date: Wed, 1 Jul 2009 09:58:43 +0000
Subject: [PATCH] Exit election loop when we know we are not the primary.
 Expect AcceptnodeIdentification message in secondary handler connected to
 primary master. It will be received just once (upon election state exit).

git-svn-id: https://svn.erp5.org/repos/neo/branches/prototype3@742 71dcc9de-d417-0410-9af5-da40c76e7ee4
---
 neo/master/app.py       |  5 +++--
 neo/master/secondary.py | 16 ++++++++++++++++
 2 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/neo/master/app.py b/neo/master/app.py
index 3b96833e..60bd2aab 100644
--- a/neo/master/app.py
+++ b/neo/master/app.py
@@ -177,8 +177,9 @@ class Application(object):
                             for addr in list(self.unconnected_master_node_set):
                                 ClientConnection(em, client_handler, addr = addr,
                                                  connector_handler = self.connector_handler)
-                    if len(self.unconnected_master_node_set) == 0 \
-                            and len(self.negotiating_master_node_set) == 0:
+                    if (len(self.unconnected_master_node_set) == 0 \
+                            and len(self.negotiating_master_node_set) == 0) \
+                       or self.primary is not None:
                         break
 
                 # Now there are three situations:
diff --git a/neo/master/secondary.py b/neo/master/secondary.py
index 4898ef78..329f63e4 100644
--- a/neo/master/secondary.py
+++ b/neo/master/secondary.py
@@ -99,3 +99,19 @@ class PrimaryMasterEventHandler(MasterEventHandler):
                     # told me at the moment.
                     if n.getUUID() is None:
                         n.setUUID(uuid)
+
+    def handleAcceptNodeIdentification(self, conn, packet, node_type,
+                                       uuid, ip_address, port, num_partitions,
+                                       num_replicas, your_uuid):
+        app = self.app
+        node = app.nm.getNodeByServer(conn.getAddress())
+        assert node_type == MASTER_NODE_TYPE
+        assert conn.getAddress() == (ip_address, port)
+
+        if your_uuid != app.uuid:
+            # uuid conflict happened, accept the new one
+            app.uuid = your_uuid
+
+        conn.setUUID(uuid)
+        node.setUUID(uuid)
+
-- 
2.30.9