|
| 1 | +#include "CondCore/CondDB/interface/ConnectionPool.h" |
| 2 | +#include "CondCore/PopCon/interface/OnlinePopCon.h" |
| 3 | +#include "FWCore/MessageLogger/interface/MessageLogger.h" |
| 4 | +#include "FWCore/ParameterSet/interface/ParameterSet.h" |
| 5 | + |
| 6 | +//#include <iostream> |
| 7 | + |
| 8 | +namespace popcon { |
| 9 | + |
| 10 | + constexpr const char* const OnlinePopCon::s_version; |
| 11 | + |
| 12 | + OnlinePopCon::OnlinePopCon(const edm::ParameterSet& pset) |
| 13 | + : m_targetSession(), |
| 14 | + m_targetConnectionString(pset.getUntrackedParameter<std::string>("targetDBConnectionString", "")), |
| 15 | + m_authPath(pset.getUntrackedParameter<std::string>("authenticationPath", "")), |
| 16 | + m_authSys(pset.getUntrackedParameter<int>("authenticationSystem", 1)), |
| 17 | + m_recordName(pset.getParameter<std::string>("record")), |
| 18 | + m_useLockRecors(pset.getUntrackedParameter<bool>("useLockRecords", false)) { |
| 19 | + edm::LogInfo("OnlinePopCon") |
| 20 | + << "This is OnlinePopCon (Populator of Condition) v" << s_version << ".\n" |
| 21 | + << "Please report any problem and feature request through the JIRA project CMSCONDDB.\n"; |
| 22 | + } |
| 23 | + |
| 24 | + OnlinePopCon::~OnlinePopCon() { |
| 25 | + if (!m_targetConnectionString.empty()) { |
| 26 | + m_targetSession.transaction().commit(); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + cond::persistency::Session OnlinePopCon::preparePopCon() { |
| 31 | + // Initialization almost identical to PopCon |
| 32 | + const std::string& connectionStr = m_dbService->session().connectionString(); |
| 33 | + m_dbService->forceInit(); |
| 34 | + std::string tagName = m_dbService->tag(m_recordName); |
| 35 | + m_tagInfo.name = tagName; |
| 36 | + if (m_targetConnectionString.empty()) { |
| 37 | + m_targetSession = m_dbService->session(); |
| 38 | + m_dbService->startTransaction(); |
| 39 | + } else { |
| 40 | + cond::persistency::ConnectionPool connPool; |
| 41 | + connPool.setAuthenticationPath(m_authPath); |
| 42 | + connPool.setAuthenticationSystem(m_authSys); |
| 43 | + connPool.configure(); |
| 44 | + m_targetSession = connPool.createSession(m_targetConnectionString); |
| 45 | + m_targetSession.transaction().start(); |
| 46 | + } |
| 47 | + |
| 48 | + m_dbService->logger().logInfo() << "OnlinePopCon::preparePopCon"; |
| 49 | + m_dbService->logger().logInfo() << " destination DB: " << connectionStr; |
| 50 | + m_dbService->logger().logInfo() << " target DB: " |
| 51 | + << (m_targetConnectionString.empty() ? connectionStr : m_targetConnectionString); |
| 52 | + |
| 53 | + if (m_targetSession.existsDatabase() && m_targetSession.existsIov(tagName)) { |
| 54 | + cond::persistency::IOVProxy iov = m_targetSession.readIov(tagName); |
| 55 | + m_tagInfo.size = iov.sequenceSize(); |
| 56 | + if (m_tagInfo.size > 0) { |
| 57 | + m_tagInfo.lastInterval = iov.getLast(); |
| 58 | + } |
| 59 | + m_dbService->logger().logInfo() << " TAG: " << tagName << ", last since/till: " << m_tagInfo.lastInterval.since |
| 60 | + << "/" << m_tagInfo.lastInterval.till; |
| 61 | + m_dbService->logger().logInfo() << " size: " << m_tagInfo.size; |
| 62 | + } else { |
| 63 | + m_dbService->logger().logInfo() << " TAG: " << tagName << "; First writer to this new tag."; |
| 64 | + } |
| 65 | + return m_targetSession; |
| 66 | + } |
| 67 | + |
| 68 | + cond::persistency::Session OnlinePopCon::initialize() { |
| 69 | + // Check if DB service is available |
| 70 | + if (!m_dbService.isAvailable()) { |
| 71 | + throw Exception("OnlinePopCon", "[initialize] DBService not available"); |
| 72 | + } |
| 73 | + |
| 74 | + // Start DB logging service |
| 75 | + m_dbLoggerReturn_ = 0; |
| 76 | + m_dbService->logger().start(); |
| 77 | + m_dbService->logger().logInfo() << "OnlinePopCon::initialize - begin logging for record: " << m_recordName; |
| 78 | + |
| 79 | + // If requested, lock records |
| 80 | + if (m_useLockRecors) { |
| 81 | + m_dbService->logger().logInfo() << "OnlinePopCon::initialize - locking records"; |
| 82 | + m_dbService->lockRecords(); |
| 83 | + } |
| 84 | + |
| 85 | + // Prepare the rest of PopCon infrastructure |
| 86 | + auto session = preparePopCon(); |
| 87 | + return session; |
| 88 | + } |
| 89 | + |
| 90 | + void OnlinePopCon::finalize() { |
| 91 | + // Check if DB service is available |
| 92 | + if (!m_dbService.isAvailable()) { |
| 93 | + throw Exception("OnlinePopCon", "[finalize] DBService not available"); |
| 94 | + } |
| 95 | + |
| 96 | + // Release locks if previously locked |
| 97 | + if (m_useLockRecors) { |
| 98 | + m_dbService->logger().logInfo() << "OnlinePopCon::finalize - releasing locks"; |
| 99 | + m_dbService->releaseLocks(); |
| 100 | + } |
| 101 | + |
| 102 | + // Finalize PopCon infrastructure |
| 103 | + if (m_targetConnectionString.empty()) { |
| 104 | + m_dbService->commitTransaction(); |
| 105 | + } else { |
| 106 | + m_targetSession.transaction().commit(); |
| 107 | + } |
| 108 | + |
| 109 | + // Stop DB logging service |
| 110 | + m_dbService->logger().logInfo() << "OnlinePopCon::finalize - end logging for record: " << m_recordName; |
| 111 | + m_dbService->logger().end(m_dbLoggerReturn_); |
| 112 | + } |
| 113 | + |
| 114 | +} // namespace popcon |
0 commit comments