Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 1 | /* |
Jian Li | e044d1a | 2016-01-25 09:01:20 -0800 | [diff] [blame] | 2 | * Copyright 2015-2016 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 | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Maps; |
| 20 | import com.google.common.collect.Sets; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Component; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Deactivate; |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Service; |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 27 | import org.onlab.util.KryoNamespace; |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 28 | import org.onosproject.cluster.ClusterService; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 29 | import org.onosproject.cluster.NodeId; |
Jian Li | 6b86a76 | 2016-01-29 09:30:40 -0800 | [diff] [blame] | 30 | import org.onosproject.cpman.ControlLoad; |
| 31 | import org.onosproject.cpman.ControlMetric; |
| 32 | import org.onosproject.cpman.ControlMetricType; |
| 33 | import org.onosproject.cpman.ControlPlaneMonitorService; |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 34 | import org.onosproject.cpman.MetricsDatabase; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 35 | import org.onosproject.net.DeviceId; |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 36 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 37 | import org.onosproject.store.cluster.messaging.MessageSubject; |
| 38 | import org.onosproject.store.serializers.KryoNamespaces; |
| 39 | import org.onosproject.store.service.Serializer; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 40 | import org.slf4j.Logger; |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 41 | import org.slf4j.LoggerFactory; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 42 | |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 43 | import java.util.Map; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 44 | import java.util.Optional; |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 45 | import java.util.Set; |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 46 | import java.util.concurrent.CompletableFuture; |
| 47 | import java.util.concurrent.ExecutorService; |
| 48 | import java.util.concurrent.Executors; |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 49 | import java.util.stream.Collectors; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 50 | |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 51 | import static com.google.common.base.Preconditions.checkArgument; |
| 52 | import static org.onlab.util.Tools.groupedThreads; |
| 53 | import static org.onosproject.cpman.ControlResource.CONTROL_MESSAGE_METRICS; |
| 54 | import static org.onosproject.cpman.ControlResource.CPU_METRICS; |
| 55 | import static org.onosproject.cpman.ControlResource.DISK_METRICS; |
| 56 | import static org.onosproject.cpman.ControlResource.MEMORY_METRICS; |
| 57 | import static org.onosproject.cpman.ControlResource.NETWORK_METRICS; |
| 58 | import static org.onosproject.cpman.ControlResource.Type; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Control plane monitoring service class. |
| 62 | */ |
| 63 | @Component(immediate = true) |
| 64 | @Service |
| 65 | public class ControlPlaneMonitor implements ControlPlaneMonitorService { |
| 66 | |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 67 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 68 | private MetricsDatabase cpuMetrics; |
| 69 | private MetricsDatabase memoryMetrics; |
| 70 | private Map<DeviceId, MetricsDatabase> controlMessageMap; |
| 71 | private Map<String, MetricsDatabase> diskMetricsMap; |
| 72 | private Map<String, MetricsDatabase> networkMetricsMap; |
| 73 | |
| 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 75 | protected ClusterService clusterService; |
| 76 | |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 78 | protected ClusterCommunicationService communicationService; |
| 79 | |
Jian Li | daf55ea | 2016-04-04 20:38:30 -0700 | [diff] [blame^] | 80 | private static final String DEFAULT_RESOURCE = "default"; |
| 81 | |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 82 | private static final Set RESOURCE_TYPE_SET = |
| 83 | ImmutableSet.of(Type.CONTROL_MESSAGE, Type.DISK, Type.NETWORK); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 84 | |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 85 | private static final MessageSubject CONTROL_STATS = |
| 86 | new MessageSubject("control-plane-stats"); |
| 87 | |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 88 | private Map<ControlMetricType, Double> cpuBuf; |
| 89 | private Map<ControlMetricType, Double> memoryBuf; |
| 90 | private Map<String, Map<ControlMetricType, Double>> diskBuf; |
| 91 | private Map<String, Map<ControlMetricType, Double>> networkBuf; |
| 92 | private Map<DeviceId, Map<ControlMetricType, Double>> ctrlMsgBuf; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 93 | |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 94 | private Map<Type, Set<String>> availableResourceMap; |
| 95 | private Set<DeviceId> availableDeviceIdSet; |
| 96 | |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 97 | private ExecutorService messageHandlingExecutor; |
| 98 | |
| 99 | private static final String METRIC_TYPE_NULL = "Control metric type cannot be null"; |
| 100 | |
Jian Li | daf55ea | 2016-04-04 20:38:30 -0700 | [diff] [blame^] | 101 | Set<Map<ControlMetricType, Double>> debugSets = Sets.newHashSet(); |
| 102 | |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 103 | private static final Serializer SERIALIZER = Serializer |
| 104 | .using(new KryoNamespace.Builder() |
| 105 | .register(KryoNamespaces.API) |
| 106 | .register(ControlMetricsRequest.class) |
| 107 | .register(DefaultControlLoad.class) |
| 108 | .register(DefaultMetricsDatabase.class) |
| 109 | .register(ControlMetricType.class) |
| 110 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID).build()); |
| 111 | |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 112 | @Activate |
| 113 | public void activate() { |
Jian Li | daf55ea | 2016-04-04 20:38:30 -0700 | [diff] [blame^] | 114 | cpuMetrics = genMDbBuilder(DEFAULT_RESOURCE, Type.CPU, CPU_METRICS); |
| 115 | memoryMetrics = genMDbBuilder(DEFAULT_RESOURCE, Type.MEMORY, MEMORY_METRICS); |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 116 | controlMessageMap = Maps.newConcurrentMap(); |
| 117 | diskMetricsMap = Maps.newConcurrentMap(); |
| 118 | networkMetricsMap = Maps.newConcurrentMap(); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 119 | |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 120 | cpuBuf = Maps.newConcurrentMap(); |
| 121 | memoryBuf = Maps.newConcurrentMap(); |
| 122 | diskBuf = Maps.newConcurrentMap(); |
| 123 | networkBuf = Maps.newConcurrentMap(); |
| 124 | ctrlMsgBuf = Maps.newConcurrentMap(); |
| 125 | |
| 126 | availableResourceMap = Maps.newConcurrentMap(); |
| 127 | availableDeviceIdSet = Sets.newConcurrentHashSet(); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 128 | |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 129 | messageHandlingExecutor = Executors.newSingleThreadScheduledExecutor( |
| 130 | groupedThreads("onos/app/cpman", "message-handlers")); |
| 131 | |
| 132 | communicationService.addSubscriber(CONTROL_STATS, |
| 133 | SERIALIZER::decode, this::handleRequest, messageHandlingExecutor); |
| 134 | |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 135 | log.info("Started"); |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Deactivate |
| 139 | public void deactivate() { |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 140 | |
| 141 | // TODO: need to handle the mdb close. |
| 142 | cpuBuf.clear(); |
| 143 | memoryBuf.clear(); |
| 144 | diskBuf.clear(); |
| 145 | networkBuf.clear(); |
| 146 | ctrlMsgBuf.clear(); |
| 147 | |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 148 | communicationService.removeSubscriber(CONTROL_STATS); |
| 149 | |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 150 | log.info("Stopped"); |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 153 | @Override |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 154 | public void updateMetric(ControlMetric cm, int updateIntervalInMinutes, |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 155 | Optional<DeviceId> deviceId) { |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 156 | if (deviceId.isPresent()) { |
Jian Li | 4614890 | 2016-01-29 13:33:50 -0800 | [diff] [blame] | 157 | |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 158 | // insert a new device entry if we cannot find any |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 159 | ctrlMsgBuf.putIfAbsent(deviceId.get(), Maps.newConcurrentMap()); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 160 | |
| 161 | // update control message metrics |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 162 | if (CONTROL_MESSAGE_METRICS.contains(cm.metricType())) { |
| 163 | |
| 164 | if (!availableDeviceIdSet.contains(deviceId.get())) { |
| 165 | availableDeviceIdSet.add(deviceId.get()); |
| 166 | } |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 167 | |
| 168 | // we will accumulate the metric value into buffer first |
| 169 | ctrlMsgBuf.get(deviceId.get()).putIfAbsent(cm.metricType(), |
| 170 | (double) cm.metricValue().getLoad()); |
| 171 | |
| 172 | // if buffer contains all control message metrics, |
| 173 | // we simply set and update the values into MetricsDatabase. |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 174 | if (ctrlMsgBuf.get(deviceId.get()).keySet() |
| 175 | .containsAll(CONTROL_MESSAGE_METRICS)) { |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 176 | updateControlMessages(ctrlMsgBuf.get(deviceId.get()), deviceId.get()); |
Jian Li | daf55ea | 2016-04-04 20:38:30 -0700 | [diff] [blame^] | 177 | ctrlMsgBuf.get(deviceId.get()); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | } else { |
| 181 | |
| 182 | // update cpu metrics |
| 183 | if (CPU_METRICS.contains(cm.metricType())) { |
| 184 | cpuBuf.putIfAbsent(cm.metricType(), |
| 185 | (double) cm.metricValue().getLoad()); |
| 186 | if (cpuBuf.keySet().containsAll(CPU_METRICS)) { |
| 187 | cpuMetrics.updateMetrics(convertMap(cpuBuf)); |
| 188 | cpuBuf.clear(); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | // update memory metrics |
| 193 | if (MEMORY_METRICS.contains(cm.metricType())) { |
| 194 | memoryBuf.putIfAbsent(cm.metricType(), |
| 195 | (double) cm.metricValue().getLoad()); |
| 196 | if (memoryBuf.keySet().containsAll(MEMORY_METRICS)) { |
| 197 | memoryMetrics.updateMetrics(convertMap(memoryBuf)); |
| 198 | memoryBuf.clear(); |
| 199 | } |
| 200 | } |
| 201 | } |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | @Override |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 205 | public void updateMetric(ControlMetric cm, int updateIntervalInMinutes, |
Jian Li | e044d1a | 2016-01-25 09:01:20 -0800 | [diff] [blame] | 206 | String resourceName) { |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 207 | // update disk metrics |
| 208 | if (DISK_METRICS.contains(cm.metricType())) { |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 209 | diskBuf.putIfAbsent(resourceName, Maps.newConcurrentMap()); |
| 210 | |
| 211 | availableResourceMap.putIfAbsent(Type.DISK, Sets.newHashSet()); |
| 212 | availableResourceMap.computeIfPresent(Type.DISK, (k, v) -> { |
| 213 | v.add(resourceName); |
| 214 | return v; |
| 215 | }); |
| 216 | |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 217 | diskBuf.get(resourceName).putIfAbsent(cm.metricType(), |
| 218 | (double) cm.metricValue().getLoad()); |
| 219 | if (diskBuf.get(resourceName).keySet().containsAll(DISK_METRICS)) { |
| 220 | updateDiskMetrics(diskBuf.get(resourceName), resourceName); |
| 221 | diskBuf.clear(); |
| 222 | } |
| 223 | } |
Jian Li | e044d1a | 2016-01-25 09:01:20 -0800 | [diff] [blame] | 224 | |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 225 | // update network metrics |
| 226 | if (NETWORK_METRICS.contains(cm.metricType())) { |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 227 | networkBuf.putIfAbsent(resourceName, Maps.newConcurrentMap()); |
| 228 | |
| 229 | availableResourceMap.putIfAbsent(Type.NETWORK, Sets.newHashSet()); |
| 230 | availableResourceMap.computeIfPresent(Type.NETWORK, (k, v) -> { |
| 231 | v.add(resourceName); |
| 232 | return v; |
| 233 | }); |
| 234 | |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 235 | networkBuf.get(resourceName).putIfAbsent(cm.metricType(), |
| 236 | (double) cm.metricValue().getLoad()); |
| 237 | if (networkBuf.get(resourceName).keySet().containsAll(NETWORK_METRICS)) { |
| 238 | updateNetworkMetrics(networkBuf.get(resourceName), resourceName); |
| 239 | networkBuf.clear(); |
| 240 | } |
| 241 | } |
Jian Li | e044d1a | 2016-01-25 09:01:20 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | @Override |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 245 | public ControlLoad getLocalLoad(ControlMetricType type, |
| 246 | Optional<DeviceId> deviceId) { |
| 247 | if (deviceId.isPresent()) { |
| 248 | if (CONTROL_MESSAGE_METRICS.contains(type) && |
| 249 | availableDeviceIdSet.contains(deviceId.get())) { |
| 250 | return new DefaultControlLoad(controlMessageMap.get(deviceId.get()), type); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 251 | } |
| 252 | } else { |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 253 | // returns controlLoad of CPU metrics |
| 254 | if (CPU_METRICS.contains(type)) { |
| 255 | return new DefaultControlLoad(cpuMetrics, type); |
| 256 | } |
| 257 | |
| 258 | // returns memoryLoad of memory metrics |
| 259 | if (MEMORY_METRICS.contains(type)) { |
| 260 | return new DefaultControlLoad(memoryMetrics, type); |
| 261 | } |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 262 | } |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 263 | return null; |
| 264 | } |
| 265 | |
| 266 | @Override |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 267 | public ControlLoad getLocalLoad(ControlMetricType type, String resourceName) { |
| 268 | if (DISK_METRICS.contains(type) && |
| 269 | availableResources(Type.DISK).contains(resourceName)) { |
| 270 | return new DefaultControlLoad(diskMetricsMap.get(resourceName), type); |
| 271 | } |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 272 | |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 273 | if (NETWORK_METRICS.contains(type) && |
| 274 | availableResources(Type.NETWORK).contains(resourceName)) { |
| 275 | return new DefaultControlLoad(networkMetricsMap.get(resourceName), type); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 276 | } |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 277 | return null; |
| 278 | } |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 279 | |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 280 | @Override |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 281 | public CompletableFuture<ControlLoad> getRemoteLoad(NodeId nodeId, |
| 282 | ControlMetricType type, |
| 283 | Optional<DeviceId> deviceId) { |
| 284 | return communicationService.sendAndReceive(createRequest(type, deviceId), |
| 285 | CONTROL_STATS, SERIALIZER::encode, SERIALIZER::decode, nodeId); |
| 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public CompletableFuture<ControlLoad> getRemoteLoad(NodeId nodeId, |
| 290 | ControlMetricType type, |
| 291 | String resourceName) { |
| 292 | return communicationService.sendAndReceive(createRequest(type, resourceName), |
| 293 | CONTROL_STATS, SERIALIZER::encode, SERIALIZER::decode, nodeId); |
| 294 | } |
| 295 | |
| 296 | |
| 297 | @Override |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 298 | public Set<String> availableResources(Type resourceType) { |
| 299 | if (RESOURCE_TYPE_SET.contains(resourceType)) { |
| 300 | if (Type.CONTROL_MESSAGE.equals(resourceType)) { |
| 301 | return availableDeviceIdSet.stream().map(id -> |
| 302 | id.toString()).collect(Collectors.toSet()); |
| 303 | } else { |
Jian Li | 4563aa2 | 2016-04-04 14:57:38 -0700 | [diff] [blame] | 304 | Set<String> res = availableResourceMap.get(resourceType); |
| 305 | return res == null ? ImmutableSet.of() : res; |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 306 | } |
| 307 | } |
Jian Li | 4563aa2 | 2016-04-04 14:57:38 -0700 | [diff] [blame] | 308 | return ImmutableSet.of(); |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Jian Li | daf55ea | 2016-04-04 20:38:30 -0700 | [diff] [blame^] | 311 | private MetricsDatabase genMDbBuilder(String resourceName, |
| 312 | Type resourceType, |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 313 | Set<ControlMetricType> metricTypes) { |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 314 | MetricsDatabase.Builder builder = new DefaultMetricsDatabase.Builder(); |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 315 | builder.withMetricName(resourceType.toString()); |
Jian Li | daf55ea | 2016-04-04 20:38:30 -0700 | [diff] [blame^] | 316 | builder.withResourceName(resourceName); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 317 | metricTypes.forEach(type -> builder.addMetricType(type.toString())); |
| 318 | return builder.build(); |
| 319 | } |
| 320 | |
| 321 | private void updateNetworkMetrics(Map<ControlMetricType, Double> metricMap, |
| 322 | String resName) { |
Jian Li | daf55ea | 2016-04-04 20:38:30 -0700 | [diff] [blame^] | 323 | networkMetricsMap.putIfAbsent(resName, genMDbBuilder(resName, |
| 324 | Type.NETWORK, NETWORK_METRICS)); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 325 | networkMetricsMap.get(resName).updateMetrics(convertMap(metricMap)); |
| 326 | } |
| 327 | |
| 328 | private void updateDiskMetrics(Map<ControlMetricType, Double> metricMap, |
| 329 | String resName) { |
Jian Li | daf55ea | 2016-04-04 20:38:30 -0700 | [diff] [blame^] | 330 | diskMetricsMap.putIfAbsent(resName, genMDbBuilder(resName, |
| 331 | Type.DISK, DISK_METRICS)); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 332 | diskMetricsMap.get(resName).updateMetrics(convertMap(metricMap)); |
| 333 | } |
| 334 | |
| 335 | private void updateControlMessages(Map<ControlMetricType, Double> metricMap, |
| 336 | DeviceId devId) { |
Jian Li | daf55ea | 2016-04-04 20:38:30 -0700 | [diff] [blame^] | 337 | controlMessageMap.putIfAbsent(devId, genMDbBuilder(devId.toString(), |
| 338 | Type.CONTROL_MESSAGE, CONTROL_MESSAGE_METRICS)); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 339 | controlMessageMap.get(devId).updateMetrics(convertMap(metricMap)); |
| 340 | } |
| 341 | |
| 342 | private Map convertMap(Map<ControlMetricType, Double> map) { |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 343 | Map newMap = Maps.newConcurrentMap(); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 344 | map.forEach((k, v) -> newMap.putIfAbsent(k.toString(), v)); |
| 345 | return newMap; |
| 346 | } |
Jian Li | 23906cc | 2016-03-31 11:16:44 -0700 | [diff] [blame] | 347 | |
| 348 | private CompletableFuture<ControlLoad> handleRequest(ControlMetricsRequest request) { |
| 349 | |
| 350 | checkArgument(request.getType() != null, METRIC_TYPE_NULL); |
| 351 | |
| 352 | ControlLoad load; |
| 353 | if (request.getResourceName() != null) { |
| 354 | load = getLocalLoad(request.getType(), request.getResourceName()); |
| 355 | } else { |
| 356 | load = getLocalLoad(request.getType(), request.getDeviceId()); |
| 357 | } |
| 358 | return CompletableFuture.completedFuture(load); |
| 359 | } |
| 360 | |
| 361 | private ControlMetricsRequest createRequest(ControlMetricType type, |
| 362 | Optional<DeviceId> deviceId) { |
| 363 | return new ControlMetricsRequest(type, deviceId); |
| 364 | } |
| 365 | |
| 366 | private ControlMetricsRequest createRequest(ControlMetricType type, |
| 367 | String resourceName) { |
| 368 | return new ControlMetricsRequest(type, resourceName); |
| 369 | } |
| 370 | } |