From: <rm...@hy...> - 2008-02-28 19:48:46
|
Author: rmorgan Date: 2008-02-28 11:48:43 -0800 (Thu, 28 Feb 2008) New Revision: 7948 URL: http://svn.hyperic.org/?view=rev&root=Hyperic+HQ&revision=7948 Modified: trunk/etc/ehcache.xml trunk/src/org/hyperic/hq/measurement/server/session/CategoryDAO.java trunk/src/org/hyperic/hq/measurement/server/session/MeasurementTemplate.java trunk/src/org/hyperic/hq/measurement/server/session/MeasurementTemplateDAO.java trunk/src/org/hyperic/hq/measurement/server/session/TemplateManagerEJBImpl.java Log: Remove direct SQL insertion of measurement templates. Introduce a cache for Category.findByName to remove the need to cache in the application code. The inserts are batched 30 at a time. This cuts down plugin deployment anywhere from 30-50% depending on the # of templates the plugin defines. Modified: trunk/etc/ehcache.xml =================================================================== --- trunk/etc/ehcache.xml 2008-02-28 17:46:58 UTC (rev 7947) +++ trunk/etc/ehcache.xml 2008-02-28 19:48:43 UTC (rev 7948) @@ -64,6 +64,12 @@ timeToIdleSeconds="0" timeToLiveSeconds="0" memoryStoreEvictionPolicy="LRU"/> + <cache name="Category.findByName" + maxElementsInMemory="4" + eternal="true" + timeToIdleSeconds="0" + timeToLiveSeconds="0" + memoryStoreEvictionPolicy="LRU"/> <cache name="org.hyperic.hq.measurement.server.session.Baseline" maxElementsInMemory="50000" eternal="true" Modified: trunk/src/org/hyperic/hq/measurement/server/session/CategoryDAO.java =================================================================== --- trunk/src/org/hyperic/hq/measurement/server/session/CategoryDAO.java 2008-02-28 17:46:58 UTC (rev 7947) +++ trunk/src/org/hyperic/hq/measurement/server/session/CategoryDAO.java 2008-02-28 19:48:43 UTC (rev 7948) @@ -48,6 +48,8 @@ Category findByName(String name) { String sql = "from Category where name=?"; return (Category)getSession().createQuery(sql). - setString(0, name).uniqueResult(); + setString(0, name). + setCacheable(true). + setCacheRegion("Category.findByName").uniqueResult(); } } Modified: trunk/src/org/hyperic/hq/measurement/server/session/MeasurementTemplate.java =================================================================== --- trunk/src/org/hyperic/hq/measurement/server/session/MeasurementTemplate.java 2008-02-28 17:46:58 UTC (rev 7947) +++ trunk/src/org/hyperic/hq/measurement/server/session/MeasurementTemplate.java 2008-02-28 19:48:43 UTC (rev 7948) @@ -56,9 +56,25 @@ private MonitorableType _monitorableType; private Category _category; - public MeasurementTemplate() { + public MeasurementTemplate(String name, String alias, String units, + int collectionType, boolean defaultOn, + long defaultInterval, boolean designate, + String template, MonitorableType type, + Category category, String plugin) + { + _name = name; + _alias = alias; + _units = units; + _collectionType = collectionType; + _defaultOn = defaultOn; + _defaultInterval = defaultInterval; + _designate = designate; + _template = template; + _monitorableType = type; + _category = category; + _plugin = plugin; } - + /** * @see org.hyperic.hibernate.ContainerManagedTimestampTrackable#allowContainerManagedLastModifiedTime() * @return <code>true</code> by default. Modified: trunk/src/org/hyperic/hq/measurement/server/session/MeasurementTemplateDAO.java =================================================================== --- trunk/src/org/hyperic/hq/measurement/server/session/MeasurementTemplateDAO.java 2008-02-28 17:46:58 UTC (rev 7947) +++ trunk/src/org/hyperic/hq/measurement/server/session/MeasurementTemplateDAO.java 2008-02-28 19:48:43 UTC (rev 7948) @@ -83,19 +83,14 @@ long defaultInterval, boolean designate, String template, MonitorableType monitorableType, Category cat, String plugin) { - MeasurementTemplate mt = new MeasurementTemplate(); - - mt.setName(name); - mt.setAlias(alias); - mt.setUnits(units); - mt.setCollectionType(collectionType); - mt.setDefaultOn(defaultOn); - mt.setDefaultInterval(defaultInterval); - mt.setDesignate(designate); - mt.setTemplate(template); - mt.setMonitorableType(monitorableType); - mt.setCategory(cat); - mt.setPlugin(plugin); + MeasurementTemplate mt = new MeasurementTemplate(name, alias, + units, collectionType, + defaultOn, + defaultInterval, + designate, + template, + monitorableType, + cat, plugin); save(mt); return mt; } Modified: trunk/src/org/hyperic/hq/measurement/server/session/TemplateManagerEJBImpl.java =================================================================== --- trunk/src/org/hyperic/hq/measurement/server/session/TemplateManagerEJBImpl.java 2008-02-28 17:46:58 UTC (rev 7947) +++ trunk/src/org/hyperic/hq/measurement/server/session/TemplateManagerEJBImpl.java 2008-02-28 19:48:43 UTC (rev 7948) @@ -25,9 +25,6 @@ package org.hyperic.hq.measurement.server.session; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -46,12 +43,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.hibernate.Session; -import org.hibernate.id.IdentifierGenerator; -import org.hibernate.impl.SessionFactoryImpl; -import org.hibernate.impl.SessionImpl; import org.hyperic.hibernate.PageInfo; -import org.hyperic.hibernate.Util; import org.hyperic.hq.appdef.shared.AppdefEntityConstants; import org.hyperic.hq.appdef.shared.AppdefEntityID; import org.hyperic.hq.appdef.shared.AppdefEntityNotFoundException; @@ -509,81 +501,37 @@ */ public void createTemplates(String pluginName, Map toAdd) throws CreateException { - // Add the new templates - Connection conn = null; - PreparedStatement tStmt = null; - PreparedStatement aStmt = null; - SessionFactoryImpl sessionFactory = - (SessionFactoryImpl)Util.getSessionFactory(); - Session session = - getMeasurementTemplateDAO().getSession(); - try { - IdentifierGenerator tmplIdGenerator = - sessionFactory - .getEntityPersister(MeasurementTemplate.class.getName()) - .getIdentifierGenerator(); + MeasurementTemplateDAO dao = getMeasurementTemplateDAO(); - conn = session.connection(); + int template_count = 0; + for (Iterator i = toAdd.entrySet().iterator(); i.hasNext();) { + Map.Entry entry = (Map.Entry) i.next(); + MonitorableType monitorableType = (MonitorableType) entry.getKey(); + Map newMetrics = (Map) entry.getValue(); - final String templatesql = "INSERT INTO EAM_MEASUREMENT_TEMPL " + - "(id, name, alias, units, collection_type, default_on, " + - "default_interval, designate, monitorable_type_id, " + - "category_id, template, plugin, ctime, mtime) " + - "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + for (Iterator j = newMetrics.values().iterator(); j.hasNext(); + template_count++) { + MeasurementInfo info = (MeasurementInfo) j.next(); - long current = System.currentTimeMillis(); + Category cat = getCategoryDAO().findByName(info.getCategory()); + dao.create(info.getName(), + info.getAlias(), + info.getUnits(), + info.getCollectionType(), + info.isDefaultOn(), + info.getInterval(), + info.isIndicator(), + info.getTemplate(), + monitorableType, + cat, + pluginName); - // can assume this is called in a single thread - // This is called at hq server startup - HashMap cats = new HashMap(); - for (Iterator i = toAdd.entrySet().iterator(); i.hasNext();) { - Map.Entry entry = (Map.Entry) i.next(); - MonitorableType monitorableType = - (MonitorableType) entry.getKey(); - Map newMetrics = (Map) entry.getValue(); - - for (Iterator j = newMetrics.values().iterator(); j.hasNext();){ - MeasurementInfo info = (MeasurementInfo)j.next(); - - Category cat = - (Category) cats.get(info.getCategory()); - if (cat == null) { - cat = getCategoryDAO().findByName(info.getCategory()); - if (cat == null) { - cat = getCategoryDAO().create(info.getCategory()); - } - cats.put(info.getCategory(), cat); - } - - int col = 1; - Integer rawid = (Integer)tmplIdGenerator. - generate((SessionImpl)session, new MeasurementTemplate()); - - tStmt = conn.prepareStatement(templatesql); - tStmt.setInt(col++, rawid.intValue()); - tStmt.setString(col++, info.getName()); - tStmt.setString(col++, info.getAlias()); - tStmt.setString(col++, info.getUnits()); - tStmt.setInt(col++, info.getCollectionType()); - tStmt.setBoolean(col++, info.isDefaultOn()); - tStmt.setLong(col++, info.getInterval()); - tStmt.setBoolean(col++, info.isIndicator()); - tStmt.setInt(col++, monitorableType.getId().intValue()); - tStmt.setInt(col++, cat.getId().intValue()); - tStmt.setString(col++, info.getTemplate()); - tStmt.setString(col++, pluginName); - tStmt.setLong(col++, current); - tStmt.setLong(col, current); - tStmt.execute(); - tStmt.close(); + if (template_count % 30 == 0) { // Same as hibernate.jdbc.batch_size + dao.getSession().flush(); + dao.getSession().clear(); } } - } catch (SQLException e) { - this.log.error("Unable to add measurements for: " + - pluginName, e); - } finally { - session.disconnect(); } } |