-
Notifications
You must be signed in to change notification settings - Fork 8
/
interfaces.py
756 lines (614 loc) · 21 KB
/
interfaces.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
import six
import base64
import binascii
from collections import defaultdict
import json
import os.path
import posixpath
import re
import boto3
import botocore
from glob import glob
import google.cloud.exceptions
from google.cloud.storage import Batch, Client
import requests
import tenacity
from .compression import COMPRESSION_TYPES
from .connectionpools import S3ConnectionPool, GCloudBucketPool, MemoryPool, MEMORY_DATA
from .lib import mkdir, sip, md5, PYTHON3
COMPRESSION_EXTENSIONS = ('.gz', '.br', '.zstd')
GZIP_TYPES = (True, 'gzip', 1)
# This is just to support pooling by bucket
class keydefaultdict(defaultdict):
def __missing__(self, key):
if self.default_factory is None:
raise KeyError( key )
else:
ret = self[key] = self.default_factory(key)
return ret
S3_POOL = None
GC_POOL = None
MEM_POOL = None
def reset_connection_pools():
global S3_POOL
global GC_POOL
global MEM_POOL
S3_POOL = keydefaultdict(lambda service: keydefaultdict(lambda bucket_name: S3ConnectionPool(service, bucket_name)))
GC_POOL = keydefaultdict(lambda bucket_name: GCloudBucketPool(bucket_name))
MEM_POOL = keydefaultdict(lambda bucket_name: MemoryPool(bucket_name))
MEMORY_DATA = {}
reset_connection_pools()
retry = tenacity.retry(
reraise=True,
stop=tenacity.stop_after_attempt(7),
wait=tenacity.wait_random_exponential(0.5, 60.0),
)
class StorageInterface(object):
exists_batch_size = 1
delete_batch_size = 1
def release_connection(self):
pass
def __enter__(self):
return self
def __exit__(self, exception_type, exception_value, traceback):
self.release_connection()
class FileInterface(StorageInterface):
def __init__(self, path, secrets=None, endpoint=None):
super(StorageInterface, self).__init__()
self._path = path
def get_path_to_file(self, file_path):
return os.path.join(
self._path.basepath, self._path.layer, file_path
)
def put_file(
self, file_path, content,
content_type, compress,
cache_control=None
):
path = self.get_path_to_file(file_path)
mkdir(os.path.dirname(path))
# keep default as gzip
if compress == "br":
path += ".br"
elif compress in GZIP_TYPES:
path += ".gz"
elif compress == "zstd":
path += ".zstd"
elif compress:
raise ValueError("Compression type {} not supported.".format(compress))
if content \
and content_type \
and re.search('json|te?xt', content_type) \
and type(content) is str:
content = content.encode('utf-8')
try:
with open(path, 'wb') as f:
f.write(content)
except IOError as err:
with open(path, 'wb') as f:
f.write(content)
def get_file(self, file_path, start=None, end=None):
path = self.get_path_to_file(file_path)
if os.path.exists(path + '.gz'):
encoding = "gzip"
path += '.gz'
elif os.path.exists(path + '.br'):
encoding = "br"
path += ".br"
elif os.path.exists(path + '.zstd'):
encoding = "zstd"
path += ".zstd"
else:
encoding = None
try:
with open(path, 'rb') as f:
if start is not None:
f.seek(start)
if end is not None:
start = start if start is not None else 0
num_bytes = end - start
data = f.read(num_bytes)
else:
data = f.read()
return (data, encoding, None, None)
except IOError:
return (None, encoding, None, None)
def size(self, file_path):
path = self.get_path_to_file(file_path)
exts = ('.gz', '.br', '.zstd', '')
if PYTHON3:
errors = (FileNotFoundError,)
else:
errors = (OSError,)
for ext in exts:
try:
return os.path.getsize(path + ext)
except errors:
continue
return None
def exists(self, file_path):
path = self.get_path_to_file(file_path)
return os.path.exists(path) or any(( os.path.exists(path + ext) for ext in COMPRESSION_EXTENSIONS ))
def files_exist(self, file_paths):
return { path: self.exists(path) for path in file_paths }
def delete_file(self, file_path):
path = self.get_path_to_file(file_path)
if os.path.exists(path):
os.remove(path)
elif os.path.exists(path + '.gz'):
os.remove(path + '.gz')
elif os.path.exists(path + ".br"):
os.remove(path + ".br")
def delete_files(self, file_paths):
for path in file_paths:
self.delete_file(path)
def list_files(self, prefix, flat):
"""
List the files in the layer with the given prefix.
flat means only generate one level of a directory,
while non-flat means generate all file paths with that
prefix.
"""
layer_path = self.get_path_to_file("")
path = os.path.join(layer_path, prefix) + '*'
filenames = []
remove = layer_path
if len(remove) and remove[-1] != '/':
remove += '/'
if flat:
for file_path in glob(path):
if not os.path.isfile(file_path):
continue
filename = file_path.replace(remove, '')
filenames.append(filename)
else:
subdir = os.path.join(layer_path, os.path.dirname(prefix))
for root, dirs, files in os.walk(subdir):
files = [ os.path.join(root, f) for f in files ]
files = [ f.replace(remove, '') for f in files ]
files = [ f for f in files if f[:len(prefix)] == prefix ]
for filename in files:
filenames.append(filename)
def stripext(fname):
(base, ext) = os.path.splitext(fname)
if ext in COMPRESSION_EXTENSIONS:
return base
else:
return fname
filenames = list(map(stripext, filenames))
return iter(_radix_sort(filenames))
class MemoryInterface(StorageInterface):
def __init__(self, path, secrets=None, endpoint=None):
super(StorageInterface, self).__init__()
self._path = path
self._data = MEM_POOL[path.bucket].get_connection(secrets, endpoint)
def get_path_to_file(self, file_path):
return os.path.join(
self._path.basepath, self._path.layer, file_path
)
def put_file(
self, file_path, content,
content_type, compress,
cache_control=None
):
path = self.get_path_to_file(file_path)
# keep default as gzip
if compress == "br":
path += ".br"
elif compress in GZIP_TYPES:
path += ".gz"
elif compress == "zstd":
path += ".zstd"
elif compress:
raise ValueError("Compression type {} not supported.".format(compress))
if content \
and content_type \
and re.search('json|te?xt', content_type) \
and type(content) is str:
content = content.encode('utf-8')
self._data[path] = content
def get_file(self, file_path, start=None, end=None):
path = self.get_path_to_file(file_path)
if path + '.gz' in self._data:
encoding = "gzip"
path += '.gz'
elif path + '.br' in self._data:
encoding = "br"
path += ".br"
elif path + '.zstd' in self._data:
encoding = "zstd"
path += ".zstd"
else:
encoding = None
slc = slice(start, end)
return (self._data[path][slc], encoding, None, None)
def size(self, file_path):
path = self.get_path_to_file(file_path)
exts = ('.gz', '.br', '.zstd')
for ext in exts:
pathext = path + ext
if pathext in self._data:
return len(self._data[pathext])
if path in self._data:
data = self._data[path]
if PYTHON3:
if isinstance(data, bytes):
return len(data)
else:
return len(data.encode('utf8'))
else:
return len(data)
return None
def exists(self, file_path):
path = self.get_path_to_file(file_path)
return path in self._data or any(( (path + ext in self._data) for ext in COMPRESSION_EXTENSIONS ))
def files_exist(self, file_paths):
return { path: self.exists(path) for path in file_paths }
def delete_file(self, file_path):
path = self.get_path_to_file(file_path)
for suffix in ([''] + list(COMPRESSION_EXTENSIONS)):
try:
del self._data[path + suffix]
break
except KeyError:
continue
def delete_files(self, file_paths):
for path in file_paths:
self.delete_file(path)
def list_files(self, prefix, flat):
"""
List the files in the layer with the given prefix.
flat means only generate one level of a directory,
while non-flat means generate all file paths with that
prefix.
Returns: iterator
"""
layer_path = self.get_path_to_file("")
path = os.path.join(layer_path, prefix) + '*'
remove = layer_path
if len(remove) and remove[-1] != '/':
remove += '/'
filenames = [ f.replace(remove, '') for f in self._data ]
filenames = [ f for f in filenames if f[:len(prefix)] == prefix ]
if flat:
filenames = [ f for f in filenames if '/' not in f.replace(prefix, '') ]
def stripext(fname):
(base, ext) = os.path.splitext(fname)
if ext in COMPRESSION_EXTENSIONS:
return base
else:
return fname
filenames = list(map(stripext, filenames))
return iter(_radix_sort(filenames))
class GoogleCloudStorageInterface(StorageInterface):
exists_batch_size = Batch._MAX_BATCH_SIZE
delete_batch_size = Batch._MAX_BATCH_SIZE
def __init__(self, path, secrets=None, endpoint=None):
super(StorageInterface, self).__init__()
global GC_POOL
self._path = path
self._bucket = GC_POOL[path.bucket].get_connection(secrets, endpoint)
def get_path_to_file(self, file_path):
return posixpath.join(self._path.no_bucket_basepath, self._path.layer, file_path)
@retry
def put_file(self, file_path, content, content_type, compress, cache_control=None):
key = self.get_path_to_file(file_path)
blob = self._bucket.blob( key )
if compress == "br":
blob.content_encoding = "br"
elif compress in GZIP_TYPES:
blob.content_encoding = "gzip"
elif compress:
raise ValueError("Compression type {} not supported.".format(compress))
if cache_control:
blob.cache_control = cache_control
blob.md5_hash = md5(content)
blob.upload_from_string(content, content_type)
@retry
def get_file(self, file_path, start=None, end=None):
key = self.get_path_to_file(file_path)
blob = self._bucket.blob( key )
if start is not None:
start = int(start)
if end is not None:
end = int(end - 1)
try:
content = blob.download_as_string(start=start, end=end, raw_download=True)
except google.cloud.exceptions.NotFound as err:
return (None, None, None, None)
hash_type = "md5"
hash_value = blob.md5_hash if blob.component_count is None else None
if hash_value is None and blob.crc32c is not None:
hash_type = "crc32c"
hash_value = blob.crc32c
return (content, blob.content_encoding, hash_value, hash_type)
@retry
def size(self, file_path):
key = self.get_path_to_file(file_path)
return self._bucket.get_blob(key).size
@retry
def exists(self, file_path):
key = self.get_path_to_file(file_path)
blob = self._bucket.blob(key)
return blob.exists()
@retry
def files_exist(self, file_paths):
result = { path: None for path in file_paths }
for batch in sip(file_paths, self.exists_batch_size):
# Retrieve current batch of blobs. On Batch __exit__ it will populate all
# future responses before raising errors about the (likely) missing keys.
try:
with self._bucket.client.batch():
for file_path in batch:
key = self.get_path_to_file(file_path)
result[file_path] = self._bucket.get_blob(key)
except google.cloud.exceptions.NotFound as err:
pass # Missing keys are expected
for file_path, blob in result.items():
# Blob exists if ``dict``, missing if ``_FutureDict``
result[file_path] = isinstance(blob._properties, dict)
return result
@retry
def delete_file(self, file_path):
key = self.get_path_to_file(file_path)
try:
self._bucket.delete_blob( key )
except google.cloud.exceptions.NotFound:
pass
@retry
def delete_files(self, file_paths):
for batch in sip(file_paths, self.delete_batch_size):
try:
with self._bucket.client.batch():
for file_path in batch:
key = self.get_path_to_file(file_path)
self._bucket.delete_blob(key)
except google.cloud.exceptions.NotFound:
pass
@retry
def list_files(self, prefix, flat=False):
"""
List the files in the layer with the given prefix.
flat means only generate one level of a directory,
while non-flat means generate all file paths with that
prefix.
"""
layer_path = self.get_path_to_file("")
path = posixpath.join(layer_path, prefix)
for blob in self._bucket.list_blobs(prefix=path):
filename = blob.name.replace(layer_path, '')
if not filename:
continue
elif not flat and filename[-1] != '/':
yield filename
elif flat and '/' not in blob.name.replace(path, ''):
yield filename
def release_connection(self):
global GC_POOL
GC_POOL[self._path.bucket].release_connection(self._bucket)
class HttpInterface(StorageInterface):
def __init__(self, path, secrets=None, endpoint=None):
super(StorageInterface, self).__init__()
self._path = path
def get_path_to_file(self, file_path):
path = posixpath.join(
self._path.basepath, self._path.layer, file_path
)
return self._path.protocol + '://' + path
# @retry
def delete_file(self, file_path):
raise NotImplementedError()
def delete_files(self, file_paths):
raise NotImplementedError()
# @retry
def put_file(self, file_path, content, content_type, compress, cache_control=None):
raise NotImplementedError()
@retry
def get_file(self, file_path, start=None, end=None):
key = self.get_path_to_file(file_path)
if start is not None or end is not None:
start = int(start) if start is not None else ''
end = int(end - 1) if end is not None else ''
headers = { "Range": "bytes={}-{}".format(start, end) }
resp = requests.get(key, headers=headers)
else:
resp = requests.get(key)
if resp.status_code in (404, 403):
return (None, None, None, None)
resp.raise_for_status()
# Don't check MD5 for http because the etag can come in many
# forms from either GCS, S3 or another service entirely. We
# probably won't figure out how to decode it right.
# etag = resp.headers.get('etag', None)
content_encoding = resp.headers.get('Content-Encoding', None)
# requests automatically decodes these
if content_encoding in (None, '', 'gzip', 'deflate', 'br'):
content_encoding = None
return (resp.content, content_encoding, None, None)
@retry
def exists(self, file_path):
key = self.get_path_to_file(file_path)
resp = requests.get(key, stream=True)
resp.close()
return resp.ok
def files_exist(self, file_paths):
return {path: self.exists(path) for path in file_paths}
def list_files(self, prefix, flat=False):
raise NotImplementedError()
class S3Interface(StorageInterface):
# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Bucket.delete_objects
# claims batch size limit is 1000
delete_batch_size = 1000
def __init__(self, path, secrets=None, endpoint=None):
super(StorageInterface, self).__init__()
global S3_POOL
self._path = path
self._conn = S3_POOL[path.protocol][path.bucket].get_connection(secrets, endpoint)
def get_path_to_file(self, file_path):
return posixpath.join(self._path.no_bucket_basepath, self._path.layer, file_path)
@retry
def put_file(
self, file_path, content,
content_type, compress,
cache_control=None, ACL="bucket-owner-full-control"
):
key = self.get_path_to_file(file_path)
attrs = {
'Bucket': self._path.bucket,
'Body': content,
'Key': key,
'ContentType': (content_type or 'application/octet-stream'),
'ACL': ACL,
'ContentMD5': md5(content),
}
# keep gzip as default
if compress == "br":
attrs['ContentEncoding'] = 'br'
elif compress in GZIP_TYPES:
attrs['ContentEncoding'] = 'gzip'
elif compress == "zstd":
attrs['ContentEncoding'] = 'zstd'
elif compress:
raise ValueError("Compression type {} not supported.".format(compress))
if cache_control:
attrs['CacheControl'] = cache_control
self._conn.put_object(**attrs)
@retry
def get_file(self, file_path, start=None, end=None):
"""
There are many types of execptions which can get raised
from this method. We want to make sure we only return
None when the file doesn't exist.
"""
kwargs = {}
if start is not None or end is not None:
start = int(start) if start is not None else ''
end = int(end - 1) if end is not None else ''
kwargs['Range'] = "bytes={}-{}".format(start, end)
try:
resp = self._conn.get_object(
Bucket=self._path.bucket,
Key=self.get_path_to_file(file_path),
**kwargs
)
encoding = ''
if 'ContentEncoding' in resp:
encoding = resp['ContentEncoding']
# s3 etags return hex digests but we need the base64 encoding
# to make uniform comparisons.
# example s3 etag: "31ee76261d87fed8cb9d4c465c48158c"
etag = resp.get('ETag', None)
if etag is not None:
etag = etag.lstrip('"').rstrip('"')
etag = base64.b64encode(binascii.unhexlify(etag)).decode('utf8')
return (resp['Body'].read(), encoding, etag, "md5")
except botocore.exceptions.ClientError as err:
if err.response['Error']['Code'] == 'NoSuchKey':
return (None, None, None, None)
else:
raise
@retry
def size(self, file_path):
try:
response = self._conn.head_object(
Bucket=self._path.bucket,
Key=self.get_path_to_file(file_path),
)
return response['ContentLength']
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == "404":
return None
raise
def exists(self, file_path):
exists = True
try:
self._conn.head_object(
Bucket=self._path.bucket,
Key=self.get_path_to_file(file_path),
)
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == "404":
exists = False
else:
raise
return exists
def files_exist(self, file_paths):
return { path: self.exists(path) for path in file_paths }
@retry
def delete_file(self, file_path):
# Not necessary to handle 404s here.
# From the boto3 documentation:
# delete_object(**kwargs)
# Removes the null version (if there is one) of an object and inserts a delete marker,
# which becomes the latest version of the object. If there isn't a null version,
# Amazon S3 does not remove any objects.
# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.delete_object
self._conn.delete_object(
Bucket=self._path.bucket,
Key=self.get_path_to_file(file_path),
)
@retry
def delete_files(self, file_paths):
for batch in sip(file_paths, self.delete_batch_size):
response = self._conn.delete_objects(
Bucket=self._path.bucket,
Delete={
'Objects': [
{ 'Key': self.get_path_to_file(filepath) } for filepath in batch
],
'Quiet': True
},
)
def list_files(self, prefix, flat=False):
"""
List the files in the layer with the given prefix.
flat means only generate one level of a directory,
while non-flat means generate all file paths with that
prefix.
"""
layer_path = self.get_path_to_file("")
path = posixpath.join(layer_path, prefix)
@retry
def s3lst(continuation_token=None):
kwargs = {
'Bucket': self._path.bucket,
'Prefix': path,
}
if continuation_token:
kwargs['ContinuationToken'] = continuation_token
return self._conn.list_objects_v2(**kwargs)
resp = s3lst()
def iterate(resp):
if 'Contents' not in resp.keys():
resp['Contents'] = []
for item in resp['Contents']:
key = item['Key']
filename = key.replace(layer_path, '')
if not flat and filename[-1] != '/':
yield filename
elif flat and '/' not in key.replace(path, ''):
yield filename
for filename in iterate(resp):
yield filename
while resp['IsTruncated'] and resp['NextContinuationToken']:
resp = s3lst(resp['NextContinuationToken'])
for filename in iterate(resp):
yield filename
def release_connection(self):
global S3_POOL
S3_POOL[self._path.protocol][self._path.bucket].release_connection(self._conn)
def _radix_sort(L, i=0):
"""
Most significant char radix sort
"""
if len(L) <= 1:
return L
done_bucket = []
buckets = [ [] for x in range(255) ]
for s in L:
if i >= len(s):
done_bucket.append(s)
else:
buckets[ ord(s[i]) ].append(s)
buckets = [ _radix_sort(b, i + 1) for b in buckets ]
return done_bucket + [ b for blist in buckets for b in blist ]