CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
XrdStorageMaker.cc
Go to the documentation of this file.
1 
6 
12 
13 #include "XrdCl/XrdClDefaultEnv.hh"
14 #include "XrdNet/XrdNetUtils.hh"
15 
16 #include <atomic>
17 #include <mutex>
18 
19 class MakerResponseHandler : public XrdCl::ResponseHandler {
20 public:
21  void HandleResponse(XrdCl::XRootDStatus *status, XrdCl::AnyObject *response) override {
22  // Note: Prepare call has a response object.
23  delete response;
24  delete status;
25  }
26 };
27 
28 namespace edm::storage {
29  class XrdStorageMaker final : public StorageMaker {
30  public:
31  static const unsigned int XRD_DEFAULT_TIMEOUT = 3 * 60;
32 
34  : m_lastDebugLevel(1), //so that 0 will trigger change
35  m_lastTimeout(0) {
36  // When CMSSW loads, both XrdCl and XrdClient end up being loaded
37  // (ROOT loads XrdClient). XrdClient forces IPv4-only. Accordingly,
38  // we must explicitly set the default network stack in XrdCl to
39  // whatever is available on the node (IPv4 or IPv6).
40  XrdCl::Env *env = XrdCl::DefaultEnv::GetEnv();
41  if (env) {
42  env->PutString("NetworkStack", "IPAuto");
43  }
44  XrdNetUtils::SetAuto(XrdNetUtils::prefAuto);
46  setDebugLevel(0);
47  }
48 
51  std::unique_ptr<Storage> open(const std::string &proto,
52  const std::string &path,
53  int mode,
54  const AuxSettings &aux) const override {
56  setTimeout(aux.timeout);
57 
59  StorageFactory::ReadHint readHint = f->readHint();
60  StorageFactory::CacheHint cacheHint = f->cacheHint();
61 
63  mode &= ~IOFlags::OpenUnbuffered;
64  else
66 
67  std::string fullpath(proto + ":" + path);
68  auto file = std::make_unique<XrdFile>(fullpath, mode);
69  return f->wrapNonLocalFile(std::move(file), proto, std::string(), mode);
70  }
71 
72  void stagein(const std::string &proto, const std::string &path, const AuxSettings &aux) const override {
74  setTimeout(aux.timeout);
75 
76  std::string fullpath(proto + ":" + path);
77  XrdCl::URL url(fullpath);
78  XrdCl::FileSystem fs(url);
79  std::vector<std::string> fileList;
80  fileList.push_back(url.GetPath());
81  auto status = fs.Prepare(fileList, XrdCl::PrepareFlags::Stage, 0, &m_null_handler);
82  if (!status.IsOK()) {
83  edm::LogWarning("StageInError") << "XrdCl::FileSystem::Prepare failed with error '" << status.ToStr()
84  << "' (errNo = " << status.errNo << ")";
85  }
86  }
87 
88  bool check(const std::string &proto,
89  const std::string &path,
90  const AuxSettings &aux,
91  IOOffset *size = nullptr) const override {
93  setTimeout(aux.timeout);
94 
95  std::string fullpath(proto + ":" + path);
96  XrdCl::URL url(fullpath);
97  XrdCl::FileSystem fs(url);
98 
99  XrdCl::StatInfo *stat;
100  if (!(fs.Stat(url.GetPath(), stat)).IsOK() || (stat == nullptr)) {
101  return false;
102  }
103 
104  if (size)
105  *size = stat->GetSize();
106  return true;
107  }
108 
109  void setDebugLevel(unsigned int level) const {
110  auto oldLevel = m_lastDebugLevel.load();
111  if (level == oldLevel) {
112  return;
113  }
114  std::lock_guard<std::mutex> guard(m_envMutex);
115  if (oldLevel != m_lastDebugLevel) {
116  //another thread just changed this value
117  return;
118  }
119 
120  // 'Error' is way too low of debug level - we have interest
121  // in warning in the default
122  switch (level) {
123  case 0:
124  XrdCl::DefaultEnv::SetLogLevel("Warning");
125  break;
126  case 1:
127  XrdCl::DefaultEnv::SetLogLevel("Info");
128  break;
129  case 2:
130  XrdCl::DefaultEnv::SetLogLevel("Debug");
131  break;
132  case 3:
133  XrdCl::DefaultEnv::SetLogLevel("Dump");
134  break;
135  case 4:
136  XrdCl::DefaultEnv::SetLogLevel("Dump");
137  break;
138  default:
140  ex << "Invalid log level specified " << level;
141  ex.addContext("Calling XrdStorageMaker::setDebugLevel()");
142  throw ex;
143  }
145  }
146 
147  void setTimeout(unsigned int timeout) const {
148  timeout = timeout ? timeout : XRD_DEFAULT_TIMEOUT;
149 
150  auto oldTimeout = m_lastTimeout.load();
151  if (oldTimeout == timeout) {
152  return;
153  }
154 
155  std::lock_guard<std::mutex> guard(m_envMutex);
156  if (oldTimeout != m_lastTimeout) {
157  //Another thread beat us to changing the value
158  return;
159  }
160 
161  XrdCl::Env *env = XrdCl::DefaultEnv::GetEnv();
162  if (env) {
163  env->PutInt("StreamTimeout", timeout);
164  env->PutInt("RequestTimeout", timeout);
165  env->PutInt("ConnectionWindow", timeout);
166  env->PutInt("StreamErrorWindow", timeout);
167  // Crank down some of the connection defaults. We have more
168  // aggressive error recovery than the default client so we
169  // can error out sooner.
170  env->PutInt("ConnectionWindow", timeout / 6 + 1);
171  env->PutInt("ConnectionRetry", 2);
172  }
174  }
175 
176  private:
179  mutable std::atomic<unsigned int> m_lastDebugLevel;
180  mutable std::atomic<unsigned int> m_lastTimeout;
181  };
182 } // namespace edm::storage
183 
184 using namespace edm::storage;
185 
187 using XrdStatisticsMaker =
bool check(const std::string &proto, const std::string &path, const AuxSettings &aux, IOOffset *size=nullptr) const override
int64_t IOOffset
Definition: IOTypes.h:20
#define DEFINE_FWK_SERVICE_MAKER(concrete, maker)
Definition: ServiceMaker.h:100
void stagein(const std::string &proto, const std::string &path, const AuxSettings &aux) const override
static std::mutex mutex
Definition: Proxy.cc:8
list status
Definition: mps_update.py:107
std::atomic< unsigned int > m_lastTimeout
ReadHint readHint(void) const
int timeout
Definition: mps_check.py:53
def fullpath
Definition: das_client.py:267
CacheHint cacheHint(void) const
void setDebugLevel(unsigned int level) const
string URL
Definition: hcal_runs.py:4
std::unique_ptr< Storage > wrapNonLocalFile(std::unique_ptr< Storage > s, const std::string &proto, const std::string &path, int mode) const
static const unsigned int XRD_DEFAULT_TIMEOUT
def move
Definition: eostools.py:511
#define CMS_THREAD_SAFE
std::unique_ptr< Storage > open(const std::string &proto, const std::string &path, int mode, const AuxSettings &aux) const override
MakerResponseHandler m_null_handler
void addContext(std::string const &context)
Definition: Exception.cc:165
void HandleResponse(XrdCl::XRootDStatus *status, XrdCl::AnyObject *response) override
void setTimeout(unsigned int timeout) const
#define DEFINE_EDM_PLUGIN(factory, type, name)
tuple level
Definition: testEve_cfg.py:47
Log< level::Warning, false > LogWarning
std::atomic< unsigned int > m_lastDebugLevel
static const StorageFactory * get(void)
tuple size
Write out results.