10 #include <openssl/sha.h>
14 #include "RelationalAccess/ITransaction.h"
15 #include "RelationalAccess/ISessionProxy.h"
16 #include "RelationalAccess/ISchema.h"
17 #include "RelationalAccess/ITable.h"
18 #include "RelationalAccess/IQuery.h"
19 #include "RelationalAccess/ICursor.h"
20 #include "RelationalAccess/ITableDataEditor.h"
21 #include "RelationalAccess/TableDescription.h"
22 #include "CoralBase/Attribute.h"
23 #include "CoralBase/AttributeList.h"
24 #include "CoralBase/TimeStamp.h"
28 template <
typename SiStripPayload>
57 template <
typename SiStripPayload>
60 m_cfgMapSchemaName(iConfig.getUntrackedParameter<
std::
string>(
"cfgMapSchemaName",
"CMS_COND_O2O")),
61 m_cfgMapTableName(iConfig.getUntrackedParameter<
std::
string>(
"cfgMapTableName",
"STRIP_CONFIG_TO_PAYLOAD_MAP")),
62 m_condDb(iConfig.getParameter<
std::
string>(
"conditionDatabase")),
63 m_localCondDbFile(iConfig.getParameter<
std::
string>(
"condDbFile")),
64 m_targetTag(iConfig.getParameter<
std::
string>(
"targetTag")),
65 m_since(iConfig.getParameter<uint32_t>(
"since")),
66 p_type(
cond::demangledName(typeid(SiStripPayload))),
67 p_cfgstr(condObjBuilder->getConfigString(typeid(SiStripPayload))) {
68 if (iConfig.
exists(
"configMapDatabase"))
70 if (iConfig.
exists(
"cfgMapDbFile"))
76 template <
typename SiStripPayload>
79 template <
typename SiStripPayload>
89 std::shared_ptr<SiStripPayload> payloadToUpload;
93 std::string mappedPayloadHash = queryConfigMap(configHash);
94 bool mapUpToDate =
false;
96 if (!mappedPayloadHash.empty()) {
100 edm::LogInfo(
"SiStripPayloadHandler") <<
"[SiStripPayloadHandler::" << __func__ <<
"] "
101 <<
"Try retrieving payload from " << m_condDb;
102 payloadToUpload = condDbSession.
fetchPayload<SiStripPayload>(mappedPayloadHash);
103 std::cout <<
"@@@[FastO2O:true]@@@" << std::endl;
104 edm::LogInfo(
"SiStripPayloadHandler") <<
"[SiStripPayloadHandler::" << __func__ <<
"] "
105 <<
" ... Payload is copied from offline condition database.";
108 edm::LogInfo(
"SiStripPayloadHandler") <<
"[SiStripPayloadHandler::" << __func__ <<
"] "
109 <<
"NO mapping payload hash found. Will run the long O2O. ";
110 SiStripPayload*
obj =
nullptr;
115 condObjBuilder->getValue(
obj);
116 payloadToUpload = std::shared_ptr<SiStripPayload>(
obj);
117 std::cout <<
"@@@[FastO2O:false]@@@" << std::endl;
118 edm::LogInfo(
"SiStripPayloadHandler") <<
"[SiStripPayloadHandler::" << __func__ <<
"] "
119 <<
" ... New payload has been created.";
124 edm::LogInfo(
"SiStripPayloadHandler") <<
"[SiStripPayloadHandler::" << __func__ <<
"] "
125 <<
"Write payload to local sqlite file: " << m_localCondDbFile;
133 iovEditor.
insert(m_since, thePayloadHash);
136 edm::LogInfo(
"SiStripPayloadHandler") <<
"[SiStripPayloadHandler::" << __func__ <<
"] "
137 <<
"Payload " << thePayloadHash <<
" inserted to sqlite with IOV " << m_since;
141 updateConfigMap(configHash, thePayloadHash);
145 if (last_hash == thePayloadHash) {
146 std::cout <<
"@@@[PayloadChange:false]@@@" << last_hash << std::endl;
148 std::cout <<
"@@@[PayloadChange:true]@@@" << last_hash <<
" -> " << thePayloadHash << std::endl;
152 template <
typename SiStripPayload>
155 template <
typename SiStripPayload>
157 edm::LogInfo(
"SiStripPayloadHandler") <<
"[SiStripPayloadHandler::" << __func__ <<
"] "
158 <<
"Convert config string to SHA-1 hash for " << p_type
159 <<
"\n... config: " << p_cfgstr;
164 if (!SHA1_Init(&ctx)) {
167 if (!SHA1_Update(&ctx, p_type.c_str(), p_type.size())) {
170 if (!SHA1_Update(&ctx, p_cfgstr.c_str(), p_cfgstr.size())) {
173 unsigned char hash[SHA_DIGEST_LENGTH];
174 if (!SHA1_Final(
hash, &ctx)) {
178 char tmp[SHA_DIGEST_LENGTH * 2 + 1];
180 for (
unsigned int i = 0;
i < SHA_DIGEST_LENGTH;
i++) {
183 tmp[SHA_DIGEST_LENGTH * 2] = 0;
185 edm::LogInfo(
"SiStripPayloadHandler") <<
"[SiStripPayloadHandler::" << __func__
192 template <
typename SiStripPayload>
194 if (m_configMapDb.empty())
197 edm::LogInfo(
"SiStripPayloadHandler") <<
"[SiStripPayloadHandler::" << __func__ <<
"] "
198 <<
"Query " << m_configMapDb <<
" to see if the payload is already in DB.";
200 auto cmDbSession = m_connectionPool.createCoralSession(m_configMapDb);
202 cmDbSession->transaction().start(
true);
203 coral::ITable& cmTable = cmDbSession->schema(m_cfgMapSchemaName).tableHandle(m_cfgMapTableName);
204 std::unique_ptr<coral::IQuery>
query(cmTable.newQuery());
205 query->addToOutputList(
"PAYLOAD_HASH");
206 query->defineOutputType(
"PAYLOAD_HASH", coral::AttributeSpecification::typeNameForType<std::string>());
208 query->addToOutputList(
"PAYLOAD_TYPE");
209 query->addToOutputList(
"CONFIG_STRING");
210 query->addToOutputList(
"INSERTION_TIME");
211 std::string whereClause(
"CONFIG_HASH = :CONFIG_HASH");
212 coral::AttributeList whereData;
214 whereData.begin()->data<
std::string>() = configHash;
215 query->setCondition(whereClause, whereData);
216 coral::ICursor& cursor =
query->execute();
220 p_hash = cursor.currentRow()[
"PAYLOAD_HASH"].data<
std::string>();
222 <<
"[SiStripPayloadHandler::" << __func__ <<
"] "
223 <<
"Found associated payload hash " << p_hash
224 <<
"\n... type=" << cursor.currentRow()[
"PAYLOAD_TYPE"].data<
std::string>()
225 <<
"\n... config=" << cursor.currentRow()[
"CONFIG_STRING"].data<
std::string>()
226 <<
"\n... insertion_time=" << cursor.currentRow()[
"INSERTION_TIME"].data<coral::TimeStamp>().
toString();
228 cmDbSession->transaction().commit();
233 template <
typename SiStripPayload>
235 if (m_cfgMapDbFile.empty())
238 edm::LogInfo(
"SiStripPayloadHandler") <<
"[SiStripPayloadHandler::" << __func__ <<
"] "
239 <<
"Updating the config to payload hash map to " << m_cfgMapDbFile;
242 auto cmSQLiteSession = m_connectionPool.createCoralSession(m_cfgMapDbFile,
true);
243 cmSQLiteSession->transaction().start(
false);
245 if (!cmSQLiteSession->nominalSchema().existsTable(m_cfgMapTableName)) {
247 coral::TableDescription mapTable;
248 mapTable.setName(m_cfgMapTableName);
249 mapTable.insertColumn(
"CONFIG_HASH", coral::AttributeSpecification::typeNameForType<std::string>());
250 mapTable.insertColumn(
"PAYLOAD_HASH", coral::AttributeSpecification::typeNameForType<std::string>());
251 mapTable.insertColumn(
"PAYLOAD_TYPE", coral::AttributeSpecification::typeNameForType<std::string>());
252 mapTable.insertColumn(
"CONFIG_STRING", coral::AttributeSpecification::typeNameForType<std::string>());
253 mapTable.insertColumn(
"INSERTION_TIME", coral::AttributeSpecification::typeNameForType<coral::TimeStamp>());
254 mapTable.setPrimaryKey(
"CONFIG_HASH");
255 mapTable.setNotNullConstraint(
"CONFIG_HASH");
256 mapTable.setNotNullConstraint(
"PAYLOAD_HASH");
257 mapTable.setNotNullConstraint(
"PAYLOAD_TYPE");
258 mapTable.setNotNullConstraint(
"CONFIG_STRING");
259 mapTable.setNotNullConstraint(
"INSERTION_TIME");
260 cmSQLiteSession->nominalSchema().createTable(mapTable);
263 coral::ITable& cmTable = cmSQLiteSession->nominalSchema().tableHandle(m_cfgMapTableName);
264 coral::AttributeList insertData;
270 insertData.extend<coral::TimeStamp>(
"INSERTION_TIME");
271 insertData[
"CONFIG_HASH"].data<
std::string>() = configHash;
272 insertData[
"PAYLOAD_HASH"].data<
std::string>() = payloadHash;
273 insertData[
"PAYLOAD_TYPE"].data<
std::string>() = p_type;
274 insertData[
"CONFIG_STRING"].data<
std::string>() = p_cfgstr;
276 cmTable.dataEditor().insertRow(insertData);
277 cmSQLiteSession->transaction().commit();
278 edm::LogInfo(
"SiStripPayloadHandler") <<
"[SiStripPayloadHandler::" << __func__ <<
"] "
279 <<
"Updated with mapping (configHash : payloadHash)" << configHash <<
" : "