Author: nunofsantos
Date: 2008-11-19 18:30:45 -0500 (Wed, 19 Nov 2008)
New Revision: 2852
Modified:
mgmt/trunk/mint/python/mint/__init__.py
mgmt/trunk/mint/python/mint/update.py
Log:
more performance improvements: get rid of subquery, refactor and optimize orphan handling
Modified: mgmt/trunk/mint/python/mint/__init__.py
===================================================================
--- mgmt/trunk/mint/python/mint/__init__.py 2008-11-19 23:01:38 UTC (rev 2851)
+++ mgmt/trunk/mint/python/mint/__init__.py 2008-11-19 23:30:45 UTC (rev 2852)
@@ -341,27 +341,6 @@
def setCloseListener(self, connCloseListener):
self.connCloseListener = connCloseListener
-# def getObjectId(self, cls, id, conn):
-# if isinstance(id, qpid.qmfconsole.ObjectId):
-# first = id.first
-# second = id.second
-# dbId = None
-# if (first, second) in self.qmfIdToDbIdMap:
-# dbId = self.qmfIdToDbIdMap[(first, second)]
-# else:
-# try:
-# cursor = conn.cursor()
-# cursor.execute("select id from %s where source_scope_id = %s and
source_object_id = %s" \
-# % (self.dbStyle.pythonClassToDBTable(cls.__name__), first,
second));
-# rec = cursor.fetchone()
-# dbId = rec[0]
-# self.qmfIdToDbIdMap[(first, second)] = dbId
-# except Exception:
-# raise ObjectNotFound()
-# return dbId
-# else:
-# raise ObjectNotFound()
-
def __pythonValueToDB(self, key, value):
if key == "qmfClassKey":
value = u"'%s'" % unicode(value,
"raw_unicode_escape").replace("'", "''")
@@ -423,6 +402,24 @@
sql = u"update %s set %s where %s" % (table, updateValues, whereClause)
return sql
+ def addOrphanObject(self, cls, idFirst, idSecond, obj):
+ # store object in orphan map, will be picked up later when parent info is received
+ log.info("Referenced object %s '%d-%d' not found, deferring creation of
orphan object" % (cls, idFirst, idSecond))
+ if (cls, idFirst, idSecond) not in self.orphanObjectMap:
+ self.orphanObjectMap[(cls, idFirst, idSecond)] = set()
+ self.orphanObjectMap[(cls, idFirst, idSecond)].add(obj)
+
+ def requeueIfOrphanParent(self, cls, idFirst, idSecond):
+ orphans = 0
+ if (cls, idFirst, idSecond) in self.orphanObjectMap:
+ # this object is the parent of orphan objects in the map, re-enqueue for insertion
+ orphanObjects = self.orphanObjectMap.pop((cls, idFirst, idSecond))
+ for orphanObj in orphanObjects:
+ self.updateThread.enqueue(orphanObj)
+ orphans += 1
+ log.info("Inserted %d orphan objects whose creation had been deferred" %
(orphans))
+ return orphans
+
def getSession(self):
return self.mgmtSession
Modified: mgmt/trunk/mint/python/mint/update.py
===================================================================
--- mgmt/trunk/mint/python/mint/update.py 2008-11-19 23:01:38 UTC (rev 2851)
+++ mgmt/trunk/mint/python/mint/update.py 2008-11-19 23:30:45 UTC (rev 2852)
@@ -93,14 +93,12 @@
otherClass = getattr(mint, className, None)
if otherClass:
foreignKey = name + "_id"
- if (value.first, value.second) in model.qmfIdToDbIdMap:
- results[foreignKey] = model.qmfIdToDbIdMap[(value.first, value.second)]
- elif not orphan:
- log.info("Referenced object %s '%s' not found, deferring
creation of orphan object" % (className, value))
- # store object in orphan map, will be picked up later when parent info is
received
- if (className, value.first, value.second) not in model.orphanObjectMap:
- model.orphanObjectMap[(className, value.first, value.second)] = set()
- model.orphanObjectMap[(className, value.first, value.second)].add(self)
+ valueFirst = value.first
+ valueSecond = value.second
+ if (valueFirst, valueSecond) in model.qmfIdToDbIdMap:
+ results[foreignKey] = model.qmfIdToDbIdMap[(valueFirst, valueSecond)]
+ else:
+ model.addOrphanObject(className, valueFirst, valueSecond, self)
orphan = True
else:
log.error("Class '%s' not found" % className)
@@ -143,6 +141,8 @@
properties = self.qmfObj.getProperties()
timestamps = self.qmfObj.getTimestamps()
id = self.qmfObj.getObjectId()
+ idFirst = id.first
+ idSecond = id.second
attrs = self.processAttributes(properties, cls, model, conn)
if attrs == None:
@@ -156,8 +156,8 @@
attrs["deletionTime"] =
time_unwarp(datetime.fromtimestamp(timestamps[2]/1000000000))
log.debug("%s(%s) marked deleted", cls, id)
- attrs["sourceScopeId"] = id.first
- attrs["sourceObjectId"] = id.second
+ attrs["sourceScopeId"] = idFirst
+ attrs["sourceObjectId"] = idSecond
attrs["qmfClassKey"] = "%s, %s, %s" % (pkg, origCls, hash)
attrs["managedBroker"] = str(self.broker.getBrokerId())
@@ -165,39 +165,38 @@
sql = model.generateSQLUpdate(sqlCls, attrs, id)
#log.debug("SQL: %s", sql)
cursor.execute(sql)
- if cursor.rowcount == 0:
+ isUpdate = cursor.rowcount > 0
+ if not isUpdate:
# update failed, need to insert
sql = model.generateSQLInsert(sqlCls, attrs)
#log.debug("SQL: %s", sql)
cursor.execute(sql)
log.debug("%s(%s) created", cls, id)
+
+ if (idFirst, idSecond) not in model.qmfIdToDbIdMap:
+ if isUpdate:
+ sql = model.generateSQLSelect(sqlCls, id)
+ else:
+ sql = "select currval('%s_id_seq')" % (sqlCls)
+ #log.debug("SQL: %s", sql)
+ cursor.execute(sql)
+ rec = cursor.fetchone()
+ dbId = rec[0]
+ model.qmfIdToDbIdMap[(idFirst, idSecond)] = dbId
+ else:
+ dbId = model.qmfIdToDbIdMap[(idFirst, idSecond)]
- if (id.first, id.second) not in model.qmfIdToDbIdMap:
- sql = " ; select currval('%s_id_seq')" % (sqlCls)
+ if cls == "Broker" and str(self.broker.getBrokerId()) in
model.managedBrokers:
+ broker, dbObjId = model.managedBrokers[str(self.broker.getBrokerId())]
+ if dbObjId == 0:
+ sql = model.generateSQLUpdateWhere("broker_registration", \
+ {"broker_id": dbId}, {"url":
self.broker.getFullUrl()})
#log.debug("SQL: %s", sql)
cursor.execute(sql)
- rec = cursor.fetchone()
- dbId = rec[0]
- model.qmfIdToDbIdMap[(id.first, id.second)] = dbId
+ model.managedBrokers[str(self.broker.getBrokerId())] = (broker, dbId)
- if cls == "Broker" and str(self.broker.getBrokerId()) in
model.managedBrokers:
- broker, dbObjId = model.managedBrokers[str(self.broker.getBrokerId())]
- if dbObjId == 0:
- sql = model.generateSQLUpdateWhere("broker_registration", \
- {"broker_id": model.qmfIdToDbIdMap[(id.first, id.second)]},
\
- {"url": self.broker.getFullUrl()})
- #log.debug("SQL: %s", sql)
- cursor.execute(sql)
- model.managedBrokers[str(self.broker.getBrokerId())] = (broker, dbId)
+ model.requeueIfOrphanParent(cls, idFirst, idSecond)
- if (cls, id.first, id.second) in model.orphanObjectMap:
- # this object is the parent of orphan objects in the map, re-enqueue for insertion
- orphanObjects = model.orphanObjectMap.pop((cls, id.first, id.second))
- for orphanObj in orphanObjects:
- model.updateThread.enqueue(orphanObj)
- log.info("Inserted %d orphan objects whose creation had been deferred" %
(len(orphanObjects)))
-
-
class StatisticUpdate(ModelUpdate):
def __init__(self, broker, obj):
ModelUpdate.__init__(self, broker, obj)
@@ -219,6 +218,8 @@
statistics = self.qmfObj.getStatistics()
timestamps = self.qmfObj.getTimestamps()
id = self.qmfObj.getObjectId()
+ idFirst = id.first
+ idSecond = id.second
statsCls = self.getStatsClass(cls)
sqlStatsCls = model.dbStyle.pythonClassToDBTable(statsCls)
@@ -230,10 +231,13 @@
return
attrs["recTime"] =
time_unwarp(datetime.fromtimestamp(timestamps[0]/1000000000))
-
+ if (idFirst, idSecond) in model.qmfIdToDbIdMap:
+ attrs[sqlCls + "_id"] = model.qmfIdToDbIdMap[(idFirst, idSecond)]
+ else:
+ model.addOrphanObject(cls, idFirst, idSecond, self)
+
cursor = conn.cursor()
- subSql = model.generateSQLSelect(sqlCls, id)
- sql = model.generateSQLInsert(sqlStatsCls, attrs, {sqlCls + "_id":
subSql})
+ sql = model.generateSQLInsert(sqlStatsCls, attrs)
#log.debug("SQL: %s", sql)
cursor.execute(sql)