From 20184c23d35334f9a72aac49eff07d46771096c5 Mon Sep 17 00:00:00 2001 From: Patrick Donnelly Date: Tue, 16 May 2023 11:49:07 -0400 Subject: [PATCH] qa: use stdin-killer for python3 command This relies on the new stdin-killer [1] teuthology helper that allows interacting with the command's stdin. [1] https://github.com/ceph/teuthology/pull/1846 Fixes: 8bb77ed9e18d134e2e9964e950cbcbfc65e1b97d Signed-off-by: Patrick Donnelly --- qa/tasks/cephfs/mount.py | 88 +++++++++++++++++++++++++++++++++++----- 1 file changed, 77 insertions(+), 11 deletions(-) diff --git a/qa/tasks/cephfs/mount.py b/qa/tasks/cephfs/mount.py index 2b9acf1a475..528a2f4f3a1 100644 --- a/qa/tasks/cephfs/mount.py +++ b/qa/tasks/cephfs/mount.py @@ -762,7 +762,7 @@ class CephFSMount(object): if sudo: args.append('sudo') omit_sudo = False - args += ['adjust-ulimits', 'daemon-helper', 'kill', py_version, '-c', pyscript] + args += ['stdin-killer', '--', py_version, '-c', pyscript] return self.client_remote.run(args=args, wait=False, stdin=run.PIPE, stdout=StringIO(), omit_sudo=omit_sudo) @@ -949,21 +949,43 @@ class CephFSMount(object): if write: pyscript = dedent(""" + import fcntl + import os + import sys import time + fcntl.fcntl(sys.stdin, fcntl.F_SETFL, fcntl.fcntl(sys.stdin, fcntl.F_GETFL) | os.O_NONBLOCK) + with open("{path}", 'w') as f: f.write("{content}") f.flush() while True: - time.sleep(1) + print("open_background: keeping file open", file=sys.stderr) + try: + if os.read(0, 4096) == b"": + break + except BlockingIOError: + pass + time.sleep(2) """).format(path=path, content=content) else: pyscript = dedent(""" + import fcntl + import os + import sys import time + fcntl.fcntl(sys.stdin, fcntl.F_SETFL, fcntl.fcntl(sys.stdin, fcntl.F_GETFL) | os.O_NONBLOCK) + with open("{path}", 'r') as f: while True: - time.sleep(1) + print("open_background: keeping file open", file=sys.stderr) + try: + if os.read(0, 4096) == b"": + break + except BlockingIOError: + pass + time.sleep(2) """).format(path=path) rproc = self._run_python(pyscript) @@ -988,13 +1010,23 @@ class CephFSMount(object): path = os.path.join(self.hostfs_mntpt, basename) pyscript = dedent(""" + import fcntl + import sys import time import os + fcntl.fcntl(sys.stdin, fcntl.F_SETFL, fcntl.fcntl(sys.stdin, fcntl.F_GETFL) | os.O_NONBLOCK) + os.mkdir("{path}") fd = os.open("{path}", os.O_RDONLY) while True: - time.sleep(1) + print("open_dir_background: keeping dir open", file=sys.stderr) + try: + if os.read(0, 4096) == b"": + break + except BlockingIOError: + pass + time.sleep(2) """).format(path=path) rproc = self._run_python(pyscript) @@ -1047,19 +1079,31 @@ class CephFSMount(object): path = os.path.join(self.hostfs_mntpt, basename) script_builder = """ + import sys import time import fcntl - import struct""" + import os + import struct + + fcntl.fcntl(sys.stdin, fcntl.F_SETFL, fcntl.fcntl(sys.stdin, fcntl.F_GETFL) | os.O_NONBLOCK) + """ if do_flock: script_builder += """ f1 = open("{path}-1", 'w') - fcntl.flock(f1, fcntl.LOCK_EX | fcntl.LOCK_NB)""" + fcntl.flock(f1, fcntl.LOCK_EX | fcntl.LOCK_NB) + """ script_builder += """ f2 = open("{path}-2", 'w') lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 0, 0, 0, 0) fcntl.fcntl(f2, fcntl.F_SETLK, lockdata) while True: - time.sleep(1) + print("lock_background: keeping lock", file=sys.stderr) + try: + if os.read(0, 4096) == b"": + break + except BlockingIOError: + pass + time.sleep(2) """ pyscript = dedent(script_builder).format(path=path) @@ -1078,6 +1122,7 @@ class CephFSMount(object): import time import fcntl import struct + import sys f1 = open("{path}-1", 'w') fcntl.flock(f1, fcntl.LOCK_EX) f2 = open("{path}-2", 'w') @@ -1097,7 +1142,9 @@ class CephFSMount(object): script_builder = """ import fcntl import errno - import struct""" + import struct + import sys + """ if do_flock: script_builder += """ f1 = open("{path}-1", 'r') @@ -1137,16 +1184,26 @@ class CephFSMount(object): path = os.path.join(self.hostfs_mntpt, basename) pyscript = dedent(""" + import fcntl import os + import sys import time + fcntl.fcntl(sys.stdin, fcntl.F_SETFL, fcntl.fcntl(sys.stdin, fcntl.F_GETFL) | os.O_NONBLOCK) + fd = os.open("{path}", os.O_RDWR | os.O_CREAT, 0o644) try: while True: + print("write_background: writing", file=sys.stderr) os.write(fd, b'content') - time.sleep(1) if not {loop}: break + try: + if os.read(0, 4096) == b"": + break + except BlockingIOError: + pass + time.sleep(2) except IOError as e: pass os.close(fd) @@ -1215,10 +1272,13 @@ class CephFSMount(object): abs_path = os.path.join(self.hostfs_mntpt, fs_path) pyscript = dedent(""" + import fcntl import sys import time import os + fcntl.fcntl(sys.stdin, fcntl.F_SETFL, fcntl.fcntl(sys.stdin, fcntl.F_GETFL) | os.O_NONBLOCK) + n = {count} abs_path = "{abs_path}" @@ -1232,8 +1292,14 @@ class CephFSMount(object): handles.append(open(path, 'w')) print("waiting with handles open", file=sys.stderr) - while os.read(0, 4096) != b"": - time.sleep(1) + while True: + print("open_n_background: keeping files open", file=sys.stderr) + try: + if os.read(0, 4096) == b"": + break + except BlockingIOError: + pass + time.sleep(2) print("stdin closed, goodbye!", file=sys.stderr) """).format(abs_path=abs_path, count=count) -- 2.39.5