Belle II Software  release-08-01-10
CentralMetadataProvider Class Reference

Class to obtain payload metadata from the central database server via REST requests. More...

#include <CentralMetadataProvider.h>

Inheritance diagram for CentralMetadataProvider:
Collaboration diagram for CentralMetadataProvider:

Public Types

using PayloadMap = std::unordered_map< std::string, PayloadMetadata >
 Type for the internal list of payloads.
 

Public Member Functions

 CentralMetadataProvider (std::string baseUrl, const std::set< std::string > &usableTagStates)
 Create using a base rest url to find the server endpoints.
 
virtual ~CentralMetadataProvider ()=default
 default destructor
 
std::string getGlobaltagStatus (const std::string &name) override
 Check the status of a given globaltag . More...
 
bool updatePayloads (const std::string &globaltag, int exp, int run) override
 Update the list of known payloads for the given globaltag/exp/run. More...
 
bool setTags (const std::vector< std::string > &tags)
 Set the list of globaltag names to be considered for payloads. More...
 
bool getPayloads (int exp, int run, std::vector< PayloadMetadata > &info)
 Update the information in the vector of metadata instances with the actual values. More...
 
std::set< std::string > getUsableTagStates ()
 Get the valid tag states when checking globaltag status.
 
void setUsableTagStates (const std::set< std::string > &states)
 Set the valid tag states for this provider when checking globaltag status. More...
 

Protected Member Functions

void addPayload (PayloadMetadata &&payload, const std::string &messagePrefix="")
 Add a payload information to the internal list. More...
 
void printInfoMessage (const std::string &provider)
 Print an INFO message about the used metadata provider. More...
 

Protected Attributes

std::vector< std::string > m_tags
 List of globaltags to consider.
 
std::unordered_map< std::string, PayloadMetadataCachem_cache
 Map of globaltag->known metadata.
 
PayloadMapm_payloads {nullptr}
 Map of known payloads for current conditions.
 
std::set< std::string > m_usableTagStates {"TESTING", "VALIDATED", "RUNNING", "PUBLISHED"}
 Set of global tag states to consider valid (except for 'INVALID' which is always considered invalid)
 

Private Member Functions

nlohmann::json get (const std::string &url)
 Downlad a given relative url (the baseUrl will be prependend) and return the json description. More...
 

Private Attributes

Downloaderm_downloader {Downloader::getDefaultInstance()}
 Reference to the downloader instance for convenience.
 
std::string m_baseUrl
 base url of the server
 

Detailed Description

Class to obtain payload metadata from the central database server via REST requests.

Definition at line 16 of file CentralMetadataProvider.h.

Member Function Documentation

◆ addPayload()

void addPayload ( PayloadMetadata &&  payload,
const std::string &  messagePrefix = "" 
)
protectedinherited

Add a payload information to the internal list.

This should be called by implementations during updatePayloads() for each payload found.

Parameters
payloadpayload information filled from the globaltag
messagePrefixa message prefix to be shown for possible log messages to indicate the correct metadata provider

Definition at line 91 of file MetadataProvider.cc.

92  {
93  const auto [it, inserted] = m_payloads->emplace(payload.name, payload);
94  if (!inserted) {
95  auto& existing = it->second;
96  if (existing.revision < payload.revision) {
97  std::swap(existing, payload);
98  }
99  B2DEBUG(36, messagePrefix << (messagePrefix.empty() ? "" : ": ") << "Found duplicate payload. Discarding one of them"
100  << LogVar("globaltag", existing.globaltag)
101  << LogVar("name", existing.name)
102  << LogVar("revision", existing.revision)
103  << LogVar("checksum", existing.checksum)
104  << LogVar("discarded revision", payload.revision));
105  } else {
106  B2DEBUG(37, messagePrefix << (messagePrefix.empty() ? "" : ": ") << "Found payload"
107  << LogVar("globaltag", payload.globaltag)
108  << LogVar("name", payload.name)
109  << LogVar("revision", payload.revision)
110  << LogVar("checksum", payload.checksum));
111  }
112  }
PayloadMap * m_payloads
Map of known payloads for current conditions.
Class to store variables with their name which were sent to the logging service.

