use of com.linkedin.cruisecontrol.metricdef.MetricInfo in project cruise-control by linkedin.
the class RawMetricValues method aggregate.
/**
* Get the aggregated values of the given sorted set of windows. The result {@link ValuesAndExtrapolations} contains
* the windows in the same order.
*
* @param windowIndexes the sorted set of windows to get values for.
* @param metricDef the metric definitions.
* @return the aggregated values and extrapolations of the given sorted set of windows in that order.
*/
public synchronized ValuesAndExtrapolations aggregate(SortedSet<Long> windowIndexes, MetricDef metricDef) {
if (_valuesByMetricId.isEmpty()) {
return ValuesAndExtrapolations.empty(windowIndexes.size(), metricDef);
}
Map<Integer, MetricValues> aggValues = new HashMap<>();
SortedMap<Integer, Extrapolation> extrapolations = new TreeMap<>();
for (Map.Entry<Integer, float[]> entry : _valuesByMetricId.entrySet()) {
int metricId = entry.getKey();
float[] values = entry.getValue();
MetricInfo info = metricDef.metricInfo(metricId);
MetricValues aggValuesForMetric = new MetricValues(windowIndexes.size());
aggValues.put(metricId, aggValuesForMetric);
int resultIndex = 0;
for (long windowIndex : windowIndexes) {
validateIndex(windowIndex);
int idx = handleWrapping(windowIndex);
// Sufficient samples
if (_counts[idx] >= _minSamplesPerWindow) {
aggValuesForMetric.set(resultIndex, getValue(info, idx, values));
// Not quite sufficient, but have some available.
} else if (_counts[idx] >= halfMinRequiredSamples()) {
extrapolations.putIfAbsent(resultIndex, Extrapolation.AVG_AVAILABLE);
aggValuesForMetric.set(resultIndex, getValue(info, idx, values));
// Not sufficient, check the neighbors. The neighbors only exist when the index is not on the edge, i.e
// neither the first nor last index.
} else if (idx != firstIdx() && idx != lastIdx() && _counts[prevIdx(idx)] >= _minSamplesPerWindow && _counts[nextIdx(idx)] >= _minSamplesPerWindow) {
extrapolations.putIfAbsent(resultIndex, Extrapolation.AVG_ADJACENT);
int prevIdx = prevIdx(idx);
int nextIdx = nextIdx(idx);
double total = _valuesByMetricId.get(metricId)[prevIdx] + (_counts[idx] == 0 ? 0 : _valuesByMetricId.get(metricId)[idx]) + _valuesByMetricId.get(metricId)[nextIdx];
switch(info.strategy()) {
case AVG:
double counts = _counts[prevIdx] + _counts[idx] + _counts[nextIdx];
aggValuesForMetric.set(resultIndex, total / counts);
break;
// fall through.
case MAX:
case LATEST:
// for max and latest, we already only keep the largest or last value.
aggValuesForMetric.set(resultIndex, _counts[idx] > 0 ? total / 3 : total / 2);
break;
default:
throw new IllegalStateException("Should never be here.");
}
// Neighbor not available, use the insufficient samples.
} else if (_counts[idx] > 0) {
aggValuesForMetric.set(resultIndex, getValue(info, idx, values));
extrapolations.putIfAbsent(resultIndex, Extrapolation.FORCED_INSUFFICIENT);
// Nothing is available, just return all 0 and NO_VALID_EXTRAPOLATION.
} else {
aggValuesForMetric.set(resultIndex, 0);
extrapolations.putIfAbsent(resultIndex, Extrapolation.NO_VALID_EXTRAPOLATION);
}
resultIndex++;
}
}
return new ValuesAndExtrapolations(new AggregatedMetricValues(aggValues), extrapolations);
}
use of com.linkedin.cruisecontrol.metricdef.MetricInfo in project cruise-control by linkedin.
the class CruiseControlUnitTestUtils method populateSampleAggregator.
public static <G, E extends Entity<G>> void populateSampleAggregator(int numWindows, int numSamplesPerWindow, MetricSampleAggregator<G, E> metricSampleAggregator, E entity, int startingWindow, long windowMs, MetricDef metricDef) {
for (int i = startingWindow; i < numWindows + startingWindow; i++) {
for (int j = 0; j < numSamplesPerWindow; j++) {
MetricSample<G, E> sample = new MetricSample<>(entity);
for (MetricInfo metricInfo : metricDef.all()) {
sample.record(metricInfo, i * 10 + j);
}
sample.close(i * windowMs + 1);
metricSampleAggregator.addSample(sample);
}
}
}
use of com.linkedin.cruisecontrol.metricdef.MetricInfo in project cruise-control by linkedin.
the class MetricSampleAggregatorTest method testAddSampleInDifferentWindows.
@Test
public void testAddSampleInDifferentWindows() throws NotEnoughValidWindowsException {
MetricSampleAggregator<String, IntegerEntity> aggregator = new MetricSampleAggregator<>(NUM_WINDOWS, WINDOW_MS, MIN_SAMPLES_PER_WINDOW, 0, 5, _metricDef);
// The remaining windows should NUM_WINDOWS - 2 to 2 * NUM_WINDOWS - 3;
populateSampleAggregator(2 * NUM_WINDOWS - 1, MIN_SAMPLES_PER_WINDOW, aggregator);
AggregationOptions<String, IntegerEntity> options = new AggregationOptions<>(1, 1, NUM_WINDOWS, Collections.emptySet(), AggregationOptions.Granularity.ENTITY_GROUP, true);
MetricSampleAggregationResult<String, IntegerEntity> aggResults = aggregator.aggregate(-1, Long.MAX_VALUE, options);
assertNotNull(aggResults);
assertEquals(1, aggResults.valuesAndExtrapolations().size());
for (Map.Entry<IntegerEntity, ValuesAndExtrapolations> entry : aggResults.valuesAndExtrapolations().entrySet()) {
ValuesAndExtrapolations valuesAndExtrapolations = entry.getValue();
List<Long> windows = valuesAndExtrapolations.windows();
assertEquals(NUM_WINDOWS, windows.size());
for (int i = 0; i < NUM_WINDOWS; i++) {
assertEquals((2 * NUM_WINDOWS - 2 - i) * WINDOW_MS, windows.get(i).longValue());
}
for (MetricInfo info : _metricDef.all()) {
MetricValues valuesForMetric = valuesAndExtrapolations.metricValues().valuesFor(info.id());
for (int i = 0; i < NUM_WINDOWS; i++) {
double expectedValue;
if (info.strategy() == AggregationFunction.LATEST || info.strategy() == AggregationFunction.MAX) {
expectedValue = (2 * NUM_WINDOWS - 3 - i) * 10 + MIN_SAMPLES_PER_WINDOW - 1;
} else {
expectedValue = (2 * NUM_WINDOWS - 3 - i) * 10 + (MIN_SAMPLES_PER_WINDOW - 1) / 2.0;
}
assertEquals("The utilization for " + info.name() + " should be " + expectedValue, expectedValue, valuesForMetric.get(i % NUM_WINDOWS), 0);
}
}
}
assertEquals(NUM_WINDOWS + 1, aggregator.allWindows().size());
assertEquals(NUM_WINDOWS, aggregator.numAvailableWindows());
}
use of com.linkedin.cruisecontrol.metricdef.MetricInfo in project cruise-control by linkedin.
the class RawMetricValuesTest method populate.
private float[][] populate(RawMetricValues rawValues, long startingWindowIndex) {
float[][] expected = new float[_metricDef.size()][NUM_WINDOWS_TO_KEEP];
for (long windowIdx = startingWindowIndex; windowIdx < startingWindowIndex + NUM_WINDOWS_TO_KEEP; windowIdx++) {
for (int i = 0; i < MIN_SAMPLES_PER_WINDOW; i++) {
float v = 10 * windowIdx + i;
MetricSample<String, IntegerEntity> m = getMetricSample(v, v, v);
rawValues.addSample(m, windowIdx, _metricDef);
for (MetricInfo info : _metricDef.all()) {
switch(info.strategy()) {
case AVG:
expected[info.id()][(int) (windowIdx % NUM_WINDOWS_TO_KEEP)] += v / MIN_SAMPLES_PER_WINDOW;
break;
case MAX:
case LATEST:
expected[info.id()][(int) (windowIdx % NUM_WINDOWS_TO_KEEP)] = v;
break;
default:
throw new IllegalStateException("Should never be here");
}
}
}
}
return expected;
}
use of com.linkedin.cruisecontrol.metricdef.MetricInfo in project cruise-control by linkedin.
the class Load method getJsonStructure.
/**
* Return an object that can be further used
* to encode into JSON
*/
public Map<String, Object> getJsonStructure() {
MetricDef metricDef = KafkaCruiseControlMetricDef.metricDef();
Map<String, Object> loadMap = new HashMap<>();
List<Object> metricValueList = new ArrayList<>();
for (MetricInfo metricInfo : metricDef.all()) {
MetricValues metricValues = _metricValues.valuesFor(metricInfo.id());
if (metricValues != null) {
Map<Long, Double> metricValuesMap = new HashMap<>();
for (int i = 0; i < _windows.size(); i++) {
metricValuesMap.put(_windows.get(i), metricValues.get(i));
}
metricValueList.add(metricValuesMap);
}
}
loadMap.put("MetricValues", metricValueList);
return loadMap;
}
Aggregations