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;
26 public class SchedulerA extends AbstractScheduler {
28 private static final Logger log = LoggerFactory.getLogger(SchedulerA.class);
30 public SchedulerA(File logFile, SchedulerType schedulerType, ScenarioType scenario, int numPMs, int numCloudPartners) throws IOException {
31 super(logFile, schedulerType, scenario, numPMs, numCloudPartners);
32 this.VMType = VMType.NonResizable;
36 protected void handleEvents(LinkedList<SchedulerEvent> events) {
38 for(SchedulerEvent evt : events) {
39 if(evt.getType() == EventType.endApplication) {
40 VirtualMachine vm = evt.getApp().getRunningOn();
41 vm.stopApplication(evt.getApp());
42 PhysicalMachine pm = vm.getRunningOn();
44 pm.stopVirtualMachine(vm);
45 } catch (ActiveApplicationsException e) {
46 log.warn("VM "+vm.getId()+"could not be stopped, "+e.getMessage());
51 // sorting applications by amount of resources (descending)
52 List<SortedApplication> sortedApps = sortApps(events);
54 for(Iterator<SortedApplication> iter = sortedApps.iterator(); iter.hasNext(); ) {
55 boolean appDeployed = false;
56 Application app = iter.next().getApp();
58 if(machineManager.getPMs().size() == 0) {
59 PhysicalMachine pm = machineManager.startPhysicalMachine();
60 boolean enoughResources = pm.checkVM(app.getSize(), app.getRam(), app.getCpu());
63 pm.startVirtualMachine(app.getSize(), app.getRam(), app.getCpu(), VMType);
65 log.debug("Application "+app.toString()+" started on new pm "+pm.getId());
68 log.warn("Application "+app.toString()+" cannot be run on empty pm "+pm.getId());
72 // sorting physical machines by resource utilization (descending)
73 List<SortedPhysicalMachine> sortedPMs = sortPMs();
75 for(Iterator<SortedPhysicalMachine> it = sortedPMs.iterator(); iter.hasNext(); ) {
76 PhysicalMachine pm = it.next().getPm();
77 boolean enoughResources = pm.checkVM(app.getSize(), app.getRam(), app.getCpu());
80 VirtualMachine vm = pm.startVirtualMachine(app.getSize(), app.getRam(), app.getCpu(), VMType);
81 vm.startApplication(app);
83 log.debug("Application "+app.toString()+" started on new pm "+pm.getId());
87 if(!appDeployed && (machineManager.getPMs().size() < machineManager.getMaxPMs())) {
89 PhysicalMachine pm = machineManager.startPhysicalMachine();
90 boolean enoughResources = pm.checkVM(app.getSize(), app.getRam(), app.getCpu());
93 VirtualMachine vm = pm.startVirtualMachine(app.getSize(), app.getRam(), app.getCpu(), VMType);
94 vm.startApplication(app);
96 log.debug("Application "+app.toString()+" started on new pm "+pm.getId());
99 log.warn("Application "+app.toString()+" cannot be run on empty pm "+pm.getId());
104 log.warn("Application "+app.toString()+" could not be deployed on any pm");
108 // sorting applications by amount of resources (descending)
109 private List<SortedApplication> sortApps(LinkedList<SchedulerEvent> events) {
110 List<SortedApplication> sortedApps = new LinkedList<SortedApplication>();
111 for(SchedulerEvent evt : events) {
112 if(evt.getType() == EventType.startApplication)
113 sortedApps.add(new SortedApplication(evt.getApp()));
115 Collections.sort(sortedApps);
116 Collections.reverse(sortedApps);
120 // sorting physical machines by resource utilization (descending)
121 private List<SortedPhysicalMachine> sortPMs() {
122 List<SortedPhysicalMachine> sortedPMs = new LinkedList<SortedPhysicalMachine>();
123 for(PhysicalMachine pm : machineManager.getPMs())
124 sortedPMs.add(new SortedPhysicalMachine(pm));
126 Collections.sort(sortedPMs);
127 Collections.reverse(sortedPMs);