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