-
Notifications
You must be signed in to change notification settings - Fork 1
/
swift.py
executable file
·178 lines (145 loc) · 7.09 KB
/
swift.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Licensed under the MIT license:
# http://www.opensource.org/licenses/mit-license
# Copyright (c) 2015 Wikimedia Foundation
#
# Stores results in Swift via HTTP
import datetime
import logging
import random
from swiftclient import client
from swiftclient.exceptions import ClientException
from thumbor.result_storages import BaseStorage, ResultStorageResult
from thumbor.utils import logger
from wikimedia_thumbor.logging import record_timing, log_extra
class Storage(BaseStorage):
swiftconn = None
swiftconn_private = None
@property
def swift(self):
if self.context.private:
if Storage.swiftconn_private:
logging.debug("Returning private connection for result storage")
return Storage.swiftconn_private
else:
if Storage.swiftconn:
return Storage.swiftconn
authurl = (
self.context.config.SWIFT_HOST +
self.context.config.SWIFT_AUTH_PATH
)
# This allows us to set the value via config, instead of depending on the
# x-storage-url header returned by Swift during auth. This is a requirement
# for communicating with Swift via HTTPS.
os_options = {
'object_storage_url': self.context.config.SWIFT_HOST + self.context.config.SWIFT_API_PATH
}
conn = client.Connection(
user=self.context.config.SWIFT_PRIVATE_USER if self.context.private else self.context.config.SWIFT_USER,
key=self.context.config.SWIFT_PRIVATE_KEY if self.context.private else self.context.config.SWIFT_KEY,
authurl=authurl,
timeout=self.context.config.SWIFT_CONNECTION_TIMEOUT,
retries=self.context.config.SWIFT_RETRIES,
cacert=self.context.config.HTTP_LOADER_CA_CERTS,
os_options=os_options
)
if self.context.private:
logging.debug("Setting private connection for result storage")
Storage.swiftconn_private = conn
else:
Storage.swiftconn = conn
return conn
def uri(self): # pragma: no cover
return (
self.context.config.SWIFT_HOST +
self.context.wikimedia_thumbnail_container +
'/' +
self.context.wikimedia_thumbnail_save_path
)
# Coverage strangely reports lines lacking coverage in that function that
# don't make sense
async def put(self, bytes): # pragma: no cover
self.debug('[SWIFT_STORAGE] put')
if not hasattr(self.context, 'wikimedia_thumbnail_container'):
return
try:
# We store the xkey alongside the object if it's set.
# This way when an thumbnail falls our of Varnish and is picked
# up from Swift again, it will have an xkey. Which lets us avoid
# computing the xkey in Varnish. It always comes from Thumbor.
xkey = self.context.request_handler._headers.get_list('xkey')
content_type = self.context.request_handler._headers.get_list('content-type')
content_disposition = self.context.request_handler._headers.get_list('content-disposition')
headers = {}
if len(content_disposition):
headers['Content-Disposition'] = content_disposition[0]
if len(xkey):
headers['Xkey'] = xkey[0]
expiry = self.context.config.get('SWIFT_THUMBNAIL_EXPIRY_SECONDS', 0)
sampling_factor = self.context.config.get('SWIFT_THUMBNAIL_EXPIRY_SAMPLING_FACTOR', 0)
if expiry > 0 and sampling_factor > 0 and random.randint(1, sampling_factor) == 1:
headers['X-Delete-After'] = expiry
content_type = content_type[0] if len(content_type) else None
start = datetime.datetime.now()
self.swift.put_object(
self.context.wikimedia_thumbnail_container,
self.context.wikimedia_thumbnail_save_path,
bytes,
headers=headers,
content_type=content_type
)
record_timing(self.context, datetime.datetime.now() - start, 'swift.thumbnail.write.success')
# We cannot set the time spent in putting to swift in the response
# headers, because the response has already been sent when saving
# to Swift happens (which is the right thing to do).
except AssertionError as e:
# Let assertion errors go through for tests
raise e
except Exception as e:
record_timing(self.context, datetime.datetime.now() - start, 'swift.thumbnail.write.exception')
self.error('[SWIFT_STORAGE] put exception: %r' % e)
# We cannnot let exceptions bubble up, because they would leave
# the client's connection hanging
async def get(self):
self.debug('[SWIFT_STORAGE] get: %r %r' % (
self.context.wikimedia_thumbnail_container,
self.context.wikimedia_thumbnail_save_path
)
)
try:
# swiftclient has this annoying habit of writing an ERROR log
# entry for the ClientException, regardless of it being caught
start = datetime.datetime.now()
# hnowlan: Temporarily disable log disabling here in case we're missing a valid exception
# logging.disable(logging.ERROR)
headers, data = self.swift.get_object(
self.context.wikimedia_thumbnail_container,
self.context.wikimedia_thumbnail_save_path
)
# hnowlan: Temporarily disable log disabling here in case we're missing a valid exception
# logging.disable(logging.NOTSET)
record_timing(self.context, datetime.datetime.now() - start, 'swift.thumbnail.read.success', 'Thumbor-Swift-Thumbnail-Success-Time')
self.debug('[SWIFT_STORAGE] found')
return ResultStorageResult(buffer=data, metadata=headers)
# We want this to be exhaustive because not catching an exception here
# would result in the request hanging indefinitely
except ClientException:
# logging.disable(logging.NOTSET)
record_timing(self.context, datetime.datetime.now() - start, 'swift.thumbnail.read.miss', 'Thumbor-Swift-Thumbnail-Miss-Time')
# No need to log this one, it's expected behavior when the
# requested object isn't there
self.debug('[SWIFT_STORAGE] missing')
return None
except AssertionError as e:
# Let assertion errors go through for tests
raise e
except Exception as e:
# logging.disable(logging.NOTSET)
record_timing(self.context, datetime.datetime.now() - start, 'swift.thumbnail.read.exception', 'Thumbor-Swift-Thumbnail-Exception-Time')
self.error('[SWIFT_STORAGE] get exception: %r' % e)
return None
def debug(self, message):
logger.debug(message, extra=log_extra(self.context))
def error(self, message):
logger.error(message, extra=log_extra(self.context))