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.exception.VMsRunningException;
16 import at.ac.tuwien.lsdc.types.Application;
17 import at.ac.tuwien.lsdc.types.PhysicalMachine;
18 import at.ac.tuwien.lsdc.types.ScenarioData;
19 import at.ac.tuwien.lsdc.types.ScenarioType;
20 import at.ac.tuwien.lsdc.types.SchedulerEvent;
21 import at.ac.tuwien.lsdc.types.SchedulerEvent.EventType;
22 import at.ac.tuwien.lsdc.types.SchedulerType;
23 import at.ac.tuwien.lsdc.types.SortedApplication;
24 import at.ac.tuwien.lsdc.types.SortedPhysicalMachine;
25 import at.ac.tuwien.lsdc.types.VirtualMachine;
26 import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
28 public class SchedulerA extends AbstractScheduler {
30 private static final Logger log = LoggerFactory.getLogger(SchedulerA.class);
32 public SchedulerA(int numPMs, int numCloudPartners, File schedulerLog,
33 ScenarioType scenario) throws IOException {
34 super(numPMs, numCloudPartners, schedulerLog, scenario);
36 this.vmType = VMType.NonResizable;
40 protected void handleEvents(LinkedList<SchedulerEvent> events) {
41 for (SchedulerEvent evt : events) {
42 if (evt.getType() == EventType.endApplication) {
43 VirtualMachine vm = evt.getApp().getRunningOn();
44 vm.stopApplication(evt.getApp());
45 PhysicalMachine pm = vm.getRunningOn();
47 pm.stopVirtualMachine(vm);
48 if (pm.countCurrentlyRunningVMs() == 0) {
50 manager.stopPhysicalMachine(pm.getId());
51 } catch (VMsRunningException e) {
52 log.warn("PM " + pm.getId()
53 + " could not be stopped, "
57 log.info("application stopped at timestamp " + currTime
58 + ", " + "vm " + vm.getPositionOnPM() + ", pm "
60 } catch (ActiveApplicationsException e) {
61 log.warn("VM " + vm.getId() + "could not be stopped, "
67 // sorting applications by amount of resources (descending)
68 List<SortedApplication> sortedApps = sortApps(events);
70 for (Iterator<SortedApplication> iter = sortedApps.iterator(); iter
72 boolean appDeployed = false;
73 Application app = iter.next().getApp();
75 if (manager.getPMs().size() == 0) {
76 PhysicalMachine pm = manager.startPhysicalMachine();
77 boolean enoughResources = pm.checkVM(app.getSize(),
78 app.getRam(), app.getCpu());
80 if (enoughResources) {
81 VirtualMachine vm = pm.startVirtualMachine(app.getSize(),
82 app.getRam(), app.getCpu(), vmType);
83 vm.startApplication(app);
84 insertStopEvent(currTime + app.getDuration(), app);
86 log.info("Application " + app.toString()
87 + " started on new pm " + pm.getId());
89 log.warn("Application " + app.toString()
90 + " cannot be run on empty pm " + pm.getId());
93 // sorting physical machines by resource utilization
95 List<SortedPhysicalMachine> sortedPMs = sortPMs();
97 for (Iterator<SortedPhysicalMachine> it = sortedPMs.iterator(); it
100 PhysicalMachine pm = it.next().getPm();
101 boolean enoughResources = pm.checkVM(app.getSize(),
102 app.getRam(), app.getCpu());
104 if (enoughResources) {
105 VirtualMachine vm = pm.startVirtualMachine(
106 app.getSize(), app.getRam(), app.getCpu(),
108 vm.startApplication(app);
109 insertStopEvent(currTime + app.getDuration(), app);
111 log.info("Application " + app.toString()
112 + " started new vm " + vm.getPositionOnPM()
113 + " on pm " + pm.getId());
118 && (manager.getPMs().size() < manager.getMaxPMs())) {
120 PhysicalMachine pm = manager.startPhysicalMachine();
121 boolean enoughResources = pm.checkVM(app.getSize(),
122 app.getRam(), app.getCpu());
124 if (enoughResources) {
125 VirtualMachine vm = pm.startVirtualMachine(
126 app.getSize(), app.getRam(), app.getCpu(),
128 vm.startApplication(app);
129 insertStopEvent(currTime + app.getDuration(), app);
131 log.info("Application " + app.toString()
132 + " started on new pm " + pm.getId());
134 log.warn("Application " + app.toString()
135 + " cannot be run on empty pm " + pm.getId());
140 log.warn("Application " + app.toString()
141 + " could not be deployed on any pm");
142 // TODO: save app in a List and try to deploy with next handled
147 // sorting applications by amount of resources (descending)
148 private List<SortedApplication> sortApps(LinkedList<SchedulerEvent> events) {
149 List<SortedApplication> sortedApps = new LinkedList<SortedApplication>();
150 for (SchedulerEvent evt : events) {
151 if (evt.getType() == EventType.startApplication)
152 sortedApps.add(new SortedApplication(evt.getApp()));
154 Collections.sort(sortedApps);
155 Collections.reverse(sortedApps);
159 // sorting physical machines by resource utilization (descending)
160 private List<SortedPhysicalMachine> sortPMs() {
161 List<SortedPhysicalMachine> sortedPMs = new LinkedList<SortedPhysicalMachine>();
162 for (PhysicalMachine pm : manager.getPMs()) {
163 sortedPMs.add(new SortedPhysicalMachine(pm));
164 // log.info("pm util = "+pm.getAverageUtilization());
167 Collections.sort(sortedPMs);
168 Collections.reverse(sortedPMs);
173 protected String getSchedulerType() {
174 return SchedulerType.A.toString();