From: <sc...@hy...> - 2010-01-19 01:05:47
|
Author: scottmf Date: 2010-01-18 17:05:35 -0800 (Mon, 18 Jan 2010) New Revision: 14195 URL: http://svn.hyperic.org/?view=rev&root=Hyperic+HQ&revision=14195 Modified: branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/appdef/server/session/AppServiceDAO.java branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/appdef/server/session/ServiceManagerEJBImpl.java Log: [HHQ-3667] rewrote findByApplication_order queries so that the application does most of the work by avoiding unneccessary db joins. Added timings. Modified: branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/appdef/server/session/AppServiceDAO.java =================================================================== --- branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/appdef/server/session/AppServiceDAO.java 2010-01-19 01:02:12 UTC (rev 14194) +++ branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/appdef/server/session/AppServiceDAO.java 2010-01-19 01:05:35 UTC (rev 14195) @@ -1,6 +1,7 @@ package org.hyperic.hq.appdef.server.session; import java.util.Collection; +import java.util.Comparator; import java.util.Iterator; import java.util.List; @@ -11,11 +12,14 @@ import org.hyperic.hq.appdef.AppSvcDependency; import org.hyperic.hq.appdef.ServiceCluster; import org.hyperic.hq.appdef.shared.ServiceManagerLocal; +import org.hyperic.hq.authz.server.session.Resource; import org.hyperic.hq.authz.server.session.ResourceGroup; import org.hyperic.hq.authz.server.session.ResourceGroupDAO; import org.hyperic.hq.authz.shared.AuthzConstants; import org.hyperic.hq.dao.HibernateDAO; +import edu.emory.mathcs.backport.java.util.Collections; + /* * NOTE: This copyright does *not* cover user programs that use HQ * program services by normal system calls through the application @@ -152,42 +156,57 @@ return create(aClusterPK, a); } - public List findByApplication_orderName(Integer id) - { - // TODO: fix this query after authz conversion - String sql= - "select distinct a from " + - "AppService a, Resource r, ResourceType t " + - "where a.application.id=:appid and (" + - "r.resourceType.id=t.id AND t.name=:groupType "+ - "AND a.resourceGroup.id IN (" + - "SELECT id FROM ResourceGroup g where g.id = r.instanceId)"+ - " OR " + - "(r.instanceId=a.service.id and " + - "r.resourceType.id=t.id AND t.name=:serviceType))) " + - "order by r.name"; - return getSession().createQuery(sql) - .setInteger("appid", id.intValue()) - .setString("groupType", groupResType) - .setString("serviceType", serviceResType) - .list(); + public List findByApplication_orderName(Integer id) { + List list = findByApplication(id); + Comparator c = new Comparator() { + private String name = null; + public int compare(Object arg0, Object arg1) { + AppService app0 = (AppService) arg0; + AppService app1 = (AppService) arg1; + return getName(app0).compareTo(getName(app1)); + } + private String getName(AppService app) { + if (name != null) { + return name; + } + Resource res = (app.isIsGroup()) ? + app.getResourceGroup().getResource() : + app.getService().getResource(); + name = (res == null || res.isInAsyncDeleteState()) ? + "" : res.getName(); + return name; + } + }; + Collections.sort(list, c); + return list; } - public List findByApplication_orderType(Integer id, boolean asc) + public List findByApplication_orderType(Integer id, final boolean asc) { - String sql="select distinct a from AppService a " + - " join fetch a.serviceType st " + - "where a.application.id=? " + - "order by st.name " + (asc ? "asc" : "desc"); - return getSession().createQuery(sql) - .setInteger(0, id.intValue()) - .list(); + List list = findByApplication(id); + Comparator c = new Comparator() { + private String type = null; + public int compare(Object arg0, Object arg1) { + AppService app0 = (AppService) arg0; + AppService app1 = (AppService) arg1; + return (asc) ? getType(app0).compareTo(getType(app1)) : + getType(app1).compareTo(getType(app0)); + } + private String getType(AppService app) { + if (type != null) { + return type; + } + type = app.getServiceType().getName(); + return type; + } + }; + Collections.sort(list, c); + return list; } - public Collection findByApplication(Integer id) + public List findByApplication(Integer id) { - String sql="select distinct a from AppService a " + - "where a.application.id=?"; + String sql="select a from AppService a where a.application.id=?"; return getSession().createQuery(sql) .setInteger(0, id.intValue()) .list(); Modified: branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/appdef/server/session/ServiceManagerEJBImpl.java =================================================================== --- branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/appdef/server/session/ServiceManagerEJBImpl.java 2010-01-19 01:02:12 UTC (rev 14194) +++ branches/HQ_4_2_0_PATCH/src/org/hyperic/hq/appdef/server/session/ServiceManagerEJBImpl.java 2010-01-19 01:05:35 UTC (rev 14195) @@ -58,6 +58,7 @@ import org.hyperic.hq.appdef.shared.ServiceManagerUtil; import org.hyperic.hq.appdef.AppService; import org.hyperic.hq.appdef.ConfigResponseDB; +import org.hyperic.hq.appdef.ServiceCluster; import org.hyperic.hq.authz.server.session.AuthzSubject; import org.hyperic.hq.authz.server.session.AuthzSubjectManagerEJBImpl; import org.hyperic.hq.authz.server.session.Resource; @@ -79,6 +80,7 @@ import org.hyperic.util.pager.PageList; import org.hyperic.util.pager.Pager; import org.hyperic.util.pager.SortAttribute; +import org.hyperic.util.timer.StopWatch; import org.hyperic.dao.DAOFactory; import org.hibernate.ObjectNotFoundException; import org.hyperic.hq.appdef.server.session.Platform; @@ -1145,20 +1147,30 @@ throw new ApplicationNotFoundException(appId, e); } + final StopWatch watch = new StopWatch(); + final boolean debug = log.isDebugEnabled(); Collection svcCollection = new ArrayList(); + if (debug) watch.markTimeBegin("getAppServices"); Collection appSvcCollection = appLocal.getAppServices(); + if (debug) watch.markTimeEnd("getAppServices"); Iterator it = appSvcCollection.iterator(); while (it != null && it.hasNext()) { AppService appService = (AppService) it.next(); if (appService.isIsGroup()) { - svcCollection.addAll(getServiceCluster( - appService.getResourceGroup()).getServices()); + if (debug) watch.markTimeBegin("getServiceCluster"); + ServiceCluster cluster = + getServiceCluster(appService.getResourceGroup()); + if (debug) watch.markTimeEnd("getServiceCluster"); + if (debug) watch.markTimeBegin("getServices"); + svcCollection.addAll(cluster.getServices()); + if (debug) watch.markTimeEnd("getServices"); } else { svcCollection.add(appService.getService()); } } + if (debug) log.debug(watch); return filterAndPage(svcCollection, subject, typeId, pc); } @@ -1200,10 +1212,14 @@ throws ServiceNotFoundException, PermissionException, ApplicationNotFoundException { - List serviceInventory = - getUnflattenedServiceInventoryByApplication( - subject, appId, PageControl.PAGE_ALL); + final boolean debug = log.isDebugEnabled(); + final StopWatch watch = new StopWatch(); + if (debug) watch.markTimeBegin("getUnflattenedServiceInventoryByApplication"); + List serviceInventory = getUnflattenedServiceInventoryByApplication( + subject, appId, PageControl.PAGE_ALL); + if (debug) watch.markTimeEnd("getUnflattenedServiceInventoryByApplication"); + List servicePKs = new ArrayList(); // flattening: open up all of the groups (if any) and get their services as well try { @@ -1223,12 +1239,13 @@ // any authz resource filtering on the group members happens // inside the group subsystem try { + if (debug) watch.markTimeBegin("getCompatGroupMembers"); List memberIds = GroupUtil.getCompatGroupMembers( subject, groupId, null, PageControl.PAGE_ALL); - for (Iterator memberIter = memberIds.iterator(); - memberIter.hasNext(); ) { + if (debug) watch.markTimeEnd("getCompatGroupMembers"); + for (Iterator it=memberIds.iterator(); it.hasNext(); ) { AppdefEntityID memberEntId = - (AppdefEntityID) memberIter.next(); + (AppdefEntityID) it.next(); servicePKs.add(memberEntId.getId()); } } catch (PermissionException e) { @@ -1245,18 +1262,16 @@ throw new ServiceNotFoundException("could not return all services", e); } - - return (Integer[]) servicePKs.toArray( - new Integer[servicePKs.size()]); + if (debug) log.debug(watch); + return (Integer[]) servicePKs.toArray(new Integer[servicePKs.size()]); } private List getUnflattenedServiceInventoryByApplication( AuthzSubject subject, Integer appId, PageControl pc) - throws ApplicationNotFoundException, ServiceNotFoundException { - - AppServiceDAO appServLocHome; - List appServiceCollection; - + throws ApplicationNotFoundException, ServiceNotFoundException { + final boolean debug = log.isDebugEnabled(); + final StopWatch watch = new StopWatch(); + try { getApplicationDAO().findById(appId); } catch (ObjectNotFoundException e) { @@ -1266,19 +1281,22 @@ pc = PageControl.initDefaults(pc, SortAttribute.SERVICE_NAME); - appServLocHome = new AppServiceDAO(DAOFactory.getDAOFactory()); + AppServiceDAO appServDAO = new AppServiceDAO(DAOFactory.getDAOFactory()); + Collection appServices = null; switch (pc.getSortattribute()) { case SortAttribute.SERVICE_NAME : case SortAttribute.RESOURCE_NAME : case SortAttribute.NAME : // TODO: Not actually sorting - appServiceCollection = - appServLocHome.findByApplication_orderName(appId); + if (debug) watch.markTimeBegin("findByApplication_orderName"); + appServices = appServDAO.findByApplication_orderName(appId); + if (debug) watch.markTimeEnd("findByApplication_orderName"); break; case SortAttribute.SERVICE_TYPE : - appServiceCollection = - appServLocHome.findByApplication_orderType(appId, - pc.isAscending()); + if (debug) watch.markTimeBegin("findByApplication_orderType"); + appServices = appServDAO.findByApplication_orderType( + appId, pc.isAscending()); + if (debug) watch.markTimeEnd("findByApplication_orderType"); break; default : throw new IllegalArgumentException( @@ -1292,17 +1310,20 @@ // to authz in batches to find out which ones we are // allowed to return. - AppService appService; - Iterator i = appServiceCollection.iterator(); - List services = new ArrayList(); - while (i.hasNext()) { - appService = (AppService) i.next(); + List services = new ArrayList(appServices.size()); + for (Iterator it=appServices.iterator(); it.hasNext(); ) { + AppService appService = (AppService) it.next(); if (appService.isIsGroup()) { + if (debug) watch.markTimeBegin("appService.getResourceGroup"); services.add(appService.getResourceGroup()); + if (debug) watch.markTimeEnd("appService.getResourceGroup"); } else { + if (debug) watch.markTimeBegin("appService.getService"); services.add(appService.getService()); + if (debug) watch.markTimeEnd("appService.getService"); } } + if (debug) log.debug(watch); return services; } |