XRootD
XrdHttpTpcMultistream.cc
Go to the documentation of this file.
1 
5 #include "XrdHttpTpcTPC.hh"
6 #include "XrdHttpTpcState.hh"
7 
8 #include "XrdSys/XrdSysError.hh"
9 
10 #include <curl/curl.h>
11 
12 #include <algorithm>
13 #include <sstream>
14 #include <stdexcept>
15 
16 
17 using namespace TPC;
18 
19 class CurlHandlerSetupError : public std::runtime_error {
20 public:
21  CurlHandlerSetupError(const std::string &msg) :
22  std::runtime_error(msg)
23  {}
24 
25  virtual ~CurlHandlerSetupError() noexcept {}
26 };
27 
28 namespace {
29 class MultiCurlHandler {
30 public:
31  MultiCurlHandler(std::vector<State*> &states, XrdSysError &log) :
32  m_handle(curl_multi_init()),
33  m_states(states),
34  m_log(log),
35  m_bytes_transferred(0),
36  m_error_code(0),
37  m_status_code(0)
38  {
39  if (m_handle == NULL) {
40  throw CurlHandlerSetupError("Failed to initialize a libcurl multi-handle");
41  }
42  m_avail_handles.reserve(states.size());
43  m_active_handles.reserve(states.size());
44  for (std::vector<State*>::const_iterator state_iter = states.begin();
45  state_iter != states.end();
46  state_iter++) {
47  m_avail_handles.push_back((*state_iter)->GetHandle());
48  }
49  }
50 
51  ~MultiCurlHandler()
52  {
53  if (!m_handle) {return;}
54  for (std::vector<CURL *>::const_iterator it = m_active_handles.begin();
55  it != m_active_handles.end();
56  it++) {
57  curl_multi_remove_handle(m_handle, *it);
58  }
59  curl_multi_cleanup(m_handle);
60  }
61 
62  MultiCurlHandler(const MultiCurlHandler &) = delete;
63 
64  CURLM *Get() const {return m_handle;}
65 
66  void FinishCurlXfer(CURL *curl) {
67  CURLMcode mres = curl_multi_remove_handle(m_handle, curl);
68  if (mres) {
69  std::stringstream ss;
70  ss << "Failed to remove transfer from set: "
71  << curl_multi_strerror(mres);
72  throw std::runtime_error(ss.str());
73  }
74  for (std::vector<State*>::iterator state_iter = m_states.begin();
75  state_iter != m_states.end();
76  state_iter++) {
77  if (curl == (*state_iter)->GetHandle()) {
78  m_bytes_transferred += (*state_iter)->BytesTransferred();
79  int error_code = (*state_iter)->GetErrorCode();
80  if (error_code && !m_error_code) {
81  m_error_code = error_code;
82  m_error_message = (*state_iter)->GetErrorMessage();
83  }
84  int status_code = (*state_iter)->GetStatusCode();
85  if (status_code >= 400 && !m_status_code) {
86  m_status_code = status_code;
87  m_error_message = (*state_iter)->GetErrorMessage();
88  }
89  (*state_iter)->ResetAfterRequest();
90  break;
91  }
92  }
93  for (std::vector<CURL *>::iterator iter = m_active_handles.begin();
94  iter != m_active_handles.end();
95  ++iter)
96  {
97  if (*iter == curl) {
98  m_active_handles.erase(iter);
99  break;
100  }
101  }
102  m_avail_handles.push_back(curl);
103  }
104 
105  off_t StartTransfers(off_t current_offset, off_t content_length, size_t block_size,
106  int &running_handles) {
107  bool started_new_xfer = false;
108  do {
109  size_t xfer_size = std::min(content_length - current_offset, static_cast<off_t>(block_size));
110  if (xfer_size == 0) {return current_offset;}
111  if (!(started_new_xfer = StartTransfer(current_offset, xfer_size))) {
112  // In this case, we need to start new transfers but weren't able to.
113  if (running_handles == 0) {
114  if (!CanStartTransfer(true)) {
115  m_log.Emsg("StartTransfers", "Unable to start transfers.");
116  }
117  }
118  break;
119  } else {
120  running_handles += 1;
121  }
122  current_offset += xfer_size;
123  } while (true);
124  return current_offset;
125  }
126 
127  // Flush the file to the local filesystem. All the states share the same
128  // underlying stream, hence a single flush is enough -- and required: a
129  // second one would be a no-op at best. Returns State::errNone and leaves
130  // error_msg untouched on success; otherwise returns State::errFlush and sets
131  // error_msg to the corresponding error message.
132  int Flush(std::string &error_msg) {
133  if (m_states.empty() || (m_states[0]->Flush() != -1)) {
134  return State::errNone;
135  }
136  error_msg = m_states[0]->GetFinalizeErrorMessage();
137  if (error_msg.empty()) {error_msg = "(no error message provided)";}
138  return State::errFlush;
139  }
140 
141  off_t BytesTransferred() const {
142  return m_bytes_transferred;
143  }
144 
145  // Number of bytes that have actually been transferred so far: the bytes of
146  // the requests that already completed plus the bytes of the requests that
147  // are still in flight. The two are disjoint: FinishCurlXfer() accumulates
148  // the counter of a state into m_bytes_transferred and then zeroes it via
149  // State::ResetAfterRequest(), so no byte is counted twice.
150  // Note this is not the same quantity as the scheduling offset maintained by
151  // StartTransfers(), which is advanced as soon as a range request is handed
152  // over to libcurl, hence before any byte of that range has been received.
153  off_t BytesInFlightAndTransferred() const {
154  off_t bytes = m_bytes_transferred;
155  for (std::vector<State*>::const_iterator state_iter = m_states.begin();
156  state_iter != m_states.end();
157  state_iter++) {
158  bytes += (*state_iter)->BytesTransferred();
159  }
160  return bytes;
161  }
162 
163  int GetStatusCode() const {
164  return m_status_code;
165  }
166 
167  int GetErrorCode() const {
168  return m_error_code;
169  }
170 
171  void SetErrorCode(int error_code) {
172  m_error_code = error_code;
173  }
174 
175  std::string GetErrorMessage() const {
176  return m_error_message;
177  }
178 
179  void SetErrorMessage(const std::string &error_msg) {
180  m_error_message = error_msg;
181  }
182 
183 private:
184 
185  bool StartTransfer(off_t offset, size_t size) {
186  if (!CanStartTransfer(false)) {return false;}
187  for (std::vector<CURL*>::const_iterator handle_it = m_avail_handles.begin();
188  handle_it != m_avail_handles.end();
189  handle_it++) {
190  for (std::vector<State*>::iterator state_it = m_states.begin();
191  state_it != m_states.end();
192  state_it++) {
193  if ((*state_it)->GetHandle() == *handle_it) { // This state object represents an idle handle.
194  (*state_it)->SetTransferParameters(offset, size);
195  ActivateHandle(**state_it);
196  return true;
197  }
198  }
199  }
200  return false;
201  }
202 
203  void ActivateHandle(State &state) {
204  CURL *curl = state.GetHandle();
205  m_active_handles.push_back(curl);
206  CURLMcode mres;
207  mres = curl_multi_add_handle(m_handle, curl);
208  if (mres) {
209  std::stringstream ss;
210  ss << "Failed to add transfer to libcurl multi-handle"
211  << curl_multi_strerror(mres);
212  throw std::runtime_error(ss.str());
213  }
214  for (auto iter = m_avail_handles.begin();
215  iter != m_avail_handles.end();
216  ++iter)
217  {
218  if (*iter == curl) {
219  m_avail_handles.erase(iter);
220  break;
221  }
222  }
223  }
224 
225  bool CanStartTransfer(bool log_reason) const {
226  size_t idle_handles = m_avail_handles.size();
227  size_t transfer_in_progress = 0;
228  for (std::vector<State*>::const_iterator state_iter = m_states.begin();
229  state_iter != m_states.end();
230  state_iter++) {
231  for (std::vector<CURL*>::const_iterator handle_iter = m_active_handles.begin();
232  handle_iter != m_active_handles.end();
233  handle_iter++) {
234  if (*handle_iter == (*state_iter)->GetHandle()) {
235  transfer_in_progress += (*state_iter)->BodyTransferInProgress();
236  break;
237  }
238  }
239  }
240  if (!idle_handles) {
241  if (log_reason) {
242  m_log.Emsg("CanStartTransfer", "Unable to start transfers as no idle CURL handles are available.");
243  }
244  return false;
245  }
246  ssize_t available_buffers = m_states[0]->AvailableBuffers();
247  // To be conservative, set aside buffers for any transfers that have been activated
248  // but don't have their first responses back yet.
249  available_buffers -= (m_active_handles.size() - transfer_in_progress);
250  if (log_reason && (available_buffers == 0)) {
251  std::stringstream ss;
252  ss << "Unable to start transfers as no buffers are available. Available buffers: " <<
253  m_states[0]->AvailableBuffers() << ", Active curl handles: " << m_active_handles.size()
254  << ", Transfers in progress: " << transfer_in_progress;
255  m_log.Emsg("CanStartTransfer", ss.str().c_str());
256  if (m_states[0]->AvailableBuffers() == 0) {
257  m_states[0]->DumpBuffers();
258  }
259  }
260  return available_buffers > 0;
261  }
262 
263  CURLM *m_handle;
264  std::vector<CURL *> m_avail_handles;
265  std::vector<CURL *> m_active_handles;
266  std::vector<State*> &m_states;
267  XrdSysError &m_log;
268  off_t m_bytes_transferred;
269  int m_error_code;
270  int m_status_code;
271  std::string m_error_message;
272 };
273 }
274 
275 
276 int TPCHandler::RunCurlWithStreamsImpl(XrdHttpExtReq &req, State &state,
277  size_t streams, std::vector<State*> &handles,
278  std::vector<ManagedCurlHandle> &curl_handles, TPCLogRecord &rec)
279 {
280  bool success;
281  // The content-length was set thanks to the call to GetRemoteFileInfoTPCPull() before calling this function
282  off_t content_size = state.GetContentLength();
283  off_t current_offset = 0;
284 
285  size_t concurrency = streams * m_pipelining_multiplier;
286 
287  handles.reserve(concurrency);
288  handles.push_back(new State());
289  handles[0]->Move(state);
290  for (size_t idx = 1; idx < concurrency; idx++) {
291  handles.push_back(handles[0]->Duplicate());
292  curl_handles.emplace_back(handles.back()->GetHandle());
293  }
294 
295  // Notify the packet marking manager that the transfer will start after this point
296  rec.pmarkManager.startTransfer();
297 
298  // Create the multi-handle and add in the current transfer to it.
299  MultiCurlHandler mch(handles, m_log);
300  CURLM *multi_handle = mch.Get();
301 
302  curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, 1);
303  curl_multi_setopt(multi_handle, CURLMOPT_MAX_HOST_CONNECTIONS, streams);
304 
305  // Start response to client prior to the first call to curl_multi_perform
306  int retval = req.StartChunkedResp(202, NULL, "Content-Type: text/plain");
307  if (retval) {
308  logTransferEvent(LogMask::Error, rec, "RESPONSE_FAIL",
309  "Failed to send the initial response to the TPC client");
310  return retval;
311  } else {
312  logTransferEvent(LogMask::Debug, rec, "RESPONSE_START",
313  "Initial transfer response sent to the TPC client");
314  }
315 
316  // Start assigning transfers
317  int running_handles = 0;
318  current_offset = mch.StartTransfers(current_offset, content_size, m_block_size, running_handles);
319 
320  // Transfer loop: use curl to actually run the transfer, but periodically
321  // interrupt things to send back performance updates to the client.
322  time_t last_marker = 0;
323  // Track the time since the transfer last made progress
324  off_t last_advance_bytes = 0;
325  time_t last_advance_time = time(NULL);
326  time_t transfer_start = last_advance_time;
327  CURLcode res = static_cast<CURLcode>(-1);
328  CURLMcode mres = CURLM_OK;
329  do {
330  time_t now = time(NULL);
331  time_t next_marker = last_marker + m_marker_period;
332  if (now >= next_marker) {
333  // Report - and watch for progress on - the bytes that have really
334  // been transferred, not the offset up to which the range requests
335  // have been scheduled: the latter runs ahead of the transfer by up
336  // to concurrency * m_block_size bytes.
337  const off_t bytes_transferred = mch.BytesInFlightAndTransferred();
338  if (bytes_transferred > last_advance_bytes) {
339  last_advance_bytes = bytes_transferred;
340  last_advance_time = now;
341  }
342  if (SendPerfMarker(req, rec, handles, bytes_transferred)) {
343  logTransferEvent(LogMask::Error, rec, "PERFMARKER_FAIL",
344  "Failed to send a perf marker to the TPC client");
345  return -1;
346  }
347  int timeout = (transfer_start == last_advance_time) ? m_first_timeout : m_timeout;
348  if (now > last_advance_time + timeout) {
349  const char *log_prefix = rec.log_prefix.c_str();
350  bool tpc_pull = strncmp("Pull", log_prefix, 4) == 0;
351 
352  mch.SetErrorCode(State::errTimeout);
353  std::stringstream ss;
354  ss << "Transfer failed because no bytes have been "
355  << (tpc_pull ? "received from the source (pull mode) in "
356  : "transmitted to the destination (push mode) in ") << timeout << " seconds.";
357  mch.SetErrorMessage(ss.str());
358  break;
359  }
360  last_marker = now;
361  }
362 
363  mres = curl_multi_perform(multi_handle, &running_handles);
364  if (mres == CURLM_CALL_MULTI_PERFORM) {
365  // curl_multi_perform should be called again immediately. On newer
366  // versions of curl, this is no longer used.
367  continue;
368  } else if (mres != CURLM_OK) {
369  break;
370  }
371 
372  rec.pmarkManager.beginPMarks();
373 
374 
375  // Harvest any messages, looking for CURLMSG_DONE.
376  CURLMsg *msg;
377  do {
378  int msgq = 0;
379  msg = curl_multi_info_read(multi_handle, &msgq);
380  if (msg && (msg->msg == CURLMSG_DONE)) {
381  CURL *easy_handle = msg->easy_handle;
382  res = msg->data.result;
383  mch.FinishCurlXfer(easy_handle);
384  // If any requests fail, cut off the entire transfer.
385  if (res != CURLE_OK) {
386  break;
387  }
388  }
389  } while (msg);
390  if (res != static_cast<CURLcode>(-1) && res != CURLE_OK) {
391  std::stringstream ss;
392  ss << "Breaking loop due to failed curl transfer: " << curl_easy_strerror(res);
393  logTransferEvent(LogMask::Debug, rec, "MULTISTREAM_CURL_FAILURE",
394  ss.str());
395  break;
396  }
397 
398  if (running_handles < static_cast<int>(concurrency)) {
399  // Issue new transfers if there is still pending work to do.
400  // Otherwise, continue running until there are no handles left.
401  if (current_offset != content_size) {
402  current_offset = mch.StartTransfers(current_offset, content_size,
403  m_block_size, running_handles);
404  if (!running_handles) {
405  std::stringstream ss;
406  ss << "No handles are able to run. Streams=" << streams << ", concurrency="
407  << concurrency;
408 
409  logTransferEvent(LogMask::Debug, rec, "MULTISTREAM_IDLE", ss.str());
410  }
411  } else if (running_handles == 0) {
412  logTransferEvent(LogMask::Debug, rec, "MULTISTREAM_IDLE",
413  "All the ranges have been scheduled and all the handles are done; ending the transfer loop.");
414  break;
415  }
416  }
417 
418  int64_t max_sleep_time = next_marker - time(NULL);
419  if (max_sleep_time <= 0) {
420  continue;
421  }
422  int fd_count;
423  mres = curl_multi_wait(multi_handle, NULL, 0, max_sleep_time*1000,
424  &fd_count);
425  if (mres != CURLM_OK) {
426  break;
427  }
428  } while (running_handles);
429 
430  if (mres != CURLM_OK) {
431  std::stringstream ss;
432  ss << "Internal libcurl multi-handle error: "
433  << curl_multi_strerror(mres);
434  logTransferEvent(LogMask::Error, rec, "MULTISTREAM_ERROR", ss.str());
435  throw std::runtime_error(ss.str());
436  }
437 
438  // Harvest any messages, looking for CURLMSG_DONE.
439  CURLMsg *msg;
440  do {
441  int msgq = 0;
442  msg = curl_multi_info_read(multi_handle, &msgq);
443  if (msg && (msg->msg == CURLMSG_DONE)) {
444  CURL *easy_handle = msg->easy_handle;
445  mch.FinishCurlXfer(easy_handle);
446  if (res == CURLE_OK || res == static_cast<CURLcode>(-1))
447  res = msg->data.result; // Transfer result will be examined below.
448  }
449  } while (msg);
450 
451  if (!state.GetErrorCode() && res == static_cast<CURLcode>(-1)) { // No transfers returned?!?
452  logTransferEvent(LogMask::Error, rec, "MULTISTREAM_ERROR",
453  "Internal state error in libcurl");
454  throw std::runtime_error("Internal state error in libcurl");
455  }
456 
457  // A failure to flush the file to the local filesystem is always logged and is
458  // appended to the error reported to the client, but it never replaces the
459  // transfer failure itself: the flush failure is usually a consequence of it.
460  std::string flushErrorMsg;
461  const int flushErrorCode = mch.Flush(flushErrorMsg);
462  std::string flushErrorSuffix;
463  if (flushErrorCode) {
464  std::replace(flushErrorMsg.begin(), flushErrorMsg.end(), '\n', ' ');
465  flushErrorMsg = "Failed to flush the file to the local filesystem. " + flushErrorMsg;
466  logTransferEvent(LogMask::Error, rec, "FLUSH_FAIL", flushErrorMsg);
467  flushErrorSuffix = "; " + flushErrorMsg;
468  }
469 
470  rec.bytes_transferred = mch.BytesTransferred();
471  rec.tpc_status = mch.GetStatusCode();
472 
473  // Generate the final response back to the client.
474  std::stringstream ss;
475  success = false;
476  if (mch.GetStatusCode() >= 400) {
477  std::string err = mch.GetErrorMessage();
478  std::stringstream ss2;
479  ss2 << "Remote side failed with status code " << mch.GetStatusCode();
480  if (!err.empty()) {
481  std::replace(err.begin(), err.end(), '\n', ' ');
482  ss2 << "; error message: \"" << err << "\"";
483  }
484  logTransferEvent(LogMask::Error, rec, "MULTISTREAM_FAIL", ss2.str());
485  ss2 << flushErrorSuffix;
486  ss << generateClientErr(ss2, rec);
487  } else if (mch.GetErrorCode() == State::errTimeout) {
488  // The stall detector fired; its message already describes precisely
489  // what happened, report it as-is.
490  std::stringstream ss2;
491  ss2 << mch.GetErrorMessage();
492  logTransferEvent(LogMask::Error, rec, "MULTISTREAM_FAIL", ss2.str());
493  ss2 << flushErrorSuffix;
494  ss << generateClientErr(ss2, rec);
495  } else if (mch.GetErrorCode()) {
496  std::string err = mch.GetErrorMessage();
497  if (err.empty()) {err = "(no error message provided)";}
498  else {std::replace(err.begin(), err.end(), '\n', ' ');}
499  std::stringstream ss2;
500  ss2 << "Error when interacting with local filesystem: " << err;
501  logTransferEvent(LogMask::Error, rec, "MULTISTREAM_FAIL", ss2.str());
502  ss2 << flushErrorSuffix;
503  ss << generateClientErr(ss2, rec);
504  } else if (res != CURLE_OK) {
505  std::stringstream ss2;
506  ss2 << "Request failed when processing";
507  std::stringstream ss3;
508  ss3 << ss2.str() << ":" << curl_easy_strerror(res);
509  logTransferEvent(LogMask::Error, rec, "MULTISTREAM_FAIL", ss3.str());
510  ss2 << flushErrorSuffix;
511  ss << generateClientErr(ss2, rec, res);
512  } else if (current_offset != content_size) {
513  std::stringstream ss2;
514  ss2 << "Internal logic error led to early abort; current offset is " <<
515  current_offset << " while full size is " << content_size;
516  logTransferEvent(LogMask::Error, rec, "MULTISTREAM_FAIL", ss2.str());
517  ss2 << flushErrorSuffix;
518  ss << generateClientErr(ss2, rec);
519  } else if (flushErrorCode) {
520  // Nothing else went wrong: the flush failure is the reason of the failure.
521  std::stringstream ss2;
522  ss2 << flushErrorMsg;
523  ss << generateClientErr(ss2, rec);
524  } else {
525  if (!handles[0]->Finalize()) {
526  std::stringstream ss2;
527  ss2 << "Failed to finalize and close file handle.";
528  std::string handleErrMsg = handles[0]->GetFinalizeErrorMessage();
529  if(handleErrMsg.size()) {
530  std::replace(handleErrMsg.begin(), handleErrMsg.end(), '\n', ' ');
531  ss2 << " " << handleErrMsg;
532  }
533  ss << generateClientErr(ss2, rec);
534  logTransferEvent(LogMask::Error, rec, "MULTISTREAM_ERROR",
535  ss2.str());
536  } else {
537  ss << "success: Created";
538  success = true;
539  }
540  }
541 
542  if ((retval = req.ChunkResp(ss.str().c_str(), 0))) {
543  logTransferEvent(LogMask::Error, rec, "TRANSFER_ERROR",
544  "Failed to send last update to remote client");
545  return retval;
546  } else if (success) {
547  logTransferEvent(LogMask::Info, rec, "TRANSFER_SUCCESS");
548  rec.status = 0;
549  }
550  return req.ChunkResp(NULL, 0);
551 }
552 
553 
554 int TPCHandler::RunCurlWithStreams(XrdHttpExtReq &req, State &state,
555  size_t streams, TPCLogRecord &rec)
556 {
557  std::vector<ManagedCurlHandle> curl_handles;
558  std::vector<State*> handles;
559  std::stringstream err_ss;
560  try {
561  int retval = RunCurlWithStreamsImpl(req, state, streams, handles, curl_handles, rec);
562  for (std::vector<State*>::iterator state_iter = handles.begin();
563  state_iter != handles.end();
564  state_iter++) {
565  delete *state_iter;
566  }
567  return retval;
568  } catch (CurlHandlerSetupError &e) {
569  for (std::vector<State*>::iterator state_iter = handles.begin();
570  state_iter != handles.end();
571  state_iter++) {
572  delete *state_iter;
573  }
574 
575  rec.status = 500;
576  logTransferEvent(LogMask::Error, rec, "MULTISTREAM_ERROR", e.what());
577  std::stringstream ss;
578  ss << e.what();
579  err_ss << generateClientErr(ss, rec);
580  return req.SendSimpleResp(rec.status, NULL, NULL, e.what(), 0);
581  } catch (std::runtime_error &e) {
582  for (std::vector<State*>::iterator state_iter = handles.begin();
583  state_iter != handles.end();
584  state_iter++) {
585  delete *state_iter;
586  }
587 
588  logTransferEvent(LogMask::Error, rec, "MULTISTREAM_ERROR", e.what());
589  std::stringstream ss;
590  ss << e.what();
591  err_ss << generateClientErr(ss, rec);
592  int retval;
593  if ((retval = req.ChunkResp(err_ss.str().c_str(), 0))) {
594  return retval;
595  }
596  return req.ChunkResp(NULL, 0);
597  }
598 }
void CURL
#define Duplicate(x, y)
bool Debug
@ Error
CurlHandlerSetupError(const std::string &msg)
virtual ~CurlHandlerSetupError() noexcept
CURL * GetHandle() const
int GetErrorCode() const
off_t GetContentLength() const
int ChunkResp(const char *body, long long bodylen)
Send a (potentially partial) body in a chunked response; invoking with NULL body.
int StartChunkedResp(int code, const char *desc, const char *header_to_add)
Starts a chunked response; body of request is sent over multiple parts using the SendChunkResp.
int SendSimpleResp(int code, const char *desc, const char *header_to_add, const char *body, long long bodylen)
Sends a basic response. If the length is < 0 then it is calculated internally.