]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/sched/SchedulerB.java
ArrayList type was killing checkstyle. "new ArrayList<>()" does harm.
[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.ArrayList;
6 import java.util.LinkedList;
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.SchedulerEvent.EventType;
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
26  * size, CPU and RAM of an existing VM to run the application. If no VM is
27  * running, create a new one (start a new PM if necessary). If the application
28  * has finished decrease the size, CPU and RAM of the VM. If no applications are
29  * running on a VM, shut down the VM. If no VM is running on a PM, shut down the
30  * PM. Try to get a maximum of utilization on every PM. Migration: Try to move
31  * applications from VMs to other VMs to get a better utilization and to use
32  * less PMs.
33  * 
34  * @author jan
35  * 
36  */
37 public class SchedulerB extends AbstractScheduler {
38         /**
39          * Logger.
40          */
41         private static final Logger LOG = LoggerFactory.getLogger(SchedulerB.class);
42
43         private final ArrayList<Application> delayedApps;
44
45         public SchedulerB(int numPMs, int numCloudPartners, File schedulerLog, ScenarioType scenario) throws IOException {
46                 super(numPMs, numCloudPartners, schedulerLog, scenario);
47
48                 vmType = VMType.Resizable;
49                 delayedApps = new ArrayList<Application>();
50         }
51
52         @Override
53         protected void handleEvents(LinkedList<SchedulerEvent> events) {
54                 LOG.debug("handleEvents():" + events);
55                 handleEndEvents(events);
56                 checkMigration();
57                 checkDelayedApps();
58                 handleStartEvents(events);
59         }
60
61         /**
62          * Check if we have any delayed apps. Try to launch them.
63          */
64         private void checkDelayedApps() {
65                 // TODO Auto-generated method stub
66         }
67
68         /**
69          * Check if we can free up a VM to shut it down.
70          */
71         private void checkMigration() {
72                 // TODO Auto-generated method stub
73         }
74
75         /**
76          * Cleanup completed apps. Downsize VMs and if an VM becomes empty, shut down
77          * VM + PM.
78          * 
79          * @param events
80          *          list of all events that happened in this timeslot.
81          */
82         protected void handleEndEvents(LinkedList<SchedulerEvent> events) {
83                 LOG.debug("stopApps():" + events);
84                 for (SchedulerEvent evt : events) {
85                         if (evt.getType() == EventType.endApplication) {
86                                 VirtualMachine vm = evt.getApp().getRunningOn();
87                                 evt.getApp().setRunningOn(null);
88                                 vm.stopApplication(evt.getApp());
89                                 try {
90                                         vm.resizeVM(vm.getSize() - evt.getApp().getSize(), vm.getRAM() - evt.getApp().getRam(), vm.getCPU()
91                                                         - evt.getApp().getCpu());
92                                 } catch (VMResizeException e) {
93                                         LOG.error("failed to resize VM: " + e.getVm(), e);
94                                 }
95                                 if (vm.getApplications().size() == 0) {
96                                         PhysicalMachine pm = vm.getRunningOn();
97                                         pm.stopVirtualMachine(vm);
98                                         manager.stopPhysicalMachine(pm.getId());
99                                 }
100                         }
101                 }
102         }
103
104         /**
105          * Try to start all Apps. Upsize the VM, if not possible start another PM, if
106          * not possible, delay start.
107          * 
108          * @param events
109          *          list of all events that happened in this timeslot.
110          */
111         protected void handleStartEvents(LinkedList<SchedulerEvent> events) {
112                 LOG.debug("startApps():" + events);
113                 for (SchedulerEvent evt : events) {
114                         if (evt.getType() == EventType.startApplication) {
115                                 VirtualMachine vm = null;
116                                 for (PhysicalMachine pm : manager.getPMs()) {
117                                         LOG.debug("PMs: " + pm);
118                                         vm = pm.getVirtualMachines().get((pm.getVirtualMachines().keySet().toArray(new Integer[0]))[0]);
119                                         try {
120                                                 vm.resizeVM(vm.getSize() + evt.getApp().getSize(), vm.getRAM() + evt.getApp().getRam(), vm.getCPU()
121                                                                 + evt.getApp().getCpu());
122                                                 break;
123                                         } catch (VMResizeException ex) {
124                                                 vm = null;
125                                         }
126                                 }
127                                 if (vm == null) {
128                                         try {
129                                                 vm = manager.startPhysicalMachine().startVirtualMachine(evt.getApp().getSize(), evt.getApp().getRam(),
130                                                                 evt.getApp().getCpu(), vmType);
131                                         } catch (OutOfPMsException e) {
132                                                 LOG.error("failed to start PM.", e);
133                                                 // TODO: try outsourcing here.
134                                                 delayedApps.add(evt.getApp());
135                                                 return;
136                                         }
137                                 }
138                                 vm.startApplication(evt.getApp());
139                                 evt.getApp().setRunningOn(vm);
140                                 insertStopEvent(currTime + evt.getApp().getDuration(), evt.getApp());
141                         }
142                 }
143         }
144
145         @Override
146         protected String getSchedulerType() {
147                 return SchedulerType.B.toString();
148         }
149
150 }