11 #include <curl/curl.h>
23 std::runtime_error(msg)
30 class MultiCurlHandler {
32 MultiCurlHandler(std::vector<State*> &states,
XrdSysError &log) :
33 m_handle(curl_multi_init()),
36 m_bytes_transferred(0),
40 if (m_handle == NULL) {
43 m_avail_handles.reserve(states.size());
44 m_active_handles.reserve(states.size());
45 for (std::vector<State*>::const_iterator state_iter = states.begin();
46 state_iter != states.end();
48 m_avail_handles.push_back((*state_iter)->GetHandle());
54 if (!m_handle) {
return;}
55 for (std::vector<CURL *>::const_iterator it = m_active_handles.begin();
56 it != m_active_handles.end();
58 curl_multi_remove_handle(m_handle, *it);
60 curl_multi_cleanup(m_handle);
63 MultiCurlHandler(
const MultiCurlHandler &) =
delete;
65 CURLM *Get()
const {
return m_handle;}
67 void FinishCurlXfer(
CURL *curl) {
68 CURLMcode mres = curl_multi_remove_handle(m_handle, curl);
71 ss <<
"Failed to remove transfer from set: "
72 << curl_multi_strerror(mres);
73 throw std::runtime_error(ss.str());
75 for (std::vector<State*>::iterator state_iter = m_states.begin();
76 state_iter != m_states.end();
78 if (curl == (*state_iter)->GetHandle()) {
79 m_bytes_transferred += (*state_iter)->BytesTransferred();
80 int error_code = (*state_iter)->GetErrorCode();
81 if (error_code && !m_error_code) {
82 m_error_code = error_code;
83 m_error_message = (*state_iter)->GetErrorMessage();
85 int status_code = (*state_iter)->GetStatusCode();
86 if (status_code >= 400 && !m_status_code) {
87 m_status_code = status_code;
88 m_error_message = (*state_iter)->GetErrorMessage();
90 (*state_iter)->ResetAfterRequest();
94 for (std::vector<CURL *>::iterator iter = m_active_handles.begin();
95 iter != m_active_handles.end();
99 m_active_handles.erase(iter);
103 m_avail_handles.push_back(curl);
106 off_t StartTransfers(off_t current_offset, off_t content_length,
size_t block_size,
107 int &running_handles) {
108 bool started_new_xfer =
false;
110 size_t xfer_size = std::min(content_length - current_offset,
static_cast<off_t
>(block_size));
111 if (xfer_size == 0) {
return current_offset;}
112 if (!(started_new_xfer = StartTransfer(current_offset, xfer_size))) {
114 if (running_handles == 0) {
115 if (!CanStartTransfer(
true)) {
116 m_log.Emsg(
"StartTransfers",
"Unable to start transfers.");
121 running_handles += 1;
123 current_offset += xfer_size;
125 return current_offset;
130 for (std::vector<State*>::iterator state_it = m_states.begin();
131 state_it != m_states.end();
134 int error = (*state_it)->Flush();
135 if (error) {last_error = error;}
140 off_t BytesTransferred()
const {
141 return m_bytes_transferred;
144 int GetStatusCode()
const {
145 return m_status_code;
148 int GetErrorCode()
const {
152 void SetErrorCode(
int error_code) {
153 m_error_code = error_code;
156 std::string GetErrorMessage()
const {
157 return m_error_message;
160 void SetErrorMessage(
const std::string &error_msg) {
161 m_error_message = error_msg;
166 bool StartTransfer(off_t offset,
size_t size) {
167 if (!CanStartTransfer(
false)) {
return false;}
168 for (std::vector<CURL*>::const_iterator handle_it = m_avail_handles.begin();
169 handle_it != m_avail_handles.end();
171 for (std::vector<State*>::iterator state_it = m_states.begin();
172 state_it != m_states.end();
174 if ((*state_it)->GetHandle() == *handle_it) {
175 (*state_it)->SetTransferParameters(offset, size);
176 ActivateHandle(**state_it);
184 void ActivateHandle(
State &state) {
186 m_active_handles.push_back(curl);
188 mres = curl_multi_add_handle(m_handle, curl);
190 std::stringstream ss;
191 ss <<
"Failed to add transfer to libcurl multi-handle"
192 << curl_multi_strerror(mres);
193 throw std::runtime_error(ss.str());
195 for (
auto iter = m_avail_handles.begin();
196 iter != m_avail_handles.end();
200 m_avail_handles.erase(iter);
206 bool CanStartTransfer(
bool log_reason)
const {
207 size_t idle_handles = m_avail_handles.size();
208 size_t transfer_in_progress = 0;
209 for (std::vector<State*>::const_iterator state_iter = m_states.begin();
210 state_iter != m_states.end();
212 for (std::vector<CURL*>::const_iterator handle_iter = m_active_handles.begin();
213 handle_iter != m_active_handles.end();
215 if (*handle_iter == (*state_iter)->GetHandle()) {
216 transfer_in_progress += (*state_iter)->BodyTransferInProgress();
223 m_log.Emsg(
"CanStartTransfer",
"Unable to start transfers as no idle CURL handles are available.");
227 ssize_t available_buffers = m_states[0]->AvailableBuffers();
230 available_buffers -= (m_active_handles.size() - transfer_in_progress);
231 if (log_reason && (available_buffers == 0)) {
232 std::stringstream ss;
233 ss <<
"Unable to start transfers as no buffers are available. Available buffers: " <<
234 m_states[0]->AvailableBuffers() <<
", Active curl handles: " << m_active_handles.size()
235 <<
", Transfers in progress: " << transfer_in_progress;
236 m_log.Emsg(
"CanStartTransfer", ss.str().c_str());
237 if (m_states[0]->AvailableBuffers() == 0) {
238 m_states[0]->DumpBuffers();
241 return available_buffers > 0;
245 std::vector<CURL *> m_avail_handles;
246 std::vector<CURL *> m_active_handles;
247 std::vector<State*> &m_states;
249 off_t m_bytes_transferred;
252 std::string m_error_message;
258 size_t streams, std::vector<State*> &handles,
259 std::vector<ManagedCurlHandle> &curl_handles, TPCLogRecord &rec)
264 off_t current_offset = 0;
266 size_t concurrency = streams * m_pipelining_multiplier;
268 handles.reserve(concurrency);
269 handles.push_back(
new State());
270 handles[0]->Move(state);
271 for (
size_t idx = 1; idx < concurrency; idx++) {
272 handles.push_back(handles[0]->
Duplicate());
273 curl_handles.emplace_back(handles.back()->GetHandle());
277 rec.pmarkManager.startTransfer();
280 MultiCurlHandler mch(handles, m_log);
281 CURLM *multi_handle = mch.Get();
283 #ifdef USE_PIPELINING
284 curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, 1);
285 curl_multi_setopt(multi_handle, CURLMOPT_MAX_HOST_CONNECTIONS, streams);
289 int retval = req.
StartChunkedResp(201,
"Created",
"Content-Type: text/plain");
292 "Failed to send the initial response to the TPC client");
296 "Initial transfer response sent to the TPC client");
300 int running_handles = 0;
301 current_offset = mch.StartTransfers(current_offset, content_size, m_block_size, running_handles);
305 time_t last_marker = 0;
307 off_t last_advance_bytes = 0;
308 time_t last_advance_time = time(NULL);
309 time_t transfer_start = last_advance_time;
310 CURLcode res =
static_cast<CURLcode
>(-1);
311 CURLMcode mres = CURLM_OK;
313 time_t now = time(NULL);
314 time_t next_marker = last_marker + m_marker_period;
315 if (now >= next_marker) {
316 if (current_offset > last_advance_bytes) {
317 last_advance_bytes = current_offset;
318 last_advance_time = now;
320 if (SendPerfMarker(req, rec, handles, current_offset)) {
322 "Failed to send a perf marker to the TPC client");
325 int timeout = (transfer_start == last_advance_time) ? m_first_timeout : m_timeout;
326 if (now > last_advance_time + timeout) {
327 const char *log_prefix = rec.log_prefix.c_str();
328 bool tpc_pull = strncmp(
"Pull", log_prefix, 4) == 0;
330 mch.SetErrorCode(10);
331 std::stringstream ss;
332 ss <<
"Transfer failed because no bytes have been "
333 << (tpc_pull ?
"received from the source (pull mode) in "
334 :
"transmitted to the destination (push mode) in ") << timeout <<
" seconds.";
335 mch.SetErrorMessage(ss.str());
341 mres = curl_multi_perform(multi_handle, &running_handles);
342 if (mres == CURLM_CALL_MULTI_PERFORM) {
346 }
else if (mres != CURLM_OK) {
350 rec.pmarkManager.beginPMarks();
357 msg = curl_multi_info_read(multi_handle, &msgq);
358 if (msg && (msg->msg == CURLMSG_DONE)) {
359 CURL *easy_handle = msg->easy_handle;
360 res = msg->data.result;
361 mch.FinishCurlXfer(easy_handle);
363 if (res != CURLE_OK) {
368 if (res !=
static_cast<CURLcode
>(-1) && res != CURLE_OK) {
369 std::stringstream ss;
370 ss <<
"Breaking loop due to failed curl transfer: " << curl_easy_strerror(res);
376 if (running_handles <
static_cast<int>(concurrency)) {
379 if (current_offset != content_size) {
380 current_offset = mch.StartTransfers(current_offset, content_size,
381 m_block_size, running_handles);
382 if (!running_handles) {
383 std::stringstream ss;
384 ss <<
"No handles are able to run. Streams=" << streams <<
", concurrency="
387 logTransferEvent(
LogMask::Debug, rec,
"MULTISTREAM_IDLE", ss.str());
389 }
else if (running_handles == 0) {
391 "Unable to start new transfers; breaking loop.");
396 int64_t max_sleep_time = next_marker - time(NULL);
397 if (max_sleep_time <= 0) {
401 #ifdef HAVE_CURL_MULTI_WAIT
402 mres = curl_multi_wait(multi_handle, NULL, 0, max_sleep_time*1000,
408 if (mres != CURLM_OK) {
411 }
while (running_handles);
413 if (mres != CURLM_OK) {
414 std::stringstream ss;
415 ss <<
"Internal libcurl multi-handle error: "
416 << curl_multi_strerror(mres);
417 logTransferEvent(
LogMask::Error, rec,
"MULTISTREAM_ERROR", ss.str());
418 throw std::runtime_error(ss.str());
425 msg = curl_multi_info_read(multi_handle, &msgq);
426 if (msg && (msg->msg == CURLMSG_DONE)) {
427 CURL *easy_handle = msg->easy_handle;
428 mch.FinishCurlXfer(easy_handle);
429 if (res == CURLE_OK || res ==
static_cast<CURLcode
>(-1))
430 res = msg->data.result;
434 if (!state.
GetErrorCode() && res ==
static_cast<CURLcode
>(-1)) {
436 "Internal state error in libcurl");
437 throw std::runtime_error(
"Internal state error in libcurl");
442 rec.bytes_transferred = mch.BytesTransferred();
443 rec.tpc_status = mch.GetStatusCode();
446 std::stringstream ss;
448 if (mch.GetStatusCode() >= 400) {
449 std::string err = mch.GetErrorMessage();
450 std::stringstream ss2;
451 ss2 <<
"Remote side failed with status code " << mch.GetStatusCode();
453 std::replace(err.begin(), err.end(),
'\n',
' ');
454 ss2 <<
"; error message: \"" << err <<
"\"";
456 logTransferEvent(
LogMask::Error, rec,
"MULTISTREAM_FAIL", ss.str());
457 ss << generateClientErr(ss2, rec);
458 }
else if (mch.GetErrorCode()) {
459 std::string err = mch.GetErrorMessage();
460 if (err.empty()) {err =
"(no error message provided)";}
461 else {std::replace(err.begin(), err.end(),
'\n',
' ');}
462 std::stringstream ss2;
463 ss2 <<
"Error when interacting with local filesystem: " << err;
464 logTransferEvent(
LogMask::Error, rec,
"MULTISTREAM_FAIL", ss2.str());
465 ss << generateClientErr(ss2, rec);
466 }
else if (res != CURLE_OK) {
467 std::stringstream ss2;
468 ss2 <<
"Request failed when processing";
469 std::stringstream ss3;
470 ss3 << ss2.str() <<
":" << curl_easy_strerror(res);
471 logTransferEvent(
LogMask::Error, rec,
"MULTISTREAM_FAIL", ss3.str());
472 ss << generateClientErr(ss2, rec, res);
473 }
else if (current_offset != content_size) {
474 std::stringstream ss2;
475 ss2 <<
"Internal logic error led to early abort; current offset is " <<
476 current_offset <<
" while full size is " << content_size;
477 logTransferEvent(
LogMask::Error, rec,
"MULTISTREAM_FAIL", ss2.str());
478 ss << generateClientErr(ss2, rec);
480 if (!handles[0]->Finalize()) {
481 std::stringstream ss2;
482 ss2 <<
"Failed to finalize and close file handle.";
483 ss << generateClientErr(ss2, rec);
487 ss <<
"success: Created";
492 if ((retval = req.
ChunkResp(ss.str().c_str(), 0))) {
494 "Failed to send last update to remote client");
496 }
else if (success) {
505 size_t streams, TPCLogRecord &rec)
507 std::vector<ManagedCurlHandle> curl_handles;
508 std::vector<State*> handles;
509 std::stringstream err_ss;
511 int retval = RunCurlWithStreamsImpl(req, state, streams, handles, curl_handles, rec);
512 for (std::vector<State*>::iterator state_iter = handles.begin();
513 state_iter != handles.end();
519 for (std::vector<State*>::iterator state_iter = handles.begin();
520 state_iter != handles.end();
526 logTransferEvent(
LogMask::Error, rec,
"MULTISTREAM_ERROR", e.what());
527 std::stringstream ss;
529 err_ss << generateClientErr(ss, rec);
531 }
catch (std::runtime_error &e) {
532 for (std::vector<State*>::iterator state_iter = handles.begin();
533 state_iter != handles.end();
538 logTransferEvent(
LogMask::Error, rec,
"MULTISTREAM_ERROR", e.what());
539 std::stringstream ss;
541 err_ss << generateClientErr(ss, rec);
543 if ((retval = req.
ChunkResp(err_ss.str().c_str(), 0))) {
CURLMcode curl_multi_wait_impl(CURLM *multi_handle, int timeout_ms, int *numfds)
CurlHandlerSetupError(const std::string &msg)
virtual ~CurlHandlerSetupError()
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.