def acquireHostId(self, hostId, wait): self.log.info("Acquiring host id for domain %s (id=%s, wait=%s)", self._sdUUID, hostId, wait) # Ensure that future calls to acquire() will wait until host id is # acquired. self._ready.valid = True with self._lock: try: with utils.stopwatch("sanlock.add_lockspace"): sanlock.add_lockspace(self._lockspace_name, hostId, self._idsPath, wait=wait) except sanlock.SanlockException as e: if e.errno == errno.EINPROGRESS: # if the request is not asynchronous wait for the ongoing # lockspace operation to complete else silently continue, # the host id has been acquired or it's in the process of # being acquired (async). if wait: if not sanlock.inq_lockspace(self._lockspace_name, hostId, self._idsPath, wait=True): raise se.AcquireHostIdFailure(self._sdUUID, e) self.log.info( "Host id for domain %s successfully " "acquired (id=%s, wait=%s)", self._sdUUID, hostId, wait) self._ready.set() elif e.errno == errno.EEXIST: self.log.info( "Host id for domain %s already acquired " "(id=%s, wait=%s)", self._sdUUID, hostId, wait) self._ready.set() else: raise se.AcquireHostIdFailure(self._sdUUID, e) else: if wait: self.log.info( "Host id for domain %s successfully " "acquired (id=%s, wait=%s)", self._sdUUID, hostId, wait) self._ready.set()
def acquireHostId(self, hostId, async): with self._lock: self.log.info("Acquiring host id for domain %s (id: %s)", self._sdUUID, hostId) try: sanlock.add_lockspace(self._sdUUID, hostId, self._idsPath, async=async) except sanlock.SanlockException as e: if e.errno == errno.EINPROGRESS: # if the request is not asynchronous wait for the ongoing # lockspace operation to complete if not async and not sanlock.inq_lockspace( self._sdUUID, hostId, self._idsPath, wait=True): raise se.AcquireHostIdFailure(self._sdUUID, e) # else silently continue, the host id has been acquired # or it's in the process of being acquired (async) elif e.errno != errno.EEXIST: raise se.AcquireHostIdFailure(self._sdUUID, e)
def acquireHostId(self, hostId, wait): with self._globalLockMapSync: currentHostId, lockFile = self._getLease() if currentHostId is not None and currentHostId != hostId: self.log.error("Different host id already acquired (id: %s)", currentHostId) raise se.AcquireHostIdFailure(self._sdUUID) self._globalLockMap[self._sdUUID] = (hostId, lockFile) self.log.debug("Host id for domain %s successfully acquired (id: %s)", self._sdUUID, hostId)
def acquire(self, hostId, lease): self.log.info("Acquiring %s for host id %s", lease, hostId) # If host id was acquired by this thread, this will return immediately. # If host is id being acquired asynchronically by the domain monitor, # wait until the domain monitor find that host id was acquired. # # IMPORTANT: This must be done *before* entering the lock. Once we # enter the lock, the domain monitor cannot check if host id was # acquired, since hasHostId() is using the same lock. if not self._ready.wait(self.ACQUIRE_HOST_ID_TIMEOUT): raise se.AcquireHostIdFailure( "Timeout acquiring host id, cannot acquire %s (id=%s)" % (lease, hostId)) with self._lock, SANLock._sanlock_lock: while True: if SANLock._sanlock_fd is None: try: SANLock._sanlock_fd = sanlock.register() except sanlock.SanlockException as e: raise se.AcquireLockFailure( self._sdUUID, e.errno, "Cannot register to sanlock", str(e)) resource_name = lease.name.encode("utf-8") try: sanlock.acquire(self._lockspace_name, resource_name, [(lease.path, lease.offset)], slkfd=SANLock._sanlock_fd) except sanlock.SanlockException as e: if e.errno != errno.EPIPE: raise se.AcquireLockFailure( self._sdUUID, e.errno, "Cannot acquire %s" % (lease, ), str(e)) SANLock._sanlock_fd = None continue break self.log.info("Successfully acquired %s for host id %s", lease, hostId)
def acquire(self, hostId, lease, lvb=False): if lvb and not supports_lvb: raise se.UnsupportedOperation( "This sanlock version does not support LVB") self.log.info("Acquiring %s for host id %s, lvb=%s", lease, hostId, lvb) # If host id was acquired by this thread, this will return immediately. # If host is id being acquired asynchronically by the domain monitor, # wait until the domain monitor find that host id was acquired. # # IMPORTANT: This must be done *before* entering the lock. Once we # enter the lock, the domain monitor cannot check if host id was # acquired, since hasHostId() is using the same lock. if not self._ready.wait(self.ACQUIRE_HOST_ID_TIMEOUT): raise se.AcquireHostIdFailure( "Timeout acquiring host id, cannot acquire %s (id=%s)" % (lease, hostId)) with self._lock, SANLock._process_lock: while True: if SANLock._process_fd is None: try: SANLock._process_fd = sanlock.register() except sanlock.SanlockException as e: raise se.AcquireLockFailure( self._sdUUID, e.errno, "Cannot register to sanlock", str(e)) self.log.info("Using sanlock process fd %d", SANLock._process_fd) # TODO: remove once sanlock 3.8.3 is available on centos. extra_args = {"lvb": lvb} if supports_lvb else {} try: sanlock.acquire( self._lockspace_name, lease.name.encode("utf-8"), [(lease.path, lease.offset)], slkfd=SANLock._process_fd, **extra_args) except sanlock.SanlockException as e: if e.errno != errno.EPIPE: raise se.AcquireLockFailure( self._sdUUID, e.errno, "Cannot acquire %s" % (lease,), str(e)) # If we hold leases, we just lost them, since sanlock is # releasing all process leases when the process fd is # closed. The only way to recover is to panic; child # processes run by vdsm will be killed, and vdsm will lose # the SPM role. if SANLock._lease_count > 0: panic("Sanlock process fd was closed while " "holding {} leases: {}" .format(SANLock._lease_count, e)) self.log.warning("Sanlock process fd was closed: %s", e) SANLock._process_fd = None continue SANLock._lease_count += 1 break self.log.info("Successfully acquired %s for host id %s", lease, hostId)