]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/sched/SchedulerB.java
Merge branch 'master' of ssh://dev.somenet.org:666/tu/lsdc
[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.Collections;
6 import java.util.LinkedList;
7 import java.util.List;
8
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import at.ac.tuwien.lsdc.exception.OutOfPMsException;
13 import at.ac.tuwien.lsdc.exception.VMResizeException;
14 import at.ac.tuwien.lsdc.types.Application;
15 import at.ac.tuwien.lsdc.types.PhysicalMachine;
16 import at.ac.tuwien.lsdc.types.ScenarioType;
17 import at.ac.tuwien.lsdc.types.SchedulerEvent;
18 import at.ac.tuwien.lsdc.types.SchedulerType;
19 import at.ac.tuwien.lsdc.types.VirtualMachine;
20 import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
21
22 /**
23  * Scheduler B.
24  *
25  * Initial State: All PMs switched off. If an application arrives, try to modify size, CPU and RAM
26  * of an existing VM to run the application. If no VM is running, create a new one (start a new PM
27  * if necessary). If the application has finished decrease the size, CPU and RAM of the VM. If no
28  * applications are running on a VM, shut down the VM. If no VM is running on a PM, shut down the
29  * PM. Try to get a maximum of utilization on every PM. Migration: Try to move applications from VMs
30  * to other VMs to get a better utilization and to use less PMs.
31  *
32  * @author jan
33  *
34  */
35 public class SchedulerB extends AbstractSchedulerWithMigration {
36         /**
37          * Logger.
38          */
39         private static final Logger LOG = LoggerFactory.getLogger(SchedulerB.class);
40
41         public SchedulerB(int numPMs, int numCloudPartners, File schedulerLog, ScenarioType scenario)
42                         throws IOException {
43                 super(numPMs, numCloudPartners, schedulerLog, scenario);
44                 vmType = VMType.Resizable;
45         }
46
47         @Override
48         protected boolean deployApp(VirtualMachine vm, Application app) {
49                 boolean deployed = false;
50                 if (!vm.enoughResources(app)) {
51                         try {
52 //                              vm.resizeVM(vm.getSize() + app.getSize(), vm.getRAM() + app.getRam(), vm.getCPU()
53 //                                              + app.getCpu());
54                                 vm.resizeUp(app);
55                         } catch (VMResizeException ex) {
56                                 return false;
57                         }
58                 }
59                 deployed = vm.startApplication(app);
60                 if (deployed)
61                         app.setRunningOn(vm);
62                 return deployed;
63         }
64
65         /**
66          * Cleanup completed apps. Downsize VMs and if an VM becomes empty, shut down VM + PM.
67          *
68          * @param events list of all events that happened in this timeslot.
69          */
70         @Override
71         protected void handleEndEvents(LinkedList<SchedulerEvent> events) {
72                 LOG.debug("stopApps():" + events);
73                 for (SchedulerEvent evt : events) {
74                         VirtualMachine vm = evt.getApp().getRunningOn();
75                         evt.getApp().setRunningOn(null);
76                         vm.stopApplication(evt.getApp());
77                         
78 //                              vm.resizeVM(vm.getSize() - evt.getApp().getSize(), vm.getRAM()
79 //                                              - evt.getApp().getRam(), vm.getCPU() - evt.getApp().getCpu());
80                         
81                         vm.resizeDown(evt.getApp());
82         
83                         if (vm.getApplications().size() == 0) {
84                                 PhysicalMachine pm = vm.getRunningOn();
85                                 pm.stopVirtualMachine(vm);
86                                 manager.stopPhysicalMachine(pm.getId());
87                         }
88                 }
89         }
90
91         /**
92          * Try to start all Apps. Upsize the VM, if not possible start another PM, if not possible,
93          * delay start.
94          *
95          * @param events list of all events that happened in this timeslot.
96          */
97         @Override
98         protected void handleStartEvents(LinkedList<SchedulerEvent> events) {
99                 LOG.debug("startApps():" + events);
100                 boolean deployed = false;
101                 for (SchedulerEvent evt : events) {
102                         deployed = false;
103                         VirtualMachine vm = null;
104                         List<PhysicalMachine> sortedPMs = manager.getSortedPMs();
105                         Collections.reverse(sortedPMs);
106                         for (PhysicalMachine pm : sortedPMs) {
107                                 if (pm.isRunning() && (pm.getTotalVMs() > 0)) {
108                                         vm = pm.getVirtualMachines().values().iterator().next();
109                                         deployed = deployApp(vm, evt.getApp());
110                                         if (deployed)
111                                                 break;
112                                 }
113                         }
114                         if (!deployed) {
115                                 try {
116                                         vm = manager.startPhysicalMachine().startVirtualMachine(evt.getApp().getSize(),
117                                                         evt.getApp().getRam(), evt.getApp().getCpu(), vmType);
118                                         deployed = deployApp(vm, evt.getApp());
119                                 } catch (OutOfPMsException e) {
120                                         if (federation.askToOutsource(evt.getApp())) {
121                                                 insertOutsourcedStartEvent(currTime + 1, evt.getApp());
122                                         } else {
123                                                 LOG.info("delaying the start of:" + evt.getApp());
124                                                 delayedApps.add(evt.getApp());
125                                                 return;
126                                         }
127                                 }
128                         }
129                         if (deployed)
130                                 insertStopEvent(currTime + evt.getApp().getDuration(), evt.getApp());
131                 }
132         }
133
134         @Override
135         protected String getSchedulerType() {
136                 return SchedulerType.B.toString();
137         }
138
139 }