]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/sched/SchedulerB.java
moved sorting of PMs into MachineManager
[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.SchedulerType;
16 import at.ac.tuwien.lsdc.types.VirtualMachine;
17 import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
18
19 /**
20  * Scheduler B.
21  *
22  * Initial State: All PMs switched off. If an application arrives, try to modify size, CPU and RAM
23  * of an existing VM to run the application. If no VM is running, create a new one (start a new PM
24  * if necessary). If the application has finished decrease the size, CPU and RAM of the VM. If no
25  * applications are running on a VM, shut down the VM. If no VM is running on a PM, shut down the
26  * PM. Try to get a maximum of utilization on every PM. Migration: Try to move applications from VMs
27  * to other VMs to get a better utilization and to use less PMs.
28  *
29  * @author jan
30  *
31  */
32 public class SchedulerB extends AbstractScheduler {
33         /**
34          * Logger.
35          */
36         private static final Logger LOG = LoggerFactory.getLogger(SchedulerB.class);
37
38         public SchedulerB(int numPMs, int numCloudPartners, File schedulerLog, ScenarioType scenario)
39                         throws IOException {
40                 super(numPMs, numCloudPartners, schedulerLog, scenario);
41                 vmType = VMType.Resizable;
42         }
43
44         /**
45          * Check if we can free up a VM to shut it down.
46          */
47         @Override
48         protected void runMigration() {
49                 // TODO Auto-generated method stub
50         }
51
52         /**
53          * Cleanup completed apps. Downsize VMs and if an VM becomes empty, shut down VM + PM.
54          *
55          * @param events list of all events that happened in this timeslot.
56          */
57         @Override
58         protected void handleEndEvents(LinkedList<SchedulerEvent> events) {
59                 LOG.debug("stopApps():" + events);
60                 for (SchedulerEvent evt : events) {
61                         VirtualMachine vm = evt.getApp().getRunningOn();
62                         evt.getApp().setRunningOn(null);
63                         vm.stopApplication(evt.getApp());
64                         try {
65                                 vm.resizeVM(vm.getSize() - evt.getApp().getSize(), vm.getRAM()
66                                                 - evt.getApp().getRam(), vm.getCPU() - evt.getApp().getCpu());
67                         } catch (VMResizeException e) {
68                                 // LOG.error("failed to resize VM: " + e.getVm(), e);
69                         }
70                         if (vm.getApplications().size() == 0) {
71                                 PhysicalMachine pm = vm.getRunningOn();
72                                 pm.stopVirtualMachine(vm);
73                                 manager.stopPhysicalMachine(pm.getId());
74                         }
75                 }
76         }
77
78         /**
79          * Try to start all Apps. Upsize the VM, if not possible start another PM, if not possible,
80          * delay start.
81          *
82          * @param events list of all events that happened in this timeslot.
83          */
84         @Override
85         protected void handleStartEvents(LinkedList<SchedulerEvent> events) {
86                 LOG.debug("startApps():" + events);
87                 for (SchedulerEvent evt : events) {
88                         VirtualMachine vm = null;
89                         for (PhysicalMachine pm : manager.getPMs()) {
90                                 // TODO: choose VM with good Utilization to get even better utilization
91                                 vm = pm.getVirtualMachines().get(
92                                                 (pm.getVirtualMachines().keySet().toArray(new Integer[0]))[0]);
93                                 try {
94                                         vm.resizeVM(vm.getSize() + evt.getApp().getSize(), vm.getRAM()
95                                                         + evt.getApp().getRam(), vm.getCPU() + evt.getApp().getCpu());
96                                         break;
97                                 } catch (VMResizeException ex) {
98                                         vm = null;
99                                 }
100                         }
101                         if (vm == null) {
102                                 try {
103                                         vm = manager.startPhysicalMachine().startVirtualMachine(evt.getApp().getSize(),
104                                                         evt.getApp().getRam(), evt.getApp().getCpu(), vmType);
105                                 } catch (OutOfPMsException e) {
106                                         if (federation.askToOutsource(evt.getApp())) {
107                                                 insertOutsourcedStartEvent(currTime + 1, evt.getApp());
108                                         } else
109                                                 LOG.info("delaying the start of:" + evt.getApp());
110                                         delayedApps.add(evt.getApp());
111                                         return;
112                                 }
113                         }
114                         vm.startApplication(evt.getApp());
115                         evt.getApp().setRunningOn(vm);
116                         insertStopEvent(currTime + evt.getApp().getDuration(), evt.getApp());
117                 }
118         }
119
120         @Override
121         protected String getSchedulerType() {
122                 return SchedulerType.B.toString();
123         }
124
125 }