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