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(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, "+e.getMessage());
54 // sorting applications by amount of resources (descending)
55 List<SortedApplication> sortedApps = sortApps(events);
57 for(Iterator<SortedApplication> iter = sortedApps.iterator(); iter.hasNext(); ) {
58 boolean appDeployed = false;
59 Application app = iter.next().getApp();
61 if(manager.getPMs().size() == 0) {
62 PhysicalMachine pm = manager.startPhysicalMachine();
63 boolean enoughResources = pm.checkVM(app.getSize(), app.getRam(), app.getCpu());
66 pm.startVirtualMachine(app.getSize(), app.getRam(), app.getCpu(), VMType);
68 log.debug("Application "+app.toString()+" started on new pm "+pm.getId());
71 log.warn("Application "+app.toString()+" cannot be run on empty pm "+pm.getId());
75 // sorting physical machines by resource utilization (descending)
76 List<SortedPhysicalMachine> sortedPMs = sortPMs();
78 for(Iterator<SortedPhysicalMachine> it = sortedPMs.iterator(); iter.hasNext(); ) {
79 PhysicalMachine pm = it.next().getPm();
80 boolean enoughResources = pm.checkVM(app.getSize(), app.getRam(), app.getCpu());
83 VirtualMachine vm = pm.startVirtualMachine(app.getSize(), app.getRam(), app.getCpu(), VMType);
84 vm.startApplication(app);
86 log.debug("Application "+app.toString()+" started on new pm "+pm.getId());
90 if(!appDeployed && (manager.getPMs().size() < manager.getMaxPMs())) {
92 PhysicalMachine pm = manager.startPhysicalMachine();
93 boolean enoughResources = pm.checkVM(app.getSize(), app.getRam(), app.getCpu());
96 VirtualMachine vm = pm.startVirtualMachine(app.getSize(), app.getRam(), app.getCpu(), VMType);
97 vm.startApplication(app);
99 log.debug("Application "+app.toString()+" started on new pm "+pm.getId());
102 log.warn("Application "+app.toString()+" cannot be run on empty pm "+pm.getId());
107 log.warn("Application "+app.toString()+" could not be deployed on any pm");
111 // sorting applications by amount of resources (descending)
112 private List<SortedApplication> sortApps(LinkedList<SchedulerEvent> events) {
113 List<SortedApplication> sortedApps = new LinkedList<SortedApplication>();
114 for(SchedulerEvent evt : events) {
115 if(evt.getType() == EventType.startApplication)
116 sortedApps.add(new SortedApplication(evt.getApp()));
118 Collections.sort(sortedApps);
119 Collections.reverse(sortedApps);
123 // sorting physical machines by resource utilization (descending)
124 private List<SortedPhysicalMachine> sortPMs() {
125 List<SortedPhysicalMachine> sortedPMs = new LinkedList<SortedPhysicalMachine>();
126 for(PhysicalMachine pm : manager.getPMs())
127 sortedPMs.add(new SortedPhysicalMachine(pm));
129 Collections.sort(sortedPMs);
130 Collections.reverse(sortedPMs);