Commit f4836f41 authored by Vincent Pelletier's avatar Vincent Pelletier

Factorise code handling expected answers.

Also, stop using a dict in an inefficient way, and replace it by a set.

git-svn-id: https://svn.erp5.org/repos/neo/trunk@2216 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 8aaff20f
...@@ -41,24 +41,40 @@ class VerificationManager(BaseServiceHandler): ...@@ -41,24 +41,40 @@ class VerificationManager(BaseServiceHandler):
BaseServiceHandler.__init__(self, app) BaseServiceHandler.__init__(self, app)
self._oid_set = set() self._oid_set = set()
self._tid_set = set() self._tid_set = set()
self._uuid_dict = {} self._uuid_set = set()
self._object_present = False self._object_present = False
def _askStorageNodesAndWait(self, packet, node_list): def _askStorageNodesAndWait(self, packet, node_list):
poll = self.app.em.poll poll = self.app.em.poll
operational = self.app.pt.operational operational = self.app.pt.operational
uuid_dict = self._uuid_dict uuid_set = self._uuid_set
uuid_dict.clear() uuid_set.clear()
for node in node_list: for node in node_list:
uuid_dict[node.getUUID()] = False uuid_set.add(node.getUUID())
node.ask(packet) node.ask(packet)
while True: while True:
poll(1) poll(1)
if not operational(): if not operational():
raise VerificationFailure raise VerificationFailure
if False not in uuid_dict.values(): if not uuid_set:
break break
def _gotAnswerFrom(self, uuid):
"""
Returns True if answer from given uuid is waited upon by
_askStorageNodesAndWait, False otherwise.
Also, mark this uuid as having answered, so it stops being waited upon
by _askStorageNodesAndWait.
"""
try:
self._uuid_set.remove(uuid)
except KeyError:
result = False
else:
result = True
return result
def getHandler(self): def getHandler(self):
return self return self
...@@ -179,18 +195,15 @@ class VerificationManager(BaseServiceHandler): ...@@ -179,18 +195,15 @@ class VerificationManager(BaseServiceHandler):
def answerUnfinishedTransactions(self, conn, tid_list): def answerUnfinishedTransactions(self, conn, tid_list):
uuid = conn.getUUID() uuid = conn.getUUID()
logging.info('got unfinished transactions %s from %r', tid_list, conn) logging.info('got unfinished transactions %s from %r', tid_list, conn)
if self._uuid_dict.get(uuid, True): if not self._gotAnswerFrom(uuid):
# No interest.
return return
self._tid_set.update(tid_list) self._tid_set.update(tid_list)
self._uuid_dict[uuid] = True
def answerTransactionInformation(self, conn, tid, def answerTransactionInformation(self, conn, tid,
user, desc, ext, packed, oid_list): user, desc, ext, packed, oid_list):
uuid = conn.getUUID() uuid = conn.getUUID()
app = self.app app = self.app
if self._uuid_dict.get(uuid, True): if not self._gotAnswerFrom(uuid):
# No interest.
return return
oid_set = set(oid_list) oid_set = set(oid_list)
if self._oid_set is None: if self._oid_set is None:
...@@ -201,34 +214,26 @@ class VerificationManager(BaseServiceHandler): ...@@ -201,34 +214,26 @@ class VerificationManager(BaseServiceHandler):
self._oid_set.update(oid_set) self._oid_set.update(oid_set)
elif self._oid_set != oid_set: elif self._oid_set != oid_set:
self._oid_set = None self._oid_set = None
self._uuid_dict[uuid] = True
def tidNotFound(self, conn, message): def tidNotFound(self, conn, message):
uuid = conn.getUUID() uuid = conn.getUUID()
logging.info('TID not found: %s', message) logging.info('TID not found: %s', message)
if self._uuid_dict.get(uuid, True): if not self._gotAnswerFrom(uuid):
# No interest.
return return
self._oid_set = None self._oid_set = None
self._uuid_dict[uuid] = True
def answerObjectPresent(self, conn, oid, tid): def answerObjectPresent(self, conn, oid, tid):
uuid = conn.getUUID() uuid = conn.getUUID()
logging.info('object %s:%s found', dump(oid), dump(tid)) logging.info('object %s:%s found', dump(oid), dump(tid))
if self._uuid_dict.get(uuid, True): self._gotAnswerFrom(uuid)
# No interest.
return
self._uuid_dict[uuid] = True
def oidNotFound(self, conn, message): def oidNotFound(self, conn, message):
uuid = conn.getUUID() uuid = conn.getUUID()
logging.info('OID not found: %s', message) logging.info('OID not found: %s', message)
app = self.app app = self.app
if self._uuid_dict.get(uuid, True): if not self._gotAnswerFrom(uuid):
# No interest.
return return
app.object_present = False app.object_present = False
self._uuid_dict[uuid] = True
def connectionCompleted(self, conn): def connectionCompleted(self, conn):
pass pass
......
...@@ -123,19 +123,18 @@ class MasterVerificationTests(NeoTestBase): ...@@ -123,19 +123,18 @@ class MasterVerificationTests(NeoTestBase):
uuid = self.identifyToMasterNode() uuid = self.identifyToMasterNode()
# do nothing # do nothing
conn = self.getFakeConnection(uuid, self.storage_address) conn = self.getFakeConnection(uuid, self.storage_address)
self.assertEquals(len(self.verification._uuid_dict), 0) self.assertEquals(len(self.verification._uuid_set), 0)
self.verification._uuid_dict[uuid] = True
self.assertEquals(len(self.verification._tid_set), 0) self.assertEquals(len(self.verification._tid_set), 0)
new_tid = self.getNextTID() new_tid = self.getNextTID()
verification.answerUnfinishedTransactions(conn, [new_tid]) verification.answerUnfinishedTransactions(conn, [new_tid])
self.assertEquals(len(self.verification._tid_set), 0) self.assertEquals(len(self.verification._tid_set), 0)
# update dict # update dict
conn = self.getFakeConnection(uuid, self.storage_address) conn = self.getFakeConnection(uuid, self.storage_address)
self.verification._uuid_dict[uuid] = False self.verification._uuid_set.add(uuid)
self.assertEquals(len(self.verification._tid_set), 0) self.assertEquals(len(self.verification._tid_set), 0)
new_tid = self.getNextTID(new_tid) new_tid = self.getNextTID(new_tid)
verification.answerUnfinishedTransactions(conn, [new_tid,]) verification.answerUnfinishedTransactions(conn, [new_tid,])
self.assertTrue(self.verification._uuid_dict[uuid]) self.assertTrue(uuid not in self.verification._uuid_set)
self.assertEquals(len(self.verification._tid_set), 1) self.assertEquals(len(self.verification._tid_set), 1)
self.assertTrue(new_tid in self.verification._tid_set) self.assertTrue(new_tid in self.verification._tid_set)
...@@ -144,8 +143,8 @@ class MasterVerificationTests(NeoTestBase): ...@@ -144,8 +143,8 @@ class MasterVerificationTests(NeoTestBase):
uuid = self.identifyToMasterNode() uuid = self.identifyToMasterNode()
# do nothing, as unfinished_oid_set is None # do nothing, as unfinished_oid_set is None
conn = self.getFakeConnection(uuid, self.storage_address) conn = self.getFakeConnection(uuid, self.storage_address)
self.assertEquals(len(self.verification._uuid_dict), 0) self.assertEquals(len(self.verification._uuid_set), 0)
self.verification._uuid_dict[uuid] = False self.verification._uuid_set.add(uuid)
self.verification._oid_set = None self.verification._oid_set = None
new_tid = self.getNextTID() new_tid = self.getNextTID()
new_oid = self.getOID(1) new_oid = self.getOID(1)
...@@ -154,8 +153,7 @@ class MasterVerificationTests(NeoTestBase): ...@@ -154,8 +153,7 @@ class MasterVerificationTests(NeoTestBase):
self.assertEquals(self.verification._oid_set, None) self.assertEquals(self.verification._oid_set, None)
# do nothing as asking_uuid_dict is True # do nothing as asking_uuid_dict is True
conn = self.getFakeConnection(uuid, self.storage_address) conn = self.getFakeConnection(uuid, self.storage_address)
self.assertEquals(len(self.verification._uuid_dict), 1) self.assertEquals(len(self.verification._uuid_set), 0)
self.verification._uuid_dict[uuid] = True
self.verification._oid_set = set() self.verification._oid_set = set()
self.assertEquals(len(self.verification._oid_set), 0) self.assertEquals(len(self.verification._oid_set), 0)
verification.answerTransactionInformation(conn, new_tid, verification.answerTransactionInformation(conn, new_tid,
...@@ -163,8 +161,8 @@ class MasterVerificationTests(NeoTestBase): ...@@ -163,8 +161,8 @@ class MasterVerificationTests(NeoTestBase):
self.assertEquals(len(self.verification._oid_set), 0) self.assertEquals(len(self.verification._oid_set), 0)
# do work # do work
conn = self.getFakeConnection(uuid, self.storage_address) conn = self.getFakeConnection(uuid, self.storage_address)
self.assertEquals(len(self.verification._uuid_dict), 1) self.assertEquals(len(self.verification._uuid_set), 0)
self.verification._uuid_dict[uuid] = False self.verification._uuid_set.add(uuid)
self.assertEquals(len(self.verification._oid_set), 0) self.assertEquals(len(self.verification._oid_set), 0)
verification.answerTransactionInformation(conn, new_tid, verification.answerTransactionInformation(conn, new_tid,
"user", "desc", "ext", False, [new_oid,]) "user", "desc", "ext", False, [new_oid,])
...@@ -172,8 +170,8 @@ class MasterVerificationTests(NeoTestBase): ...@@ -172,8 +170,8 @@ class MasterVerificationTests(NeoTestBase):
self.assertTrue(new_oid in self.verification._oid_set) self.assertTrue(new_oid in self.verification._oid_set)
# do not work as oid is diff # do not work as oid is diff
conn = self.getFakeConnection(uuid, self.storage_address) conn = self.getFakeConnection(uuid, self.storage_address)
self.assertEquals(len(self.verification._uuid_dict), 1) self.assertEquals(len(self.verification._uuid_set), 0)
self.verification._uuid_dict[uuid] = False self.verification._uuid_set.add(uuid)
self.assertEquals(len(self.verification._oid_set), 1) self.assertEquals(len(self.verification._oid_set), 1)
new_oid = self.getOID(2) new_oid = self.getOID(2)
verification.answerTransactionInformation(conn, new_tid, verification.answerTransactionInformation(conn, new_tid,
...@@ -185,15 +183,14 @@ class MasterVerificationTests(NeoTestBase): ...@@ -185,15 +183,14 @@ class MasterVerificationTests(NeoTestBase):
uuid = self.identifyToMasterNode() uuid = self.identifyToMasterNode()
# do nothing as asking_uuid_dict is True # do nothing as asking_uuid_dict is True
conn = self.getFakeConnection(uuid, self.storage_address) conn = self.getFakeConnection(uuid, self.storage_address)
self.assertEquals(len(self.verification._uuid_dict), 0) self.assertEquals(len(self.verification._uuid_set), 0)
self.verification._uuid_dict[uuid] = True
self.verification._oid_set = [] self.verification._oid_set = []
verification.tidNotFound(conn, "msg") verification.tidNotFound(conn, "msg")
self.assertNotEqual(self.verification._oid_set, None) self.assertNotEqual(self.verification._oid_set, None)
# do work as asking_uuid_dict is False # do work as asking_uuid_dict is False
conn = self.getFakeConnection(uuid, self.storage_address) conn = self.getFakeConnection(uuid, self.storage_address)
self.assertEquals(len(self.verification._uuid_dict), 1) self.assertEquals(len(self.verification._uuid_set), 0)
self.verification._uuid_dict[uuid] = False self.verification._uuid_set.add(uuid)
self.verification._oid_set = [] self.verification._oid_set = []
verification.tidNotFound(conn, "msg") verification.tidNotFound(conn, "msg")
self.assertEqual(self.verification._oid_set, None) self.assertEqual(self.verification._oid_set, None)
...@@ -205,35 +202,33 @@ class MasterVerificationTests(NeoTestBase): ...@@ -205,35 +202,33 @@ class MasterVerificationTests(NeoTestBase):
new_tid = self.getNextTID() new_tid = self.getNextTID()
new_oid = self.getOID(1) new_oid = self.getOID(1)
conn = self.getFakeConnection(uuid, self.storage_address) conn = self.getFakeConnection(uuid, self.storage_address)
self.assertEquals(len(self.verification._uuid_dict), 0) self.assertEquals(len(self.verification._uuid_set), 0)
self.verification._uuid_dict[uuid] = True
verification.answerObjectPresent(conn, new_oid, new_tid) verification.answerObjectPresent(conn, new_oid, new_tid)
# do work # do work
conn = self.getFakeConnection(uuid, self.storage_address) conn = self.getFakeConnection(uuid, self.storage_address)
self.assertEquals(len(self.verification._uuid_dict), 1) self.assertEquals(len(self.verification._uuid_set), 0)
self.verification._uuid_dict[uuid] = False self.verification._uuid_set.add(uuid)
verification.answerObjectPresent(conn, new_oid, new_tid) verification.answerObjectPresent(conn, new_oid, new_tid)
self.assertTrue(self.verification._uuid_dict[uuid]) self.assertTrue(uuid not in self.verification._uuid_set)
def test_15_oidNotFound(self): def test_15_oidNotFound(self):
verification = self.verification verification = self.verification
uuid = self.identifyToMasterNode() uuid = self.identifyToMasterNode()
# do nothing as asking_uuid_dict is True # do nothing as asking_uuid_dict is True
conn = self.getFakeConnection(uuid, self.storage_address) conn = self.getFakeConnection(uuid, self.storage_address)
self.assertEquals(len(self.verification._uuid_dict), 0) self.assertEquals(len(self.verification._uuid_set), 0)
self.verification._uuid_dict[uuid] = True
self.app.object_present = True self.app.object_present = True
self.assertTrue(self.app.object_present) self.assertTrue(self.app.object_present)
verification.oidNotFound(conn, "msg") verification.oidNotFound(conn, "msg")
self.assertTrue(self.app.object_present) self.assertTrue(self.app.object_present)
# do work as asking_uuid_dict is False # do work as asking_uuid_dict is False
conn = self.getFakeConnection(uuid, self.storage_address) conn = self.getFakeConnection(uuid, self.storage_address)
self.assertEquals(len(self.verification._uuid_dict), 1) self.assertEquals(len(self.verification._uuid_set), 0)
self.verification._uuid_dict[uuid] = False self.verification._uuid_set.add(uuid)
self.assertTrue(self.app.object_present) self.assertTrue(self.app.object_present)
verification.oidNotFound(conn, "msg") verification.oidNotFound(conn, "msg")
self.assertFalse(self.app.object_present) self.assertFalse(self.app.object_present)
self.assertTrue(self.verification._uuid_dict[uuid ]) self.assertTrue(uuid not in self.verification._uuid_set)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
......
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