GCC Code Coverage Report


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