3 from __future__
import print_function
9 import CondCore.Utilities.conddb_serialization_metadata
as sm
10 import CondCore.Utilities.credentials
as auth
11 import CondCore.Utilities.conddb_time
as conddb_time
14 prod_db_service = (
'cms_orcon_prod',{
'w':
'cms_orcon_prod/cms_cond_general_w',
'r':
'cms_orcon_prod/cms_cond_general_r'})
15 adg_db_service = (
'cms_orcon_adg',{
'r':
'cms_orcon_adg/cms_cond_general_r'})
16 dev_db_service = (
'cms_orcoff_prep',{
'w':
'cms_orcoff_prep/cms_cond_general_w',
'r':
'cms_orcoff_prep/cms_cond_general_r'})
17 schema_name =
'CMS_CONDITIONS'
19 fmt_str =
"[%(asctime)s] %(levelname)s: %(message)s"
20 logLevel = logging.INFO
21 logFormatter = logging.Formatter(fmt_str)
42 line += (fmt.format( row[ind] )+
' ')
49 hsep += (fmt.format(
'')+
' ')
61 cursor = self.
db.cursor()
62 cursor.execute(
'SELECT BOOST_VERSION, CMSSW_VERSION FROM CMSSW_BOOST_MAP');
63 rows = cursor.fetchall()
70 cursor = self.
db.cursor()
71 cursor.execute(
'SELECT RUN_NUMBER, RUN_START_TIME, BOOST_VERSION, INSERTION_TIME FROM BOOST_RUN_MAP ORDER BY RUN_NUMBER, INSERTION_TIME')
72 rows = cursor.fetchall()
79 cursor = self.
db.cursor()
80 cursor.execute(
'SELECT MIN(RUN_NUMBER) FROM RUN_INFO WHERE RUN_NUMBER >= :RUN',(run,))
81 res = cursor.fetchone()
82 if res
is not None and res[0]
is not None:
84 cursor.execute(
'SELECT START_TIME FROM RUN_INFO WHERE RUN_NUMBER=:RUN',(min_run,))
85 min_run_time = cursor.fetchone()[0]
86 min_run_ts = calendar.timegm( min_run_time.utctimetuple() ) << 32
90 now = datetime.datetime.utcnow()
91 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) )
94 cursor = self.
db.cursor()
95 cursor.execute(
'INSERT INTO CMSSW_BOOST_MAP ( CMSSW_VERSION, BOOST_VERSION ) VALUES ( :CMSSW_VERSION, :BOOST_VERSION )',(cmssw_version,boost_version))
98 cmssw_v = sm.check_cmssw_version( cmssw_version )
101 if sm.is_release_cycle( cmssw_v ):
102 cmssw_v = sm.strip_cmssw_version( cmssw_v )
103 archs = sm.get_production_arch( cmssw_v )
105 path = sm.get_release_root( cmssw_v, arch )
106 if os.path.exists(os.path.join(path,cmssw_v)):
110 if releaseRoot
is None:
113 releaseRoot = sm.get_release_root( cmssw_v, arch )
114 for r
in sorted (os.listdir( releaseRoot )):
115 if r.startswith(cmssw_v):
117 logging.debug(
'Boost version will be verified in release %s' %cmssw_v)
122 if releaseRoot
is None:
123 archs = sm.get_production_arch( cmssw_v )
125 path = sm.get_release_root( cmssw_v, arch )
126 if os.path.exists(os.path.join(path,cmssw_v)):
130 logging.debug(
'Release path: %s' %releaseRoot)
131 boost_version = sm.get_cmssw_boost( the_arch,
'%s/%s' %(releaseRoot,cmssw_v) )
132 if not boost_version
is None:
138 cursor = self.
db.cursor()
139 cursor.execute(
'SELECT DISTINCT(RELEASE) FROM GLOBAL_TAG')
140 rows = cursor.fetchall()
150 self.
logger.setLevel(logLevel)
151 consoleHandler = logging.StreamHandler(sys.stdout)
152 consoleHandler.setFormatter(logFormatter)
153 self.
logger.addHandler(consoleHandler)
158 if self.
args.db
is None:
160 if self.
args.db ==
'dev' or self.
args.db ==
'oradev' :
161 db_service = dev_db_service
162 elif self.
args.db ==
'orapro':
163 db_service = adg_db_service
164 elif self.
args.db !=
'onlineorapro' or self.
args.db !=
'pro':
165 db_service = prod_db_service
167 raise Exception(
"Database '%s' is not known." %args.db )
168 if self.
args.accessType
not in db_service[1].
keys():
169 raise Exception(
'The specified database connection %s does not support the requested action.' %db_service[0])
170 service = db_service[1][self.
args.accessType]
171 creds = auth.get_credentials( service, self.
args.auth )
173 raise Exception(
"Could not find credentials for service %s" %service)
174 (username, account, pwd) = creds
175 connStr =
'%s/%s@%s' %(username,pwd,db_service[0])
176 self.
db = cx_Oracle.connect(connStr)
177 logging.info(
'Connected to %s as user %s' %(db_service[0],username))
178 self.
db.current_schema = schema_name
181 if self.
iovs is None:
183 cursor = self.
db.cursor()
184 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'
186 if timeCut
and tagBoostVersion
is not None and not validate:
187 whereClauseOnSince =
' AND IOV.INSERTION_TIME>:TIME_CUT'
188 stmt = stmt + whereClauseOnSince
189 params = params + (timeCut,)
190 stmt = stmt +
' ORDER BY SINCE'
191 logging.debug(
'Executing: "%s"' %stmt)
192 cursor.execute(stmt,params)
194 streamer_info =
str(r[2].
read())
200 if tagBoostVersion
is not None:
202 for iov
in self.
iovs:
203 if validate
and timeCut
is not None and timeCut < iov[1]:
206 iovBoostVersion, tagBoostVersion = sm.update_tag_boost_version( tagBoostVersion, minIov, iov[2], iov[0], timetype, self.
version_db.boost_run_map )
207 if minIov
is None or iov[0]<minIov:
209 logging.debug(
'iov: %s - inserted on %s - streamer: %s' %(iov[0],iov[1],iov[2]))
210 logging.debug(
'current tag boost version: %s minIov: %s' %(tagBoostVersion,minIov))
211 if lastBoost
is None or lastBoost!=iovBoostVersion:
213 lastBoost = iovBoostVersion
215 if tagBoostVersion
is None:
217 logging.warning(
'No iovs found. boost version cannot be determined.')
220 logging.error(
'Could not determine the tag boost version.' )
224 logging.info(
'Tag boost version has not changed.')
226 msg =
'Found tag boost version %s ( min iov: %s ) combining payloads from %s iovs' %(tagBoostVersion,minIov,niovs)
227 if timeCut
is not None:
229 msg +=
' (iov insertion time>%s)' %
str(timeCut)
231 msg +=
' (iov insertion time<%s)' %
str(timeCut)
233 return tagBoostVersion, minIov
236 cursor = self.
db.cursor()
237 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,))
238 rows = cursor.fetchall()
243 gts.append((r[0],r[1],r[2]))
245 logging.info(
'validating %s gts.' %len(gts))
246 boost_snapshot_map = {}
249 logging.debug(
'Validating for GT %s (release %s)' %(gt[0],gt[1]))
250 gtCMSSWVersion = sm.check_cmssw_version( gt[1] )
251 gtBoostVersion = self.
version_db.lookup_boost_in_cmssw( gtCMSSWVersion )
252 if sm.cmp_boost_version( gtBoostVersion, tagBoostVersion )<0:
253 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])))
254 if str(gt[2])
not in boost_snapshot_map.keys():
255 tagSnapshotBoostVersion =
None
258 if tagSnapshotBoostVersion
is not None:
259 boost_snapshot_map[
str(gt[2])] = tagSnapshotBoostVersion
263 tagSnapshotBoostVersion = boost_snapshot_map[
str(gt[2])]
264 if sm.cmp_boost_version( gtBoostVersion, tagSnapshotBoostVersion )<0:
265 logging.error(
'The snapshot from tag used by gt %s (consuming ver: %s) has an incompatible combined boost version %s' %(gt[0],gtBoostVersion,tagSnapshotBoostVersion))
266 invalid_gts.append( ( gt[0], gtBoostVersion ) )
267 if len(invalid_gts)==0:
269 logging.info(
'boost version for the tag validated in %s referencing Gts' %(ngt))
271 logging.info(
'No GT referencing this tag found.')
273 logging.error(
'boost version for the tag is invalid.')
277 cursor = self.
db.cursor()
278 now = datetime.datetime.utcnow()
280 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))
282 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))
283 logging.info(
'Minimum boost version for the tag updated.')
286 cursor = self.
db.cursor()
292 if self.
args.name
is not None:
293 stmt0 =
'SELECT NAME FROM TAG WHERE NAME = :TAG_NAME'
294 wpars = (self.
args.name,)
295 cursor.execute(stmt0,wpars);
296 rows = cursor.fetchall()
302 raise Exception(
'Tag %s does not exists in the database.' %self.
args.name )
303 tags[self.
args.name] =
None
304 stmt1 =
'SELECT MIN_SERIALIZATION_V, MIN_SINCE, CAST(MODIFICATION_TIME AS TIMESTAMP(0)) FROM TAG_METADATA WHERE TAG_NAME = :NAME'
305 cursor.execute(stmt1,wpars);
306 rows = cursor.fetchall()
308 tags[self.
args.name] = (r[0],r[1],r[2])
310 stmt0 =
'SELECT NAME FROM TAG WHERE NAME NOT IN ( SELECT TAG_NAME FROM TAG_METADATA) ORDER BY NAME'
312 if self.
args.max
is not None:
317 stmt0 =
'SELECT NAME FROM (SELECT NAME FROM TAG WHERE NAME NOT IN ( SELECT TAG_NAME FROM TAG_METADATA ) ORDER BY NAME) WHERE ROWNUM<= :MAXR'
319 cursor.execute(stmt0,wpars);
320 rows = cursor.fetchall()
323 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'
324 nmax = nmax-len(tags)
326 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'
328 cursor.execute(stmt1,wpars);
329 rows = cursor.fetchall()
333 if nmax >=0
and i>nmax:
335 tags[r[0]] = (r[1],r[2],r[3])
336 logging.info(
'Processing boost version for %s tags' %len(tags))
338 for t
in sorted(tags.keys()):
342 cursor.execute(
'SELECT TIME_TYPE FROM TAG WHERE NAME= :TAG_NAME',(t,))
343 timetype = cursor.fetchone()[0]
345 logging.info(
'************************************************************************')
346 logging.info(
'Tag [%s] %s - timetype: %s' %(count,t,timetype))
347 tagBoostVersion =
None
350 if tags[t]
is not None:
352 tagBoostVersion = tags[t][0]
356 if tagBoostVersion
is None:
358 logging.debug(
'boost versions in the %s iovs: %s' %(len(self.
iovs),
str(self.
versionIovs)))
359 if self.
args.validate:
361 if len(invalid_gts)>0:
362 with open(
'invalid_tags_in_gts.txt',
'a')
as error_file:
363 for gt
in invalid_gts:
364 error_file.write(
'Tag %s (boost %s) is invalid for GT %s ( boost %s) \n' %(t,tagBoostVersion,gt[0],gt[1]))
366 if self.
iovs[0][0]<minIov:
367 minIov = self.
iovs[0]
370 except Exception
as e:
371 logging.error(
str(e))
374 cursor = self.
db.cursor()
376 if self.
args.min_ts
is None:
377 raise Exception(
"Run %s has not been found in the database - please provide an explicit TimeType value with the min_ts parameter ." %self.
args.since )
380 logging.info(
'boost version %s inserted with since %s' %(self.
args.label,self.
args.since))
383 cursor = self.
db.cursor()
386 headers = [
'Run',
'Run start time',
'Boost Version',
'Insertion time']
390 cursor = self.
db.cursor()
391 tag = self.
args.tag_name
392 cursor.execute(
'SELECT TIME_TYPE FROM TAG WHERE NAME= :TAG_NAME',(tag,))
393 rows = cursor.fetchall()
395 t_modificationTime =
None
399 raise Exception(
"Tag %s does not exist in the database." %tag)
400 cursor.execute(
'SELECT MAX(INSERTION_TIME) FROM IOV WHERE TAG_NAME= :TAG_NAME',(tag,))
401 rows = cursor.fetchall()
403 t_modificationTime = r[0]
404 if t_modificationTime
is None:
405 raise Exception(
"Tag %s does not have any iov stored." %tag)
406 logging.info(
'Tag %s - timetype: %s' %(tag,timeType))
407 cursor.execute(
'SELECT MIN_SERIALIZATION_V, MIN_SINCE, MODIFICATION_TIME FROM TAG_METADATA WHERE TAG_NAME= :TAG_NAME',(tag,))
408 rows = cursor.fetchall()
409 tagBoostVersion =
None
411 v_modificationTime =
None
413 tagBoostVersion = r[0]
415 v_modificationTime = r[2]
416 if v_modificationTime
is not None:
417 if t_modificationTime > v_modificationTime:
418 logging.warning(
'The minimum boost version stored is out of date.')
420 logging.info(
'The minimum boost version stored is up to date.')
422 if v_modificationTime
is not None:
423 mt =
str(v_modificationTime)
424 r_tagBoostVersion =
None
425 if self.
args.rebuild
or self.
args.full:
429 logging.info(
'Calculating minimum boost version for the available iovs...')
431 print(
'# Currently stored: %s (min iov:%s)' %(tagBoostVersion,minIov))
432 print(
'# Last update: %s' %mt)
433 print(
'# Last update on the iovs: %s' %
str(t_modificationTime))
434 if self.
args.rebuild
or self.
args.full:
435 print(
'# Based on the %s available IOVs: %s (min iov:%s)' %(len(self.
iovs),r_tagBoostVersion,r_minIov))
437 headers = [
'Run',
'Boost Version']
445 parser = argparse.ArgumentParser(description=
'CMS conddb command-line tool for serialiation metadata. For general help (manual page), use the help subcommand.')
446 parser.add_argument(
'--db', type=str, help=
'The target database: pro ( for prod ) or dev ( for prep ). default=pro')
447 parser.add_argument(
"--auth",
"-a", type=str, help=
"The path of the authentication file")
448 parser.add_argument(
'--verbose',
'-v', action=
'count', help=
'The verbosity level')
449 parser_subparsers = parser.add_subparsers(title=
'Available subcommands')
450 parser_update_tags = parser_subparsers.add_parser(
'update_tags', description=
'Update the existing tag headers with the boost version')
451 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.')
452 parser_update_tags.add_argument(
'--max',
'-m', type=int, help=
'the maximum number of tags processed',default=100)
453 parser_update_tags.add_argument(
'--all',action=
'store_true', help=
'process all of the tags with boost_version = None')
454 parser_update_tags.add_argument(
'--validate',action=
'store_true', help=
'validate the tag/boost version under processing')
455 parser_update_tags.set_defaults(func=tool.update_tags,accessType=
'w')
456 parser_insert_boost_version = parser_subparsers.add_parser(
'insert', description=
'Insert a new boost version range in the run map')
457 parser_insert_boost_version.add_argument(
'--label',
'-l',type=str, help=
'The boost version label',required=
True)
458 parser_insert_boost_version.add_argument(
'--since',
'-s',type=int, help=
'The since validity (run number)',required=
True)
459 parser_insert_boost_version.add_argument(
'--min_ts',
'-t',type=str, help=
'The since validity (Time timetype)', required=
False)
460 parser_insert_boost_version.set_defaults(func=tool.insert_boost_run,accessType=
'w')
461 parser_list_boost_versions = parser_subparsers.add_parser(
'list', description=
'list the boost versions in the run map')
462 parser_list_boost_versions.set_defaults(func=tool.list_boost_run,accessType=
'r')
463 parser_show_version = parser_subparsers.add_parser(
'show_tag', description=
'Display the minimum boost version for the specified tag (the value stored, by default)')
464 parser_show_version.add_argument(
'tag_name',help=
'The name of the tag')
465 parser_show_version.add_argument(
'--rebuild',
'-r',action=
'store_true',default=
False,help=
'Re-calculate the minimum boost versio ')
466 parser_show_version.add_argument(
'--full',action=
'store_true',default=
False,help=
'Recalulate the minimum boost version, listing the versions in the iov sequence')
467 parser_show_version.set_defaults(func=tool.show_tag_boost_version,accessType=
'r')
468 args = parser.parse_args()
471 tool.logger.setLevel(logging.DEBUG)
477 sys.exit( args.func())
478 except Exception
as e:
482 if __name__ ==
'__main__':