kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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.nodemetrics.impl; |
| 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Modified; |
| 22 | import org.apache.felix.scr.annotations.Property; |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.apache.felix.scr.annotations.Service; |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 26 | import org.osgi.service.component.ComponentContext; |
| 27 | import org.onosproject.cfg.ComponentConfigService; |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 28 | import org.hyperic.sigar.CpuPerc; |
| 29 | import org.hyperic.sigar.FileSystemUsage; |
| 30 | import org.hyperic.sigar.Mem; |
| 31 | import org.hyperic.sigar.Sigar; |
| 32 | import org.hyperic.sigar.SigarException; |
| 33 | import org.onlab.util.KryoNamespace; |
| 34 | import org.onlab.util.Tools; |
| 35 | import org.onosproject.cluster.ClusterService; |
| 36 | import org.onosproject.cluster.NodeId; |
| 37 | import org.onosproject.core.ApplicationId; |
| 38 | import org.onosproject.core.CoreService; |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 39 | import org.onosproject.nodemetrics.NodeCpuUsage; |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 40 | import org.onosproject.nodemetrics.NodeDiskUsage; |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 41 | import org.onosproject.nodemetrics.NodeMemoryUsage; |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 42 | import org.onosproject.nodemetrics.NodeMetricsService; |
| 43 | import org.onosproject.nodemetrics.Units; |
| 44 | import org.onosproject.store.serializers.KryoNamespaces; |
| 45 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 46 | import org.onosproject.store.service.LogicalClockService; |
| 47 | import org.onosproject.store.service.StorageService; |
| 48 | import org.slf4j.Logger; |
| 49 | import org.slf4j.LoggerFactory; |
| 50 | |
| 51 | import java.util.Map; |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 52 | import java.util.Objects; |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 53 | import java.util.concurrent.Executors; |
| 54 | import java.util.concurrent.ScheduledExecutorService; |
| 55 | import java.util.concurrent.ScheduledFuture; |
| 56 | import java.util.concurrent.TimeUnit; |
| 57 | import java.util.stream.Collectors; |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 58 | import java.util.Dictionary; |
| 59 | import static org.onlab.util.Tools.getIntegerProperty; |
| 60 | |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 61 | |
| 62 | @Service |
| 63 | @Component(immediate = true) |
| 64 | public class NodeMetricsManager implements NodeMetricsService { |
| 65 | private static final int DEFAULT_POLL_FREQUENCY_SECONDS = 15; |
| 66 | private static final String SLASH = "/"; |
| 67 | private static final Double PERCENTAGE_MULTIPLIER = 100.0; |
| 68 | private final Logger log = LoggerFactory |
| 69 | .getLogger(this.getClass()); |
| 70 | |
| 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 72 | protected CoreService coreService; |
| 73 | |
| 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 75 | protected StorageService storageService; |
| 76 | |
| 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 78 | protected ClusterService clusterService; |
| 79 | |
| 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 81 | protected LogicalClockService clockService; |
| 82 | |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 84 | protected ComponentConfigService cfgService; |
| 85 | |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 86 | private ScheduledExecutorService metricsExecutor; |
| 87 | private ScheduledFuture<?> scheduledTask; |
| 88 | |
| 89 | private ApplicationId appId; |
| 90 | private NodeId localNodeId; |
| 91 | |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 92 | private EventuallyConsistentMap<NodeId, NodeMemoryUsage> memoryStore; |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 93 | private EventuallyConsistentMap<NodeId, NodeDiskUsage> diskStore; |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 94 | private EventuallyConsistentMap<NodeId, NodeCpuUsage> cpuStore; |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 95 | |
| 96 | private Sigar sigar; |
| 97 | |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 98 | @Property(name = "metricPollFrequencySeconds", intValue = DEFAULT_POLL_FREQUENCY_SECONDS, |
| 99 | label = "Frequency (in seconds) for polling controller metrics") |
| 100 | protected int metricPollFrequencySeconds = DEFAULT_POLL_FREQUENCY_SECONDS; |
| 101 | |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 102 | @Activate |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 103 | public void activate(ComponentContext context) { |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 104 | appId = coreService |
| 105 | .registerApplication("org.onosproject.nodemetrics"); |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 106 | cfgService.registerProperties(getClass()); |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 107 | metricsExecutor = Executors.newSingleThreadScheduledExecutor( |
| 108 | Tools.groupedThreads("nodemetrics/pollingStatics", |
| 109 | "statistics-executor-%d", log)); |
| 110 | |
| 111 | localNodeId = clusterService.getLocalNode().id(); |
| 112 | KryoNamespace.Builder serializer = KryoNamespace.newBuilder() |
| 113 | .register(KryoNamespaces.API) |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 114 | .register(NodeMemoryUsage.class) |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 115 | .register(NodeDiskUsage.class) |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 116 | .register(NodeCpuUsage.class) |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 117 | .register(Units.class); |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 118 | memoryStore = storageService.<NodeId, NodeMemoryUsage>eventuallyConsistentMapBuilder() |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 119 | .withSerializer(serializer) |
| 120 | .withTimestampProvider((nodeId, memory) -> clockService.getTimestamp()) |
| 121 | .withName("nodemetrics-memory") |
| 122 | .build(); |
| 123 | |
| 124 | diskStore = storageService.<NodeId, NodeDiskUsage>eventuallyConsistentMapBuilder() |
| 125 | .withSerializer(serializer) |
| 126 | .withTimestampProvider((nodeId, disk) -> clockService.getTimestamp()) |
| 127 | .withName("nodemetrics-disk") |
| 128 | .build(); |
| 129 | |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 130 | cpuStore = storageService.<NodeId, NodeCpuUsage>eventuallyConsistentMapBuilder() |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 131 | .withSerializer(serializer) |
| 132 | .withTimestampProvider((nodeId, cpu) -> clockService.getTimestamp()) |
| 133 | .withName("nodemetrics-cpu") |
| 134 | .build(); |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 135 | modified(context); |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 136 | sigar = new Sigar(); |
| 137 | pollMetrics(); |
| 138 | } |
| 139 | |
| 140 | @Deactivate |
| 141 | public void deactivate() { |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 142 | cfgService.unregisterProperties(getClass(), false); |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 143 | scheduledTask.cancel(true); |
| 144 | metricsExecutor.shutdown(); |
| 145 | sigar.close(); |
| 146 | } |
| 147 | |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 148 | @Modified |
| 149 | public void modified(ComponentContext context) { |
| 150 | if (context == null) { |
| 151 | log.info("No component configuration"); |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | Dictionary<?, ?> properties = context.getProperties(); |
| 156 | int newPollFrequency = getNewPollFrequency(properties); |
| 157 | //First time call to this modified method is when app activates |
| 158 | if (Objects.isNull(scheduledTask)) { |
| 159 | metricPollFrequencySeconds = newPollFrequency; |
| 160 | scheduledTask = schedulePolling(); |
| 161 | } else { |
| 162 | if (newPollFrequency != metricPollFrequencySeconds) { |
| 163 | metricPollFrequencySeconds = newPollFrequency; |
| 164 | //stops the old scheduled task |
| 165 | scheduledTask.cancel(true); |
| 166 | //schedules new task at the new polling rate |
| 167 | scheduledTask = schedulePolling(); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 172 | @Override |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 173 | public Map<NodeId, NodeMemoryUsage> memory() { |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 174 | return this.ecToMap(memoryStore); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public Map<NodeId, NodeDiskUsage> disk() { |
| 179 | return this.ecToMap(diskStore); |
| 180 | } |
| 181 | |
| 182 | @Override |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 183 | public Map<NodeId, NodeCpuUsage> cpu() { |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 184 | return this.ecToMap(cpuStore); |
| 185 | } |
| 186 | |
| 187 | @Override |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 188 | public NodeMemoryUsage memory(NodeId nodeid) { |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 189 | return memoryStore.get(nodeid); |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public NodeDiskUsage disk(NodeId nodeid) { |
| 194 | return diskStore.get(nodeid); |
| 195 | } |
| 196 | |
| 197 | @Override |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 198 | public NodeCpuUsage cpu(NodeId nodeid) { |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 199 | return cpuStore.get(nodeid); |
| 200 | } |
| 201 | |
| 202 | private ScheduledFuture schedulePolling() { |
| 203 | return metricsExecutor.scheduleAtFixedRate(this::pollMetrics, |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 204 | metricPollFrequencySeconds / 4, |
| 205 | metricPollFrequencySeconds, TimeUnit.SECONDS); |
| 206 | } |
| 207 | |
| 208 | private int getNewPollFrequency(Dictionary<?, ?> properties) { |
| 209 | int newPollFrequency; |
| 210 | try { |
| 211 | newPollFrequency = getIntegerProperty(properties, "metricPollFrequencySeconds"); |
| 212 | //String s = getIntegerProperty(properties, "metricPollFrequencySeconds"); |
| 213 | //newPollFrequency = isNullOrEmpty(s) ? pollFrequency : Integer.parseInt(s.trim()); |
| 214 | } catch (NumberFormatException | ClassCastException e) { |
| 215 | newPollFrequency = DEFAULT_POLL_FREQUENCY_SECONDS; |
| 216 | } |
| 217 | return newPollFrequency; |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | private <K, V> Map<K, V> ecToMap(EventuallyConsistentMap<K, V> ecMap) { |
| 221 | return ecMap.entrySet() |
| 222 | .stream() |
| 223 | .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
| 224 | } |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 225 | |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 226 | private void pollMetrics() { |
| 227 | try { |
| 228 | CpuPerc cpu = sigar.getCpuPerc(); |
| 229 | Mem mem = sigar.getMem(); |
| 230 | FileSystemUsage disk = sigar.getFileSystemUsage(SLASH); |
| 231 | |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 232 | NodeMemoryUsage memoryNode = new NodeMemoryUsage.Builder().free(mem.getFree()) |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 233 | .used(mem.getUsed()).total(mem.getTotal()).withUnit(Units.BYTES) |
| 234 | .withNode(localNodeId).build(); |
nitinanand | 920fcb4 | 2018-05-18 17:30:36 +0530 | [diff] [blame] | 235 | NodeCpuUsage cpuNode = new NodeCpuUsage.Builder().withNode(localNodeId) |
kdarapu | 97843dc | 2018-05-10 12:46:32 +0530 | [diff] [blame] | 236 | .usage(cpu.getCombined() * PERCENTAGE_MULTIPLIER).build(); |
| 237 | NodeDiskUsage diskNode = new NodeDiskUsage.Builder().withNode(localNodeId) |
| 238 | .free(disk.getFree()).used(disk.getUsed()).withUnit(Units.KBYTES) |
| 239 | .total(disk.getTotal()).build(); |
| 240 | diskStore.put(localNodeId, diskNode); |
| 241 | memoryStore.put(localNodeId, memoryNode); |
| 242 | cpuStore.put(localNodeId, cpuNode); |
| 243 | |
| 244 | } catch (SigarException e) { |
| 245 | log.error("Exception occurred ", e); |
| 246 | } |
| 247 | |
| 248 | } |
| 249 | } |