]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/sched/SchedulerC.java
Fixed bug in scheduler A
[pub/jan/lsdc.git] / src / at / ac / tuwien / lsdc / sched / SchedulerC.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.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 public class SchedulerC extends AbstractSchedulerWithMigration {
22
23         private static final Logger LOG = LoggerFactory.getLogger(SchedulerC.class);
24
25         public SchedulerC(int numPMs, int numCloudPartners, File schedulerLog, ScenarioType scenario)
26                         throws IOException {
27                 super(numPMs, numCloudPartners, schedulerLog, scenario);
28
29                 this.vmType = VMType.NonResizable;
30         }
31
32         @Override
33         protected boolean deployApp(VirtualMachine vm, Application app) {
34                 boolean deployed = false;
35                 if (!vm.enoughResources(app))
36                         return false;
37
38                 deployed = vm.startApplication(app);
39                 if (deployed)
40                         app.setRunningOn(vm);
41                 return deployed;
42         }
43
44         /**
45          * Cleanup completed apps. If an VM becomes empty, shut down VM + PM.
46          *
47          * @param events list of all events that happened in this timeslot.
48          */
49         @Override
50         protected void handleEndEvents(LinkedList<SchedulerEvent> events) {
51 //              LOG.debug("stopApps():" + events);
52                 for (SchedulerEvent evt : events) {
53                         VirtualMachine vm = evt.getApp().getRunningOn();
54                         evt.getApp().setRunningOn(null);
55                         vm.stopApplication(evt.getApp());
56
57                         if (vm.getApplications().size() == 0) {
58                                 PhysicalMachine pm = vm.getRunningOn();
59                                 pm.stopVirtualMachine(vm);
60                                 manager.stopPhysicalMachine(pm.getId());
61                         }
62                 }
63         }
64
65
66         /**
67          * Try to start all Apps. If not enough space start another PM, if not possible,
68          * delay start.
69          *
70          * @param events list of all events that happened in this timeslot.
71          */
72         @Override
73         protected void handleStartEvents(LinkedList<SchedulerEvent> events) {
74 //              LOG.debug("startApps():" + events);
75                 boolean deployed = false;
76                 for (SchedulerEvent evt : events) {
77                         deployed = false;
78                         VirtualMachine vm = null;
79                         List<PhysicalMachine> sortedPMs = manager.getRevSortedPMs();
80
81                         for (PhysicalMachine pm : sortedPMs) {
82                                 if (pm.isRunning() && (pm.countCurrentlyRunningVMs() > 0)) {
83                                         vm = pm.getVirtualMachines().values().iterator().next();
84                                         deployed = deployApp(vm, evt.getApp());
85                                         if (deployed)
86                                                 break;
87                                 }
88                         }
89                         if (!deployed) {
90                                 try {
91                                         vm = manager.startPhysicalMachine().startVirtualMachine(vmType);
92                                         deployed = deployApp(vm, evt.getApp());
93                                 } catch (OutOfPMsException e) {
94                                         if (federation.askToOutsource(evt.getApp())) {
95                                                 insertOutsourcedStartEvent(currTime + 1, evt.getApp());
96                                         } else {
97                                                 LOG.info("delaying the start of:" + evt.getApp());
98                                                 delayedApps.add(evt.getApp());
99                                                 return;
100                                         }
101                                 }
102                         }
103                         if (deployed) {
104                                 insertStopEvent(currTime + evt.getApp().getDuration(), evt.getApp());
105
106                                 if(manager.countCurrentlyRunningPMs() < manager.getMaxPMs()) {
107                                         Application app = federation.askToInsource();
108                                         if(app != null) {
109                                                 insertInsourcedStartEvent(currTime + 1, evt.getApp());
110                                         }
111                                 }
112                         }
113                 }
114         }
115
116         @Override
117         protected String getSchedulerType() {
118                 return SchedulerType.C.toString();
119         }
120
121 }