Commit 50d99997 authored by Vincent Pelletier's avatar Vincent Pelletier

Add -w/--wait storage parameter, to wait for backend to be available.

parent 6f2ee50e
...@@ -59,6 +59,9 @@ class ConfigurationManager(object): ...@@ -59,6 +59,9 @@ class ConfigurationManager(object):
def getDatabase(self): def getDatabase(self):
return self.__get('database') return self.__get('database')
def getWait(self):
return self.__get('wait')
def getDynamicMasterList(self): def getDynamicMasterList(self):
return self.__get('dynamic_master_list', optional=True) return self.__get('dynamic_master_list', optional=True)
......
...@@ -42,6 +42,8 @@ parser.add_option('-a', '--adapter', help = 'database adapter to use') ...@@ -42,6 +42,8 @@ parser.add_option('-a', '--adapter', help = 'database adapter to use')
parser.add_option('-d', '--database', help = 'database connections string') parser.add_option('-d', '--database', help = 'database connections string')
parser.add_option('-D', '--dynamic-master-list', help='path of the file ' parser.add_option('-D', '--dynamic-master-list', help='path of the file '
'containing dynamic master node list') 'containing dynamic master node list')
parser.add_option('-w', '--wait', help='seconds to wait for backend to be '
'available, before erroring-out (-1 = infinite)', type='float', default=0)
defaults = dict( defaults = dict(
name = 'storage', name = 'storage',
...@@ -61,6 +63,7 @@ def main(args=None): ...@@ -61,6 +63,7 @@ def main(args=None):
database = options.database, database = options.database,
reset = options.reset, reset = options.reset,
adapter = options.adapter, adapter = options.adapter,
wait = options.wait,
) )
config = ConfigurationManager( config = ConfigurationManager(
defaults, defaults,
......
...@@ -49,7 +49,7 @@ class Application(object): ...@@ -49,7 +49,7 @@ class Application(object):
self.nm = NodeManager(config.getDynamicMasterList()) self.nm = NodeManager(config.getDynamicMasterList())
self.tm = TransactionManager(self) self.tm = TransactionManager(self)
self.dm = buildDatabaseManager(config.getAdapter(), self.dm = buildDatabaseManager(config.getAdapter(),
(config.getDatabase(), ) (config.getDatabase(), config.getWait())
) )
# load master nodes # load master nodes
......
...@@ -121,8 +121,8 @@ def safeIter(func, *args, **kw): ...@@ -121,8 +121,8 @@ def safeIter(func, *args, **kw):
class BTreeDatabaseManager(DatabaseManager): class BTreeDatabaseManager(DatabaseManager):
def __init__(self, database): def __init__(self, database, wait):
super(BTreeDatabaseManager, self).__init__(database) super(BTreeDatabaseManager, self).__init__(database, wait)
self.setup(reset=1) self.setup(reset=1)
@property @property
......
...@@ -25,11 +25,12 @@ class CreationUndone(Exception): ...@@ -25,11 +25,12 @@ class CreationUndone(Exception):
class DatabaseManager(object): class DatabaseManager(object):
"""This class only describes an interface for database managers.""" """This class only describes an interface for database managers."""
def __init__(self, database): def __init__(self, database, wait):
""" """
Initialize the object. Initialize the object.
""" """
self._under_transaction = False self._under_transaction = False
self._wait = wait
self._parse(database) self._parse(database)
def _parse(self, database): def _parse(self, database):
......
...@@ -25,6 +25,7 @@ from array import array ...@@ -25,6 +25,7 @@ from array import array
from hashlib import sha1 from hashlib import sha1
import re import re
import string import string
import time
from . import DatabaseManager from . import DatabaseManager
from .manager import CreationUndone from .manager import CreationUndone
...@@ -55,8 +56,8 @@ class MySQLDatabaseManager(DatabaseManager): ...@@ -55,8 +56,8 @@ class MySQLDatabaseManager(DatabaseManager):
# (tested with testOudatedCellsOnDownStorage). # (tested with testOudatedCellsOnDownStorage).
_use_partition = False _use_partition = False
def __init__(self, database): def __init__(self, database, wait):
super(MySQLDatabaseManager, self).__init__(database) super(MySQLDatabaseManager, self).__init__(database, wait)
self.conn = None self.conn = None
self._config = {} self._config = {}
self._connect() self._connect()
...@@ -79,7 +80,21 @@ class MySQLDatabaseManager(DatabaseManager): ...@@ -79,7 +80,21 @@ class MySQLDatabaseManager(DatabaseManager):
neo.lib.logging.info( neo.lib.logging.info(
'connecting to MySQL on the database %s with user %s', 'connecting to MySQL on the database %s with user %s',
self.db, self.user) self.db, self.user)
self.conn = MySQLdb.connect(**kwd) if self._wait < 0:
timeout_at = None
else:
timeout_at = time.time() + self._wait
while True:
try:
self.conn = MySQLdb.connect(**kwd)
except Exception:
if timeout_at is not None and time.time() >= timeout_at:
raise
neo.lib.logging.exception('Connection to MySQL failed, '
'retrying.')
time.sleep(1)
else:
break
self.conn.autocommit(False) self.conn.autocommit(False)
self.conn.query("SET SESSION group_concat_max_len = -1") self.conn.query("SET SESSION group_concat_max_len = -1")
self.conn.set_sql_mode("TRADITIONAL,NO_ENGINE_SUBSTITUTION") self.conn.set_sql_mode("TRADITIONAL,NO_ENGINE_SUBSTITUTION")
......
...@@ -118,7 +118,7 @@ class ReplicationTests(NeoUnitTestBase): ...@@ -118,7 +118,7 @@ class ReplicationTests(NeoUnitTestBase):
def buildStorage(self, transactions, objects, name='BTree', database=None): def buildStorage(self, transactions, objects, name='BTree', database=None):
def makeid(oid_or_tid): def makeid(oid_or_tid):
return pack('!Q', oid_or_tid) return pack('!Q', oid_or_tid)
storage = buildDatabaseManager(name, (database, )) storage = buildDatabaseManager(name, (database, 0))
storage.setup(reset=True) storage.setup(reset=True)
storage.setNumPartitions(1) storage.setNumPartitions(1)
storage._transactions = transactions storage._transactions = transactions
......
...@@ -24,7 +24,7 @@ class StorageBTreeTests(StorageDBTests): ...@@ -24,7 +24,7 @@ class StorageBTreeTests(StorageDBTests):
def getDB(self, reset=0): def getDB(self, reset=0):
# db manager # db manager
db = BTreeDatabaseManager('') db = BTreeDatabaseManager('', 0)
db.setup(reset) db.setup(reset)
return db return db
......
...@@ -31,7 +31,7 @@ class StorageMySQSLdbTests(StorageDBTests): ...@@ -31,7 +31,7 @@ class StorageMySQSLdbTests(StorageDBTests):
self.prepareDatabase(number=1, prefix=NEO_SQL_DATABASE[:-1]) self.prepareDatabase(number=1, prefix=NEO_SQL_DATABASE[:-1])
# db manager # db manager
database = '%s@%s' % (NEO_SQL_USER, NEO_SQL_DATABASE) database = '%s@%s' % (NEO_SQL_USER, NEO_SQL_DATABASE)
db = MySQLDatabaseManager(database) db = MySQLDatabaseManager(database, 0)
db.setup(reset) db.setup(reset)
return db return db
...@@ -41,7 +41,8 @@ class StorageMySQSLdbTests(StorageDBTests): ...@@ -41,7 +41,8 @@ class StorageMySQSLdbTests(StorageDBTests):
call.checkArgs('BEGIN') call.checkArgs('BEGIN')
def test_MySQLDatabaseManagerInit(self): def test_MySQLDatabaseManagerInit(self):
db = MySQLDatabaseManager('%s@%s' % (NEO_SQL_USER, NEO_SQL_DATABASE)) db = MySQLDatabaseManager('%s@%s' % (NEO_SQL_USER, NEO_SQL_DATABASE),
0)
# init # init
self.assertEqual(db.db, NEO_SQL_DATABASE) self.assertEqual(db.db, NEO_SQL_DATABASE)
self.assertEqual(db.user, NEO_SQL_USER) self.assertEqual(db.user, NEO_SQL_USER)
......
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