-
Notifications
You must be signed in to change notification settings - Fork 0
/
load-queue.py
334 lines (285 loc) · 10.3 KB
/
load-queue.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
import argparse
import ctypes
from datetime import datetime
import glob
import itertools
import logging
import multiprocessing
import os
import sqlite3
import sys
import time
import orjson as json
import zst
# Arguments
parser = argparse.ArgumentParser()
parser.add_argument('db')
parser.add_argument('files', nargs='+')
parser.add_argument('--readers', type=int, default=1, help="num reader processes")
parser.add_argument('--decoders', type=int, default=1, help="num decoder processes")
parser.add_argument('--chunk-lines', type=int, default=1000000, help="Lines per each chuck")
parser.add_argument('--comments', action='store_true', help="process comments files")
parser.add_argument('--index', action='store_true', help="Do nothing but create indexes")
parser.add_argument('--thin', action='store_true', help="Insert fewer columns")
args = parser.parse_args()
args.files = sum((glob.glob(f) for f in args.files), [])
#print(args.files[:5])
logger_root = logging.getLogger('')
fh = logging.FileHandler('load-queue.log')
fh.setLevel(logging.INFO)
logger_root.addHandler(fh)
log = logging.getLogger(__name__)
columns_submissions = [
('subreddit', 'TEXT'),
('id', 'TEXT', lambda x: 't3_'+x['id']),
('created_utc', 'INTEGER'),
('author', 'TEXT'),
('is_self', 'INTEGER'),
('title', 'TEXT'),
('score', 'INTEGER'),
('num_comments', 'INTEGER'),
]
if not args.thin:
columns_submissions.extend([
('subreddit_id', 'TEXT'),
('hidden', 'INTEGER'),
('domain', 'TEXT'),
('over_18', 'INTEGER'),
('url', 'TEXT'),
('selftext', 'TEXT'),
])
columns_comments = [
('subreddit', 'TEXT'),
('author', 'TEXT'),
('id', 'TEXT', lambda x: 't1_'+x['id']),
('link_id', 'TEXT'),
('created_utc', 'INTEGER'),
('score', 'INTEGER'),
]
if not args.thin:
columns_comments.extend([
('parent_id', 'TEXT'),
('controversiality', 'INTEGER'),
('distinguished', 'INTEGER'),
('ups', ''),
('downs', 'INTEGER'),
('gilded', 'INTEGER'),
('score_hidden', 'INTEGER'),
('subreddit_id', 'TEXT'),
('name', 'TEXT'),
('body', 'TEXT'),
])
# Hackish way to select if we import submissions or comments
TABLE = 'submissions'
COLUMNS = columns_submissions
TYPE = 'S'
if args.comments:
TABLE = 'comments'
COLUMNS = columns_comments
TYPE = 'C'
# Open and set up database
conn = sqlite3.connect(args.db)
conn.execute(f'PRAGMA page_size = 32768;')
conn.execute(f'PRAGMA mmap_size = {200 * 2**30}')
conn.execute(f'PRAGMA journal_mode = off;') # or WAL
conn.commit()
# --index: don't do anything else, but make indexes
if args.index:
indexes = [
('submissions', 'subreddit, created_utc'),
('submissions', 'subreddit, author'),
('submissions', 'id'),
('submissions', 'author'),
('comments', 'subreddit, created_utc'),
('comments', 'subreddit, author'),
('comments', 'subreddit, author, score'),
('comments', 'author'),
('comments', 'id'),
('comments', 'link_id'),
]
if not args.thin:
indexes.extend([
('comments', 'parent_id'),
]
conn.execute('PRAGMA journal_mode = WAL;') # or WAL
for i, (table, cols) in enumerate(indexes):
name = '_'.join(x[:3] for x in cols.split(', '))
cmd = f"CREATE INDEX IF NOT EXISTS idx_{table[:3]}_{name} ON {table} ({cols})"
print(cmd, flush=True)
conn.execute(cmd)
conn.commit()
print("ANALYZE;", flush=True)
conn.execute("ANALYZE;")
conn.commit()
exit(0)
# Make columns, etc.
#conn.execute('CREATE TABLE IF NOT EXISTS submissions (sub TEXT, time INTEGER, author TEXT, body BLOB)')
conn.execute(f'CREATE TABLE IF NOT EXISTS {TABLE} ('
f'{", ".join(" ".join(x[:2]) for x in COLUMNS)}'
f')')
conn.commit()
# Store the history of this table
conn.execute(f'CREATE TABLE IF NOT EXISTS history ('
f'time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, command TEXT'
f')')
conn.commit()
conn.execute("INSERT INTO history (command) VALUES (?)", (json.dumps(sys.argv), ))
conn.commit()
class Averager:
def __init__(self, alpha=.01):
self.alpha = alpha
self.n = multiprocessing.Value(ctypes.c_long, 0)
self.a = multiprocessing.Value(ctypes.c_double, float('nan'))
def add(self, x):
with self.n.get_lock():
self.n.value += 1
with self.a.get_lock():
if self.n.value == 1:
self.a.value = x
else:
self.a.value = x*self.alpha + self.a.value*(1-self.alpha)
@property
def avg(self):
return self.a.value
time_read = Averager()
time_decode = Averager()
time_insert = Averager()
def print_status(extra=''):
print(TYPE,
f"Queues: D: {queue1.qsize():3d}, I: {queue2.qsize():3d} "
f"Times: R:{time_read.avg:5.3f} D:{time_decode.avg:5.3f} I:{time_insert.avg:5.3f} -> {time_read.avg/time_insert.avg:4.1f} {time_decode.avg/time_insert.avg:4.1f} " + extra
)
def read(file_):
"""Read and decompress lines from file, put in queue
"""
queue = queue1
file_size = os.stat(file_).st_size
sub = os.path.basename(file_).rsplit('_', 1)[0]
lines_file = 0
accumulated = [ ]
start = time.time()
#print(f"read: starting {file_}")
for i, (line, file_bytes_processed) in enumerate(zst.read_lines_zst(file_)):
lines_file += 1
accumulated.append((i, line))
# Every 100000 lines, print status and push into queue.
if lines_file % args.chunk_lines == 0:
#created = datetime.utcfromtimestamp(int(obj['created_utc']))
print_status(f"{sub:20s} "
#f"{created.strftime('%Y-%m-%d %H:%M:%S')} : "
f"LinesSub: {lines_file:,} LinesTotal: {lines_total.value:,} LinesBad: {lines_bad.value:,} "
f"FilePercent: {(file_bytes_processed / file_size) * 100:3.0f}% "
f"TotalPercent: ({((file_bytes_processed + bytes_processed.value) / bytes_total) * 100:5.1f}%) "
)
time_read.add(time.time() - start)
start = time.time()
queue.put((file_, accumulated))
accumulated = [ ]
# Put all the last stuff into queue
time_read.add(time.time() - start)
queue.put((file_, accumulated))
with bytes_processed.get_lock():
bytes_processed.value += file_bytes_processed
with lines_total.get_lock():
lines_total.value += lines_file
sys.stdout.flush()
#print(f"read: done with {file_}")
#queue.close()
def decode(queue_in, queue_out):
"""Read from queue, decode JSON and make fields, add to next queue"""
accumulated = [ ]
# While there is stuff in the queue...
for i in itertools.count():
x = queue_in.get()
start = time.time()
# This is our sentinel to end processing. It seems the queue
# should raise ValueError once closed, but I haven't gotten
# that to work. Maybe it needs to be closed in every process.
if x == 'DONE':
print(' '*7, 'decode: done')
break
# For each line, load JSON and accumulate whatever our final
# values will be.
file_, lines = x
print_status(f'decode: {len(lines)}')
if i % 100 == 0:
sys.stdout.flush()
for lineno, line in lines:
try:
obj = json.loads(line)
except (KeyError, json.JSONDecodeError) as err:
with lines_bad.get_lock():
lines_bad.value += 1
log.warning("bad line: %s: %s", file_, lineno)
db_row = tuple(col[2](obj) if len(col)>2 else obj.get(col[0], None)
for col in COLUMNS)
accumulated.append(db_row)
with n_decode.get_lock():
n_decode.value += 1
time_decode.add(time.time() - start)
queue_out.put(accumulated)
accumulated = [ ]
# Don't forget to push the final stuff through.
queue_out.put(accumulated)
INSERT = f'INSERT INTO {TABLE} VALUES({",".join(["?"]*len(COLUMNS))})'
def insert(queue):
"""Read from queue and insert into the database"""
def get():
"""Generator to indefinitely return stuff to insert into the database"""
for i in itertools.count():
x = queue.get()
start = time.time()
if x == 'DONE':
print(' '*15, 'insert: done')
break
print_status('insert')
if i % 100 == 0:
sys.stdout.flush()
with n_insert.get_lock():
n_insert.value += 1
yield from x
time_insert.add(time.time() - start)
conn.executemany(INSERT, get())
conn.commit()
# Verify that --comments is used for comment files and vice versa
if args.comments:
assert all(x.endswith('_comments.zst') for x in args.files), "--comments but not all files end in _comments.zst"
else:
assert all(x.endswith('_submissions.zst') for x in args.files), "not all files end in _submissions.zst"
# Status variables for our progress
bytes_total = sum(os.stat(file_).st_size for file_ in args.files)
bytes_processed = multiprocessing.Value(ctypes.c_long, 0)
lines_total = multiprocessing.Value(ctypes.c_long, 0)
lines_bad = multiprocessing.Value(ctypes.c_long, 0)
n_decode = multiprocessing.Value(ctypes.c_long, 0)
n_insert = multiprocessing.Value(ctypes.c_long, 0)
start = time.time()
def runtime():
return time.time() - start
# Queues
queue1 = multiprocessing.Queue(maxsize=50)
queue2 = multiprocessing.Queue(maxsize=50)
# start decoding process
decode_ps = [ multiprocessing.Process(target=decode, args=(queue1, queue2,)) for _ in range(args.decoders) ]
for p in decode_ps:
p.start()
# start inserting process
insert_p = multiprocessing.Process(target=insert, args=(queue2,))
insert_p.start()
# For every file, via multiprocessing.Pool
#p_read = multiprocessing.Process(target=read, args=(queue1, file_))
read_pool = multiprocessing.Pool(processes=args.readers)
read_pool.map(read, args.files)
read_pool.close()
read_pool.join()
print("reading: done")
# Close all decoders
for _ in range(args.decoders):
queue1.put('DONE')
queue1.close()
for p in decode_ps:
p.join()
# Close all joiners
queue2.put('DONE')
insert_p.join()
conn.execute('PRAGMA journal_mode = WAL;')