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 {
23 }
24 
26 {
27  for (MakerTable::iterator i = m_makers.begin (); i != m_makers.end (); ++i)
28  delete i->second;
29 }
30 
33 { return &s_instance; }
34 
35 bool
37 {
38  bool old = m_accounting;
39  m_accounting = enabled;
40  return old;
41 }
42 
43 bool
45 { return m_accounting; }
46 
47 void
49 { m_cacheHint = value; }
50 
53 { return m_cacheHint; }
54 
55 void
57 { m_readHint = value; }
58 
61 { return m_readHint; }
62 
63 void
64 StorageFactory::setTimeout(unsigned int timeout)
65 { m_timeout = timeout; }
66 
67 unsigned int
69 { return m_timeout; }
70 
71 void
72 StorageFactory::setTempDir(const std::string &s, double minFreeSpace)
73 {
74 #if 0
75  std::cerr /* edm::LogInfo("StorageFactory") */
76  << "Considering path '" << s
77  << "', min free space " << minFreeSpace
78  << "GB for temp dir" << std::endl;
79 #endif
80 
81  size_t begin = 0;
82  std::vector<std::string> dirs;
83  dirs.reserve(std::count(s.begin(), s.end(), ':') + 1);
84 
85  while (true)
86  {
87  size_t end = s.find(':', begin);
88  if (end == std::string::npos)
89  {
90  dirs.push_back(s.substr(begin, end));
91  break;
92  }
93  else
94  {
95  dirs.push_back(s.substr(begin, end - begin));
96  begin = end+1;
97  }
98  }
99 
100  m_temppath = s;
101  m_tempfree = minFreeSpace;
102  m_tempdir = m_lfs.findCachePath(dirs, minFreeSpace);
103 
104 #if 0
105  std::cerr /* edm::LogInfo("StorageFactory") */
106  << "Using '" << m_tempdir << "' for temp dir"
107  << std::endl;
108 #endif
109 }
110 
111 std::string
113 { return m_tempdir; }
114 
115 std::string
117 { return m_temppath; }
118 
119 double
121 { return m_tempfree; }
122 
123 StorageMaker *
124 StorageFactory::getMaker (const std::string &proto)
125 {
126  StorageMaker *&instance = m_makers [proto];
129  if (! instance)
130  instance = StorageMakerFactory::get()->tryToCreate(proto);
131  return instance;
132 }
133 
134 StorageMaker *
135 StorageFactory::getMaker (const std::string &url,
136  std::string &protocol,
137  std::string &rest)
138 {
139  size_t p = url.find(':');
140  if (p != std::string::npos)
141  {
142  protocol = url.substr(0,p);
143  rest = url.substr(p+1);
144  }
145  else
146  {
147  protocol = "file";
148  rest = url;
149  }
150 
151  return getMaker (protocol);
152 }
153 
154 Storage *
155 StorageFactory::open (const std::string &url, int mode /* = IOFlags::OpenRead */)
156 {
157  std::string protocol;
158  std::string rest;
159  Storage *ret = 0;
160  boost::shared_ptr<StorageAccount::Stamp> stats;
161  if (StorageMaker *maker = getMaker (url, protocol, rest))
162  {
163  if (m_accounting)
164  stats.reset(new StorageAccount::Stamp(StorageAccount::counter (protocol, "open")));
165  try
166  {
167  if (Storage *storage = maker->open (protocol, rest, mode))
168  {
169  if (dynamic_cast<LocalCacheFile *>(storage))
170  protocol = "local-cache";
171 
172  if (m_accounting)
173  ret = new StorageAccountProxy(protocol, storage);
174  else
175  ret = storage;
176 
177  if (stats)
178  stats->tick();
179  }
180  }
181  catch (cms::Exception &err)
182  {
183  throw cms::Exception("StorageFactory::open()")
184  << "Failed to open the file '" << url << "' because:\n"
185  << err;
186  }
187  }
188  return ret;
189 }
190 
191 void
192 StorageFactory::stagein (const std::string &url)
193 {
194  std::string protocol;
195  std::string rest;
196 
197  boost::shared_ptr<StorageAccount::Stamp> stats;
198  if (StorageMaker *maker = getMaker (url, protocol, rest))
199  {
200  if (m_accounting)
201  stats.reset(new StorageAccount::Stamp(StorageAccount::counter (protocol, "stagein")));
202  try
203  {
204  maker->stagein (protocol, rest);
205  if (stats) stats->tick();
206  }
207  catch (cms::Exception &err)
208  {
209  edm::LogWarning("StorageFactory::stagein()")
210  << "Failed to stage in file '" << url << "' because:\n"
211  << err.explainSelf();
212  }
213  }
214 }
215 
216 bool
217 StorageFactory::check (const std::string &url, IOOffset *size /* = 0 */)
218 {
219  std::string protocol;
220  std::string rest;
221 
222  bool ret = false;
223  boost::shared_ptr<StorageAccount::Stamp> stats;
224  if (StorageMaker *maker = getMaker (url, protocol, rest))
225  {
226  if (m_accounting)
227  stats.reset(new StorageAccount::Stamp(StorageAccount::counter (protocol, "check")));
228  try
229  {
230  ret = maker->check (protocol, rest, size);
231  if (stats) stats->tick();
232  }
233  catch (cms::Exception &err)
234  {
235  edm::LogWarning("StorageFactory::check()")
236  << "Existence or size check for the file '" << url << "' failed because:\n"
237  << err.explainSelf();
238  }
239  }
240 
241  return ret;
242 }
243 
244 Storage *
246  const std::string &proto,
247  const std::string &path,
248  int mode)
249 {
253  && ! (mode & IOFlags::OpenWrite)
254  && ! m_tempdir.empty()
255  && (path.empty() || ! m_lfs.isLocalPath(path)))
256  {
257  if (accounting())
258  s = new StorageAccountProxy(proto, s);
259  s = new LocalCacheFile(s, m_tempdir);
260  }
261 
262  return s;
263 }
264 
265 void
266 StorageFactory::activateTimeout (const std::string &url)
267 {
268  std::string protocol;
269  std::string rest;
270 
271  if (StorageMaker *maker = getMaker (url, protocol, rest))
272  {
273  maker->setTimeout (m_timeout);
274  }
275 }
276 
277 
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)
std::string m_tempdir
virtual std::string explainSelf() const
Definition: Exception.cc:56
void setReadHint(ReadHint value)
Storage * open(const std::string &url, int mode=IOFlags::OpenRead)
bool check(const std::string &url, IOOffset *size=0)
Definition: Storage.h:8
ReadHint m_readHint
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)
int path() const
Definition: HLTadd.h:3
static StorageFactory * get(void)
PluginManager::Config config()
Definition: standard.cc:22
std::string tempDir(void) const
unsigned int timeout(void) const
#define end
Definition: vmac.h:38
CacheHint m_cacheHint
std::string m_temppath
static Counter & counter(const std::string &storageClass, const std::string &operation)
ReadHint readHint(void) const
int mode
Definition: AMPTWrapper.h:139
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:31
void activateTimeout(const std::string &url)
string s
Definition: asciidump.py:422
MakerTable m_makers
LocalFileSystem m_lfs
tuple size
Write out results.
T get(const Candidate &c)
Definition: component.h:56