Jian Li
Committed by Gerrit Code Review

[ONOS-3536] Implement back-end metrics saving logic using RRD

Change-Id: I1b3c495380884571dc88d2f9fb3152fdf41ef655
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.cpman;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* Database for storing a metric.
*/
public interface MetricsDatabase {
/**
* Returns the metric name of this database.
*
* @return metric name
*/
String metricName();
/**
* Update metric value by specifying metric type.
*
* @param metricType metric type (e.g., load, usage, etc.)
* @param value metric value
*/
void updateMetric(String metricType, double value);
/**
* Update metric value by specifying metric type in a certain time.
*
* @param metricType metric type (e.g., load, usage, etc.)
* @param value metric value
* @param time update time in seconds
*/
void updateMetric(String metricType, double value, long time);
/**
* Update metric values of a collection of metric types.
*
* @param metrics a collection of metrics which consists of a pair of
* metric type and metric value
* @param time update time in seconds
*/
void updateMetrics(Map<String, Double> metrics, long time);
/**
* Update metric values of a collection of metric types.
*
* @param metrics a collection of metrics which consists of a pair of
* metric type and metric value
*/
void updateMetrics(Map<String, Double> metrics);
/**
* Returns most recent metric value of a given metric type.
*
* @param metricType metric type
* @return metric value
*/
double recentMetric(String metricType);
/**
* Return most recent metric values of a given metric type for a given period.
*
* @param metricType metric type
* @param duration duration
* @param unit time unit
* @return a collection of metric value
*/
double[] recentMetrics(String metricType, int duration, TimeUnit unit);
/**
* Returns minimum metric value of a given metric type.
*
* @param metricType metric type
* @return metric value
*/
double minMetric(String metricType);
/**
* Returns maximum metric value of a given metric type.
*
* @param metricType metric type
* @return metric value
*/
double maxMetric(String metricType);
/**
* Returns a collection of metric values of a given metric type for a day.
*
* @param metricType metric type
* @return a collection of metric value
*/
double[] metrics(String metricType);
/**
* Returns a collection of metric values of a given metric type for
* a given period.
*
* @param metricType metric type
* @param startTime start time
* @param endTime end time
* @return a collection of metric value
*/
double[] metrics(String metricType, long startTime, long endTime);
/**
* A builder of MetricsDatabase.
*/
interface Builder {
/**
* Sets the metric name.
*
* @param metricName metric name
* @return builder object
*/
Builder withMetricName(String metricName);
/**
* Add a new metric to be monitored.
*
* @param metricType control metric type
*/
Builder addMetricType(String metricType);
/**
* Builds a MetricDatabase instance.
*
* @return MetricDatabase instance
*/
MetricsDatabase build();
}
}
......@@ -105,6 +105,11 @@
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.rrd4j</groupId>
<artifactId>rrd4j</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
</dependency>
......
......@@ -43,7 +43,6 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService {
@Activate
public void activate() {
}
@Deactivate
......@@ -53,6 +52,7 @@ public class ControlPlaneMonitor implements ControlPlaneMonitorService {
@Override
public void updateMetric(ControlMetric cpm, Integer updateInterval,
Optional<DeviceId> deviceId) {
}
@Override
......
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.cpman.impl;
import org.junit.Before;
import org.junit.Test;
import org.onosproject.cpman.MetricsDatabase;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
/**
* Unit test for control plane metrics back-end database.
*/
public class MetricsDatabaseTest {
private MetricsDatabase mdb;
private static final String CPU_METRIC = "cpu";
private static final String CPU_LOAD = "load";
private static final String MEMORY_METRIC = "memory";
private static final String MEMORY_FREE_PERC = "freePerc";
private static final String MEMORY_USED_PERC = "usedPerc";
/**
* Initializes the MetricsDatabase instance.
*/
@Before
public void setUp() {
mdb = new DefaultMetricsDatabase.Builder()
.withMetricName(CPU_METRIC)
.addMetricType(CPU_LOAD)
.build();
}
/**
* Tests the metric update function.
*/
@Test
public void testMetricUpdate() {
long currentTime = System.currentTimeMillis() / 1000L;
mdb.updateMetric(CPU_LOAD, 30, currentTime);
assertThat(30D, is(mdb.recentMetric(CPU_LOAD)));
mdb.updateMetric(CPU_LOAD, 40, currentTime + 60);
assertThat(40D, is(mdb.recentMetric(CPU_LOAD)));
mdb.updateMetric(CPU_LOAD, 50, currentTime + 120);
assertThat(50D, is(mdb.recentMetric(CPU_LOAD)));
}
/**
* Tests the metric range fetch function.
*/
@Test
public void testMetricRangeFetch() {
// full range fetch
assertThat(mdb.metrics(CPU_LOAD).length, is(60 * 24));
// query one minute time range
assertThat(mdb.recentMetrics(CPU_LOAD, 1, TimeUnit.MINUTES).length, is(1));
// query one hour time range
assertThat(mdb.recentMetrics(CPU_LOAD, 1, TimeUnit.HOURS).length, is(60));
// query one day time range
assertThat(mdb.recentMetrics(CPU_LOAD, 1, TimeUnit.DAYS).length, is(60 * 24));
// query a specific time range
long endTime = System.currentTimeMillis() / 1000L;
long startTime = endTime - 60 * 5;
assertThat(mdb.metrics(CPU_LOAD, startTime, endTime).length, is(5));
}
/**
* Test the projected time range.
*/
@Test(expected = IllegalArgumentException.class)
public void testExceededTimeRange() {
// query 25 hours time range
assertThat(mdb.recentMetrics(CPU_LOAD, 25, TimeUnit.HOURS).length, is(60 * 24));
}
/**
* Test the projected time range.
*/
@Test(expected = IllegalArgumentException.class)
public void testInsufficientTimeRange() {
// query 50 seconds time range
assertThat(mdb.recentMetrics(CPU_LOAD, 50, TimeUnit.SECONDS).length, is(1));
}
/**
* Test multiple metrics update and query.
*/
@Test
public void testMultipleMetrics() {
MetricsDatabase multiMdb = new DefaultMetricsDatabase.Builder()
.withMetricName(MEMORY_METRIC)
.addMetricType(MEMORY_FREE_PERC)
.addMetricType(MEMORY_USED_PERC)
.build();
Map<String, Double> metrics = new HashMap<>();
metrics.putIfAbsent(MEMORY_FREE_PERC, 30D);
metrics.putIfAbsent(MEMORY_USED_PERC, 70D);
multiMdb.updateMetrics(metrics);
assertThat(30D, is(multiMdb.recentMetric(MEMORY_FREE_PERC)));
assertThat(70D, is(multiMdb.recentMetric(MEMORY_USED_PERC)));
}
}