File indexing completed on 2024-04-06 12:01:26
0001 #include "CondCore/CondDB/interface/ConnectionPool.h"
0002 #include "DbConnectionString.h"
0003 #include "IDbAuthentication.h"
0004 #include "SessionImpl.h"
0005 #include "IOVSchema.h"
0006 #include "CoralMsgReporter.h"
0007
0008 #include "CondCore/CondDB/interface/CoralServiceManager.h"
0009 #include "CondCore/CondDB/interface/Auth.h"
0010
0011 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0012
0013 #include "RelationalAccess/ConnectionService.h"
0014 #include "RelationalAccess/IAuthenticationService.h"
0015 #include "RelationalAccess/IWebCacheControl.h"
0016 #include "RelationalAccess/ISessionProxy.h"
0017 #include "RelationalAccess/IConnectionServiceConfiguration.h"
0018 #include "CoralKernel/Context.h"
0019 #include "CoralKernel/IProperty.h"
0020 #include "CoralKernel/IPropertyManager.h"
0021
0022 namespace cond {
0023
0024 namespace persistency {
0025
0026 ConnectionPool::ConnectionPool() {
0027 m_pluginManager = new cond::CoralServiceManager;
0028 m_msgReporter = new CoralMsgReporter;
0029 coral::MessageStream::installMsgReporter(m_msgReporter);
0030 configure();
0031 }
0032
0033 ConnectionPool::~ConnectionPool() { delete m_pluginManager; }
0034
0035 void ConnectionPool::setAuthenticationPath(const std::string& p) { m_authPath = p; }
0036
0037 void ConnectionPool::setAuthenticationSystem(int authSysCode) { m_authSys = authSysCode; }
0038
0039 void ConnectionPool::setFrontierSecurity(const std::string& signature) { m_frontierSecurity = signature; }
0040
0041 void ConnectionPool::setLogging(bool flag) { m_loggingEnabled = flag; }
0042
0043 void ConnectionPool::setParameters(const edm::ParameterSet& connectionPset) {
0044
0045
0046
0047 setAuthenticationPath(connectionPset.getUntrackedParameter<std::string>("authenticationPath", m_authPath));
0048 setAuthenticationSystem(connectionPset.getUntrackedParameter<int>("authenticationSystem", m_authSys));
0049 setFrontierSecurity(connectionPset.getUntrackedParameter<std::string>("security", m_frontierSecurity));
0050 int messageLevel =
0051 connectionPset.getUntrackedParameter<int>("messageLevel", 0);
0052 coral::MsgLevel level = m_messageLevel;
0053 switch (messageLevel) {
0054 case 0:
0055 level = coral::Error;
0056 break;
0057 case 1:
0058 level = coral::Warning;
0059 break;
0060 case 2:
0061 level = coral::Info;
0062 break;
0063 case 3:
0064 level = coral::Debug;
0065 break;
0066 default:
0067 level = coral::Error;
0068 }
0069 setMessageVerbosity(level);
0070 setConnectionTimeout(connectionPset.getUntrackedParameter<int>("connectionTimeout", m_connectionTimeout));
0071 setLogging(connectionPset.getUntrackedParameter<bool>("logging", m_loggingEnabled));
0072 }
0073
0074 bool ConnectionPool::isLoggingEnabled() const { return m_loggingEnabled; }
0075
0076 void ConnectionPool::configure(coral::IConnectionServiceConfiguration& coralConfig) {
0077 coralConfig.disablePoolAutomaticCleanUp();
0078 coralConfig.disableConnectionSharing();
0079 coralConfig.setConnectionTimeOut(m_connectionTimeout);
0080
0081 coral::MessageStream::setMsgVerbosity(m_messageLevel);
0082 m_msgReporter->setOutputLevel(m_messageLevel);
0083
0084
0085 m_authenticationService = std::string("CORAL/Services/EnvironmentAuthenticationService");
0086 std::string authPath = m_authPath;
0087
0088 if (authPath.empty()) {
0089
0090 const char* authEnv = std::getenv(cond::auth::COND_AUTH_PATH);
0091 if (authEnv) {
0092 authPath += authEnv;
0093 }
0094 }
0095 int authSys = m_authSys;
0096
0097 const char* authSysEnv = std::getenv(cond::auth::COND_AUTH_SYS);
0098 if (authSysEnv) {
0099 authSys = ::atoi(authSysEnv);
0100 }
0101 if (authSys != CondDbKey && authSys != CoralXMLFile) {
0102
0103 authSys = CondDbKey;
0104 }
0105 std::string servName("");
0106 if (authSys == CondDbKey) {
0107 if (authPath.empty()) {
0108 const char* authEnv = std::getenv("HOME");
0109 if (authEnv) {
0110 authPath += authEnv;
0111 }
0112 }
0113 servName = "COND/Services/RelationalAuthenticationService";
0114 } else if (authSys == CoralXMLFile) {
0115 if (authPath.empty()) {
0116 authPath = ".";
0117 }
0118 servName = "COND/Services/XMLAuthenticationService";
0119 }
0120 if (!authPath.empty()) {
0121 m_authenticationService = servName;
0122 coral::Context::instance().PropertyManager().property(cond::auth::COND_AUTH_PATH_PROPERTY)->set(authPath);
0123 coral::Context::instance().loadComponent(m_authenticationService, m_pluginManager);
0124 }
0125
0126 coralConfig.setAuthenticationService(m_authenticationService);
0127 }
0128
0129 void ConnectionPool::configure() {
0130 coral::ConnectionService connServ;
0131 configure(connServ.configuration());
0132 }
0133
0134 std::shared_ptr<coral::ISessionProxy> ConnectionPool::createCoralSession(const std::string& connectionString,
0135 const std::string& transactionId,
0136 bool writeCapable) {
0137 coral::ConnectionService connServ;
0138
0139 std::pair<std::string, std::string> fullConnectionPars =
0140 getConnectionParams(connectionString, transactionId, m_frontierSecurity);
0141 if (!fullConnectionPars.second.empty()) {
0142
0143 connServ.webCacheControl().setTableTimeToLive(fullConnectionPars.second, TAG::tname, 1);
0144 connServ.webCacheControl().setTableTimeToLive(fullConnectionPars.second, IOV::tname, 1);
0145 connServ.webCacheControl().setTableTimeToLive(fullConnectionPars.second, PAYLOAD::tname, 3);
0146 }
0147
0148 return std::shared_ptr<coral::ISessionProxy>(
0149 connServ.connect(fullConnectionPars.first,
0150 writeCapable ? auth::COND_WRITER_ROLE : auth::COND_READER_ROLE,
0151 writeCapable ? coral::Update : coral::ReadOnly));
0152 }
0153
0154 Session ConnectionPool::createSession(const std::string& connectionString,
0155 const std::string& transactionId,
0156 bool writeCapable) {
0157 std::shared_ptr<coral::ISessionProxy> coralSession =
0158 createCoralSession(connectionString, transactionId, writeCapable);
0159
0160 std::string principalName("");
0161 if (!m_authenticationService.empty()) {
0162
0163 if (m_authenticationService == "COND/Services/RelationalAuthenticationService") {
0164 coral::IHandle<coral::IAuthenticationService> authSvc =
0165 coral::Context::instance().query<coral::IAuthenticationService>(m_authenticationService);
0166 IDbAuthentication* dbAuth = dynamic_cast<IDbAuthentication*>(authSvc.get());
0167 principalName = dbAuth->principalName();
0168 }
0169 }
0170
0171 return Session(std::make_shared<SessionImpl>(coralSession, connectionString, principalName));
0172 }
0173
0174 Session ConnectionPool::createSession(const std::string& connectionString, bool writeCapable) {
0175 return createSession(connectionString, "", writeCapable);
0176 }
0177
0178 Session ConnectionPool::createReadOnlySession(const std::string& connectionString,
0179 const std::string& transactionId) {
0180 return createSession(connectionString, transactionId);
0181 }
0182
0183 std::shared_ptr<coral::ISessionProxy> ConnectionPool::createCoralSession(const std::string& connectionString,
0184 bool writeCapable) {
0185 return createCoralSession(connectionString, "", writeCapable);
0186 }
0187
0188 void ConnectionPool::setMessageVerbosity(coral::MsgLevel level) { m_messageLevel = level; }
0189
0190 void ConnectionPool::setConnectionTimeout(int seconds) { m_connectionTimeout = seconds; }
0191
0192 void ConnectionPool::setLogDestination(Logger& logger) { m_msgReporter->subscribe(logger); }
0193
0194 }
0195 }