def test_add_rem_lockspace(tmpdir, sanlock_daemon, size, offset): path = str(tmpdir.join("ls_name")) util.create_file(path, size) sanlock.write_lockspace("ls_name", path, offset=offset, iotimeout=1) # Since the lockspace is not acquired, we exepect to get False. acquired = sanlock.inq_lockspace("ls_name", 1, path, offset=offset, wait=False) assert acquired is False sanlock.add_lockspace("ls_name", 1, path, offset=offset, iotimeout=1) # Once the lockspace is acquired, we exepect to get True. acquired = sanlock.inq_lockspace("ls_name", 1, path, offset=offset, wait=False) assert acquired is True sanlock.rem_lockspace("ls_name", 1, path, offset=offset) # Once the lockspace is released, we exepect to get False. acquired = sanlock.inq_lockspace("ls_name", 1, path, offset=offset, wait=False) assert acquired is False
def test_write_lockspace_4k(user_4k_path, sanlock_daemon, align): # Poison lockspace area, ensuring that previous tests will not break this # test, and sanlock does not write beyond the lockspace area. with io.open(user_4k_path, "rb+") as f: f.write(align * b"x") util.write_guard(user_4k_path, align) sanlock.write_lockspace( "name", user_4k_path, iotimeout=1, align=align, sector=SECTOR_SIZE_4K) ls = sanlock.read_lockspace( user_4k_path, align=align, sector=SECTOR_SIZE_4K) assert ls == {"iotimeout": 1, "lockspace": b"name"} acquired = sanlock.inq_lockspace("name", 1, user_4k_path, wait=False) assert acquired is False # Verify that lockspace was written. with io.open(user_4k_path, "rb") as f: magic, = struct.unpack("< I", f.read(4)) assert magic == constants.DELTA_DISK_MAGIC # Check that sanlock did not write beyond the lockspace area. util.check_guard(user_4k_path, align)
def hasHostId(self, hostId): with self._lock: try: return sanlock.inq_lockspace(self._sdUUID, hostId, self._idsPath) except sanlock.SanlockException: self.log.debug("Unable to inquire sanlock lockspace " "status, returning False", exc_info=True) return False
def test_write_lockspace(tmpdir, sanlock_daemon, filename, encoding, size, offset): path = util.generate_path(tmpdir, filename, encoding) util.create_file(path, size) # Test read and write with default alignment and sector size values. sanlock.write_lockspace("name", path, offset=offset, iotimeout=1) ls = sanlock.read_lockspace(path, offset=offset) assert ls == {"iotimeout": 1, "lockspace": b"name"} # Test read and write with explicit alignment and sector size values. sanlock.write_lockspace( "name", path, offset=offset, iotimeout=1, align=ALIGNMENT_1M, sector=SECTOR_SIZE_512) ls = sanlock.read_lockspace( path, offset=offset, align=ALIGNMENT_1M, sector=SECTOR_SIZE_512) assert ls == {"iotimeout": 1, "lockspace": b"name"} acquired = sanlock.inq_lockspace( "name", 1, path, offset=offset, wait=False) assert acquired is False with io.open(path, "rb") as f: f.seek(offset) magic, = struct.unpack("< I", f.read(4)) assert magic == constants.DELTA_DISK_MAGIC # TODO: check more stuff here... util.check_guard(path, size)
def test_write_lockspace_4k(user_4k_path, sanlock_daemon, align): # Poison lockspace area, ensuring that previous tests will not break this # test, and sanlock does not write beyond the lockspace area. with io.open(user_4k_path, "rb+") as f: f.write(align * b"x") util.write_guard(user_4k_path, align) sanlock.write_lockspace(b"ls_name", user_4k_path, iotimeout=1, align=align, sector=SECTOR_SIZE_4K) ls = sanlock.read_lockspace(user_4k_path, align=align, sector=SECTOR_SIZE_4K) assert ls == {"iotimeout": 1, "lockspace": b"ls_name"} acquired = sanlock.inq_lockspace(b"ls_name", 1, user_4k_path, wait=False) assert acquired is False # Verify that lockspace was written. magic = util.read_magic(user_4k_path) assert magic == constants.DELTA_DISK_MAGIC # Check that sanlock did not write beyond the lockspace area. util.check_guard(user_4k_path, align)
def test_write_lockspace( tmpdir, sanlock_daemon, filename, encoding, size, offset): path = util.generate_path(tmpdir, filename, encoding) util.create_file(path, size) # Test read and write with default alignment and sector size values. sanlock.write_lockspace(b"ls_name", path, offset=offset, iotimeout=1) ls = sanlock.read_lockspace(path, offset=offset) assert ls == {"iotimeout": 1, "lockspace": b"ls_name"} # Test read and write with explicit alignment and sector size values. sanlock.write_lockspace( b"ls_name", path, offset=offset, iotimeout=1, align=ALIGNMENT_1M, sector=SECTOR_SIZE_512) ls = sanlock.read_lockspace( path, offset=offset, align=ALIGNMENT_1M, sector=SECTOR_SIZE_512) assert ls == {"iotimeout": 1, "lockspace": b"ls_name"} acquired = sanlock.inq_lockspace( b"ls_name", 1, path, offset=offset, wait=False) assert acquired is False magic = util.read_magic(path, offset) assert magic == constants.DELTA_DISK_MAGIC # TODO: check more stuff here... util.check_guard(path, size)
def test_add_rem_lockspace(tmpdir, sanlock_daemon, size, offset): path = str(tmpdir.join("ls_name")) util.create_file(path, size) sanlock.write_lockspace(b"ls_name", path, offset=offset, iotimeout=1) # Since the lockspace is not acquired, we exepect to get False. acquired = sanlock.inq_lockspace(b"ls_name", 1, path, offset=offset, wait=False) assert acquired is False sanlock.add_lockspace(b"ls_name", 1, path, offset=offset, iotimeout=1) # Once the lockspace is acquired, we exepect to get True. acquired = sanlock.inq_lockspace(b"ls_name", 1, path, offset=offset, wait=False) assert acquired is True lockspaces = sanlock.get_lockspaces() assert lockspaces == [{ 'flags': 0, 'host_id': 1, 'lockspace': b'ls_name', 'offset': offset, 'path': path }] sanlock.rem_lockspace(b"ls_name", 1, path, offset=offset) # Once the lockspace is released, we exepect to get False. acquired = sanlock.inq_lockspace(b"ls_name", 1, path, offset=offset, wait=False) assert acquired is False lockspaces = sanlock.get_lockspaces() assert lockspaces == []
def sanlock_acquire(hostId, lockspacePath, leasePath): sfd = sanlock.register() if not sanlock.inq_lockspace(LOCKSPACE_NAME, hostId, lockspacePath): msg = "Try to acquire host id %s:%s:%s:0" % (LOCKSPACE_NAME, hostId, lockspacePath) print(msg) sanlock.add_lockspace(LOCKSPACE_NAME, hostId, lockspacePath) msg = "Try to acquire leader lease:%s" % str(leasePath) print(msg) sanlock.acquire(LOCKSPACE_NAME, RESOURCE_NAME, [(leasePath, 0)], sfd)
def test_add_rem_lockspace_async(tmpdir, sanlock_daemon): path = str(tmpdir.join("ls_name")) util.create_file(path, MiB) sanlock.write_lockspace(b"ls_name", path, iotimeout=1) acquired = sanlock.inq_lockspace(b"ls_name", 1, path, wait=False) assert acquired is False # This will take 3 seconds. sanlock.add_lockspace(b"ls_name", 1, path, iotimeout=1, wait=False) # While the lockspace is being aquired, we expect to get None. time.sleep(1) acquired = sanlock.inq_lockspace(b"ls_name", 1, path, wait=False) assert acquired is None # Once the lockspace is acquired, we exepect to get True. acquired = sanlock.inq_lockspace(b"ls_name", 1, path, wait=True) assert acquired is True # This will take about 3 seconds. sanlock.rem_lockspace(b"ls_name", 1, path, wait=False) # Wait until the lockspace change state from True to None. while sanlock.inq_lockspace(b"ls_name", 1, path, wait=False): time.sleep(1) # While the lockspace is being released, we expect to get None. acquired = sanlock.inq_lockspace(b"ls_name", 1, path, wait=False) assert acquired is None # Once the lockspace was released, we expect to get False. acquired = sanlock.inq_lockspace(b"ls_name", 1, path, wait=True) assert acquired is False
def test_add_rem_lockspace_async(tmpdir, sanlock_daemon): path = str(tmpdir.join("ls_name")) util.create_file(path, MiB) sanlock.write_lockspace("ls_name", path, iotimeout=1) acquired = sanlock.inq_lockspace("ls_name", 1, path, wait=False) assert acquired is False # This will take 3 seconds. sanlock.add_lockspace("ls_name", 1, path, iotimeout=1, **{"async": True}) # While the lockspace is being aquired, we expect to get None. time.sleep(1) acquired = sanlock.inq_lockspace("ls_name", 1, path, wait=False) assert acquired is None # Once the lockspace is acquired, we exepect to get True. acquired = sanlock.inq_lockspace("ls_name", 1, path, wait=True) assert acquired is True # This will take about 3 seconds. sanlock.rem_lockspace("ls_name", 1, path, **{"async": True}) # Wait until the lockspace change state from True to None. while sanlock.inq_lockspace("ls_name", 1, path, wait=False): time.sleep(1) # While the lockspace is being released, we expect to get None. acquired = sanlock.inq_lockspace("ls_name", 1, path, wait=False) assert acquired is None # Once the lockspace was released, we expect to get False. acquired = sanlock.inq_lockspace("ls_name", 1, path, wait=True) assert acquired is False
def test_add_rem_lockspace(tmpdir, sanlock_daemon, size, offset): path = str(tmpdir.join("ls_name")) util.create_file(path, size) sanlock.write_lockspace("ls_name", path, offset=offset, iotimeout=1) # Since the lockspace is not acquired, we exepect to get False. acquired = sanlock.inq_lockspace( "ls_name", 1, path, offset=offset, wait=False) assert acquired is False sanlock.add_lockspace("ls_name", 1, path, offset=offset, iotimeout=1) # Once the lockspace is acquired, we exepect to get True. acquired = sanlock.inq_lockspace( "ls_name", 1, path, offset=offset, wait=False) assert acquired is True lockspaces = sanlock.get_lockspaces() assert lockspaces == [{ 'flags': 0, 'host_id': 1, 'lockspace': b'ls_name', 'offset': offset, 'path': path }] sanlock.rem_lockspace("ls_name", 1, path, offset=offset) # Once the lockspace is released, we exepect to get False. acquired = sanlock.inq_lockspace( "ls_name", 1, path, offset=offset, wait=False) assert acquired is False lockspaces = sanlock.get_lockspaces() assert lockspaces == []
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): 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: self._start_add_lockspace() try: sanlock.add_lockspace( self._lockspace_name, hostId, self._idsPath, iotimeout=self._io_timeout, 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._end_add_lockspace(hostId) self._ready.set() elif e.errno == errno.EEXIST: self.log.info("Host id %s for domain %s already acquired", hostId, self._sdUUID) self._cancel_add_lockspace() self._ready.set() else: self._cancel_add_lockspace() raise se.AcquireHostIdFailure(self._sdUUID, e) else: if wait: self._end_add_lockspace(hostId) 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 test_write_lockspace(tmpdir, sanlock_daemon, size, offset): path = str(tmpdir.join("lockspace")) util.create_file(path, size) # test read and write with default alignment and sector size values sanlock.write_lockspace("name", path, offset=offset, iotimeout=1) ls = sanlock.read_lockspace(path, offset=offset) assert ls == {"iotimeout": 1, "lockspace": "name"} # test read and write with explicit alignment and sector size values sanlock.write_lockspace("name", path, offset=offset, iotimeout=1, align=ALIGNMENT_1M, sector=SECTOR_SIZE_512) ls = sanlock.read_lockspace(path, offset=offset, align=ALIGNMENT_1M, sector=SECTOR_SIZE_512) assert ls == {"iotimeout": 1, "lockspace": "name"} acquired = sanlock.inq_lockspace("name", 1, path, offset=offset, wait=False) assert acquired is False with io.open(path, "rb") as f: f.seek(offset) magic, = struct.unpack("< I", f.read(4)) assert magic == constants.DELTA_DISK_MAGIC # TODO: check more stuff here... util.check_guard(path, size)
def hasHostId(self, hostId): with self._lock: try: has_host_id = sanlock.inq_lockspace( self._lockspace_name, hostId, self._idsPath) except sanlock.SanlockException: self.log.debug("Unable to inquire sanlock lockspace " "status, returning False", exc_info=True) return False if has_host_id: # Host id was acquired. Wake up threads waiting in acquire(). self._ready.set() self._end_add_lockspace(hostId) else: if self._ready.valid and self._ready.is_set(): # Host id was released by sanlock. This happens after # renewal failure when storage is inaccessible. self.log.warning("Host id %s for domain %s was released", hostId, self._sdUUID) self._ready.clear() return has_host_id
def test_inq_lockspace_parse_args(no_sanlock_daemon, name): with raises_sanlock_errno(): sanlock.inq_lockspace(name, 1, "path", wait=False)
def _inquire_whiteboard_lock(self): return sanlock.inq_lockspace(broker_constants.LOCKSPACE_NAME.encode(), self.host_id, self._lease_file)
def test_inq_lockspace_parse_args(no_sanlock_daemon, name, filename, encoding): path = util.generate_path("/tmp/", filename, encoding) with raises_sanlock_errno(): sanlock.inq_lockspace(name, 1, path, wait=False)
def _inquire_whiteboard_lock(self): return sanlock.inq_lockspace(broker_constants.LOCKSPACE_NAME, self.host_id, self._lease_file)