From: <sc...@hy...> - 2010-01-13 02:06:11
|
Author: scottmf Date: 2010-01-12 17:38:00 -0800 (Tue, 12 Jan 2010) New Revision: 14170 URL: http://svn.hyperic.org/?view=rev&root=Hyperic+HQ&revision=14170 Modified: branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/appdef/server/session/ApplicationManagerEJBImpl.java 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/MetricSessionEJB.java branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/measurement/server/session/AvailabilityManagerEJBImpl.java Log: [HHQ-3667] consolidated queries to speed up loading of availability indicators in the resource hub for Applications Modified: branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/appdef/server/session/ApplicationManagerEJBImpl.java =================================================================== --- branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/appdef/server/session/ApplicationManagerEJBImpl.java 2010-01-13 01:35:56 UTC (rev 14169) +++ branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/appdef/server/session/ApplicationManagerEJBImpl.java 2010-01-13 01:38:00 UTC (rev 14170) @@ -38,6 +38,7 @@ import org.hyperic.hq.appdef.server.session.Application; import org.hyperic.hq.appdef.server.session.ApplicationType; +import org.hyperic.hq.appdef.shared.AppServiceValue; import org.hyperic.hq.appdef.shared.AppdefDuplicateNameException; import org.hyperic.hq.appdef.shared.AppdefEntityConstants; import org.hyperic.hq.appdef.shared.AppdefEntityID; @@ -329,8 +330,9 @@ * Find application by name * @param subject - who * @param name - name of app + * @ejb:interface-method */ - private Application findApplicationByName(AuthzSubject subject, String name) + public Application findApplicationByName(AuthzSubject subject, String name) throws ApplicationNotFoundException, PermissionException { Application app = getApplicationDAO().findByName(name); if (app == null) { @@ -400,6 +402,21 @@ } /** + * @return {@link List} of {@link Resource} + * @ejb:interface-method + */ + public List getApplicationResources(AuthzSubject subject, Integer appId) + throws ApplicationNotFoundException, PermissionException { + List services = getApplicationServices(subject, appId); + List rtn = new ArrayList(services.size()); + for (Iterator it=services.iterator(); it.hasNext(); ) { + AppServiceValue val = (AppServiceValue) it.next(); + rtn.add(val.getService().getResource()); + } + return rtn; + } + + /** * Get all the application services for this application * @param subject * @param appId 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-13 01:35:56 UTC (rev 14169) +++ branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/bizapp/server/session/MeasurementBossEJBImpl.java 2010-01-13 01:38:00 UTC (rev 14170) @@ -2983,7 +2983,15 @@ public double getAvailability(AuthzSubject subj, AppdefEntityID id) throws AppdefEntityNotFoundException, PermissionException { - return getAvailability(subj, id, null, null); + final Map measCache = + getMetricManager().getAvailMeasurements(Collections.singleton(id)); + Map availCache = null; + if (id.isApplication()) { + List members = + getApplicationManager().getApplicationResources(subj, id.getId()); + availCache = getAvailManager().getLastAvail(members, measCache); + } + return getAvailability(subj, id, measCache, availCache); } /** @@ -2997,9 +3005,8 @@ throws AppdefEntityNotFoundException, PermissionException { StopWatch watch = new StopWatch(); - if (_log.isDebugEnabled()) - _log.debug("BEGIN getAvailability()"); - + final boolean debug = _log.isDebugEnabled(); + if (debug) _log.debug("BEGIN getAvailability()"); try { if (id.isGroup()) { return getGroupAvailability( @@ -3007,18 +3014,22 @@ } else if (id.isApplication()) { AppdefEntityValue appVal = new AppdefEntityValue(id, subject); + if (debug) watch.markTimeBegin("getFlattenedServiceIds"); AppdefEntityID[] services = appVal.getFlattenedServiceIds(); + if (debug) watch.markTimeEnd("getFlattenedServiceIds"); - return getAggregateAvailability( + 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 (_log.isDebugEnabled()) - _log.debug("END getAvailability() -- " + watch); + if (debug) _log.debug("END getAvailability() -- " + watch); } } Modified: branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/bizapp/server/session/MetricSessionEJB.java =================================================================== --- branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/bizapp/server/session/MetricSessionEJB.java 2010-01-13 01:35:56 UTC (rev 14169) +++ branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/bizapp/server/session/MetricSessionEJB.java 2010-01-13 01:38:00 UTC (rev 14170) @@ -39,12 +39,8 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.hyperic.hq.appdef.Agent; -import org.hyperic.hq.appdef.server.session.AgentManagerEJBImpl; import org.hyperic.hq.appdef.server.session.AppdefResource; import org.hyperic.hq.appdef.server.session.PlatformType; -import org.hyperic.hq.appdef.shared.AgentManagerLocal; -import org.hyperic.hq.appdef.shared.AgentNotFoundException; import org.hyperic.hq.appdef.shared.AppdefCompatException; import org.hyperic.hq.appdef.shared.AppdefEntityConstants; import org.hyperic.hq.appdef.shared.AppdefEntityID; @@ -83,6 +79,7 @@ import org.hyperic.hq.measurement.shared.MeasurementManagerLocal; import org.hyperic.hq.product.MetricValue; import org.hyperic.util.pager.PageControl; +import org.hyperic.util.timer.StopWatch; public class MetricSessionEJB extends BizappSessionEJB { private Log log = LogFactory.getLog(MetricSessionEJB.class.getName()); @@ -407,47 +404,58 @@ throws ApplicationNotFoundException, AppdefEntityNotFoundException, PermissionException { - final AgentManagerLocal agentMan = AgentManagerEJBImpl.getOne(); final double[] result = new double[ids.length]; Arrays.fill(result, MeasurementConstants.AVAIL_UNKNOWN); final Map data = new HashMap(); final MeasurementManagerLocal mMan = getMetricManager(); final ResourceManagerLocal rMan = getResourceManager(); final AvailabilityManagerLocal aMan = getAvailManager(); + final StopWatch watch = new StopWatch(); + final boolean debug = log.isDebugEnabled(); + final Map prefetched = new HashMap(); if (midMap.size() > 0) { final List mids = new ArrayList(); final List aeids = Arrays.asList(ids); + final int size = aeids.size(); for (final Iterator it=aeids.iterator(); it.hasNext(); ) { final AppdefEntityID aeid = (AppdefEntityID)it.next(); + if (debug) watch.markTimeBegin("findResource size=" + size); final Resource r = rMan.findResource(aeid); + if (debug) watch.markTimeEnd("findResource size=" + size); + prefetched.put(aeid, r); if (r == null || r.isInAsyncDeleteState()) { continue; } - Measurement meas; - if (null == midMap || - null == (meas = (Measurement)midMap.get(r.getId()))) { + Measurement meas = (Measurement)midMap.get(r.getId()); + if (null == midMap || null == meas) { + if (debug) watch.markTimeBegin("getAvailabilityMeasurement"); meas = mMan.getAvailabilityMeasurement(r); + if (debug) watch.markTimeEnd("getAvailabilityMeasurement"); } if (meas == null) { continue; } - MetricValue mv; - if (null != availCache && - null != (mv = (MetricValue)availCache.get(meas.getId()))) { - data.put(meas.getId(), mv); + if (availCache != null) { + MetricValue mv = (MetricValue)availCache.get(meas.getId()); + if (mv != null) { + data.put(meas.getId(), mv); + } else { + mids.add(meas.getId()); + } } else { mids.add(meas.getId()); } } + if (debug) watch.markTimeBegin("getLastAvail"); data.putAll( aMan.getLastAvail((Integer[])mids.toArray(new Integer[0]))); + if (debug) watch.markTimeEnd("getLastAvail"); } - - // Organize by agent - HashMap toGetLive = new HashMap(); - for (int i = 0; i < ids.length; i++) { - final Resource r = rMan.findResource(ids[i]); + Resource r = (Resource) prefetched.get(ids[i]); + if (r == null) { + r = rMan.findResource(ids[i]); + } if (r == null || r.isInAsyncDeleteState()) { continue; } @@ -456,21 +464,6 @@ MetricValue mval = null; if (null != (mval = (MetricValue)data.get(mid))) { result[i] = mval.getValue(); - } else { - // First figure out if the agent of this appdef entity - // already has a list - try { - Agent agent = agentMan.getAgent(ids[i]); - List toGetLiveList; - if (null == (toGetLiveList = (List)toGetLive.get(agent))) { - toGetLiveList = new ArrayList(); - toGetLive.put(agent, toGetLiveList); - } - // Now add to list - toGetLiveList.add(new Integer(i)); - } catch (AgentNotFoundException e) { - result[i] = AVAIL_DOWN; - } } } else { // cases for abstract resources whose availability are xor'd @@ -478,21 +471,28 @@ case AppdefEntityConstants.APPDEF_TYPE_APPLICATION : AppdefEntityValue appVal = new AppdefEntityValue(ids[i], subject); + if (debug) watch.markTimeBegin("getFlattenedServiceIds"); AppdefEntityID[] services = appVal.getFlattenedServiceIds(); + if (debug) watch.markTimeEnd("getFlattenedServiceIds"); + if (debug) watch.markTimeBegin("getAggregateAvailability"); result[i] = getAggregateAvailability( subject, services, null, availCache); + if (debug) watch.markTimeEnd("getAggregateAvailability"); break; case AppdefEntityConstants.APPDEF_TYPE_GROUP : + if (debug) watch.markTimeBegin("getGroupAvailability"); result[i] = getGroupAvailability( subject, ids[i].getId(), null, null); + if (debug) watch.markTimeEnd("getGroupAvailability"); break; default : break; } } } + if (debug) log.debug(watch); return result; } @@ -533,39 +533,31 @@ Map measCache, Map availCache) throws AppdefEntityNotFoundException, PermissionException { - if (ids.length == 0) + if (ids.length == 0) { return MeasurementConstants.AVAIL_UNKNOWN; - - // Break them up and do 5 at a time - int length = 5; + } + final StopWatch watch = new StopWatch(); + final boolean debug = log.isDebugEnabled(); double sum = 0; int count = 0; int unknownCount = 0; final Map midMap = getMidMap(ids, measCache); - for (int ind = 0; ind < ids.length; ind += length) { - if (ids.length - ind < length) { - length = ids.length - ind; - } - AppdefEntityID[] subids = new AppdefEntityID[length]; - for (int i = ind; i < ind + length; i++) { - subids[i - ind] = ids[i]; - } - double[] avails = - getAvailability(subject, subids, midMap, availCache); - for (int i = 0; i < avails.length; i++) { - if (avails[i] == MeasurementConstants.AVAIL_UNKNOWN) { - unknownCount++; - } else { - sum += avails[i]; - count++; - } + if (debug) watch.markTimeBegin("getAvailability"); + double[] avails = getAvailability(subject, ids, midMap, availCache); + if (debug) watch.markTimeEnd("getAvailability"); + for (int i = 0; i < avails.length; i++) { + if (avails[i] == MeasurementConstants.AVAIL_UNKNOWN) { + unknownCount++; + } else { + sum += avails[i]; + count++; } - } - - if (unknownCount == ids.length) + } + if (unknownCount == ids.length) { // All resources are unknown return MeasurementConstants.AVAIL_UNKNOWN; - + } + if (debug) log.debug(watch); return sum / count; } @@ -601,6 +593,8 @@ } /** + * @param measCache {@link Map} of {@link Integer} of Resource.getId() to + * {@link List} of {@link Measurement} * @return {@link Map} of {@link Integer} to {@link Measurement} * Integer = Resource.getId() */ @@ -609,15 +603,20 @@ final ResourceManagerLocal rMan = getResourceManager(); final MeasurementManagerLocal mMan = getMetricManager(); final List toGet = new ArrayList(); - for (final Iterator it=Arrays.asList(ids).iterator(); it.hasNext(); ) { + final boolean debug = log.isDebugEnabled(); + final StopWatch watch = new StopWatch(); + final List aeids = Arrays.asList(ids); + final int size = aeids.size(); + for (final Iterator it=aeids.iterator(); it.hasNext(); ) { final AppdefEntityID id = (AppdefEntityID)it.next(); if (id == null) { continue; } + if (debug) watch.markTimeBegin("findResource size=" + size); final Resource res = rMan.findResource(id); - List list; - if (null != measCache && - null != (list = (List)measCache.get(res.getId()))) { + if (debug) watch.markTimeEnd("findResource size=" + size); + List list = (List) measCache.get(res.getId()); + if (null != measCache && null != list) { if (list.size() > 1) { log.warn("resourceId " + res.getId() + " has more than one availability measurement " + @@ -631,7 +630,9 @@ toGet.add(res); } } + if (debug) watch.markTimeBegin("getAvailMeasurements"); final Map measMap = mMan.getAvailMeasurements(toGet); + if (debug) watch.markTimeEnd("getAvailMeasurements"); for (final Iterator it=measMap.entrySet().iterator(); it.hasNext(); ) { final Map.Entry entry = (Map.Entry)it.next(); final Integer id = (Integer)entry.getKey(); @@ -648,6 +649,7 @@ } rtn.put(r.getId(), m); } + if (debug) log.debug(watch); return rtn; } Modified: branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/measurement/server/session/AvailabilityManagerEJBImpl.java =================================================================== --- branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/measurement/server/session/AvailabilityManagerEJBImpl.java 2010-01-13 01:35:56 UTC (rev 14169) +++ branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/measurement/server/session/AvailabilityManagerEJBImpl.java 2010-01-13 01:38:00 UTC (rev 14170) @@ -554,7 +554,9 @@ * {@link AppdefEntityId}, {@link AppdefEntityValue}, * {@link AppdefResourceValue} or {@link Integer} * @param measCache Map<Integer, List> optional arg (may be null) to supply - * measurement id(s) of ResourceIds. Integer => Resource.getId() + * measurement id(s) of ResourceIds. Integer => Resource.getId(). If a + * measurement is not specified in the measCache parameter it will be added + * to the map * @return Map<Integer, MetricValue> Integer => Measurement.getId() * @ejb:interface-method */ @@ -601,6 +603,15 @@ final List mids = (List)it.next(); for (final Iterator iter=mids.iterator(); iter.hasNext(); ) { final Measurement m = (Measurement)iter.next(); + // populate the Map if value doesn't exist + if (measCache != null && measCache != Collections.EMPTY_MAP) { + List measids = (List) measCache.get(m.getResource().getId()); + if (measids == null) { + measids = new ArrayList(); + } + measids.add(m); + measCache.put(m.getResource(), m); + } midsToGet.add(m.getId()); } } |