Commit 5be77f53 authored by Grégory Wisniewski's avatar Grégory Wisniewski

Remove duplicate computation of next TID, from protocol tests and master

application.


git-svn-id: https://svn.erp5.org/repos/neo/branches/prototype3@1285 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 731d875d
...@@ -708,30 +708,6 @@ class Application(object): ...@@ -708,30 +708,6 @@ class Application(object):
handler.connectionCompleted(conn) handler.connectionCompleted(conn)
self.cluster_state = state self.cluster_state = state
def getNextTID(self):
tm = time()
gmt = gmtime(tm)
upper = ((((gmt.tm_year - 1900) * 12 + gmt.tm_mon - 1) * 31 \
+ gmt.tm_mday - 1) * 24 + gmt.tm_hour) * 60 + gmt.tm_min
lower = int((gmt.tm_sec % 60 + (tm - int(tm))) / (60.0 / 65536.0 / 65536.0))
tid = pack('!LL', upper, lower)
if tid <= self.ltid:
upper, lower = unpack('!LL', self.ltid)
if lower == 0xffffffff:
# This should not happen usually.
from datetime import timedelta, datetime
d = datetime(gmt.tm_year, gmt.tm_mon, gmt.tm_mday,
gmt.tm_hour, gmt.tm_min) \
+ timedelta(0, 60)
upper = ((((d.year - 1900) * 12 + d.month - 1) * 31 \
+ d.day - 1) * 24 + d.hour) * 60 + d.minute
lower = 0
else:
lower += 1
tid = pack('!LL', upper, lower)
self.ltid = tid
return tid
def getNewOIDList(self, num_oids): def getNewOIDList(self, num_oids):
if self.loid is None: if self.loid is None:
raise RuntimeError, 'I do not know the last OID' raise RuntimeError, 'I do not know the last OID'
......
...@@ -21,7 +21,7 @@ from neo import protocol ...@@ -21,7 +21,7 @@ from neo import protocol
from neo.protocol import HIDDEN_STATE from neo.protocol import HIDDEN_STATE
from neo.master.handlers import BaseServiceHandler from neo.master.handlers import BaseServiceHandler
from neo.protocol import UnexpectedPacketError from neo.protocol import UnexpectedPacketError
from neo.util import dump from neo.util import dump, getNextTID
class FinishingTransaction(object): class FinishingTransaction(object):
"""This class describes a finishing transaction.""" """This class describes a finishing transaction."""
...@@ -87,7 +87,7 @@ class ClientServiceHandler(BaseServiceHandler): ...@@ -87,7 +87,7 @@ class ClientServiceHandler(BaseServiceHandler):
raise protocol.ProtocolError('invalid TID requested') raise protocol.ProtocolError('invalid TID requested')
if tid is None: if tid is None:
# give a new transaction ID # give a new transaction ID
tid = app.getNextTID() tid = getNextTID(app.ltid)
app.ltid = tid app.ltid = tid
app.finishing_transaction_dict[tid] = FinishingTransaction(conn) app.finishing_transaction_dict[tid] = FinishingTransaction(conn)
conn.answer(protocol.answerBeginTransaction(tid), packet.getId()) conn.answer(protocol.answerBeginTransaction(tid), packet.getId())
......
...@@ -20,6 +20,7 @@ from mock import Mock ...@@ -20,6 +20,7 @@ from mock import Mock
from neo import protocol from neo import protocol
from neo.protocol import * from neo.protocol import *
from neo.tests import NeoTestBase from neo.tests import NeoTestBase
from neo.util import getNextTID
from time import time, gmtime from time import time, gmtime
class ProtocolTests(NeoTestBase): class ProtocolTests(NeoTestBase):
...@@ -31,28 +32,8 @@ class ProtocolTests(NeoTestBase): ...@@ -31,28 +32,8 @@ class ProtocolTests(NeoTestBase):
pass pass
def getNextTID(self): def getNextTID(self):
tm = time() self.ltid = getNextTID(self.ltid)
gmt = gmtime(tm) return self.ltid
upper = ((((gmt.tm_year - 1900) * 12 + gmt.tm_mon - 1) * 31 \
+ gmt.tm_mday - 1) * 24 + gmt.tm_hour) * 60 + gmt.tm_min
lower = int((gmt.tm_sec % 60 + (tm - int(tm))) / (60.0 / 65536.0 / 65536.0))
tid = pack('!LL', upper, lower)
if tid <= self.ltid:
upper, lower = unpack('!LL', self.ltid)
if lower == 0xffffffff:
# This should not happen usually.
from datetime import timedelta, datetime
d = datetime(gmt.tm_year, gmt.tm_mon, gmt.tm_mday,
gmt.tm_hour, gmt.tm_min) \
+ timedelta(0, 60)
upper = ((((d.year - 1900) * 12 + d.month - 1) * 31 \
+ d.day - 1) * 24 + d.hour) * 60 + d.minute
lower = 0
else:
lower += 1
tid = pack('!LL', upper, lower)
self.ltid = tid
return tid
def test_01_Packet_init(self): def test_01_Packet_init(self):
p = Packet(msg_type=ASK_PRIMARY_MASTER, body=None) p = Packet(msg_type=ASK_PRIMARY_MASTER, body=None)
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
from zlib import adler32 from zlib import adler32
from struct import pack, unpack from struct import pack, unpack
from time import time, gmtime
def u64(s): def u64(s):
return unpack('!Q', s)[0] return unpack('!Q', s)[0]
...@@ -52,3 +53,27 @@ def bin(s): ...@@ -52,3 +53,27 @@ def bin(s):
def makeChecksum(s): def makeChecksum(s):
"""Return a 4-byte integer checksum against a string.""" """Return a 4-byte integer checksum against a string."""
return adler32(s) & 0xffffffff return adler32(s) & 0xffffffff
def getNextTID(ltid):
""" Compute the next TID based on the current time and check collisions """
tm = time()
gmt = gmtime(tm)
upper = ((((gmt.tm_year - 1900) * 12 + gmt.tm_mon - 1) * 31 \
+ gmt.tm_mday - 1) * 24 + gmt.tm_hour) * 60 + gmt.tm_min
lower = int((gmt.tm_sec % 60 + (tm - int(tm))) / (60.0 / 65536.0 / 65536.0))
tid = pack('!LL', upper, lower)
if tid <= ltid:
upper, lower = unpack('!LL', ltid)
if lower == 0xffffffff:
# This should not happen usually.
from datetime import timedelta, datetime
d = datetime(gmt.tm_year, gmt.tm_mon, gmt.tm_mday,
gmt.tm_hour, gmt.tm_min) \
+ timedelta(0, 60)
upper = ((((d.year - 1900) * 12 + d.month - 1) * 31 \
+ d.day - 1) * 24 + d.hour) * 60 + d.minute
lower = 0
else:
lower += 1
tid = pack('!LL', upper, lower)
return tid
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