Commit d4fa5c23 authored by Iustin Pop's avatar Iustin Pop
Browse files

Remove the old locking functions

This removes (hopefully) all traces of the old locking functions and
uses.

Reviewed-by: imsnah
parent 2467e0d3
......@@ -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__":
......
......@@ -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)
......
......@@ -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.
......
......@@ -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"""
......
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