diff --git a/daemons/ganeti-masterd b/daemons/ganeti-masterd index 80380cf0740aa8177afeb80ed18412bc2cb5fb77..5c8d11692d075f75f95fe1790fe945a859a5e7d9 100755 --- a/daemons/ganeti-masterd +++ b/daemons/ganeti-masterd @@ -393,24 +393,13 @@ def main(): logger.SetupDaemon(constants.LOG_MASTERDAEMON, debug=options.debug) - logger.Info("ganeti master daemon startup") + logging.info("ganeti master daemon startup") + master.setup_queue() try: - utils.Lock('cmd', debug=options.debug) - except errors.LockError, err: - print >> sys.stderr, str(err) - master.server_cleanup() - return - - try: - master.setup_queue() - try: - master.serve_forever() - finally: - master.server_cleanup() + master.serve_forever() finally: - utils.Unlock('cmd') - utils.LockCleanup() + master.server_cleanup() if __name__ == "__main__": diff --git a/lib/cmdlib.py b/lib/cmdlib.py index a9f091c2c94964ba67ea666a3df7c99e07efce19..8c8f4f7c3473f4ab6d92ece0c3b61df66f8dc136 100644 --- a/lib/cmdlib.py +++ b/lib/cmdlib.py @@ -1093,15 +1093,7 @@ def _WaitForSync(cfgw, instance, proc, oneshot=False, unlock=False): if done or oneshot: break - if unlock: - #utils.Unlock('cmd') - pass - try: - time.sleep(min(60, max_time)) - finally: - if unlock: - #utils.Lock('cmd') - pass + time.sleep(min(60, max_time)) if done: proc.LogInfo("Instance %s's disks are in sync." % instance.name) diff --git a/lib/utils.py b/lib/utils.py index ade6b9b5420eb6b279fa29e73ea8aaf4ddd4bd55..731752821f41138631eaace0d8994df45d1d2ce7 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -101,113 +101,6 @@ class RunResult(object): output = property(_GetOutput, None, None, "Return full output") -def _GetLockFile(subsystem): - """Compute the file name for a given lock name.""" - return "%s/ganeti_lock_%s" % (constants.LOCK_DIR, subsystem) - - -def Lock(name, max_retries=None, debug=False): - """Lock a given subsystem. - - In case the lock is already held by an alive process, the function - will sleep indefintely and poll with a one second interval. - - When the optional integer argument 'max_retries' is passed with a - non-zero value, the function will sleep only for this number of - times, and then it will will raise a LockError if the lock can't be - acquired. Passing in a negative number will cause only one try to - get the lock. Passing a positive number will make the function retry - for approximately that number of seconds. - - """ - lockfile = _GetLockFile(name) - - if name in _locksheld: - raise errors.LockError('Lock "%s" already held!' % (name,)) - - errcount = 0 - - retries = 0 - while True: - try: - fd = os.open(lockfile, os.O_CREAT | os.O_EXCL | os.O_RDWR | os.O_SYNC) - break - except OSError, creat_err: - if creat_err.errno != errno.EEXIST: - raise errors.LockError("Can't create the lock file. Error '%s'." % - str(creat_err)) - - try: - pf = open(lockfile, 'r') - except IOError, open_err: - errcount += 1 - if errcount >= 5: - raise errors.LockError("Lock file exists but cannot be opened." - " Error: '%s'." % str(open_err)) - time.sleep(1) - continue - - try: - pid = int(pf.read()) - except ValueError: - raise errors.LockError("Invalid pid string in %s" % - (lockfile,)) - - if not IsProcessAlive(pid): - raise errors.LockError("Stale lockfile %s for pid %d?" % - (lockfile, pid)) - - if max_retries and max_retries <= retries: - raise errors.LockError("Can't acquire lock during the specified" - " time, aborting.") - if retries == 5 and (debug or sys.stdin.isatty()): - logger.ToStderr("Waiting for '%s' lock from pid %d..." % (name, pid)) - - time.sleep(1) - retries += 1 - continue - - os.write(fd, '%d\n' % (os.getpid(),)) - os.close(fd) - - _locksheld.append(name) - - -def Unlock(name): - """Unlock a given subsystem. - - """ - lockfile = _GetLockFile(name) - - try: - fd = os.open(lockfile, os.O_RDONLY) - except OSError: - raise errors.LockError('Lock "%s" not held.' % (name,)) - - f = os.fdopen(fd, 'r') - pid_str = f.read() - - try: - pid = int(pid_str) - except ValueError: - raise errors.LockError('Unable to determine PID of locking process.') - - if pid != os.getpid(): - raise errors.LockError('Lock not held by me (%d != %d)' % - (os.getpid(), pid,)) - - os.unlink(lockfile) - _locksheld.remove(name) - - -def LockCleanup(): - """Remove all locks. - - """ - for lock in _locksheld: - Unlock(lock) - - def RunCmd(cmd): """Execute a (shell) command. @@ -281,30 +174,6 @@ def RunCmd(cmd): return RunResult(exitcode, signal, out, err, strcmd) -def RunCmdUnlocked(cmd): - """Execute a shell command without the 'cmd' lock. - - This variant of `RunCmd()` drops the 'cmd' lock before running the - command and re-aquires it afterwards, thus it can be used to call - other ganeti commands. - - The argument and return values are the same as for the `RunCmd()` - function. - - Args: - cmd - command to run. (str) - - Returns: - `RunResult` - - """ - Unlock('cmd') - ret = RunCmd(cmd) - Lock('cmd') - - return ret - - def RemoveFile(filename): """Remove a file ignoring some errors. diff --git a/test/ganeti.utils_unittest.py b/test/ganeti.utils_unittest.py index 1d9bd2f1a57c2655f092bb15e1ddf54ffdca1d9a..175c319f3da292e4cccd576c30014a285882a5a4 100755 --- a/test/ganeti.utils_unittest.py +++ b/test/ganeti.utils_unittest.py @@ -99,46 +99,6 @@ class TestIsProcessAlive(unittest.TestCase): "noexisting process detected") -class TestLocking(unittest.TestCase): - """Testing case for the Lock/Unlock functions""" - - def setUp(self): - lock_dir = tempfile.mkdtemp(prefix="ganeti.unittest.", - suffix=".locking") - self.old_lock_dir = constants.LOCK_DIR - constants.LOCK_DIR = lock_dir - - def tearDown(self): - try: - ganeti.utils.Unlock("unittest") - except LockError: - pass - shutil.rmtree(constants.LOCK_DIR, ignore_errors=True) - constants.LOCK_DIR = self.old_lock_dir - - def clean_lock(self, name): - try: - ganeti.utils.Unlock("unittest") - except LockError: - pass - - - def testLock(self): - self.clean_lock("unittest") - self.assertEqual(None, Lock("unittest")) - - - def testUnlock(self): - self.clean_lock("unittest") - ganeti.utils.Lock("unittest") - self.assertEqual(None, Unlock("unittest")) - - def testDoubleLock(self): - self.clean_lock("unittest") - ganeti.utils.Lock("unittest") - self.assertRaises(LockError, Lock, "unittest") - - class TestRunCmd(unittest.TestCase): """Testing case for the RunCmd function"""