Skip to content
This repository has been archived by the owner on Jul 11, 2022. It is now read-only.

Commit

Permalink
[BZ 1059412] Applying patch to speed up OOB calculations
Browse files Browse the repository at this point in the history
(cherry picked from commit 3b9bbfd)
Signed-off-by: Lukas Krejci <lkrejci@redhat.com>
  • Loading branch information
John Sanda authored and metlos committed Jun 21, 2014
1 parent 715a848 commit 6eb9c50
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
@NamedQuery(name = MeasurementBaseline.QUERY_DELETE_BY_COMPUTE_TIME, query = "" //
+ "DELETE MeasurementBaseline bl " //
+ " WHERE bl.computeTime < :timestamp "),
@NamedQuery(name = MeasurementBaseline.QUERY_BY_SCHEDULE_IDS, query = "select baseline from MeasurementBaseline baseline where baseline.schedule.id IN (:scheduleIds)"),
@NamedQuery(name = MeasurementBaseline.QUERY_DELETE_BY_RESOURCES, query = "DELETE MeasurementBaseline bl WHERE bl.schedule IN ( SELECT ms FROM MeasurementSchedule ms WHERE ms.resource.id IN ( :resourceIds ) )") })
@SequenceGenerator(allocationSize = org.rhq.core.domain.util.Constants.ALLOCATION_SIZE, name = "RHQ_MEASUREMENT_BLINE_ID_SEQ", sequenceName = "RHQ_MEASUREMENT_BLINE_ID_SEQ")
@SuppressWarnings("unused")
Expand All @@ -69,6 +70,7 @@ public class MeasurementBaseline implements Serializable {
public static final String QUERY_DELETE_BY_RESOURCES = "MeasurementBaseline.deleteByResources";
public static final String QUERY_CALC_FIRST_AUTOBASELINE = "MeasurementBaseline.calcFirstAutoBaseline";
public static final String QUERY_DELETE_EXISTING_AUTOBASELINES = "MeasurementBaseline.deleteExistingAutoBaseline";
public static final String QUERY_BY_SCHEDULE_IDS = "MeasurementBaseline.queryByScheduleIds";

private static final long serialVersionUID = 1L;
@GeneratedValue(strategy = GenerationType.AUTO, generator = "RHQ_MEASUREMENT_BLINE_ID_SEQ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -71,6 +72,7 @@
@javax.annotation.Resource(name = "RHQ_DS", mappedName = RHQConstants.DATASOURCE_JNDI_NAME)
public class MeasurementOOBManagerBean implements MeasurementOOBManagerLocal {

private static final int BATCH_SIZE = 500;
private final Log log = LogFactory.getLog(MeasurementOOBManagerBean.class);

@PersistenceContext(unitName = RHQConstants.PERSISTENCE_UNIT_NAME)
Expand Down Expand Up @@ -199,13 +201,44 @@ public void computeOOBsForLastHour(Subject subject, Iterable<AggregateNumericMet
log.info("Computing OOBs");
int count = 0;
long startTime = System.currentTimeMillis();

try {
for (AggregateNumericMetric metric : metrics) {
try {
count += oobManager.calculateOOB(metric);
} catch (Exception e) {
log.error("An error occurred while calculating OOBs for " + metric, e);
throw new RuntimeException(e);
Iterator<AggregateNumericMetric> iterator = metrics.iterator();
while (iterator.hasNext()) {

List<Integer> scheduleIds = new ArrayList<Integer>(BATCH_SIZE);
List<AggregateNumericMetric> metricList = new ArrayList<AggregateNumericMetric>(BATCH_SIZE);

int i = 0;
do {
AggregateNumericMetric aggregate = iterator.next();
scheduleIds.add(aggregate.getScheduleId());
metricList.add(aggregate);
i++;
} while (i < BATCH_SIZE && iterator.hasNext());

Query q = entityManager.createNamedQuery(MeasurementBaseline.QUERY_BY_SCHEDULE_IDS);
q.setParameter("scheduleIds",scheduleIds);
List<MeasurementBaseline> tmpList = q.getResultList();

// put the result in a HashMap to speed up query later
Map<Integer,MeasurementBaseline> baselineMap = new HashMap<Integer, MeasurementBaseline>(tmpList.size());
for (MeasurementBaseline baseline : tmpList) {
baselineMap.put(baseline.getScheduleId(),baseline);
}

for (AggregateNumericMetric metric : metricList) {
MeasurementBaseline baseline = baselineMap.get(metric.getScheduleId());
if (baseline==null)
continue;

try {
count += oobManager.calculateOOB(metric,baseline);
} catch (Exception e) {
log.error("An error occurred while calculating OOBs for " + metric, e);
throw new RuntimeException(e);
}

}
}
} finally {
Expand All @@ -218,15 +251,11 @@ public void computeOOBsForLastHour(Subject subject, Iterable<AggregateNumericMet

@SuppressWarnings("unchecked")
@TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW)
public int calculateOOB(AggregateNumericMetric metric) {
List<MeasurementBaseline> baselines = entityManager.createQuery(
"select baseline from MeasurementBaseline baseline where baseline.schedule.id = :scheduleId")
.setParameter("scheduleId", metric.getScheduleId())
.getResultList();
if (baselines.isEmpty()) {
public int calculateOOB(AggregateNumericMetric metric,MeasurementBaseline baseline) {
if (baseline==null) {
return 0;
}
MeasurementBaseline baseline = baselines.get(0);

Long upperDelta = null;
Long lowerDelta = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
*/
package org.rhq.enterprise.server.measurement;

import java.util.List;
import java.util.Map;

import javax.ejb.Local;

import org.rhq.core.domain.auth.Subject;
import org.rhq.core.domain.measurement.MeasurementBaseline;
import org.rhq.core.domain.measurement.MeasurementSchedule;
import org.rhq.core.domain.measurement.composite.MeasurementOOBComposite;
import org.rhq.core.domain.util.PageControl;
Expand Down Expand Up @@ -66,7 +70,7 @@ public interface MeasurementOOBManagerLocal {
* be generated
* @return 1 if an OOB is generated, 0 otherwise
*/
int calculateOOB(AggregateNumericMetric metric);
int calculateOOB(AggregateNumericMetric metric, MeasurementBaseline baseline);

/**
* Return OOB Composites that contain all information about the OOBs in a given time as aggregates.
Expand Down

0 comments on commit 6eb9c50

Please sign in to comment.