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