Package ganeti :: Package utils :: Module livelock
[hide private]
[frames] | no frames]

Source Code for Module ganeti.utils.livelock

  1  # 
  2  # 
  3   
  4  # Copyright (C) 2014 Google Inc. 
  5  # All rights reserved. 
  6  # 
  7  # Redistribution and use in source and binary forms, with or without 
  8  # modification, are permitted provided that the following conditions are 
  9  # met: 
 10  # 
 11  # 1. Redistributions of source code must retain the above copyright notice, 
 12  # this list of conditions and the following disclaimer. 
 13  # 
 14  # 2. Redistributions in binary form must reproduce the above copyright 
 15  # notice, this list of conditions and the following disclaimer in the 
 16  # documentation and/or other materials provided with the distribution. 
 17  # 
 18  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 
 19  # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
 20  # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 21  # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
 22  # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 23  # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 24  # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 25  # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 26  # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 27  # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 28  # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 29   
 30  """Lockfiles to prove liveliness 
 31   
 32  When requesting resources, like locks, from wconfd, requesters have 
 33  to provide the name of a file they own an exclusive lock on, to prove 
 34  that they are still alive. Provide methods to obtain such a file. 
 35  """ 
 36   
 37  import fcntl 
 38  import os 
 39  import struct 
 40  import time 
 41   
 42  from ganeti.utils.algo import NiceSort 
 43  from ganeti import pathutils 
 44   
 45   
46 -class LiveLockName(object):
47 - def __init__(self, name):
48 self._name = name
49
50 - def GetPath(self):
51 return self._name
52
53 - def close(self):
54 """Clean up the lockfile. 55 56 """ 57 os.remove(self._name)
58
59 - def __str__(self):
60 return "LiveLockName(" + self.GetPath() + ")"
61 62
63 -class LiveLock(object):
64 """Utility for a lockfile needed to request resources from WconfD. 65 66 """
67 - def __init__(self, name=None):
68 if name is None: 69 name = "pid%d_" % os.getpid() 70 # to avoid reusing existing lock files, extend name 71 # by the current time 72 name = "%s_%d" % (name, int(time.time())) 73 fname = os.path.join(pathutils.LIVELOCK_DIR, name) 74 self.lockfile = open(fname, 'w') 75 fcntl.fcntl(self.lockfile, fcntl.F_SETLKW, 76 struct.pack('hhllhh', fcntl.F_WRLCK, 0, 0, 0, 0, 0))
77
78 - def GetPath(self):
79 return self.lockfile.name
80
81 - def close(self):
82 """Close the lockfile and clean it up. 83 84 """ 85 self.lockfile.close() 86 os.remove(self.lockfile.name)
87
88 - def __str__(self):
89 return "LiveLock(" + self.GetPath() + ")"
90 91
92 -def GuessLockfileFor(name):
93 """For a given name, take the latest file matching. 94 95 @return: the file with the latest name matching the given 96 prefix in LIVELOCK_DIR, or the plain name, if none 97 exists. 98 """ 99 lockfiles = filter(lambda n: n.startswith(name), 100 os.listdir(pathutils.LIVELOCK_DIR)) 101 if len(lockfiles) > 0: 102 lockfile = NiceSort(lockfiles)[-1] 103 else: 104 lockfile = name 105 106 return os.path.join(pathutils.LIVELOCK_DIR, lockfile)
107