Commit 521d36d3 authored by Grégory Wisniewski's avatar Grégory Wisniewski

Fix load/store of None configuration values with database manager.

- Store None values as NULL column values
- Raise KeyError if the configuration entry is not found

git-svn-id: https://svn.erp5.org/repos/neo/trunk@1993 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 7a736ff7
......@@ -87,34 +87,46 @@ class Application(object):
"""Load persistent configuration data from the database.
If data is not present, generate it."""
def NoneOnKeyError(getter):
try:
return getter()
except KeyError:
return None
dm = self.dm
self.uuid = dm.getUUID()
num_partitions = dm.getNumPartitions()
num_replicas = dm.getNumReplicas()
# check cluster name
try:
if dm.getName() != self.name:
raise RuntimeError('name does not match with the database')
except KeyError:
dm.setName(self.name)
# load configuration
self.uuid = NoneOnKeyError(dm.getUUID)
num_partitions = NoneOnKeyError(dm.getNumPartitions)
num_replicas = NoneOnKeyError(dm.getNumReplicas)
ptid = NoneOnKeyError(dm.getPTID)
# check partition table configuration
if num_partitions is not None and num_replicas is not None:
if num_partitions <= 0:
raise RuntimeError, 'partitions must be more than zero'
# create a partition table
self.pt = PartitionTable(num_partitions, num_replicas)
name = dm.getName()
if name is None:
dm.setName(self.name)
elif name != self.name:
raise RuntimeError('name does not match with the database')
ptid = dm.getPTID()
logging.info('Configuration loaded:')
logging.info('UUID : %s', dump(self.uuid))
logging.info('PTID : %s', dump(ptid))
logging.info('Name : %s', name)
logging.info('Name : %s', self.name)
logging.info('Partitions: %s', num_partitions)
logging.info('Replicas : %s', num_replicas)
def loadPartitionTable(self):
"""Load a partition table from the database."""
try:
ptid = self.dm.getPTID()
except KeyError:
ptid = None
cell_list = self.dm.getPartitionTable()
new_cell_list = []
for offset, uuid, state in cell_list:
......
......@@ -135,7 +135,7 @@ class MySQLDatabaseManager(DatabaseManager):
# persistent data.
q("""CREATE TABLE IF NOT EXISTS config (
name VARBINARY(16) NOT NULL PRIMARY KEY,
value VARBINARY(255) NOT NULL
value VARBINARY(255) NULL
) ENGINE = InnoDB""")
# The table "pt" stores a partition table.
......@@ -191,18 +191,20 @@ class MySQLDatabaseManager(DatabaseManager):
q = self.query
e = self.escape
key = e(str(key))
r = q("""SELECT value FROM config WHERE name = '%s'""" % key)
try:
return r[0][0]
return q("SELECT value FROM config WHERE name = '%s'" % key)[0][0]
except IndexError:
return None
raise KeyError, key
def _setConfiguration(self, key, value):
q = self.query
e = self.escape
key = e(str(key))
value = e(str(value))
q("""REPLACE INTO config VALUES ('%s', '%s')""" % (key, value))
if value is None:
value = 'NULL'
else:
value = "'%s'" % (e(str(value)), )
q("""REPLACE INTO config VALUES ('%s', %s)""" % (key, value))
def getPartitionTable(self):
q = self.query
......
......@@ -27,8 +27,14 @@ class VerificationHandler(BaseMasterHandler):
def askLastIDs(self, conn):
app = self.app
try:
oid = app.dm.getLastOID()
except KeyError:
oid = None
try:
tid = app.dm.getLastTID()
except KeyError:
tid = None
conn.answer(Packets.AnswerLastIDs(oid, tid, app.pt.getID()))
def askPartitionTable(self, conn, offset_list):
......
......@@ -151,9 +151,8 @@ class StorageMySQSLdbTests(NeoTestBase):
def test_10_getConfiguration(self):
# check if a configuration entry is well read
self.db.setup()
result = self.db.getConfiguration('a')
# doesn't exists, None expected
self.assertEquals(result, None)
# doesn't exists, raise
self.assertRaises(KeyError, self.db.getConfiguration, 'a')
self.db.query("insert into config values ('a', 'b');")
result = self.db.getConfiguration('a')
# exists, check result
......@@ -169,7 +168,7 @@ class StorageMySQSLdbTests(NeoTestBase):
def checkConfigEntry(self, get_call, set_call, value):
# generic test for all configuration entries accessors
self.db.setup()
self.assertEquals(get_call(), None)
self.assertRaises(KeyError, get_call)
set_call(value)
self.assertEquals(get_call(), value)
set_call(value * 2)
......@@ -194,13 +193,10 @@ class StorageMySQSLdbTests(NeoTestBase):
value='TEST_NAME')
def test_15_PTID(self):
test = '\x01' * 8
self.db.setup()
self.assertEquals(self.db.getPTID(), None)
self.db.setPTID(test)
self.assertEquals(self.db.getPTID(), test)
self.db.setPTID(test * 2)
self.assertEquals(self.db.getPTID(), test * 2)
self.checkConfigEntry(
get_call=self.db.getPTID,
set_call=self.db.setPTID,
value=self.getPTID(1))
def test_16_getPartitionTable(self):
# insert an entry and check it
......
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