modules/core/domain/src/main/java/org/rhq/core/domain/measurement/MeasurementReport.java
| 52 +++
modules/core/plugin-api/src/main/java/org/rhq/core/pluginapi/measurement/MeasurementCollectorRunnable.java
| 2
modules/core/plugin-container/src/test/java/org/rhq/core/pc/CollectorThreadPoolTest.java
| 139 +++++++++-
3 files changed, 185 insertions(+), 8 deletions(-)
New commits:
commit a1c3d0e458a74756a4ec6954bf39cd01fa406560
Author: John Mazzitelli <mazz(a)redhat.com>
Date: Tue Jun 26 17:37:49 2012 -0400
[BZ 783603] only return the metrics we asked for in the getLastValues() call
diff --git
a/modules/core/domain/src/main/java/org/rhq/core/domain/measurement/MeasurementReport.java
b/modules/core/domain/src/main/java/org/rhq/core/domain/measurement/MeasurementReport.java
index 50b2d23..f812575 100644
---
a/modules/core/domain/src/main/java/org/rhq/core/domain/measurement/MeasurementReport.java
+++
b/modules/core/domain/src/main/java/org/rhq/core/domain/measurement/MeasurementReport.java
@@ -23,6 +23,7 @@
package org.rhq.core.domain.measurement;
import java.io.Serializable;
+import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
@@ -141,12 +142,53 @@ public class MeasurementReport implements Serializable {
* Adds measurement data from the given report and updates the collection time.
* The assumption is the given report is newer than this instance.
*
- * @param report measurements to add
+ * @param report the report that contains the measurements to add
+ * @param metrics the metrics whose measurement data (if found in the given report)
should be added to this report.
+ * If null or empty, then all of the data in the given report will be
added to this report.
*/
- public synchronized void add(MeasurementReport report) {
- measurementNumericData.addAll(report.measurementNumericData);
- measurementTraitData.addAll(report.measurementTraitData);
- callTimeData.addAll(report.callTimeData);
+ public synchronized void add(MeasurementReport report,
Set<MeasurementScheduleRequest> metrics) {
+ if (metrics == null || metrics.isEmpty()) {
+ measurementNumericData.addAll(report.measurementNumericData);
+ measurementTraitData.addAll(report.measurementTraitData);
+ callTimeData.addAll(report.callTimeData);
+ } else {
+ // note that usually the metric set is very small (typically not more than
around 5, probably normally around 1 or 2)
+ // so this loop isn't going to be performed with lots of iterations.
+ for (MeasurementScheduleRequest metric : metrics) {
+ switch (metric.getDataType()) {
+ case MEASUREMENT: {
+ Iterator<MeasurementDataNumeric> i =
report.measurementNumericData.iterator();
+ while (i.hasNext()) {
+ MeasurementDataNumeric data = i.next();
+ if (data.getName().equals(metric.getName())) {
+ measurementNumericData.add(data);
+ }
+ }
+ break;
+ }
+ case TRAIT: {
+ Iterator<MeasurementDataTrait> i =
report.measurementTraitData.iterator();
+ while (i.hasNext()) {
+ MeasurementDataTrait data = i.next();
+ if (data.getName().equals(metric.getName())) {
+ measurementTraitData.add(data);
+ }
+ }
+ break;
+ }
+ case CALLTIME: {
+ // There is only ever one calltime metric per resource so if we are
being asked to
+ // add the calltime data, we can avoid doing any iterations and just
use addAll API.
+ callTimeData.addAll(report.callTimeData);
+ break;
+ }
+ default: {
+ // ignore any others, we only care about measurement type data
+ }
+ }
+ }
+ }
+
setCollectionTime(report.collectionTime);
}
diff --git
a/modules/core/plugin-api/src/main/java/org/rhq/core/pluginapi/measurement/MeasurementCollectorRunnable.java
b/modules/core/plugin-api/src/main/java/org/rhq/core/pluginapi/measurement/MeasurementCollectorRunnable.java
index 19bcc9c..94c024b 100644
---
a/modules/core/plugin-api/src/main/java/org/rhq/core/pluginapi/measurement/MeasurementCollectorRunnable.java
+++
b/modules/core/plugin-api/src/main/java/org/rhq/core/pluginapi/measurement/MeasurementCollectorRunnable.java
@@ -145,7 +145,7 @@ public class MeasurementCollectorRunnable implements Runnable {
*/
public void getLastValues(MeasurementReport report,
Set<MeasurementScheduleRequest> metrics) throws Exception {
this.requestedMetrics.addAll(metrics);
- report.add(this.lastReport);
+ report.add(this.lastReport, metrics);
}
/**
diff --git
a/modules/core/plugin-container/src/test/java/org/rhq/core/pc/CollectorThreadPoolTest.java
b/modules/core/plugin-container/src/test/java/org/rhq/core/pc/CollectorThreadPoolTest.java
index dc00bee..f3a6310 100644
---
a/modules/core/plugin-container/src/test/java/org/rhq/core/pc/CollectorThreadPoolTest.java
+++
b/modules/core/plugin-container/src/test/java/org/rhq/core/pc/CollectorThreadPoolTest.java
@@ -22,6 +22,7 @@
*/
package org.rhq.core.pc;
+import java.util.Date;
import java.util.HashSet;
import java.util.Set;
@@ -33,9 +34,11 @@ import org.testng.annotations.Test;
import org.rhq.core.domain.measurement.AvailabilityType;
import org.rhq.core.domain.measurement.DataType;
+import org.rhq.core.domain.measurement.MeasurementDataNumeric;
import org.rhq.core.domain.measurement.MeasurementDataTrait;
import org.rhq.core.domain.measurement.MeasurementReport;
import org.rhq.core.domain.measurement.MeasurementScheduleRequest;
+import org.rhq.core.domain.measurement.calltime.CallTimeData;
import org.rhq.core.pluginapi.availability.AvailabilityCollectorRunnable;
import org.rhq.core.pluginapi.availability.AvailabilityFacet;
import org.rhq.core.pluginapi.measurement.MeasurementCollectorRunnable;
@@ -94,7 +97,7 @@ public class CollectorThreadPoolTest {
// 0L means do the initial collection immediately with no delay - so our test can
run fast
MeasurementCollectorRunnable runnable = new
MeasurementCollectorRunnable(component, 0L, 500L, null,
this.threadPool.getExecutor());
- Set<MeasurementScheduleRequest> metrics = new HashSet();
+ Set<MeasurementScheduleRequest> metrics = new
HashSet<MeasurementScheduleRequest>();
metrics.add(new MeasurementScheduleRequest(0, "name", 0, true,
DataType.TRAIT));
MeasurementReport report = new MeasurementReport();
runnable.getLastValues(report, metrics);
@@ -110,6 +113,96 @@ public class CollectorThreadPoolTest {
runnable.getLastValues(report, metrics);
assert 42 == report.getCollectionTime();
assert !report.getTraitData().isEmpty();
+
+ runnable.stop();
+ }
+
+ public void testMultipleMeasurements() throws Exception {
+ log("testMultipleMeasurements");
+
+ TestMultipleMeasumentFacet component = new TestMultipleMeasumentFacet();
+
+ Set<MeasurementScheduleRequest> onlyNumericMetric = new
HashSet<MeasurementScheduleRequest>();
+ onlyNumericMetric.add(component.getNumericMetricSchedule());
+
+ Set<MeasurementScheduleRequest> onlyTraitMetric = new
HashSet<MeasurementScheduleRequest>();
+ onlyTraitMetric.add(component.getTraitMetricSchedule());
+
+ Set<MeasurementScheduleRequest> onlyCalltimeMetric = new
HashSet<MeasurementScheduleRequest>();
+ onlyCalltimeMetric.add(component.getCalltimeMetricSchedule());
+
+ Set<MeasurementScheduleRequest> allMetrics = new
HashSet<MeasurementScheduleRequest>();
+ allMetrics.addAll(onlyNumericMetric);
+ allMetrics.addAll(onlyTraitMetric);
+ allMetrics.addAll(onlyCalltimeMetric);
+
+ MeasurementCollectorRunnable runnable = new
MeasurementCollectorRunnable(component, 0L, 123L, null,
+ this.threadPool.getExecutor());
+
+ MeasurementReport report = new MeasurementReport();
+ runnable.getLastValues(report, onlyNumericMetric); // we only ask for the numeric
metric, not all
+ // we haven't started the runnable yet, so nothing is collected yet
+ assert 0 == report.getCollectionTime();
+ assert report.getNumericData().isEmpty();
+ assert report.getTraitData().isEmpty();
+ assert report.getCallTimeData().isEmpty();
+
+ runnable.start();
+ Thread.sleep(1000L);
+
+ report = new MeasurementReport();
+ runnable.getLastValues(report, allMetrics); // even though we are asking for all,
last time we only collected the numeric metric
+ assert 1000 == report.getCollectionTime();
+ assert !report.getNumericData().isEmpty();
+ assert report.getTraitData().isEmpty() : "should not have collected the
trait data yet";
+ assert report.getCallTimeData().isEmpty() : "should not have collected the
calltime data yet";
+
+ // let's create another runnable so we can speed up the test (using a 0
initial delay)
+ runnable.stop();
+ component = new TestMultipleMeasumentFacet();
+ runnable = new MeasurementCollectorRunnable(component, 0L, 123L, null,
this.threadPool.getExecutor());
+
+ report = new MeasurementReport();
+ runnable.getLastValues(report, allMetrics); // prime the pump by asking for all
metrics
+ // we haven't started the runnable yet, so nothing is collected yet
+ assert 0 == report.getCollectionTime();
+ assert report.getNumericData().isEmpty();
+ assert report.getTraitData().isEmpty();
+ assert report.getCallTimeData().isEmpty();
+
+ runnable.start();
+ Thread.sleep(1000L);
+
+ report = new MeasurementReport();
+ runnable.getLastValues(report, allMetrics); // we are asking for all metrics, and
we should have collected them all
+ assert 1000 == report.getCollectionTime();
+ assert !report.getNumericData().isEmpty();
+ assert !report.getTraitData().isEmpty();
+ assert !report.getCallTimeData().isEmpty();
+
+ // now we only ask for individual metrics, not all of them at once
+ report = new MeasurementReport();
+ runnable.getLastValues(report, onlyNumericMetric);
+ assert 1000 == report.getCollectionTime();
+ assert !report.getNumericData().isEmpty();
+ assert report.getTraitData().isEmpty() : "we didn't ask for the trait
data";
+ assert report.getCallTimeData().isEmpty() : "we didn't ask for the
calltime data";
+
+ report = new MeasurementReport();
+ runnable.getLastValues(report, onlyTraitMetric);
+ assert 1000 == report.getCollectionTime();
+ assert report.getNumericData().isEmpty() : "we didn't ask for the
numeric data";
+ assert !report.getTraitData().isEmpty();
+ assert report.getCallTimeData().isEmpty() : "we didn't ask for the
calltime data";
+
+ report = new MeasurementReport();
+ runnable.getLastValues(report, onlyCalltimeMetric);
+ assert 1000 == report.getCollectionTime();
+ assert report.getNumericData().isEmpty() : "we didn't ask for the
numeric data";
+ assert report.getTraitData().isEmpty() : "we didn't ask for the trait
data";
+ assert !report.getCallTimeData().isEmpty();
+
+ runnable.stop();
}
protected class TestAvailabilityFacet implements AvailabilityFacet {
@@ -130,7 +223,7 @@ public class CollectorThreadPoolTest {
@Override
public void getValues(MeasurementReport report,
Set<MeasurementScheduleRequest> metrics) throws Exception {
- log("getValues " + metrics);
+ log("TestMeasumentFacet.getValues " + metrics);
report.setCollectionTime(42L);
for (MeasurementScheduleRequest request : metrics) {
report.addData(new MeasurementDataTrait(0, request, "good
times"));
@@ -138,4 +231,46 @@ public class CollectorThreadPoolTest {
}
}
+
+ protected class TestMultipleMeasumentFacet implements MeasurementFacet {
+ public static final String NUMERIC_METRIC_NAME = "numericMetric";
+ public static final String TRAIT_METRIC_NAME = "traitMetric";
+
+ public MeasurementScheduleRequest getNumericMetricSchedule() {
+ return new MeasurementScheduleRequest(1, NUMERIC_METRIC_NAME, 30000L, true,
DataType.MEASUREMENT);
+ }
+
+ public MeasurementScheduleRequest getTraitMetricSchedule() {
+ return new MeasurementScheduleRequest(2, TRAIT_METRIC_NAME, 30000L, true,
DataType.TRAIT);
+ }
+
+ public MeasurementScheduleRequest getCalltimeMetricSchedule() {
+ return new MeasurementScheduleRequest(3, "calltime", 30000L, true,
DataType.CALLTIME);
+ }
+
+ private long collectionTime = 1000;
+ private int counter = 0;
+
+ @Override
+ public void getValues(MeasurementReport report,
Set<MeasurementScheduleRequest> metrics) throws Exception {
+ counter++;
+ log("TestMultipleMeasumentFacet.getValues [" + counter + "]:
" + metrics);
+ report.setCollectionTime(collectionTime++);
+ for (MeasurementScheduleRequest request : metrics) {
+ if (request.getDataType() == DataType.CALLTIME) {
+ CallTimeData data = new CallTimeData(request);
+ data.addCallData("dest1", new Date(collectionTime), (long)
counter);
+ report.addData(data);
+ } else if (request.getName().equals(NUMERIC_METRIC_NAME)) {
+ report.addData(new MeasurementDataNumeric(collectionTime, request,
(double) counter));
+ } else if (request.getName().equals(TRAIT_METRIC_NAME)) {
+ report.addData(new MeasurementDataTrait(collectionTime, request,
"" + counter));
+ } else {
+ log.info("bad test - unknown metric: " + request);
+ throw new IllegalStateException("bad test - unknown metric:
" + request);
+ }
+ }
+ }
+
+ }
}