From: <sc...@hy...> - 2010-01-19 01:25:59
|
Author: scottmf Date: 2010-01-18 17:25:52 -0800 (Mon, 18 Jan 2010) New Revision: 14197 URL: http://svn.hyperic.org/?view=rev&root=Hyperic+HQ&revision=14197 Modified: branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/bizapp/server/session/MeasurementBossEJBImpl.java branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/dao/HibernateDAO.java branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/measurement/server/session/MeasurementDAO.java branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/measurement/server/session/MeasurementManagerEJBImpl.java Log: [HHQ-3676] batched up a query which finds designated metrics per resource Modified: branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/bizapp/server/session/MeasurementBossEJBImpl.java =================================================================== --- branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/bizapp/server/session/MeasurementBossEJBImpl.java 2010-01-19 01:25:15 UTC (rev 14196) +++ branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/bizapp/server/session/MeasurementBossEJBImpl.java 2010-01-19 01:25:52 UTC (rev 14197) @@ -1204,29 +1204,28 @@ MeasurementTemplate tmpl, long begin, long end, long interval, boolean returnNulls, PageControl pc) - throws SessionNotFoundException, SessionTimeoutException, - AppdefEntityNotFoundException, - PermissionException, MeasurementNotFoundException { - + throws SessionNotFoundException, SessionTimeoutException, + AppdefEntityNotFoundException, PermissionException, + MeasurementNotFoundException { final AuthzSubject subject = manager.getSubject(sessionId); - + final boolean debug = _log.isDebugEnabled(); + final StopWatch watch = new StopWatch(); List measurements; - if (aid.isApplication() && tmpl.isAvailability()) { // Special case for application availability _log.debug("BEGIN findMeasurementData()"); - AppdefEntityValue aeval = new AppdefEntityValue(aid, subject); - // Get the flattened list of services + if (debug) watch.markTimeBegin("getFlattenedServiceIds"); AppdefEntityID[] serviceIds = aeval.getFlattenedServiceIds(); - + if (debug) watch.markTimeEnd("getFlattenedServiceIds"); + if (debug) watch.markTimeBegin("findDesignatedMeasurements"); Map midMap = getMetricManager() .findDesignatedMeasurements(subject, serviceIds, MeasurementConstants.CAT_AVAILABILITY); + if (debug) watch.markTimeEnd("findDesignatedMeasurements"); measurements = new ArrayList(midMap.values()); - } - else { + } else { measurements = getMeasurementsForResource(subject, aid, tmpl); if (measurements == null || measurements.size() == 0) { throw new MeasurementNotFoundException( @@ -1234,10 +1233,13 @@ tmpl.getId()); } } - - return getDataMan().getHistoricalData(measurements, begin, end, - interval, tmpl.getCollectionType(), - returnNulls, pc); + if (debug) watch.markTimeBegin("getHistoricalData"); + PageList rtn = getDataMan().getHistoricalData( + measurements, begin, end, interval, tmpl.getCollectionType(), + returnNulls, pc); + if (debug) watch.markTimeEnd("getHistoricalData"); + if (debug) _log.debug(watch); + return rtn; } /** @@ -3006,30 +3008,27 @@ PermissionException { StopWatch watch = new StopWatch(); final boolean debug = _log.isDebugEnabled(); - if (debug) _log.debug("BEGIN getAvailability()"); + if (debug) _log.debug("BEGIN getAvailability() id=" + id); try { if (id.isGroup()) { return getGroupAvailability( subject, id.getId(), measCache, availCache); - } - else if (id.isApplication()) { + } else if (id.isApplication()) { AppdefEntityValue appVal = new AppdefEntityValue(id, subject); if (debug) watch.markTimeBegin("getFlattenedServiceIds"); AppdefEntityID[] services = appVal.getFlattenedServiceIds(); if (debug) watch.markTimeEnd("getFlattenedServiceIds"); - if (debug) watch.markTimeBegin("getAggregateAvailability"); double rtn = getAggregateAvailability( subject, services, measCache, availCache); if (debug) watch.markTimeEnd("getAggregateAvailability"); return rtn; } - AppdefEntityID[] ids = new AppdefEntityID[] { id }; return getAvailability( subject, ids, getMidMap(ids, measCache), availCache)[0]; } finally { - if (debug) _log.debug("END getAvailability() -- " + watch); + if (debug) _log.debug("END getAvailability() id=" + id + " -- " + watch); } } Modified: branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/dao/HibernateDAO.java =================================================================== --- branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/dao/HibernateDAO.java 2010-01-19 01:25:15 UTC (rev 14196) +++ branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/dao/HibernateDAO.java 2010-01-19 01:25:52 UTC (rev 14197) @@ -49,7 +49,7 @@ public abstract class HibernateDAO { private Class _persistentClass; private DAOFactory _daoFactory; - protected static final int BATCH_SIZE = 500; + protected static final int BATCH_SIZE = 1000; protected HibernateDAO(Class persistentClass, DAOFactory f) { _persistentClass = persistentClass; Modified: branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/measurement/server/session/MeasurementDAO.java =================================================================== --- branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/measurement/server/session/MeasurementDAO.java 2010-01-19 01:25:15 UTC (rev 14196) +++ branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/measurement/server/session/MeasurementDAO.java 2010-01-19 01:25:52 UTC (rev 14197) @@ -329,23 +329,37 @@ .uniqueResult(); } - List findDesignatedByResourceForCategory(Resource resource, String cat) - { - String sql = - "select m from Measurement m " + - "join m.template t " + - "join t.category c " + - "where m.resource = ? and " + - "t.designate = true and " + - "c.name = ? " + - "order by t.name"; + /** + * @param resources {@link List} of {@link Resource}s + * @return {@link List} of {@link Measurement}s + */ + List findDesignatedByResourcesForCategory(List resources, String cat) { + String sql = new StringBuilder(512) + .append("select m from Measurement m ") + .append("join m.template t ") + .append("join t.category c ") + .append("where m.resource in (:rids) and ") + .append("t.designate = true and ") + .append("c.name = :cat") + .toString(); + int size = resources.size(); + List rtn = new ArrayList(size*5); + for (int i=0; i<size; i+=BATCH_SIZE) { + int end = Math.min(size, i+BATCH_SIZE); + rtn.addAll(getSession().createQuery(sql) + .setParameterList("rids", resources.subList(i, end)) + .setParameter("cat", cat) + .list()); + } + return rtn; + } - return getSession().createQuery(sql) - .setParameter(0, resource) - .setParameter(1, cat) - .setCacheable(true) - .setCacheRegion("Measurement.findDesignatedByResourceForCategory") - .list(); + /** + * @return {@link List} of {@link Measurement}s + */ + List findDesignatedByResourceForCategory(Resource resource, String cat) { + return findDesignatedByResourcesForCategory( + Collections.singletonList(resource), cat); } List findDesignatedByResource(Resource resource) { Modified: branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/measurement/server/session/MeasurementManagerEJBImpl.java =================================================================== --- branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/measurement/server/session/MeasurementManagerEJBImpl.java 2010-01-19 01:25:15 UTC (rev 14196) +++ branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/measurement/server/session/MeasurementManagerEJBImpl.java 2010-01-19 01:25:52 UTC (rev 14197) @@ -746,33 +746,22 @@ public Map findDesignatedMeasurements(AuthzSubject subject, AppdefEntityID[] ids, String cat) throws MeasurementNotFoundException { - - Map midMap = new HashMap(); - if (ids.length == 0) - return midMap; - - for (int i = 0; i < ids.length; i++) { + if (ids.length == 0) { + return Collections.EMPTY_MAP; + } + Map midMap = new HashMap(ids.length); + ResourceManagerLocal rMan = ResourceManagerEJBImpl.getOne(); + ArrayList resources = new ArrayList(ids.length); + for (int i=0; i<ids.length; i++) { AppdefEntityID id = ids[i]; - try { - List metrics = getMeasurementDAO(). - findDesignatedByResourceForCategory(getResource(id), - cat); - - if (metrics.size() == 0) { - throw new FinderException("No metrics found"); - } - - Measurement m = (Measurement) metrics.get(0); - midMap.put(id, m); - } catch (FinderException e) { - // Throw an exception if we're only looking for one - // measurement - if (ids.length == 1) { - throw new MeasurementNotFoundException(cat + " metric for " + - id + " not found"); - } - } + resources.add(rMan.findResource(id)); } + MeasurementDAO dao = getMeasurementDAO(); + List list = dao.findDesignatedByResourcesForCategory(resources, cat); + for (Iterator it=list.iterator(); it.hasNext(); ) { + Measurement m = (Measurement) it.next(); + midMap.put(new AppdefEntityID(m.getResource()), m); + } return midMap; } |