3 #define _GLIBCXX_USE_NANOSLEEP
13 #include "XrdCl/XrdClFile.hh"
23 #define MAX_REQUEST 256 * 1024
24 #define XRD_CL_MAX_CHUNK 512 * 1024
28 #define XRD_DELAY 1000
29 #define XRD_SLOW_RATE 2
35 using namespace XrdAdaptor;
49 if (!
m_fh->Close(
this).IsOK()) {
59 XrdCl::HostList *hostList)
override {
60 if (status && !status->IsOK()) {
61 edm::LogWarning(
"XrdFileWarning") <<
"Source delayed close failed with error '" << status->ToStr()
62 <<
"' (errno=" << status->errNo <<
", code=" << status->code
63 <<
", server=" <<
m_id <<
", site=" << m_site <<
")";
83 friend std::unique_ptr<QueryAttrHandler> std::make_unique<QueryAttrHandler>();
90 static XrdCl::XRootDStatus
query(XrdCl::FileSystem &fs,
92 std::chrono::milliseconds
timeout,
94 auto handler = std::make_unique<QueryAttrHandler>();
95 auto l_state = std::make_shared<QueryAttrState>();
96 handler->m_state = l_state;
97 XrdCl::Buffer
arg(attr.size());
108 std::unique_lock<std::mutex> guard(l_state->m_mutex);
110 l_state->m_condvar.wait_for(guard, timeout, [&] {
return l_state->m_status.get(); });
112 if (l_state->m_status) {
113 if (l_state->m_status->IsOK()) {
114 result = l_state->m_response->ToString();
116 return *(l_state->m_status);
118 return XrdCl::XRootDStatus(
119 XrdCl::stError, XrdCl::errSocketTimeout, 1,
"Timeout when waiting for query callback.");
128 std::unique_ptr<XrdCl::AnyObject> response_mgr;
129 response_mgr.reset(response);
132 auto l_state = m_state.lock();
139 std::unique_ptr<char, std::function<void(char *)>> notify_guard(
nullptr,
140 [&](
char *) { l_state->m_condvar.notify_all(); });
144 std::unique_ptr<char, std::function<void(char *)>> exit_guard(
nullptr, [&](
char *) {
145 if (!l_state->m_status)
146 l_state->m_status = std::make_unique<XrdCl::XRootDStatus>(XrdCl::stError, XrdCl::errInternal);
151 if (status->IsOK()) {
155 XrdCl::Buffer *buf_ptr;
156 response->Get(buf_ptr);
158 response->Set(static_cast<int *>(
nullptr));
159 l_state->m_response.reset(buf_ptr);
161 l_state->m_status.reset(status);
182 : m_lastDowngrade({0, 0}),
195 if (!
m_fh->GetProperty(
"DataServer",
m_id)) {
196 edm::LogWarning(
"XrdFileWarning") <<
"Source::Source() failed to determine data server name.'";
205 if (getDomain(
m_id, domain_id)) {
208 m_site =
"Unknown (" +
m_id +
")";
220 size_t pos =
id.find(
':');
222 if ((pos != std::string::npos) && (pos > 0)) {
223 hostname =
id.substr(0, pos);
227 if (!hostname.empty() && ((hostname[0] ==
'[') || isdigit(hostname[0]))) {
229 struct addrinfo hints;
230 memset(&hints, 0,
sizeof(
struct addrinfo));
231 hints.ai_family = AF_UNSPEC;
233 if (!getaddrinfo(hostname.c_str(),
nullptr, &hints, &
result)) {
234 std::vector<char>
host;
236 if (!getnameinfo(result->ai_addr, result->ai_addrlen, &host[0], 255,
nullptr, 0, NI_NAMEREQD)) {
240 freeaddrinfo(result);
248 size_t pos = domain.find(
'.');
249 if (pos != std::string::npos && (pos < domain.size())) {
250 domain = domain.substr(pos + 1);
253 return !domain.empty();
264 file.GetProperty(
"LastURL", lastUrl);
265 if (!lastUrl.empty()) {
267 if (result && hostList && (hostList->size() > 1)) {
268 if (
isDCachePool((*hostList)[hostList->size() - 2].url.GetURL())) {
280 XrdCl::URL::ParamsMap map = url.GetParams();
282 if (map.find(
"org.dcache.uuid") != map.end()) {
295 if (hostList && (hostList->size() > 3) &&
isDCachePool(file, hostList)) {
296 const XrdCl::HostInfo &
info = (*hostList)[hostList->size() - 3];
297 exclude = info.url.GetHostName();
299 file.GetProperty(
"LastURL", lastUrl);
300 edm::LogVerbatim(
"XrdAdaptorInternal") <<
"Changing exclude list for URL " << lastUrl <<
" to " << exclude;
306 fh.GetProperty(
"LastURL", lastUrl);
309 if (!fh.GetProperty(
"DataServer", server)) {
314 if (lastUrl.empty()) {
315 edm::LogWarning(
"XrdFileWarning") <<
"Unable to determine the URL associated with server " <<
id;
318 if (!server.empty()) {
329 XrdCl::Buffer
arg(attr.size());
330 arg.FromString(attr);
332 XrdCl::FileSystem fs(url);
341 if (!rsite.empty() && (rsite[rsite.size() - 1] ==
'\n')) {
342 rsite = rsite.substr(0, rsite.size() - 1);
344 if (rsite ==
"sitename") {
358 <<
" did not provide a sitename. Monitoring may be incomplete.";
371 off_t last_offset = -1;
372 for (
const auto &ci : cl) {
373 assert(static_cast<off_t>(ci.offset) > last_offset);
374 last_offset = ci.offset;
376 assert(ci.offset < 0x1ffffffffff);
379 assert(cl.size() <= 1024);
383 edm::LogVerbatim(
"XrdAdaptorInternal") <<
"Reading from " <<
ID() <<
", quality " <<
m_qm->get() << std::endl;
384 c->m_source = shared_from_this();
385 c->m_self_reference =
c;
386 m_qm->startWatch(c->m_qmw);
389 c->setStatistics(readStats);
393 std::this_thread::sleep_for(std::chrono::milliseconds(XRD_DELAY));
396 XrdCl::XRootDStatus
status;
399 status =
m_fh->Read(c->m_off, c->m_size, c->m_into, c.get());
402 cl.reserve(c->m_iolist->size());
403 for (
const auto &it : *c->m_iolist) {
404 cl.emplace_back(it.offset(), it.size(), it.data());
407 status =
m_fh->VectorRead(cl,
nullptr, c.get());
410 if (!status.IsOK()) {
412 ex <<
"XrdFile::Read or XrdFile::VectorRead failed with error: '" << status.ToStr() <<
"' (errNo = " << status.errNo
Log< level::Info, true > LogVerbatim
static bool getXrootdSite(XrdCl::File &file, std::string &site)
std::atomic< int > g_delayCount
const edm::EventSetup & c
uint16_t *__restrict__ id
std::unique_ptr< XrdCl::Buffer > m_response
edm::propagate_const< std::shared_ptr< XrdSiteStatistics > > m_stats
static void validateList(const XrdCl::ChunkList &cl)
static XrdCl::XRootDStatus query(XrdCl::FileSystem &fs, const std::string &attr, std::chrono::milliseconds timeout, std::string &result)
static void determineHostExcludeString(XrdCl::File &file, const XrdCl::HostList *hostList, std::string &exclude)
DelayedClose(std::shared_ptr< XrdCl::File > fh, const std::string &id, const std::string &site)
void handle(std::shared_ptr< ClientRequest >)
std::shared_ptr< XrdSiteStatistics const > stats() const
edm::propagate_const< std::shared_ptr< XrdCl::File > > m_fh
static std::unique_ptr< QualityMetricSource > get(timespec now, const std::string &id)
edm::propagate_const< std::shared_ptr< XrdCl::File > > m_fh
void HandleResponse(XrdCl::XRootDStatus *status, XrdCl::AnyObject *response) override
static std::shared_ptr< XrdReadStatistics > startRead(std::shared_ptr< XrdSiteStatistics > parent, std::shared_ptr< ClientRequest > req)
static bool getDomain(const std::string &host, std::string &domain)
static bool isDCachePool(XrdCl::File &file, const XrdCl::HostList *hostList=nullptr)
XrdSiteStatisticsInformation * statsService
std::shared_ptr< XrdCl::File const > fh() const
Log< level::Info, false > LogInfo
void addContext(std::string const &context)
void HandleResponseWithHosts(XrdCl::XRootDStatus *status, XrdCl::AnyObject *response, XrdCl::HostList *hostList) override
std::condition_variable m_condvar
std::shared_ptr< XrdCl::File > getFileHandle()
static bool getXrootdSiteFromURL(std::string url, std::string &site)
Log< level::Warning, false > LogWarning
edm::propagate_const< std::unique_ptr< QualityMetricSource > > m_qm
std::weak_ptr< QueryAttrState > m_state
std::unique_ptr< XrdCl::XRootDStatus > m_status
static bool getHostname(const std::string &id, std::string &hostname)
const std::string & ID() const
Source(const Source &)=delete