1 package at.ac.tuwien.lsdc.sched;
4 import java.io.IOException;
5 import java.util.Iterator;
6 import java.util.LinkedList;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
12 import edu.emory.mathcs.backport.java.util.Collections;
14 import at.ac.tuwien.lsdc.exception.ActiveApplicationsException;
15 import at.ac.tuwien.lsdc.types.Application;
16 import at.ac.tuwien.lsdc.types.PhysicalMachine;
17 import at.ac.tuwien.lsdc.types.ScenarioType;
18 import at.ac.tuwien.lsdc.types.SchedulerEvent;
19 import at.ac.tuwien.lsdc.types.SchedulerEvent.EventType;
20 import at.ac.tuwien.lsdc.types.SchedulerType;
21 import at.ac.tuwien.lsdc.types.SortedApplication;
22 import at.ac.tuwien.lsdc.types.SortedPhysicalMachine;
23 import at.ac.tuwien.lsdc.types.VirtualMachine;
24 import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
27 public class SchedulerA extends AbstractScheduler {
29 private static final Logger log = LoggerFactory.getLogger(SchedulerA.class);
31 public SchedulerA(int numPMs, int numCloudPartners, File schedulerLog,
32 SchedulerType schedulerType, ScenarioType scenario)
34 super(numPMs, numCloudPartners, schedulerLog, schedulerType, scenario);
36 this.vmType = VMType.NonResizable;
40 protected void handleEvents(LinkedList<SchedulerEvent> events) {
42 for(SchedulerEvent evt : events) {
43 if(evt.getType() == EventType.endApplication) {
44 VirtualMachine vm = evt.getApp().getRunningOn();
45 vm.stopApplication(evt.getApp());
46 PhysicalMachine pm = vm.getRunningOn();
48 pm.stopVirtualMachine(vm);
49 } catch (ActiveApplicationsException e) {
50 log.warn("VM "+vm.getId()+"could not be stopped, "+e.getMessage());
55 // sorting applications by amount of resources (descending)
56 List<SortedApplication> sortedApps = sortApps(events);
58 for(Iterator<SortedApplication> iter = sortedApps.iterator(); iter.hasNext(); ) {
59 boolean appDeployed = false;
60 Application app = iter.next().getApp();
62 if(manager.getPMs().size() == 0) {
63 PhysicalMachine pm = manager.startPhysicalMachine();
64 boolean enoughResources = pm.checkVM(app.getSize(), app.getRam(), app.getCpu());
67 pm.startVirtualMachine(app.getSize(), app.getRam(), app.getCpu(), vmType);
69 log.debug("Application "+app.toString()+" started on new pm "+pm.getId());
72 log.warn("Application "+app.toString()+" cannot be run on empty pm "+pm.getId());
76 // sorting physical machines by resource utilization (descending)
77 List<SortedPhysicalMachine> sortedPMs = sortPMs();
79 for(Iterator<SortedPhysicalMachine> it = sortedPMs.iterator(); iter.hasNext(); ) {
80 PhysicalMachine pm = it.next().getPm();
81 boolean enoughResources = pm.checkVM(app.getSize(), app.getRam(), app.getCpu());
84 VirtualMachine vm = pm.startVirtualMachine(app.getSize(), app.getRam(), app.getCpu(), vmType);
85 vm.startApplication(app);
87 log.debug("Application "+app.toString()+" started on new pm "+pm.getId());
91 if(!appDeployed && (manager.getPMs().size() < manager.getMaxPMs())) {
93 PhysicalMachine pm = manager.startPhysicalMachine();
94 boolean enoughResources = pm.checkVM(app.getSize(), app.getRam(), app.getCpu());
97 VirtualMachine vm = pm.startVirtualMachine(app.getSize(), app.getRam(), app.getCpu(), vmType);
98 vm.startApplication(app);
100 log.debug("Application "+app.toString()+" started on new pm "+pm.getId());
103 log.warn("Application "+app.toString()+" cannot be run on empty pm "+pm.getId());
108 log.warn("Application "+app.toString()+" could not be deployed on any pm");
112 // sorting applications by amount of resources (descending)
113 private List<SortedApplication> sortApps(LinkedList<SchedulerEvent> events) {
114 List<SortedApplication> sortedApps = new LinkedList<SortedApplication>();
115 for(SchedulerEvent evt : events) {
116 if(evt.getType() == EventType.startApplication)
117 sortedApps.add(new SortedApplication(evt.getApp()));
119 Collections.sort(sortedApps);
120 Collections.reverse(sortedApps);
124 // sorting physical machines by resource utilization (descending)
125 private List<SortedPhysicalMachine> sortPMs() {
126 List<SortedPhysicalMachine> sortedPMs = new LinkedList<SortedPhysicalMachine>();
127 for(PhysicalMachine pm : manager.getPMs())
128 sortedPMs.add(new SortedPhysicalMachine(pm));
130 Collections.sort(sortedPMs);
131 Collections.reverse(sortedPMs);