Belle II Software  release-05-01-25
CentralMetadataProvider.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2019 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Martin Ritter *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <framework/database/CentralMetadataProvider.h>
12 #include <framework/logging/Logger.h>
13 
14 using json = nlohmann::json;
15 
16 namespace Belle2::Conditions {
17  CentralMetadataProvider::CentralMetadataProvider(std::string baseUrl, const std::set<std::string>& usableTagStates):
18  MetadataProvider(usableTagStates), m_baseUrl(std::move(baseUrl))
19  {
20  // We want to be sure on construction that the server is working. So let's
21  // just check the list of valid states
22  const auto result = get("/v2/globalTagStatus");
23  // check list of valid states
24  auto validStates = getUsableTagStates();
25  std::string invalidStates = "";
26  for (const auto& info : result) {
27  const std::string status = info.at("name");
28  if (not validStates.erase(status)) {
29  if (!invalidStates.empty()) invalidStates += ", ";
30  invalidStates += status;
31  }
32  }
33  B2INFO("Conditions Database: found working server" << LogVar("url", m_baseUrl));
34  B2DEBUG(31, "Conditions Database: unusable globaltag states: " << invalidStates);
35  for (const auto& status : validStates) {
36  B2WARNING("Conditions Database: status marked as usable for global tags is not known to the database"
37  << LogVar("status", status));
38 
39  }
40  }
41 
42  json CentralMetadataProvider::get(const std::string& url)
43  {
44  std::stringstream stream;
45  const auto fullUrl = m_downloader.joinWithSlash(m_baseUrl, url);
46  m_downloader.download(fullUrl, stream);
47  stream.clear();
48  stream.seekg(0, std::ios::beg);
49  return json::parse(stream);
50  }
51 
52  std::string CentralMetadataProvider::getGlobaltagStatus(const std::string& name)
53  {
54  auto escaped = m_downloader.escapeString(name);
55  const std::string url = "/v2/globalTag/" + escaped;
56  try {
57  const auto gtinfo = get(url);
58  return gtinfo.at("globalTagStatus").at("name");
59  } catch (std::runtime_error& e) {
60  B2ERROR("Conditions Database: Cannot download information on global tag. Usually this means it "
61  "doesn't exist and you misspelled the name"
62  << LogVar("server url", m_baseUrl) << LogVar("globaltag", name));
63  } catch (std::exception& e) {
64  B2ERROR("Conditions Database: Problem determining global tag status"
65  << LogVar("server url", m_baseUrl) << LogVar("globaltag", name) << LogVar("error", e.what()));
66  }
67  return "";
68  }
69 
70  bool CentralMetadataProvider::updatePayloads(const std::string& globaltag, int exp, int run)
71  {
72  auto escaped = m_downloader.escapeString(globaltag);
73  const std::string url = "v2/iovPayloads/?gtName=" + escaped +
74  "&expNumber=" + std::to_string(exp) +
75  "&runNumber=" + std::to_string(run);
76  try {
77  const auto payloads = get(url);
78  if (!payloads.is_array()) throw std::runtime_error("expected array");
79  for (const auto& info : payloads) {
80  if (!info.is_object()) throw std::runtime_error("excpected payload object");
81  const auto& payload = info.at("payload");
82  const auto& iov = info.at("payloadIov");
84  payload.at("basf2Module").at("name"),
85  globaltag,
86  payload.at("payloadUrl"),
87  payload.at("baseUrl"),
88  payload.at("checksum"),
89  iov.at("expStart"), iov.at("runStart"), iov.at("expEnd"), iov.at("runEnd"),
90  payload.at("revision")
91  ));
92  }
93  } catch (std::exception& e) {
94  B2ERROR("Conditions Database: Problem parsing payload information."
95  << LogVar("globaltag", globaltag) << LogVar("server url", m_baseUrl) << LogVar("error", e.what()));
96  return false;
97  }
98  return true;
99  }
100 } // Belle2::Conditions namespace
Belle2::Conditions::MetadataProvider
Base class for a payload metadata provider.
Definition: MetadataProvider.h:37
Belle2::Conditions::CentralMetadataProvider::updatePayloads
bool updatePayloads(const std::string &globaltag, int exp, int run) override
Update the list of known payloads for the given globaltag/exp/run.
Definition: CentralMetadataProvider.cc:70
Belle2::Conditions::Downloader::download
bool download(const std::string &url, std::ostream &stream, bool silentOnMissing=false)
get an url and save the content to stream This function raises exceptions when there are any problems
Definition: Downloader.cc:268
Belle2::Conditions::CentralMetadataProvider::get
nlohmann::json get(const std::string &url)
Downlad a given relative url (the baseUrl will be prependend) and return the json description.
Definition: CentralMetadataProvider.cc:42
Belle2::Conditions::MetadataProvider::addPayload
void addPayload(PayloadMetadata &&payload, const std::string &messagePrefix="")
Add a payload information to the internal list.
Definition: MetadataProvider.cc:101
Belle2::Conditions::Downloader::escapeString
std::string escapeString(const std::string &text)
Escape a string to make it safe to be used in web requests.
Definition: Downloader.cc:151
Belle2::Conditions::Downloader::joinWithSlash
std::string joinWithSlash(const std::string &base, const std::string &second)
Join two strings and make sure that there is exactly one '/' between them.
Definition: Downloader.cc:165
Belle2::Conditions::CentralMetadataProvider::getGlobaltagStatus
std::string getGlobaltagStatus(const std::string &name) override
Check the status of a given globaltag .
Definition: CentralMetadataProvider.cc:52
Belle2::Conditions::MetadataProvider::getUsableTagStates
std::set< std::string > getUsableTagStates()
Get the valid tag states when checking globaltag status.
Definition: MetadataProvider.h:140
LogVar
Class to store variables with their name which were sent to the logging service.
Definition: LogVariableStream.h:24
Belle2::Conditions::CentralMetadataProvider::m_baseUrl
std::string m_baseUrl
base url of the server
Definition: CentralMetadataProvider.h:58
Belle2::Conditions::PayloadMetadata
Simple struct to group all information necessary for a single payload.
Definition: PayloadMetadata.h:25
Belle2::Conditions::CentralMetadataProvider::CentralMetadataProvider
CentralMetadataProvider(std::string baseUrl, const std::set< std::string > &usableTagStates)
Create using a base rest url to find the server endpoints.
Definition: CentralMetadataProvider.cc:17
Belle2::Conditions::CentralMetadataProvider::m_downloader
Downloader & m_downloader
Reference to the downloader instance for convenience.
Definition: CentralMetadataProvider.h:56