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 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;
26 public class SchedulerA extends AbstractScheduler {
28 private static final Logger log = LoggerFactory.getLogger(SchedulerA.class);
30 public SchedulerA(int numPMs, int numCloudPartners, File schedulerLog,
31 SchedulerType schedulerType, ScenarioType scenario)
33 super(numPMs, numCloudPartners, schedulerLog, schedulerType, scenario);
35 this.vmType = VMType.NonResizable;
39 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 } catch (ActiveApplicationsException e) {
49 log.warn("VM " + vm.getId() + "could not be stopped, "
55 // sorting applications by amount of resources (descending)
56 List<SortedApplication> sortedApps = sortApps(events);
58 for (Iterator<SortedApplication> iter = sortedApps.iterator(); iter
60 boolean appDeployed = false;
61 Application app = iter.next().getApp();
63 if (manager.getPMs().size() == 0) {
64 PhysicalMachine pm = manager.startPhysicalMachine();
65 boolean enoughResources = pm.checkVM(app.getSize(),
66 app.getRam(), app.getCpu());
68 if (enoughResources) {
69 pm.startVirtualMachine(app.getSize(), app.getRam(),
70 app.getCpu(), vmType);
71 insertStopEvent(currTime + app.getDuration(), app);
73 log.debug("Application " + app.toString()
74 + " started on new pm " + pm.getId());
76 log.warn("Application " + app.toString()
77 + " cannot be run on empty pm " + pm.getId());
80 // sorting physical machines by resource utilization
82 List<SortedPhysicalMachine> sortedPMs = sortPMs();
84 for (Iterator<SortedPhysicalMachine> it = sortedPMs.iterator(); iter
86 PhysicalMachine pm = it.next().getPm();
87 boolean enoughResources = pm.checkVM(app.getSize(),
88 app.getRam(), app.getCpu());
90 if (enoughResources) {
91 VirtualMachine vm = pm.startVirtualMachine(
92 app.getSize(), app.getRam(), app.getCpu(),
94 vm.startApplication(app);
95 insertStopEvent(currTime + app.getDuration(), app);
97 log.debug("Application " + app.toString()
98 + " started on new pm " + pm.getId());
103 && (manager.getPMs().size() < manager.getMaxPMs())) {
105 PhysicalMachine pm = manager.startPhysicalMachine();
106 boolean enoughResources = pm.checkVM(app.getSize(),
107 app.getRam(), app.getCpu());
109 if (enoughResources) {
110 VirtualMachine vm = pm.startVirtualMachine(
111 app.getSize(), app.getRam(), app.getCpu(),
113 vm.startApplication(app);
114 insertStopEvent(currTime + app.getDuration(), app);
116 log.debug("Application " + app.toString()
117 + " started on new pm " + pm.getId());
119 log.warn("Application " + app.toString()
120 + " cannot be run on empty pm " + pm.getId());
125 log.warn("Application " + app.toString()
126 + " could not be deployed on any pm");
130 // sorting applications by amount of resources (descending)
131 private List<SortedApplication> sortApps(LinkedList<SchedulerEvent> events) {
132 List<SortedApplication> sortedApps = new LinkedList<SortedApplication>();
133 for (SchedulerEvent evt : events) {
134 if (evt.getType() == EventType.startApplication)
135 sortedApps.add(new SortedApplication(evt.getApp()));
137 Collections.sort(sortedApps);
138 Collections.reverse(sortedApps);
142 // sorting physical machines by resource utilization (descending)
143 private List<SortedPhysicalMachine> sortPMs() {
144 List<SortedPhysicalMachine> sortedPMs = new LinkedList<SortedPhysicalMachine>();
145 for (PhysicalMachine pm : manager.getPMs())
146 sortedPMs.add(new SortedPhysicalMachine(pm));
148 Collections.sort(sortedPMs);
149 Collections.reverse(sortedPMs);