Commit dd02fa4c authored by Grégory Wisniewski's avatar Grégory Wisniewski

Remove manager getter/setter from Node class and require it in the constructor

since a node must always be registered to the manager. Remove *args parameters,
positional arguments are no more allowed for node creation.
Fix wrong definition of dict in createFromNodeType.


git-svn-id: https://svn.erp5.org/repos/neo/trunk@1313 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 68835978
......@@ -29,18 +29,12 @@ from neo.util import dump
class Node(object):
"""This class represents a node."""
def __init__(self, server=None, uuid=None, state=protocol.UNKNOWN_STATE):
def __init__(self, manager, server=None, uuid=None, state=protocol.UNKNOWN_STATE):
self.state = state
self.server = server
self.uuid = uuid
self.manager = None
self.last_state_change = time()
def setManager(self, manager):
self.manager = manager
def getManager(self):
return self.manager
self.last_state_change = time()
def getLastStateChange(self):
return self.last_state_change
......@@ -135,7 +129,6 @@ class NodeManager(object):
self.uuid_dict = {}
def add(self, node):
node.setManager(self)
self.node_list.append(node)
if node.getServer() is not None:
self.registerServer(node)
......@@ -200,38 +193,38 @@ class NodeManager(object):
return None
return self.uuid_dict.get(uuid)
def _createNode(self, klass, *args, **kw):
node = klass(*args, **kw)
def _createNode(self, klass, **kw):
node = klass(self, **kw)
self.add(node)
return node
def createMaster(self, *args, **kw):
def createMaster(self, **kw):
""" Create and register a new master """
return self._createNode(MasterNode, *args, **kw)
return self._createNode(MasterNode, **kw)
def createStorage(self, *args, **kw):
""" Create and register a new storage """
return self._createNode(StorageNode, *args, **kw)
return self._createNode(StorageNode, **kw)
def createClient(self, *args, **kw):
""" Create and register a new client """
return self._createNode(ClientNode, *args, **kw)
return self._createNode(ClientNode, **kw)
def createAdmin(self, *args, **kw):
""" Create and register a new admin """
return self._createNode(AdminNode, *args, **kw)
return self._createNode(AdminNode, **kw)
def createFromNodeType(self, node_type, *args, **kw):
def createFromNodeType(self, node_type, **kw):
# XXX: use a static dict or drop this
klass = {
protocol.MASTER_NODE_TYPE: self.createMaster,
protocol.STORAGE_NODE_TYPE: self.createStorage,
protocol.CLIENT_NODE_TYPE: self.createClient,
protocol.ADMIN_NODE_TYPE: self.createAdmin,
protocol.MASTER_NODE_TYPE: MasterNode,
protocol.STORAGE_NODE_TYPE: StorageNode,
protocol.CLIENT_NODE_TYPE: ClientNode,
protocol.ADMIN_NODE_TYPE: AdminNode,
}.get(node_type)
if klass is None:
raise RuntimeError('Unknown node type : %s' % node_type)
return self._createNode(klass, *args, **kw)
return self._createNode(klass, **kw)
def clear(self, filter=None):
for node in self.getNodeList():
......@@ -264,7 +257,7 @@ class NodeManager(object):
klass = NODE_TYPE_MAPPING.get(node_type, None)
if klass is None:
raise RuntimeError('Unknown node type')
node = klass(server=addr, uuid=uuid)
node = klass(self, server=addr, uuid=uuid)
node.setState(state)
self.add(node)
logging.info('create node %s %s %s %s' % log_args)
......
......@@ -72,18 +72,18 @@ class MasterPartitionTableTests(NeoTestBase):
# add nodes
uuid1 = self.getNewUUID()
server1 = ("127.0.0.1", 19001)
sn1 = StorageNode(server1, uuid1)
sn1 = StorageNode(Mock(), server1, uuid1)
pt.setCell(0, sn1, UP_TO_DATE_STATE)
pt.setCell(1, sn1, UP_TO_DATE_STATE)
pt.setCell(2, sn1, UP_TO_DATE_STATE)
uuid2 = self.getNewUUID()
server2 = ("127.0.0.2", 19001)
sn2 = StorageNode(server2, uuid2)
sn2 = StorageNode(Mock(), server2, uuid2)
pt.setCell(0, sn2, UP_TO_DATE_STATE)
pt.setCell(1, sn2, UP_TO_DATE_STATE)
uuid3 = self.getNewUUID()
server3 = ("127.0.0.3", 19001)
sn3 = StorageNode(server3, uuid3)
sn3 = StorageNode(Mock(), server3, uuid3)
pt.setCell(0, sn3, UP_TO_DATE_STATE)
# test
node = pt.findLeastUsedNode()
......@@ -97,19 +97,19 @@ class MasterPartitionTableTests(NeoTestBase):
# create nodes
uuid1 = self.getNewUUID()
server1 = ("127.0.0.1", 19001)
sn1 = StorageNode(server1, uuid1)
sn1 = StorageNode(Mock(), server1, uuid1)
uuid2 = self.getNewUUID()
server2 = ("127.0.0.2", 19002)
sn2 = StorageNode(server2, uuid2)
sn2 = StorageNode(Mock(), server2, uuid2)
uuid3 = self.getNewUUID()
server3 = ("127.0.0.3", 19003)
sn3 = StorageNode(server3, uuid3)
sn3 = StorageNode(Mock(), server3, uuid3)
uuid4 = self.getNewUUID()
server4 = ("127.0.0.4", 19004)
sn4 = StorageNode(server4, uuid4)
sn4 = StorageNode(Mock(), server4, uuid4)
uuid5 = self.getNewUUID()
server5 = ("127.0.0.5", 19005)
sn5 = StorageNode(server5, uuid5)
sn5 = StorageNode(Mock(), server5, uuid5)
# create partition table
num_partitions = 5
num_replicas = 3
......@@ -165,7 +165,7 @@ class MasterPartitionTableTests(NeoTestBase):
# add nodes
uuid1 = self.getNewUUID()
server1 = ("127.0.0.1", 19001)
sn1 = StorageNode(server1, uuid1)
sn1 = StorageNode(Mock(), server1, uuid1)
# add it to an empty pt
cell_list = pt.addNode(sn1)
self.assertEqual(len(cell_list), 5)
......@@ -186,7 +186,7 @@ class MasterPartitionTableTests(NeoTestBase):
# add a second node to fill the partition table
uuid2 = self.getNewUUID()
server2 = ("127.0.0.2", 19002)
sn2 = StorageNode(server2, uuid2)
sn2 = StorageNode(Mock(), server2, uuid2)
# add it
cell_list = pt.addNode(sn2)
self.assertEqual(len(cell_list), 5)
......@@ -197,13 +197,13 @@ class MasterPartitionTableTests(NeoTestBase):
# test the most used node is remove from some partition
uuid3 = self.getNewUUID()
server3 = ("127.0.0.3", 19001)
sn3 = StorageNode(server3, uuid3)
sn3 = StorageNode(Mock(), server3, uuid3)
uuid4 = self.getNewUUID()
server4 = ("127.0.0.4", 19001)
sn4 = StorageNode(server4, uuid4)
sn4 = StorageNode(Mock(), server4, uuid4)
uuid5 = self.getNewUUID()
server5 = ("127.0.0.5", 1900)
sn5 = StorageNode(server5, uuid5)
sn5 = StorageNode(Mock(), server5, uuid5)
# partition looks like:
# 0 : sn1, sn2
# 1 : sn1, sn3
......@@ -223,7 +223,7 @@ class MasterPartitionTableTests(NeoTestBase):
pt.setCell(3, sn5, UP_TO_DATE_STATE)
uuid6 = self.getNewUUID()
server6 = ("127.0.0.6", 19006)
sn6 = StorageNode(server6, uuid6)
sn6 = StorageNode(Mock(), server6, uuid6)
cell_list = pt.addNode(sn6)
# sn1 is removed twice and sn6 is added twice
self.assertEqual(len(cell_list), 4)
......@@ -301,16 +301,16 @@ class MasterPartitionTableTests(NeoTestBase):
# add nodes
uuid1 = self.getNewUUID()
server1 = ("127.0.0.1", 19001)
sn1 = StorageNode(server1, uuid1)
sn1 = StorageNode(Mock(), server1, uuid1)
uuid2 = self.getNewUUID()
server2 = ("127.0.0.2", 19002)
sn2 = StorageNode(server2, uuid2)
sn2 = StorageNode(Mock(), server2, uuid2)
uuid3 = self.getNewUUID()
server3 = ("127.0.0.3", 19001)
sn3 = StorageNode(server3, uuid3)
sn3 = StorageNode(Mock(), server3, uuid3)
uuid4 = self.getNewUUID()
server4 = ("127.0.0.4", 19001)
sn4 = StorageNode(server4, uuid4)
sn4 = StorageNode(Mock(), server4, uuid4)
# partition looks like:
# 0 : sn1, sn2
# 1 : sn1, sn3
......@@ -368,22 +368,22 @@ class MasterPartitionTableTests(NeoTestBase):
# add nodes
uuid1 = self.getNewUUID()
server1 = ("127.0.0.1", 19001)
sn1 = StorageNode(server1, uuid1)
sn1 = StorageNode(Mock(), server1, uuid1)
# add not running node
uuid2 = self.getNewUUID()
server2 = ("127.0.0.2", 19001)
sn2 = StorageNode(server2, uuid2)
sn2 = StorageNode(Mock(), server2, uuid2)
sn2.setState(TEMPORARILY_DOWN_STATE)
# add node without uuid
server3 = ("127.0.0.3", 19001)
sn3 = StorageNode(server3, None)
sn3 = StorageNode(Mock(), server3, None)
# add clear node
uuid4 = self.getNewUUID()
server4 = ("127.0.0.4", 19001)
sn4 = StorageNode(server4, uuid4)
sn4 = StorageNode(Mock(), server4, uuid4)
uuid5 = self.getNewUUID()
server5 = ("127.0.0.5", 1900)
sn5 = StorageNode(server5, uuid5)
sn5 = StorageNode(Mock(), server5, uuid5)
# make the table
pt.make([sn1, sn2, sn3, sn4, sn5,])
# check it's ok, only running nodes and node with uuid
......@@ -422,19 +422,19 @@ class MasterPartitionTableTests(NeoTestBase):
# create nodes
uuid1 = self.getNewUUID()
server1 = ("127.0.0.1", 19001)
sn1 = StorageNode(server1, uuid1)
sn1 = StorageNode(Mock(), server1, uuid1)
uuid2 = self.getNewUUID()
server2 = ("127.0.0.2", 19002)
sn2 = StorageNode(server2, uuid2)
sn2 = StorageNode(Mock(), server2, uuid2)
uuid3 = self.getNewUUID()
server3 = ("127.0.0.3", 19003)
sn3 = StorageNode(server3, uuid3)
sn3 = StorageNode(Mock(), server3, uuid3)
uuid4 = self.getNewUUID()
server4 = ("127.0.0.4", 19004)
sn4 = StorageNode(server4, uuid4)
sn4 = StorageNode(Mock(), server4, uuid4)
uuid5 = self.getNewUUID()
server5 = ("127.0.0.5", 19005)
sn5 = StorageNode(server5, uuid5)
sn5 = StorageNode(Mock(), server5, uuid5)
# create partition table
# 0 : sn1(discarded), sn2(up), -> sn2 must remain
# 1 : sn1(feeding), sn2(feeding), sn3(up) -> one feeding and sn3 must remain
......
......@@ -51,24 +51,15 @@ class NodesTests(NeoTestBase):
""" Check the node initialization """
server = ('127.0.0.1', 10000)
uuid = self.getNewUUID()
node = Node(server=server, uuid=uuid)
node = Node(self.manager, server=server, uuid=uuid)
self.assertEqual(node.getState(), protocol.UNKNOWN_STATE)
self.assertEqual(node.getServer(), server)
self.assertEqual(node.getUUID(), uuid)
self.assertEqual(node.getManager(), None)
self.assertTrue(time() - 1 < node.getLastStateChange() < time())
def testManager(self):
""" Check if the node manager is well binded to the node """
node = Node()
self.assertEqual(node.getManager(), None)
node.setManager(self.manager)
self.assertTrue(node.getManager() is self.manager)
# XXX: the manager should index the node by uuid and address
def testState(self):
""" Check if the last changed time is updated when state is changed """
node = Node()
node = Node(self.manager)
self.assertEqual(node.getState(), protocol.UNKNOWN_STATE)
self.assertTrue(time() - 1 < node.getLastStateChange() < time())
previous_time = node.getLastStateChange()
......@@ -78,26 +69,24 @@ class NodesTests(NeoTestBase):
self.assertTrue(time() - 1 < node.getLastStateChange() < time())
def testServer(self):
""" Check if the manager is updated when a node change it's address """
node = Node()
""" Check if the node is indexed by server """
node = Node(self.manager)
self.assertEqual(node.getServer(), None)
node.setManager(self.manager)
server = ('127.0.0.1', 10000)
node.setServer(server)
self._updatedByServer(node)
def testUUID(self):
""" As for Server but UUID """
node = Node()
node = Node(self.manager)
self.assertEqual(node.getServer(), None)
node.setManager(self.manager)
uuid = self.getNewUUID()
node.setUUID(uuid)
self._updatedByUUID(node)
def testTypes(self):
""" Check that the abstract node has no type """
node = Node()
node = Node(self.manager)
self.assertRaises(NotImplementedError, node.getType)
self.assertFalse(node.isStorage())
self.assertFalse(node.isMaster())
......@@ -106,7 +95,7 @@ class NodesTests(NeoTestBase):
def testMaster(self):
""" Check Master sub class """
node = MasterNode()
node = MasterNode(self.manager)
self.assertEqual(node.getType(), protocol.MASTER_NODE_TYPE)
self.assertTrue(node.isMaster())
self.assertFalse(node.isStorage())
......@@ -115,7 +104,7 @@ class NodesTests(NeoTestBase):
def testStorage(self):
""" Check Storage sub class """
node = StorageNode()
node = StorageNode(self.manager)
self.assertEqual(node.getType(), protocol.STORAGE_NODE_TYPE)
self.assertTrue(node.isStorage())
self.assertFalse(node.isMaster())
......@@ -124,7 +113,7 @@ class NodesTests(NeoTestBase):
def testClient(self):
""" Check Client sub class """
node = ClientNode()
node = ClientNode(self.manager)
self.assertEqual(node.getType(), protocol.CLIENT_NODE_TYPE)
self.assertTrue(node.isClient())
self.assertFalse(node.isMaster())
......@@ -133,7 +122,7 @@ class NodesTests(NeoTestBase):
def testAdmin(self):
""" Check Admin sub class """
node = AdminNode()
node = AdminNode(self.manager)
self.assertEqual(node.getType(), protocol.ADMIN_NODE_TYPE)
self.assertTrue(node.isAdmin())
self.assertFalse(node.isMaster())
......@@ -141,16 +130,14 @@ class NodesTests(NeoTestBase):
self.assertFalse(node.isClient())
class NodeManagerTests(NeoTestBase):
def setUp(self):
self.manager = NodeManager()
self.storage = StorageNode(('127.0.0.1', 1000), self.getNewUUID())
self.master = MasterNode(('127.0.0.1', 2000), self.getNewUUID())
self.client = ClientNode(None, self.getNewUUID())
self.admin = AdminNode(('127.0.0.1', 4000), self.getNewUUID())
self.manager = nm = NodeManager()
self.storage = StorageNode(nm, ('127.0.0.1', 1000), self.getNewUUID())
self.master = MasterNode(nm, ('127.0.0.1', 2000), self.getNewUUID())
self.client = ClientNode(nm, None, self.getNewUUID())
self.admin = AdminNode(nm, ('127.0.0.1', 4000), self.getNewUUID())
def checkNodes(self, node_list):
manager = self.manager
......
......@@ -29,7 +29,7 @@ class PartitionTableTests(NeoTestBase):
def test_01_Cell(self):
uuid = self.getNewUUID()
server = ("127.0.0.1", 19001)
sn = StorageNode(server, uuid)
sn = StorageNode(Mock(), server, uuid)
cell = Cell(sn)
self.assertEquals(cell.node, sn)
self.assertEquals(cell.state, UP_TO_DATE_STATE)
......@@ -53,7 +53,7 @@ class PartitionTableTests(NeoTestBase):
pt = PartitionTable(num_partitions, num_replicas)
uuid1 = self.getNewUUID()
server1 = ("127.0.0.1", 19001)
sn1 = StorageNode(server1, uuid1)
sn1 = StorageNode(Mock(), server1, uuid1)
for x in xrange(num_partitions):
self.assertEqual(len(pt.partition_list[x]), 0)
# add a cell to an empty row
......@@ -132,7 +132,7 @@ class PartitionTableTests(NeoTestBase):
pt = PartitionTable(num_partitions, num_replicas)
uuid1 = self.getNewUUID()
server1 = ("127.0.0.1", 19001)
sn1 = StorageNode(server1, uuid1)
sn1 = StorageNode(Mock(), server1, uuid1)
for x in xrange(num_partitions):
self.assertEqual(len(pt.partition_list[x]), 0)
# add a cell to an empty row
......@@ -172,19 +172,19 @@ class PartitionTableTests(NeoTestBase):
# add two kind of node, usable and unsable
uuid1 = self.getNewUUID()
server1 = ("127.0.0.1", 19001)
sn1 = StorageNode(server1, uuid1)
sn1 = StorageNode(Mock(), server1, uuid1)
pt.setCell(0, sn1, UP_TO_DATE_STATE)
uuid2 = self.getNewUUID()
server2 = ("127.0.0.2", 19001)
sn2 = StorageNode(server2, uuid2)
sn2 = StorageNode(Mock(), server2, uuid2)
pt.setCell(0, sn2, OUT_OF_DATE_STATE)
uuid3 = self.getNewUUID()
server3 = ("127.0.0.3", 19001)
sn3 = StorageNode(server3, uuid3)
sn3 = StorageNode(Mock(), server3, uuid3)
pt.setCell(0, sn3, FEEDING_STATE)
uuid4 = self.getNewUUID()
server4 = ("127.0.0.4", 19001)
sn4 = StorageNode(server4, uuid4)
sn4 = StorageNode(Mock(), server4, uuid4)
pt.setCell(0, sn4, DISCARDED_STATE) # won't be added
# now checks result
self.assertEqual(len(pt.partition_list[0]), 3)
......@@ -234,15 +234,15 @@ class PartitionTableTests(NeoTestBase):
# add two kind of node, usable and unsable
uuid1 = self.getNewUUID()
server1 = ("127.0.0.1", 19001)
sn1 = StorageNode(server1, uuid1)
sn1 = StorageNode(Mock(), server1, uuid1)
pt.setCell(0, sn1, UP_TO_DATE_STATE)
uuid2 = self.getNewUUID()
server2 = ("127.0.0.2", 19001)
sn2 = StorageNode(server2, uuid2)
sn2 = StorageNode(Mock(), server2, uuid2)
pt.setCell(1, sn2, OUT_OF_DATE_STATE)
uuid3 = self.getNewUUID()
server3 = ("127.0.0.3", 19001)
sn3 = StorageNode(server3, uuid3)
sn3 = StorageNode(Mock(), server3, uuid3)
pt.setCell(2, sn3, FEEDING_STATE)
# now checks result
self.assertEqual(len(pt.partition_list[0]), 1)
......@@ -264,19 +264,19 @@ class PartitionTableTests(NeoTestBase):
# add two kind of node, usable and unsable
uuid1 = self.getNewUUID()
server1 = ("127.0.0.1", 19001)
sn1 = StorageNode(server1, uuid1)
sn1 = StorageNode(Mock(), server1, uuid1)
pt.setCell(0, sn1, UP_TO_DATE_STATE)
uuid2 = self.getNewUUID()
server2 = ("127.0.0.2", 19001)
sn2 = StorageNode(server2, uuid2)
sn2 = StorageNode(Mock(), server2, uuid2)
pt.setCell(0, sn2, OUT_OF_DATE_STATE)
uuid3 = self.getNewUUID()
server3 = ("127.0.0.3", 19001)
sn3 = StorageNode(server3, uuid3)
sn3 = StorageNode(Mock(), server3, uuid3)
pt.setCell(0, sn3, FEEDING_STATE)
uuid4 = self.getNewUUID()
server4 = ("127.0.0.4", 19001)
sn4 = StorageNode(server4, uuid4)
sn4 = StorageNode(Mock(), server4, uuid4)
pt.setCell(0, sn4, DISCARDED_STATE) # won't be added
# must get only two node as feeding and discarded not taken
# into account
......@@ -297,7 +297,7 @@ class PartitionTableTests(NeoTestBase):
# adding a node in all partition
uuid1 = self.getNewUUID()
server1 = ("127.0.0.1", 19001)
sn1 = StorageNode(server1, uuid1)
sn1 = StorageNode(Mock(), server1, uuid1)
for x in xrange(num_partitions):
pt.setCell(x, sn1, UP_TO_DATE_STATE)
self.assertEqual(pt.num_filled_rows, num_partitions)
......@@ -310,7 +310,7 @@ class PartitionTableTests(NeoTestBase):
# add two kind of node, usable and unsable
uuid1 = self.getNewUUID()
server1 = ("127.0.0.1", 19001)
sn1 = StorageNode(server1, uuid1)
sn1 = StorageNode(Mock(), server1, uuid1)
pt.setCell(0, sn1, UP_TO_DATE_STATE)
# now test
self.assertTrue(pt.hasOffset(0))
......@@ -327,7 +327,7 @@ class PartitionTableTests(NeoTestBase):
# adding a node in all partition
uuid1 = self.getNewUUID()
server1 = ("127.0.0.1", 19001)
sn1 = StorageNode(server1, uuid1)
sn1 = StorageNode(Mock(), server1, uuid1)
for x in xrange(num_partitions):
pt.setCell(x, sn1, UP_TO_DATE_STATE)
self.assertTrue(pt.filled())
......@@ -341,7 +341,7 @@ class PartitionTableTests(NeoTestBase):
# adding a node in all partition
uuid1 = self.getNewUUID()
server1 = ("127.0.0.1", 19001)
sn1 = StorageNode(server1, uuid1)
sn1 = StorageNode(Mock(), server1, uuid1)
for x in xrange(num_partitions):
pt.setCell(x, sn1, FEEDING_STATE)
self.assertTrue(pt.filled())
......@@ -356,7 +356,7 @@ class PartitionTableTests(NeoTestBase):
# adding a node in all partition
uuid1 = self.getNewUUID()
server1 = ("127.0.0.1", 19001)
sn1 = StorageNode(server1, uuid1)
sn1 = StorageNode(Mock(), server1, uuid1)
sn1.setState(TEMPORARILY_DOWN_STATE)
for x in xrange(num_partitions):
pt.setCell(x, sn1, FEEDING_STATE)
......@@ -371,7 +371,7 @@ class PartitionTableTests(NeoTestBase):
# adding a node in all partition
uuid1 = self.getNewUUID()
server1 = ("127.0.0.1", 19001)
sn1 = StorageNode(server1, uuid1)
sn1 = StorageNode(Mock(), server1, uuid1)
for x in xrange(num_partitions):
pt.setCell(x, sn1, OUT_OF_DATE_STATE)
self.assertTrue(pt.filled())
......@@ -385,18 +385,18 @@ class PartitionTableTests(NeoTestBase):
# add nodes
uuid1 = self.getNewUUID()
server1 = ("127.0.0.1", 19001)
sn1 = StorageNode(server1, uuid1)
sn1 = StorageNode(Mock(), server1, uuid1)
pt.setCell(0, sn1, UP_TO_DATE_STATE)
pt.setCell(1, sn1, UP_TO_DATE_STATE)
pt.setCell(2, sn1, UP_TO_DATE_STATE)
uuid2 = self.getNewUUID()
server2 = ("127.0.0.2", 19001)
sn2 = StorageNode(server2, uuid2)
sn2 = StorageNode(Mock(), server2, uuid2)
pt.setCell(0, sn2, UP_TO_DATE_STATE)
pt.setCell(1, sn2, UP_TO_DATE_STATE)
uuid3 = self.getNewUUID()
server3 = ("127.0.0.3", 19001)
sn3 = StorageNode(server3, uuid3)
sn3 = StorageNode(Mock(), server3, uuid3)
pt.setCell(0, sn3, UP_TO_DATE_STATE)
# test
row_0 = pt.getRow(0)
......
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