◆ get()

json get ( const std::string &  url)
private

Downlad a given relative url (the baseUrl will be prependend) and return the json description.

Will raise exceptions on error

Definition at line 40 of file CentralMetadataProvider.cc.

41  {
42  std::stringstream stream;
43  const auto fullUrl = m_downloader.joinWithSlash(m_baseUrl, url);
44  m_downloader.download(fullUrl, stream);
45  stream.clear();
46  stream.seekg(0, std::ios::beg);
47  return json::parse(stream);
48  }
Downloader & m_downloader
Reference to the downloader instance for convenience.
std::string m_baseUrl
base url of the server
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:258
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:155

◆ getGlobaltagStatus()

std::string getGlobaltagStatus ( const std::string &  name)
overridevirtual

Check the status of a given globaltag .

Returns
the status of the globaltag (like "OPEN", "INVALID", "PUBLISHED"). Returns empty string on any errors or if the tag doesn't exist.

Implements MetadataProvider.

Definition at line 50 of file CentralMetadataProvider.cc.

◆ getPayloads()

bool getPayloads ( int  exp,
int  run,
std::vector< PayloadMetadata > &  info 
)
inherited

Update the information in the vector of metadata instances with the actual values.

The input is a list of metadata instances and the metadata provider will try to modify them in place and add all the missing information it can find.

  • It will try to find iov, revision, checksum, url for each entry which doesn't have a valid revision number (revision=0)
  • It will ignore any entries with a valid revision number (revision>0)
  • any payload which cannot be found in any globaltag will raise an error as long as required=true
  • the modified list can still contain payloads without a valid revision some payloads could not be found.

This function will call updatePayloads() if necessary to update the list of known payloads for a given run but it will try to cache these results so incrementally asking for new payloads as they get requested is perfectly fine.

updatePayloads() will only called for globaltags if we still need to find payload information. If all payloads are found no further queries will be made so adding additional globaltags that are not used at the end is not expensive.

Parameters
expthe experiment number
runthe run number
[in,out]infoa list of metadata instances which we try to find the missing metadata for.
Returns
true if all required payloads could be found, false if at least one payload which still didn't have a valid revision number and is required could not be found.

Definition at line 36 of file MetadataProvider.cc.

◆ printInfoMessage()

void printInfoMessage ( const std::string &  provider = "")
protectedinherited

Print an INFO message about the used metadata provider.

Parameters
providerthe actual metadata provider used for the job

Definition at line 114 of file MetadataProvider.cc.

◆ setTags()

bool setTags ( const std::vector< std::string > &  tags)
inherited

Set the list of globaltag names to be considered for payloads.

This should be called before requesting any payloads but after calling setValidTagStates().

Returns
true if all globaltags can be found and are in a valid state

Definition at line 15 of file MetadataProvider.cc.

◆ setUsableTagStates()

void setUsableTagStates ( const std::set< std::string > &  states)
inlineinherited

Set the valid tag states for this provider when checking globaltag status.

Should be called before setTags() if necessary.

Warning
The state "INVALID" will never be accepted and removed from the given set if present

Definition at line 128 of file MetadataProvider.h.

129  {
130  m_usableTagStates = states;
131  m_usableTagStates.erase("INVALID");
132  }
std::set< std::string > m_usableTagStates
Set of global tag states to consider valid (except for 'INVALID' which is always considered invalid)

◆ updatePayloads()

bool updatePayloads ( const std::string &  globaltag,
int  exp,
int  run 
)
overridevirtual

Update the list of known payloads for the given globaltag/exp/run.

This basically just calls addPayload() for each payload it finds in the globaltag for the given exp/run.

Returns
true on sucess, false on any error

Implements MetadataProvider.

Definition at line 68 of file CentralMetadataProvider.cc.


The documentation for this class was generated from the following files: