def save(volpath, kv_dict): """ Save the dictionary to side car. """ meta_file = lib.DiskLib_SidecarMakeFileName(volpath.encode(), DVOL_KEY.encode()) kv_str = json.dumps(kv_dict) retry_count = 0 vol_name = vmdk_utils.get_volname_from_vmdk_path(volpath) while True: try: with open(meta_file, "w") as fh: fh.write(align_str(kv_str, KV_ALIGN)) break except IOError as open_error: # This is a workaround to the timing/locking with metadata files issue #626 if open_error.errno == errno.EBUSY and retry_count <= vmdk_utils.VMDK_RETRY_COUNT: logging.warning("Meta file %s busy for save(), retrying...", meta_file) vmdk_utils.log_volume_lsof(vol_name) retry_count += 1 time.sleep(vmdk_utils.VMDK_RETRY_SLEEP) else: logging.exception("Failed to save meta-data for %s", volpath) return False return True
def load(volpath): """ Load and return dictionary from the sidecar """ meta_file = lib.DiskLib_SidecarMakeFileName(volpath.encode(), DVOL_KEY.encode()) retry_count = 0 vol_name = vmdk_utils.get_volname_from_vmdk_path(volpath) while True: try: with open(meta_file, "r") as fh: kv_str = fh.read() break except IOError as open_error: # This is a workaround to the timing/locking with metadata files issue #626 if open_error.errno == errno.EBUSY and retry_count <= vmdk_utils.VMDK_RETRY_COUNT: logging.warning("Meta file %s busy for load(), retrying...", meta_file) vmdk_utils.log_volume_lsof(vol_name) retry_count += 1 time.sleep(vmdk_utils.VMDK_RETRY_SLEEP) else: logging.exception("Failed to access %s", meta_file) return None try: return json.loads(kv_str) except ValueError: logging.exception("load:Failed to decode meta-data for %s", volpath) return None
def save(volpath, kv_dict, key=None, value=None): """ Save the dictionary to side car. """ vol_type = get_vol_type(volpath) if not vol_type: logging.warning("KV delete - could not determine type of volume %s", volpath) return False if vol_type == c_uint32(KV_VOL_VIRTUAL).value: meta_file = lib.DiskLib_SidecarMakeFileName(volpath.encode(), DVOL_KEY.encode()) else: meta_file = get_kv_filename(volpath) if not meta_file: return False kv_str = json.dumps(kv_dict) retry_count = 0 vol_name = vmdk_utils.get_volname_from_vmdk_path(volpath) while True: try: if key: with open(meta_file, "r") as rfh: kv_val = rfh.read() try: kv_match = json.loads(kv_val) except ValueError: logging.exception("load:Failed to decode meta-data for %s", volpath) return False if key in kv_match and kv_match[key] != value: return False with open(meta_file, "w") as fh: fh.write(align_str(kv_str, KV_ALIGN)) break except IOError as open_error: # This is a workaround to the timing/locking with metadata files issue #626 if open_error.errno == errno.EBUSY and retry_count <= vmdk_utils.VMDK_RETRY_COUNT: logging.warning("Meta file %s busy for save(), retrying...", meta_file) vmdk_utils.log_volume_lsof(vol_name) retry_count += 1 time.sleep(vmdk_utils.VMDK_RETRY_SLEEP) else: logging.exception("Failed to save meta-data for %s", volpath) return False return True
def save(volpath, kv_dict, key=None, value=None): """ Save the dictionary to side car. """ meta_file = lib.DiskLib_SidecarMakeFileName(volpath.encode(), DVOL_KEY.encode()) kv_str = json.dumps(kv_dict) retry_count = 0 vol_name = vmdk_utils.get_volname_from_vmdk_path(volpath) while True: try: if key: with open(meta_file, "r") as rfh: kv_val = rfh.read() try: kv_match = json.loads(kv_val) except ValueError: logging.exception("load:Failed to decode meta-data for %s", volpath) return False if key in kv_match and kv_match[key] != value: return False with open(meta_file, "w") as fh: fh.write(align_str(kv_str, KV_ALIGN)) break except IOError as open_error: # This is a workaround to the timing/locking with metadata files issue #626 if open_error.errno == errno.EBUSY and retry_count <= vmdk_utils.VMDK_RETRY_COUNT: logging.warning("Meta file %s busy for save(), retrying...", meta_file) vmdk_utils.log_volume_lsof(vol_name) retry_count += 1 time.sleep(vmdk_utils.VMDK_RETRY_SLEEP) else: logging.exception("Failed to save meta-data for %s", volpath) return False return True