CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
StorageFactory.cc
Go to the documentation of this file.
10 #include <boost/shared_ptr.hpp>
11 
13 
15  : m_cacheHint(CACHE_HINT_AUTO_DETECT),
16  m_readHint(READ_HINT_AUTO),
17  m_accounting (false),
18  m_tempfree (4.), // GB
19  m_temppath (".:$TMPDIR"),
20  m_timeout(0U),
21  m_debugLevel(0U)
22 {
24 }
25 
27 {
28  for (MakerTable::iterator i = m_makers.begin (); i != m_makers.end (); ++i)
29  delete i->second;
30 }
31 
34 { return &s_instance; }
35 
36 bool
38 {
39  bool old = m_accounting;
40  m_accounting = enabled;
41  return old;
42 }
43 
44 bool
46 { return m_accounting; }
47 
48 void
50 { m_cacheHint = value; }
51 
54 { return m_cacheHint; }
55 
56 void
58 { m_readHint = value; }
59 
62 { return m_readHint; }
63 
64 void
65 StorageFactory::setTimeout(unsigned int timeout)
66 { m_timeout = timeout; }
67 
68 unsigned int
70 { return m_timeout; }
71 
72 void
74 { m_debugLevel = level; }
75 
76 unsigned int
78 { return m_debugLevel; }
79 
80 void
81 StorageFactory::setTempDir(const std::string &s, double minFreeSpace)
82 {
83 #if 0
84  std::cerr /* edm::LogInfo("StorageFactory") */
85  << "Considering path '" << s
86  << "', min free space " << minFreeSpace
87  << "GB for temp dir" << std::endl;
88 #endif
89 
90  size_t begin = 0;
91  std::vector<std::string> dirs;
92  dirs.reserve(std::count(s.begin(), s.end(), ':') + 1);
93 
94  while (true)
95  {
96  size_t end = s.find(':', begin);
97  if (end == std::string::npos)
98  {
99  dirs.push_back(s.substr(begin, end));
100  break;
101  }
102  else
103  {
104  dirs.push_back(s.substr(begin, end - begin));
105  begin = end+1;
106  }
107  }
108 
109  m_temppath = s;
110  m_tempfree = minFreeSpace;
111  m_tempdir = m_lfs.findCachePath(dirs, minFreeSpace);
112 
113 #if 0
114  std::cerr /* edm::LogInfo("StorageFactory") */
115  << "Using '" << m_tempdir << "' for temp dir"
116  << std::endl;
117 #endif
118 }
119 
122 { return m_tempdir; }
123 
126 { return m_temppath; }
127 
128 double
130 { return m_tempfree; }
131 
132 StorageMaker *
134 {
135  StorageMaker *&instance = m_makers [proto];
138  if (! instance)
139  instance = StorageMakerFactory::get()->tryToCreate(proto);
140  return instance;
141 }
142 
143 StorageMaker *
145  std::string &protocol,
146  std::string &rest)
147 {
148  size_t p = url.find(':');
149  if (p != std::string::npos)
150  {
151  protocol = url.substr(0,p);
152  rest = url.substr(p+1);
153  }
154  else
155  {
156  protocol = "file";
157  rest = url;
158  }
159 
160  return getMaker (protocol);
161 }
162 
163 Storage *
164 StorageFactory::open (const std::string &url, int mode /* = IOFlags::OpenRead */)
165 {
166  std::string protocol;
167  std::string rest;
168  Storage *ret = 0;
169  boost::shared_ptr<StorageAccount::Stamp> stats;
170  if (StorageMaker *maker = getMaker (url, protocol, rest))
171  {
172  maker->setDebugLevel(m_debugLevel);
173  if (m_accounting)
174  stats.reset(new StorageAccount::Stamp(StorageAccount::counter (protocol, "open")));
175  try
176  {
177  if (Storage *storage = maker->open (protocol, rest, mode))
178  {
179  if (dynamic_cast<LocalCacheFile *>(storage))
180  protocol = "local-cache";
181 
182  if (m_accounting)
183  ret = new StorageAccountProxy(protocol, storage);
184  else
185  ret = storage;
186 
187  if (stats)
188  stats->tick();
189  }
190  }
191  catch (cms::Exception &err)
192  {
193  err.addContext("Calling StorageFactory::open()");
194  err.addAdditionalInfo(err.message());
195  err.clearMessage();
196  err << "Failed to open the file '" << url << "'";
197  throw;
198  }
199  }
200  return ret;
201 }
202 
203 void
205 {
206  std::string protocol;
207  std::string rest;
208 
209  boost::shared_ptr<StorageAccount::Stamp> stats;
210  if (StorageMaker *maker = getMaker (url, protocol, rest))
211  {
212  if (m_accounting)
213  stats.reset(new StorageAccount::Stamp(StorageAccount::counter (protocol, "stagein")));
214  try
215  {
216  maker->stagein (protocol, rest);
217  if (stats) stats->tick();
218  }
219  catch (cms::Exception &err)
220  {
221  edm::LogWarning("StorageFactory::stagein()")
222  << "Failed to stage in file '" << url << "' because:\n"
223  << err.explainSelf();
224  }
225  }
226 }
227 
228 bool
230 {
231  std::string protocol;
232  std::string rest;
233 
234  bool ret = false;
235  boost::shared_ptr<StorageAccount::Stamp> stats;
236  if (StorageMaker *maker = getMaker (url, protocol, rest))
237  {
238  if (m_accounting)
239  stats.reset(new StorageAccount::Stamp(StorageAccount::counter (protocol, "check")));
240  try
241  {
242  ret = maker->check (protocol, rest, size);
243  if (stats) stats->tick();
244  }
245  catch (cms::Exception &err)
246  {
247  edm::LogWarning("StorageFactory::check()")
248  << "Existence or size check for the file '" << url << "' failed because:\n"
249  << err.explainSelf();
250  }
251  }
252 
253  return ret;
254 }
255 
256 Storage *
258  const std::string &proto,
259  const std::string &path,
260  int mode)
261 {
264  && ! (mode & IOFlags::OpenWrite)
265  && ! m_tempdir.empty()
266  && (path.empty() || ! m_lfs.isLocalPath(path)))
267  {
268  if (accounting())
269  s = new StorageAccountProxy(proto, s);
270  s = new LocalCacheFile(s, m_tempdir);
271  }
272 
273  return s;
274 }
275 
276 void
278 {
279  std::string protocol;
280  std::string rest;
281 
282  if (StorageMaker *maker = getMaker (url, protocol, rest))
283  {
284  maker->setTimeout (m_timeout);
285  }
286 }
287 
288 
int i
Definition: DBlmapReader.cc:9
CacheHint cacheHint(void) const
void stagein(const std::string &url)
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)
Storage * open(const std::string &url, int mode=IOFlags::OpenRead)
bool check(const std::string &url, IOOffset *size=0)
ReadHint m_readHint
std::string message() const
Definition: Exception.cc:187
unsigned int m_debugLevel
std::string findCachePath(const std::vector< std::string > &paths, double minFreeSpace)
StorageMaker * getMaker(const std::string &proto)
double tempMinFree(void) const
Storage * wrapNonLocalFile(Storage *s, const std::string &proto, const std::string &path, int mode)
bool isLocalPath(const std::string &path)
static StorageFactory * get(void)
tuple path
else: Piece not in the list, fine.
PluginManager::Config config()
Definition: standard.cc:21
std::string tempDir(void) const
unsigned int timeout(void) const
void addAdditionalInfo(std::string const &info)
Definition: Exception.cc:235
#define end
Definition: vmac.h:37
void setDebugLevel(unsigned int level)
CacheHint m_cacheHint
void clearMessage()
Definition: Exception.cc:215
std::string m_temppath
static Counter & counter(const std::string &storageClass, const std::string &operation)
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
void activateTimeout(const std::string &url)
tuple level
Definition: testEve_cfg.py:34
volatile std::atomic< bool > shutdown_flag false
MakerTable m_makers
LocalFileSystem m_lfs
tuple size
Write out results.
T get(const Candidate &c)
Definition: component.h:55
unsigned int debugLevel(void) const