-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_files.py
More file actions
executable file
·154 lines (131 loc) · 5.55 KB
/
remove_files.py
File metadata and controls
executable file
·154 lines (131 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#! /usr/bin/env python3
import asyncio
import argparse
import os
import logging
import datetime
async def execute_remove(queue, worker, dry_run):
logger = logging.getLogger()
logger.info(f"{worker}: Activated")
dry_run_option = "--dry-run" if dry_run else ""
interrupted = False
while not queue.empty() and not interrupted:
lfn = None
try:
lfn, storage_prefix = await queue.get()
logger.info(f"{worker}: Starting removal process for file {lfn}")
except asyncio.CancelledError:
logger.info(f"{worker}: Shutting down due to interruption")
interrupted = True
if lfn:
# For removal we always act on the target storage prefix (single prefix)
# Build a well-formed URL/path: avoid double slashes
target_filepath = storage_prefix.rstrip("/") + "/" + lfn.lstrip("/")
retcode = 1
command = f"gfal-rm {dry_run_option} {target_filepath}"
try:
# retcode 2 corresponds to MISSING file
while retcode and retcode != 2:
logger.info(f"{worker}: Remove command:\n{command}")
remove_process = await asyncio.create_subprocess_shell(
command,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
stdin=asyncio.subprocess.PIPE,
)
stdout, stderr = await remove_process.communicate()
logger.info(f"{worker}: remove command return code: {remove_process.returncode}")
logger.info(f"{worker}: remove command standard output:\n{stdout.decode('utf-8').strip()}")
logger.info(f"{worker}: remove command error output:\n{stderr.decode('utf-8').strip()}")
retcode = remove_process.returncode
except asyncio.CancelledError:
logger.warning(f"{worker}: Cancelling remove command subprocess due to interruption")
remove_process.terminate()
interrupted = True
if not interrupted:
queue.task_done()
else:
continue
async def main(n_threads, dry_run, storage_prefix, filelist):
logger.info(f"Main: Starting removal process with {len(filelist)} files")
remove_task_queue = asyncio.Queue()
for f in filelist:
logger.info(f"Main: putting {f} in queue")
remove_task_queue.put_nowait((f, storage_prefix))
worker_name_template = "remove_worker_{INDEX}"
remove_workers = []
for i in range(n_threads):
worker = asyncio.create_task(
execute_remove(remove_task_queue, worker_name_template.format(INDEX=i), dry_run)
)
remove_workers.append(worker)
logger.info(f"Main: queue size: {remove_task_queue.qsize()}")
logger.info(f"Main: workers size: {len(remove_workers)}")
try:
logger.info(f"Main: joining queue")
await remove_task_queue.join()
logger.info(f"Main: joining queue finished")
except asyncio.CancelledError:
logger.warning(f"Main: Caught interruption")
finally:
for index, worker in enumerate(remove_workers):
logger.warning(
f"Main: Cancelling worker " + worker_name_template.format(INDEX=index)
)
worker.cancel()
await asyncio.gather(*remove_workers, return_exceptions=True)
logger.info(f"Main: Removal finished")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Small script to remove files on a target storage with gfal-rm using a queue-based approach",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"--n-threads",
type=int,
default=15,
help="Number of parallel threads to be used for removing.",
)
parser.add_argument(
"--logfile",
type=str,
default=f"/ceph/{os.environ['USER']}/logfile_remove_with_gfal.txt",
help="Path to the logfile used by this script.",
)
parser.add_argument("--filelist", required=True, help="Path to the list of logical file names to be removed.")
parser.add_argument(
"--storage-prefix",
required=True,
help="Storage prefix (target) where the files will be removed, e.g. davs://cmsdcache-kit-disk.gridka.de:2880/pnfs/gridka.de/cms/disk-only",
)
parser.add_argument(
"--dry-run",
action="store_true",
help="Running in dry-run mode for testing purposes",
)
args = parser.parse_args()
logging.getLogger("asyncio").setLevel(logging.NOTSET)
logger = logging.getLogger()
logger.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s | %(levelname)s | %(message)s")
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.INFO)
stream_handler.setFormatter(formatter)
infofile_handler = logging.FileHandler(
filename=f"_{datetime.datetime.now():%Y-%m-%d_%H-%M-%S}".join(os.path.splitext(args.logfile))
)
infofile_handler.setFormatter(formatter)
infofile_handler.setLevel(logging.INFO)
logger.addHandler(stream_handler)
logger.addHandler(infofile_handler)
try:
asyncio.run(
main(
args.n_threads,
args.dry_run,
args.storage_prefix,
[l.strip().split(",")[0] for l in open(args.filelist, "r").readlines()],
)
)
except asyncio.CancelledError:
pass