CMS 3D CMS Logo

StorageFactory.cc
Go to the documentation of this file.
10 
12 
14  : m_cacheHint(CACHE_HINT_AUTO_DETECT),
15  m_readHint(READ_HINT_AUTO),
16  m_accounting (false),
17  m_tempfree (4.), // GB
18  m_temppath (".:$TMPDIR"),
19  m_timeout(0U),
20  m_debugLevel(0U)
21 {
23 }
24 
26 {
27 }
28 
29 const StorageFactory *
31 { return &s_instance; }
32 
35 { return &s_instance; }
36 
37 bool
39 {
40  bool old = m_accounting;
42  return old;
43 }
44 
45 bool
47 { return m_accounting; }
48 
49 void
51 { m_cacheHint = value; }
52 
55 { return m_cacheHint; }
56 
57 void
59 { m_readHint = value; }
60 
63 { return m_readHint; }
64 
65 void
67 { m_timeout = timeout; }
68 
69 unsigned int
71 { return m_timeout; }
72 
73 void
75 { m_debugLevel = level; }
76 
77 unsigned int
79 { return m_debugLevel; }
80 
81 void
82 StorageFactory::setTempDir(const std::string &s, double minFreeSpace)
83 {
84 #if 0
85  std::cerr /* edm::LogInfo("StorageFactory") */
86  << "Considering path '" << s
87  << "', min free space " << minFreeSpace
88  << "GB for temp dir" << std::endl;
89 #endif
90 
91  size_t begin = 0;
92  std::vector<std::string> dirs;
93  dirs.reserve(std::count(s.begin(), s.end(), ':') + 1);
94 
95  while (true)
96  {
97  size_t end = s.find(':', begin);
98  if (end == std::string::npos)
99  {
100  dirs.push_back(s.substr(begin, end));
101  break;
102  }
103  else
104  {
105  dirs.push_back(s.substr(begin, end - begin));
106  begin = end+1;
107  }
108  }
109 
110  m_temppath = s;
111  m_tempfree = minFreeSpace;
112  std::tie(m_tempdir, m_unusableDirWarnings) = m_lfs.findCachePath(dirs, minFreeSpace);
113 
114 #if 0
115  std::cerr /* edm::LogInfo("StorageFactory") */
116  << "Using '" << m_tempdir << "' for temp dir"
117  << std::endl;
118 #endif
119 }
120 
123 { return m_tempdir; }
124 
127 { return m_temppath; }
128 
129 double
131 { return m_tempfree; }
132 
133 StorageMaker *
135 {
136  auto itFound = m_makers.find(proto);
137  if(itFound != m_makers.end()) {
138  return itFound->second.get();
139  }
142  }
143  std::shared_ptr<StorageMaker> instance{ StorageMakerFactory::get()->tryToCreate(proto)};
144  auto insertResult = m_makers.insert(MakerTable::value_type(proto,instance));
145  //Can't use instance since it is possible that another thread beat
146  // us to the insertion so the map contains a different instance.
147  return insertResult.first->second.get();
148 }
149 
150 StorageMaker *
152  std::string &protocol,
153  std::string &rest) const
154 {
155  size_t p = url.find(':');
156  if (p != std::string::npos)
157  {
158  protocol = url.substr(0,p);
159  rest = url.substr(p+1);
160  }
161  else
162  {
163  protocol = "file";
164  rest = url;
165  }
166 
167  return getMaker (protocol);
168 }
169 
170 std::unique_ptr<Storage>
171 StorageFactory::open (const std::string &url, int mode /* = IOFlags::OpenRead */) const
172 {
173  std::string protocol;
174  std::string rest;
175  std::unique_ptr<Storage> ret;
176  std::unique_ptr<StorageAccount::Stamp> stats;
177  if (StorageMaker *maker = getMaker (url, protocol, rest))
178  {
179  if (m_accounting) {
180  auto token = StorageAccount::tokenForStorageClassName(protocol);
182  }
183  try
184  {
185  if (auto storage = maker->open (protocol, rest, mode, StorageMaker::AuxSettings{}.setDebugLevel(m_debugLevel).setTimeout(m_timeout)))
186  {
187  if (dynamic_cast<LocalCacheFile *>(storage.get()))
188  protocol = "local-cache";
189 
190  if (m_accounting)
191  ret = std::make_unique<StorageAccountProxy>(protocol, std::move(storage));
192  else
193  ret = std::move(storage);
194 
195  if (stats)
196  stats->tick();
197  }
198  }
199  catch (cms::Exception &err)
200  {
201  err.addContext("Calling StorageFactory::open()");
202  err.addAdditionalInfo(err.message());
203  err.clearMessage();
204  err << "Failed to open the file '" << url << "'";
205  throw;
206  }
207  }
208  return ret;
209 }
210 
211 void
213 {
214  std::string protocol;
215  std::string rest;
216 
217  std::unique_ptr<StorageAccount::Stamp> stats;
218  if (StorageMaker *maker = getMaker (url, protocol, rest))
219  {
220  if (m_accounting) {
221  auto token = StorageAccount::tokenForStorageClassName(protocol);
223  }
224  try
225  {
226  maker->stagein (protocol, rest,StorageMaker::AuxSettings{}.setDebugLevel(m_debugLevel).setTimeout(m_timeout));
227  if (stats) stats->tick();
228  }
229  catch (cms::Exception &err)
230  {
231  edm::LogWarning("StorageFactory::stagein()")
232  << "Failed to stage in file '" << url << "' because:\n"
233  << err.explainSelf();
234  }
235  }
236 }
237 
238 bool
240 {
241  std::string protocol;
242  std::string rest;
243 
244  bool ret = false;
245  std::unique_ptr<StorageAccount::Stamp> stats;
246  if (StorageMaker *maker = getMaker (url, protocol, rest))
247  {
248  if (m_accounting) {
249  auto token = StorageAccount::tokenForStorageClassName(protocol);
251  }
252  try
253  {
254  ret = maker->check (protocol, rest, StorageMaker::AuxSettings{}.setDebugLevel(m_debugLevel).setTimeout(m_timeout), size);
255  if (stats) stats->tick();
256  }
257  catch (cms::Exception &err)
258  {
259  edm::LogWarning("StorageFactory::check()")
260  << "Existence or size check for the file '" << url << "' failed because:\n"
261  << err.explainSelf();
262  }
263  }
264 
265  return ret;
266 }
267 
268 std::unique_ptr<Storage>
269 StorageFactory::wrapNonLocalFile (std::unique_ptr<Storage> s,
270  const std::string &proto,
271  const std::string &path,
272  int mode) const
273 {
276  {
277  if (mode & IOFlags::OpenWrite)
278  {
279  // For now, issue no warning - otherwise, we'd always warn on output files.
280  }
281  else if (m_tempdir.empty())
282  {
283  edm::LogWarning("StorageFactory") << m_unusableDirWarnings;
284  }
285  else if ( (not path.empty()) and m_lfs.isLocalPath(path))
286  {
287  // For now, issue no warning - otherwise, we'd always warn on local input files.
288  }
289  else
290  {
291  if (accounting()) {s = std::make_unique<StorageAccountProxy>(proto, std::move(s));}
292  s = std::make_unique<LocalCacheFile>(std::move(s), m_tempdir);
293  }
294  }
295 
296  return s;
297 }
298 
299 
300 
size
Write out results.
bool check(const std::string &url, IOOffset *size=0) const
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
bool enableAccounting(bool enabled)
static PluginManager & configure(const Config &)
std::string tempPath(void) const
void setTimeout(unsigned int timeout)
static PFTauRenderPlugin instance
std::string m_tempdir
virtual std::string explainSelf() const
Definition: Exception.cc:146
void setReadHint(ReadHint value)
ReadHint m_readHint
std::string message() const
Definition: Exception.cc:187
unsigned int m_debugLevel
double tempMinFree(void) const
static const StorageFactory * get(void)
StorageMaker * getMaker(const std::string &proto) const
static StorageClassToken tokenForStorageClassName(std::string const &iName)
PluginManager::Config config()
Definition: standard.cc:21
void stagein(const std::string &url) const
std::string tempDir(void) const
unsigned int timeout(void) const
void addAdditionalInfo(std::string const &info)
Definition: Exception.cc:235
bool isLocalPath(const std::string &path) const
#define end
Definition: vmac.h:37
Definition: value.py:1
void setDebugLevel(unsigned int level)
CacheHint m_cacheHint
void clearMessage()
Definition: Exception.cc:215
static Counter & counter(StorageClassToken token, Operation operation)
std::pair< std::string, std::string > findCachePath(const std::vector< std::string > &paths, double minFreeSpace) const
std::string m_temppath
ReadHint readHint(void) const
void addContext(std::string const &context)
Definition: Exception.cc:227
int64_t IOOffset
Definition: IOTypes.h:19
bool accounting(void) const
void setTempDir(const std::string &s, double minFreeSpace)
static StorageFactory s_instance
void setCacheHint(CacheHint value)
unsigned int m_timeout
#define begin
Definition: vmac.h:30
AuxSettings & setDebugLevel(unsigned int iLevel)
Definition: StorageMaker.h:16
MakerTable m_makers
LocalFileSystem m_lfs
std::string m_unusableDirWarnings
def move(src, dest)
Definition: eostools.py:510
T get(const Candidate &c)
Definition: component.h:55
AuxSettings & setTimeout(unsigned int iTime)
Definition: StorageMaker.h:21
static StorageFactory * getToModify(void)
unsigned int debugLevel(void) const
std::unique_ptr< Storage > open(const std::string &url, int mode=IOFlags::OpenRead) const