GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/network/s3fanout.cc
Date: 2026-09-20 02:39:58
Exec Total Coverage
Lines: 649 916 70.9%
Branches: 469 1308 35.9%

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 14431 static string XmlEscape(const string &input) {
32 14431 string result;
33
1/2
✓ Branch 2 taken 14431 times.
✗ Branch 3 not taken.
14431 result.reserve(input.size());
34
2/2
✓ Branch 1 taken 329254 times.
✓ Branch 2 taken 14431 times.
343685 for (unsigned i = 0; i < input.size(); ++i) {
35
3/3
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 329252 times.
329254 switch (input[i]) {
36 1 case '&':
37
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 result += "&amp;";
38 1 break;
39 1 case '<':
40
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 result += "&lt;";
41 1 break;
42 329252 default:
43
1/2
✓ Branch 2 taken 329252 times.
✗ Branch 3 not taken.
329252 result += input[i];
44 329252 break;
45 }
46 }
47 14431 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 29478 string MkV2CanonicalResource(const string &bucket, const string &object_key,
61 bool dns_buckets, bool multi_delete) {
62 29478 string resource = "/" + bucket;
63
6/6
✓ Branch 0 taken 29475 times.
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 29305 times.
✓ Branch 4 taken 170 times.
✓ Branch 5 taken 29308 times.
✓ Branch 6 taken 170 times.
29478 if (dns_buckets || !object_key.empty())
64
2/4
✓ Branch 1 taken 29308 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 29308 times.
✗ Branch 5 not taken.
29308 resource += "/" + object_key;
65 // V2 requires subresources to be signed; multi-delete posts to "?delete"
66
2/2
✓ Branch 0 taken 170 times.
✓ Branch 1 taken 29308 times.
29478 if (multi_delete)
67
1/2
✓ Branch 1 taken 170 times.
✗ Branch 2 not taken.
170 resource += "?delete";
68 29478 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 172 string ComposeDeleteMultiXml(const vector<string> &keys) {
77 string xml = "<?xml version=\"1.0\" "
78
1/2
✓ Branch 2 taken 172 times.
✗ Branch 3 not taken.
172 "encoding=\"UTF-8\"?><Delete><Quiet>true</Quiet>";
79 // ~70 bytes per <Object><Key>...</Key></Object> entry
80
1/2
✓ Branch 3 taken 172 times.
✗ Branch 4 not taken.
172 xml.reserve(xml.size() + keys.size() * 70 + 10);
81
2/2
✓ Branch 1 taken 14431 times.
✓ Branch 2 taken 172 times.
14603 for (unsigned i = 0; i < keys.size(); ++i) {
82
4/8
✓ Branch 2 taken 14431 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 14431 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 14431 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 14431 times.
✗ Branch 12 not taken.
14431 xml += "<Object><Key>" + XmlEscape(keys[i]) + "</Key></Object>";
83 }
84
1/2
✓ Branch 1 taken 172 times.
✗ Branch 2 not taken.
172 xml += "</Delete>";
85 172 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 4 unsigned ParseDeleteMultiResponse(const string &response,
95 vector<string> *error_keys,
96 vector<string> *error_codes,
97 vector<string> *error_messages) {
98 4 unsigned num_errors = 0;
99 4 string::size_type pos = 0;
100
101 while (true) {
102 7 const string::size_type err_start = response.find("<Error>", pos);
103
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4 times.
7 if (err_start == string::npos)
104 3 break;
105 4 const string::size_type err_end = response.find("</Error>", err_start);
106
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (err_end == string::npos)
107 1 break;
108
109
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 const string error_block = response.substr(err_start, err_end - err_start);
110 3 num_errors++;
111
112 // Extract <Key>...</Key>
113 3 const string::size_type key_start = error_block.find("<Key>");
114 3 const string::size_type key_end = error_block.find("</Key>");
115
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 if (key_start != string::npos && key_end != string::npos) {
116
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 error_keys->push_back(
117
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
6 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 3 const string::size_type code_start = error_block.find("<Code>");
124 3 const string::size_type code_end = error_block.find("</Code>");
125
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 if (code_start != string::npos && code_end != string::npos) {
126
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 error_codes->push_back(
127
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
6 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 3 const string::size_type msg_start = error_block.find("<Message>");
134 3 const string::size_type msg_end = error_block.find("</Message>");
135
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 if (msg_start != string::npos && msg_end != string::npos) {
136
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 error_messages->push_back(
137
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
6 error_block.substr(msg_start + 9, msg_end - msg_start - 9));
138 } else {
139 error_messages->push_back("");
140 }
141
142 3 pos = err_end + 8; // length of "</Error>"
143 3 }
144
145 4 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 312 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 312 bool value_determined = false;
161
162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 312 times.
312 if (overrideMaxAge >= 0) {
163 max_age_sec = overrideMaxAge;
164 value_determined = true;
165 }
166
167
1/2
✓ Branch 0 taken 312 times.
✗ Branch 1 not taken.
312 if (!value_determined) {
168 312 var = getenv("CVMFS_MAX_TTL_SECS");
169
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
312 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 312 times.
✗ Branch 1 not taken.
312 if (!value_determined) {
178 312 var = getenv("CVMFS_MAX_TTL");
179
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
312 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 312 times.
✗ Branch 1 not taken.
312 if (!value_determined) {
189 312 max_age_sec = defaultMaxAge;
190 }
191
192
2/4
✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 312 times.
✗ Branch 5 not taken.
624 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 302 void S3FanoutManager::DetectThrottleIndicator(const std::string &header,
199 JobInfo *info) {
200 302 std::string value_str;
201
4/6
✓ Branch 2 taken 302 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 302 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 103 times.
✓ Branch 10 taken 199 times.
302 if (HasPrefix(header, "retry-after:", true))
202
1/2
✓ Branch 1 taken 103 times.
✗ Branch 2 not taken.
103 value_str = header.substr(12);
203
4/6
✓ Branch 2 taken 302 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 302 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 5 times.
✓ Branch 10 taken 297 times.
302 if (HasPrefix(header, "x-retry-in:", true))
204
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 value_str = header.substr(11);
205
206
1/2
✓ Branch 1 taken 302 times.
✗ Branch 2 not taken.
302 value_str = Trim(value_str, true /* trim_newline */);
207
2/2
✓ Branch 1 taken 106 times.
✓ Branch 2 taken 196 times.
302 if (!value_str.empty()) {
208
1/2
✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
106 const unsigned value_numeric = String2Uint64(value_str);
209
2/4
✓ Branch 2 taken 106 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 106 times.
✗ Branch 6 not taken.
212 const unsigned value_ms = HasSuffix(value_str, "ms", true /* ignore_case */)
210
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 101 times.
106 ? value_numeric
211 106 : (value_numeric * 1000);
212
2/2
✓ Branch 0 taken 105 times.
✓ Branch 1 taken 1 times.
106 if (value_ms > 0)
213 105 info->throttle_ms = std::min(value_ms, kMax429ThrottleMs);
214 }
215 302 }
216
217
218 /**
219 * Called by curl for every HTTP header. Not called for file:// transfers.
220 */
221 88800 static size_t CallbackCurlHeader(void *ptr, size_t size, size_t nmemb,
222 void *info_link) {
223 88800 const size_t num_bytes = size * nmemb;
224
1/2
✓ Branch 2 taken 88800 times.
✗ Branch 3 not taken.
88800 const string header_line(static_cast<const char *>(ptr), num_bytes);
225 88800 JobInfo *info = static_cast<JobInfo *>(info_link);
226
227 // Check for http status code errors
228
4/6
✓ Branch 2 taken 88800 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 88800 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 29568 times.
✓ Branch 10 taken 59232 times.
88800 if (HasPrefix(header_line, "HTTP/1.", false)) {
229
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 29568 times.
29568 if (header_line.length() < 10)
230 return 0;
231
232 unsigned i;
233
5/6
✓ Branch 1 taken 59136 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 29568 times.
✓ Branch 5 taken 29568 times.
✓ Branch 6 taken 29568 times.
✓ Branch 7 taken 29568 times.
59136 for (i = 8; (i < header_line.length()) && (header_line[i] == ' '); ++i) {
234 }
235
236
2/2
✓ Branch 1 taken 14832 times.
✓ Branch 2 taken 14736 times.
29568 if (header_line[i] == '2') {
237 14832 return num_bytes;
238 } else {
239
1/2
✓ Branch 2 taken 14736 times.
✗ Branch 3 not taken.
14736 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 14736 times.
14736 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 14736 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 14736 times.
✗ Branch 7 not taken.
14736 info->http_error = String2Int64(string(&header_line[i], 3));
248
249
2/7
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 14640 times.
✗ Branch 6 not taken.
14736 switch (info->http_error) {
250 96 case 429:
251 96 info->error_code = kFailRetry;
252 96 info->throttle_ms = S3FanoutManager::kDefault429ThrottleMs;
253 96 info->throttle_timestamp = platform_monotonic_time();
254 96 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 14640 case 404:
271 14640 info->error_code = kFailNotFound;
272 14640 return num_bytes;
273 default:
274 info->error_code = kFailOther;
275 }
276 return 0;
277 }
278 }
279
280
2/2
✓ Branch 0 taken 288 times.
✓ Branch 1 taken 58944 times.
59232 if (info->error_code == kFailRetry) {
281
1/2
✓ Branch 1 taken 288 times.
✗ Branch 2 not taken.
288 S3FanoutManager::DetectThrottleIndicator(header_line, info);
282 }
283
284 59232 return num_bytes;
285 88800 }
286
287
288 /**
289 * Called by curl for every new chunk to upload.
290 */
291 376224 static size_t CallbackCurlData(void *ptr, size_t size, size_t nmemb,
292 void *info_link) {
293 376224 const size_t num_bytes = size * nmemb;
294 376224 JobInfo *info = static_cast<JobInfo *>(info_link);
295
296 376224 LogCvmfs(kLogS3Fanout, kLogDebug, "Data callback with %zu bytes", num_bytes);
297
298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 376224 times.
376224 if (num_bytes == 0)
299 return 0;
300
301 376224 const uint64_t read_bytes = info->origin->Read(ptr, num_bytes);
302
303 376224 LogCvmfs(kLogS3Fanout, kLogDebug, "source buffer pushed out %lu bytes",
304 read_bytes);
305
306 376224 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 65544 int S3FanoutManager::CallbackCurlSocket(CURL *easy, curl_socket_t s, int action,
329 void *userp, void *socketp) {
330 65544 S3FanoutManager *s3fanout_mgr = static_cast<S3FanoutManager *>(userp);
331 65544 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 65544 s3fanout_mgr->available_jobs_->Get());
337
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65544 times.
65544 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 103320 times.
✓ Branch 1 taken 29568 times.
132888 for (index = 2; index < s3fanout_mgr->watch_fds_inuse_; ++index) {
344
2/2
✓ Branch 0 taken 35976 times.
✓ Branch 1 taken 67344 times.
103320 if (s3fanout_mgr->watch_fds_[index].fd == s)
345 35976 break;
346 }
347 // Or create newly
348
2/2
✓ Branch 0 taken 29568 times.
✓ Branch 1 taken 35976 times.
65544 if (index == s3fanout_mgr->watch_fds_inuse_) {
349 // Extend array if necessary
350
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 29496 times.
29568 if (s3fanout_mgr->watch_fds_inuse_ == s3fanout_mgr->watch_fds_size_) {
351 72 s3fanout_mgr->watch_fds_size_ *= 2;
352 72 s3fanout_mgr->watch_fds_ = static_cast<struct pollfd *>(
353 72 srealloc(s3fanout_mgr->watch_fds_,
354 72 s3fanout_mgr->watch_fds_size_ * sizeof(struct pollfd)));
355 }
356 29568 s3fanout_mgr->watch_fds_[s3fanout_mgr->watch_fds_inuse_].fd = s;
357 29568 s3fanout_mgr->watch_fds_[s3fanout_mgr->watch_fds_inuse_].events = 0;
358 29568 s3fanout_mgr->watch_fds_[s3fanout_mgr->watch_fds_inuse_].revents = 0;
359 29568 s3fanout_mgr->watch_fds_inuse_++;
360 }
361
362
4/5
✓ Branch 0 taken 29568 times.
✓ Branch 1 taken 72 times.
✓ Branch 2 taken 6336 times.
✓ Branch 3 taken 29568 times.
✗ Branch 4 not taken.
65544 switch (action) {
363 29568 case CURL_POLL_IN:
364 29568 s3fanout_mgr->watch_fds_[index].events = POLLIN | POLLPRI;
365 29568 break;
366 72 case CURL_POLL_OUT:
367 72 s3fanout_mgr->watch_fds_[index].events = POLLOUT | POLLWRBAND;
368 72 break;
369 6336 case CURL_POLL_INOUT:
370 6336 s3fanout_mgr->watch_fds_[index].events = POLLIN | POLLPRI | POLLOUT
371 | POLLWRBAND;
372 6336 break;
373 29568 case CURL_POLL_REMOVE:
374
2/2
✓ Branch 0 taken 4776 times.
✓ Branch 1 taken 24792 times.
29568 if (index < s3fanout_mgr->watch_fds_inuse_ - 1)
375 s3fanout_mgr
376 4776 ->watch_fds_[index] = s3fanout_mgr->watch_fds_
377 4776 [s3fanout_mgr->watch_fds_inuse_ - 1];
378 29568 s3fanout_mgr->watch_fds_inuse_--;
379 // Shrink array if necessary
380
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29568 times.
29568 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 29568 break;
389 default:
390 PANIC(NULL);
391 }
392
393 65544 return 0;
394 }
395
396
397 /**
398 * Worker thread event loop.
399 */
400 312 void *S3FanoutManager::MainUpload(void *data) {
401
1/2
✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
312 LogCvmfs(kLogS3Fanout, kLogDebug, "Upload I/O thread started");
402 312 S3FanoutManager *s3fanout_mgr = static_cast<S3FanoutManager *>(data);
403
404 312 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 312 unsigned jobs_in_flight = 0;
410
411 while (true) {
412 // Check events with 100ms timeout
413 400944 const int timeout_ms = 100;
414
1/2
✓ Branch 1 taken 400944 times.
✗ Branch 2 not taken.
400944 int retval = poll(s3fanout_mgr->watch_fds_, s3fanout_mgr->watch_fds_inuse_,
415 timeout_ms);
416
2/2
✓ Branch 0 taken 2328 times.
✓ Branch 1 taken 398616 times.
400944 if (retval == 0) {
417 // Handle timeout
418 2328 int still_running = 0;
419
1/2
✓ Branch 1 taken 2328 times.
✗ Branch 2 not taken.
2328 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 2328 times.
2328 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 398616 times.
398616 } else if (retval < 0) {
426 assert(errno == EINTR);
427 continue;
428 }
429
430 // Terminate I/O thread
431
2/2
✓ Branch 0 taken 312 times.
✓ Branch 1 taken 400632 times.
400944 if (s3fanout_mgr->watch_fds_[0].revents)
432 312 break;
433
434 // New job incoming
435
2/2
✓ Branch 0 taken 14880 times.
✓ Branch 1 taken 385752 times.
400632 if (s3fanout_mgr->watch_fds_[1].revents) {
436 14880 s3fanout_mgr->watch_fds_[1].revents = 0;
437 JobInfo *info;
438
1/2
✓ Branch 1 taken 14880 times.
✗ Branch 2 not taken.
14880 ReadPipe(s3fanout_mgr->pipe_jobs_[0], &info, sizeof(info));
439
1/2
✓ Branch 1 taken 14880 times.
✗ Branch 2 not taken.
14880 CURL *handle = s3fanout_mgr->AcquireCurlHandle();
440
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14880 times.
14880 if (handle == NULL) {
441 PANIC(kLogStderr, "Failed to acquire CURL handle.");
442 }
443
1/2
✓ Branch 1 taken 14880 times.
✗ Branch 2 not taken.
14880 const s3fanout::Failures init_failure = s3fanout_mgr->InitializeRequest(
444 info, handle);
445
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14880 times.
14880 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 14880 times.
✗ Branch 2 not taken.
14880 s3fanout_mgr->SetUrlOptions(info);
451
452
1/2
✓ Branch 1 taken 14880 times.
✗ Branch 2 not taken.
14880 curl_multi_add_handle(s3fanout_mgr->curl_multi_, handle);
453
1/2
✓ Branch 1 taken 14880 times.
✗ Branch 2 not taken.
14880 s3fanout_mgr->active_requests_->insert(info);
454 14880 jobs_in_flight++;
455 14880 int still_running = 0, retval = 0;
456
1/2
✓ Branch 1 taken 14880 times.
✗ Branch 2 not taken.
14880 retval = curl_multi_socket_action(s3fanout_mgr->curl_multi_,
457 CURL_SOCKET_TIMEOUT, 0, &still_running);
458
459
1/2
✓ Branch 1 taken 14880 times.
✗ Branch 2 not taken.
14880 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 894192 times.
✓ Branch 1 taken 400632 times.
1294824 for (int32_t i = s3fanout_mgr->watch_fds_inuse_ - 1; i >= 2; --i) {
471
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 894192 times.
894192 if (static_cast<uint32_t>(i) >= s3fanout_mgr->watch_fds_inuse_) {
472 continue;
473 }
474
2/2
✓ Branch 0 taken 409896 times.
✓ Branch 1 taken 484296 times.
894192 if (s3fanout_mgr->watch_fds_[i].revents) {
475 409896 int ev_bitmask = 0;
476
2/2
✓ Branch 0 taken 44328 times.
✓ Branch 1 taken 365568 times.
409896 if (s3fanout_mgr->watch_fds_[i].revents & (POLLIN | POLLPRI))
477 44328 ev_bitmask |= CURL_CSELECT_IN;
478
2/2
✓ Branch 0 taken 365568 times.
✓ Branch 1 taken 44328 times.
409896 if (s3fanout_mgr->watch_fds_[i].revents & (POLLOUT | POLLWRBAND))
479 365568 ev_bitmask |= CURL_CSELECT_OUT;
480 409896 if (s3fanout_mgr->watch_fds_[i].revents
481
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 409896 times.
409896 & (POLLERR | POLLHUP | POLLNVAL))
482 ev_bitmask |= CURL_CSELECT_ERR;
483 409896 s3fanout_mgr->watch_fds_[i].revents = 0;
484
485 409896 int still_running = 0;
486 409896 retval = curl_multi_socket_action(s3fanout_mgr->curl_multi_,
487
1/2
✓ Branch 1 taken 409896 times.
✗ Branch 2 not taken.
409896 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 430200 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 29568 times.
✓ Branch 4 taken 400632 times.
430200 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 29568 times.
29568 assert(curl_msg->msg == CURLMSG_DONE);
499
500 29568 s3fanout_mgr->statistics_->num_requests++;
501 JobInfo *info;
502 29568 CURL *easy_handle = curl_msg->easy_handle;
503 29568 const int curl_error = curl_msg->data.result;
504
1/2
✓ Branch 1 taken 29568 times.
✗ Branch 2 not taken.
29568 curl_easy_getinfo(easy_handle, CURLINFO_PRIVATE, &info);
505
506
1/2
✓ Branch 1 taken 29568 times.
✗ Branch 2 not taken.
29568 curl_multi_remove_handle(s3fanout_mgr->curl_multi_, easy_handle);
507
3/4
✓ Branch 1 taken 29568 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14688 times.
✓ Branch 4 taken 14880 times.
29568 if (s3fanout_mgr->VerifyAndFinalize(curl_error, info)) {
508
1/2
✓ Branch 1 taken 14688 times.
✗ Branch 2 not taken.
14688 curl_multi_add_handle(s3fanout_mgr->curl_multi_, easy_handle);
509 14688 int still_running = 0;
510
1/2
✓ Branch 1 taken 14688 times.
✗ Branch 2 not taken.
14688 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 14880 jobs_in_flight--;
515
1/2
✓ Branch 1 taken 14880 times.
✗ Branch 2 not taken.
14880 s3fanout_mgr->active_requests_->erase(info);
516
1/2
✓ Branch 1 taken 14880 times.
✗ Branch 2 not taken.
14880 s3fanout_mgr->ReleaseCurlHandle(info, easy_handle);
517
1/2
✓ Branch 1 taken 14880 times.
✗ Branch 2 not taken.
14880 s3fanout_mgr->available_jobs_->Decrement();
518
519 // Add to list of completed jobs
520
1/2
✓ Branch 1 taken 14880 times.
✗ Branch 2 not taken.
14880 s3fanout_mgr->PushCompletedJob(info);
521 }
522 }
523 400632 }
524
525 312 set<CURL *>::iterator i = s3fanout_mgr->pool_handles_inuse_->begin();
526 312 const set<CURL *>::const_iterator i_end = s3fanout_mgr->pool_handles_inuse_
527 312 ->end();
528
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 312 times.
312 for (; i != i_end; ++i) {
529 curl_multi_remove_handle(s3fanout_mgr->curl_multi_, *i);
530 curl_easy_cleanup(*i);
531 }
532 312 s3fanout_mgr->pool_handles_inuse_->clear();
533 312 free(s3fanout_mgr->watch_fds_);
534
535
1/2
✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
312 LogCvmfs(kLogS3Fanout, kLogDebug, "Upload I/O thread terminated");
536 312 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 14880 CURL *S3FanoutManager::AcquireCurlHandle() const {
545 CURL *handle;
546
547 14880 const MutexLockGuard guard(curl_handle_lock_);
548
549
2/2
✓ Branch 1 taken 1320 times.
✓ Branch 2 taken 13560 times.
14880 if (pool_handles_idle_->empty()) {
550 CURLcode retval;
551
552 // Create a new handle
553
1/2
✓ Branch 1 taken 1320 times.
✗ Branch 2 not taken.
1320 handle = curl_easy_init();
554
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1320 times.
1320 assert(handle != NULL);
555
556 // Other settings
557
1/2
✓ Branch 1 taken 1320 times.
✗ Branch 2 not taken.
1320 retval = curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1);
558
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1320 times.
1320 assert(retval == CURLE_OK);
559
1/2
✓ Branch 1 taken 1320 times.
✗ Branch 2 not taken.
1320 retval = curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION,
560 CallbackCurlHeader);
561
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1320 times.
1320 assert(retval == CURLE_OK);
562
1/2
✓ Branch 1 taken 1320 times.
✗ Branch 2 not taken.
1320 retval = curl_easy_setopt(handle, CURLOPT_READFUNCTION, CallbackCurlData);
563
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1320 times.
1320 assert(retval == CURLE_OK);
564
1/2
✓ Branch 1 taken 1320 times.
✗ Branch 2 not taken.
1320 retval = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, CallbackCurlBody);
565
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1320 times.
1320 assert(retval == CURLE_OK);
566 // WRITEDATA is set per-request in InitializeRequest
567 } else {
568 13560 handle = *(pool_handles_idle_->begin());
569
1/2
✓ Branch 2 taken 13560 times.
✗ Branch 3 not taken.
13560 pool_handles_idle_->erase(pool_handles_idle_->begin());
570 }
571
572
1/2
✓ Branch 1 taken 14880 times.
✗ Branch 2 not taken.
14880 pool_handles_inuse_->insert(handle);
573
574 14880 return handle;
575 14880 }
576
577
578 14880 void S3FanoutManager::ReleaseCurlHandle(JobInfo *info, CURL *handle) const {
579
1/2
✓ Branch 0 taken 14880 times.
✗ Branch 1 not taken.
14880 if (info->http_headers) {
580
1/2
✓ Branch 1 taken 14880 times.
✗ Branch 2 not taken.
14880 curl_slist_free_all(info->http_headers);
581 14880 info->http_headers = NULL;
582 }
583
584 14880 const MutexLockGuard guard(curl_handle_lock_);
585
586
1/2
✓ Branch 1 taken 14880 times.
✗ Branch 2 not taken.
14880 const set<CURL *>::iterator elem = pool_handles_inuse_->find(handle);
587
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 14880 times.
14880 assert(elem != pool_handles_inuse_->end());
588
589
2/2
✓ Branch 1 taken 696 times.
✓ Branch 2 taken 14184 times.
14880 if (pool_handles_idle_->size() > config_.pool_max_handles) {
590
1/2
✓ Branch 1 taken 696 times.
✗ Branch 2 not taken.
696 const CURLcode retval = curl_easy_setopt(handle, CURLOPT_SHARE, NULL);
591
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 696 times.
696 assert(retval == CURLE_OK);
592
1/2
✓ Branch 1 taken 696 times.
✗ Branch 2 not taken.
696 curl_easy_cleanup(handle);
593 const std::map<CURL *, S3FanOutDnsEntry *>::size_type
594
1/2
✓ Branch 1 taken 696 times.
✗ Branch 2 not taken.
696 retitems = curl_sharehandles_->erase(handle);
595
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 696 times.
696 assert(retitems == 1);
596 } else {
597
1/2
✓ Branch 1 taken 14184 times.
✗ Branch 2 not taken.
14184 pool_handles_idle_->insert(handle);
598 }
599
600
1/2
✓ Branch 1 taken 14880 times.
✗ Branch 2 not taken.
14880 pool_handles_inuse_->erase(elem);
601 14880 }
602
603 312 void S3FanoutManager::InitPipeWatchFds() {
604
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 312 times.
312 assert(watch_fds_inuse_ == 0);
605
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 312 times.
312 assert(watch_fds_size_ >= 2);
606 312 watch_fds_[0].fd = pipe_terminate_[0];
607 312 watch_fds_[0].events = POLLIN | POLLPRI;
608 312 watch_fds_[0].revents = 0;
609 312 ++watch_fds_inuse_;
610 312 watch_fds_[1].fd = pipe_jobs_[0];
611 312 watch_fds_[1].events = POLLIN | POLLPRI;
612 312 watch_fds_[1].revents = 0;
613 312 ++watch_fds_inuse_;
614 312 }
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 29472 bool S3FanoutManager::MkV2Authz(const JobInfo &info,
621 vector<string> *headers) const {
622 29472 string payload_hash;
623
1/2
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
29472 const bool retval = MkPayloadHash(info, &payload_hash);
624
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29472 times.
29472 if (!retval)
625 return false;
626
1/2
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
29472 const string content_type = GetContentType(info);
627
1/2
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
29472 const string request = GetRequestString(info);
628
629
1/2
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
29472 const string timestamp = RfcTimestamp();
630
5/10
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 29472 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 29472 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 29472 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 29472 times.
✗ Branch 14 not taken.
58944 string to_sign = request + "\n" + payload_hash + "\n" + content_type + "\n"
631
2/4
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 29472 times.
✗ Branch 5 not taken.
58944 + timestamp + "\n";
632
2/4
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 29472 times.
✗ Branch 4 not taken.
29472 if (config_.x_amz_acl != "")
633
1/2
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
58944 to_sign += "x-amz-acl:" + config_.x_amz_acl
634
2/4
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 29472 times.
✗ Branch 5 not taken.
29472 + "\n"; // CanonicalizedAmzHeaders
635 29472 to_sign += MkV2CanonicalResource(config_.bucket, info.object_key,
636 29472 config_.dns_buckets,
637
2/4
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 29472 times.
✗ Branch 5 not taken.
29472 info.request == JobInfo::kReqDeleteMulti);
638
1/2
✓ Branch 3 taken 29472 times.
✗ Branch 4 not taken.
29472 LogCvmfs(kLogS3Fanout, kLogDebug, "%s string to sign: %s", request.c_str(),
639 to_sign.c_str());
640
641
1/2
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
29472 shash::Any hmac;
642 29472 hmac.algorithm = shash::kSha1;
643
1/2
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
29472 shash::Hmac(config_.secret_key,
644 29472 reinterpret_cast<const unsigned char *>(to_sign.data()),
645 29472 to_sign.length(), &hmac);
646
647
3/6
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 29472 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 29472 times.
✗ Branch 8 not taken.
88416 headers->push_back("Authorization: AWS " + config_.access_key + ":"
648
3/6
✓ Branch 2 taken 29472 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 29472 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 29472 times.
✗ Branch 9 not taken.
147360 + Base64(string(reinterpret_cast<char *>(hmac.digest),
649 29472 hmac.GetDigestSize())));
650
2/4
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 29472 times.
✗ Branch 5 not taken.
29472 headers->push_back("Date: " + timestamp);
651
2/4
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 29472 times.
✗ Branch 5 not taken.
29472 headers->push_back("X-Amz-Acl: " + config_.x_amz_acl);
652
2/2
✓ Branch 1 taken 14760 times.
✓ Branch 2 taken 14712 times.
29472 if (!payload_hash.empty())
653
2/4
✓ Branch 1 taken 14760 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 14760 times.
✗ Branch 5 not taken.
14760 headers->push_back("Content-MD5: " + payload_hash);
654
2/2
✓ Branch 1 taken 14760 times.
✓ Branch 2 taken 14712 times.
29472 if (!content_type.empty())
655
2/4
✓ Branch 1 taken 14760 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 14760 times.
✗ Branch 5 not taken.
14760 headers->push_back("Content-Type: " + content_type);
656 29472 return true;
657 29472 }
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 29472 void S3FanoutManager::InitializeDnsSettingsCurl(CURL *handle,
848 CURLSH *sharehandle,
849 curl_slist *clist) const {
850 29472 CURLcode retval = curl_easy_setopt(handle, CURLOPT_SHARE, sharehandle);
851
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29472 times.
29472 assert(retval == CURLE_OK);
852 29472 retval = curl_easy_setopt(handle, CURLOPT_RESOLVE, clist);
853
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29472 times.
29472 assert(retval == CURLE_OK);
854 29472 }
855
856
857 29472 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 29472 times.
✗ Branch 2 not taken.
29472 it = curl_sharehandles_->find(handle);
862
2/2
✓ Branch 3 taken 28152 times.
✓ Branch 4 taken 1320 times.
29472 if (it != curl_sharehandles_->end()) {
863
1/2
✓ Branch 1 taken 28152 times.
✗ Branch 2 not taken.
28152 InitializeDnsSettingsCurl(handle, it->second->sharehandle,
864 28152 it->second->clist);
865 28152 return 0;
866 }
867
868 // Add protocol information for extraction of fields for DNS
869
2/4
✓ Branch 1 taken 1320 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1320 times.
✗ Branch 4 not taken.
1320 if (!IsHttpUrl(host_with_port))
870
2/4
✓ Branch 1 taken 1320 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1320 times.
✗ Branch 5 not taken.
1320 host_with_port = config_.protocol + "://" + host_with_port;
871
1/2
✓ Branch 1 taken 1320 times.
✗ Branch 2 not taken.
1320 const std::string remote_host = dns::ExtractHost(host_with_port);
872
1/2
✓ Branch 1 taken 1320 times.
✗ Branch 2 not taken.
1320 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 1320 S3FanOutDnsEntry *useme = NULL;
876 1320 unsigned int usemin = UINT_MAX;
877 1320 std::set<S3FanOutDnsEntry *>::iterator its3 = sharehandles_->begin();
878
2/2
✓ Branch 3 taken 1056 times.
✓ Branch 4 taken 1320 times.
2376 for (; its3 != sharehandles_->end(); ++its3) {
879
1/2
✓ Branch 2 taken 1056 times.
✗ Branch 3 not taken.
1056 if ((*its3)->dns_name == remote_host) {
880
1/2
✓ Branch 1 taken 1056 times.
✗ Branch 2 not taken.
1056 if (usemin >= (*its3)->counter) {
881 1056 usemin = (*its3)->counter;
882 1056 useme = (*its3);
883 }
884 }
885 }
886
2/2
✓ Branch 0 taken 1056 times.
✓ Branch 1 taken 264 times.
1320 if (useme != NULL) {
887
1/2
✓ Branch 1 taken 1056 times.
✗ Branch 2 not taken.
1056 curl_sharehandles_->insert(
888 1056 std::pair<CURL *, S3FanOutDnsEntry *>(handle, useme));
889 1056 useme->counter++;
890
1/2
✓ Branch 1 taken 1056 times.
✗ Branch 2 not taken.
1056 InitializeDnsSettingsCurl(handle, useme->sharehandle, useme->clist);
891 1056 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 264 times.
✗ Branch 2 not taken.
264 const dns::Host host = resolver_->Resolve(remote_host);
897
1/2
✓ Branch 2 taken 264 times.
✗ Branch 3 not taken.
264 set<string> const ipv4_addresses = host.ipv4_addresses();
898 264 std::set<string>::iterator its = ipv4_addresses.begin();
899 264 S3FanOutDnsEntry *dnse = NULL;
900
2/2
✓ Branch 3 taken 264 times.
✓ Branch 4 taken 264 times.
528 for (; its != ipv4_addresses.end(); ++its) {
901
2/4
✓ Branch 1 taken 264 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 264 times.
✗ Branch 5 not taken.
264 dnse = new S3FanOutDnsEntry();
902 264 dnse->counter = 0;
903
1/2
✓ Branch 1 taken 264 times.
✗ Branch 2 not taken.
264 dnse->dns_name = remote_host;
904
4/12
✗ Branch 1 not taken.
✓ Branch 2 taken 264 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 8 taken 264 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 264 times.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 264 times.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
264 dnse->port = remote_port.size() == 0 ? "80" : remote_port;
905
1/2
✓ Branch 2 taken 264 times.
✗ Branch 3 not taken.
264 dnse->ip = *its;
906 264 dnse->clist = NULL;
907
1/2
✓ Branch 2 taken 264 times.
✗ Branch 3 not taken.
264 dnse->clist = curl_slist_append(
908 dnse->clist,
909
4/8
✓ Branch 1 taken 264 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 264 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 264 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 264 times.
✗ Branch 11 not taken.
528 (dnse->dns_name + ":" + dnse->port + ":" + dnse->ip).c_str());
910
1/2
✓ Branch 1 taken 264 times.
✗ Branch 2 not taken.
264 dnse->sharehandle = curl_share_init();
911
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 264 times.
264 assert(dnse->sharehandle != NULL);
912
1/2
✓ Branch 1 taken 264 times.
✗ Branch 2 not taken.
264 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 264 times.
264 assert(share_retval == CURLSHE_OK);
915
1/2
✓ Branch 1 taken 264 times.
✗ Branch 2 not taken.
264 sharehandles_->insert(dnse);
916 }
917
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 264 times.
264 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 264 times.
✗ Branch 2 not taken.
264 curl_sharehandles_->insert(
925 264 std::pair<CURL *, S3FanOutDnsEntry *>(handle, dnse));
926 264 dnse->counter++;
927
1/2
✓ Branch 1 taken 264 times.
✗ Branch 2 not taken.
264 InitializeDnsSettingsCurl(handle, dnse->sharehandle, dnse->clist);
928
929 264 return 0;
930 1320 }
931
932
933 29472 bool S3FanoutManager::MkPayloadHash(const JobInfo &info,
934 string *hex_hash) const {
935
2/2
✓ Branch 0 taken 29352 times.
✓ Branch 1 taken 120 times.
29472 if (info.request == JobInfo::kReqHeadOnly
936
2/2
✓ Branch 0 taken 14760 times.
✓ Branch 1 taken 14592 times.
29352 || info.request == JobInfo::kReqHeadPut
937
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14760 times.
14760 || info.request == JobInfo::kReqDelete) {
938
1/4
✓ Branch 0 taken 14712 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
14712 switch (config_.authz_method) {
939 14712 case kAuthzAwsV2:
940 14712 hex_hash->clear();
941 14712 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 14712 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 14760 times.
✗ Branch 2 not taken.
14760 shash::Any payload_hash(shash::kMd5);
962
963 unsigned char *data;
964 14760 const unsigned int nbytes = info.origin->Data(
965
2/4
✓ Branch 2 taken 14760 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 14760 times.
✗ Branch 6 not taken.
14760 reinterpret_cast<void **>(&data), info.origin->GetSize(), 0);
966
2/4
✓ Branch 2 taken 14760 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 14760 times.
14760 assert(nbytes == info.origin->GetSize());
967
968
1/4
✓ Branch 0 taken 14760 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
14760 switch (config_.authz_method) {
969 14760 case kAuthzAwsV2:
970
1/2
✓ Branch 1 taken 14760 times.
✗ Branch 2 not taken.
14760 shash::HashMem(data, nbytes, &payload_hash);
971
2/4
✓ Branch 2 taken 14760 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 14760 times.
✗ Branch 6 not taken.
44280 *hex_hash = Base64(string(reinterpret_cast<char *>(payload_hash.digest),
972 29520 payload_hash.GetDigestSize()));
973 14760 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 29472 string S3FanoutManager::GetRequestString(const JobInfo &info) const {
987
3/5
✓ Branch 0 taken 14712 times.
✓ Branch 1 taken 14592 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 168 times.
✗ Branch 4 not taken.
29472 switch (info.request) {
988 14712 case JobInfo::kReqHeadOnly:
989 case JobInfo::kReqHeadPut:
990
1/2
✓ Branch 2 taken 14712 times.
✗ Branch 3 not taken.
14712 return "HEAD";
991 14592 case JobInfo::kReqPutCas:
992 case JobInfo::kReqPutDotCvmfs:
993 case JobInfo::kReqPutHtml:
994 case JobInfo::kReqPutBucket:
995
1/2
✓ Branch 2 taken 14592 times.
✗ Branch 3 not taken.
14592 return "PUT";
996 case JobInfo::kReqDelete:
997 return "DELETE";
998 168 case JobInfo::kReqDeleteMulti:
999
1/2
✓ Branch 2 taken 168 times.
✗ Branch 3 not taken.
168 return "POST";
1000 default:
1001 PANIC(NULL);
1002 }
1003 }
1004
1005
1006 29472 string S3FanoutManager::GetContentType(const JobInfo &info) const {
1007
3/6
✓ Branch 0 taken 14712 times.
✓ Branch 1 taken 14592 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 168 times.
✗ Branch 5 not taken.
29472 switch (info.request) {
1008 14712 case JobInfo::kReqHeadOnly:
1009 case JobInfo::kReqHeadPut:
1010 case JobInfo::kReqDelete:
1011
1/2
✓ Branch 2 taken 14712 times.
✗ Branch 3 not taken.
14712 return "";
1012 14592 case JobInfo::kReqPutCas:
1013
1/2
✓ Branch 2 taken 14592 times.
✗ Branch 3 not taken.
14592 return "application/octet-stream";
1014 case JobInfo::kReqPutDotCvmfs:
1015 return "application/x-cvmfs";
1016 case JobInfo::kReqPutHtml:
1017 return "text/html";
1018 168 case JobInfo::kReqPutBucket:
1019 case JobInfo::kReqDeleteMulti:
1020
1/2
✓ Branch 2 taken 168 times.
✗ Branch 3 not taken.
168 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 29472 Failures S3FanoutManager::InitializeRequest(JobInfo *info, CURL *handle) const {
1032 // Initialize internal download state
1033 29472 info->curl_handle = handle;
1034 29472 info->error_code = kFailOk;
1035 29472 info->http_error = 0;
1036 29472 info->num_retries = 0;
1037 29472 info->backoff_ms = 0;
1038 29472 info->throttle_ms = 0;
1039 29472 info->throttle_timestamp = 0;
1040 29472 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 29472 times.
✗ Branch 3 not taken.
29472 info->payload_size = info->origin->GetSize();
1044
1045
2/4
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 29472 times.
✗ Branch 5 not taken.
29472 InitializeDnsSettings(handle, complete_hostname_);
1046
1047 CURLcode retval;
1048
2/2
✓ Branch 0 taken 29352 times.
✓ Branch 1 taken 120 times.
29472 if (info->request == JobInfo::kReqHeadOnly
1049
2/2
✓ Branch 0 taken 14760 times.
✓ Branch 1 taken 14592 times.
29352 || info->request == JobInfo::kReqHeadPut
1050
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14760 times.
14760 || info->request == JobInfo::kReqDelete) {
1051
1/2
✓ Branch 1 taken 14712 times.
✗ Branch 2 not taken.
14712 retval = curl_easy_setopt(handle, CURLOPT_UPLOAD, 0);
1052
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14712 times.
14712 assert(retval == CURLE_OK);
1053
1/2
✓ Branch 1 taken 14712 times.
✗ Branch 2 not taken.
14712 retval = curl_easy_setopt(handle, CURLOPT_NOBODY, 1);
1054
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14712 times.
14712 assert(retval == CURLE_OK);
1055
1056
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14712 times.
14712 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 14712 times.
✗ Branch 2 not taken.
14712 retval = curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, NULL);
1062
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14712 times.
14712 assert(retval == CURLE_OK);
1063 }
1064
2/2
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 14592 times.
14760 } else if (info->request == JobInfo::kReqDeleteMulti) {
1065 // POST request with XML body read from origin buffer
1066
1/2
✓ Branch 1 taken 168 times.
✗ Branch 2 not taken.
168 retval = curl_easy_setopt(handle, CURLOPT_UPLOAD, 1);
1067
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 168 times.
168 assert(retval == CURLE_OK);
1068
1/2
✓ Branch 1 taken 168 times.
✗ Branch 2 not taken.
168 retval = curl_easy_setopt(handle, CURLOPT_NOBODY, 0);
1069
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 168 times.
168 assert(retval == CURLE_OK);
1070
1/2
✓ Branch 1 taken 168 times.
✗ Branch 2 not taken.
168 retval = curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "POST");
1071
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 168 times.
168 assert(retval == CURLE_OK);
1072
2/4
✓ Branch 2 taken 168 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 168 times.
✗ Branch 6 not taken.
168 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 168 times.
168 assert(retval == CURLE_OK);
1075 168 info->response_body.clear();
1076 } else {
1077
1/2
✓ Branch 1 taken 14592 times.
✗ Branch 2 not taken.
14592 retval = curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, NULL);
1078
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14592 times.
14592 assert(retval == CURLE_OK);
1079
1/2
✓ Branch 1 taken 14592 times.
✗ Branch 2 not taken.
14592 retval = curl_easy_setopt(handle, CURLOPT_UPLOAD, 1);
1080
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14592 times.
14592 assert(retval == CURLE_OK);
1081
1/2
✓ Branch 1 taken 14592 times.
✗ Branch 2 not taken.
14592 retval = curl_easy_setopt(handle, CURLOPT_NOBODY, 0);
1082
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14592 times.
14592 assert(retval == CURLE_OK);
1083
2/4
✓ Branch 2 taken 14592 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 14592 times.
✗ Branch 6 not taken.
14592 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 14592 times.
14592 assert(retval == CURLE_OK);
1086
1087
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14592 times.
14592 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 14592 times.
✗ Branch 1 not taken.
14592 } else if (info->request == JobInfo::kReqPutCas) {
1091
1/2
✓ Branch 1 taken 14592 times.
✗ Branch 2 not taken.
14592 info->http_headers = curl_slist_append(info->http_headers,
1092 kCacheControlCas);
1093 }
1094 }
1095
1096 bool retval_b;
1097
1098 // Authorization
1099 29472 vector<string> authz_headers;
1100
1/4
✓ Branch 0 taken 29472 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
29472 switch (config_.authz_method) {
1101 29472 case kAuthzAwsV2:
1102
1/2
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
29472 retval_b = MkV2Authz(*info, &authz_headers);
1103 29472 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 29472 times.
29472 if (!retval_b)
1114 return kFailLocalIO;
1115
2/2
✓ Branch 1 taken 117936 times.
✓ Branch 2 taken 29472 times.
147408 for (unsigned i = 0; i < authz_headers.size(); ++i) {
1116
1/2
✓ Branch 2 taken 117936 times.
✗ Branch 3 not taken.
117936 info->http_headers = curl_slist_append(info->http_headers,
1117 117936 authz_headers[i].c_str());
1118 }
1119
1120 // Common headers
1121
1/2
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
29472 info->http_headers = curl_slist_append(info->http_headers,
1122 "Connection: Keep-Alive");
1123
1/2
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
29472 info->http_headers = curl_slist_append(info->http_headers, "Pragma:");
1124 // No 100-continue
1125
1/2
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
29472 info->http_headers = curl_slist_append(info->http_headers, "Expect:");
1126 // Strip unnecessary header
1127
1/2
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
29472 info->http_headers = curl_slist_append(info->http_headers, "Accept:");
1128
1/2
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
29472 info->http_headers = curl_slist_append(info->http_headers,
1129 29472 user_agent_->c_str());
1130
1131 // Set curl parameters
1132
1/2
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
29472 retval = curl_easy_setopt(handle, CURLOPT_PRIVATE, static_cast<void *>(info));
1133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29472 times.
29472 assert(retval == CURLE_OK);
1134
1/2
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
29472 retval = curl_easy_setopt(handle, CURLOPT_HEADERDATA,
1135 static_cast<void *>(info));
1136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29472 times.
29472 assert(retval == CURLE_OK);
1137
1/2
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
29472 retval = curl_easy_setopt(handle, CURLOPT_READDATA,
1138 static_cast<void *>(info));
1139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29472 times.
29472 assert(retval == CURLE_OK);
1140
1/2
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
29472 retval = curl_easy_setopt(handle, CURLOPT_WRITEDATA,
1141 static_cast<void *>(info));
1142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29472 times.
29472 assert(retval == CURLE_OK);
1143
1/2
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
29472 retval = curl_easy_setopt(handle, CURLOPT_HTTPHEADER, info->http_headers);
1144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29472 times.
29472 assert(retval == CURLE_OK);
1145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29472 times.
29472 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 29472 times.
✗ Branch 2 not taken.
29472 retval = curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1L);
1151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29472 times.
29472 assert(retval == CURLE_OK);
1152
1153
1/2
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
29472 retval = curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, info->errorbuffer);
1154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29472 times.
29472 assert(retval == CURLE_OK);
1155
1156
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 29472 times.
29472 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 29472 return kFailOk;
1167 29472 }
1168
1169
1170 /**
1171 * Sets the URL specific options such as host to use and timeout.
1172 */
1173 29472 void S3FanoutManager::SetUrlOptions(JobInfo *info) const {
1174 29472 CURL *curl_handle = info->curl_handle;
1175 CURLcode retval;
1176
1177
1/2
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
29472 retval = curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT,
1178 config_.opt_timeout_sec);
1179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29472 times.
29472 assert(retval == CURLE_OK);
1180
1/2
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
29472 retval = curl_easy_setopt(curl_handle, CURLOPT_LOW_SPEED_LIMIT,
1181 kLowSpeedLimit);
1182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29472 times.
29472 assert(retval == CURLE_OK);
1183
1/2
✓ Branch 1 taken 29472 times.
✗ Branch 2 not taken.
29472 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 29472 times.
29472 assert(retval == CURLE_OK);
1186
1187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29472 times.
29472 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 29472 times.
✗ Branch 2 not taken.
29472 string url = MkUrl(info->object_key);
1193
2/2
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 29304 times.
29472 if (info->request == JobInfo::kReqDeleteMulti)
1194
1/2
✓ Branch 1 taken 168 times.
✗ Branch 2 not taken.
168 url += "?delete";
1195
1/2
✓ Branch 2 taken 29472 times.
✗ Branch 3 not taken.
29472 retval = curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());
1196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29472 times.
29472 assert(retval == CURLE_OK);
1197
1198
1/2
✓ Branch 2 taken 29472 times.
✗ Branch 3 not taken.
29472 retval = curl_easy_setopt(curl_handle, CURLOPT_PROXY, config_.proxy.c_str());
1199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29472 times.
29472 assert(retval == CURLE_OK);
1200 29472 }
1201
1202
1203 /**
1204 * Adds transfer time and uploaded bytes to the global counters.
1205 */
1206 29568 void S3FanoutManager::UpdateStatistics(CURL *handle) {
1207 curl_off_t val;
1208
1209
2/4
✓ Branch 1 taken 29568 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 29568 times.
✗ Branch 4 not taken.
29568 if (curl_easy_getinfo(handle, CURLINFO_SIZE_UPLOAD_T, &val) == CURLE_OK)
1210 29568 statistics_->transferred_bytes += val;
1211 29568 }
1212
1213
1214 /**
1215 * Retry if possible and if not already done too often.
1216 */
1217 144 bool S3FanoutManager::CanRetry(const JobInfo *info) {
1218 144 return (info->error_code == kFailHostConnection
1219
1/2
✓ Branch 0 taken 144 times.
✗ Branch 1 not taken.
144 || info->error_code == kFailHostResolve
1220
1/2
✓ Branch 0 taken 144 times.
✗ Branch 1 not taken.
144 || info->error_code == kFailServiceUnavailable
1221
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 48 times.
144 || info->error_code == kFailRetry)
1222
2/4
✓ Branch 0 taken 144 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 96 times.
✗ Branch 3 not taken.
288 && (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 96 void S3FanoutManager::Backoff(JobInfo *info) {
1232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
96 if (info->error_code != kFailRetry)
1233 info->num_retries++;
1234 96 statistics_->num_retries++;
1235
1236
1/2
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
96 if (info->throttle_ms > 0) {
1237 96 LogCvmfs(kLogS3Fanout, kLogDebug, "throttling for %d ms",
1238 info->throttle_ms);
1239 96 const uint64_t now = platform_monotonic_time();
1240
1/2
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
96 if ((info->throttle_timestamp + (info->throttle_ms / 1000)) >= now) {
1241
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 72 times.
96 if ((now - timestamp_last_throttle_report_)
1242 > kThrottleReportIntervalSec) {
1243 24 LogCvmfs(kLogS3Fanout, kLogStdout,
1244 "Warning: S3 backend throttling %ums "
1245 "(total backoff time so far %lums)",
1246 24 info->throttle_ms, statistics_->ms_throttled);
1247 24 timestamp_last_throttle_report_ = now;
1248 }
1249 96 statistics_->ms_throttled += info->throttle_ms;
1250 96 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 96 }
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 29568 bool S3FanoutManager::VerifyAndFinalize(const int curl_error, JobInfo *info) {
1276 29568 LogCvmfs(kLogS3Fanout, kLogDebug,
1277 "Verify uploaded/tested object %s "
1278 "(curl error %d, info error %d, info request %d)",
1279 29568 info->object_key.c_str(), curl_error, info->error_code,
1280 29568 info->request);
1281 29568 UpdateStatistics(info->curl_handle);
1282
1283 // Verification and error classification
1284
1/6
✓ Branch 0 taken 29568 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
29568 switch (curl_error) {
1285 29568 case CURLE_OK:
1286
2/2
✓ Branch 0 taken 29472 times.
✓ Branch 1 taken 96 times.
29568 if ((info->error_code != kFailRetry)
1287
2/2
✓ Branch 0 taken 14832 times.
✓ Branch 1 taken 14640 times.
29472 && (info->error_code != kFailNotFound)) {
1288 14832 info->error_code = kFailOk;
1289 }
1290 29568 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 info->error_code = kFailHostConnection;
1303 break;
1304 case CURLE_ABORTED_BY_CALLBACK:
1305 case CURLE_WRITE_ERROR:
1306 // Error set by callback
1307 break;
1308 default:
1309 LogCvmfs(kLogS3Fanout, kLogStderr | kLogSyslogErr,
1310 "unexpected curl error (%d) while trying to upload %s: %s",
1311 curl_error, info->object_key.c_str(), info->errorbuffer);
1312 info->error_code = kFailOther;
1313 break;
1314 }
1315
1316 // Transform HEAD to PUT request
1317
2/2
✓ Branch 0 taken 14640 times.
✓ Branch 1 taken 14928 times.
29568 if ((info->error_code == kFailNotFound)
1318
2/2
✓ Branch 0 taken 14592 times.
✓ Branch 1 taken 48 times.
14640 && (info->request == JobInfo::kReqHeadPut)) {
1319 14592 LogCvmfs(kLogS3Fanout, kLogDebug, "not found: %s, uploading",
1320 info->object_key.c_str());
1321 14592 info->request = JobInfo::kReqPutCas;
1322 14592 curl_slist_free_all(info->http_headers);
1323 14592 info->http_headers = NULL;
1324 14592 const s3fanout::Failures init_failure = InitializeRequest(
1325 info, info->curl_handle);
1326
1327
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14592 times.
14592 if (init_failure != s3fanout::kFailOk) {
1328 PANIC(kLogStderr,
1329 "Failed to initialize CURL handle "
1330 "(error: %d - %s | errno: %d)",
1331 init_failure, Code2Ascii(init_failure), errno);
1332 }
1333 14592 SetUrlOptions(info);
1334 // Reset origin
1335 14592 info->origin->Rewind();
1336 14592 return true; // Again, Put
1337 }
1338
1339 // Determination if failed request should be repeated
1340 14976 bool try_again = false;
1341
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 14832 times.
14976 if (info->error_code != kFailOk) {
1342 144 try_again = CanRetry(info);
1343 }
1344
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 14880 times.
14976 if (try_again) {
1345
1/2
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
96 if (info->request == JobInfo::kReqPutCas
1346
1/2
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
96 || info->request == JobInfo::kReqPutDotCvmfs
1347
1/2
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
96 || info->request == JobInfo::kReqPutHtml
1348
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
96 || info->request == JobInfo::kReqDeleteMulti) {
1349 LogCvmfs(kLogS3Fanout, kLogDebug, "Trying again to upload %s",
1350 info->object_key.c_str());
1351 // Reset origin
1352 info->origin->Rewind();
1353 if (info->request == JobInfo::kReqDeleteMulti)
1354 info->response_body.clear();
1355 }
1356 96 Backoff(info);
1357 96 info->error_code = kFailOk;
1358 96 info->http_error = 0;
1359 96 info->throttle_ms = 0;
1360 96 info->backoff_ms = 0;
1361 96 info->throttle_timestamp = 0;
1362 96 return true; // try again
1363 }
1364
1365 // Cleanup opened resources
1366 14880 info->origin.reset();
1367
1368
3/4
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 14832 times.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
14880 if ((info->error_code != kFailOk) && (info->http_error != 0)
1369
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 && (info->http_error != 404)) {
1370 LogCvmfs(kLogS3Fanout, kLogStderr, "S3: HTTP failure %d", info->http_error);
1371 }
1372 14880 return false; // stop transfer
1373 }
1374
1375
2/4
✓ Branch 3 taken 312 times.
✗ Branch 4 not taken.
✓ Branch 9 taken 312 times.
✗ Branch 10 not taken.
312 S3FanoutManager::S3FanoutManager(const S3Config &config) : config_(config) {
1376 312 atomic_init32(&multi_threaded_);
1377
1/2
✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
312 MakePipe(pipe_terminate_);
1378
1/2
✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
312 MakePipe(pipe_jobs_);
1379
1/2
✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
312 MakePipe(pipe_completed_);
1380
1381 int retval;
1382 312 jobs_todo_lock_ = reinterpret_cast<pthread_mutex_t *>(
1383 312 smalloc(sizeof(pthread_mutex_t)));
1384 312 retval = pthread_mutex_init(jobs_todo_lock_, NULL);
1385
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 312 times.
312 assert(retval == 0);
1386 312 curl_handle_lock_ = reinterpret_cast<pthread_mutex_t *>(
1387 312 smalloc(sizeof(pthread_mutex_t)));
1388 312 retval = pthread_mutex_init(curl_handle_lock_, NULL);
1389
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 312 times.
312 assert(retval == 0);
1390
1391
1/2
✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
312 active_requests_ = new set<JobInfo *>;
1392
1/2
✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
312 pool_handles_idle_ = new set<CURL *>;
1393
1/2
✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
312 pool_handles_inuse_ = new set<CURL *>;
1394
1/2
✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
312 curl_sharehandles_ = new map<CURL *, S3FanOutDnsEntry *>;
1395
1/2
✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
312 sharehandles_ = new set<S3FanOutDnsEntry *>;
1396 312 watch_fds_max_ = 4 * config_.pool_max_handles;
1397 312 max_available_jobs_ = 4 * config_.pool_max_handles;
1398
2/4
✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 312 times.
✗ Branch 5 not taken.
312 available_jobs_ = new Semaphore(max_available_jobs_);
1399
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 312 times.
312 assert(NULL != available_jobs_);
1400
1401
1/2
✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
312 statistics_ = new Statistics();
1402
1/2
✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
312 user_agent_ = new string();
1403
2/4
✓ Branch 2 taken 312 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 312 times.
✗ Branch 6 not taken.
312 *user_agent_ = "User-Agent: cvmfs " + string(CVMFS_VERSION);
1404
1/2
✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
312 complete_hostname_ = MkCompleteHostname();
1405
1/2
✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
312 dot_cvmfs_cache_control_header = MkDotCvmfsCacheControlHeader();
1406
1407
1/2
✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
312 const CURLcode cretval = curl_global_init(CURL_GLOBAL_ALL);
1408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 312 times.
312 assert(cretval == CURLE_OK);
1409
1/2
✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
312 curl_multi_ = curl_multi_init();
1410
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 312 times.
312 assert(curl_multi_ != NULL);
1411 CURLMcode mretval;
1412
1/2
✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
312 mretval = curl_multi_setopt(curl_multi_, CURLMOPT_SOCKETFUNCTION,
1413 CallbackCurlSocket);
1414
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 312 times.
312 assert(mretval == CURLM_OK);
1415
1/2
✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
312 mretval = curl_multi_setopt(curl_multi_, CURLMOPT_SOCKETDATA,
1416 static_cast<void *>(this));
1417
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 312 times.
312 assert(mretval == CURLM_OK);
1418
1/2
✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
312 mretval = curl_multi_setopt(curl_multi_, CURLMOPT_MAX_TOTAL_CONNECTIONS,
1419 config_.pool_max_handles);
1420
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 312 times.
312 assert(mretval == CURLM_OK);
1421
1422 312 prng_.InitLocaltime();
1423
1424 312 thread_upload_ = 0;
1425 312 timestamp_last_throttle_report_ = 0;
1426 312 is_curl_debug_ = (getenv("_CVMFS_CURL_DEBUG") != NULL);
1427
1428 // Parsing environment variables
1429 312 if ((getenv("CVMFS_IPV4_ONLY") != NULL)
1430
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 312 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 312 times.
312 && (strlen(getenv("CVMFS_IPV4_ONLY")) > 0)) {
1431 opt_ipv4_only_ = true;
1432 } else {
1433 312 opt_ipv4_only_ = false;
1434 }
1435
1436
1/2
✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
312 resolver_ = dns::CaresResolver::Create(opt_ipv4_only_, 2, 2000);
1437
1438 312 watch_fds_ = static_cast<struct pollfd *>(smalloc(4 * sizeof(struct pollfd)));
1439 312 watch_fds_size_ = 4;
1440 312 watch_fds_inuse_ = 0;
1441
1442
1/2
✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
312 ssl_certificate_store_.UseSystemCertificatePath();
1443 312 }
1444
1445 624 S3FanoutManager::~S3FanoutManager() {
1446 312 pthread_mutex_destroy(jobs_todo_lock_);
1447 312 free(jobs_todo_lock_);
1448 312 pthread_mutex_destroy(curl_handle_lock_);
1449 312 free(curl_handle_lock_);
1450
1451
1/2
✓ Branch 1 taken 312 times.
✗ Branch 2 not taken.
312 if (atomic_xadd32(&multi_threaded_, 0) == 1) {
1452 // Shutdown I/O thread
1453 312 char buf = 'T';
1454 312 WritePipe(pipe_terminate_[1], &buf, 1);
1455 312 pthread_join(thread_upload_, NULL);
1456 }
1457 312 ClosePipe(pipe_terminate_);
1458 312 ClosePipe(pipe_jobs_);
1459 312 ClosePipe(pipe_completed_);
1460
1461 312 set<CURL *>::iterator i = pool_handles_idle_->begin();
1462 312 const set<CURL *>::const_iterator iEnd = pool_handles_idle_->end();
1463
2/2
✓ Branch 2 taken 624 times.
✓ Branch 3 taken 312 times.
936 for (; i != iEnd; ++i) {
1464 624 curl_easy_cleanup(*i);
1465 }
1466
1467 312 set<S3FanOutDnsEntry *>::iterator is = sharehandles_->begin();
1468 312 const set<S3FanOutDnsEntry *>::const_iterator isEnd = sharehandles_->end();
1469
2/2
✓ Branch 2 taken 264 times.
✓ Branch 3 taken 312 times.
576 for (; is != isEnd; ++is) {
1470 264 curl_share_cleanup((*is)->sharehandle);
1471 264 curl_slist_free_all((*is)->clist);
1472
1/2
✓ Branch 1 taken 264 times.
✗ Branch 2 not taken.
264 delete *is;
1473 }
1474 312 pool_handles_idle_->clear();
1475 312 curl_sharehandles_->clear();
1476 312 sharehandles_->clear();
1477
1/2
✓ Branch 0 taken 312 times.
✗ Branch 1 not taken.
312 delete active_requests_;
1478
1/2
✓ Branch 0 taken 312 times.
✗ Branch 1 not taken.
312 delete pool_handles_idle_;
1479
1/2
✓ Branch 0 taken 312 times.
✗ Branch 1 not taken.
312 delete pool_handles_inuse_;
1480
1/2
✓ Branch 0 taken 312 times.
✗ Branch 1 not taken.
312 delete curl_sharehandles_;
1481
1/2
✓ Branch 0 taken 312 times.
✗ Branch 1 not taken.
312 delete sharehandles_;
1482
1/2
✓ Branch 0 taken 312 times.
✗ Branch 1 not taken.
312 delete user_agent_;
1483 312 curl_multi_cleanup(curl_multi_);
1484
1485
1/2
✓ Branch 0 taken 312 times.
✗ Branch 1 not taken.
312 delete statistics_;
1486
1487
1/2
✓ Branch 0 taken 312 times.
✗ Branch 1 not taken.
312 delete available_jobs_;
1488
1489 312 curl_global_cleanup();
1490 312 }
1491
1492 /**
1493 * Spawns the I/O worker thread. No way back except ~S3FanoutManager.
1494 */
1495 312 void S3FanoutManager::Spawn() {
1496 312 LogCvmfs(kLogS3Fanout, kLogDebug, "S3FanoutManager spawned");
1497
1498 312 const int retval = pthread_create(&thread_upload_, NULL, MainUpload,
1499 static_cast<void *>(this));
1500
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 312 times.
312 assert(retval == 0);
1501
1502 312 atomic_inc32(&multi_threaded_);
1503 312 }
1504
1505 72 const Statistics &S3FanoutManager::GetStatistics() { return *statistics_; }
1506
1507 /**
1508 * Push new job to be uploaded to the S3 cloud storage.
1509 */
1510 14880 void S3FanoutManager::PushNewJob(JobInfo *info) {
1511 14880 available_jobs_->Increment();
1512 14880 WritePipe(pipe_jobs_[1], &info, sizeof(info));
1513 14880 }
1514
1515 /**
1516 * Push completed job to list of completed jobs
1517 */
1518 15192 void S3FanoutManager::PushCompletedJob(JobInfo *info) {
1519 15192 WritePipe(pipe_completed_[1], &info, sizeof(info));
1520 15192 }
1521
1522 /**
1523 * Pop completed job
1524 */
1525 15192 JobInfo *S3FanoutManager::PopCompletedJob() {
1526 JobInfo *info;
1527
1/2
✓ Branch 1 taken 15192 times.
✗ Branch 2 not taken.
15192 ReadPipe(pipe_completed_[0], &info, sizeof(info));
1528 15192 return info;
1529 }
1530
1531 //------------------------------------------------------------------------------
1532
1533
1534 string Statistics::Print() const {
1535 return "Transferred Bytes: " + StringifyInt(uint64_t(transferred_bytes))
1536 + "\n" + "Transfer duration: " + StringifyInt(uint64_t(transfer_time))
1537 + " s\n" + "Number of requests: " + StringifyInt(num_requests) + "\n"
1538 + "Number of retries: " + StringifyInt(num_retries) + "\n";
1539 }
1540
1541 } // namespace s3fanout
1542