CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SiteLocalConfigService.cc
Go to the documentation of this file.
1 //<<<<<< INCLUDES >>>>>>
2 
8 #include <xercesc/dom/DOM.hpp>
9 #include <xercesc/parsers/XercesDOMParser.hpp>
11 #include <xercesc/util/XMLString.hpp>
12 #include <sstream>
13 #include <memory>
14 #include <boost/algorithm/string.hpp>
15 
16 using namespace xercesc;
17 
18 //<<<<<< PRIVATE DEFINES >>>>>>
19 //<<<<<< PRIVATE CONSTANTS >>>>>>
20 //<<<<<< PRIVATE TYPES >>>>>>
21 //<<<<<< PRIVATE VARIABLE DEFINITIONS >>>>>>
22 //<<<<<< PUBLIC VARIABLE DEFINITIONS >>>>>>
23 //<<<<<< CLASS STRUCTURE INITIALIZATION >>>>>>
24 //<<<<<< PRIVATE FUNCTION DEFINITIONS >>>>>>
25 //<<<<<< PUBLIC FUNCTION DEFINITIONS >>>>>>
26 //<<<<<< MEMBER FUNCTION DEFINITIONS >>>>>>
27 
28 namespace {
29  inline std::string _toString(XMLCh const* toTranscode) {
30  std::string tmp(XMLString::transcode(toTranscode));
31  return tmp;
32  }
33 
34  inline unsigned int _toUInt(XMLCh const* toTranscode) {
35  std::istringstream iss(_toString(toTranscode));
36  unsigned int returnValue;
37  iss >> returnValue;
38  return returnValue;
39  }
40 
41  inline bool _toBool(XMLCh const* toTranscode) {
42  std::string value = _toString(toTranscode);
43  if ((value == "true") || (value == "1"))
44  return true;
45  return false;
46  }
47 
48  inline double _toDouble(XMLCh const* toTranscode) {
49  std::istringstream iss(_toString(toTranscode));
50  double returnValue;
51  iss >> returnValue;
52  return returnValue;
53  }
54 
55  inline XMLCh* _toDOMS(std::string temp) {
56  XMLCh* buff = XMLString::transcode(temp.c_str());
57  return buff;
58  }
59 
60  // concatenate all the XML node attribute/value pairs into a
61  // paren-separated string (for use by CORAL and frontier_client)
62  inline std::string _toParenString(DOMNode const& nodeToConvert) {
63  std::ostringstream oss;
64 
65  DOMNodeList *childList = nodeToConvert.getChildNodes();
66 
67  unsigned int numNodes = childList->getLength();
68  for (unsigned int i = 0; i < numNodes; ++i) {
69  DOMNode *childNode = childList->item(i);
70  if (childNode->getNodeType() != DOMNode::ELEMENT_NODE) {
71  continue;
72  }
73  DOMElement *child = static_cast<DOMElement *>(childNode);
74 
75  DOMNamedNodeMap *attributes = child->getAttributes();
76  unsigned int numAttributes = attributes->getLength();
77  for (unsigned int j = 0; j < numAttributes; ++j) {
78  DOMNode *attributeNode = attributes->item(j);
79  if (attributeNode->getNodeType() != DOMNode::ATTRIBUTE_NODE) {
80  continue;
81  }
82  DOMAttr *attribute = static_cast<DOMAttr *>(attributeNode);
83 
84  oss << "(" << _toString(child->getTagName()) <<
85  _toString(attribute->getName()) << "=" <<
86  _toString(attribute->getValue()) << ")";
87  }
88  }
89  return oss.str();
90  }
91 
92  template<typename T>
93  static
94  void overrideFromPSet(char const* iName, edm::ParameterSet const& iPSet,
95  T& iHolder, T const*& iPointer) {
96  if(iPSet.exists(iName)) {
97  iHolder = iPSet.getUntrackedParameter<T>(iName);
98  iPointer = &iHolder;
99  }
100  }
101 }
102 
103 namespace edm {
104  namespace service {
105 
106  const std::string SiteLocalConfigService::m_statisticsDefaultPort = "3334";
107 
108  SiteLocalConfigService::SiteLocalConfigService(ParameterSet const& pset) :
109  m_url("/SITECONF/local/JobConfig/site-local-config.xml"),
110  m_dataCatalog(),
111  m_fallbackDataCatalog(),
112  m_frontierConnect(),
113  m_rfioType("castor"),
114  m_connected(false),
115  m_cacheTempDir(),
116  m_cacheTempDirPtr(nullptr),
117  m_cacheMinFree(),
118  m_cacheMinFreePtr(nullptr),
119  m_cacheHint(),
120  m_cacheHintPtr(nullptr),
121  m_readHint(),
122  m_readHintPtr(nullptr),
123  m_ttreeCacheSize(0U),
124  m_ttreeCacheSizePtr(nullptr),
125  m_timeout(0U),
126  m_timeoutPtr(nullptr),
127  m_debugLevel(0U),
128  m_enablePrefetching(false),
129  m_enablePrefetchingPtr(nullptr),
130  m_nativeProtocols(),
131  m_nativeProtocolsPtr(nullptr),
132  m_statisticsDestination(),
133  m_statisticsAddrInfo(nullptr),
134  m_siteName() {
135 
136  char* tmp = getenv("CMS_PATH");
137 
138  if (tmp) {
139  m_url = tmp + m_url;
140  }
141 
142  this->parse(m_url);
143 
144  //apply overrides
145  overrideFromPSet("overrideSourceCacheTempDir", pset, m_cacheTempDir, m_cacheTempDirPtr);
146  overrideFromPSet("overrideSourceCacheMinFree", pset, m_cacheMinFree, m_cacheMinFreePtr);
147  overrideFromPSet("overrideSourceCacheHintDir", pset, m_cacheHint, m_cacheHintPtr);
148  overrideFromPSet("overrideSourceReadHint", pset, m_readHint, m_readHintPtr);
149  overrideFromPSet("overrideSourceNativeProtocols", pset, m_nativeProtocols, m_nativeProtocolsPtr);
150  overrideFromPSet("overrideSourceTTreeCacheSize", pset, m_ttreeCacheSize, m_ttreeCacheSizePtr);
151  overrideFromPSet("overrideSourceTimeout", pset, m_timeout, m_timeoutPtr);
152  overrideFromPSet("overridePrefetching", pset, m_enablePrefetching, m_enablePrefetchingPtr);
153  const std::string * tmpStringPtr = NULL;
154  overrideFromPSet("overrideStatisticsDestination", pset, m_statisticsDestination, tmpStringPtr);
156 
157  if(pset.exists("debugLevel")) {
158  m_debugLevel = pset.getUntrackedParameter<unsigned int>("debugLevel");
159  }
160 
161  }
162 
164  if (m_statisticsAddrInfo) {
165  freeaddrinfo(m_statisticsAddrInfo);
166  m_statisticsAddrInfo = nullptr;
167  }
168  }
169 
170  std::string const
172  if (!m_connected) {
173  //throw cms::Exception("Incomplete configuration")
174  // << "Valid site-local-config not found at " << m_url;
175  // Return PoolFileCatalog.xml for now
176  return "file:PoolFileCatalog.xml";
177  }
178 
179  if (m_dataCatalog == "") {
180  throw cms::Exception("Incomplete configuration")
181  << "Did not find catalog in event-data section in " << m_url;
182  }
183 
184  return m_dataCatalog;
185  }
186 
187  std::string const
189  if (!m_connected) {
190  //throw cms::Exception("Incomplete configuration")
191  // << "Valid site-local-config not found at " << m_url;
192  // Return PoolFileCatalog.xml for now
193  return "file:PoolFileCatalog.xml";
194  }
195 
196  // Note: Unlike the dataCatalog, the fallbackDataCatalog may be empty!
197  return m_fallbackDataCatalog;
198  }
199 
200  std::string const
202  if (!m_connected) {
203  throw cms::Exception("Incomplete configuration")
204  << "Valid site-local-config not found at " << m_url;
205  }
206 
207  if (m_frontierConnect == "") {
208  throw cms::Exception("Incomplete configuration")
209  << "Did not find frontier-connect in calib-data section in " << m_url;
210  }
211 
212  if (servlet == "") {
213  return m_frontierConnect;
214  }
215 
216  // Replace the last component of every "serverurl=" piece (up to the
217  // next close-paren) with the servlet
218  std::string::size_type nextparen = 0;
219  std::string::size_type serverurl, lastslash;
220  std::string complexstr = "";
221  while ((serverurl = m_frontierConnect.find("(serverurl=", nextparen)) != std::string::npos) {
222  complexstr.append(m_frontierConnect, nextparen, serverurl - nextparen);
223  nextparen = m_frontierConnect.find(')', serverurl);
224  lastslash = m_frontierConnect.rfind('/', nextparen);
225  complexstr.append(m_frontierConnect, serverurl, lastslash - serverurl + 1);
226  complexstr.append(servlet);
227  }
228  complexstr.append(m_frontierConnect, nextparen, m_frontierConnect.length()-nextparen);
229 
230  return complexstr;
231  }
232 
233  std::string const
235  static std::string const proto = "frontier://";
236 
237  if (input.substr(0,proto.length()) == proto) {
238  // Replace the part after the frontier:// and before either an open-
239  // parentheses (which indicates user-supplied options) or the last
240  // slash (which indicates start of the schema) with the complex
241  // parenthesized string returned from frontierConnect() (which
242  // contains all the information needed to connect to frontier),
243  // if that part is a simple servlet name (non-empty and not
244  // containing special characters)
245  // Example connect strings where servlet is replaced:
246  // frontier://cms_conditions_data/CMS_COND_ECAL
247  // frontier://FrontierInt/CMS_COND_ECAL
248  // frontier://FrontierInt(retrieve-ziplevel=0)/CMS_COND_ECAL
249  // Example connect strings left untouched:
250  // frontier://cmsfrontier.cern.ch:8000/FrontierInt/CMS_COND_ECAL
251  // frontier://(serverurl=cmsfrontier.cern.ch:8000/FrontierInt)/CMS_COND_ECAL
252  std::string::size_type startservlet = proto.length();
253  // if user supplied extra parenthesized options, stop servlet there
254  std::string::size_type endservlet = input.find("(", startservlet);
255  if (endservlet == std::string::npos) {
256  endservlet = input.rfind('/', input.length());
257  }
258  std::string servlet = input.substr(startservlet, endservlet - startservlet);
259  if ((servlet != "") && (servlet.find_first_of(":/)[]") == std::string::npos)) {
260  if (servlet == "cms_conditions_data") {
261  // use the default servlet from site-local-config.xml
262  servlet = "";
263  }
264  return proto + frontierConnect(servlet) + input.substr(endservlet);
265  }
266  }
267  return input;
268  }
269 
270  std::string const
272  return m_rfioType;
273  }
274 
275  std::string const*
277  return m_cacheTempDirPtr;
278  }
279 
280  double const*
282  return m_cacheMinFreePtr;
283  }
284 
285  std::string const*
287  return m_cacheHintPtr;
288  }
289 
290  std::string const*
292  return m_readHintPtr;
293  }
294 
295  unsigned int const*
297  return m_ttreeCacheSizePtr;
298  }
299 
300  unsigned int const*
302  return m_timeoutPtr;
303  }
304 
305  bool
308  }
309 
310  unsigned int
312  return m_debugLevel;
313  }
314 
315  std::vector<std::string> const*
317  return m_nativeProtocolsPtr;
318  }
319 
320  struct addrinfo const*
322  return m_statisticsAddrInfo;
323  }
324 
325  std::string const&
327  return m_siteName;
328  }
329 
330  void
333  std::auto_ptr<XercesDOMParser> parser(new XercesDOMParser);
334  try {
335  parser->setValidationScheme(XercesDOMParser::Val_Auto);
336  parser->setDoNamespaces(false);
337 
338  parser->parse(url.c_str());
339  DOMDocument* doc = parser->getDocument();
340  if (!doc) {
341  return;
342  }
343 
344  // The Site Config has the following format
345  // <site-local-config>
346  // <site name="FNAL">
347  // <event-data>
348  // <catalog url="trivialcatalog_file:/x/y/z.xml"/>
349  // <rfiotype value="castor"/>
350  // </event-data>
351  // <calib-data>
352  // <catalog url="trivialcatalog_file:/x/y/z.xml"/>
353  // <frontier-connect>
354  // ... frontier-interpreted server/proxy xml ...
355  // </frontier-connect>
356  // </calib-data>
357  // <source-config>
358  // <cache-temp-dir name="/a/b/c"/>
359  // <cache-hint value="..."/>
360  // <read-hint value="..."/>
361  // <ttree-cache-size value="0"/>
362  // <native-protocols>
363  // <protocol prefix="dcache"/>
364  // <protocol prefix="file"/>
365  // </native-protocols>
366  // </source-config>
367  // </site>
368  // </site-local-config>
369 
370  // FIXME: should probably use the parser for validating the XML.
371 
372  DOMNodeList *sites = doc->getElementsByTagName(_toDOMS("site"));
373  unsigned int numSites = sites->getLength();
374  for (unsigned int i = 0; i < numSites; ++i) {
375  DOMElement *site = static_cast<DOMElement *>(sites->item(i));
376 
377  // Parse the site name
378  m_siteName = _toString(site->getAttribute(_toDOMS("name")));
379 
380  // Parsing of the event data section
381  {
382  DOMNodeList *eventDataList = site->getElementsByTagName(_toDOMS("event-data"));
383  if (eventDataList->getLength() > 0) {
384  DOMElement *eventData = static_cast<DOMElement *>(eventDataList->item(0));
385 
386  DOMNodeList *catalogs = eventData->getElementsByTagName(_toDOMS("catalog"));
387 
388  if (catalogs->getLength() > 0) {
389  DOMElement * catalog = static_cast<DOMElement *>(catalogs->item(0));
390  m_dataCatalog = _toString(catalog->getAttribute(_toDOMS("url")));
391  }
392 
393  if (catalogs->getLength() > 1) {
394  DOMElement * catalog = static_cast<DOMElement *>(catalogs->item(1));
395  m_fallbackDataCatalog = _toString(catalog->getAttribute(_toDOMS("url")));
396  }
397 
398  DOMNodeList* rfiotypes = eventData->getElementsByTagName(_toDOMS("rfiotype"));
399 
400  if (rfiotypes->getLength() > 0) {
401  DOMElement* rfiotype = static_cast<DOMElement *>(rfiotypes->item(0));
402  m_rfioType = _toString(rfiotype->getAttribute(_toDOMS("value")));
403  }
404  }
405  }
406 
407  // Parsing of the calib-data section
408  {
409  DOMNodeList *calibDataList = site->getElementsByTagName(_toDOMS("calib-data"));
410 
411  if (calibDataList->getLength() > 0) {
412  DOMElement *calibData = static_cast<DOMElement *>(calibDataList->item(0));
413  DOMNodeList *frontierConnectList = calibData->getElementsByTagName(_toDOMS("frontier-connect"));
414 
415  if (frontierConnectList->getLength() > 0) {
416  DOMElement *frontierConnect = static_cast<DOMElement *>(frontierConnectList->item(0));
417  m_frontierConnect = _toParenString(*frontierConnect);
418  }
419  }
420  }
421  // Parsing of the source config section
422  {
423  DOMNodeList *sourceConfigList = site->getElementsByTagName(_toDOMS("source-config"));
424 
425  if (sourceConfigList->getLength() > 0) {
426  DOMElement *sourceConfig = static_cast<DOMElement *>(sourceConfigList->item(0));
427  DOMNodeList *cacheTempDirList = sourceConfig->getElementsByTagName(_toDOMS("cache-temp-dir"));
428 
429  if (cacheTempDirList->getLength() > 0) {
430  DOMElement *cacheTempDir = static_cast<DOMElement *>(cacheTempDirList->item(0));
431  m_cacheTempDir = _toString(cacheTempDir->getAttribute(_toDOMS("name")));
433  }
434 
435  DOMNodeList *cacheMinFreeList = sourceConfig->getElementsByTagName(_toDOMS("cache-min-free"));
436 
437  if (cacheMinFreeList->getLength() > 0) {
438  DOMElement *cacheMinFree = static_cast<DOMElement *>(cacheMinFreeList->item(0));
439  m_cacheMinFree = _toDouble(cacheMinFree->getAttribute(_toDOMS("value")));
441  }
442 
443  DOMNodeList *cacheHintList = sourceConfig->getElementsByTagName(_toDOMS("cache-hint"));
444 
445  if (cacheHintList->getLength() > 0) {
446  DOMElement *cacheHint = static_cast<DOMElement *>(cacheHintList->item(0));
447  m_cacheHint = _toString(cacheHint->getAttribute(_toDOMS("value")));
449  }
450 
451  DOMNodeList *readHintList = sourceConfig->getElementsByTagName(_toDOMS("read-hint"));
452 
453  if (readHintList->getLength() > 0) {
454  DOMElement *readHint = static_cast<DOMElement *>(readHintList->item(0));
455  m_readHint = _toString(readHint->getAttribute(_toDOMS("value")));
457  }
458 
459  DOMNodeList *ttreeCacheSizeList = sourceConfig->getElementsByTagName(_toDOMS("ttree-cache-size"));
460 
461  if (ttreeCacheSizeList->getLength() > 0) {
462  DOMElement *ttreeCacheSize = static_cast<DOMElement *>(ttreeCacheSizeList->item(0));
463  m_ttreeCacheSize = _toUInt(ttreeCacheSize->getAttribute(_toDOMS("value")));
465  }
466 
467  DOMNodeList *timeoutList = sourceConfig->getElementsByTagName(_toDOMS("timeout-in-seconds"));
468 
469  if (timeoutList->getLength() > 0) {
470  DOMElement *timeout = static_cast<DOMElement *>(timeoutList->item(0));
471  m_timeout = _toUInt(timeout->getAttribute(_toDOMS("value")));
473  }
474 
475  DOMNodeList *statsDestList = sourceConfig->getElementsByTagName(_toDOMS("statistics-destination"));
476 
477  if (statsDestList->getLength() > 0) {
478  DOMElement *statsDest = static_cast<DOMElement *>(statsDestList->item(0));
479  m_statisticsDestination = _toString(statsDest->getAttribute(_toDOMS("name")));
480  }
481 
482  DOMNodeList *prefetchingList = sourceConfig->getElementsByTagName(_toDOMS("prefetching"));
483 
484  if (prefetchingList->getLength() > 0) {
485  DOMElement *prefetching = static_cast<DOMElement *>(prefetchingList->item(0));
486  m_enablePrefetching = _toBool(prefetching->getAttribute(_toDOMS("value")));
488  }
489 
490  DOMNodeList *nativeProtocolsList = sourceConfig->getElementsByTagName(_toDOMS("native-protocols"));
491 
492  if (nativeProtocolsList->getLength() > 0) {
493  DOMElement *nativeProtocol = static_cast<DOMElement *>(nativeProtocolsList->item(0));
494  DOMNodeList *childList = nativeProtocol->getChildNodes();
495 
496  XMLCh* prefixXMLCh = _toDOMS("prefix");
497  unsigned int numNodes = childList->getLength();
498  for (unsigned int i = 0; i < numNodes; ++i) {
499  DOMNode *childNode = childList->item(i);
500  if (childNode->getNodeType() != DOMNode::ELEMENT_NODE) {
501  continue;
502  }
503  DOMElement *child = static_cast<DOMElement *>(childNode);
504  m_nativeProtocols.push_back(_toString(child->getAttribute(prefixXMLCh)));
505  }
507  }
508  }
509  }
510  }
511  m_connected = true;
512  }
513  catch (xercesc::DOMException const& e) {
514  }
515  }
516 
517  void
519  std::vector<std::string> inputStrings;
520  boost::split(inputStrings, m_statisticsDestination, boost::is_any_of(":"));
521  const std::string &host=inputStrings[0];
522  const std::string &port = (inputStrings.size() > 1) ? inputStrings[1] : m_statisticsDefaultPort;
523  struct addrinfo *res;
524  struct addrinfo hints;
525  memset(&hints, '\0', sizeof(hints));
526  hints.ai_socktype = SOCK_DGRAM;
527  hints.ai_flags = AI_ADDRCONFIG;
528  hints.ai_family = AF_UNSPEC;
529  int e = getaddrinfo(host.c_str(), port.c_str(), &hints, &res);
530  if (e != 0) {
531  // Silent failure - there's no way to report non-fatal failures from here.
532  return;
533  }
534  m_statisticsAddrInfo = res;
535  }
536 
537  void
540  desc.setComment("Service to translate logical file names to physical file names.");
541 
542  desc.addOptionalUntracked<std::string>("overrideSourceCacheTempDir");
543  desc.addOptionalUntracked<double>("overrideSourceCacheMinFree");
544  desc.addOptionalUntracked<std::string>("overrideSourceCacheHintDir");
545  desc.addOptionalUntracked<std::string>("overrideSourceReadHint");
546  desc.addOptionalUntracked<std::vector<std::string> >("overrideSourceNativeProtocols");
547  desc.addOptionalUntracked<unsigned int>("overrideSourceTTreeCacheSize");
548  desc.addOptionalUntracked<unsigned int>("overrideSourceTimeout");
549  desc.addOptionalUntracked<unsigned int>("debugLevel");
550  desc.addOptionalUntracked<bool>("overridePrefetching")
551  ->setComment("Request ROOT to asynchronously prefetch I/O during computation.");
552  desc.addOptionalUntracked<std::string>("overrideStatisticsDestination")
553  ->setComment("Provide an alternate network destination for I/O statistics (must be in the form of host:port).");
554 
555  descriptions.add("SiteLocalConfigService", desc);
556  }
557  }
558 }
std::string const fallbackDataCatalog(void) const
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
static void fillDescriptions(ConfigurationDescriptions &descriptions)
std::string const rfioType(void) const
static const std::string m_statisticsDefaultPort
#define nullptr
std::string _toString(const XMLCh *toTranscode)
std::string const dataCatalog(void) const
bool exists(std::string const &parameterName) const
checks if a parameter exists
#define NULL
Definition: scimark2.h:8
struct addrinfo const * statisticsDestination() const
void xercesInitialize()
Definition: Xerces.cc:17
uint16_t size_type
int port
Definition: query.py:115
std::vector< std::string > const * m_nativeProtocolsPtr
static std::string const input
Definition: EdmProvDump.cc:44
std::string const lookupCalibConnect(std::string const &input) const
void setComment(std::string const &value)
std::string const * sourceCacheHint() const
list attributes
Definition: asciidump.py:415
std::string const * sourceReadHint() const
int j
Definition: DBlmapReader.cc:9
tuple doc
Definition: asciidump.py:381
std::vector< std::string > m_nativeProtocols
string host
Definition: query.py:114
string const
Definition: compareJSON.py:14
void add(std::string const &label, ParameterSetDescription const &psetDescription)
std::string const * sourceCacheTempDir() const
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
XMLCh * _toDOMS(std::string temp)
unsigned int const * sourceTimeout() const
ParameterDescriptionBase * addOptionalUntracked(U const &iLabel, T const &value)
volatile std::atomic< bool > shutdown_flag false
std::string const frontierConnect(std::string const &servlet) const
std::vector< std::string > const * sourceNativeProtocols() const
long double T
unsigned int const * sourceTTreeCacheSize() const
double split
Definition: MVATrainer.cc:139