Commit 4e885787 authored by Vincent Pelletier's avatar Vincent Pelletier

Define accessors for App's tid, conflict_serial, txn_finished and txn_voted...

Define accessors for App's tid, conflict_serial, txn_finished and txn_voted (all in one commit since they are closely related in usage).
Use those accessors instead of accessing properties directly.
Update tests.


git-svn-id: https://svn.erp5.org/repos/neo/branches/prototype3@362 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 680e33ae
...@@ -100,19 +100,21 @@ class Storage(BaseStorage.BaseStorage, ...@@ -100,19 +100,21 @@ class Storage(BaseStorage.BaseStorage,
data = data, version = version, data = data, version = version,
transaction = transaction) transaction = transaction)
except NEOStorageConflictError: except NEOStorageConflictError:
if app.conflict_serial <= app.tid: conflict_serial = app.getConflictSerial()
tid = app.getTID()
if conflict_serial <= tid:
# Try to resolve conflict only if conflicting serial is older # Try to resolve conflict only if conflicting serial is older
# than the current transaction ID # than the current transaction ID
new_data = self.tryToResolveConflict(oid, new_data = self.tryToResolveConflict(oid,
app.conflict_serial, conflict_serial,
serial, data) serial, data)
if new_data is not None: if new_data is not None:
# Try again after conflict resolution # Try again after conflict resolution
self.store(oid, app.conflict_serial, self.store(oid, conflict_serial,
new_data, version, transaction) new_data, version, transaction)
return ConflictResolution.ResolvedSerial return ConflictResolution.ResolvedSerial
raise POSException.ConflictError(oid=oid, raise POSException.ConflictError(oid=oid,
serials=(app.tid, serials=(tid,
serial),data=data) serial),data=data)
def _clear_temp(self): def _clear_temp(self):
......
...@@ -212,8 +212,8 @@ class Application(object): ...@@ -212,8 +212,8 @@ class Application(object):
self.txn = None self.txn = None
self.txn_data_dict = {} self.txn_data_dict = {}
self.txn_object_stored = 0 self.txn_object_stored = 0
self.txn_voted = 0 self.txn_voted = False
self.txn_finished = 0 self.txn_finished = False
# Internal attribute distinct between thread # Internal attribute distinct between thread
self.local_var = local() self.local_var = local()
# Lock definition : # Lock definition :
...@@ -554,12 +554,12 @@ class Application(object): ...@@ -554,12 +554,12 @@ class Application(object):
conn.addPacket(p) conn.addPacket(p)
conn.expectMessage(msg_id) conn.expectMessage(msg_id)
self.dispatcher.register(conn, msg_id, self.getQueue()) self.dispatcher.register(conn, msg_id, self.getQueue())
self.txn_voted = 0 self.txn_voted = False
finally: finally:
conn.unlock() conn.unlock()
self._waitMessage(conn, msg_id) self._waitMessage(conn, msg_id)
if self.txn_voted != 1: if not self.isTransactionVoted():
raise NEOStorageError('tpc_vote failed') raise NEOStorageError('tpc_vote failed')
def _clear_txn(self): def _clear_txn(self):
...@@ -567,8 +567,8 @@ class Application(object): ...@@ -567,8 +567,8 @@ class Application(object):
self.tid = None self.tid = None
self.txn = None self.txn = None
self.txn_data_dict.clear() self.txn_data_dict.clear()
self.txn_voted = 0 self.txn_voted = False
self.txn_finished = 0 self.txn_finished = False
def tpc_abort(self, transaction): def tpc_abort(self, transaction):
"""Abort current transaction.""" """Abort current transaction."""
...@@ -651,7 +651,7 @@ class Application(object): ...@@ -651,7 +651,7 @@ class Application(object):
# Wait for answer # Wait for answer
self._waitMessage(conn, msg_id) self._waitMessage(conn, msg_id)
if self.txn_finished != 1: if not self.isTransactionFinished():
raise NEOStorageError('tpc_finish failed') raise NEOStorageError('tpc_finish failed')
# Update cache # Update cache
...@@ -1000,3 +1000,24 @@ class Application(object): ...@@ -1000,3 +1000,24 @@ class Application(object):
def isNodeReady(self): def isNodeReady(self):
return self.local_var.node_ready return self.local_var.node_ready
def setTID(self, value):
self.tid = value
def getTID(self):
return self.tid
def getConflictSerial(self):
return self.conflict_serial
def setTransactionFinished(self):
self.txn_finished = True
def isTransactionFinished(self):
return self.txn_finished
def setTransactionVoted(self):
self.txn_voted = True
def isTransactionVoted(self):
return self.txn_voted
...@@ -401,7 +401,7 @@ class ClientAnswerEventHandler(BaseClientEventHandler): ...@@ -401,7 +401,7 @@ class ClientAnswerEventHandler(BaseClientEventHandler):
def handleAnswerNewTID(self, conn, packet, tid): def handleAnswerNewTID(self, conn, packet, tid):
app = self.app app = self.app
app.tid = tid app.setTID(tid)
def handleAnswerNewOIDs(self, conn, packet, oid_list): def handleAnswerNewOIDs(self, conn, packet, oid_list):
app = self.app app = self.app
...@@ -410,11 +410,8 @@ class ClientAnswerEventHandler(BaseClientEventHandler): ...@@ -410,11 +410,8 @@ class ClientAnswerEventHandler(BaseClientEventHandler):
def handleNotifyTransactionFinished(self, conn, packet, tid): def handleNotifyTransactionFinished(self, conn, packet, tid):
app = self.app app = self.app
if tid != app.tid: if tid == app.getTID():
app.txn_finished = -1 app.setTransactionFinished()
else:
app.txn_finished = 1
# Storage node handler # Storage node handler
def handleAnswerObject(self, conn, packet, oid, start_serial, end_serial, compression, def handleAnswerObject(self, conn, packet, oid, start_serial, end_serial, compression,
...@@ -432,7 +429,7 @@ class ClientAnswerEventHandler(BaseClientEventHandler): ...@@ -432,7 +429,7 @@ class ClientAnswerEventHandler(BaseClientEventHandler):
def handleAnswerStoreTransaction(self, conn, packet, tid): def handleAnswerStoreTransaction(self, conn, packet, tid):
app = self.app app = self.app
app.txn_voted = 1 app.setTransactionVoted()
def handleAnswerTransactionInformation(self, conn, packet, tid, def handleAnswerTransactionInformation(self, conn, packet, tid,
user, desc, ext, oid_list): user, desc, ext, oid_list):
......
...@@ -830,26 +830,24 @@ class ClientEventHandlerTest(unittest.TestCase): ...@@ -830,26 +830,24 @@ class ClientEventHandlerTest(unittest.TestCase):
self.assertEqual(state, test_cell_list[0][2]) self.assertEqual(state, test_cell_list[0][2])
def test_AnswerNewTID(self): def test_AnswerNewTID(self):
class App: app = Mock({'setTID': None})
tid = None
app = App()
dispatcher = self.getDispatcher() dispatcher = self.getDispatcher()
client_handler = ClientAnswerEventHandler(app, dispatcher) client_handler = ClientAnswerEventHandler(app, dispatcher)
conn = self.getConnection() conn = self.getConnection()
test_tid = 1 test_tid = 1
client_handler.handleAnswerNewTID(conn, None, test_tid) client_handler.handleAnswerNewTID(conn, None, test_tid)
self.assertEquals(app.tid, test_tid) setTID_call_list = app.mockGetNamedCalls('setTID')
self.assertEquals(len(setTID_call_list), 1)
self.assertEquals(setTID_call_list[0].getParam(0), test_tid)
def test_NotifyTransactionFinished(self): def test_NotifyTransactionFinished(self):
class App: test_tid = 1
tid = 1 app = Mock({'getTID': test_tid, 'setTransactionFinished': None})
txn_finished = None
app = App()
dispatcher = self.getDispatcher() dispatcher = self.getDispatcher()
client_handler = ClientAnswerEventHandler(app, dispatcher) client_handler = ClientAnswerEventHandler(app, dispatcher)
conn = self.getConnection() conn = self.getConnection()
client_handler.handleNotifyTransactionFinished(conn, None, 1) client_handler.handleNotifyTransactionFinished(conn, None, test_tid)
self.assertEquals(app.txn_finished, 1) self.assertEquals(len(app.mockGetNamedCalls('setTransactionFinished')), 1)
# TODO: decide what to do when non-current transaction is notified as finished, and test that behaviour # TODO: decide what to do when non-current transaction is notified as finished, and test that behaviour
def test_InvalidateObjects(self): def test_InvalidateObjects(self):
...@@ -949,16 +947,13 @@ class ClientEventHandlerTest(unittest.TestCase): ...@@ -949,16 +947,13 @@ class ClientEventHandlerTest(unittest.TestCase):
self.assertEqual(app.txn_object_stored, (test_oid, test_serial)) self.assertEqual(app.txn_object_stored, (test_oid, test_serial))
def test_AnswerStoreTransaction(self): def test_AnswerStoreTransaction(self):
class App: test_tid = 10
tid = 10 app = Mock({'getTID': test_tid, 'setTransactionVoted': None})
txn_voted = 0
app = App()
dispatcher = self.getDispatcher() dispatcher = self.getDispatcher()
client_handler = ClientAnswerEventHandler(app, dispatcher) client_handler = ClientAnswerEventHandler(app, dispatcher)
conn = self.getConnection() conn = self.getConnection()
test_tid = 10
client_handler.handleAnswerStoreTransaction(conn, None, test_tid) client_handler.handleAnswerStoreTransaction(conn, None, test_tid)
self.assertEquals(app.txn_voted, 1) self.assertEquals(len(app.mockGetNamedCalls('setTransactionVoted')), 1)
# TODO: test handleAnswerObject with test_tid not matching app.tid (not handled in program) # TODO: test handleAnswerObject with test_tid not matching app.tid (not handled in program)
def test_AnswerTransactionInformation(self): def test_AnswerTransactionInformation(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