Commit 9af67a09 authored by Vincent Pelletier's avatar Vincent Pelletier

Only primary master allocates UUIDs.

parent a5be7f16
...@@ -83,12 +83,9 @@ class Application(object): ...@@ -83,12 +83,9 @@ class Application(object):
self.cluster_state = None self.cluster_state = None
self._startup_allowed = False self._startup_allowed = False
# Generate an UUID for self
uuid = config.getUUID() uuid = config.getUUID()
if uuid is None or uuid == '': if uuid:
uuid = self.getNewUUID(None, self.server, NodeTypes.MASTER) self.uuid = uuid
self.uuid = uuid
logging.info('UUID : %s', dump(uuid))
# election related data # election related data
self.unconnected_master_node_set = set() self.unconnected_master_node_set = set()
...@@ -331,6 +328,16 @@ class Application(object): ...@@ -331,6 +328,16 @@ class Application(object):
if node.isRunning(): if node.isRunning():
node.setTemporarilyDown() node.setTemporarilyDown()
if self.uuid is None:
self.uuid = self.getNewUUID(None, self.server, NodeTypes.MASTER)
logging.info('My UUID: ' + dump(self.uuid))
else:
in_conflict = self.nm.getByUUID(self.uuid)
if in_conflict is not None:
logging.warning('UUID conflict at election exit with %r',
in_conflict)
in_conflict.setUUID(None)
# recover the cluster status at startup # recover the cluster status at startup
self.runManager(RecoveryManager) self.runManager(RecoveryManager)
while True: while True:
......
...@@ -82,13 +82,6 @@ class ClientElectionHandler(BaseElectionHandler): ...@@ -82,13 +82,6 @@ class ClientElectionHandler(BaseElectionHandler):
num_replicas, your_uuid, primary, known_master_list): num_replicas, your_uuid, primary, known_master_list):
app = self.app app = self.app
if your_uuid != app.uuid:
# uuid conflict happened, accept the new one
app.uuid = your_uuid
logging.info('UUID conflict, new UUID: %s', dump(your_uuid))
node.setUUID(peer_uuid)
# Register new master nodes. # Register new master nodes.
for address, uuid in known_master_list: for address, uuid in known_master_list:
if app.server == address: if app.server == address:
...@@ -99,11 +92,6 @@ class ClientElectionHandler(BaseElectionHandler): ...@@ -99,11 +92,6 @@ class ClientElectionHandler(BaseElectionHandler):
n = app.nm.getByAddress(address) n = app.nm.getByAddress(address)
if n is None: if n is None:
n = app.nm.createMaster(address=address) n = app.nm.createMaster(address=address)
if uuid is not None:
# If I don't know the UUID yet, believe what the peer
# told me at the moment.
if n.getUUID() is None or n.getUUID() != uuid:
n.setUUID(uuid)
if primary is not None: if primary is not None:
# The primary master is defined. # The primary master is defined.
...@@ -139,11 +127,7 @@ class ServerElectionHandler(BaseElectionHandler, MasterHandler): ...@@ -139,11 +127,7 @@ class ServerElectionHandler(BaseElectionHandler, MasterHandler):
if node is None: if node is None:
node = app.nm.createMaster(address=address) node = app.nm.createMaster(address=address)
# supply another uuid in case of conflict
uuid = app.getNewUUID(uuid, address, node_type)
node.setUUID(uuid)
conn.setUUID(uuid)
elect(app, address) elect(app, address)
return uuid return uuid
...@@ -93,7 +93,6 @@ class PrimaryHandler(EventHandler): ...@@ -93,7 +93,6 @@ class PrimaryHandler(EventHandler):
raise PrimaryFailure('unexpected primary uuid') raise PrimaryFailure('unexpected primary uuid')
if your_uuid != app.uuid: if your_uuid != app.uuid:
# uuid conflict happened, accept the new one
app.uuid = your_uuid app.uuid = your_uuid
logging.info('My UUID: ' + dump(your_uuid)) logging.info('My UUID: ' + dump(your_uuid))
......
...@@ -186,16 +186,6 @@ class MasterClientElectionTests(MasterClientElectionTestBase): ...@@ -186,16 +186,6 @@ class MasterClientElectionTests(MasterClientElectionTestBase):
], ],
) )
def test_acceptIdentification2(self):
""" UUID conflict """
node, conn = self.identifyToMasterNode()
new_uuid = self._makeUUID('M')
args = (node.getUUID(), 0, 10, new_uuid, None,
self._getMasterList())
self.assertRaises(ElectionFailure, self.election.acceptIdentification,
conn, NodeTypes.MASTER, *args)
self.assertEqual(self.app.uuid, new_uuid)
def test_acceptIdentification3(self): def test_acceptIdentification3(self):
""" Identification accepted """ """ Identification accepted """
node, conn = self.identifyToMasterNode() node, conn = self.identifyToMasterNode()
...@@ -269,20 +259,6 @@ class MasterServerElectionTests(MasterClientElectionTestBase): ...@@ -269,20 +259,6 @@ class MasterServerElectionTests(MasterClientElectionTestBase):
self.assertEqual(node.getUUID(), new_uuid) self.assertEqual(node.getUUID(), new_uuid)
self.assertNotEqual(node.getUUID(), uuid) self.assertNotEqual(node.getUUID(), uuid)
def test_requestIdentification5(self):
""" UUID conflict """
node, conn = self.identifyToMasterNode()
args = (self.app.uuid, node.getAddress(), self.app.name)
self.election.requestIdentification(conn,
NodeTypes.MASTER, *args)
self.checkUUIDSet(conn, check_intermediate=False)
args = self.checkAcceptIdentification(conn, decode=True)
(node_type, uuid, partitions, replicas, new_uuid, primary_uuid,
master_list) = args
self.assertNotEqual(self.app.uuid, new_uuid)
self.assertEqual(self.app.uuid, uuid)
def _getNodeList(self): def _getNodeList(self):
return [x.asTuple() for x in self.app.nm.getList()] return [x.asTuple() for x in self.app.nm.getList()]
...@@ -329,7 +305,7 @@ class MasterServerElectionTests(MasterClientElectionTestBase): ...@@ -329,7 +305,7 @@ class MasterServerElectionTests(MasterClientElectionTestBase):
self.assertEqual(uuid, self.app.uuid) self.assertEqual(uuid, self.app.uuid)
self.assertEqual(partitions, self.app.pt.getPartitions()) self.assertEqual(partitions, self.app.pt.getPartitions())
self.assertEqual(replicas, self.app.pt.getReplicas()) self.assertEqual(replicas, self.app.pt.getReplicas())
self.assertTrue((address, peer_uuid) in master_list) self.assertTrue(address in [x[0] for x in master_list])
self.assertTrue(self.app.server in [x[0] for x in master_list]) self.assertTrue(self.app.server in [x[0] for x in master_list])
self.assertEqual(peer_uuid, _peer_uuid) self.assertEqual(peer_uuid, _peer_uuid)
return primary return primary
......
...@@ -800,12 +800,16 @@ def predictable_random(seed=None): ...@@ -800,12 +800,16 @@ def predictable_random(seed=None):
if node_type == NodeTypes.CLIENT: if node_type == NodeTypes.CLIENT:
return super(MasterApplication, self).getNewUUID(uuid, return super(MasterApplication, self).getNewUUID(uuid,
address, node_type) address, node_type)
while uuid is None or uuid == self.uuid: if None != uuid != self.uuid and \
node = self.nm.getByUUID(uuid) self.nm.getByAddress(address) is \
if node is None or node.getAddress() in (None, addr): self.nm.getByUUID(uuid):
uuid = UUID_NAMESPACES[node_type] + ''.join( return uuid
while True:
uuid = UUID_NAMESPACES[node_type] + ''.join(
chr(r.randrange(256)) for _ in xrange(15)) chr(r.randrange(256)) for _ in xrange(15))
return uuid if uuid != self.uuid and \
self.nm.getByUUID(uuid) is None:
return uuid
MasterApplication.getNewUUID = getNewUUID MasterApplication.getNewUUID = getNewUUID
administration.random = backup_app.random = replicator.random \ administration.random = backup_app.random = replicator.random \
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment