]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/sched/SchedulerB.java
Restoring the original state (migration)
[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                 VirtualMachine current = app.getRunningOn();
50                 boolean deployed = false;
51                 if (!vm.enoughResources(app)) {
52                         try {
53                                 vm.resizeUp(app);
54                         } catch (VMResizeException ex) {
55                                 return false;
56                         }
57                 }
58                 deployed = vm.startApplication(app);
59                 if (deployed) {
60                         app.setRunningOn(vm);
61                         insertStopEvent(currTime + app.getDuration(), app);
62                         if (current != null) {
63                                 current.stopApplication(app);
64                                 current.resizeDown(app);
65                         }
66                 }
67                 return deployed;
68         }
69
70         /**
71          * Cleanup completed apps. Downsize VMs and if an VM becomes empty, shut down VM + PM.
72          *
73          * @param events list of all events that happened in this timeslot.
74          */
75         @Override
76         protected void handleEndEvents(LinkedList<SchedulerEvent> events) {
77 //              LOG.debug("stopApps():" + events);
78                 for (SchedulerEvent evt : events) {
79                         VirtualMachine vm = evt.getApp().getRunningOn();
80                         vm.stopApplication(evt.getApp());
81                         vm.resizeDown(evt.getApp());
82
83                         numStopped ++;
84                         
85                         if (vm.getApplications().size() == 0) {
86                                 PhysicalMachine pm = vm.getRunningOn();
87                                 pm.stopVirtualMachine(vm);
88                                 manager.stopPhysicalMachine(pm.getId());
89                         }
90                 }
91         }
92
93         /**
94          * Try to start all Apps. Upsize the VM, if not possible start another PM, if not possible,
95          * delay start.
96          *
97          * @param events list of all events that happened in this timeslot.
98          */
99         @Override
100         protected void handleStartEvents(LinkedList<SchedulerEvent> events) {
101 //              LOG.debug("startApps():" + events);
102                 boolean deployed = false;
103                 for (SchedulerEvent evt : events) {
104                         deployed = false;
105                         VirtualMachine vm = null;
106                         List<PhysicalMachine> sortedPMs = manager.getRevSortedPMs();
107                         
108                         for (PhysicalMachine pm : sortedPMs) {
109                                 if (pm.isRunning() && (pm.countCurrentlyRunningVMs() > 0)) {
110                                         vm = pm.getVirtualMachines().values().iterator().next();
111                                         deployed = deployApp(vm, evt.getApp());
112                                         if (deployed)
113                                                 break;
114                                 }
115                         }
116                         if (!deployed) {
117                                 try {
118                                         vm = manager.startPhysicalMachine().startVirtualMachine(evt.getApp().getSize(),
119                                                         evt.getApp().getRam(), evt.getApp().getCpu(), vmType);
120                                         deployed = deployApp(vm, evt.getApp());
121                                 } catch (OutOfPMsException e) {
122                                         if (federation.askToOutsource(evt.getApp())) {
123                                                 insertOutsourcedStartEvent(currTime + 1, evt.getApp());
124                                         } else {
125 //                                              LOG.info("delaying the start of:" + evt.getApp());
126                                                 delayedApps.add(evt.getApp());
127                                                 return;
128                                         }
129                                 }
130                         }
131                         if (deployed) {
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 }