]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/sched/SchedulerB.java
moved DelayedApps List to Abstract scheduler, remove started apps from delayed apps...
[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.Application;
13 import at.ac.tuwien.lsdc.types.PhysicalMachine;
14 import at.ac.tuwien.lsdc.types.ScenarioType;
15 import at.ac.tuwien.lsdc.types.SchedulerEvent;
16 import at.ac.tuwien.lsdc.types.SchedulerEvent.EventType;
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 AbstractScheduler {
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 void handleEvents(LinkedList<SchedulerEvent> events) {
48                 LOG.debug("handleEvents():" + events);
49                 handleEndEvents(events);
50                 handleOutsourcedEndEvents(events);
51                 runMigration();
52                 runDelayedApps();
53                 handleOutsourcedStartEvents(events);
54                 handleStartEvents(events);
55         }
56
57         /**
58          * handle running of outsourced apps.
59          * 
60          * @param events list of all events that happened in this timeslot.
61          */
62         private void handleOutsourcedStartEvents(LinkedList<SchedulerEvent> events) {
63                 for (SchedulerEvent evt : events) {
64                         if (evt.getType() == EventType.startOutsourcedApplication) {
65                                 insertOutsourcedStopEvent(currTime + evt.getApp().getDuration(), evt.getApp());
66                                 numCurrOutSourced++;
67                                 numTotalOutSourced++;
68                         }
69                 }
70         }
71
72         /**
73          * handle stopping of outsourced apps.
74          * 
75          * @param events list of all events that happened in this timeslot.
76          */
77         private void handleOutsourcedEndEvents(LinkedList<SchedulerEvent> events) {
78                 for (SchedulerEvent evt : events) {
79                         if (evt.getType() == EventType.endOutsourcedApplication) {
80                                 numCurrOutSourced--;
81                         }
82                 }
83         }
84
85         /**
86          * Check if we have any delayed apps. Try to launch them.
87          */
88         private void runDelayedApps() {
89                 // TODO: probably mostly the same code as handleStartEvents
90                 // TOOD: (namely: use case: "start an application",do merge it
91                 for (Application app : delayedApps) {
92                         VirtualMachine vm = null;
93                         for (PhysicalMachine pm : manager.getPMs()) {
94                                 // TODO: choose PM better to get better Utilization
95                                 vm = pm.getVirtualMachines().get(
96                                                 (pm.getVirtualMachines().keySet().toArray(new Integer[0]))[0]);
97                                 try {
98                                         vm.resizeVM(vm.getSize() + app.getSize(), vm.getRAM() + app.getRam(),
99                                                         vm.getCPU() + app.getCpu());
100                                         break;
101                                 } catch (VMResizeException ex) {
102                                         vm = null;
103                                 }
104                         }
105                         if (vm == null) {
106                                 try {
107                                         vm = manager.startPhysicalMachine().startVirtualMachine(app.getSize(),
108                                                         app.getRam(), app.getCpu(), vmType);
109                                 } catch (OutOfPMsException e) {
110                                         // LOG.error("failed to start PM.", e);
111                                         if (federation.askToOutsource(app)) {
112                                                 insertOutsourcedStartEvent(currTime + 1, app);
113                                         } else
114                                                 LOG.info("delaying the start of:" + app);
115                                         return;
116                                 }
117                         }
118                         vm.startApplication(app);
119                         app.setRunningOn(vm);
120                         insertStopEvent(currTime + app.getDuration(), app);
121                         delayedApps.remove(app);
122                 }
123         }
124
125         /**
126          * Check if we can free up a VM to shut it down.
127          */
128         private void runMigration() {
129                 // TODO Auto-generated method stub
130         }
131
132         /**
133          * Cleanup completed apps. Downsize VMs and if an VM becomes empty, shut down VM + PM.
134          * 
135          * @param events list of all events that happened in this timeslot.
136          */
137         protected void handleEndEvents(LinkedList<SchedulerEvent> events) {
138                 LOG.debug("stopApps():" + events);
139                 for (SchedulerEvent evt : events) {
140                         if (evt.getType() == EventType.endApplication) {
141                                 VirtualMachine vm = evt.getApp().getRunningOn();
142                                 evt.getApp().setRunningOn(null);
143                                 vm.stopApplication(evt.getApp());
144                                 try {
145                                         vm.resizeVM(vm.getSize() - evt.getApp().getSize(), vm.getRAM()
146                                                         - evt.getApp().getRam(), vm.getCPU() - evt.getApp().getCpu());
147                                 } catch (VMResizeException e) {
148                                         // LOG.error("failed to resize VM: " + e.getVm(), e);
149                                 }
150                                 if (vm.getApplications().size() == 0) {
151                                         PhysicalMachine pm = vm.getRunningOn();
152                                         pm.stopVirtualMachine(vm);
153                                         manager.stopPhysicalMachine(pm.getId());
154                                 }
155                         }
156                 }
157         }
158
159         /**
160          * Try to start all Apps. Upsize the VM, if not possible start another PM, if not possible,
161          * delay start.
162          * 
163          * @param events list of all events that happened in this timeslot.
164          */
165         protected void handleStartEvents(LinkedList<SchedulerEvent> events) {
166                 LOG.debug("startApps():" + events);
167                 for (SchedulerEvent evt : events) {
168                         if (evt.getType() == EventType.startApplication) {
169                                 VirtualMachine vm = null;
170                                 for (PhysicalMachine pm : manager.getPMs()) {
171                                         // TODO: choose VM with good Utilization to get even better utilization
172                                         vm = pm.getVirtualMachines().get(
173                                                         (pm.getVirtualMachines().keySet().toArray(new Integer[0]))[0]);
174                                         try {
175                                                 vm.resizeVM(vm.getSize() + evt.getApp().getSize(), vm.getRAM()
176                                                                 + evt.getApp().getRam(), vm.getCPU() + evt.getApp().getCpu());
177                                                 break;
178                                         } catch (VMResizeException ex) {
179                                                 vm = null;
180                                         }
181                                 }
182                                 if (vm == null) {
183                                         try {
184                                                 vm = manager.startPhysicalMachine().startVirtualMachine(
185                                                                 evt.getApp().getSize(), evt.getApp().getRam(),
186                                                                 evt.getApp().getCpu(), vmType);
187                                         } catch (OutOfPMsException e) {
188                                                 if (federation.askToOutsource(evt.getApp())) {
189                                                         insertOutsourcedStartEvent(currTime + 1, evt.getApp());
190                                                 } else
191                                                         LOG.info("delaying the start of:" + evt.getApp());
192                                                 delayedApps.add(evt.getApp());
193                                                 return;
194                                         }
195                                 }
196                                 vm.startApplication(evt.getApp());
197                                 evt.getApp().setRunningOn(vm);
198                                 insertStopEvent(currTime + evt.getApp().getDuration(), evt.getApp());
199                         }
200                 }
201         }
202
203         @Override
204         protected String getSchedulerType() {
205                 return SchedulerType.B.toString();
206         }
207
208 }