GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/network/s3fanout.cc
Date: 2026-10-25 02:40:14
Exec Total Coverage
Lines: 649 919 70.6%
Branches: 469 1337 35.1%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 *
4 * Runs a thread using libcurls asynchronous I/O mode to push data to S3
5 */
6
7 #include "s3fanout.h"
8
9 #include <pthread.h>
10
11 #include <algorithm>
12 #include <cassert>
13 #include <cerrno>
14 #include <cstring>
15 #include <utility>
16
17 #include "crypto/hash.h"
18 #include "util/exception.h"
19 #include "util/platform.h"
20 #include "util/posix.h"
21 #include "util/string.h"
22
23 using namespace std; // NOLINT
24
25 namespace s3fanout {
26
27 /**
28 * Escapes characters that are not allowed in XML text content.
29 * Only & and < need escaping; >, ', " are safe in text nodes.
30 */
31 6346 static string XmlEscape(const string &input) {
32 6346 string result;
33
1/2
✓ Branch 2 taken 6346 times.
✗ Branch 3 not taken.
6346 result.reserve(input.size());
34
2/2
✓ Branch 1 taken 142804 times.
✓ Branch 2 taken 6346 times.
149150 for (unsigned i = 0; i < input.size(); ++i) {
35
3/3
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 48 times.
✓ Branch 3 taken 142708 times.
142804 switch (input[i]) {
36 48 case '&':
37
1/2
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
48 result += "&amp;";
38 48 break;
39 48 case '<':
40
1/2
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
48 result += "&lt;";
41 48 break;
42 142708 default:
43
1/2
✓ Branch 2 taken 142708 times.
✗ Branch 3 not taken.
142708 result += input[i];
44 142708 break;
45 }
46 }
47 6346 return result;
48 }
49
50
51 /**
52 * The CanonicalizedResource of an S3 V2 signature. It must mirror MkUrl():
53 * the server rebuilds the resource from the request path, so any difference,
54 * down to a trailing slash, yields SignatureDoesNotMatch.
55 *
56 * With DNS buckets the bucket lives in the hostname and the path is
57 * "/" + object_key, so an empty key gives "/<bucket>/". In path style the
58 * path already carries the bucket and an empty key gives "/<bucket>".
59 */
60 12568 string MkV2CanonicalResource(const string &bucket, const string &object_key,
61 bool dns_buckets, bool multi_delete) {
62 12568 string resource = "/" + bucket;
63
6/6
✓ Branch 0 taken 12424 times.
✓ Branch 1 taken 144 times.
✓ Branch 3 taken 12258 times.
✓ Branch 4 taken 166 times.
✓ Branch 5 taken 12402 times.
✓ Branch 6 taken 166 times.
12568 if (dns_buckets || !object_key.empty())
64
2/4
✓ Branch 1 taken 12402 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12402 times.
✗ Branch 5 not taken.
12402 resource += "/" + object_key;
65 // V2 requires subresources to be signed; multi-delete posts to "?delete"
66
2/2
✓ Branch 0 taken 166 times.
✓ Branch 1 taken 12402 times.
12568 if (multi_delete)
67
1/2
✓ Branch 1 taken 166 times.
✗ Branch 2 not taken.
166 resource += "?delete";
68 12568 return resource;
69 }
70
71
72 /**
73 * Builds an S3 DeleteObjects XML request body for multi-object delete.
74 * Uses Quiet mode so the response only contains errors, not successes.
75 */
76 262 string ComposeDeleteMultiXml(const vector<string> &keys) {
77 string xml = "<?xml version=\"1.0\" "
78
1/2
✓ Branch 2 taken 262 times.
✗ Branch 3 not taken.
262 "encoding=\"UTF-8\"?><Delete><Quiet>true</Quiet>";
79 // ~70 bytes per <Object><Key>...</Key></Object> entry
80
1/2
✓ Branch 3 taken 262 times.
✗ Branch 4 not taken.
262 xml.reserve(xml.size() + keys.size() * 70 + 10);
81
2/2
✓ Branch 1 taken 6346 times.
✓ Branch 2 taken 262 times.
6608 for (unsigned i = 0; i < keys.size(); ++i) {
82
4/8
✓ Branch 2 taken 6346 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 6346 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 6346 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 6346 times.
✗ Branch 12 not taken.
6346 xml += "<Object><Key>" + XmlEscape(keys[i]) + "</Key></Object>";
83 }
84
1/2
✓ Branch 1 taken 262 times.
✗ Branch 2 not taken.
262 xml += "</Delete>";
85 262 return xml;
86 }
87
88
89 /**
90 * Parses the S3 DeleteObjects error response XML.
91 * In Quiet mode, the response only contains <Error> elements for failed keys.
92 * Returns the number of errors found.
93 */
94 192 unsigned ParseDeleteMultiResponse(const string &response,
95 vector<string> *error_keys,
96 vector<string> *error_codes,
97 vector<string> *error_messages) {
98 192 unsigned num_errors = 0;
99 192 string::size_type pos = 0;
100
101 while (true) {
102 336 const string::size_type err_start = response.find("<Error>", pos);
103
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 192 times.
336 if (err_start == string::npos)
104 144 break;
105 192 const string::size_type err_end = response.find("</Error>", err_start);
106
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 144 times.
192 if (err_end == string::npos)
107 48 break;
108
109
1/2
✓ Branch 1 taken 144 times.
✗ Branch 2 not taken.
144 const string error_block = response.substr(err_start, err_end - err_start);
110 144 num_errors++;
111
112 // Extract <Key>...</Key>
113 144 const string::size_type key_start = error_block.find("<Key>");
114 144 const string::size_type key_end = error_block.find("</Key>");
115
2/4
✓ Branch 0 taken 144 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 144 times.
✗ Branch 3 not taken.
144 if (key_start != string::npos && key_end != string::npos) {
116
1/2
✓ Branch 1 taken 144 times.
✗ Branch 2 not taken.
144 error_keys->push_back(
117
1/2
✓ Branch 1 taken 144 times.
✗ Branch 2 not taken.
288 error_block.substr(key_start + 5, key_end - key_start - 5));
118 } else {
119 ✗ error_keys->push_back("");
120 }
121
122 // Extract <Code>...</Code>
123 144 const string::size_type code_start = error_block.find("<Code>");
124 144 const string::size_type code_end = error_block.find("</Code>");
125
2/4
✓ Branch 0 taken 144 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 144 times.
✗ Branch 3 not taken.
144 if (code_start != string::npos && code_end != string::npos) {
126
1/2
✓ Branch 1 taken 144 times.
✗ Branch 2 not taken.
144 error_codes->push_back(
127
1/2
✓ Branch 1 taken 144 times.
✗ Branch 2 not taken.
288 error_block.substr(code_start + 6, code_end - code_start - 6));
128 } else {
129 ✗ error_codes->push_back("");
130 }
131
132 // Extract <Message>...</Message>
133 144 const string::size_type msg_start = error_block.find("<Message>");
134 144 const string::size_type msg_end = error_block.find("</Message>");
135
2/4
✓ Branch 0 taken 144 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 144 times.
✗ Branch 3 not taken.
144 if (msg_start != string::npos && msg_end != string::npos) {
136
1/2
✓ Branch 1 taken 144 times.
✗ Branch 2 not taken.
144 error_messages->push_back(
137
1/2
✓ Branch 1 taken 144 times.
✗ Branch 2 not taken.
288 error_block.substr(msg_start + 9, msg_end - msg_start - 9));
138 } else {
139 ✗ error_messages->push_back("");
140 }
141
142 144 pos = err_end + 8; // length of "</Error>"
143 144 }
144
145 192 return num_errors;
146 }
147
148 const char *S3FanoutManager::kCacheControlCas = "Cache-Control: max-age=259200";
149 const unsigned S3FanoutManager::kDefault429ThrottleMs = 250;
150 const unsigned S3FanoutManager::kMax429ThrottleMs = 10000;
151 const unsigned S3FanoutManager::kThrottleReportIntervalSec = 10;
152 const unsigned S3FanoutManager::kDefaultHTTPPort = 80;
153 const unsigned S3FanoutManager::kDefaultHTTPSPort = 443;
154
155 130 std::string S3FanoutManager::MkDotCvmfsCacheControlHeader(
156 unsigned defaultMaxAge, int overrideMaxAge) {
157 const char *var;
158 int max_age_sec;
159 char *at_null_terminator_if_number;
160 130 bool value_determined = false;
161
162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
130 if (overrideMaxAge >= 0) {
163 ✗ max_age_sec = overrideMaxAge;
164 ✗ value_determined = true;
165 }
166
167
1/2
✓ Branch 0 taken 130 times.
✗ Branch 1 not taken.
130 if (!value_determined) {
168 130 var = getenv("CVMFS_MAX_TTL_SECS");
169
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
130 if (var && var[0]) {
170 ✗ max_age_sec = strtoll(var, &at_null_terminator_if_number, 10);
171 ✗ if (*at_null_terminator_if_number == '\0' && max_age_sec >= 0) {
172 ✗ value_determined = true;
173 }
174 }
175 }
176
177
1/2
✓ Branch 0 taken 130 times.
✗ Branch 1 not taken.
130 if (!value_determined) {
178 130 var = getenv("CVMFS_MAX_TTL");
179
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
130 if (var && var[0]) {
180 ✗ max_age_sec = strtoll(var, &at_null_terminator_if_number, 10);
181 ✗ if (*at_null_terminator_if_number == '\0' && max_age_sec >= 0) {
182 ✗ max_age_sec *= 60;
183 ✗ value_determined = true;
184 }
185 }
186 }
187
188
1/2
✓ Branch 0 taken 130 times.
✗ Branch 1 not taken.
130 if (!value_determined) {
189 130 max_age_sec = defaultMaxAge;
190 }
191
192
2/4
✓ Branch 1 taken 130 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 130 times.
✗ Branch 5 not taken.
260 return "Cache-Control: max-age=" + std::to_string(max_age_sec);
193 }
194
195 /**
196 * Parses Retry-After and X-Retry-In headers attached to HTTP 429 responses
197 */
198 792 void S3FanoutManager::DetectThrottleIndicator(const std::string &header,
199 JobInfo *info) {
200 792 std::string value_str;
201
4/6
✓ Branch 2 taken 792 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 792 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 376 times.
✓ Branch 10 taken 416 times.
792 if (HasPrefix(header, "retry-after:", true))
202
1/2
✓ Branch 1 taken 376 times.
✗ Branch 2 not taken.
376 value_str = header.substr(12);
203
4/6
✓ Branch 2 taken 792 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 792 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 240 times.
✓ Branch 10 taken 552 times.
792 if (HasPrefix(header, "x-retry-in:", true))
204
1/2
✓ Branch 1 taken 240 times.
✗ Branch 2 not taken.
240 value_str = header.substr(11);
205
206
1/2
✓ Branch 1 taken 792 times.
✗ Branch 2 not taken.
792 value_str = Trim(value_str, true /* trim_newline */);
207
2/2
✓ Branch 1 taken 520 times.
✓ Branch 2 taken 272 times.
792 if (!value_str.empty()) {
208
1/2
✓ Branch 1 taken 520 times.
✗ Branch 2 not taken.
520 const unsigned value_numeric = String2Uint64(value_str);
209
2/4
✓ Branch 2 taken 520 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 520 times.
✗ Branch 6 not taken.
1040 const unsigned value_ms = HasSuffix(value_str, "ms", true /* ignore_case */)
210
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 280 times.
520 ? value_numeric
211 520 : (value_numeric * 1000);
212
2/2
✓ Branch 0 taken 472 times.
✓ Branch 1 taken 48 times.
520 if (value_ms > 0)
213 472 info->throttle_ms = std::min(value_ms, kMax429ThrottleMs);
214 }
215 792 }
216
217
218 /**
219 * Called by curl for every HTTP header. Not called for file:// transfers.
220 */
221 37000 static size_t CallbackCurlHeader(void *ptr, size_t size, size_t nmemb,
222 void *info_link) {
223 37000 const size_t num_bytes = size * nmemb;
224
1/2
✓ Branch 2 taken 37000 times.
✗ Branch 3 not taken.
37000 const string header_line(static_cast<const char *>(ptr), num_bytes);
225 37000 JobInfo *info = static_cast<JobInfo *>(info_link);
226
227 // Check for http status code errors
228
4/6
✓ Branch 2 taken 37000 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 37000 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 12320 times.
✓ Branch 10 taken 24680 times.
37000 if (HasPrefix(header_line, "HTTP/1.", false)) {
229
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12320 times.
12320 if (header_line.length() < 10)
230 ✗ return 0;
231
232 unsigned i;
233
5/6
✓ Branch 1 taken 24640 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12320 times.
✓ Branch 5 taken 12320 times.
✓ Branch 6 taken 12320 times.
✓ Branch 7 taken 12320 times.
24640 for (i = 8; (i < header_line.length()) && (header_line[i] == ' '); ++i) {
234 }
235
236
2/2
✓ Branch 1 taken 6180 times.
✓ Branch 2 taken 6140 times.
12320 if (header_line[i] == '2') {
237 6180 return num_bytes;
238 } else {
239
1/2
✓ Branch 2 taken 6140 times.
✗ Branch 3 not taken.
6140 LogCvmfs(kLogS3Fanout, kLogDebug, "http status error code [info %p]: %s",
240 info, header_line.c_str());
241
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6140 times.
6140 if (header_line.length() < i + 3) {
242 ✗ LogCvmfs(kLogS3Fanout, kLogStderr, "S3: invalid HTTP response '%s'",
243 header_line.c_str());
244 ✗ info->error_code = kFailOther;
245 ✗ return 0;
246 }
247
2/4
✓ Branch 3 taken 6140 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 6140 times.
✗ Branch 7 not taken.
6140 info->http_error = String2Int64(string(&header_line[i], 3));
248
249
2/7
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 6100 times.
✗ Branch 6 not taken.
6140 switch (info->http_error) {
250 40 case 429:
251 40 info->error_code = kFailRetry;
252 40 info->throttle_ms = S3FanoutManager::kDefault429ThrottleMs;
253 40 info->throttle_timestamp = platform_monotonic_time();
254 40 return num_bytes;
255 ✗ case 507: // Insufficient Storage
256 ✗ info->error_code = kFailInsufficientStorage;
257 ✗ break;
258 ✗ case 503:
259 case 502: // Can happen if the S3 gateway-backend connection breaks
260 case 500: // sometimes see this as a transient error from S3
261 ✗ info->error_code = kFailServiceUnavailable;
262 ✗ break;
263 ✗ case 501:
264 case 400:
265 ✗ info->error_code = kFailBadRequest;
266 ✗ break;
267 ✗ case 403:
268 ✗ info->error_code = kFailForbidden;
269 ✗ break;
270 6100 case 404:
271 6100 info->error_code = kFailNotFound;
272 6100 return num_bytes;
273 ✗ default:
274 ✗ info->error_code = kFailOther;
275 }
276 ✗ return 0;
277 }
278 }
279
280
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 24560 times.
24680 if (info->error_code == kFailRetry) {
281
1/2
✓ Branch 1 taken 120 times.
✗ Branch 2 not taken.
120 S3FanoutManager::DetectThrottleIndicator(header_line, info);
282 }
283
284 24680 return num_bytes;
285 37000 }
286
287
288 /**
289 * Called by curl for every new chunk to upload.
290 */
291 156760 static size_t CallbackCurlData(void *ptr, size_t size, size_t nmemb,
292 void *info_link) {
293 156760 const size_t num_bytes = size * nmemb;
294 156760 JobInfo *info = static_cast<JobInfo *>(info_link);
295
296 156760 LogCvmfs(kLogS3Fanout, kLogDebug, "Data callback with %zu bytes", num_bytes);
297
298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156760 times.
156760 if (num_bytes == 0)
299 ✗ return 0;
300
301 156760 const uint64_t read_bytes = info->origin->Read(ptr, num_bytes);
302
303 156760 LogCvmfs(kLogS3Fanout, kLogDebug, "source buffer pushed out %lu bytes",
304 read_bytes);
305
306 156760 return read_bytes;
307 }
308
309
310 /**
311 * Captures the HTTP response body. For kReqDeleteMulti, the response contains
312 * XML error information. For other requests, the body is ignored.
313 */
314 ✗ static size_t CallbackCurlBody(char *ptr, size_t size, size_t nmemb,
315 void *userdata) {
316 ✗ const size_t num_bytes = size * nmemb;
317 ✗ JobInfo *info = static_cast<JobInfo *>(userdata);
318 ✗ if (info != NULL && info->request == JobInfo::kReqDeleteMulti) {
319 ✗ info->response_body.append(ptr, num_bytes);
320 }
321 ✗ return num_bytes;
322 }
323
324
325 /**
326 * Called when new curl sockets arrive or existing curl sockets depart.
327 */
328 27310 int S3FanoutManager::CallbackCurlSocket(CURL *easy, curl_socket_t s, int action,
329 void *userp, void *socketp) {
330 27310 S3FanoutManager *s3fanout_mgr = static_cast<S3FanoutManager *>(userp);
331 27310 LogCvmfs(kLogS3Fanout, kLogDebug,
332 "CallbackCurlSocket called with easy "
333 "handle %p, socket %d, action %d, up %p, "
334 "sp %p, fds_inuse %d, jobs %d",
335 easy, s, action, userp, socketp, s3fanout_mgr->watch_fds_inuse_,
336 27310 s3fanout_mgr->available_jobs_->Get());
337
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27310 times.
27310 if (action == CURL_POLL_NONE)
338 ✗ return 0;
339
340 // Find s in watch_fds_
341 // First 2 fds are job and terminate pipes (not curl related)
342 unsigned index;
343
2/2
✓ Branch 0 taken 42740 times.
✓ Branch 1 taken 12320 times.
55060 for (index = 2; index < s3fanout_mgr->watch_fds_inuse_; ++index) {
344
2/2
✓ Branch 0 taken 14990 times.
✓ Branch 1 taken 27750 times.
42740 if (s3fanout_mgr->watch_fds_[index].fd == s)
345 14990 break;
346 }
347 // Or create newly
348
2/2
✓ Branch 0 taken 12320 times.
✓ Branch 1 taken 14990 times.
27310 if (index == s3fanout_mgr->watch_fds_inuse_) {
349 // Extend array if necessary
350
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 12290 times.
12320 if (s3fanout_mgr->watch_fds_inuse_ == s3fanout_mgr->watch_fds_size_) {
351 30 s3fanout_mgr->watch_fds_size_ *= 2;
352 30 s3fanout_mgr->watch_fds_ = static_cast<struct pollfd *>(
353 30 srealloc(s3fanout_mgr->watch_fds_,
354 30 s3fanout_mgr->watch_fds_size_ * sizeof(struct pollfd)));
355 }
356 12320 s3fanout_mgr->watch_fds_[s3fanout_mgr->watch_fds_inuse_].fd = s;
357 12320 s3fanout_mgr->watch_fds_[s3fanout_mgr->watch_fds_inuse_].events = 0;
358 12320 s3fanout_mgr->watch_fds_[s3fanout_mgr->watch_fds_inuse_].revents = 0;
359 12320 s3fanout_mgr->watch_fds_inuse_++;
360 }
361
362
4/5
✓ Branch 0 taken 12320 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 2640 times.
✓ Branch 3 taken 12320 times.
✗ Branch 4 not taken.
27310 switch (action) {
363 12320 case CURL_POLL_IN:
364 12320 s3fanout_mgr->watch_fds_[index].events = POLLIN | POLLPRI;
365 12320 break;
366 30 case CURL_POLL_OUT:
367 30 s3fanout_mgr->watch_fds_[index].events = POLLOUT | POLLWRBAND;
368 30 break;
369 2640 case CURL_POLL_INOUT:
370 2640 s3fanout_mgr->watch_fds_[index].events = POLLIN | POLLPRI | POLLOUT
371 | POLLWRBAND;
372 2640 break;
373 12320 case CURL_POLL_REMOVE:
374
2/2
✓ Branch 0 taken 1980 times.
✓ Branch 1 taken 10340 times.
12320 if (index < s3fanout_mgr->watch_fds_inuse_ - 1)
375 s3fanout_mgr
376 1980 ->watch_fds_[index] = s3fanout_mgr->watch_fds_
377 1980 [s3fanout_mgr->watch_fds_inuse_ - 1];
378 12320 s3fanout_mgr->watch_fds_inuse_--;
379 // Shrink array if necessary
380
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12320 times.
12320 if ((s3fanout_mgr->watch_fds_inuse_ > s3fanout_mgr->watch_fds_max_)
381 ✗ && (s3fanout_mgr->watch_fds_inuse_
382 ✗ < s3fanout_mgr->watch_fds_size_ / 2)) {
383 ✗ s3fanout_mgr->watch_fds_size_ /= 2;
384 ✗ s3fanout_mgr->watch_fds_ = static_cast<struct pollfd *>(
385 ✗ srealloc(s3fanout_mgr->watch_fds_,
386 ✗ s3fanout_mgr->watch_fds_size_ * sizeof(struct pollfd)));
387 }
388 12320 break;
389 ✗ default:
390 ✗ PANIC(NULL);
391 }
392
393 27310 return 0;
394 }
395
396
397 /**
398 * Worker thread event loop.
399 */
400 130 void *S3FanoutManager::MainUpload(void *data) {
401
1/2
✓ Branch 1 taken 130 times.
✗ Branch 2 not taken.
130 LogCvmfs(kLogS3Fanout, kLogDebug, "Upload I/O thread started");
402 130 S3FanoutManager *s3fanout_mgr = static_cast<S3FanoutManager *>(data);
403
404 130 s3fanout_mgr->InitPipeWatchFds();
405
406 // Don't schedule more jobs into the multi handle than the maximum number of
407 // parallel connections. This should prevent starvation and thus a timeout
408 // of the authorization header (CVM-1339).
409 130 unsigned jobs_in_flight = 0;
410
411 while (true) {
412 // Check events with 100ms timeout
413 166900 const int timeout_ms = 100;
414
1/2
✓ Branch 1 taken 166900 times.
✗ Branch 2 not taken.
166900 int retval = poll(s3fanout_mgr->watch_fds_, s3fanout_mgr->watch_fds_inuse_,
415 timeout_ms);
416
2/2
✓ Branch 0 taken 980 times.
✓ Branch 1 taken 165920 times.
166900 if (retval == 0) {
417 // Handle timeout
418 980 int still_running = 0;
419
1/2
✓ Branch 1 taken 980 times.
✗ Branch 2 not taken.
980 retval = curl_multi_socket_action(s3fanout_mgr->curl_multi_,
420 CURL_SOCKET_TIMEOUT, 0, &still_running);
421
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 980 times.
980 if (retval != CURLM_OK) {
422 ✗ LogCvmfs(kLogS3Fanout, kLogStderr, "Error, timeout due to: %d", retval);
423 ✗ assert(retval == CURLM_OK);
424 }
425
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 165920 times.
165920 } else if (retval < 0) {
426 ✗ assert(errno == EINTR);
427 ✗ continue;
428 }
429
430 // Terminate I/O thread
431
2/2
✓ Branch 0 taken 130 times.
✓ Branch 1 taken 166770 times.
166900 if (s3fanout_mgr->watch_fds_[0].revents)
432 130 break;
433
434 // New job incoming
435
2/2
✓ Branch 0 taken 6200 times.
✓ Branch 1 taken 160570 times.
166770 if (s3fanout_mgr->watch_fds_[1].revents) {
436 6200 s3fanout_mgr->watch_fds_[1].revents = 0;
437 JobInfo *info;
438
1/2
✓ Branch 1 taken 6200 times.
✗ Branch 2 not taken.
6200 ReadPipe(s3fanout_mgr->pipe_jobs_[0], &info, sizeof(info));
439
1/2
✓ Branch 1 taken 6200 times.
✗ Branch 2 not taken.
6200 CURL *handle = s3fanout_mgr->AcquireCurlHandle();
440
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6200 times.
6200 if (handle == NULL) {
441 ✗ PANIC(kLogStderr, "Failed to acquire CURL handle.");
442 }
443
1/2
✓ Branch 1 taken 6200 times.
✗ Branch 2 not taken.
6200 const s3fanout::Failures init_failure = s3fanout_mgr->InitializeRequest(
444 info, handle);
445
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6200 times.
6200 if (init_failure != s3fanout::kFailOk) {
446 ✗ PANIC(kLogStderr,
447 "Failed to initialize CURL handle (error: %d - %s | errno: %d)",
448 init_failure, Code2Ascii(init_failure), errno);
449 }
450
1/2
✓ Branch 1 taken 6200 times.
✗ Branch 2 not taken.
6200 s3fanout_mgr->SetUrlOptions(info);
451
452
1/2
✓ Branch 1 taken 6200 times.
✗ Branch 2 not taken.
6200 curl_multi_add_handle(s3fanout_mgr->curl_multi_, handle);
453
1/2
✓ Branch 1 taken 6200 times.
✗ Branch 2 not taken.
6200 s3fanout_mgr->active_requests_->insert(info);
454 6200 jobs_in_flight++;
455 6200 int still_running = 0, retval = 0;
456
1/2
✓ Branch 1 taken 6200 times.
✗ Branch 2 not taken.
6200 retval = curl_multi_socket_action(s3fanout_mgr->curl_multi_,
457 CURL_SOCKET_TIMEOUT, 0, &still_running);
458
459
1/2
✓ Branch 1 taken 6200 times.
✗ Branch 2 not taken.
6200 LogCvmfs(kLogS3Fanout, kLogDebug, "curl_multi_socket_action: %d - %d",
460 retval, still_running);
461 }
462
463
464 // Activity on curl sockets
465 // Within this loop the curl_multi_socket_action() may cause socket(s)
466 // to be removed from watch_fds_. If a socket is removed it is replaced
467 // by the socket at the end of the array and the inuse count is decreased.
468 // Therefore loop over the array in reverse order.
469 // First 2 fds are job and terminate pipes (not curl related)
470
2/2
✓ Branch 0 taken 370560 times.
✓ Branch 1 taken 166770 times.
537330 for (int32_t i = s3fanout_mgr->watch_fds_inuse_ - 1; i >= 2; --i) {
471
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 370560 times.
370560 if (static_cast<uint32_t>(i) >= s3fanout_mgr->watch_fds_inuse_) {
472 ✗ continue;
473 }
474
2/2
✓ Branch 0 taken 170790 times.
✓ Branch 1 taken 199770 times.
370560 if (s3fanout_mgr->watch_fds_[i].revents) {
475 170790 int ev_bitmask = 0;
476
2/2
✓ Branch 0 taken 18470 times.
✓ Branch 1 taken 152320 times.
170790 if (s3fanout_mgr->watch_fds_[i].revents & (POLLIN | POLLPRI))
477 18470 ev_bitmask |= CURL_CSELECT_IN;
478
2/2
✓ Branch 0 taken 152320 times.
✓ Branch 1 taken 18470 times.
170790 if (s3fanout_mgr->watch_fds_[i].revents & (POLLOUT | POLLWRBAND))
479 152320 ev_bitmask |= CURL_CSELECT_OUT;
480 170790 if (s3fanout_mgr->watch_fds_[i].revents
481
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 170790 times.
170790 & (POLLERR | POLLHUP | POLLNVAL))
482 ✗ ev_bitmask |= CURL_CSELECT_ERR;
483 170790 s3fanout_mgr->watch_fds_[i].revents = 0;
484
485 170790 int still_running = 0;
486 170790 retval = curl_multi_socket_action(s3fanout_mgr->curl_multi_,
487
1/2
✓ Branch 1 taken 170790 times.
✗ Branch 2 not taken.
170790 s3fanout_mgr->watch_fds_[i].fd,
488 ev_bitmask,
489 &still_running);
490 }
491 }
492
493 // Check if transfers are completed
494 CURLMsg *curl_msg;
495 int msgs_in_queue;
496
3/4
✓ Branch 1 taken 179090 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12320 times.
✓ Branch 4 taken 166770 times.
179090 while ((curl_msg = curl_multi_info_read(s3fanout_mgr->curl_multi_,
497 &msgs_in_queue))) {
498
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12320 times.
12320 assert(curl_msg->msg == CURLMSG_DONE);
499
500 12320 s3fanout_mgr->statistics_->num_requests++;
501 JobInfo *info;
502 12320 CURL *easy_handle = curl_msg->easy_handle;
503 12320 const int curl_error = curl_msg->data.result;
504
1/2
✓ Branch 1 taken 12320 times.
✗ Branch 2 not taken.
12320 curl_easy_getinfo(easy_handle, CURLINFO_PRIVATE, &info);
505
506
1/2
✓ Branch 1 taken 12320 times.
✗ Branch 2 not taken.
12320 curl_multi_remove_handle(s3fanout_mgr->curl_multi_, easy_handle);
507
3/4
✓ Branch 1 taken 12320 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6120 times.
✓ Branch 4 taken 6200 times.
12320 if (s3fanout_mgr->VerifyAndFinalize(curl_error, info)) {
508
1/2
✓ Branch 1 taken 6120 times.
✗ Branch 2 not taken.
6120 curl_multi_add_handle(s3fanout_mgr->curl_multi_, easy_handle);
509 6120 int still_running = 0;
510
1/2
✓ Branch 1 taken 6120 times.
✗ Branch 2 not taken.
6120 curl_multi_socket_action(s3fanout_mgr->curl_multi_, CURL_SOCKET_TIMEOUT,
511 0, &still_running);
512 } else {
513 // Return easy handle into pool and write result back
514 6200 jobs_in_flight--;
515
1/2
✓ Branch 1 taken 6200 times.
✗ Branch 2 not taken.
6200 s3fanout_mgr->active_requests_->erase(info);
516
1/2
✓ Branch 1 taken 6200 times.
✗ Branch 2 not taken.
6200 s3fanout_mgr->ReleaseCurlHandle(info, easy_handle);
517
1/2
✓ Branch 1 taken 6200 times.
✗ Branch 2 not taken.
6200 s3fanout_mgr->available_jobs_->Decrement();
518
519 // Add to list of completed jobs
520
1/2
✓ Branch 1 taken 6200 times.
✗ Branch 2 not taken.
6200 s3fanout_mgr->PushCompletedJob(info);
521 }
522 }
523 166770 }
524
525 130 set<CURL *>::iterator i = s3fanout_mgr->pool_handles_inuse_->begin();
526 130 const set<CURL *>::const_iterator i_end = s3fanout_mgr->pool_handles_inuse_
527 130 ->end();
528
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 130 times.
130 for (; i != i_end; ++i) {
529 ✗ curl_multi_remove_handle(s3fanout_mgr->curl_multi_, *i);
530 ✗ curl_easy_cleanup(*i);
531 }
532 130 s3fanout_mgr->pool_handles_inuse_->clear();
533 130 free(s3fanout_mgr->watch_fds_);
534
535
1/2
✓ Branch 1 taken 130 times.
✗ Branch 2 not taken.
130 LogCvmfs(kLogS3Fanout, kLogDebug, "Upload I/O thread terminated");
536 130 return NULL;
537 }
538
539
540 /**
541 * Gets an idle CURL handle from the pool. Creates a new one and adds it to
542 * the pool if necessary.
543 */
544 6200 CURL *S3FanoutManager::AcquireCurlHandle() const {
545 CURL *handle;
546
547 6200 const MutexLockGuard guard(curl_handle_lock_);
548
549
2/2
✓ Branch 1 taken 550 times.
✓ Branch 2 taken 5650 times.
6200 if (pool_handles_idle_->empty()) {
550 CURLcode retval;
551
552 // Create a new handle
553
1/2
✓ Branch 1 taken 550 times.
✗ Branch 2 not taken.
550 handle = curl_easy_init();
554
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 550 times.
550 assert(handle != NULL);
555
556 // Other settings
557
1/2
✓ Branch 1 taken 550 times.
✗ Branch 2 not taken.
550 retval = curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1);
558
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 550 times.
550 assert(retval == CURLE_OK);
559
1/2
✓ Branch 1 taken 550 times.
✗ Branch 2 not taken.
550 retval = curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION,
560 CallbackCurlHeader);
561
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 550 times.
550 assert(retval == CURLE_OK);
562
1/2
✓ Branch 1 taken 550 times.
✗ Branch 2 not taken.
550 retval = curl_easy_setopt(handle, CURLOPT_READFUNCTION, CallbackCurlData);
563
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 550 times.
550 assert(retval == CURLE_OK);
564
1/2
✓ Branch 1 taken 550 times.
✗ Branch 2 not taken.
550 retval = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, CallbackCurlBody);
565
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 550 times.
550 assert(retval == CURLE_OK);
566 // WRITEDATA is set per-request in InitializeRequest
567 } else {
568 5650 handle = *(pool_handles_idle_->begin());
569
1/2
✓ Branch 2 taken 5650 times.
✗ Branch 3 not taken.
5650 pool_handles_idle_->erase(pool_handles_idle_->begin());
570 }
571
572
1/2
✓ Branch 1 taken 6200 times.
✗ Branch 2 not taken.
6200 pool_handles_inuse_->insert(handle);
573
574 6200 return handle;
575 6200 }
576
577
578 6200 void S3FanoutManager::ReleaseCurlHandle(JobInfo *info, CURL *handle) const {
579
1/2
✓ Branch 0 taken 6200 times.
✗ Branch 1 not taken.
6200 if (info->http_headers) {
580
1/2
✓ Branch 1 taken 6200 times.
✗ Branch 2 not taken.
6200 curl_slist_free_all(info->http_headers);
581 6200 info->http_headers = NULL;
582 }
583
584 6200 const MutexLockGuard guard(curl_handle_lock_);
585
586
1/2
✓ Branch 1 taken 6200 times.
✗ Branch 2 not taken.
6200 const set<CURL *>::iterator elem = pool_handles_inuse_->find(handle);
587
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 6200 times.
6200 assert(elem != pool_handles_inuse_->end());
588
589
2/2
✓ Branch 1 taken 290 times.
✓ Branch 2 taken 5910 times.
6200 if (pool_handles_idle_->size() > config_.pool_max_handles) {
590
1/2
✓ Branch 1 taken 290 times.
✗ Branch 2 not taken.
290 const CURLcode retval = curl_easy_setopt(handle, CURLOPT_SHARE, NULL);
591
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 290 times.
290 assert(retval == CURLE_OK);
592
1/2
✓ Branch 1 taken 290 times.
✗ Branch 2 not taken.
290 curl_easy_cleanup(handle);
593 const std::map<CURL *, S3FanOutDnsEntry *>::size_type
594
1/2
✓ Branch 1 taken 290 times.
✗ Branch 2 not taken.
290 retitems = curl_sharehandles_->erase(handle);
595
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 290 times.
290 assert(retitems == 1);
596 } else {
597
1/2
✓ Branch 1 taken 5910 times.
✗ Branch 2 not taken.
5910 pool_handles_idle_->insert(handle);
598 }
599
600
1/2
✓ Branch 1 taken 6200 times.
✗ Branch 2 not taken.
6200 pool_handles_inuse_->erase(elem);
601 6200 }
602
603 130 void S3FanoutManager::InitPipeWatchFds() {
604
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
130 assert(watch_fds_inuse_ == 0);
605
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
130 assert(watch_fds_size_ >= 2);
606 130 watch_fds_[0].fd = pipe_terminate_[0];
607 130 watch_fds_[0].events = POLLIN | POLLPRI;
608 130 watch_fds_[0].revents = 0;
609 130 ++watch_fds_inuse_;
610 130 watch_fds_[1].fd = pipe_jobs_[0];
611 130 watch_fds_[1].events = POLLIN | POLLPRI;
612 130 watch_fds_[1].revents = 0;
613 130 ++watch_fds_inuse_;
614 130 }
615
616 /**
617 * The Amazon AWS 2 authorization header according to
618 * http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html#ConstructingTheAuthenticationHeader
619 */
620 12280 bool S3FanoutManager::MkV2Authz(const JobInfo &info,
621 vector<string> *headers) const {
622 12280 string payload_hash;
623
1/2
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
12280 const bool retval = MkPayloadHash(info, &payload_hash);
624
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12280 times.
12280 if (!retval)
625 ✗ return false;
626
1/2
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
12280 const string content_type = GetContentType(info);
627
1/2
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
12280 const string request = GetRequestString(info);
628
629
1/2
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
12280 const string timestamp = RfcTimestamp();
630
5/10
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12280 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 12280 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 12280 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 12280 times.
✗ Branch 14 not taken.
24560 string to_sign = request + "\n" + payload_hash + "\n" + content_type + "\n"
631
2/4
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12280 times.
✗ Branch 5 not taken.
24560 + timestamp + "\n";
632
2/4
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12280 times.
✗ Branch 4 not taken.
12280 if (config_.x_amz_acl != "")
633
1/2
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
24560 to_sign += "x-amz-acl:" + config_.x_amz_acl
634
2/4
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12280 times.
✗ Branch 5 not taken.
12280 + "\n"; // CanonicalizedAmzHeaders
635 12280 to_sign += MkV2CanonicalResource(config_.bucket, info.object_key,
636 12280 config_.dns_buckets,
637
2/4
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12280 times.
✗ Branch 5 not taken.
12280 info.request == JobInfo::kReqDeleteMulti);
638
1/2
✓ Branch 3 taken 12280 times.
✗ Branch 4 not taken.
12280 LogCvmfs(kLogS3Fanout, kLogDebug, "%s string to sign: %s", request.c_str(),
639 to_sign.c_str());
640
641
1/2
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
12280 shash::Any hmac;
642 12280 hmac.algorithm = shash::kSha1;
643
1/2
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
12280 shash::Hmac(config_.secret_key,
644 12280 reinterpret_cast<const unsigned char *>(to_sign.data()),
645 12280 to_sign.length(), &hmac);
646
647
3/6
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12280 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 12280 times.
✗ Branch 8 not taken.
36840 headers->push_back("Authorization: AWS " + config_.access_key + ":"
648
3/6
✓ Branch 2 taken 12280 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 12280 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 12280 times.
✗ Branch 9 not taken.
61400 + Base64(string(reinterpret_cast<char *>(hmac.digest),
649 12280 hmac.GetDigestSize())));
650
2/4
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12280 times.
✗ Branch 5 not taken.
12280 headers->push_back("Date: " + timestamp);
651
2/4
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12280 times.
✗ Branch 5 not taken.
12280 headers->push_back("X-Amz-Acl: " + config_.x_amz_acl);
652
2/2
✓ Branch 1 taken 6150 times.
✓ Branch 2 taken 6130 times.
12280 if (!payload_hash.empty())
653
2/4
✓ Branch 1 taken 6150 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6150 times.
✗ Branch 5 not taken.
6150 headers->push_back("Content-MD5: " + payload_hash);
654
2/2
✓ Branch 1 taken 6150 times.
✓ Branch 2 taken 6130 times.
12280 if (!content_type.empty())
655
2/4
✓ Branch 1 taken 6150 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6150 times.
✗ Branch 5 not taken.
6150 headers->push_back("Content-Type: " + content_type);
656 12280 return true;
657 12280 }
658
659
660 ✗ string S3FanoutManager::GetUriEncode(const string &val,
661 bool encode_slash) const {
662 ✗ string result;
663 ✗ const unsigned len = val.length();
664 ✗ result.reserve(len);
665 ✗ for (unsigned i = 0; i < len; ++i) {
666 ✗ const char c = val[i];
667 ✗ if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')
668 ✗ || (c >= '0' && c <= '9') || c == '_' || c == '-' || c == '~'
669 ✗ || c == '.') {
670 ✗ result.push_back(c);
671 ✗ } else if (c == '/') {
672 ✗ if (encode_slash) {
673 ✗ result += "%2F";
674 } else {
675 ✗ result.push_back(c);
676 }
677 } else {
678 ✗ result.push_back('%');
679 ✗ result.push_back((c / 16) + ((c / 16 <= 9) ? '0' : 'A' - 10));
680 ✗ result.push_back((c % 16) + ((c % 16 <= 9) ? '0' : 'A' - 10));
681 }
682 }
683 ✗ return result;
684 }
685
686
687 ✗ string S3FanoutManager::GetAwsV4SigningKey(const string &date) const {
688 ✗ if (last_signing_key_.first == date)
689 ✗ return last_signing_key_.second;
690
691 ✗ const string date_key = shash::Hmac256("AWS4" + config_.secret_key, date,
692 ✗ true);
693 ✗ const string date_region_key = shash::Hmac256(date_key, config_.region, true);
694 const string date_region_service_key = shash::Hmac256(date_region_key, "s3",
695 ✗ true);
696 string signing_key = shash::Hmac256(date_region_service_key, "aws4_request",
697 ✗ true);
698 ✗ last_signing_key_.first = date;
699 ✗ last_signing_key_.second = signing_key;
700 ✗ return signing_key;
701 }
702
703
704 /**
705 * The Amazon AWS4 authorization header according to
706 * http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-auth-using-authorization-header.html
707 */
708 ✗ bool S3FanoutManager::MkV4Authz(const JobInfo &info,
709 vector<string> *headers) const {
710 ✗ string payload_hash;
711 ✗ const bool retval = MkPayloadHash(info, &payload_hash);
712 ✗ if (!retval)
713 ✗ return false;
714 ✗ const string content_type = GetContentType(info);
715 ✗ const string timestamp = IsoTimestamp();
716 ✗ const string date = timestamp.substr(0, 8);
717 ✗ vector<string> tokens = SplitString(complete_hostname_, ':');
718 ✗ assert(tokens.size() <= 2);
719 ✗ string canonical_hostname = tokens[0];
720
721 // if we could split the hostname in two and if the port is *NOT* a default
722 // one
723 ✗ if (tokens.size() == 2
724 ✗ && !((String2Uint64(tokens[1]) == kDefaultHTTPPort)
725 ✗ || (String2Uint64(tokens[1]) == kDefaultHTTPSPort)))
726 ✗ canonical_hostname += ":" + tokens[1];
727
728 ✗ string signed_headers;
729 ✗ string canonical_headers;
730 ✗ if (!content_type.empty()) {
731 ✗ signed_headers += "content-type;";
732 ✗ headers->push_back("Content-Type: " + content_type);
733 ✗ canonical_headers += "content-type:" + content_type + "\n";
734 }
735 ✗ if (config_.x_amz_acl != "") {
736 ✗ signed_headers += "host;x-amz-acl;x-amz-content-sha256;x-amz-date";
737 } else {
738 ✗ signed_headers += "host;x-amz-content-sha256;x-amz-date";
739 }
740 ✗ canonical_headers += "host:" + canonical_hostname + "\n";
741 ✗ if (config_.x_amz_acl != "") {
742 ✗ canonical_headers += "x-amz-acl:" + config_.x_amz_acl + "\n";
743 }
744 ✗ canonical_headers += "x-amz-content-sha256:" + payload_hash + "\n"
745 ✗ + "x-amz-date:" + timestamp + "\n";
746
747 ✗ const string scope = date + "/" + config_.region + "/s3/aws4_request";
748 ✗ string uri;
749 ✗ if (config_.dns_buckets) {
750 ✗ uri = string("/") + info.object_key;
751 ✗ } else if (info.object_key.empty()) {
752 ✗ uri = string("/") + config_.bucket;
753 } else {
754 ✗ uri = string("/") + config_.bucket + "/" + info.object_key;
755 }
756
757 // V4 canonical query string: empty for most requests, "delete=" for
758 // multi-object delete (the S3 ?delete parameter has no value)
759 ✗ const string canonical_query = (info.request == JobInfo::kReqDeleteMulti)
760 ? "delete="
761 ✗ : "";
762
763 ✗ const string canonical_request = GetRequestString(info) + "\n"
764 ✗ + GetUriEncode(uri, false) + "\n"
765 ✗ + canonical_query + "\n" + canonical_headers
766 ✗ + "\n" + signed_headers + "\n"
767 ✗ + payload_hash;
768
769 ✗ const string hash_request = shash::Sha256String(canonical_request.c_str());
770
771 ✗ const string string_to_sign = "AWS4-HMAC-SHA256\n" + timestamp + "\n" + scope
772 ✗ + "\n" + hash_request;
773
774 ✗ const string signing_key = GetAwsV4SigningKey(date);
775 ✗ const string signature = shash::Hmac256(signing_key, string_to_sign);
776
777 ✗ headers->push_back("X-Amz-Acl: " + config_.x_amz_acl);
778 ✗ headers->push_back("X-Amz-Content-Sha256: " + payload_hash);
779 ✗ headers->push_back("X-Amz-Date: " + timestamp);
780 ✗ headers->push_back("Authorization: AWS4-HMAC-SHA256 "
781 "Credential="
782 ✗ + config_.access_key + "/" + scope
783 ✗ + ","
784 "SignedHeaders="
785 ✗ + signed_headers
786 ✗ + ","
787 "Signature="
788 ✗ + signature);
789
790 // S3 requires Content-MD5 for multi-object delete
791 ✗ if (info.request == JobInfo::kReqDeleteMulti) {
792 ✗ shash::Any md5_hash(shash::kMd5);
793 unsigned char *data;
794 ✗ const unsigned int nbytes = info.origin->Data(
795 ✗ reinterpret_cast<void **>(&data), info.origin->GetSize(), 0);
796 ✗ assert(nbytes == info.origin->GetSize());
797 ✗ shash::HashMem(data, nbytes, &md5_hash);
798 ✗ headers->push_back(
799 "Content-MD5: "
800 ✗ + Base64(string(reinterpret_cast<char *>(md5_hash.digest),
801 ✗ md5_hash.GetDigestSize())));
802 }
803 ✗ return true;
804 }
805
806 /**
807 * The Azure Blob authorization header according to
808 * https://docs.microsoft.com/en-us/rest/api/storageservices/authorize-with-shared-key
809 */
810 ✗ bool S3FanoutManager::MkAzureAuthz(const JobInfo &info,
811 vector<string> *headers) const {
812 ✗ const string timestamp = RfcTimestamp();
813 const string canonical_headers = "x-ms-blob-type:BlockBlob\nx-ms-date:"
814 ✗ + timestamp + "\nx-ms-version:2011-08-18";
815 ✗ const string canonical_resource = "/" + config_.access_key + "/"
816 ✗ + config_.bucket + "/" + info.object_key;
817
818 ✗ string string_to_sign;
819 ✗ if ((info.request == JobInfo::kReqHeadOnly)
820 ✗ || (info.request == JobInfo::kReqHeadPut)
821 ✗ || (info.request == JobInfo::kReqDelete)) {
822 ✗ string_to_sign = GetRequestString(info) + string("\n\n\n")
823 ✗ + "\n\n\n\n\n\n\n\n\n" + canonical_headers + "\n"
824 ✗ + canonical_resource;
825 } else {
826 ✗ string_to_sign = GetRequestString(info) + string("\n\n\n")
827 ✗ + string(StringifyInt(info.origin->GetSize()))
828 ✗ + "\n\n\n\n\n\n\n\n\n" + canonical_headers + "\n"
829 ✗ + canonical_resource;
830 }
831
832 ✗ string signing_key;
833 ✗ const int retval = Debase64(config_.secret_key, &signing_key);
834 ✗ if (!retval)
835 ✗ return false;
836
837 ✗ const string signature = shash::Hmac256(signing_key, string_to_sign, true);
838
839 ✗ headers->push_back("x-ms-date: " + timestamp);
840 ✗ headers->push_back("x-ms-version: 2011-08-18");
841 ✗ headers->push_back("Authorization: SharedKey " + config_.access_key + ":"
842 ✗ + Base64(signature));
843 ✗ headers->push_back("x-ms-blob-type: BlockBlob");
844 ✗ return true;
845 }
846
847 12280 void S3FanoutManager::InitializeDnsSettingsCurl(CURL *handle,
848 CURLSH *sharehandle,
849 curl_slist *clist) const {
850 12280 CURLcode retval = curl_easy_setopt(handle, CURLOPT_SHARE, sharehandle);
851
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12280 times.
12280 assert(retval == CURLE_OK);
852 12280 retval = curl_easy_setopt(handle, CURLOPT_RESOLVE, clist);
853
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12280 times.
12280 assert(retval == CURLE_OK);
854 12280 }
855
856
857 12280 int S3FanoutManager::InitializeDnsSettings(CURL *handle,
858 std::string host_with_port) const {
859 // Use existing handle
860 const std::map<CURL *, S3FanOutDnsEntry *>::const_iterator
861
1/2
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
12280 it = curl_sharehandles_->find(handle);
862
2/2
✓ Branch 3 taken 11730 times.
✓ Branch 4 taken 550 times.
12280 if (it != curl_sharehandles_->end()) {
863
1/2
✓ Branch 1 taken 11730 times.
✗ Branch 2 not taken.
11730 InitializeDnsSettingsCurl(handle, it->second->sharehandle,
864 11730 it->second->clist);
865 11730 return 0;
866 }
867
868 // Add protocol information for extraction of fields for DNS
869
2/4
✓ Branch 1 taken 550 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 550 times.
✗ Branch 4 not taken.
550 if (!IsHttpUrl(host_with_port))
870
2/4
✓ Branch 1 taken 550 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 550 times.
✗ Branch 5 not taken.
550 host_with_port = config_.protocol + "://" + host_with_port;
871
1/2
✓ Branch 1 taken 550 times.
✗ Branch 2 not taken.
550 const std::string remote_host = dns::ExtractHost(host_with_port);
872
1/2
✓ Branch 1 taken 550 times.
✗ Branch 2 not taken.
550 const std::string remote_port = dns::ExtractPort(host_with_port);
873
874 // If we have the name already resolved, use the least used IP
875 550 S3FanOutDnsEntry *useme = NULL;
876 550 unsigned int usemin = UINT_MAX;
877 550 std::set<S3FanOutDnsEntry *>::iterator its3 = sharehandles_->begin();
878
2/2
✓ Branch 3 taken 440 times.
✓ Branch 4 taken 550 times.
990 for (; its3 != sharehandles_->end(); ++its3) {
879
1/2
✓ Branch 2 taken 440 times.
✗ Branch 3 not taken.
440 if ((*its3)->dns_name == remote_host) {
880
1/2
✓ Branch 1 taken 440 times.
✗ Branch 2 not taken.
440 if (usemin >= (*its3)->counter) {
881 440 usemin = (*its3)->counter;
882 440 useme = (*its3);
883 }
884 }
885 }
886
2/2
✓ Branch 0 taken 440 times.
✓ Branch 1 taken 110 times.
550 if (useme != NULL) {
887
1/2
✓ Branch 1 taken 440 times.
✗ Branch 2 not taken.
440 curl_sharehandles_->insert(
888 440 std::pair<CURL *, S3FanOutDnsEntry *>(handle, useme));
889 440 useme->counter++;
890
1/2
✓ Branch 1 taken 440 times.
✗ Branch 2 not taken.
440 InitializeDnsSettingsCurl(handle, useme->sharehandle, useme->clist);
891 440 return 0;
892 }
893
894 // We need to resolve the hostname
895 // TODO(ssheikki): support ipv6 also... if (opt_ipv4_only_)
896
1/2
✓ Branch 1 taken 110 times.
✗ Branch 2 not taken.
110 const dns::Host host = resolver_->Resolve(remote_host);
897
1/2
✓ Branch 2 taken 110 times.
✗ Branch 3 not taken.
110 set<string> const ipv4_addresses = host.ipv4_addresses();
898 110 std::set<string>::iterator its = ipv4_addresses.begin();
899 110 S3FanOutDnsEntry *dnse = NULL;
900
2/2
✓ Branch 3 taken 110 times.
✓ Branch 4 taken 110 times.
220 for (; its != ipv4_addresses.end(); ++its) {
901
2/4
✓ Branch 1 taken 110 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 110 times.
✗ Branch 5 not taken.
110 dnse = new S3FanOutDnsEntry();
902 110 dnse->counter = 0;
903
1/2
✓ Branch 1 taken 110 times.
✗ Branch 2 not taken.
110 dnse->dns_name = remote_host;
904
4/12
✗ Branch 1 not taken.
✓ Branch 2 taken 110 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 8 taken 110 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 110 times.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 110 times.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
110 dnse->port = remote_port.size() == 0 ? "80" : remote_port;
905
1/2
✓ Branch 2 taken 110 times.
✗ Branch 3 not taken.
110 dnse->ip = *its;
906 110 dnse->clist = NULL;
907
1/2
✓ Branch 2 taken 110 times.
✗ Branch 3 not taken.
110 dnse->clist = curl_slist_append(
908 dnse->clist,
909
4/8
✓ Branch 1 taken 110 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 110 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 110 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 110 times.
✗ Branch 11 not taken.
220 (dnse->dns_name + ":" + dnse->port + ":" + dnse->ip).c_str());
910
1/2
✓ Branch 1 taken 110 times.
✗ Branch 2 not taken.
110 dnse->sharehandle = curl_share_init();
911
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
110 assert(dnse->sharehandle != NULL);
912
1/2
✓ Branch 1 taken 110 times.
✗ Branch 2 not taken.
110 const CURLSHcode share_retval = curl_share_setopt(
913 dnse->sharehandle, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
914
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
110 assert(share_retval == CURLSHE_OK);
915
1/2
✓ Branch 1 taken 110 times.
✗ Branch 2 not taken.
110 sharehandles_->insert(dnse);
916 }
917
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
110 if (dnse == NULL) {
918 ✗ LogCvmfs(kLogS3Fanout, kLogStderr | kLogSyslogErr,
919 "Error: DNS resolve failed for address '%s'.",
920 remote_host.c_str());
921 ✗ assert(dnse != NULL);
922 ✗ return -1;
923 }
924
1/2
✓ Branch 1 taken 110 times.
✗ Branch 2 not taken.
110 curl_sharehandles_->insert(
925 110 std::pair<CURL *, S3FanOutDnsEntry *>(handle, dnse));
926 110 dnse->counter++;
927
1/2
✓ Branch 1 taken 110 times.
✗ Branch 2 not taken.
110 InitializeDnsSettingsCurl(handle, dnse->sharehandle, dnse->clist);
928
929 110 return 0;
930 550 }
931
932
933 12280 bool S3FanoutManager::MkPayloadHash(const JobInfo &info,
934 string *hex_hash) const {
935
2/2
✓ Branch 0 taken 12230 times.
✓ Branch 1 taken 50 times.
12280 if (info.request == JobInfo::kReqHeadOnly
936
2/2
✓ Branch 0 taken 6150 times.
✓ Branch 1 taken 6080 times.
12230 || info.request == JobInfo::kReqHeadPut
937
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6150 times.
6150 || info.request == JobInfo::kReqDelete) {
938
1/4
✓ Branch 0 taken 6130 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6130 switch (config_.authz_method) {
939 6130 case kAuthzAwsV2:
940 6130 hex_hash->clear();
941 6130 break;
942 ✗ case kAuthzAwsV4:
943 // Sha256 over empty string
944 *hex_hash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b78"
945 ✗ "52b855";
946 ✗ break;
947 ✗ case kAuthzAzure:
948 // no payload hash required for Azure signature
949 ✗ hex_hash->clear();
950 ✗ break;
951 ✗ default:
952 ✗ PANIC(NULL);
953 }
954 6130 return true;
955 }
956
957 // kReqDeleteMulti is a POST with a body (XML payload), same hashing as PUT
958 // falls through intentionally.
959
960 // PUT or POST with payload
961
1/2
✓ Branch 1 taken 6150 times.
✗ Branch 2 not taken.
6150 shash::Any payload_hash(shash::kMd5);
962
963 unsigned char *data;
964 6150 const unsigned int nbytes = info.origin->Data(
965
2/4
✓ Branch 2 taken 6150 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 6150 times.
✗ Branch 6 not taken.
6150 reinterpret_cast<void **>(&data), info.origin->GetSize(), 0);
966
2/4
✓ Branch 2 taken 6150 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 6150 times.
6150 assert(nbytes == info.origin->GetSize());
967
968
1/4
✓ Branch 0 taken 6150 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6150 switch (config_.authz_method) {
969 6150 case kAuthzAwsV2:
970
1/2
✓ Branch 1 taken 6150 times.
✗ Branch 2 not taken.
6150 shash::HashMem(data, nbytes, &payload_hash);
971
2/4
✓ Branch 2 taken 6150 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 6150 times.
✗ Branch 6 not taken.
18450 *hex_hash = Base64(string(reinterpret_cast<char *>(payload_hash.digest),
972 12300 payload_hash.GetDigestSize()));
973 6150 return true;
974 ✗ case kAuthzAwsV4:
975 ✗ *hex_hash = shash::Sha256Mem(data, nbytes);
976 ✗ return true;
977 ✗ case kAuthzAzure:
978 // no payload hash required for Azure signature
979 ✗ hex_hash->clear();
980 ✗ return true;
981 ✗ default:
982 ✗ PANIC(NULL);
983 }
984 }
985
986 12280 string S3FanoutManager::GetRequestString(const JobInfo &info) const {
987
3/5
✓ Branch 0 taken 6130 times.
✓ Branch 1 taken 6080 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 70 times.
✗ Branch 4 not taken.
12280 switch (info.request) {
988 6130 case JobInfo::kReqHeadOnly:
989 case JobInfo::kReqHeadPut:
990
1/2
✓ Branch 2 taken 6130 times.
✗ Branch 3 not taken.
6130 return "HEAD";
991 6080 case JobInfo::kReqPutCas:
992 case JobInfo::kReqPutDotCvmfs:
993 case JobInfo::kReqPutHtml:
994 case JobInfo::kReqPutBucket:
995
1/2
✓ Branch 2 taken 6080 times.
✗ Branch 3 not taken.
6080 return "PUT";
996 ✗ case JobInfo::kReqDelete:
997 ✗ return "DELETE";
998 70 case JobInfo::kReqDeleteMulti:
999
1/2
✓ Branch 2 taken 70 times.
✗ Branch 3 not taken.
70 return "POST";
1000 ✗ default:
1001 ✗ PANIC(NULL);
1002 }
1003 }
1004
1005
1006 12280 string S3FanoutManager::GetContentType(const JobInfo &info) const {
1007
3/6
✓ Branch 0 taken 6130 times.
✓ Branch 1 taken 6080 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 70 times.
✗ Branch 5 not taken.
12280 switch (info.request) {
1008 6130 case JobInfo::kReqHeadOnly:
1009 case JobInfo::kReqHeadPut:
1010 case JobInfo::kReqDelete:
1011
1/2
✓ Branch 2 taken 6130 times.
✗ Branch 3 not taken.
6130 return "";
1012 6080 case JobInfo::kReqPutCas:
1013
1/2
✓ Branch 2 taken 6080 times.
✗ Branch 3 not taken.
6080 return "application/octet-stream";
1014 ✗ case JobInfo::kReqPutDotCvmfs:
1015 ✗ return "application/x-cvmfs";
1016 ✗ case JobInfo::kReqPutHtml:
1017 ✗ return "text/html";
1018 70 case JobInfo::kReqPutBucket:
1019 case JobInfo::kReqDeleteMulti:
1020
1/2
✓ Branch 2 taken 70 times.
✗ Branch 3 not taken.
70 return "text/xml";
1021 ✗ default:
1022 ✗ PANIC(NULL);
1023 }
1024 }
1025
1026
1027 /**
1028 * Request parameters set the URL and other options such as timeout and
1029 * proxy.
1030 */
1031 12280 Failures S3FanoutManager::InitializeRequest(JobInfo *info, CURL *handle) const {
1032 // Initialize internal download state
1033 12280 info->curl_handle = handle;
1034 12280 info->error_code = kFailOk;
1035 12280 info->http_error = 0;
1036 12280 info->num_retries = 0;
1037 12280 info->backoff_ms = 0;
1038 12280 info->throttle_ms = 0;
1039 12280 info->throttle_timestamp = 0;
1040 12280 info->http_headers = NULL;
1041 // info->payload_size is needed in S3Uploader::MainCollectResults,
1042 // where info->origin is already destroyed.
1043
1/2
✓ Branch 2 taken 12280 times.
✗ Branch 3 not taken.
12280 info->payload_size = info->origin->GetSize();
1044
1045
2/4
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12280 times.
✗ Branch 5 not taken.
12280 InitializeDnsSettings(handle, complete_hostname_);
1046
1047 CURLcode retval;
1048
2/2
✓ Branch 0 taken 12230 times.
✓ Branch 1 taken 50 times.
12280 if (info->request == JobInfo::kReqHeadOnly
1049
2/2
✓ Branch 0 taken 6150 times.
✓ Branch 1 taken 6080 times.
12230 || info->request == JobInfo::kReqHeadPut
1050
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6150 times.
6150 || info->request == JobInfo::kReqDelete) {
1051
1/2
✓ Branch 1 taken 6130 times.
✗ Branch 2 not taken.
6130 retval = curl_easy_setopt(handle, CURLOPT_UPLOAD, 0);
1052
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6130 times.
6130 assert(retval == CURLE_OK);
1053
1/2
✓ Branch 1 taken 6130 times.
✗ Branch 2 not taken.
6130 retval = curl_easy_setopt(handle, CURLOPT_NOBODY, 1);
1054
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6130 times.
6130 assert(retval == CURLE_OK);
1055
1056
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6130 times.
6130 if (info->request == JobInfo::kReqDelete) {
1057 ✗ retval = curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST,
1058 GetRequestString(*info).c_str());
1059 ✗ assert(retval == CURLE_OK);
1060 } else {
1061
1/2
✓ Branch 1 taken 6130 times.
✗ Branch 2 not taken.
6130 retval = curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, NULL);
1062
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6130 times.
6130 assert(retval == CURLE_OK);
1063 }
1064
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 6080 times.
6150 } else if (info->request == JobInfo::kReqDeleteMulti) {
1065 // POST request with XML body read from origin buffer
1066
1/2
✓ Branch 1 taken 70 times.
✗ Branch 2 not taken.
70 retval = curl_easy_setopt(handle, CURLOPT_UPLOAD, 1);
1067
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 assert(retval == CURLE_OK);
1068
1/2
✓ Branch 1 taken 70 times.
✗ Branch 2 not taken.
70 retval = curl_easy_setopt(handle, CURLOPT_NOBODY, 0);
1069
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 assert(retval == CURLE_OK);
1070
1/2
✓ Branch 1 taken 70 times.
✗ Branch 2 not taken.
70 retval = curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "POST");
1071
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 assert(retval == CURLE_OK);
1072
2/4
✓ Branch 2 taken 70 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 70 times.
✗ Branch 6 not taken.
70 retval = curl_easy_setopt(handle, CURLOPT_INFILESIZE_LARGE,
1073 static_cast<curl_off_t>(info->origin->GetSize()));
1074
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 assert(retval == CURLE_OK);
1075 70 info->response_body.clear();
1076 } else {
1077
1/2
✓ Branch 1 taken 6080 times.
✗ Branch 2 not taken.
6080 retval = curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, NULL);
1078
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6080 times.
6080 assert(retval == CURLE_OK);
1079
1/2
✓ Branch 1 taken 6080 times.
✗ Branch 2 not taken.
6080 retval = curl_easy_setopt(handle, CURLOPT_UPLOAD, 1);
1080
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6080 times.
6080 assert(retval == CURLE_OK);
1081
1/2
✓ Branch 1 taken 6080 times.
✗ Branch 2 not taken.
6080 retval = curl_easy_setopt(handle, CURLOPT_NOBODY, 0);
1082
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6080 times.
6080 assert(retval == CURLE_OK);
1083
2/4
✓ Branch 2 taken 6080 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 6080 times.
✗ Branch 6 not taken.
6080 retval = curl_easy_setopt(handle, CURLOPT_INFILESIZE_LARGE,
1084 static_cast<curl_off_t>(info->origin->GetSize()));
1085
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6080 times.
6080 assert(retval == CURLE_OK);
1086
1087
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6080 times.
6080 if (info->request == JobInfo::kReqPutDotCvmfs) {
1088 ✗ info->http_headers = curl_slist_append(
1089 info->http_headers, dot_cvmfs_cache_control_header.c_str());
1090
1/2
✓ Branch 0 taken 6080 times.
✗ Branch 1 not taken.
6080 } else if (info->request == JobInfo::kReqPutCas) {
1091
1/2
✓ Branch 1 taken 6080 times.
✗ Branch 2 not taken.
6080 info->http_headers = curl_slist_append(info->http_headers,
1092 kCacheControlCas);
1093 }
1094 }
1095
1096 bool retval_b;
1097
1098 // Authorization
1099 12280 vector<string> authz_headers;
1100
1/4
✓ Branch 0 taken 12280 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
12280 switch (config_.authz_method) {
1101 12280 case kAuthzAwsV2:
1102
1/2
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
12280 retval_b = MkV2Authz(*info, &authz_headers);
1103 12280 break;
1104 ✗ case kAuthzAwsV4:
1105 ✗ retval_b = MkV4Authz(*info, &authz_headers);
1106 ✗ break;
1107 ✗ case kAuthzAzure:
1108 ✗ retval_b = MkAzureAuthz(*info, &authz_headers);
1109 ✗ break;
1110 ✗ default:
1111 ✗ PANIC(NULL);
1112 }
1113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12280 times.
12280 if (!retval_b)
1114 ✗ return kFailLocalIO;
1115
2/2
✓ Branch 1 taken 49140 times.
✓ Branch 2 taken 12280 times.
61420 for (unsigned i = 0; i < authz_headers.size(); ++i) {
1116
1/2
✓ Branch 2 taken 49140 times.
✗ Branch 3 not taken.
49140 info->http_headers = curl_slist_append(info->http_headers,
1117 49140 authz_headers[i].c_str());
1118 }
1119
1120 // Common headers
1121
1/2
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
12280 info->http_headers = curl_slist_append(info->http_headers,
1122 "Connection: Keep-Alive");
1123
1/2
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
12280 info->http_headers = curl_slist_append(info->http_headers, "Pragma:");
1124 // No 100-continue
1125
1/2
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
12280 info->http_headers = curl_slist_append(info->http_headers, "Expect:");
1126 // Strip unnecessary header
1127
1/2
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
12280 info->http_headers = curl_slist_append(info->http_headers, "Accept:");
1128
1/2
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
12280 info->http_headers = curl_slist_append(info->http_headers,
1129 12280 user_agent_->c_str());
1130
1131 // Set curl parameters
1132
1/2
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
12280 retval = curl_easy_setopt(handle, CURLOPT_PRIVATE, static_cast<void *>(info));
1133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12280 times.
12280 assert(retval == CURLE_OK);
1134
1/2
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
12280 retval = curl_easy_setopt(handle, CURLOPT_HEADERDATA,
1135 static_cast<void *>(info));
1136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12280 times.
12280 assert(retval == CURLE_OK);
1137
1/2
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
12280 retval = curl_easy_setopt(handle, CURLOPT_READDATA,
1138 static_cast<void *>(info));
1139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12280 times.
12280 assert(retval == CURLE_OK);
1140
1/2
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
12280 retval = curl_easy_setopt(handle, CURLOPT_WRITEDATA,
1141 static_cast<void *>(info));
1142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12280 times.
12280 assert(retval == CURLE_OK);
1143
1/2
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
12280 retval = curl_easy_setopt(handle, CURLOPT_HTTPHEADER, info->http_headers);
1144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12280 times.
12280 assert(retval == CURLE_OK);
1145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12280 times.
12280 if (opt_ipv4_only_) {
1146 ✗ retval = curl_easy_setopt(handle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
1147 ✗ assert(retval == CURLE_OK);
1148 }
1149 // Follow HTTP redirects
1150
1/2
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
12280 retval = curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1L);
1151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12280 times.
12280 assert(retval == CURLE_OK);
1152
1153
1/2
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
12280 retval = curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, info->errorbuffer);
1154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12280 times.
12280 assert(retval == CURLE_OK);
1155
1156
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12280 times.
12280 if (config_.protocol == "https") {
1157 ✗ retval = curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 1L);
1158 ✗ assert(retval == CURLE_OK);
1159 ✗ retval = curl_easy_setopt(handle, CURLOPT_PROXY_SSL_VERIFYPEER, 1L);
1160 ✗ assert(retval == CURLE_OK);
1161 ✗ const bool add_cert = ssl_certificate_store_.ApplySslCertificatePath(
1162 handle);
1163 ✗ assert(add_cert);
1164 }
1165
1166 12280 return kFailOk;
1167 12280 }
1168
1169
1170 /**
1171 * Sets the URL specific options such as host to use and timeout.
1172 */
1173 12280 void S3FanoutManager::SetUrlOptions(JobInfo *info) const {
1174 12280 CURL *curl_handle = info->curl_handle;
1175 CURLcode retval;
1176
1177
1/2
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
12280 retval = curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT,
1178 config_.opt_timeout_sec);
1179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12280 times.
12280 assert(retval == CURLE_OK);
1180
1/2
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
12280 retval = curl_easy_setopt(curl_handle, CURLOPT_LOW_SPEED_LIMIT,
1181 kLowSpeedLimit);
1182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12280 times.
12280 assert(retval == CURLE_OK);
1183
1/2
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
12280 retval = curl_easy_setopt(curl_handle, CURLOPT_LOW_SPEED_TIME,
1184 config_.opt_timeout_sec);
1185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12280 times.
12280 assert(retval == CURLE_OK);
1186
1187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12280 times.
12280 if (is_curl_debug_) {
1188 ✗ retval = curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1);
1189 ✗ assert(retval == CURLE_OK);
1190 }
1191
1192
1/2
✓ Branch 1 taken 12280 times.
✗ Branch 2 not taken.
12280 string url = MkUrl(info->object_key);
1193
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 12210 times.
12280 if (info->request == JobInfo::kReqDeleteMulti)
1194
1/2
✓ Branch 1 taken 70 times.
✗ Branch 2 not taken.
70 url += "?delete";
1195
1/2
✓ Branch 2 taken 12280 times.
✗ Branch 3 not taken.
12280 retval = curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());
1196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12280 times.
12280 assert(retval == CURLE_OK);
1197
1198
1/2
✓ Branch 2 taken 12280 times.
✗ Branch 3 not taken.
12280 retval = curl_easy_setopt(curl_handle, CURLOPT_PROXY, config_.proxy.c_str());
1199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12280 times.
12280 assert(retval == CURLE_OK);
1200 12280 }
1201
1202
1203 /**
1204 * Adds transfer time and uploaded bytes to the global counters.
1205 */
1206 12320 void S3FanoutManager::UpdateStatistics(CURL *handle) {
1207 curl_off_t val;
1208
1209
2/4
✓ Branch 1 taken 12320 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12320 times.
✗ Branch 4 not taken.
12320 if (curl_easy_getinfo(handle, CURLINFO_SIZE_UPLOAD_T, &val) == CURLE_OK)
1210 12320 statistics_->transferred_bytes += val;
1211 12320 }
1212
1213
1214 /**
1215 * Retry if possible and if not already done too often.
1216 */
1217 60 bool S3FanoutManager::CanRetry(const JobInfo *info) {
1218 60 return (info->error_code == kFailHostConnection
1219
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 || info->error_code == kFailHostResolve
1220
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 || info->error_code == kFailServiceUnavailable
1221
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 20 times.
60 || info->error_code == kFailRetry)
1222
2/4
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
120 && (info->num_retries < config_.opt_max_retries);
1223 }
1224
1225
1226 /**
1227 * Backoff for retry to introduce a jitter into a upload sequence.
1228 *
1229 * \return true if backoff has been performed, false otherwise
1230 */
1231 40 void S3FanoutManager::Backoff(JobInfo *info) {
1232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (info->error_code != kFailRetry)
1233 ✗ info->num_retries++;
1234 40 statistics_->num_retries++;
1235
1236
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 if (info->throttle_ms > 0) {
1237 40 LogCvmfs(kLogS3Fanout, kLogDebug, "throttling for %d ms",
1238 info->throttle_ms);
1239 40 const uint64_t now = platform_monotonic_time();
1240
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 if ((info->throttle_timestamp + (info->throttle_ms / 1000)) >= now) {
1241
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 30 times.
40 if ((now - timestamp_last_throttle_report_)
1242 > kThrottleReportIntervalSec) {
1243 10 LogCvmfs(kLogS3Fanout, kLogStdout,
1244 "Warning: S3 backend throttling %ums "
1245 "(total backoff time so far %lums)",
1246 10 info->throttle_ms, statistics_->ms_throttled);
1247 10 timestamp_last_throttle_report_ = now;
1248 }
1249 40 statistics_->ms_throttled += info->throttle_ms;
1250 40 SafeSleepMs(info->throttle_ms);
1251 }
1252 } else {
1253 ✗ if (info->backoff_ms == 0) {
1254 // Must be != 0
1255 ✗ info->backoff_ms = prng_.Next(config_.opt_backoff_init_ms + 1);
1256 } else {
1257 ✗ info->backoff_ms *= 2;
1258 }
1259 ✗ if (info->backoff_ms > config_.opt_backoff_max_ms)
1260 ✗ info->backoff_ms = config_.opt_backoff_max_ms;
1261
1262 ✗ LogCvmfs(kLogS3Fanout, kLogDebug, "backing off for %d ms",
1263 info->backoff_ms);
1264 ✗ SafeSleepMs(info->backoff_ms);
1265 }
1266 40 }
1267
1268
1269 /**
1270 * Checks the result of a curl request and implements the failure logic
1271 * and takes care of cleanup.
1272 *
1273 * @return true if request should be repeated, false otherwise
1274 */
1275 12320 bool S3FanoutManager::VerifyAndFinalize(const int curl_error, JobInfo *info) {
1276 12320 LogCvmfs(kLogS3Fanout, kLogDebug,
1277 "Verify uploaded/tested object %s "
1278 "(curl error %d, info error %d, info request %d)",
1279 12320 info->object_key.c_str(), curl_error, info->error_code,
1280 12320 info->request);
1281 12320 UpdateStatistics(info->curl_handle);
1282
1283 // Verification and error classification
1284
1/6
✓ Branch 0 taken 12320 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
12320 switch (curl_error) {
1285 12320 case CURLE_OK:
1286
2/2
✓ Branch 0 taken 12280 times.
✓ Branch 1 taken 40 times.
12320 if ((info->error_code != kFailRetry)
1287
2/2
✓ Branch 0 taken 6180 times.
✓ Branch 1 taken 6100 times.
12280 && (info->error_code != kFailNotFound)) {
1288 6180 info->error_code = kFailOk;
1289 }
1290 12320 break;
1291 ✗ case CURLE_UNSUPPORTED_PROTOCOL:
1292 case CURLE_URL_MALFORMAT:
1293 ✗ info->error_code = kFailBadRequest;
1294 ✗ break;
1295 ✗ case CURLE_COULDNT_RESOLVE_HOST:
1296 ✗ info->error_code = kFailHostResolve;
1297 ✗ break;
1298 ✗ case CURLE_COULDNT_CONNECT:
1299 case CURLE_OPERATION_TIMEDOUT:
1300 case CURLE_SEND_ERROR:
1301 case CURLE_RECV_ERROR:
1302 // Server or proxy dropped the connection mid-response (e.g. a reaped
1303 // keep-alive connection); the request body is rewound before retrying.
1304 case CURLE_PARTIAL_FILE:
1305 case CURLE_GOT_NOTHING:
1306 ✗ info->error_code = kFailHostConnection;
1307 ✗ break;
1308 ✗ case CURLE_ABORTED_BY_CALLBACK:
1309 case CURLE_WRITE_ERROR:
1310 // Error set by callback
1311 ✗ break;
1312 ✗ default: {
1313 ✗ const string what = (info->request == JobInfo::kReqDeleteMulti)
1314 ? "multi-delete of "
1315 ✗ + StringifyUint(
1316 info->multi_delete_keys.size())
1317 + " objects"
1318 ✗ : "upload of '" + info->object_key + "'";
1319 ✗ LogCvmfs(kLogS3Fanout, kLogStderr | kLogSyslogErr,
1320 "unexpected curl error (%d) during %s: %s", curl_error,
1321 what.c_str(), info->errorbuffer);
1322 ✗ info->error_code = kFailOther;
1323 ✗ break;
1324 }
1325 }
1326
1327 // Transform HEAD to PUT request
1328
2/2
✓ Branch 0 taken 6100 times.
✓ Branch 1 taken 6220 times.
12320 if ((info->error_code == kFailNotFound)
1329
2/2
✓ Branch 0 taken 6080 times.
✓ Branch 1 taken 20 times.
6100 && (info->request == JobInfo::kReqHeadPut)) {
1330 6080 LogCvmfs(kLogS3Fanout, kLogDebug, "not found: %s, uploading",
1331 info->object_key.c_str());
1332 6080 info->request = JobInfo::kReqPutCas;
1333 6080 curl_slist_free_all(info->http_headers);
1334 6080 info->http_headers = NULL;
1335 6080 const s3fanout::Failures init_failure = InitializeRequest(
1336 info, info->curl_handle);
1337
1338
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6080 times.
6080 if (init_failure != s3fanout::kFailOk) {
1339 ✗ PANIC(kLogStderr,
1340 "Failed to initialize CURL handle "
1341 "(error: %d - %s | errno: %d)",
1342 init_failure, Code2Ascii(init_failure), errno);
1343 }
1344 6080 SetUrlOptions(info);
1345 // Reset origin
1346 6080 info->origin->Rewind();
1347 6080 return true; // Again, Put
1348 }
1349
1350 // Determination if failed request should be repeated
1351 6240 bool try_again = false;
1352
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 6180 times.
6240 if (info->error_code != kFailOk) {
1353 60 try_again = CanRetry(info);
1354 }
1355
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 6200 times.
6240 if (try_again) {
1356
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 if (info->request == JobInfo::kReqPutCas
1357
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 || info->request == JobInfo::kReqPutDotCvmfs
1358
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 || info->request == JobInfo::kReqPutHtml
1359
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 || info->request == JobInfo::kReqDeleteMulti) {
1360 ✗ LogCvmfs(kLogS3Fanout, kLogDebug, "Trying again to upload %s",
1361 info->object_key.c_str());
1362 // Reset origin
1363 ✗ info->origin->Rewind();
1364 ✗ if (info->request == JobInfo::kReqDeleteMulti)
1365 ✗ info->response_body.clear();
1366 }
1367 40 Backoff(info);
1368 40 info->error_code = kFailOk;
1369 40 info->http_error = 0;
1370 40 info->throttle_ms = 0;
1371 40 info->backoff_ms = 0;
1372 40 info->throttle_timestamp = 0;
1373 40 return true; // try again
1374 }
1375
1376 // Cleanup opened resources
1377 6200 info->origin.reset();
1378
1379
3/4
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 6180 times.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
6200 if ((info->error_code != kFailOk) && (info->http_error != 0)
1380
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 && (info->http_error != 404)) {
1381 ✗ LogCvmfs(kLogS3Fanout, kLogStderr, "S3: HTTP failure %d", info->http_error);
1382 }
1383 6200 return false; // stop transfer
1384 }
1385
1386
2/4
✓ Branch 2 taken 130 times.
✗ Branch 3 not taken.
✓ Branch 8 taken 130 times.
✗ Branch 9 not taken.
130 S3FanoutManager::S3FanoutManager(const S3Config &config) : config_(config) {
1387 130 multi_threaded_.store(0);
1388
1/2
✓ Branch 1 taken 130 times.
✗ Branch 2 not taken.
130 MakePipe(pipe_terminate_);
1389
1/2
✓ Branch 1 taken 130 times.
✗ Branch 2 not taken.
130 MakePipe(pipe_jobs_);
1390
1/2
✓ Branch 1 taken 130 times.
✗ Branch 2 not taken.
130 MakePipe(pipe_completed_);
1391
1392 int retval;
1393 130 jobs_todo_lock_ = reinterpret_cast<pthread_mutex_t *>(
1394 130 smalloc(sizeof(pthread_mutex_t)));
1395 130 retval = pthread_mutex_init(jobs_todo_lock_, NULL);
1396
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
130 assert(retval == 0);
1397 130 curl_handle_lock_ = reinterpret_cast<pthread_mutex_t *>(
1398 130 smalloc(sizeof(pthread_mutex_t)));
1399 130 retval = pthread_mutex_init(curl_handle_lock_, NULL);
1400
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
130 assert(retval == 0);
1401
1402
1/2
✓ Branch 1 taken 130 times.
✗ Branch 2 not taken.
130 active_requests_ = new set<JobInfo *>;
1403
1/2
✓ Branch 1 taken 130 times.
✗ Branch 2 not taken.
130 pool_handles_idle_ = new set<CURL *>;
1404
1/2
✓ Branch 1 taken 130 times.
✗ Branch 2 not taken.
130 pool_handles_inuse_ = new set<CURL *>;
1405
1/2
✓ Branch 1 taken 130 times.
✗ Branch 2 not taken.
130 curl_sharehandles_ = new map<CURL *, S3FanOutDnsEntry *>;
1406
1/2
✓ Branch 1 taken 130 times.
✗ Branch 2 not taken.
130 sharehandles_ = new set<S3FanOutDnsEntry *>;
1407 130 watch_fds_max_ = 4 * config_.pool_max_handles;
1408 130 max_available_jobs_ = 4 * config_.pool_max_handles;
1409
2/4
✓ Branch 1 taken 130 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 130 times.
✗ Branch 5 not taken.
130 available_jobs_ = new Semaphore(max_available_jobs_);
1410
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
130 assert(NULL != available_jobs_);
1411
1412
1/2
✓ Branch 1 taken 130 times.
✗ Branch 2 not taken.
130 statistics_ = new Statistics();
1413
1/2
✓ Branch 1 taken 130 times.
✗ Branch 2 not taken.
130 user_agent_ = new string();
1414
2/4
✓ Branch 2 taken 130 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 130 times.
✗ Branch 6 not taken.
130 *user_agent_ = "User-Agent: cvmfs " + string(CVMFS_VERSION);
1415
1/2
✓ Branch 1 taken 130 times.
✗ Branch 2 not taken.
130 complete_hostname_ = MkCompleteHostname();
1416
1/2
✓ Branch 1 taken 130 times.
✗ Branch 2 not taken.
130 dot_cvmfs_cache_control_header = MkDotCvmfsCacheControlHeader();
1417
1418
1/2
✓ Branch 1 taken 130 times.
✗ Branch 2 not taken.
130 const CURLcode cretval = curl_global_init(CURL_GLOBAL_ALL);
1419
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
130 assert(cretval == CURLE_OK);
1420
1/2
✓ Branch 1 taken 130 times.
✗ Branch 2 not taken.
130 curl_multi_ = curl_multi_init();
1421
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
130 assert(curl_multi_ != NULL);
1422 CURLMcode mretval;
1423
1/2
✓ Branch 1 taken 130 times.
✗ Branch 2 not taken.
130 mretval = curl_multi_setopt(curl_multi_, CURLMOPT_SOCKETFUNCTION,
1424 CallbackCurlSocket);
1425
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
130 assert(mretval == CURLM_OK);
1426
1/2
✓ Branch 1 taken 130 times.
✗ Branch 2 not taken.
130 mretval = curl_multi_setopt(curl_multi_, CURLMOPT_SOCKETDATA,
1427 static_cast<void *>(this));
1428
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
130 assert(mretval == CURLM_OK);
1429
1/2
✓ Branch 1 taken 130 times.
✗ Branch 2 not taken.
130 mretval = curl_multi_setopt(curl_multi_, CURLMOPT_MAX_TOTAL_CONNECTIONS,
1430 config_.pool_max_handles);
1431
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
130 assert(mretval == CURLM_OK);
1432
1433 130 prng_.InitLocaltime();
1434
1435 130 thread_upload_ = 0;
1436 130 timestamp_last_throttle_report_ = 0;
1437 130 is_curl_debug_ = (getenv("_CVMFS_CURL_DEBUG") != NULL);
1438
1439 // Parsing environment variables
1440 130 if ((getenv("CVMFS_IPV4_ONLY") != NULL)
1441
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 130 times.
130 && (strlen(getenv("CVMFS_IPV4_ONLY")) > 0)) {
1442 ✗ opt_ipv4_only_ = true;
1443 } else {
1444 130 opt_ipv4_only_ = false;
1445 }
1446
1447
1/2
✓ Branch 1 taken 130 times.
✗ Branch 2 not taken.
130 resolver_ = dns::CaresResolver::Create(opt_ipv4_only_, 2, 2000);
1448
1449 130 watch_fds_ = static_cast<struct pollfd *>(smalloc(4 * sizeof(struct pollfd)));
1450 130 watch_fds_size_ = 4;
1451 130 watch_fds_inuse_ = 0;
1452
1453
1/2
✓ Branch 1 taken 130 times.
✗ Branch 2 not taken.
130 ssl_certificate_store_.UseSystemCertificatePath();
1454 130 }
1455
1456 260 S3FanoutManager::~S3FanoutManager() {
1457 130 pthread_mutex_destroy(jobs_todo_lock_);
1458 130 free(jobs_todo_lock_);
1459 130 pthread_mutex_destroy(curl_handle_lock_);
1460 130 free(curl_handle_lock_);
1461
1462
1/2
✓ Branch 1 taken 130 times.
✗ Branch 2 not taken.
260 if (multi_threaded_.load() == 1) {
1463 // Shutdown I/O thread
1464 130 char buf = 'T';
1465 130 WritePipe(pipe_terminate_[1], &buf, 1);
1466 130 pthread_join(thread_upload_, NULL);
1467 }
1468 130 ClosePipe(pipe_terminate_);
1469 130 ClosePipe(pipe_jobs_);
1470 130 ClosePipe(pipe_completed_);
1471
1472 130 set<CURL *>::iterator i = pool_handles_idle_->begin();
1473 130 const set<CURL *>::const_iterator iEnd = pool_handles_idle_->end();
1474
2/2
✓ Branch 2 taken 260 times.
✓ Branch 3 taken 130 times.
390 for (; i != iEnd; ++i) {
1475 260 curl_easy_cleanup(*i);
1476 }
1477
1478 130 set<S3FanOutDnsEntry *>::iterator is = sharehandles_->begin();
1479 130 const set<S3FanOutDnsEntry *>::const_iterator isEnd = sharehandles_->end();
1480
2/2
✓ Branch 2 taken 110 times.
✓ Branch 3 taken 130 times.
240 for (; is != isEnd; ++is) {
1481 110 curl_share_cleanup((*is)->sharehandle);
1482 110 curl_slist_free_all((*is)->clist);
1483
1/2
✓ Branch 1 taken 110 times.
✗ Branch 2 not taken.
110 delete *is;
1484 }
1485 130 pool_handles_idle_->clear();
1486 130 curl_sharehandles_->clear();
1487 130 sharehandles_->clear();
1488
1/2
✓ Branch 0 taken 130 times.
✗ Branch 1 not taken.
130 delete active_requests_;
1489
1/2
✓ Branch 0 taken 130 times.
✗ Branch 1 not taken.
130 delete pool_handles_idle_;
1490
1/2
✓ Branch 0 taken 130 times.
✗ Branch 1 not taken.
130 delete pool_handles_inuse_;
1491
1/2
✓ Branch 0 taken 130 times.
✗ Branch 1 not taken.
130 delete curl_sharehandles_;
1492
1/2
✓ Branch 0 taken 130 times.
✗ Branch 1 not taken.
130 delete sharehandles_;
1493
1/2
✓ Branch 0 taken 130 times.
✗ Branch 1 not taken.
130 delete user_agent_;
1494 130 curl_multi_cleanup(curl_multi_);
1495
1496
1/2
✓ Branch 0 taken 130 times.
✗ Branch 1 not taken.
130 delete statistics_;
1497
1498
1/2
✓ Branch 0 taken 130 times.
✗ Branch 1 not taken.
130 delete available_jobs_;
1499
1500 130 curl_global_cleanup();
1501 130 }
1502
1503 /**
1504 * Spawns the I/O worker thread. No way back except ~S3FanoutManager.
1505 */
1506 130 void S3FanoutManager::Spawn() {
1507 130 LogCvmfs(kLogS3Fanout, kLogDebug, "S3FanoutManager spawned");
1508
1509 130 const int retval = pthread_create(&thread_upload_, NULL, MainUpload,
1510 static_cast<void *>(this));
1511
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
130 assert(retval == 0);
1512
1513 130 multi_threaded_.fetch_add(1);
1514 130 }
1515
1516 30 const Statistics &S3FanoutManager::GetStatistics() { return *statistics_; }
1517
1518 /**
1519 * Push new job to be uploaded to the S3 cloud storage.
1520 */
1521 6200 void S3FanoutManager::PushNewJob(JobInfo *info) {
1522 6200 available_jobs_->Increment();
1523 6200 WritePipe(pipe_jobs_[1], &info, sizeof(info));
1524 6200 }
1525
1526 /**
1527 * Push completed job to list of completed jobs
1528 */
1529 6330 void S3FanoutManager::PushCompletedJob(JobInfo *info) {
1530 6330 WritePipe(pipe_completed_[1], &info, sizeof(info));
1531 6330 }
1532
1533 /**
1534 * Pop completed job
1535 */
1536 6330 JobInfo *S3FanoutManager::PopCompletedJob() {
1537 JobInfo *info;
1538
1/2
✓ Branch 1 taken 6330 times.
✗ Branch 2 not taken.
6330 ReadPipe(pipe_completed_[0], &info, sizeof(info));
1539 6330 return info;
1540 }
1541
1542 //------------------------------------------------------------------------------
1543
1544
1545 ✗ string Statistics::Print() const {
1546 ✗ return "Transferred Bytes: " + StringifyInt(uint64_t(transferred_bytes))
1547 ✗ + "\n" + "Transfer duration: " + StringifyInt(uint64_t(transfer_time))
1548 ✗ + " s\n" + "Number of requests: " + StringifyInt(num_requests) + "\n"
1549 ✗ + "Number of retries: " + StringifyInt(num_retries) + "\n";
1550 }
1551
1552 } // namespace s3fanout
1553