From: <sc...@hy...> - 2010-02-27 02:37:07
|
Author: scottmf Date: 2010-02-26 17:34:00 -0800 (Fri, 26 Feb 2010) New Revision: 14331 URL: http://svn.hyperic.org/?view=rev&root=Hyperic+HQ&revision=14331 Modified: trunk/src/org/hyperic/hq/bizapp/server/session/EventsBossEJBImpl.java trunk/src/org/hyperic/hq/events/server/session/ActionDAO.java trunk/src/org/hyperic/hq/events/server/session/AlertDefinitionDAO.java trunk/src/org/hyperic/hq/events/server/session/AlertDefinitionManagerEJBImpl.java Log: [HHQ-3534] separate cleanupAlertDefs action into different hibernate sessions and prefetched all related pojos to ehcache before delete starts. Modified: trunk/src/org/hyperic/hq/bizapp/server/session/EventsBossEJBImpl.java =================================================================== --- trunk/src/org/hyperic/hq/bizapp/server/session/EventsBossEJBImpl.java 2010-02-27 00:51:11 UTC (rev 14330) +++ trunk/src/org/hyperic/hq/bizapp/server/session/EventsBossEJBImpl.java 2010-02-27 01:34:00 UTC (rev 14331) @@ -49,7 +49,6 @@ import org.hyperic.hibernate.PageInfo; import org.hyperic.hq.appdef.server.session.AppdefResourceType; import org.hyperic.hq.appdef.server.session.ResourceDeletedZevent; -import org.hyperic.hq.appdef.server.session.ResourceZevent; import org.hyperic.hq.appdef.shared.AppdefEntityConstants; import org.hyperic.hq.appdef.shared.AppdefEntityID; import org.hyperic.hq.appdef.shared.AppdefEntityNotFoundException; @@ -73,7 +72,6 @@ import org.hyperic.hq.authz.shared.PermissionException; import org.hyperic.hq.authz.shared.PermissionManagerFactory; import org.hyperic.hq.authz.shared.ResourceGroupManagerLocal; -import org.hyperic.hq.bizapp.server.trigger.conditional.ConditionalTriggerInterface; import org.hyperic.hq.bizapp.shared.EventsBossLocal; import org.hyperic.hq.bizapp.shared.EventsBossUtil; import org.hyperic.hq.common.ApplicationException; @@ -109,7 +107,6 @@ import org.hyperic.hq.events.server.session.AlertManagerEJBImpl; import org.hyperic.hq.events.server.session.AlertSortField; import org.hyperic.hq.events.server.session.ClassicEscalationAlertType; -import org.hyperic.hq.events.server.session.EventsStartupListener; import org.hyperic.hq.events.server.session.RegisteredTriggerManagerEJBImpl; import org.hyperic.hq.events.server.session.TriggersCreatedZevent; import org.hyperic.hq.events.shared.ActionManagerLocal; @@ -120,7 +117,6 @@ import org.hyperic.hq.events.shared.AlertManagerLocal; import org.hyperic.hq.events.shared.MaintenanceEventManagerInterface; import org.hyperic.hq.events.shared.RegisteredTriggerManagerLocal; -import org.hyperic.hq.events.shared.RegisteredTriggerValue; import org.hyperic.hq.galerts.server.session.GalertDef; import org.hyperic.hq.galerts.server.session.GalertEscalationAlertType; import org.hyperic.hq.galerts.server.session.GalertLogSortField; @@ -1835,27 +1831,41 @@ inst.addBufferedListener( events, new ZeventListener() { public void processEvents(List events) { - final StopWatch watch = new StopWatch(); - final Log log = LogFactory.getLog(this.getClass().getName()); - final boolean debug = log.isDebugEnabled(); - final AlertDefinitionManagerLocal adm = getADM(); + final int batchSize = 500; final List alertDefs = getADM().getAllDeletedAlertDefs(); - final int batchSize = 500; - for (int i=0; i<alertDefs.size(); i+=batchSize) { - final int end = Math.min(i+batchSize, alertDefs.size()); - final List sublist = alertDefs.subList(i, end); - // can't pass in pojos since the session changes - final List ids = new ArrayList(sublist.size()); - for (final Iterator it=sublist.iterator(); it.hasNext(); ) { - final AlertDefinition def = (AlertDefinition) it.next(); - ids.add(def.getId()); + try { + final int size = alertDefs.size(); + for (int ii=0; ii<size; ii+=batchSize) { + final int end = Math.min(size, ii+batchSize); + final List list = alertDefs.subList(ii, end); + org.hyperic.hq.hibernate.SessionManager.runInSession( + new org.hyperic.hq.hibernate.SessionManager.SessionRunner() { + public void run() { + deleteAlertDefs(list); + } + public String getName() { + return "CleanupAlertDefRunner"; + } + } + ); } - if (debug) watch.markTimeBegin("cleanupAlertDefs"); - adm.cleanupAlertDefs(ids); - if (debug) watch.markTimeEnd("cleanupAlertDefs"); + } catch (Exception e) { + _log.error(e,e); } - if (debug) log.debug(watch); } + + private void deleteAlertDefs(List defIds) { + final StopWatch watch = new StopWatch(); + final boolean debug = _log.isDebugEnabled(); + final AlertDefinitionManagerLocal adm = getADM(); + if (defIds.size() == 0) { + return; + } + if (debug) watch.markTimeBegin("cleanupAlertDefs"); + adm.cleanupAlertDefs(defIds); + if (debug) watch.markTimeEnd("cleanupAlertDefs"); + if (debug) _log.debug(watch); + } public String toString() { return "AlertDefCleanupListener"; Modified: trunk/src/org/hyperic/hq/events/server/session/ActionDAO.java =================================================================== --- trunk/src/org/hyperic/hq/events/server/session/ActionDAO.java 2010-02-27 00:51:11 UTC (rev 14330) +++ trunk/src/org/hyperic/hq/events/server/session/ActionDAO.java 2010-02-27 01:34:00 UTC (rev 14331) @@ -25,8 +25,11 @@ package org.hyperic.hq.events.server.session; import java.util.ArrayList; +import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import org.hibernate.Query; import org.hibernate.criterion.Restrictions; @@ -54,18 +57,26 @@ super.remove(entity); } - private void removeActionCascade(Action action) { - if (action.getParent() != null) { - action.getParent().getChildrenBag().remove(action); - } - remove(action); - } - void removeActions(AlertDefinition def) { + Map parentToActions = new HashMap(); for (Iterator it = def.getActions().iterator(); it.hasNext(); ) { Action action = (Action) it.next(); - removeActionCascade(action); + List list; + if (null == (list = (List)parentToActions.get(action.getParent()))) { + list = new ArrayList(); + parentToActions.put(action.getParent(), list); + } + list.add(action); } + for (Iterator it = parentToActions.entrySet().iterator(); it.hasNext(); ) { + Map.Entry entry = (Entry) it.next(); + Action parent = (Action) entry.getKey(); + if (parent == null) { + continue; + } + List actions = (List) entry.getValue(); + parent.getChildrenBag().removeAll(actions); + } def.clearActions(); } @@ -73,7 +84,10 @@ if (action.getAlertDefinition() != null) { action.getAlertDefinition().getActionsBag().remove(action); } - removeActionCascade(action); + if (action.getParent() != null) { + action.getParent().getChildrenBag().remove(action); + } + remove(action); } void deleteAlertDefinition(AlertDefinition def) { Modified: trunk/src/org/hyperic/hq/events/server/session/AlertDefinitionDAO.java =================================================================== --- trunk/src/org/hyperic/hq/events/server/session/AlertDefinitionDAO.java 2010-02-27 00:51:11 UTC (rev 14330) +++ trunk/src/org/hyperic/hq/events/server/session/AlertDefinitionDAO.java 2010-02-27 01:34:00 UTC (rev 14331) @@ -73,11 +73,43 @@ return createCriteria().add(Restrictions.eq("resource", r)).list(); } - public List findAllDeletedResources() { - return createCriteria() - .add(Restrictions.isNull("resource")) - .add(Restrictions.eq("deleted", Boolean.TRUE)) - .list(); + /** + * Prefetches all collections associated with each alertDef that is deleted and has a + * null resourceId into ehcache. + * @return {@link List} of {@link Integer} of {@link AlertDefintion} ids + */ + public List findAndPrefetchAllDeletedAlertDefs() { + // need to pre-fetch one bag at a time due to bug + // http://opensource.atlassian.com/projects/hibernate/browse/HHH-2980 + String hql = new StringBuilder() + .append("from AlertDefinition def ") + .append("left outer join fetch def.childrenBag cb ") + .append("where def.resource is null and def.deleted = '1'") + .toString(); + getSession().createQuery(hql).list(); + hql = new StringBuilder() + .append("from AlertDefinition def ") + .append("left outer join fetch def.actionsBag ab ") + .append("where def.resource is null and def.deleted = '1'") + .toString(); + getSession().createQuery(hql).list(); + hql = new StringBuilder() + .append("from AlertDefinition def ") + .append("left outer join fetch def.conditionsBag condb ") + .append("where def.resource is null and def.deleted = '1'") + .toString(); + getSession().createQuery(hql).list(); + hql = new StringBuilder() + .append("from AlertDefinition def ") + .append("left outer join fetch def.triggersBag tb ") + .append("where def.resource is null and def.deleted = '1'") + .toString(); + getSession().createQuery(hql).list(); + hql = new StringBuilder() + .append("select def.id from AlertDefinition def ") + .append("where def.resource is null and def.deleted = '1'") + .toString(); + return getSession().createQuery(hql).list(); } /** Modified: trunk/src/org/hyperic/hq/events/server/session/AlertDefinitionManagerEJBImpl.java =================================================================== --- trunk/src/org/hyperic/hq/events/server/session/AlertDefinitionManagerEJBImpl.java 2010-02-27 00:51:11 UTC (rev 14330) +++ trunk/src/org/hyperic/hq/events/server/session/AlertDefinitionManagerEJBImpl.java 2010-02-27 01:34:00 UTC (rev 14331) @@ -792,10 +792,13 @@ } /** + * Prefetches all collections associated with each alertDef that is deleted and has a + * null resourceId into ehcache. + * @return {@link List} of {@link Integer} of {@link AlertDefintion} ids * @ejb:interface-method */ public List getAllDeletedAlertDefs() { - return getAlertDefDAO().findAllDeletedResources(); + return getAlertDefDAO().findAndPrefetchAllDeletedAlertDefs(); } /** @@ -811,11 +814,14 @@ final AlertDAO dao = getAlertDAO(); final AlertDefinitionDAO aDao = getAlertDefDAO(); final ActionDAO actionDAO = getActionDAO(); + int i=0; try { final List alertDefs = new ArrayList(alertDefIds.size()); - for (final Iterator i = alertDefIds.iterator(); i.hasNext();) { - final Integer alertdefId = (Integer) i.next(); + for (final Iterator it = alertDefIds.iterator(); it.hasNext();) { + final Integer alertdefId = (Integer) it.next(); + if (debug) watch.markTimeBegin("findById"); final AlertDefinition alertdef = aDao.findById(alertdefId); + if (debug) watch.markTimeEnd("findById"); alertDefs.add(alertdef); } // Delete the alerts @@ -823,8 +829,9 @@ dao.deleteByAlertDefinitions(alertDefs); if (debug) watch.markTimeEnd("deleteByAlertDefinition"); - for (final Iterator i = alertDefs.iterator(); i.hasNext();) { - final AlertDefinition alertdef = (AlertDefinition) i.next(); + if (debug) watch.markTimeBegin("loop"); + for (final Iterator it = alertDefs.iterator(); it.hasNext();) { + final AlertDefinition alertdef = (AlertDefinition) it.next(); // Remove the conditions if (debug) watch.markTimeBegin("remove conditions and triggers"); @@ -847,10 +854,12 @@ if (debug) watch.markTimeBegin("remove"); aDao.remove(alertdef); if (debug) watch.markTimeEnd("remove"); + i++; } + if (debug) watch.markTimeEnd("loop"); } finally { - if (debug) log.debug("deleted " + alertDefIds.size() + " alertDefs: " + watch); + if (debug) log.debug("deleted " + i + " alertDefs: " + watch); } } |