8 import CondCore.Utilities.conddb_serialization_metadata
as sm
9 import CondCore.Utilities.credentials
as auth
12 authPathEnvVar =
'COND_AUTH_PATH' 13 prod_db_service = (
'cms_orcon_prod',{
'w':
'cms_orcon_prod/cms_cond_general_w',
'r':'cms_orcon_prod/cms_cond_general_r'})
14 adg_db_service = (
'cms_orcon_adg',{
'r':'cms_orcon_adg/cms_cond_general_r'})
15 dev_db_service = (
'cms_orcoff_prep',{
'w':
'cms_orcoff_prep/cms_cond_general_w',
'r':'cms_orcoff_prep/cms_cond_general_r'})
16 schema_name =
'CMS_CONDITIONS' 18 fmt_str =
"[%(asctime)s] %(levelname)s: %(message)s" 19 logLevel = logging.INFO
20 logFormatter = logging.Formatter(fmt_str)
41 line += (fmt.format( row[ind] )+
' ')
48 hsep += (fmt.format(
'')+
' ')
60 cursor = self.db.cursor()
61 cursor.execute(
'SELECT BOOST_VERSION, CMSSW_VERSION FROM CMSSW_BOOST_MAP');
62 rows = cursor.fetchall()
69 cursor = self.db.cursor()
70 cursor.execute(
'SELECT RUN_NUMBER, RUN_START_TIME, BOOST_VERSION, INSERTION_TIME FROM BOOST_RUN_MAP ORDER BY RUN_NUMBER, INSERTION_TIME')
71 rows = cursor.fetchall()
74 self.boost_run_map.append( (r[0],r[1],r[2],
str(r[3])) )
78 cursor = self.db.cursor()
79 cursor.execute(
'SELECT MIN(RUN_NUMBER) FROM RUN_INFO WHERE RUN_NUMBER >= :RUN',(run,))
80 min_run = cursor.fetchone()[0]
81 cursor.execute(
'SELECT START_TIME FROM RUN_INFO WHERE RUN_NUMBER=:RUN',(min_run,))
82 min_run_time = cursor.fetchone()[0]
83 min_run_ts = calendar.timegm( min_run_time.utctimetuple() ) << 32
84 now = datetime.datetime.utcnow()
85 cursor.execute(
'INSERT INTO BOOST_RUN_MAP ( RUN_NUMBER, RUN_START_TIME, BOOST_VERSION, INSERTION_TIME ) VALUES (:RUN, :RUN_START_T, :BOOST, :TIME)',(run,min_run_ts,boost_version,now) )
88 cursor = self.db.cursor()
89 cursor.execute(
'INSERT INTO CMSSW_BOOST_MAP ( CMSSW_VERSION, BOOST_VERSION ) VALUES ( :CMSSW_VERSION, :BOOST_VERSION )',(cmssw_version,boost_version))
92 cmssw_v = sm.check_cmssw_version( cmssw_version )
95 if sm.is_release_cycle( cmssw_v ):
96 cmssw_v = sm.strip_cmssw_version( cmssw_v )
97 archs = sm.get_production_arch( cmssw_v )
99 path = sm.get_release_root( cmssw_v, arch )
100 if os.path.exists(os.path.join(path,cmssw_v)):
104 if releaseRoot
is None:
107 releaseRoot = sm.get_release_root( cmssw_v, arch )
108 for r
in sorted (os.listdir( releaseRoot )):
109 if r.startswith(cmssw_v):
111 logging.debug(
'Boost version will be verified in release %s' %cmssw_v)
113 if cmssw_v
in self.cmssw_boost_map.keys():
116 if releaseRoot
is None:
117 archs = sm.get_production_arch( cmssw_v )
119 path = sm.get_release_root( cmssw_v, arch )
120 if os.path.exists(os.path.join(path,cmssw_v)):
124 logging.debug(
'Release path: %s' %releaseRoot)
125 boost_version = sm.get_cmssw_boost( the_arch,
'%s/%s' %(releaseRoot,cmssw_v) )
126 if not boost_version
is None:
132 cursor = self.db.cursor()
133 cursor.execute(
'SELECT DISTINCT(RELEASE) FROM GLOBAL_TAG')
134 rows = cursor.fetchall()
144 self.logger.setLevel(logLevel)
145 consoleHandler = logging.StreamHandler(sys.stdout)
146 consoleHandler.setFormatter(logFormatter)
147 self.logger.addHandler(consoleHandler)
152 if self.args.db
is None:
154 if self.args.db ==
'dev' or self.args.db ==
'oradev' :
155 db_service = dev_db_service
156 elif self.args.db ==
'orapro':
157 db_service = adg_db_service
158 elif self.args.db !=
'onlineorapro' or self.args.db !=
'pro':
159 db_service = prod_db_service
161 raise Exception(
"Database '%s' is not known." %args.db )
162 if self.args.accessType
not in db_service[1].
keys():
163 raise Exception(
'The specified database connection %s does not support the requested action.' %db_service[0])
164 service = db_service[1][self.args.accessType]
165 creds = auth.get_credentials( authPathEnvVar, service, self.args.auth )
167 raise Exception(
"Could not find credentials for service %s" %service)
168 (username, account, pwd) = creds
169 connStr =
'%s/%s@%s' %(username,pwd,db_service[0])
170 self.
db = cx_Oracle.connect(connStr)
171 logging.info(
'Connected to %s as user %s' %(db_service[0],username))
172 self.db.current_schema = schema_name
175 if self.
iovs is None:
177 cursor = self.db.cursor()
178 stmt =
'SELECT IOV.SINCE SINCE, IOV.INSERTION_TIME INSERTION_TIME, P.STREAMER_INFO STREAMER_INFO FROM TAG, IOV, PAYLOAD P WHERE TAG.NAME = IOV.TAG_NAME AND P.HASH = IOV.PAYLOAD_HASH AND TAG.NAME = :TAG_NAME' 180 stmt = stmt +
' ORDER BY SINCE' 181 logging.debug(
'Executing: "%s"' %stmt)
182 cursor.execute(stmt,params)
184 streamer_info =
str(r[2].read())
185 self.iovs.append((r[0],r[1],streamer_info))
190 if tagBoostVersion
is not None:
192 for iov
in self.
iovs:
193 if timeCut
is not None:
194 if tagBoostVersion
is not None:
201 iovBoostVersion, tagBoostVersion = sm.update_tag_boost_version( tagBoostVersion, minIov, iov[2], iov[0], timetype, self.version_db.boost_run_map )
202 if minIov
is None or iov[0]<minIov:
204 logging.debug(
'iov: %s - inserted on %s - streamer: %s' %(iov[0],iov[1],iov[2]))
205 logging.debug(
'current tag boost version: %s minIov: %s' %(tagBoostVersion,minIov))
206 if lastBoost
is None or lastBoost!=iovBoostVersion:
207 self.versionIovs.append((iov[0],iovBoostVersion))
208 lastBoost = iovBoostVersion
210 if tagBoostVersion
is None:
212 logging.warning(
'No iovs found. boost version cannot be determined.')
215 logging.error(
'Could not determine the tag boost version.' )
219 logging.info(
'Tag boost version has not changed.')
221 msg =
'Found tag boost version %s ( min iov: %s ) combining payloads from %s iovs' %(tagBoostVersion,minIov,niovs)
222 if timeCut
is not None:
224 msg +=
' (iov insertion time>%s)' %
str(timeCut)
226 msg +=
' (iov insertion time<%s)' %
str(timeCut)
228 return tagBoostVersion, minIov
231 cursor = self.db.cursor()
232 cursor.execute(
'SELECT GT.NAME, GT.RELEASE, GT.SNAPSHOT_TIME FROM GLOBAL_TAG GT, GLOBAL_TAG_MAP GTM WHERE GT.NAME = GTM.GLOBAL_TAG_NAME AND GTM.TAG_NAME = :TAG_NAME',(t,))
233 rows = cursor.fetchall()
238 gts.append((r[0],r[1],r[2]))
240 logging.info(
'validating %s gts.' %len(gts))
241 boost_snapshot_map = {}
244 logging.debug(
'Validating for GT %s (release %s)' %(gt[0],gt[1]))
245 gtCMSSWVersion = sm.check_cmssw_version( gt[1] )
246 gtBoostVersion = self.version_db.lookup_boost_in_cmssw( gtCMSSWVersion )
247 if sm.cmp_boost_version( gtBoostVersion, tagBoostVersion )<0:
248 logging.warning(
'The boost version computed from all the iovs in the tag (%s) is incompatible with the gt [%s] %s (consuming ver: %s, snapshot: %s)' %(tagBoostVersion,ngt,gt[0],gtBoostVersion,
str(gt[2])))
249 if str(gt[2])
not in boost_snapshot_map.keys():
250 tagSnapshotBoostVersion =
None 253 if tagSnapshotBoostVersion
is not None:
254 boost_snapshot_map[
str(gt[2])] = tagSnapshotBoostVersion
258 tagSnapshotBoostVersion = boost_snapshot_map[
str(gt[2])]
259 if sm.cmp_boost_version( gtBoostVersion, tagSnapshotBoostVersion )<0:
260 logging.error(
'The snapshot from tag used by gt %s (consuming ver: %s) has an incompatible combined boost version %s' %(gt[0],gtBoostVersion,tagSnapshotBoostVersion))
261 invalid_gts.append( ( gt[0], gtBoostVersion ) )
262 if len(invalid_gts)==0:
264 logging.info(
'boost version for the tag validated in %s referencing Gts' %(ngt))
266 logging.info(
'No GT referencing this tag found.')
268 logging.error(
'boost version for the tag is invalid.')
272 cursor = self.db.cursor()
273 now = datetime.datetime.utcnow()
275 cursor.execute(
'UPDATE TAG_METADATA SET MIN_SERIALIZATION_V=:BOOST_V, MIN_SINCE=:MIN_IOV, MODIFICATION_TIME=:NOW WHERE TAG_NAME = :NAME',( tagBoostVersion,minIov,now,t))
277 cursor.execute(
'INSERT INTO TAG_METADATA ( TAG_NAME, MIN_SERIALIZATION_V, MIN_SINCE, MODIFICATION_TIME ) VALUES ( :NAME, :BOOST_V, :MIN_IOV, :NOW )',(t, tagBoostVersion,minIov,now))
278 logging.info(
'Minimum boost version for the tag updated.')
281 cursor = self.db.cursor()
283 self.version_db.fetch_cmssw_boost_map()
284 self.version_db.fetch_boost_run_map()
287 if self.args.name
is not None:
288 stmt0 =
'SELECT NAME FROM TAG WHERE NAME = :TAG_NAME' 289 wpars = (self.args.name,)
290 cursor.execute(stmt0,wpars);
291 rows = cursor.fetchall()
297 raise Exception(
'Tag %s does not exists in the database.' %self.args.name )
298 tags[self.args.name] =
None 299 stmt1 =
'SELECT MIN_SERIALIZATION_V, MIN_SINCE, CAST(MODIFICATION_TIME AS TIMESTAMP(0)) FROM TAG_METADATA WHERE TAG_NAME = :NAME' 300 cursor.execute(stmt1,wpars);
301 rows = cursor.fetchall()
303 tags[self.args.name] = (r[0],r[1],r[2])
305 stmt0 =
'SELECT NAME FROM TAG WHERE NAME NOT IN ( SELECT TAG_NAME FROM TAG_METADATA) ORDER BY NAME' 307 if self.args.max
is not None:
312 stmt0 =
'SELECT NAME FROM (SELECT NAME FROM TAG WHERE NAME NOT IN ( SELECT TAG_NAME FROM TAG_METADATA ) ORDER BY NAME) WHERE ROWNUM<= :MAXR' 314 cursor.execute(stmt0,wpars);
315 rows = cursor.fetchall()
318 stmt1 =
'SELECT T.NAME NAME, TM.MIN_SERIALIZATION_V MIN_SERIALIZATION_V, TM.MIN_SINCE MIN_SINCE, CAST(TM.MODIFICATION_TIME AS TIMESTAMP(0)) MODIFICATION_TIME FROM TAG T, TAG_METADATA TM WHERE T.NAME=TM.TAG_NAME AND CAST(TM.MODIFICATION_TIME AS TIMESTAMP(0)) < (SELECT MAX(INSERTION_TIME) FROM IOV WHERE IOV.TAG_NAME=TM.TAG_NAME) ORDER BY NAME' 319 nmax = nmax-len(tags)
321 stmt1 =
'SELECT NAME, MIN_SERIALIZATION_V, MIN_SINCE, MODIFICATION_TIME FROM (SELECT T.NAME NAME, TM.MIN_SERIALIZATION_V MIN_SERIALIZATION_V, TM.MIN_SINCE MIN_SINCE, CAST(TM.MODIFICATION_TIME AS TIMESTAMP(0)) MODIFICATION_TIME FROM TAG T, TAG_METADATA TM WHERE T.NAME=TM.TAG_NAME AND CAST(TM.MODIFICATION_TIME AS TIMESTAMP(0)) < (SELECT MAX(INSERTION_TIME) FROM IOV WHERE IOV.TAG_NAME=TM.TAG_NAME) ORDER BY NAME) WHERE ROWNUM<= :MAXR' 323 cursor.execute(stmt1,wpars);
324 rows = cursor.fetchall()
328 if nmax >=0
and i>nmax:
330 tags[r[0]] = (r[1],r[2],r[3])
331 logging.info(
'Processing boost version for %s tags' %len(tags))
333 for t
in sorted(tags.keys()):
337 cursor.execute(
'SELECT TIME_TYPE FROM TAG WHERE NAME= :TAG_NAME',(t,))
338 timetype = cursor.fetchone()[0]
340 logging.info(
'************************************************************************')
341 logging.info(
'Tag [%s] %s - timetype: %s' %(count,t,timetype))
342 tagBoostVersion =
None 345 if tags[t]
is not None:
347 tagBoostVersion = tags[t][0]
351 if tagBoostVersion
is None:
353 logging.debug(
'boost versions in the %s iovs: %s' %(len(self.
iovs),
str(self.
versionIovs)))
355 if len(invalid_gts)>0:
356 with open(
'invalid_tags_in_gts.txt',
'a')
as error_file:
357 for gt
in invalid_gts:
358 error_file.write(
'Tag %s (boost %s) is invalid for GT %s ( boost %s) \n' %(t,tagBoostVersion,gt[0],gt[1]))
360 if self.
iovs[0][0]<minIov:
361 minIov = self.
iovs[0]
364 except Exception
as e:
365 logging.error(
str(e))
368 cursor = self.db.cursor()
370 self.version_db.insert_boost_run_range( self.args.since, self.args.label )
372 logging.info(
'boost version %s inserted with since %s' %(self.args.label,self.args.since))
375 cursor = self.db.cursor()
377 self.version_db.fetch_boost_run_map()
378 headers = [
'Run',
'Run start time',
'Boost Version',
'Insertion time']
379 print_table( headers, self.version_db.boost_run_map )
382 cursor = self.db.cursor()
383 tag = self.args.tag_name
384 cursor.execute(
'SELECT TIME_TYPE FROM TAG WHERE NAME= :TAG_NAME',(tag,))
385 rows = cursor.fetchall()
387 t_modificationTime =
None 391 raise Exception(
"Tag %s does not exist in the database." %tag)
392 cursor.execute(
'SELECT MAX(INSERTION_TIME) FROM IOV WHERE TAG_NAME= :TAG_NAME',(tag,))
393 rows = cursor.fetchall()
395 t_modificationTime = r[0]
396 if t_modificationTime
is None:
397 raise Exception(
"Tag %s does not have any iov stored." %tag)
398 logging.info(
'Tag %s - timetype: %s' %(tag,timeType))
399 cursor.execute(
'SELECT MIN_SERIALIZATION_V, MIN_SINCE, MODIFICATION_TIME FROM TAG_METADATA WHERE TAG_NAME= :TAG_NAME',(tag,))
400 rows = cursor.fetchall()
401 tagBoostVersion =
None 403 v_modificationTime =
None 405 tagBoostVersion = r[0]
407 v_modificationTime = r[2]
408 if v_modificationTime
is not None:
409 if t_modificationTime > v_modificationTime:
410 logging.warning(
'The minimum boost version stored is out of date.')
412 logging.info(
'The minimum boost version stored is up to date.')
414 if v_modificationTime
is not None:
415 mt =
str(v_modificationTime)
416 r_tagBoostVersion =
None 417 if self.args.rebuild
or self.args.full:
419 self.version_db.fetch_boost_run_map()
421 logging.info(
'Calculating minimum boost version for the available iovs...')
423 print '# Currently stored: %s (min iov:%s)' %(tagBoostVersion,minIov)
424 print '# Last update: %s' %mt
425 print '# Last update on the iovs: %s' %
str(t_modificationTime)
426 if self.args.rebuild
or self.args.full:
427 print '# Based on the %s available IOVs: %s (min iov:%s)' %(len(self.
iovs),r_tagBoostVersion,r_minIov)
429 headers = [
'Run',
'Boost Version']
437 parser =
argparse.ArgumentParser(description=
'CMS conddb command-line tool for serialiation metadata. For general help (manual page), use the help subcommand.')
438 parser.add_argument(
'--db', type=str, help=
'The target database: pro ( for prod ) or dev ( for prep ). default=pro')
439 parser.add_argument(
"--auth",
"-a", type=str, help=
"The path of the authentication file")
440 parser.add_argument(
'--verbose',
'-v', action=
'count', help=
'The verbosity level')
441 parser_subparsers = parser.add_subparsers(title=
'Available subcommands')
442 parser_update_tags = parser_subparsers.add_parser(
'update_tags', description=
'Update the existing tags headers with the boost version')
443 parser_update_tags.add_argument(
'--name',
'-n', type=str, help=
'Name of the specific tag to process (default=None - in this case all of the tags will be processed.')
444 parser_update_tags.add_argument(
'--max',
'-m', type=int, help=
'the maximum number of tags processed',default=100)
445 parser_update_tags.add_argument(
'--all',action=
'store_true', help=
'process all of the tags with boost_version = None')
446 parser_update_tags.set_defaults(func=tool.update_tags,accessType=
'w')
447 parser_insert_boost_version = parser_subparsers.add_parser(
'insert_boost_version', description=
'Insert a new boost version range in the run map')
448 parser_insert_boost_version.add_argument(
'--label',
'-l',type=str, help=
'The boost version label',required=
True)
449 parser_insert_boost_version.add_argument(
'--since',
'-s',type=int, help=
'The since validity (run number)',required=
True)
450 parser_insert_boost_version.set_defaults(func=tool.insert_boost_run,accessType=
'w')
451 parser_list_boost_versions = parser_subparsers.add_parser(
'list_boost_versions', description=
'list the boost versions in the run map')
452 parser_list_boost_versions.set_defaults(func=tool.list_boost_run,accessType=
'r') 453 parser_show_version = parser_subparsers.add_parser('show_boost_version', description=
'Display the minimum boost version for the specified tag (the value stored, by default)')
454 parser_show_version.add_argument(
'tag_name',help=
'The name of the tag')
455 parser_show_version.add_argument(
'--rebuild',
'-r',action=
'store_true',default=
False,help=
'Re-calculate the minimum boost versio ')
456 parser_show_version.add_argument(
'--full',action=
'store_true',default=
False,help=
'Recalulate the minimum boost version, listing the versions in the iov sequence')
457 parser_show_version.set_defaults(func=tool.show_tag_boost_version,accessType=
'r') 458 args = parser.parse_args() 461 tool.logger.setLevel(logging.DEBUG)
467 sys.exit( args.func())
468 except Exception
as e:
472 if __name__ ==
'__main__':
def print_table(headers, table)
def lookup_boost_in_cmssw(self, cmssw_version)
def insert_cmssw_boost(self, cmssw_version, boost_version)
def insert_boost_run_range(self, run, boost_version)
def fetch_cmssw_boost_map(self)
def fetch_boost_run_map(self)
def populate_for_gts(self)