This repository has been archived by the owner on Jan 18, 2020. It is now read-only.
forked from snowflakedb/snowflake-connector-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote_storage_util.py
300 lines (271 loc) · 11.9 KB
/
remote_storage_util.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012-2019 Snowflake Computing Inc. All right reserved.
#
from __future__ import division
import os
import shutil
import time
from collections import namedtuple
from logging import getLogger
from .azure_util import SnowflakeAzureUtil
from .constants import (SHA256_DIGEST, ResultStatus)
from .encryption_util import (SnowflakeEncryptionUtil)
from .gcs_util import SnowflakeGCSUtil
from .s3_util import SnowflakeS3Util
DEFAULT_CONCURRENCY = 1
DEFAULT_MAX_RETRY = 5
"""
Encryption Material
"""
SnowflakeFileEncryptionMaterial = namedtuple(
"SnowflakeS3FileEncryptionMaterial", [
"query_stage_master_key", # query stage master key
"query_id", # query id
"smk_id" # SMK id
]
)
class NeedRenewTokenError(Exception):
pass
class SnowflakeRemoteStorageUtil(object):
@staticmethod
def getForStorageType(type):
if (type == u'S3'):
return SnowflakeS3Util
elif (type == u'AZURE'):
return SnowflakeAzureUtil
elif (type == u'GCS'):
return SnowflakeGCSUtil
else:
return None
@staticmethod
def create_client(stage_info, use_accelerate_endpoint=False):
util_class = SnowflakeRemoteStorageUtil.getForStorageType(
stage_info[u'locationType'])
return util_class.create_client(
stage_info,
use_accelerate_endpoint=use_accelerate_endpoint)
@staticmethod
def upload_one_file(meta):
"""
Uploads a file to S3
:param meta: a file meta
"""
logger = getLogger(__name__)
encryption_metadata = None
if u'encryption_material' in meta:
(encryption_metadata,
data_file) = SnowflakeEncryptionUtil.encrypt_file(
meta[u'encryption_material'],
meta[u'real_src_file_name'], tmp_dir=meta[u'tmp_dir'])
logger.debug(
u'encrypted data file=%s, size=%s', data_file,
os.path.getsize(data_file))
else:
logger.debug(u'not encrypted data file')
data_file = meta[u'real_src_file_name']
util_class = SnowflakeRemoteStorageUtil.getForStorageType(
meta[u'stage_info'][u'locationType'])
if not meta.get(u'overwrite'):
file_header = util_class.get_file_header(
meta, meta[u'dst_file_name'])
if meta[u'result_status'] == ResultStatus.RENEW_TOKEN:
# need renew token
return
elif meta[u'result_status'] == ResultStatus.RENEW_PRESIGNED_URL:
return
elif file_header and \
meta[u'result_status'] == ResultStatus.UPLOADED:
logger.debug(
u'file already exists, checking digest: '
u'location="%s", file_name="%s"',
meta[u'stage_info'][u'location'],
meta[u'dst_file_name'])
sfc_digest = file_header.digest
if sfc_digest == meta[SHA256_DIGEST]:
logger.debug(u'file digest matched: digest=%s',
sfc_digest)
meta[u'dst_file_size'] = 0
meta[u'error_details'] = \
(u'File with the same destination name '
u'and checksum already exists')
meta[u'result_status'] = ResultStatus.SKIPPED
return
else:
logger.debug(
u"file digest didn't match: digest_s3=%s, "
u"digest_local=%s",
sfc_digest, meta[SHA256_DIGEST])
logger.debug(u'putting a file: %s, %s',
meta[u'stage_info'][u'location'], meta[u'dst_file_name'])
max_concurrency = meta[u'parallel']
last_err = None
max_retry = DEFAULT_MAX_RETRY
for retry in range(max_retry):
util_class.upload_file(
data_file,
meta,
encryption_metadata,
max_concurrency
)
if (meta[u'result_status'] == ResultStatus.UPLOADED):
return
elif (meta[u'result_status'] == ResultStatus.RENEW_TOKEN):
return
elif (meta[u'result_status'] == ResultStatus.RENEW_PRESIGNED_URL):
return
elif (meta[u'result_status'] == ResultStatus.NEED_RETRY):
last_err = meta[u'last_error']
logger.debug(
'Failed to upload a file: %s, err: %s. Retrying with '
'max concurrency: %s',
data_file, last_err, max_concurrency)
if 'no_sleeping_time' not in meta:
sleeping_time = min(2 ** retry, 16)
logger.debug(u"sleeping: %s", sleeping_time)
time.sleep(sleeping_time)
elif (meta[
u'result_status'] == ResultStatus.NEED_RETRY_WITH_LOWER_CONCURRENCY):
last_err = meta[u'last_error']
max_concurrency = meta[u'parallel'] - int(
retry * meta[u'parallel'] / max_retry)
max_concurrency = max(DEFAULT_CONCURRENCY, max_concurrency)
meta['last_max_concurrency'] = max_concurrency
logger.debug(
'Failed to upload a file: %s, err: %s. Retrying with '
'max concurrency: %s',
data_file, last_err, max_concurrency)
if 'no_sleeping_time' not in meta:
sleeping_time = min(2 ** retry, 16)
logger.debug(u"sleeping: %s", sleeping_time)
time.sleep(sleeping_time)
else:
if last_err:
raise last_err
else:
raise Exception(
"Unknown Error in uploading a file: %s",
data_file)
@staticmethod
def download_one_file(meta):
"""
Downloads a file from S3
:param meta: file meta
"""
logger = getLogger(__name__)
full_dst_file_name = os.path.join(
meta[u'local_location'],
os.path.basename(meta[u'dst_file_name']))
full_dst_file_name = os.path.realpath(full_dst_file_name)
# TODO: validate full_dst_file_name is under the writable directory
base_dir = os.path.dirname(full_dst_file_name)
if not os.path.exists(base_dir):
os.makedirs(base_dir)
util_class = SnowflakeRemoteStorageUtil.getForStorageType(
meta[u'stage_info'][u'locationType'])
file_header = util_class.get_file_header(meta, meta[u'src_file_name'])
if file_header:
meta[u'src_file_size'] = file_header.content_length
full_dst_file_name = os.path.join(
meta[u'local_location'],
os.path.basename(meta[u'dst_file_name']))
full_dst_file_name = os.path.realpath(full_dst_file_name)
max_concurrency = meta[u'parallel']
last_err = None
max_retry = DEFAULT_MAX_RETRY
for retry in range(max_retry):
util_class._native_download_file(meta, full_dst_file_name,
max_concurrency)
if (meta[u'result_status'] == ResultStatus.DOWNLOADED):
if u'encryption_material' in meta:
logger.debug(
u'encrypted data file=%s', full_dst_file_name)
# For storage utils that do not have the privilege of
# getting the metadata early, both object and metadata
# are downloaded at once. In which case, the file meta will
# be updated with all the metadata that we need and
# then we can call get_file_header to get just that and also
# preserve the idea of getting metadata in the first place.
# One example of this is the utils that use presigned url
# for upload/download and not the storage client library.
if meta.get(u'presigned_url', None):
file_header = util_class.get_file_header(meta, meta[
u'src_file_name'])
tmp_dst_file_name = SnowflakeEncryptionUtil.decrypt_file(
file_header.encryption_metadata,
meta[u'encryption_material'],
full_dst_file_name, tmp_dir=meta[u'tmp_dir'])
shutil.copyfile(tmp_dst_file_name, full_dst_file_name)
os.unlink(tmp_dst_file_name)
else:
logger.debug(u'not encrypted data file=%s',
full_dst_file_name)
statinfo = os.stat(full_dst_file_name)
meta[u'dst_file_size'] = statinfo.st_size
return
elif (meta[u'result_status'] == ResultStatus.RENEW_PRESIGNED_URL):
return
elif (meta[u'result_status'] == ResultStatus.RENEW_TOKEN):
return
elif (meta[
u'result_status'] == ResultStatus.NEED_RETRY_WITH_LOWER_CONCURRENCY):
max_concurrency = meta[u'parallel'] - int(
retry * meta[u'parallel'] / max_retry)
max_concurrency = max(DEFAULT_CONCURRENCY, max_concurrency)
meta['last_max_concurrency'] = max_concurrency
last_err = meta[u'last_error']
logger.debug(
'Failed to download a file: %s, err: %s. Retrying with '
'max concurrency: %s',
full_dst_file_name, last_err, max_concurrency)
if 'no_sleeping_time' not in meta:
sleeping_time = min(2 ** retry, 16)
logger.debug(u"sleeping: %s", sleeping_time)
time.sleep(sleeping_time)
elif (meta[u'result_status'] == ResultStatus.NEED_RETRY):
last_err = meta[u'last_error']
logger.debug(
'Failed to download a file: %s, err: %s. Retrying with '
'max concurrency: %s',
full_dst_file_name, last_err, max_concurrency)
if 'no_sleeping_time' not in meta:
sleeping_time = min(2 ** retry, 16)
logger.debug(u"sleeping: %s", sleeping_time)
time.sleep(sleeping_time)
else:
if last_err:
raise last_err
else:
raise Exception(
"Unknown Error in downloading a file: %s",
full_dst_file_name)
@staticmethod
def upload_one_file_with_retry(meta):
"""
Uploads one file with retry
:param meta: a file meta
"""
logger = getLogger(__name__)
util_class = SnowflakeRemoteStorageUtil.getForStorageType(
meta[u'stage_info'][u'locationType'])
for _ in range(10):
# retry
SnowflakeRemoteStorageUtil.upload_one_file(meta)
if meta[u'result_status'] == ResultStatus.UPLOADED:
for _ in range(10):
util_class.get_file_header(
meta, meta[u'dst_file_name'])
if meta[u'result_status'] == ResultStatus.NOT_FOUND_FILE:
time.sleep(1) # wait 1 second
logger.debug('not found. double checking...')
continue
break
else:
# not found. retry with the outer loop
logger.debug('not found. gave up. reuploading...')
continue
break
else:
# could not upload a file even after retry
meta[u'result_status'] = ResultStatus.ERROR