def acquire(self, hostId): with self._globalLockMapSync: self.log.info("Acquiring local lock for domain %s (id: %s)", self._sdUUID, hostId) lockFile = self._globalLockMap.get(self._sdUUID, None) if lockFile: try: misc.NoIntrCall(fcntl.fcntl, lockFile, fcntl.F_GETFD) except IOError as e: # We found a stale file descriptor, removing. del self._globalLockMap[self._sdUUID] # Raise any other unkown error. if e.errno != os.errno.EBADF: raise else: self.log.debug( "Local lock already acquired for domain " "%s (id: %s)", self._sdUUID, hostId) return # success, the lock was already acquired lockFile = misc.NoIntrCall(os.open, self._idsPath, os.O_RDONLY) try: misc.NoIntrCall(fcntl.flock, lockFile, fcntl.LOCK_EX | fcntl.LOCK_NB) except IOError as e: misc.NoIntrCall(os.close, lockFile) if e.errno in (os.errno.EACCES, os.errno.EAGAIN): raise se.AcquireLockFailure(self._sdUUID, e.errno, "Cannot acquire local lock", str(e)) raise else: self._globalLockMap[self._sdUUID] = lockFile self.log.debug( "Local lock for domain %s successfully acquired " "(id: %s)", self._sdUUID, hostId)
def release(self): with self._globalLockMapSync: self.log.info("Releasing local lock for domain %s", self._sdUUID) lockFile = self._globalLockMap.get(self._sdUUID, None) if not lockFile: self.log.debug("Local lock already released for domain %s", self._sdUUID) return misc.NoIntrCall(os.close, lockFile) del self._globalLockMap[self._sdUUID] self.log.debug("Local lock for domain %s successfully released", self._sdUUID)