-
Notifications
You must be signed in to change notification settings - Fork 7
/
bulkupload.py
458 lines (362 loc) · 13.5 KB
/
bulkupload.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
import datetime
import os
import sys
import time
from multiprocessing import Process, Lock, Value, Manager
import swiftclient
import olrcdb
# Settings
SEGMENT_SIZE = 100 * 10 ** 6
COUNT = 0
FAILED_COUNT = 0
SLEEP = 1 # Sleep timeout when trying to connect to the database.
LOGDIR = '/data/swiftbulkuploader/logs_upload/'
REQUIRED_VARIABLES = [
'OS_PASSWORD',
'OS_USERNAME',
'OS_PROJECT_NAME',
'OS_PROJECT_DOMAIN_NAME',
'OS_USER_DOMAIN_NAME',
'OS_AUTH_URL',
'OS_REGION_NAME',
'OS_INTERFACE',
'OS_IDENTITY_API_VERSION',
'MYSQL_HOST',
'MYSQL_USER',
'MYSQL_PASSWD',
'MYSQL_DB'
]
def upload_file(path, connection_storage_url, auth_token, container, path_cutoff="", attempts=0):
"""Given String source_file, upload the file to the OLRC to target_file
and return True if successful. """
try:
opened_source_file = open(path, 'rb')
except IOError as e:
try:
print("Error opening %s: %s" % (path, str(e)))
return False
except UnicodeEncodeError:
print("Error opening (+ unicode error): " + path.encode('utf-8'))
return False
swift_path = path
if path_cutoff:
swift_path = swift_path.lstrip(path_cutoff)
# Paths beginning with "/" will lose their folder structure on swift.
# Removing it will preserve it.
if swift_path == "/":
swift_path = swift_path[1:]
try:
swiftclient.client.put_object(
connection_storage_url,
auth_token,
container,
swift_path,
opened_source_file)
except (UnicodeDecodeError, ConnectionError, swiftclient.client.ClientException) as e:
sys.stderr.flush()
sys.stderr.write(
"\rError! {0} Uploading {1} to OLRC encountered the following issue: "
"{2}".format(
time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()),
path,
str(e)
)
)
return False
return True
def olrc_connect():
"""Connect to the OLRC with the global variables. Exit if connection
fails."""
global SLEEP
swift_auth_url, username, password, identity_api_version, os_options = get_env_vars()
try:
return swiftclient.client.get_auth(
swift_auth_url, username, password,
auth_version=identity_api_version,
os_options=os_options
)
except swiftclient.client.ClientException as e:
print(e)
sys.stdout.flush()
sys.stdout.write(
"\rError! {0} Connection to OLRC failed."
" Trying again in {1} seconds.\n".format(
time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()),
SLEEP
)
)
time.sleep(SLEEP)
SLEEP += 1
return olrc_connect()
def create_container(storage_url, auth_token, container):
"""Create the container on swift."""
try:
swiftclient.client.put_container(storage_url, auth_token, container)
except swiftclient.client.ClientException as e:
print(e)
sys.stdout.flush()
sys.stdout.write(
"\rError! {0} Failed to create container {1}".format(
time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()),
container
)
)
def env_vars_set(required_variables):
"""Check all the required environment variables are set. Return false if
any of them are undefined."""
for required_variable in required_variables:
if (not os.environ.get(required_variable)
and os.environ.get(required_variable) != ""):
return False
return True
def get_env_vars():
"""Get the global variables for swift client assuming they exist in the
environment."""
swift_auth_url = os.environ.get("OS_AUTH_URL")
username = os.environ.get("OS_USERNAME")
password = os.environ.get("OS_PASSWORD")
try:
identity_api_version = int(os.environ.get("OS_IDENTITY_API_VERSION"))
except ValueError:
raise ValueError("Environment variable OS_IDENTITY_API_VERSION needs to be a number. Got %s instead."
% os.environ.get("OS_IDENTITY_API_VERSION"))
os_options = {
"user_domain_name": os.environ.get("OS_USER_DOMAIN_NAME"),
"project_name": os.environ.get("OS_PROJECT_NAME"),
"project_domain_name": os.environ.get("OS_PROJECT_DOMAIN_NAME"),
"auth_version": identity_api_version,
"region_name": os.environ.get("OS_REGION_NAME"),
"endpoint_type": os.environ.get("OS_INTERFACE")
}
return swift_auth_url, username, password, identity_api_version, os_options
def upload_table(lock, table_name, container, counter, failed_counter, speed, connection_storage_url,
auth_token, entries, path_cutoff=""):
"""
Given a table_name, upload all the paths from the table where upload is 0.
Using the range value, complete a BATCH worth of uploads at a time.
"""
def get_entry():
try:
return entries.pop()
except IndexError:
return None
global FAILED_COUNT
total = get_total_to_upload(table_name)
# In order for the current process to upload a unique set of files,
# acquire the lock to read from range's value.
lock.acquire()
cur_entry = get_entry()
lock.release()
while cur_entry is not None:
retry = 0
success = False
while retry < 5 and not success:
# If the upload is successful, update the database
if upload_file(cur_entry[1], connection_storage_url, auth_token, container, path_cutoff=path_cutoff):
lock.acquire()
counter.value += 1
lock.release()
set_uploaded(cur_entry[0], table_name)
success = True
else:
retry += 1
time.sleep(1)
connection_storage_url, auth_token = olrc_connect()
if not success:
lock.acquire()
sys.stdout.flush()
sys.stdout.write(
"Error! {0} Upload of {1} to OLRC failed"
" after {2} attempts.\n".format(
time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()),
cur_entry[1],
retry
)
)
failed_counter.value += 1
error_log = open(LOGDIR + table_name + '.upload.error.log', 'a')
error_log.write(
"\rFailed: {0}\n".format(
cur_entry[1].encode('utf-8')))
error_log.close()
lock.release()
print_status(counter, lock, speed, table_name, total)
lock.acquire()
cur_entry = get_entry()
lock.release()
def get_total_to_upload(table_name):
"""Given a table_name, get the total number of rows"""
query = "SELECT COUNT(*) FROM {0}".format(table_name)
connect = olrcdb.DatabaseConnection()
result = connect.execute_query(query)
result_tuple = result.fetchone()
return result_tuple[0]
def get_total_uploaded(table_name):
"""Given a table_name, get the total number of rows where upload is 1."""
query = "SELECT COUNT(*) FROM {0} WHERE uploaded=1".format(table_name)
connect = olrcdb.DatabaseConnection()
result = connect.execute_query(query)
result_tuple = result.fetchone()
return result_tuple[0]
def set_uploaded(id, table_name):
"""For the given path, set uploaded to 1 in table_name."""
query = "UPDATE {0} set uploaded='1' WHERE id='{1}'".format(
table_name,
id
)
connect = olrcdb.DatabaseConnection()
connect.execute_query(query)
def check_env_args():
"""Do checks on the environment and args."""
# Check environment variables
if not env_vars_set(REQUIRED_VARIABLES):
set_env_message = "The following environment variables need to be " \
"set:\n"
set_env_message += " \n".join(REQUIRED_VARIABLES)
set_env_message += "\nPlease set these environment variables to " \
"connect to the OLRC."
print(set_env_message)
exit(0)
total = len(sys.argv)
usage = "Please pass in a few arguments, see example below \n" \
"python bulkupload.py container-name mysql-table n-processes path-cutoff\n" \
"where mysql-table is table created from prepareupload.py, " \
"n-process is the number of processes created to run this script and" \
" path-cutoff is the string that indicates from where the path is" \
" truncated from the front."
# Do not execute if no directory provided.
if total != 4 and total != 5:
print(usage)
exit(0)
def start_reporting(table_name):
"""Create an error log file. Note the time of execution."""
# Open error log:
error_log = open(LOGDIR + table_name + '.upload.error.log', 'w+')
error_log.write("From execution {0}:\n".format(
str(datetime.datetime.now())
))
error_log.close()
def end_reporting(counter, failed_counter, table_name):
"""Create a report log. Output upload summary."""
report_log = open(LOGDIR + table_name + '.upload.report.log', 'w+')
report_log.write("From execution {0}:\n".format(
str(datetime.datetime.now())
))
report = "\nTotal uploaded: {0}\nTotal failed uploaded: {1}\n" \
"Failed uploads stored in error.log\n" \
"Reported saved in report.log.\n" \
.format(counter.value, failed_counter.value)
report_log.write(report)
report_log.close()
# Output report to user
sys.stdout.flush()
sys.stdout.write(report)
def print_status(counter, lock, speed, table_name, total):
"""Print the current status of uploaded files."""
lock.acquire()
percentage_uploaded = format(
(float(counter.value) / float(total)) * 100,
'.8f'
)
lock.release()
sys.stdout.flush()
sys.stdout.write("\r{0}% Uploaded at {1:.2f} uploads/second. ".format(
percentage_uploaded, speed.value))
# Log the final count
report = open(LOGDIR + table_name + ".upload.out", 'w+')
report.write(
"\r{0}% Uploaded at {1:.2f} uploads/second. ".format(
percentage_uploaded, speed.value))
report.close()
def get_min_id(table_name):
"""Return the minimum id from table_name where uploaded=0"""
query = "SELECT MIN(id) FROM {0} WHERE uploaded=0".format(table_name)
connect = olrcdb.DatabaseConnection()
result = connect.execute_query(query)
result_tuple = result.fetchone()
if not result_tuple[0]:
sys.exit("Nothing to upload from table {0}".format(table_name))
return int(result_tuple[0])
def get_all_entries_to_upload():
"""Return a tuple of all database entries that need to be uploaded."""
# We order the entries descending because we are going to be popping this list (i.e. starting by the end)
query = "SELECT * FROM {0} WHERE uploaded=0 ORDER BY id DESC".format(table_name)
connect = olrcdb.DatabaseConnection()
result = connect.execute_query(query)
return result.fetchall()
def set_speed(lock, counter, speed, entries):
"""Calculate the upload speed for the next minute and set it in the
speed."""
while entries:
lock.acquire()
start_count = counter.value
start_time = time.time()
lock.release()
time.sleep(5) # Sleep for 60 seconds.
lock.acquire()
stop_count = counter.value
stop_time = time.time()
lock.release()
lock.acquire()
speed.value = (
float(stop_count - start_count) /
float(stop_time - start_time)
)
# Save the speed calculation.
lock.release()
if __name__ == "__main__":
check_env_args()
container = sys.argv[1] # Swift container files will be uploaded to.
table_name = sys.argv[2] # Name of table to read file paths from.
n_processes = int(sys.argv[3]) # Number of processes to create for uploading.
if len(sys.argv) == 5:
path_cutoff = sys.argv[4] # The path cutoff
else:
path_cutoff = ''
storage_url, auth_token = olrc_connect()
create_container(storage_url, auth_token, container)
start_reporting(table_name)
manager = Manager()
# Integer value of uploaded files within target table.
counter = Value("i", get_total_uploaded(table_name))
failed_counter = Value("i", 0)
lock = Lock()
# Load entries into manager list
entries = manager.list(get_all_entries_to_upload())
speed = Value("d", 0.0) # Tracker for upload speed.
processes = []
# Create a new process n times.
for process in range(n_processes):
p = Process(
target=upload_table,
args=(
lock,
table_name,
container,
counter,
failed_counter,
speed,
storage_url,
auth_token,
entries
),
kwargs={"path_cutoff": path_cutoff}
)
# Execute the upload_table function
p.start()
processes.append(p)
# Create a process to calculate the speed of uploads.
p = Process(
target=set_speed,
args=(
lock,
counter,
speed,
entries
))
p.start()
processes.append(p)
# Join all processes
for process in processes:
process.join()
end_reporting(counter, failed_counter, table_name)