// Copyright 2012 Citrix Systems, Inc. Licensed under the // Apache License, Version 2.0 (the "License"); you may not use this // file except in compliance with the License. Citrix Systems, Inc. // reserves all rights not expressly granted by 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. // // Automatically generated by addcopyright.py at 04/03/2012 package com.cloud.agent; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; import org.apache.log4j.Logger; import com.cloud.utils.concurrency.NamedThreadFactory; public class MetricsCollector { private static final Logger s_logger = Logger.getLogger(MetricsCollector.class); private final Set vmNames = new HashSet(); private final Set newVMnames = new HashSet(); private final Map metricsMap = new HashMap(); private final transient ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1, new NamedThreadFactory("Metrics")); private Set _currentVms; public MetricsCollector(Set currentVms) { _currentVms = currentVms; getAllVMNames(); } public MetricsCollector() { } public synchronized void getAllVMNames() { Set currentVMs = _currentVms; newVMnames.clear(); newVMnames.addAll(currentVMs); newVMnames.removeAll(vmNames); //leave only new vms vmNames.removeAll(currentVMs); //old vms - current vms --> leave non-running vms; for (String vm: vmNames) { removeVM(vm); } vmNames.clear(); vmNames.addAll(currentVMs); } public synchronized void submitMetricsJobs() { s_logger.debug("Submit Metric Jobs called"); for (String vm : newVMnames) { MockVmMetrics task = new MockVmMetrics(vm); if (!metricsMap.containsKey(vm)) { metricsMap.put(vm, task); ScheduledFuture sf = executor.scheduleWithFixedDelay(task, 2, 600, TimeUnit.SECONDS); task.setFuture(sf); } } newVMnames.clear(); } public synchronized void addVM(String vmName) { newVMnames.add(vmName); s_logger.debug("Added vm name= " + vmName); } public synchronized void removeVM(String vmName) { newVMnames.remove(vmName); vmNames.remove(vmName); MockVmMetrics task = metricsMap.get(vmName); if (task != null) { task.stop(); boolean r1= task.getFuture().cancel(false); metricsMap.remove(vmName); s_logger.debug("removeVM: cancel returned " + r1 + " for VM " + vmName); } else { s_logger.warn("removeVM called for nonexistent VM " + vmName); } } public synchronized Set getVMNames() { return vmNames; } public synchronized Map getMetricsMap() { return metricsMap; } }