Commit 7a09c709 authored by Grégory Wisniewski's avatar Grégory Wisniewski

Factorize handelNotifyNodeInformation for primary bootstrap & notification

handlers and update tests.


git-svn-id: https://svn.erp5.org/repos/neo/branches/prototype3@593 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 351c920c
......@@ -61,8 +61,52 @@ class BaseHandler(EventHandler):
else:
queue.put((conn, packet))
class PrimaryHandler(BaseHandler):
class PrimaryBootstrapHandler(BaseHandler):
def handleNotifyNodeInformation(self, conn, packet, node_list):
app = self.app
nm = app.nm
for node_type, ip_address, port, uuid, state in node_list:
# Register new nodes.
addr = (ip_address, port)
# Try to retrieve it from nm
n = None
if uuid != INVALID_UUID:
n = nm.getNodeByUUID(uuid)
if n is None:
n = nm.getNodeByServer(addr)
if n is not None and uuid != INVALID_UUID:
# node only exists by address, remove it
nm.remove(n)
n = None
elif n.getServer() != addr:
# same uuid but different address, remove it
nm.remove(n)
n = None
if node_type == MASTER_NODE_TYPE:
if n is None:
n = MasterNode(server = addr)
nm.add(n)
if uuid != INVALID_UUID:
# If I don't know the UUID yet, believe what the peer
# told me at the moment.
if n.getUUID() is None:
n.setUUID(uuid)
elif node_type == STORAGE_NODE_TYPE:
if uuid == INVALID_UUID:
# No interest.
continue
if n is None:
n = StorageNode(server = addr, uuid = uuid)
nm.add(n)
elif node_type == CLIENT_NODE_TYPE:
continue
n.setState(state)
class PrimaryBootstrapHandler(PrimaryHandler):
""" Bootstrap handler used when looking for the primary master """
def connectionFailed(self, conn):
......@@ -168,58 +212,6 @@ class PrimaryBootstrapHandler(BaseHandler):
# Whatever the situation is, I trust this master.
app.primary_master_node = primary_node
@identification_required
def handleNotifyNodeInformation(self, conn, packet, node_list):
uuid = conn.getUUID()
app = self.app
nm = app.nm
node = nm.getNodeByUUID(uuid)
# This must be sent only by a primary master node.
# Note that this may be sent before I know that it is
# a primary master node.
if node.getNodeType() != MASTER_NODE_TYPE:
logging.warn('ignoring notify node information from %s',
dump(uuid))
return
for node_type, ip_address, port, uuid, state in node_list:
# Register new nodes.
addr = (ip_address, port)
# Try to retrieve it from nm
n = None
if uuid != INVALID_UUID:
n = nm.getNodeByUUID(uuid)
if n is None:
n = nm.getNodeByServer(addr)
if n is not None and uuid != INVALID_UUID:
# node only exists by address, remove it
nm.remove(n)
n = None
elif n.getServer() != addr:
# same uuid but different address, remove it
nm.remove(n)
n = None
if node_type == MASTER_NODE_TYPE:
if n is None:
n = MasterNode(server = addr)
nm.add(n)
if uuid != INVALID_UUID:
# If I don't know the UUID yet, believe what the peer
# told me at the moment.
if n.getUUID() is None:
n.setUUID(uuid)
elif node_type == STORAGE_NODE_TYPE:
if uuid == INVALID_UUID:
# No interest.
continue
if n is None:
n = StorageNode(server = addr, uuid = uuid)
nm.add(n)
elif node_type == CLIENT_NODE_TYPE:
continue
n.setState(state)
@identification_required
def handleSendPartitionTable(self, conn, packet, ptid, row_list):
# This handler is in PrimaryBootstrapHandler, since this
......@@ -256,7 +248,7 @@ class PrimaryBootstrapHandler(BaseHandler):
pt.setCell(offset, node, state)
class PrimaryNotificationsHandler(BaseHandler):
class PrimaryNotificationsHandler(PrimaryHandler):
""" Handler that process the notifications from the primary master """
def connectionClosed(self, conn):
......@@ -297,38 +289,6 @@ class PrimaryNotificationsHandler(BaseHandler):
finally:
app._cache_lock_release()
def handleNotifyNodeInformation(self, conn, packet, node_list):
app = self.app
nm = app.nm
for node_type, ip_address, port, uuid, state in node_list:
# Register new nodes.
addr = (ip_address, port)
if node_type == MASTER_NODE_TYPE:
n = nm.getNodeByServer(addr)
if n is None:
n = MasterNode(server = addr)
nm.add(n)
if uuid != INVALID_UUID:
# If I don't know the UUID yet, believe what the peer
# told me at the moment.
if n.getUUID() is None:
n.setUUID(uuid)
elif node_type == STORAGE_NODE_TYPE:
if uuid == INVALID_UUID:
# No interest.
continue
n = nm.getNodeByUUID(uuid)
if n is None:
n = StorageNode(server = addr, uuid = uuid)
nm.add(n)
else:
n.setServer(addr)
elif node_type == CLIENT_NODE_TYPE:
continue
n.setState(state)
def handleNotifyPartitionChanges(self, conn, packet, ptid, cell_list):
app = self.app
......
......@@ -310,16 +310,17 @@ class ClientHandlerTest(NeoTestBase):
self.assertEquals(app.pt, None)
self.assertEquals(app.uuid, 'C' * 16)
def _testHandleUnexpectedPacketCalledWithMedhod(self, client_handler, method, args=(), kw=()):
call_list = []
def _testHandleUnexpectedPacketCalledWithMedhod(self, method, args=(), kw=()):
self.assertRaises(UnexpectedPacketError, method, *args, **dict(kw))
# Master node handler
def test_initialAnswerPrimaryMaster(self):
client_handler = PrimaryBootstrapHandler(None, self.getDispatcher())
class App:
nm = Mock()
client_handler = PrimaryBootstrapHandler(App(), self.getDispatcher())
conn = Mock({'getUUID': None})
self._testHandleUnexpectedPacketCalledWithMedhod(
client_handler, client_handler.handleAnswerPrimaryMaster,
client_handler.handleAnswerPrimaryMaster,
args=(conn, None, 0, []))
def test_nonMasterAnswerPrimaryMaster(self):
......@@ -511,7 +512,7 @@ class ClientHandlerTest(NeoTestBase):
client_handler = PrimaryBootstrapHandler(None, self.getDispatcher())
conn = Mock({'getUUID': None})
self._testHandleUnexpectedPacketCalledWithMedhod(
client_handler, client_handler.handleSendPartitionTable,
client_handler.handleSendPartitionTable,
args=(conn, None, None, None))
def test_nonMasterSendPartitionTable(self):
......@@ -589,13 +590,6 @@ class ClientHandlerTest(NeoTestBase):
setCell_call_list[0].checkArgs(test_row_list[0][0], test_node,
test_row_list[0][1][0][1])
def test_initialNotifyNodeInformation(self):
client_handler = PrimaryBootstrapHandler(None, self.getDispatcher())
conn = Mock({'getUUID': None})
self._testHandleUnexpectedPacketCalledWithMedhod(
client_handler, client_handler.handleNotifyNodeInformation,
args=(conn, None, None))
def test_nonMasterNotifyNodeInformation(self):
for node_type in (CLIENT_NODE_TYPE, STORAGE_NODE_TYPE):
test_master_uuid = self.getNewUUID()
......@@ -663,9 +657,15 @@ class ClientHandlerTest(NeoTestBase):
RUNNING_STATE)
nm = self._testNotifyNodeInformation(test_node, getNodeByServer=node,
getNodeByUUID=node)
# Check that no node got added
self.assertEqual(len(nm.mockGetNamedCalls('add')), 0)
add_call_list = node.mockGetNamedCalls('add')
# Check that node got replaced
add_call_list = nm.mockGetNamedCalls('add')
self.assertEquals(len(add_call_list), 1)
remove_call_list = nm.mockGetNamedCalls('remove')
self.assertEquals(len(remove_call_list), 1)
# Check node state has been updated
setState_call_list = node.mockGetNamedCalls('setState')
self.assertEqual(len(setState_call_list), 1)
self.assertEqual(setState_call_list[0].getParam(0), test_node[4])
def test_unknownStorageNotifyNodeInformation(self):
test_node = (STORAGE_NODE_TYPE, '127.0.0.1', 10010, self.getNewUUID(),
......@@ -682,23 +682,18 @@ class ClientHandlerTest(NeoTestBase):
# Likewise for server address and node uuid.
def test_knownStorageNotifyNodeInformation(self):
import pdb
pdb.set_trace()
node = Mock({'setState': None, 'setServer': None})
test_node = (STORAGE_NODE_TYPE, '127.0.0.1', 10010, self.getNewUUID(),
RUNNING_STATE)
nm = self._testNotifyNodeInformation(test_node, getNodeByUUID=node)
# Check that no node got added
self.assertEqual(len(nm.mockGetNamedCalls('add')), 0)
# Check that server address got set
setServer_call_list = node.mockGetNamedCalls('setServer')
self.assertEqual(len(setServer_call_list), 1)
self.assertEqual(setServer_call_list[0].getParam(0),
(test_node[1], test_node[2]))
# Check that node got replaced
add_call_list = nm.mockGetNamedCalls('add')
self.assertEquals(len(add_call_list), 1)
remove_call_list = nm.mockGetNamedCalls('remove')
self.assertEquals(len(remove_call_list), 1)
# Check node state has been updated
setState_call_list = node.mockGetNamedCalls('setState')
self.assertEqual(len(setState_call_list), 1)
self.assertEqual(setState_call_list[0].getParam(0), test_node[4])
node = add_call_list[0].getParam(0)
self.assertEquals(node.getState(), test_node[4])
def test_initialNotifyPartitionChanges(self):
class App:
......@@ -709,7 +704,7 @@ class ClientHandlerTest(NeoTestBase):
client_handler = PrimaryBootstrapHandler(app, self.getDispatcher())
conn = Mock({'getUUID': None})
self._testHandleUnexpectedPacketCalledWithMedhod(
client_handler, client_handler.handleNotifyPartitionChanges,
client_handler.handleNotifyPartitionChanges,
args=(conn, None, None, None))
def test_nonMasterNotifyPartitionChanges(self):
......
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