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

Store the PTID in memory as an integer.

git-svn-id: https://svn.erp5.org/repos/neo/trunk@2309 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 061d2e99
...@@ -23,14 +23,14 @@ class PartitionTable(neo.pt.PartitionTable): ...@@ -23,14 +23,14 @@ class PartitionTable(neo.pt.PartitionTable):
"""This class manages a partition table for the primary master node""" """This class manages a partition table for the primary master node"""
def setID(self, id): def setID(self, id):
self.id = id assert isinstance(id, (int, long)) or id is None, id
self._id = id
def setNextID(self): def setNextID(self):
if self.id is None: if self._id is None:
raise RuntimeError, 'I do not know the last Partition Table ID' raise RuntimeError, 'I do not know the last Partition Table ID'
last_id = unpack('!Q', self.id)[0] self._id += 1
self.id = pack('!Q', last_id + 1) return self._id
return self.id
def getPartition(self, oid_or_tid): def getPartition(self, oid_or_tid):
return unpack('!Q', oid_or_tid)[0] % self.getPartitions() return unpack('!Q', oid_or_tid)[0] % self.getPartitions()
...@@ -38,7 +38,7 @@ class PartitionTable(neo.pt.PartitionTable): ...@@ -38,7 +38,7 @@ class PartitionTable(neo.pt.PartitionTable):
def make(self, node_list): def make(self, node_list):
"""Make a new partition table from scratch.""" """Make a new partition table from scratch."""
# start with the first PTID # start with the first PTID
self.id = pack('!Q', 1) self._id = 1
# First, filter the list of nodes. # First, filter the list of nodes.
node_list = [n for n in node_list if n.isRunning() \ node_list = [n for n in node_list if n.isRunning() \
and n.getUUID() is not None] and n.getUUID() is not None]
...@@ -115,7 +115,7 @@ class PartitionTable(neo.pt.PartitionTable): ...@@ -115,7 +115,7 @@ class PartitionTable(neo.pt.PartitionTable):
raise IndexError, offset raise IndexError, offset
# store the partition table # store the partition table
self.clear() self.clear()
self.id = ptid self._id = ptid
new_nodes = [] new_nodes = []
for offset, row in row_list: for offset, row in row_list:
for uuid, state in row: for uuid, state in row:
......
...@@ -202,12 +202,13 @@ def _encodeUUID(uuid): ...@@ -202,12 +202,13 @@ def _encodeUUID(uuid):
def _decodePTID(ptid): def _decodePTID(ptid):
if ptid == INVALID_PTID: if ptid == INVALID_PTID:
return None return None
return ptid return unpack('!Q', ptid)[0]
def _encodePTID(ptid): def _encodePTID(ptid):
if ptid is None: if ptid is None:
return INVALID_PTID return INVALID_PTID
return ptid assert isinstance(ptid, (int, long)), ptid
return pack('!Q', ptid)
def _decodeTID(tid): def _decodeTID(tid):
if tid == INVALID_TID: if tid == INVALID_TID:
......
...@@ -70,7 +70,7 @@ class PartitionTable(object): ...@@ -70,7 +70,7 @@ class PartitionTable(object):
"""This class manages a partition table.""" """This class manages a partition table."""
def __init__(self, num_partitions, num_replicas): def __init__(self, num_partitions, num_replicas):
self.id = None self._id = None
self.np = num_partitions self.np = num_partitions
self.nr = num_replicas self.nr = num_replicas
self.num_filled_rows = 0 self.num_filled_rows = 0
...@@ -81,7 +81,7 @@ class PartitionTable(object): ...@@ -81,7 +81,7 @@ class PartitionTable(object):
self.count_dict = {} self.count_dict = {}
def getID(self): def getID(self):
return self.id return self._id
def getPartitions(self): def getPartitions(self):
return self.np return self.np
...@@ -91,7 +91,7 @@ class PartitionTable(object): ...@@ -91,7 +91,7 @@ class PartitionTable(object):
def clear(self): def clear(self):
"""Forget an existing partition table.""" """Forget an existing partition table."""
self.id = None self._id = None
self.num_filled_rows = 0 self.num_filled_rows = 0
# Note: don't use [[]] * self.np construct, as it duplicates # Note: don't use [[]] * self.np construct, as it duplicates
# instance *references*, so the outer list contains really just one # instance *references*, so the outer list contains really just one
...@@ -201,7 +201,7 @@ class PartitionTable(object): ...@@ -201,7 +201,7 @@ class PartitionTable(object):
content. content.
""" """
self.clear() self.clear()
self.id = ptid self._id = ptid
for offset, row in row_list: for offset, row in row_list:
if offset >= self.getPartitions(): if offset >= self.getPartitions():
raise IndexError raise IndexError
...@@ -219,10 +219,10 @@ class PartitionTable(object): ...@@ -219,10 +219,10 @@ class PartitionTable(object):
if the partition table ID is not greater than the current one. If a node if the partition table ID is not greater than the current one. If a node
is not known, it is created in the node manager and set as unavailable is not known, it is created in the node manager and set as unavailable
""" """
if ptid <= self.id: if ptid <= self._id:
logging.warning('ignoring older partition changes') logging.warning('ignoring older partition changes')
return return
self.id = ptid self._id = ptid
for offset, uuid, state in cell_list: for offset, uuid, state in cell_list:
node = nm.getByUUID(uuid) node = nm.getByUUID(uuid)
assert node is not None, 'No node found for uuid %r' % (dump(uuid), ) assert node is not None, 'No node found for uuid %r' % (dump(uuid), )
......
...@@ -149,13 +149,16 @@ class DatabaseManager(object): ...@@ -149,13 +149,16 @@ class DatabaseManager(object):
""" """
Load a Partition Table ID from a database. Load a Partition Table ID from a database.
""" """
return util.bin(self.getConfiguration('ptid')) return long(self.getConfiguration('ptid'))
def setPTID(self, ptid): def setPTID(self, ptid):
""" """
Store a Partition Table ID into a database. Store a Partition Table ID into a database.
""" """
self.setConfiguration('ptid', util.dump(ptid)) if ptid is not None:
assert isinstance(ptid, (int, long)), ptid
ptid = str(ptid)
self.setConfiguration('ptid', ptid)
def getLastOID(self): def getLastOID(self):
""" """
......
...@@ -132,10 +132,10 @@ class NeoTestBase(unittest.TestCase): ...@@ -132,10 +132,10 @@ class NeoTestBase(unittest.TestCase):
return tid return tid
def getPTID(self, i=None): def getPTID(self, i=None):
""" Return a 8-bytes PTID """ """ Return an integer PTID """
if i is None: if i is None:
return os.urandom(8) return os.unpack('!Q', os.urandom(8))[0]
return pack('!Q', i) return i
def getOID(self, i=None): def getOID(self, i=None):
""" Return a 8-bytes OID """ """ Return a 8-bytes OID """
......
...@@ -30,7 +30,7 @@ class MasterClientHandlerTests(NeoTestBase): ...@@ -30,7 +30,7 @@ class MasterClientHandlerTests(NeoTestBase):
config = self.getMasterConfiguration(master_number=1, replicas=1) config = self.getMasterConfiguration(master_number=1, replicas=1)
self.app = Application(config) self.app = Application(config)
self.app.pt.clear() self.app.pt.clear()
self.app.pt.setID(pack('!Q', 1)) self.app.pt.setID(1)
self.app.em = Mock() self.app.em = Mock()
self.app.loid = '\0' * 8 self.app.loid = '\0' * 8
self.app.tm.setLastTID('\0' * 8) self.app.tm.setLastTID('\0' * 8)
......
...@@ -32,7 +32,6 @@ class MasterStorageHandlerTests(NeoTestBase): ...@@ -32,7 +32,6 @@ class MasterStorageHandlerTests(NeoTestBase):
config = self.getMasterConfiguration(master_number=1, replicas=1) config = self.getMasterConfiguration(master_number=1, replicas=1)
self.app = Application(config) self.app = Application(config)
self.app.pt.clear() self.app.pt.clear()
self.app.pt.setID(pack('!Q', 1))
self.app.em = Mock() self.app.em = Mock()
self.service = StorageServiceHandler(self.app) self.service = StorageServiceHandler(self.app)
self.client_handler = ClientServiceHandler(self.app) self.client_handler = ClientServiceHandler(self.app)
......
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