]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/sched/SchedulerB.java
added bin/ to .gitignore
[pub/jan/lsdc.git] / src / at / ac / tuwien / lsdc / sched / SchedulerB.java
1 package at.ac.tuwien.lsdc.sched;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.LinkedList;
6
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9
10 import at.ac.tuwien.lsdc.exception.OutOfPMsException;
11 import at.ac.tuwien.lsdc.exception.VMResizeException;
12 import at.ac.tuwien.lsdc.types.PhysicalMachine;
13 import at.ac.tuwien.lsdc.types.ScenarioType;
14 import at.ac.tuwien.lsdc.types.SchedulerEvent;
15 import at.ac.tuwien.lsdc.types.SchedulerEvent.EventType;
16 import at.ac.tuwien.lsdc.types.SchedulerType;
17 import at.ac.tuwien.lsdc.types.VirtualMachine;
18 import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
19
20 /**
21  * Scheduler B.
22  * 
23  * Initial State: All PMs switched off. If an application arrives, try to modify
24  * size, CPU and RAM of an existing VM to run the application. If no VM is
25  * running, create a new one (start a new PM if necessary). If the application
26  * has finished decrease the size, CPU and RAM of the VM. If no applications are
27  * running on a VM, shut down the VM. If no VM is running on a PM, shut down the
28  * PM. Try to get a maximum of utilization on every PM. Migration: Try to move
29  * applications from VMs to other VMs to get a better utilization and to use
30  * less PMs.
31  * 
32  * @author jan
33  * 
34  */
35 public class SchedulerB extends AbstractScheduler {
36
37         private static final Logger log = LoggerFactory.getLogger(SchedulerB.class);
38
39         public SchedulerB(int numPMs, int numCloudPartners, File schedulerLog, ScenarioType scenario) throws IOException {
40                 super(numPMs, numCloudPartners, schedulerLog, scenario);
41
42                 vmType = VMType.Resizable;
43         }
44
45         @Override
46         protected void handleEvents(LinkedList<SchedulerEvent> events) {
47                 log.debug("handleEvents():" + events);
48                 for (SchedulerEvent evt : events) {
49                         if (evt.getType() == EventType.endApplication) {
50                                 VirtualMachine vm = evt.getApp().getRunningOn();
51                                 evt.getApp().setRunningOn(null);
52                                 vm.stopApplication(evt.getApp());
53                                 try {
54                                         vm.resizeVM(vm.getSize() - evt.getApp().getSize(), vm.getRAM() - evt.getApp().getRam(), vm.getCPU()
55                                                         - evt.getApp().getCpu());
56                                 } catch (VMResizeException e) {
57                                         log.error("lol: " + e.getVm(), e);
58                                 }
59                                 if (vm.getApplications().size() == 0) {
60                                         PhysicalMachine pm = vm.getRunningOn();
61                                         pm.stopVirtualMachine(vm);
62                                         manager.stopPhysicalMachine(pm.getId());
63                                 }
64                         }
65                 }
66
67                 for (SchedulerEvent evt : events) {
68                         if (evt.getType() == EventType.startApplication) {
69                                 VirtualMachine vm = null;
70                                 for (PhysicalMachine pm : manager.getPMs()) {
71                                         log.debug("PMs: " + pm);
72                                         vm = pm.getVirtualMachines().get((pm.getVirtualMachines().keySet().toArray(new Integer[0]))[0]);
73                                         try {
74                                                 vm.resizeVM(vm.getSize() + evt.getApp().getSize(), vm.getRAM() + evt.getApp().getRam(), vm.getCPU()
75                                                                 + evt.getApp().getCpu());
76                                                 break;
77                                         } catch (VMResizeException ex) {
78                                                 vm = null;
79                                         }
80                                 }
81                                 if (vm == null) {
82                                         try {
83                                                 vm = manager.startPhysicalMachine().startVirtualMachine(evt.getApp().getSize(), evt.getApp().getRam(),
84                                                                 evt.getApp().getCpu(), vmType);
85                                         } catch (OutOfPMsException e) {
86                                                 log.error("OOP!", e);
87                                                 // TODO: delay APP start.
88                                                 return;
89                                         }
90                                 }
91                                 vm.startApplication(evt.getApp());
92                                 evt.getApp().setRunningOn(vm);
93                                 insertStopEvent(currTime + evt.getApp().getDuration(), evt.getApp());
94                         }
95                 }
96         }
97
98         @Override
99         protected String getSchedulerType() {
100                 return SchedulerType.B.toString();
101         }
102
103 }