Committed by
Gerrit Code Review
Implement DefaultControlLoad, add lastUpdate for MetricsDatabase
Change-Id: Ice2ba793927117245c3fd12f32da239b60240c90
Showing
4 changed files
with
125 additions
and
0 deletions
| ... | @@ -39,4 +39,20 @@ public interface ControlLoad extends Load { | ... | @@ -39,4 +39,20 @@ public interface ControlLoad extends Load { |
| 39 | * @return average control plane metric value | 39 | * @return average control plane metric value |
| 40 | */ | 40 | */ |
| 41 | long average(); | 41 | long average(); |
| 42 | + | ||
| 43 | + /** | ||
| 44 | + * Obtains the most recent metric values of the specified time duration. | ||
| 45 | + * | ||
| 46 | + * @param duration time duration | ||
| 47 | + * @param unit time unit | ||
| 48 | + * @return a collection of the most recent metric values | ||
| 49 | + */ | ||
| 50 | + long[] recent(int duration, TimeUnit unit); | ||
| 51 | + | ||
| 52 | + /** | ||
| 53 | + * Obtains all metrics. | ||
| 54 | + * | ||
| 55 | + * @return a collection of the all metric values | ||
| 56 | + */ | ||
| 57 | + long[] all(); | ||
| 42 | } | 58 | } | ... | ... |
| ... | @@ -117,6 +117,14 @@ public interface MetricsDatabase { | ... | @@ -117,6 +117,14 @@ public interface MetricsDatabase { |
| 117 | double[] metrics(String metricType, long startTime, long endTime); | 117 | double[] metrics(String metricType, long startTime, long endTime); |
| 118 | 118 | ||
| 119 | /** | 119 | /** |
| 120 | + * Returns the latest metric update time. | ||
| 121 | + * | ||
| 122 | + * @param metricType metric type | ||
| 123 | + * @return timestamp | ||
| 124 | + */ | ||
| 125 | + long lastUpdate(String metricType); | ||
| 126 | + | ||
| 127 | + /** | ||
| 120 | * A builder of MetricsDatabase. | 128 | * A builder of MetricsDatabase. |
| 121 | */ | 129 | */ |
| 122 | interface Builder { | 130 | interface Builder { | ... | ... |
| 1 | +/* | ||
| 2 | + * Copyright 2016 Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.cpman.impl; | ||
| 17 | + | ||
| 18 | +import org.onosproject.cpman.ControlLoad; | ||
| 19 | +import org.onosproject.cpman.ControlMetricType; | ||
| 20 | +import org.onosproject.cpman.MetricsDatabase; | ||
| 21 | + | ||
| 22 | +import java.util.Arrays; | ||
| 23 | +import java.util.concurrent.TimeUnit; | ||
| 24 | +import java.util.stream.IntStream; | ||
| 25 | + | ||
| 26 | +/** | ||
| 27 | + * An implementation of control plane load. | ||
| 28 | + */ | ||
| 29 | +public class DefaultControlLoad implements ControlLoad { | ||
| 30 | + | ||
| 31 | + private final MetricsDatabase mdb; | ||
| 32 | + private final ControlMetricType type; | ||
| 33 | + | ||
| 34 | + public DefaultControlLoad(MetricsDatabase mdb, ControlMetricType type) { | ||
| 35 | + this.mdb = mdb; | ||
| 36 | + this.type = type; | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + @Override | ||
| 40 | + public long average(int duration, TimeUnit unit) { | ||
| 41 | + return (long) Arrays.stream(recent(duration, unit)).average().getAsDouble(); | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + @Override | ||
| 45 | + public long average() { | ||
| 46 | + return (long) Arrays.stream(all()).average().getAsDouble(); | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + @Override | ||
| 50 | + public long rate() { | ||
| 51 | + return 0; | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + @Override | ||
| 55 | + public long latest() { | ||
| 56 | + return (long) mdb.recentMetric(type.toString()); | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + @Override | ||
| 60 | + public boolean isValid() { | ||
| 61 | + return true; | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + @Override | ||
| 65 | + public long time() { | ||
| 66 | + return mdb.lastUpdate(type.toString()); | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + @Override | ||
| 70 | + public long[] recent(int duration, TimeUnit unit) { | ||
| 71 | + return doubleToLong(mdb.recentMetrics(type.toString(), duration, unit)); | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + @Override | ||
| 75 | + public long[] all() { | ||
| 76 | + return doubleToLong(mdb.metrics(type.toString())); | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + private double nanToZero(double d) { | ||
| 80 | + return Double.isNaN(d) ? 0D : d; | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + private long[] doubleToLong(double[] array) { | ||
| 84 | + final long[] longArray = new long[array.length]; | ||
| 85 | + IntStream.range(0, array.length).forEach(i -> | ||
| 86 | + longArray[i] = (long) nanToZero(array[i])); | ||
| 87 | + | ||
| 88 | + return longArray; | ||
| 89 | + } | ||
| 90 | +} |
| ... | @@ -183,6 +183,17 @@ public final class DefaultMetricsDatabase implements MetricsDatabase { | ... | @@ -183,6 +183,17 @@ public final class DefaultMetricsDatabase implements MetricsDatabase { |
| 183 | return new double[0]; | 183 | return new double[0]; |
| 184 | } | 184 | } |
| 185 | 185 | ||
| 186 | + @Override | ||
| 187 | + public long lastUpdate(String metricType) { | ||
| 188 | + try { | ||
| 189 | + checkArgument(rrdDb.containsDs(metricType), NON_EXIST_METRIC); | ||
| 190 | + rrdDb.getLastUpdateTime(); | ||
| 191 | + } catch (IOException e) { | ||
| 192 | + e.printStackTrace(); | ||
| 193 | + } | ||
| 194 | + return 0L; | ||
| 195 | + } | ||
| 196 | + | ||
| 186 | // try to check whether projected time range is within a day | 197 | // try to check whether projected time range is within a day |
| 187 | private boolean checkTimeRange(long startTime, long endTime) { | 198 | private boolean checkTimeRange(long startTime, long endTime) { |
| 188 | // check whether the given startTime and endTime larger than 1 minute | 199 | // check whether the given startTime and endTime larger than 1 minute | ... | ... |
-
Please register or login to post a comment