Skip to content

Commit

Permalink
feat: better control over RCDB URL choice
Browse files Browse the repository at this point in the history
  • Loading branch information
c-dilks committed Oct 7, 2024
1 parent e84c0a4 commit 6c022d9
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace iguana::physics {
i_qE = result_schema.getEntryOrder("qE");

// instantiate RCDB reader
m_rcdb = std::make_unique<RCDBReader>("RCDB|" + GetName());
m_rcdb = std::make_unique<RCDBReader>("RCDB|" + GetName(), m_log->GetLevel());

// parse config file
ParseYAMLConfig();
Expand Down
1 change: 1 addition & 0 deletions src/iguana/services/GlobalParam.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace iguana {

// default param values
GlobalParam<std::string> GlobalConcurrencyModel{"none"};
GlobalParam<std::string> GlobalRcdbUrl{""};

// template specializations
template class GlobalParam<std::string>;
Expand Down
8 changes: 8 additions & 0 deletions src/iguana/services/GlobalParam.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,12 @@ namespace iguana {
/// option will be _chosen_ by `ConcurrentParamFactory::Create` instead
extern GlobalParam<std::string> GlobalConcurrencyModel;

/// @brief RCDB URL
/// @par Notes
/// The RCDB will check for a URL in the following order:
/// - This parameter, `iguana::GlobalRcdbUrl`; by default it is not set to any value
/// - The environment variable `RCDB_CONNECTION` (which is likely set if you are on `ifarm`)
/// - A default URL, which will be printed in a warning; see `iguana::RCDBReader::m_default_url`
extern GlobalParam<std::string> GlobalRcdbUrl;

}
4 changes: 2 additions & 2 deletions src/iguana/services/Object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace iguana {

Object::Object(std::string_view name)
Object::Object(std::string_view name, Logger::Level lev)
: m_name(name)
, m_log(std::make_unique<Logger>(m_name))
, m_log(std::make_unique<Logger>(m_name, lev))
{}

std::unique_ptr<Logger>& Object::Log()
Expand Down
3 changes: 2 additions & 1 deletion src/iguana/services/Object.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ namespace iguana {
public:

/// @param name the name of this object
Object(std::string_view name = "");
/// @param lev the log level
Object(std::string_view name = "", Logger::Level lev = Logger::DEFAULT_LEVEL);
~Object() {}

/// Get the logger
Expand Down
28 changes: 24 additions & 4 deletions src/iguana/services/RCDBReader.cc
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
#include "RCDBReader.h"
#include "GlobalParam.h"

namespace iguana {

RCDBReader::RCDBReader(std::string_view name) : Object(name)
RCDBReader::RCDBReader(std::string_view name, Logger::Level lev) : Object(name, lev)
{
#ifdef USE_RCDB
auto url_ptr = std::getenv("RCDB_CONNECTION");
m_url = url_ptr != nullptr ? std::string(url_ptr) : default_url;
m_log->Debug("RCDB URL: {}", m_url);

// choose the RCDB URL, from the following priority ordering
// 1. try `GlobalRcdbUrl`
m_url = GlobalRcdbUrl();
if(!m_url.empty())
m_log->Debug("RCDB URL set from 'iguana::GlobalRcdbUrl': {:?}", m_url);
else {
// 2. try env var `RCDB_CONNECTION`
if(auto url_ptr{std::getenv("RCDB_CONNECTION")}; url_ptr != nullptr)
m_url = std::string(url_ptr);
if(!m_url.empty())
m_log->Debug("RCDB URL set from env var 'RCDB_CONNECTION': {:?}", m_url);
else {
// 3. fallback to default value
m_log->Warn("RCDB URL not set; you may choose a URL with the environment variable 'RCDB_CONNECTION' or with the global parameter 'iguana::GlobalRcdbUrl'; for now, let's proceed with the URL set to {:?}", m_default_url);
m_url = m_default_url;
m_log->Debug("RCDB URL set from default fallback: {:?}", m_url);
}
}

// then start the connection
m_rcdb_connection = std::make_unique<rcdb::Connection>(m_url, true);

#endif
}

Expand Down
7 changes: 5 additions & 2 deletions src/iguana/services/RCDBReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@ namespace iguana {
public:

/// @param name the name of this reader
RCDBReader(std::string_view name = "rcdb");
/// @param lev the log level
RCDBReader(std::string_view name = "rcdb", Logger::Level lev = Logger::DEFAULT_LEVEL);

/// @param runnum run number
/// @returns the beam energy in GeV
double GetBeamEnergy(int const runnum);

private:

/// @brief default RCDB URL, used as a last resort
std::string const m_default_url = "mysql://[email protected]/rcdb";

std::string m_url;
std::string const default_url = "mysql://[email protected]/rcdb";
std::once_flag m_error_once;

#ifdef USE_RCDB
Expand Down

0 comments on commit 6c022d9

Please sign in to comment.