1 package at.ac.tuwien.lsdc.sched;
4 import java.io.IOException;
6 import java.util.Iterator;
7 import java.util.LinkedList;
9 import java.util.Collections;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
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.ScenarioData;
18 import at.ac.tuwien.lsdc.types.ScenarioType;
19 import at.ac.tuwien.lsdc.types.SchedulerEvent;
20 import at.ac.tuwien.lsdc.types.SchedulerEvent.EventType;
21 import at.ac.tuwien.lsdc.types.SchedulerType;
22 import at.ac.tuwien.lsdc.types.SortedApplication;
23 import at.ac.tuwien.lsdc.types.SortedPhysicalMachine;
24 import at.ac.tuwien.lsdc.types.VirtualMachine;
25 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 ScenarioType scenario) throws IOException {
33 super(numPMs, numCloudPartners, schedulerLog, scenario);
35 this.vmType = VMType.NonResizable;
39 protected void handleEvents(LinkedList<SchedulerEvent> events) {
40 for (SchedulerEvent evt : events) {
41 if (evt.getType() == EventType.endApplication) {
42 VirtualMachine vm = evt.getApp().getRunningOn();
43 vm.stopApplication(evt.getApp());
44 PhysicalMachine pm = vm.getRunningOn();
46 pm.stopVirtualMachine(vm);
47 log.info("application stopped at timestamp "+currTime+", " +
48 "vm "+vm.getPositionOnPM()+", pm "+pm.getId());
49 } catch (ActiveApplicationsException e) {
50 log.warn("VM " + vm.getId() + "could not be stopped, "
56 // sorting applications by amount of resources (descending)
57 List<SortedApplication> sortedApps = sortApps(events);
59 for (Iterator<SortedApplication> iter = sortedApps.iterator(); iter
61 boolean appDeployed = false;
62 Application app = iter.next().getApp();
64 if (manager.getPMs().size() == 0) {
65 PhysicalMachine pm = manager.startPhysicalMachine();
66 boolean enoughResources = pm.checkVM(app.getSize(),
67 app.getRam(), app.getCpu());
69 if (enoughResources) {
70 VirtualMachine vm = pm.startVirtualMachine(app.getSize(), app.getRam(),
71 app.getCpu(), vmType);
72 vm.startApplication(app);
73 insertStopEvent(currTime + app.getDuration(), app);
75 log.info("Application " + app.toString()
76 + " started on new pm " + pm.getId());
78 log.warn("Application " + app.toString()
79 + " cannot be run on empty pm " + pm.getId());
82 // sorting physical machines by resource utilization
84 List<SortedPhysicalMachine> sortedPMs = sortPMs();
86 for (Iterator<SortedPhysicalMachine> it = sortedPMs.iterator(); it
89 PhysicalMachine pm = it.next().getPm();
90 boolean enoughResources = pm.checkVM(app.getSize(),
91 app.getRam(), app.getCpu());
93 if (enoughResources) {
94 VirtualMachine vm = pm.startVirtualMachine(
95 app.getSize(), app.getRam(), app.getCpu(),
97 vm.startApplication(app);
98 insertStopEvent(currTime + app.getDuration(), app);
100 log.info("Application " + app.toString()
101 + " started new vm " +vm.getPositionOnPM()+" on pm "+ pm.getId());
106 && (manager.getPMs().size() < manager.getMaxPMs())) {
108 PhysicalMachine pm = manager.startPhysicalMachine();
109 boolean enoughResources = pm.checkVM(app.getSize(),
110 app.getRam(), app.getCpu());
112 if (enoughResources) {
113 VirtualMachine vm = pm.startVirtualMachine(
114 app.getSize(), app.getRam(), app.getCpu(),
116 vm.startApplication(app);
117 insertStopEvent(currTime + app.getDuration(), app);
119 log.info("Application " + app.toString()
120 + " started on new pm " + pm.getId());
122 log.warn("Application " + app.toString()
123 + " cannot be run on empty pm " + pm.getId());
128 log.warn("Application " + app.toString()
129 + " could not be deployed on any pm");
130 //TODO: save app in a List and try to deploy with next handled events
134 // sorting applications by amount of resources (descending)
135 private List<SortedApplication> sortApps(LinkedList<SchedulerEvent> events) {
136 List<SortedApplication> sortedApps = new LinkedList<SortedApplication>();
137 for (SchedulerEvent evt : events) {
138 if (evt.getType() == EventType.startApplication)
139 sortedApps.add(new SortedApplication(evt.getApp()));
141 Collections.sort(sortedApps);
142 Collections.reverse(sortedApps);
146 // sorting physical machines by resource utilization (descending)
147 private List<SortedPhysicalMachine> sortPMs() {
148 List<SortedPhysicalMachine> sortedPMs = new LinkedList<SortedPhysicalMachine>();
149 for (PhysicalMachine pm : manager.getPMs()) {
150 sortedPMs.add(new SortedPhysicalMachine(pm));
151 // log.info("pm util = "+pm.getAverageUtilization());
154 Collections.sort(sortedPMs);
155 Collections.reverse(sortedPMs);
161 * this creates the total summary which should be written to a CSV at the
164 * @return a ScenarioData Object that holds the values to be logged
166 protected ScenarioData doEndLogging() {
167 return new ScenarioData(SchedulerType.A.toString(), scenario.toString(),
168 1, 1, 1, 1, numTotalInSourced, numTotalOutSourced);