Skip to content

Commit 8dcaabb

Browse files
committed
util: support sharded csiDirectory in tracevol
Update tracevol.py to resolve volume and snapshot request-name mappings from the hashed csiDirectory shard oid, with fallback to the legacy unsharded oid. Also add an instanceid option so non-default ceph-csi instances can be traced. Signed-off-by: Yite Gu <guyite@bytedance.com>
1 parent 32842dd commit 8dcaabb

1 file changed

Lines changed: 38 additions & 30 deletions

File tree

troubleshooting/tools/tracevol.py

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import prettytable
4444
PARSER = argparse.ArgumentParser()
4545
ARGS=None # just so that the linter is happy, ARGS is global already.
46+
DEFAULT_CSI_DIRECTORY_SHARDS = 11
4647

4748
# -p pvc-test -k /home/.kube/config -n default -rn rook-ceph
4849
PARSER.add_argument("-p", "--pvcname", default="", help="PVC name")
@@ -66,6 +67,8 @@
6667
help="configmap name which holds the cephcsi configuration")
6768
PARSER.add_argument("-cmn", "--configmapnamespace", default="default",
6869
help="namespace where configmap exists")
70+
PARSER.add_argument("--instanceid", default="default",
71+
help="ceph-csi instance ID")
6972

7073

7174
def list_pvc_vol_name_mapping(arg):
@@ -187,34 +190,11 @@ def check_pv_name_in_rados(arg, image_id, pvc_name, pool_name, is_rbd):
187190
validate pvc information in rados
188191
"""
189192
omapkey = f'csi.volume.{pvc_name}'
190-
cmd = ['rados', 'getomapval', 'csi.volumes.default',
191-
omapkey, "--pool", pool_name]
192-
if not arg.userkey:
193-
cmd += ["--id", arg.userid, "--key", arg.userkey]
194-
if not is_rbd:
195-
cmd += ["--namespace", "csi"]
196-
if arg.toolboxdeployed is True:
197-
kube = get_cmd_prefix(arg)
198-
cmd = kube + cmd
199-
with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) as out:
200-
stdout, stderr = out.communicate()
201-
202-
if stderr is not None:
203-
return False
204-
name = b''
205-
lines = [x.strip() for x in stdout.split(b"\n")]
206-
for line in lines:
207-
if b' ' not in line:
208-
continue
209-
if b'value' in line and b'bytes' in line:
210-
continue
211-
part = re.findall(br'[A-Za-z0-9\-]+', line)
212-
if part:
213-
name += part[-1]
214-
if name.decode() != image_id:
193+
omapobj = f'csi.volumes.{arg.instanceid}'
194+
name = get_val_from_csi_directory(omapobj, omapkey, pool_name, is_rbd)
195+
if name != image_id:
215196
if arg.debug:
216-
decoded_name = name.decode()
217-
print(f"expected image Id {image_id} found Id in rados {decoded_name}")
197+
print(f"expected image Id {image_id} found Id in rados {name}")
218198
return False
219199
return True
220200

@@ -643,15 +623,43 @@ def get_val_from_omap(obj, key, pool, is_rbd, regex=br'[A-Za-z0-9\-]+'):
643623

644624
return result.decode()
645625

626+
def fnv32a(key):
627+
"""
628+
Returns the 32-bit FNV-1a hash for a string.
629+
"""
630+
hval = 0x811c9dc5
631+
for byte in key.encode():
632+
hval ^= byte
633+
hval = (hval * 0x01000193) & 0xffffffff
634+
return hval
635+
636+
def get_csi_directory_shard(base_oid, key):
637+
"""
638+
Returns the sharded csiDirectory object for a key.
639+
"""
640+
shard = fnv32a(key) % DEFAULT_CSI_DIRECTORY_SHARDS
641+
return f"{base_oid}.{shard}"
642+
643+
def get_val_from_csi_directory(base_oid, key, pool, is_rbd):
644+
"""
645+
Retrieve a value from sharded csiDirectory with legacy oid fallback.
646+
"""
647+
shard_oid = get_csi_directory_shard(base_oid, key)
648+
val = get_val_from_omap(shard_oid, key, pool, is_rbd)
649+
if val:
650+
return val
651+
652+
return get_val_from_omap(base_oid, key, pool, is_rbd)
653+
646654
def check_snap_content_name_in_rados(snap_uuid, snap_content_name, pool, is_rbd):
647655
"""
648-
Validates if snapshot content name is listed in csi.snaps.default omap
656+
Validates if snapshot content name is listed in csi.snaps omap
649657
"""
650658
snap_content_name = snap_content_name.replace("snapcontent", "snapshot")
651-
omap_obj = "csi.snaps.default"
659+
omap_obj = f"csi.snaps.{ARGS.instanceid}"
652660
omap_key = f'csi.snap.{snap_content_name}'
653661

654-
val = get_val_from_omap(omap_obj, omap_key, pool, is_rbd)
662+
val = get_val_from_csi_directory(omap_obj, omap_key, pool, is_rbd)
655663
if val != snap_uuid:
656664
if ARGS.debug:
657665
print(f"expected image Id {snap_uuid} found Id in rados {val}")

0 commit comments

Comments
 (0)