CMS 3D CMS Logo

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