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):
handler.connectionCompleted(conn)
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):
if self.loid is None:
raise RuntimeError, 'I do not know the last OID'
......
......@@ -21,7 +21,7 @@ from neo import protocol
from neo.protocol import HIDDEN_STATE
from neo.master.handlers import BaseServiceHandler
from neo.protocol import UnexpectedPacketError
from neo.util import dump
from neo.util import dump, getNextTID
class FinishingTransaction(object):
"""This class describes a finishing transaction."""
......@@ -87,7 +87,7 @@ class ClientServiceHandler(BaseServiceHandler):
raise protocol.ProtocolError('invalid TID requested')
if tid is None:
# give a new transaction ID
tid = app.getNextTID()
tid = getNextTID(app.ltid)
app.ltid = tid
app.finishing_transaction_dict[tid] = FinishingTransaction(conn)
conn.answer(protocol.answerBeginTransaction(tid), packet.getId())
......
......@@ -20,6 +20,7 @@ from mock import Mock
from neo import protocol
from neo.protocol import *
from neo.tests import NeoTestBase
from neo.util import getNextTID
from time import time, gmtime
class ProtocolTests(NeoTestBase):
......@@ -31,28 +32,8 @@ class ProtocolTests(NeoTestBase):
pass
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
self.ltid = getNextTID(self.ltid)
return self.ltid
def test_01_Packet_init(self):
p = Packet(msg_type=ASK_PRIMARY_MASTER, body=None)
......
......@@ -18,6 +18,7 @@
from zlib import adler32
from struct import pack, unpack
from time import time, gmtime
def u64(s):
return unpack('!Q', s)[0]
......@@ -52,3 +53,27 @@ def bin(s):
def makeChecksum(s):
"""Return a 4-byte integer checksum against a string."""
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