forked from microsoft/ai4eutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallel_enumerate_blobs.py
338 lines (244 loc) · 10.3 KB
/
parallel_enumerate_blobs.py
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#
# parallel_enumerate_blobs.py
#
# Read a list of prefixes from a text file, then enumerates a blob
# container, parallelizing across those prefixes (on thread/process per prefix).
#
# Creates one output file per prefix, which we typically just cat together
# after the fact.
#
# In practice, the prefix list is generated using enumerate_folders_at_depth.py,
# but it's just a flat list, so you can generate it however like.
#
# Uses one thread/process per prefix.
#
# Optionally reads the size for each blob, which it separates from the filename
# in the output files with \t .
#
#%% Constants and imports
import os
import sys
import time
import argparse
import multiprocessing
import humanfriendly
from azure.storage.blob import BlobServiceClient
from tqdm import tqdm
# Assumes that the parent folder of the ai4eutils repo is on the PYTHONPATH
#
# import sys; sys.path.append('/home/dmorris/git/ai4eutils')
# export PYTHONPATH="$PYTHONPATH:/home/dmorris/git/ai4eutils"
import path_utils
from ai4e_azure_utils import sas_blob_utils
n_blobs_per_page = 5000
n_print = 10000
# Toggles between threads (True) and processes (False)
use_threads = False
verbose = False
# This is a bit of a hack, but it has a *massive* impact on performance and on
# minimizing storage-account-level throttling. So... don't set this to zero.
sleep_time_per_page = 0.001
# Limit the number of files to enumerate per thread; used only for debugging
debug_max_files = -1
#%% Read prefix list
def read_prefix_list(prefix_list_file):
with open(prefix_list_file,'r') as f:
prefixes = f.readlines()
prefixes = [s.strip() for s in prefixes]
print('Read {} prefixes from {}'.format(len(prefixes),
prefix_list_file))
return prefixes
#%% Multiprocessing init
def pinit(c):
global cnt
cnt = c
class Counter(object):
def __init__(self, total):
# 'i' means integer
self.val = multiprocessing.Value('i', 0)
self.total = multiprocessing.Value('i', total)
self.last_print = multiprocessing.Value('i', 0)
def increment(self, n=1):
b_print = False
with self.val.get_lock():
self.val.value += n
if ((self.val.value - self.last_print.value) >= n_print):
self.last_print.value = self.val.value
b_print = True
if b_print:
total_string = ''
if self.total.value > 0:
total_string = ' of {}'.format(self.total.value)
print('{}: iteration {}{}'.format(time.strftime("%Y-%m-%d %H:%M:%S"),
self.val.value,total_string),flush=True)
@property
def value(self):
return self.val.value
def last_print_value(self):
return self.last_print.value
pinit(Counter(-1))
#%% Enumeration function
def enumerate_prefix(prefix,sas_url,output_folder,get_sizes=False):
account_name = sas_blob_utils.get_account_from_uri(sas_url)
container_name = sas_blob_utils.get_container_from_uri(sas_url)
ro_sas_token = sas_blob_utils.get_sas_token_from_uri(sas_url)
assert not ro_sas_token.startswith('?')
ro_sas_token = '?' + ro_sas_token
storage_account_url_blob = 'https://' + account_name + '.blob.core.windows.net'
# prefix = prefixes[0]; print(prefix)
print('Starting enumeration for prefix {}'.format(prefix))
# Open the output file
fn = path_utils.clean_filename(prefix)
output_file = os.path.join(output_folder,fn)
# Create the container
blob_service_client = BlobServiceClient(
account_url=storage_account_url_blob,
credential=ro_sas_token)
container_client = blob_service_client.get_container_client(container_name)
# Enumerate
with open(output_file,'w') as output_f:
continuation_token = ''
hit_debug_limit = False
i_blob = 0
while (continuation_token is not None) and (not hit_debug_limit):
blobs_iter = container_client.list_blobs(
name_starts_with=prefix,
results_per_page=n_blobs_per_page).by_page(
continuation_token=continuation_token)
# This is a paged list of BlobProperties objects
blobs = next(blobs_iter)
n_blobs_this_page = 0
for blob in blobs:
i_blob += 1
n_blobs_this_page += 1
if (debug_max_files > 0) and (i_blob > debug_max_files):
print('Hit debug path limit for prefix {}'.format(prefix))
i_blob -= 1
hit_debug_limit = True
break
else:
size_string = ''
if get_sizes:
size_string = '\t' + str(blob.size)
output_f.write(blob.name + size_string + '\n')
# print('Enumerated {} blobs'.format(n_blobs_this_page))
cnt.increment(n=n_blobs_this_page)
continuation_token = blobs_iter.continuation_token
if sleep_time_per_page > 0:
time.sleep(sleep_time_per_page)
# ...while we're enumerating
# ...with open(output_file)
print('Finished enumerating {} blobs for prefix {}'.format(
i_blob,prefix))
#%% Thread-based implementation
from threading import Thread
def enumerate_blobs_threads(prefixes,sas_url,output_folder,get_sizes=False):
all_threads = []
for s in prefixes:
# print('Starting thread for prefix {}'.format(s))
t = Thread(name=s,target=enumerate_prefix,args=(s,sas_url,output_folder,get_sizes,))
t.daemon = False
t.start()
all_threads.append(t)
for t in all_threads:
t.join()
# print('Thread {} finished'.format(t.name))
#%% Process-based implementation
from multiprocessing import Process
def enumerate_blobs_processes(prefixes,sas_url,output_folder,get_sizes=False):
all_processes = []
for s in prefixes:
# print('Starting process for prefix {}'.format(s))
p = Process(name=s,target=enumerate_prefix,args=(s,sas_url,output_folder,get_sizes,))
p.daemon = False
p.start()
all_processes.append(p)
for p in all_processes:
p.join()
# print('Process {} finished'.format(p.name))
#%% Main function
def enumerate_blobs(prefix_list_file,sas_url,output_folder,get_sizes=False):
assert(os.path.isfile(prefix_list_file))
os.makedirs(output_folder,exist_ok=True)
pinit(Counter(-1))
prefixes = read_prefix_list(prefix_list_file)
if use_threads:
enumerate_blobs_threads(prefixes,sas_url,output_folder,get_sizes)
else:
enumerate_blobs_processes(prefixes,sas_url,output_folder,get_sizes)
#%% Test driver
if False:
#%%
prefix_list_file = r'c:\temp\prefixes.txt'
sas_url = 'https://lilablobssc.blob.core.windows.net/nacti-unzipped?sv='
output_folder = r'c:\temp\enumeration_test'
get_sizes = True
enumerate_blobs(prefix_list_file,sas_url,output_folder,get_sizes)
# python parallel_enumerate_blobs.py "c:\temp\prefixes.txt" "https://lilablobssc.blob.core.windows.net/nacti-unzipped?sv=" "c:\temp\enumeration_test" --get_sizes
#%% Command-line driver
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser = argparse.ArgumentParser(
description='Enumerate blobs in a container, using one thread/process per prefix from a specified list of prefixes.')
parser.add_argument(
'prefix_list_file',
help='Text file containing one prefix per line')
parser.add_argument(
'sas_url',
help='Read-/list-capable, container-level SAS URL to the target container')
parser.add_argument(
'output_folder',
help='Output folder; one flat file per prefix will be written to this folder')
parser.add_argument(
'--get_sizes',action='store_true',
help='Include sizes for each blob in the output files (default: False)')
if len(sys.argv[1:]) == 0:
parser.print_help()
parser.exit()
args = parser.parse_args()
enumerate_blobs(args.prefix_list_file,args.sas_url,args.output_folder,args.get_sizes)
#%% Handy functions for working with the output files/folders from this script
# import os; import humanfriendly; from tqdm import tqdm
def parse_filenames_and_sizes(list_file):
"""
Takes a file with tab-delimited filename/size pairs and returns a
filename-->size dict.
"""
filename_to_size = {}
with open(list_file,'r') as f:
for line in f:
if ('catalog.json' in line) or ('stac.json' in line):
continue
tokens = line.split('\t')
assert len(tokens) == 2
fn = tokens[0]
size_str = tokens[1]
size = int(size_str)
if size == 0:
continue
filename_to_size[fn] = size
# ...for each line
# ...with open()
return filename_to_size
def parse_enumeration_folder(folder_name):
"""
Takes a folder full of files with tab-delimited filename/size pairs
and returns a filename-->size dict.
"""
filename_to_size = {}
enumeration_files = os.listdir(folder_name)
for fn in enumeration_files:
filename_to_size.update(parse_filenames_and_sizes(os.path.join(folder_name,fn)))
return filename_to_size
def summarize_enumeration_folder(folder_name):
enumeration_files = os.listdir(folder_name)
total_files = 0
total_size = 0
enumeration_files = os.listdir(folder_name)
for fn in tqdm(enumeration_files):
filename_to_size = parse_filenames_and_sizes(os.path.join(folder_name,fn))
total_files += len(filename_to_size)
size_this_file = sum(filename_to_size .values())
assert isinstance(size_this_file,int) and size_this_file > 0
total_size += size_this_file
print('Read {} files totaling {}'.format(total_files,humanfriendly.format_size(total_size)))