CMS 3D CMS Logo

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