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