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):
def getDatabase(self):
return self.__get('database')
def getWait(self):
return self.__get('wait')
def getDynamicMasterList(self):
return self.__get('dynamic_master_list', optional=True)
......
......@@ -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', '--dynamic-master-list', help='path of the file '
'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(
name = 'storage',
......@@ -61,6 +63,7 @@ def main(args=None):
database = options.database,
reset = options.reset,
adapter = options.adapter,
wait = options.wait,
)
config = ConfigurationManager(
defaults,
......
......@@ -49,7 +49,7 @@ class Application(object):
self.nm = NodeManager(config.getDynamicMasterList())
self.tm = TransactionManager(self)
self.dm = buildDatabaseManager(config.getAdapter(),
(config.getDatabase(), )
(config.getDatabase(), config.getWait())
)
# load master nodes
......
......@@ -121,8 +121,8 @@ def safeIter(func, *args, **kw):
class BTreeDatabaseManager(DatabaseManager):
def __init__(self, database):
super(BTreeDatabaseManager, self).__init__(database)
def __init__(self, database, wait):
super(BTreeDatabaseManager, self).__init__(database, wait)
self.setup(reset=1)
@property
......
......@@ -25,11 +25,12 @@ class CreationUndone(Exception):
class DatabaseManager(object):
"""This class only describes an interface for database managers."""
def __init__(self, database):
def __init__(self, database, wait):
"""
Initialize the object.
"""
self._under_transaction = False
self._wait = wait
self._parse(database)
def _parse(self, database):
......
......@@ -25,6 +25,7 @@ from array import array
from hashlib import sha1
import re
import string
import time
from . import DatabaseManager
from .manager import CreationUndone
......@@ -55,8 +56,8 @@ class MySQLDatabaseManager(DatabaseManager):
# (tested with testOudatedCellsOnDownStorage).
_use_partition = False
def __init__(self, database):
super(MySQLDatabaseManager, self).__init__(database)
def __init__(self, database, wait):
super(MySQLDatabaseManager, self).__init__(database, wait)
self.conn = None
self._config = {}
self._connect()
......@@ -79,7 +80,21 @@ class MySQLDatabaseManager(DatabaseManager):
neo.lib.logging.info(
'connecting to MySQL on the database %s with user %s',
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.query("SET SESSION group_concat_max_len = -1")
self.conn.set_sql_mode("TRADITIONAL,NO_ENGINE_SUBSTITUTION")
......
......@@ -118,7 +118,7 @@ class ReplicationTests(NeoUnitTestBase):
def buildStorage(self, transactions, objects, name='BTree', database=None):
def makeid(oid_or_tid):
return pack('!Q', oid_or_tid)
storage = buildDatabaseManager(name, (database, ))
storage = buildDatabaseManager(name, (database, 0))
storage.setup(reset=True)
storage.setNumPartitions(1)
storage._transactions = transactions
......
......@@ -24,7 +24,7 @@ class StorageBTreeTests(StorageDBTests):
def getDB(self, reset=0):
# db manager
db = BTreeDatabaseManager('')
db = BTreeDatabaseManager('', 0)
db.setup(reset)
return db
......
......@@ -31,7 +31,7 @@ class StorageMySQSLdbTests(StorageDBTests):
self.prepareDatabase(number=1, prefix=NEO_SQL_DATABASE[:-1])
# db manager
database = '%s@%s' % (NEO_SQL_USER, NEO_SQL_DATABASE)
db = MySQLDatabaseManager(database)
db = MySQLDatabaseManager(database, 0)
db.setup(reset)
return db
......@@ -41,7 +41,8 @@ class StorageMySQSLdbTests(StorageDBTests):
call.checkArgs('BEGIN')
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
self.assertEqual(db.db, NEO_SQL_DATABASE)
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