1 package at.ac.tuwien.lsdc.sched;
4 import java.io.IOException;
5 import java.util.Collections;
6 import java.util.LinkedList;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
12 import at.ac.tuwien.lsdc.exception.ActiveApplicationsException;
13 import at.ac.tuwien.lsdc.exception.VMsRunningException;
14 import at.ac.tuwien.lsdc.types.Application;
15 import at.ac.tuwien.lsdc.types.ApplicationResourceComparator;
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.SchedulerType;
20 import at.ac.tuwien.lsdc.types.VirtualMachine;
21 import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
23 public class SchedulerA extends AbstractScheduler {
25 private static final Logger log = LoggerFactory.getLogger(SchedulerA.class);
27 public SchedulerA(int numPMs, int numCloudPartners, File schedulerLog, ScenarioType scenario)
29 super(numPMs, numCloudPartners, schedulerLog, scenario);
31 this.vmType = VMType.NonResizable;
35 protected void handleEndEvents(LinkedList<SchedulerEvent> events) {
36 for (SchedulerEvent evt : events) {
37 VirtualMachine vm = evt.getApp().getRunningOn();
38 vm.stopApplication(evt.getApp());
39 PhysicalMachine pm = vm.getRunningOn();
41 pm.stopVirtualMachine(vm);
42 if (pm.countCurrentlyRunningVMs() == 0) {
44 manager.stopPhysicalMachine(pm.getId());
45 } catch (VMsRunningException e) {
46 log.warn("PM " + pm.getId() + " could not be stopped, " + e.getMessage());
49 log.info("application stopped at timestamp " + currTime + ", " + "vm "
50 + vm.getPositionOnPM() + ", pm " + pm.getId());
51 } catch (ActiveApplicationsException e) {
52 log.warn("VM " + vm.getId() + "could not be stopped, " + e.getMessage());
58 protected void handleStartEvents(LinkedList<SchedulerEvent> events) {
59 // sorting applications by amount of resources (descending)
60 List<Application> sortedApps = sortApps(events);
62 for (Application app : sortedApps) {
63 boolean appDeployed = false;
65 if (manager.getPMs().size() == 0) {
66 PhysicalMachine pm = manager.startPhysicalMachine();
67 boolean enoughResources = pm.checkVM(app.getSize(), 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() + " started on new pm " + pm.getId());
77 log.warn("Application " + app.toString() + " cannot be run on empty pm "
81 // sorting physical machines by resource utilization
83 List<PhysicalMachine> sortedPMs = manager.getSortedPMs();
84 Collections.reverse(sortedPMs);
86 for (PhysicalMachine pm : sortedPMs) {
88 boolean enoughResources = pm.checkVM(app.getSize(), app.getRam(), app.getCpu());
90 if (enoughResources) {
91 VirtualMachine vm = pm.startVirtualMachine(app.getSize(), app.getRam(),
92 app.getCpu(), vmType);
93 vm.startApplication(app);
94 insertStopEvent(currTime + app.getDuration(), app);
96 log.info("Application " + app.toString() + " started new vm "
97 + vm.getPositionOnPM() + " on pm " + pm.getId());
101 if (!appDeployed && (manager.getPMs().size() < manager.getMaxPMs())) {
103 PhysicalMachine pm = manager.startPhysicalMachine();
104 boolean enoughResources = pm.checkVM(app.getSize(), app.getRam(), app.getCpu());
106 if (enoughResources) {
107 VirtualMachine vm = pm.startVirtualMachine(app.getSize(), app.getRam(),
108 app.getCpu(), vmType);
109 vm.startApplication(app);
110 insertStopEvent(currTime + app.getDuration(), app);
112 log.info("Application " + app.toString() + " started on new pm "
115 log.warn("Application " + app.toString() + " cannot be run on empty pm "
121 if (federation.askToOutsource(app)) {
122 insertOutsourcedStartEvent(currTime + 1, app);
125 delayedApps.add(app);
128 log.warn("Application " + app.toString() + " could not be deployed on any pm");
133 // sorting applications by amount of resources (descending)
134 private List<Application> sortApps(LinkedList<SchedulerEvent> events) {
135 List<Application> sortedApps = new LinkedList<Application>();
136 for (SchedulerEvent evt : events) {
137 sortedApps.add(evt.getApp());
139 Collections.sort(sortedApps, new ApplicationResourceComparator());
140 Collections.reverse(sortedApps);
145 protected String getSchedulerType() {
146 return SchedulerType.A.toString();