Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 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 | */ |
Jian Li | 6b86a76 | 2016-01-29 09:30:40 -0800 | [diff] [blame] | 16 | package org.onosproject.cpman.impl; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 17 | |
Jian Li | 9f3a885 | 2016-04-07 13:37:39 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableMap; |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 20 | import com.google.common.collect.Maps; |
| 21 | import com.google.common.collect.Sets; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Activate; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Component; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Deactivate; |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Service; |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 28 | import org.onlab.util.KryoNamespace; |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 29 | import org.onosproject.cluster.ClusterService; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 30 | import org.onosproject.cluster.NodeId; |
Jian Li | 6b86a76 | 2016-01-29 09:30:40 -0800 | [diff] [blame] | 31 | import org.onosproject.cpman.ControlLoad; |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame^] | 32 | import org.onosproject.cpman.ControlLoadSnapshot; |
Jian Li | 6b86a76 | 2016-01-29 09:30:40 -0800 | [diff] [blame] | 33 | import org.onosproject.cpman.ControlMetric; |
| 34 | import org.onosproject.cpman.ControlMetricType; |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame^] | 35 | import org.onosproject.cpman.ControlMetricsRequest; |
Jian Li | 6b86a76 | 2016-01-29 09:30:40 -0800 | [diff] [blame] | 36 | import org.onosproject.cpman.ControlPlaneMonitorService; |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 37 | import org.onosproject.cpman.MetricsDatabase; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 38 | import org.onosproject.net.DeviceId; |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 39 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 40 | import org.onosproject.store.cluster.messaging.MessageSubject; |
| 41 | import org.onosproject.store.serializers.KryoNamespaces; |
| 42 | import org.onosproject.store.service.Serializer; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 43 | import org.slf4j.Logger; |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 44 | import org.slf4j.LoggerFactory; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 45 | |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 46 | import java.util.Map; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 47 | import java.util.Optional; |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 48 | import java.util.Set; |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 49 | import java.util.concurrent.CompletableFuture; |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame^] | 50 | import java.util.concurrent.TimeUnit; |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 51 | import java.util.stream.Collectors; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 52 | |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 53 | import static com.google.common.base.Preconditions.checkArgument; |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 54 | import static org.onosproject.cpman.ControlResource.CONTROL_MESSAGE_METRICS; |
| 55 | import static org.onosproject.cpman.ControlResource.CPU_METRICS; |
| 56 | import static org.onosproject.cpman.ControlResource.DISK_METRICS; |
| 57 | import static org.onosproject.cpman.ControlResource.MEMORY_METRICS; |
| 58 | import static org.onosproject.cpman.ControlResource.NETWORK_METRICS; |
| 59 | import static org.onosproject.cpman.ControlResource.Type; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Control plane monitoring service class. |
| 63 | */ |
| 64 | @Component(immediate = true) |
| 65 | @Service |
| 66 | public class ControlPlaneMonitor implements ControlPlaneMonitorService { |
| 67 | |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 68 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 69 | private MetricsDatabase cpuMetrics; |
| 70 | private MetricsDatabase memoryMetrics; |
| 71 | private Map<DeviceId, MetricsDatabase> controlMessageMap; |
| 72 | private Map<String, MetricsDatabase> diskMetricsMap; |
| 73 | private Map<String, MetricsDatabase> networkMetricsMap; |
| 74 | |
| 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 76 | protected ClusterService clusterService; |
| 77 | |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 79 | protected ClusterCommunicationService communicationService; |
| 80 | |
Jian Li | daf55ea | 2016-04-04 20:38:30 -0700 | [diff] [blame] | 81 | private static final String DEFAULT_RESOURCE = "default"; |
| 82 | |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 83 | private static final Set RESOURCE_TYPE_SET = |
| 84 | ImmutableSet.of(Type.CONTROL_MESSAGE, Type.DISK, Type.NETWORK); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 85 | |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 86 | private static final MessageSubject CONTROL_STATS = |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame^] | 87 | new MessageSubject("control-plane-stats"); |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 88 | |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 89 | private Map<ControlMetricType, Double> cpuBuf; |
| 90 | private Map<ControlMetricType, Double> memoryBuf; |
| 91 | private Map<String, Map<ControlMetricType, Double>> diskBuf; |
| 92 | private Map<String, Map<ControlMetricType, Double>> networkBuf; |
| 93 | private Map<DeviceId, Map<ControlMetricType, Double>> ctrlMsgBuf; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 94 | |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 95 | private Map<Type, Set<String>> availableResourceMap; |
| 96 | private Set<DeviceId> availableDeviceIdSet; |
| 97 | |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 98 | private static final String METRIC_TYPE_NULL = "Control metric type cannot be null"; |
| 99 | |
| 100 | private static final Serializer SERIALIZER = Serializer |
| 101 | .using(new KryoNamespace.Builder() |
| 102 | .register(KryoNamespaces.API) |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame^] | 103 | .register(KryoNamespaces.BASIC) |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 104 | .register(ControlMetricsRequest.class) |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame^] | 105 | .register(ControlLoadSnapshot.class) |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 106 | .register(ControlMetricType.class) |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame^] | 107 | .register(TimeUnit.class) |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 108 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID).build()); |
| 109 | |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 110 | @Activate |
| 111 | public void activate() { |
Jian Li | daf55ea | 2016-04-04 20:38:30 -0700 | [diff] [blame] | 112 | cpuMetrics = genMDbBuilder(DEFAULT_RESOURCE, Type.CPU, CPU_METRICS); |
| 113 | memoryMetrics = genMDbBuilder(DEFAULT_RESOURCE, Type.MEMORY, MEMORY_METRICS); |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 114 | controlMessageMap = Maps.newConcurrentMap(); |
| 115 | diskMetricsMap = Maps.newConcurrentMap(); |
| 116 | networkMetricsMap = Maps.newConcurrentMap(); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 117 | |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 118 | cpuBuf = Maps.newConcurrentMap(); |
| 119 | memoryBuf = Maps.newConcurrentMap(); |
| 120 | diskBuf = Maps.newConcurrentMap(); |
| 121 | networkBuf = Maps.newConcurrentMap(); |
| 122 | ctrlMsgBuf = Maps.newConcurrentMap(); |
| 123 | |
| 124 | availableResourceMap = Maps.newConcurrentMap(); |
| 125 | availableDeviceIdSet = Sets.newConcurrentHashSet(); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 126 | |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame^] | 127 | communicationService.<ControlMetricsRequest, ControlLoadSnapshot>addSubscriber(CONTROL_STATS, |
| 128 | SERIALIZER::decode, this::handleRequest, SERIALIZER::encode); |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 129 | |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 130 | log.info("Started"); |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Deactivate |
| 134 | public void deactivate() { |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 135 | |
| 136 | // TODO: need to handle the mdb close. |
| 137 | cpuBuf.clear(); |
| 138 | memoryBuf.clear(); |
| 139 | diskBuf.clear(); |
| 140 | networkBuf.clear(); |
| 141 | ctrlMsgBuf.clear(); |
| 142 | |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 143 | communicationService.removeSubscriber(CONTROL_STATS); |
| 144 | |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 145 | log.info("Stopped"); |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 148 | @Override |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 149 | public void updateMetric(ControlMetric cm, int updateIntervalInMinutes, |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 150 | Optional<DeviceId> deviceId) { |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 151 | if (deviceId.isPresent()) { |
Jian Li | 4614890 | 2016-01-29 13:33:50 -0800 | [diff] [blame] | 152 | |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 153 | // insert a new device entry if we cannot find any |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 154 | ctrlMsgBuf.putIfAbsent(deviceId.get(), Maps.newConcurrentMap()); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 155 | |
| 156 | // update control message metrics |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 157 | if (CONTROL_MESSAGE_METRICS.contains(cm.metricType())) { |
| 158 | |
| 159 | if (!availableDeviceIdSet.contains(deviceId.get())) { |
| 160 | availableDeviceIdSet.add(deviceId.get()); |
| 161 | } |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 162 | |
| 163 | // we will accumulate the metric value into buffer first |
| 164 | ctrlMsgBuf.get(deviceId.get()).putIfAbsent(cm.metricType(), |
| 165 | (double) cm.metricValue().getLoad()); |
| 166 | |
| 167 | // if buffer contains all control message metrics, |
| 168 | // we simply set and update the values into MetricsDatabase. |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 169 | if (ctrlMsgBuf.get(deviceId.get()).keySet() |
| 170 | .containsAll(CONTROL_MESSAGE_METRICS)) { |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 171 | updateControlMessages(ctrlMsgBuf.get(deviceId.get()), deviceId.get()); |
Jian Li | daf55ea | 2016-04-04 20:38:30 -0700 | [diff] [blame] | 172 | ctrlMsgBuf.get(deviceId.get()); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | } else { |
| 176 | |
| 177 | // update cpu metrics |
| 178 | if (CPU_METRICS.contains(cm.metricType())) { |
| 179 | cpuBuf.putIfAbsent(cm.metricType(), |
| 180 | (double) cm.metricValue().getLoad()); |
| 181 | if (cpuBuf.keySet().containsAll(CPU_METRICS)) { |
| 182 | cpuMetrics.updateMetrics(convertMap(cpuBuf)); |
| 183 | cpuBuf.clear(); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | // update memory metrics |
| 188 | if (MEMORY_METRICS.contains(cm.metricType())) { |
| 189 | memoryBuf.putIfAbsent(cm.metricType(), |
| 190 | (double) cm.metricValue().getLoad()); |
| 191 | if (memoryBuf.keySet().containsAll(MEMORY_METRICS)) { |
| 192 | memoryMetrics.updateMetrics(convertMap(memoryBuf)); |
| 193 | memoryBuf.clear(); |
| 194 | } |
| 195 | } |
| 196 | } |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | @Override |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 200 | public void updateMetric(ControlMetric cm, int updateIntervalInMinutes, |
Jian Li | e044d1a | 2016-01-25 09:01:20 -0800 | [diff] [blame] | 201 | String resourceName) { |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 202 | // update disk metrics |
| 203 | if (DISK_METRICS.contains(cm.metricType())) { |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 204 | diskBuf.putIfAbsent(resourceName, Maps.newConcurrentMap()); |
| 205 | |
| 206 | availableResourceMap.putIfAbsent(Type.DISK, Sets.newHashSet()); |
| 207 | availableResourceMap.computeIfPresent(Type.DISK, (k, v) -> { |
| 208 | v.add(resourceName); |
| 209 | return v; |
| 210 | }); |
| 211 | |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 212 | diskBuf.get(resourceName).putIfAbsent(cm.metricType(), |
| 213 | (double) cm.metricValue().getLoad()); |
| 214 | if (diskBuf.get(resourceName).keySet().containsAll(DISK_METRICS)) { |
| 215 | updateDiskMetrics(diskBuf.get(resourceName), resourceName); |
| 216 | diskBuf.clear(); |
| 217 | } |
| 218 | } |
Jian Li | e044d1a | 2016-01-25 09:01:20 -0800 | [diff] [blame] | 219 | |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 220 | // update network metrics |
| 221 | if (NETWORK_METRICS.contains(cm.metricType())) { |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 222 | networkBuf.putIfAbsent(resourceName, Maps.newConcurrentMap()); |
| 223 | |
| 224 | availableResourceMap.putIfAbsent(Type.NETWORK, Sets.newHashSet()); |
| 225 | availableResourceMap.computeIfPresent(Type.NETWORK, (k, v) -> { |
| 226 | v.add(resourceName); |
| 227 | return v; |
| 228 | }); |
| 229 | |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 230 | networkBuf.get(resourceName).putIfAbsent(cm.metricType(), |
| 231 | (double) cm.metricValue().getLoad()); |
| 232 | if (networkBuf.get(resourceName).keySet().containsAll(NETWORK_METRICS)) { |
| 233 | updateNetworkMetrics(networkBuf.get(resourceName), resourceName); |
| 234 | networkBuf.clear(); |
| 235 | } |
| 236 | } |
Jian Li | e044d1a | 2016-01-25 09:01:20 -0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | @Override |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame^] | 240 | public CompletableFuture<ControlLoadSnapshot> getLoad(NodeId nodeId, |
| 241 | ControlMetricType type, |
| 242 | Optional<DeviceId> deviceId) { |
| 243 | if (clusterService.getLocalNode().id().equals(nodeId)) { |
| 244 | return CompletableFuture.completedFuture(snapshot(getLocalLoad(type, deviceId))); |
| 245 | } else { |
| 246 | return communicationService.sendAndReceive(createRequest(type, deviceId), |
| 247 | CONTROL_STATS, SERIALIZER::encode, SERIALIZER::decode, nodeId); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public CompletableFuture<ControlLoadSnapshot> getLoad(NodeId nodeId, |
| 253 | ControlMetricType type, |
| 254 | String resourceName) { |
| 255 | if (clusterService.getLocalNode().id().equals(nodeId)) { |
| 256 | return CompletableFuture.completedFuture(snapshot(getLocalLoad(type, resourceName))); |
| 257 | } else { |
| 258 | return communicationService.sendAndReceive(createRequest(type, resourceName), |
| 259 | CONTROL_STATS, SERIALIZER::encode, SERIALIZER::decode, nodeId); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | @Override |
| 264 | public CompletableFuture<ControlLoadSnapshot> getLoad(NodeId nodeId, |
| 265 | ControlMetricType type, |
| 266 | int duration, TimeUnit unit, |
| 267 | Optional<DeviceId> deviceId) { |
| 268 | if (clusterService.getLocalNode().id().equals(nodeId)) { |
| 269 | return CompletableFuture.completedFuture(snapshot(getLocalLoad(type, deviceId), duration, unit)); |
| 270 | } else { |
| 271 | return communicationService.sendAndReceive(createRequest(type, duration, unit, deviceId), |
| 272 | CONTROL_STATS, SERIALIZER::encode, SERIALIZER::decode, nodeId); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | @Override |
| 277 | public CompletableFuture<ControlLoadSnapshot> getLoad(NodeId nodeId, |
| 278 | ControlMetricType type, |
| 279 | int duration, TimeUnit unit, |
| 280 | String resourceName) { |
| 281 | if (clusterService.getLocalNode().id().equals(nodeId)) { |
| 282 | return CompletableFuture.completedFuture(snapshot(getLocalLoad(type, resourceName), duration, unit)); |
| 283 | } else { |
| 284 | return communicationService.sendAndReceive(createRequest(type, duration, unit, resourceName), |
| 285 | CONTROL_STATS, SERIALIZER::encode, SERIALIZER::decode, nodeId); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | @Override |
| 290 | public Set<String> availableResources(Type resourceType) { |
| 291 | if (RESOURCE_TYPE_SET.contains(resourceType)) { |
| 292 | if (Type.CONTROL_MESSAGE.equals(resourceType)) { |
| 293 | return availableDeviceIdSet.stream().map(id -> |
| 294 | id.toString()).collect(Collectors.toSet()); |
| 295 | } else { |
| 296 | Set<String> res = availableResourceMap.get(resourceType); |
| 297 | return res == null ? ImmutableSet.of() : res; |
| 298 | } |
| 299 | } |
| 300 | return ImmutableSet.of(); |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Builds and returns metric database instance with given resource name, |
| 305 | * resource type and metric type. |
| 306 | * |
| 307 | * @param resourceName resource name |
| 308 | * @param resourceType resource type |
| 309 | * @param metricTypes metric type |
| 310 | * @return metric database instance |
| 311 | */ |
| 312 | private MetricsDatabase genMDbBuilder(String resourceName, |
| 313 | Type resourceType, |
| 314 | Set<ControlMetricType> metricTypes) { |
| 315 | MetricsDatabase.Builder builder = new DefaultMetricsDatabase.Builder(); |
| 316 | builder.withMetricName(resourceType.toString()); |
| 317 | builder.withResourceName(resourceName); |
| 318 | metricTypes.forEach(type -> builder.addMetricType(type.toString())); |
| 319 | return builder.build(); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Updates network metrics with given metric map and resource name. |
| 324 | * |
| 325 | * @param metricMap a metric map which is comprised of metric type and value |
| 326 | * @param resourceName resource name |
| 327 | */ |
| 328 | private void updateNetworkMetrics(Map<ControlMetricType, Double> metricMap, |
| 329 | String resourceName) { |
| 330 | networkMetricsMap.putIfAbsent(resourceName, genMDbBuilder(resourceName, |
| 331 | Type.NETWORK, NETWORK_METRICS)); |
| 332 | networkMetricsMap.get(resourceName).updateMetrics(convertMap(metricMap)); |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Updates disk metrics with given metric map and resource name. |
| 337 | * |
| 338 | * @param metricMap a metric map which is comprised of metric type and value |
| 339 | * @param resourceName resource name |
| 340 | */ |
| 341 | private void updateDiskMetrics(Map<ControlMetricType, Double> metricMap, |
| 342 | String resourceName) { |
| 343 | diskMetricsMap.putIfAbsent(resourceName, genMDbBuilder(resourceName, |
| 344 | Type.DISK, DISK_METRICS)); |
| 345 | diskMetricsMap.get(resourceName).updateMetrics(convertMap(metricMap)); |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Updates control message metrics with given metric map and device identifier. |
| 350 | * |
| 351 | * @param metricMap a metric map which is comprised of metric type and value |
| 352 | * @param deviceId device identifier |
| 353 | */ |
| 354 | private void updateControlMessages(Map<ControlMetricType, Double> metricMap, |
| 355 | DeviceId deviceId) { |
| 356 | controlMessageMap.putIfAbsent(deviceId, genMDbBuilder(deviceId.toString(), |
| 357 | Type.CONTROL_MESSAGE, CONTROL_MESSAGE_METRICS)); |
| 358 | controlMessageMap.get(deviceId).updateMetrics(convertMap(metricMap)); |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Converts metric map into a new map which contains string formatted metric type as key. |
| 363 | * |
| 364 | * @param metricMap metric map in which ControlMetricType is key |
| 365 | * @return a new map in which string formatted metric type is key |
| 366 | */ |
| 367 | private Map<String, Double> convertMap(Map<ControlMetricType, Double> metricMap) { |
| 368 | if (metricMap == null) { |
| 369 | return ImmutableMap.of(); |
| 370 | } |
| 371 | Map newMap = Maps.newConcurrentMap(); |
| 372 | metricMap.forEach((k, v) -> newMap.putIfAbsent(k.toString(), v)); |
| 373 | return newMap; |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Handles control metric request from remote node. |
| 378 | * |
| 379 | * @param request control metric request |
| 380 | * @return completable future object of control load snapshot |
| 381 | */ |
| 382 | private CompletableFuture<ControlLoadSnapshot> handleRequest(ControlMetricsRequest request) { |
| 383 | |
| 384 | checkArgument(request.getType() != null, METRIC_TYPE_NULL); |
| 385 | |
| 386 | ControlLoad load; |
| 387 | if (request.getResourceName() != null && request.getUnit() != null) { |
| 388 | load = getLocalLoad(request.getType(), request.getResourceName()); |
| 389 | } else { |
| 390 | load = getLocalLoad(request.getType(), request.getDeviceId()); |
| 391 | } |
| 392 | |
| 393 | long average; |
| 394 | if (request.getUnit() != null) { |
| 395 | average = load.average(request.getDuration(), request.getUnit()); |
| 396 | } else { |
| 397 | average = load.average(); |
| 398 | } |
| 399 | ControlLoadSnapshot resp = |
| 400 | new ControlLoadSnapshot(load.latest(), average, load.time()); |
| 401 | return CompletableFuture.completedFuture(resp); |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Generates a control metric request. |
| 406 | * |
| 407 | * @param type control metric type |
| 408 | * @param deviceId device identifier |
| 409 | * @return control metric request instance |
| 410 | */ |
| 411 | private ControlMetricsRequest createRequest(ControlMetricType type, |
| 412 | Optional<DeviceId> deviceId) { |
| 413 | return new ControlMetricsRequest(type, deviceId); |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Generates a control metric request with given projected time range. |
| 418 | * |
| 419 | * @param type control metric type |
| 420 | * @param duration projected time duration |
| 421 | * @param unit projected time unit |
| 422 | * @param deviceId device identifier |
| 423 | * @return control metric request instance |
| 424 | */ |
| 425 | private ControlMetricsRequest createRequest(ControlMetricType type, |
| 426 | int duration, TimeUnit unit, |
| 427 | Optional<DeviceId> deviceId) { |
| 428 | return new ControlMetricsRequest(type, duration, unit, deviceId); |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Generates a control metric request. |
| 433 | * |
| 434 | * @param type control metric type |
| 435 | * @param resourceName resource name |
| 436 | * @return control metric request instance |
| 437 | */ |
| 438 | private ControlMetricsRequest createRequest(ControlMetricType type, |
| 439 | String resourceName) { |
| 440 | return new ControlMetricsRequest(type, resourceName); |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Generates a control metric request with given projected time range. |
| 445 | * |
| 446 | * @param type control metric type |
| 447 | * @param duration projected time duration |
| 448 | * @param unit projected time unit |
| 449 | * @param resourceName resource name |
| 450 | * @return control metric request instance |
| 451 | */ |
| 452 | private ControlMetricsRequest createRequest(ControlMetricType type, |
| 453 | int duration, TimeUnit unit, |
| 454 | String resourceName) { |
| 455 | return new ControlMetricsRequest(type, duration, unit, resourceName); |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Returns a snapshot of control load. |
| 460 | * |
| 461 | * @param cl control load |
| 462 | * @return a snapshot of control load |
| 463 | */ |
| 464 | private ControlLoadSnapshot snapshot(ControlLoad cl) { |
| 465 | if (cl != null) { |
| 466 | return new ControlLoadSnapshot(cl.latest(), cl.average(), cl.time()); |
| 467 | } |
| 468 | return null; |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Returns a snapshot of control load with given projected time range. |
| 473 | * |
| 474 | * @param cl control load |
| 475 | * @param duration projected time duration |
| 476 | * @param unit projected time unit |
| 477 | * @return a snapshot of control load |
| 478 | */ |
| 479 | private ControlLoadSnapshot snapshot(ControlLoad cl, int duration, TimeUnit unit) { |
| 480 | if (cl != null) { |
| 481 | return new ControlLoadSnapshot(cl.latest(), cl.average(duration, unit), cl.time()); |
| 482 | } |
| 483 | return null; |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * Returns local control load. |
| 488 | * |
| 489 | * @param type metric type |
| 490 | * @param deviceId device identifier |
| 491 | * @return control load |
| 492 | */ |
| 493 | private ControlLoad getLocalLoad(ControlMetricType type, |
| 494 | Optional<DeviceId> deviceId) { |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 495 | if (deviceId.isPresent()) { |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame^] | 496 | // returns control message stats |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 497 | if (CONTROL_MESSAGE_METRICS.contains(type) && |
| 498 | availableDeviceIdSet.contains(deviceId.get())) { |
| 499 | return new DefaultControlLoad(controlMessageMap.get(deviceId.get()), type); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 500 | } |
| 501 | } else { |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 502 | // returns controlLoad of CPU metrics |
| 503 | if (CPU_METRICS.contains(type)) { |
| 504 | return new DefaultControlLoad(cpuMetrics, type); |
| 505 | } |
| 506 | |
| 507 | // returns memoryLoad of memory metrics |
| 508 | if (MEMORY_METRICS.contains(type)) { |
| 509 | return new DefaultControlLoad(memoryMetrics, type); |
| 510 | } |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 511 | } |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 512 | return null; |
| 513 | } |
| 514 | |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame^] | 515 | /** |
| 516 | * Returns local control load. |
| 517 | * |
| 518 | * @param type metric type |
| 519 | * @param resourceName resource name |
| 520 | * @return control load |
| 521 | */ |
| 522 | private ControlLoad getLocalLoad(ControlMetricType type, String resourceName) { |
| 523 | // returns disk I/O stats |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 524 | if (DISK_METRICS.contains(type) && |
| 525 | availableResources(Type.DISK).contains(resourceName)) { |
| 526 | return new DefaultControlLoad(diskMetricsMap.get(resourceName), type); |
| 527 | } |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 528 | |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame^] | 529 | // returns network I/O stats |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 530 | if (NETWORK_METRICS.contains(type) && |
| 531 | availableResources(Type.NETWORK).contains(resourceName)) { |
| 532 | return new DefaultControlLoad(networkMetricsMap.get(resourceName), type); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 533 | } |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 534 | return null; |
| 535 | } |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 536 | } |