]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/sched/SchedulerA.java
Adding debug count for started / stopped
[pub/jan/lsdc.git] / src / at / ac / tuwien / lsdc / sched / SchedulerA.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.ActiveApplicationsException;
13 import at.ac.tuwien.lsdc.exception.VMsRunningException;
14 import at.ac.tuwien.lsdc.types.Application;
15 import at.ac.tuwien.lsdc.types.ApplicationResourceComparator;
16 import at.ac.tuwien.lsdc.types.PhysicalMachine;
17 import at.ac.tuwien.lsdc.types.ScenarioType;
18 import at.ac.tuwien.lsdc.types.SchedulerEvent;
19 import at.ac.tuwien.lsdc.types.SchedulerType;
20 import at.ac.tuwien.lsdc.types.VirtualMachine;
21 import at.ac.tuwien.lsdc.types.VirtualMachine.VMType;
22
23 public class SchedulerA extends AbstractScheduler {
24
25         private static final Logger log = LoggerFactory.getLogger(SchedulerA.class);
26
27         public SchedulerA(int numPMs, int numCloudPartners, File schedulerLog, ScenarioType scenario)
28                         throws IOException {
29                 super(numPMs, numCloudPartners, schedulerLog, scenario);
30
31                 this.vmType = VMType.NonResizable;
32         }
33
34         @Override
35         protected void handleEndEvents(LinkedList<SchedulerEvent> events) {
36                 for (SchedulerEvent evt : events) {
37                         VirtualMachine vm = evt.getApp().getRunningOn();
38                         vm.stopApplication(evt.getApp());
39                         PhysicalMachine pm = vm.getRunningOn();
40                         try {
41                                 pm.stopVirtualMachine(vm);
42                                 if (pm.countCurrentlyRunningVMs() == 0) {
43                                         try {
44                                                 manager.stopPhysicalMachine(pm.getId());
45                                         } catch (VMsRunningException e) {
46                                                 log.warn("PM " + pm.getId() + " could not be stopped, " + e.getMessage());
47                                         }
48                                 }
49                                 numStopped ++;
50 //                              log.info("application stopped at timestamp " + currTime + ", " + "vm "
51 //                                              + vm.getPositionOnPM() + ", pm " + pm.getId());
52                         } catch (ActiveApplicationsException e) {
53                                 log.warn("VM " + vm.getId() + "could not be stopped, " + e.getMessage());
54                         }
55                 }
56         }
57
58         @Override
59         protected void handleStartEvents(LinkedList<SchedulerEvent> events) {
60                 // sorting applications by amount of resources (descending)
61 //              List<Application> sortedApps = sortApps(events);
62                 List<Application> apps = getApplications(events);
63                 
64                 for (Application app : apps) {
65                         boolean appDeployed = false;
66
67                         if (manager.getPMs().size() == 0) {
68                                 PhysicalMachine pm = manager.startPhysicalMachine();
69
70                                 if (pm.checkVM(app.getSize(), app.getRam(), app.getCpu())) 
71                                         appDeployed = startApp(pm, app);
72                                 
73                         } else {
74                                 // sorting physical machines by resource utilization (descending)
75                                 List<PhysicalMachine> revSortedPMs = manager.getRevSortedPMs();
76
77                                 for (PhysicalMachine pm : revSortedPMs) {
78                                         if (pm.checkVM(app.getSize(), app.getRam(), app.getCpu())) {
79                                                 appDeployed = startApp(pm, app);
80                                                 break;
81                                         }
82                                 }
83                                 if (!appDeployed && (manager.getPMs().size() < manager.getMaxPMs())) {
84                                         PhysicalMachine pm = manager.startPhysicalMachine();
85                                         if (pm.checkVM(app.getSize(), app.getRam(), app.getCpu()))
86                                                 appDeployed = startApp(pm, app);
87                                 }
88                         }
89                         if (!appDeployed) {
90                                 if (federation.askToOutsource(app)) {
91                                         insertOutsourcedStartEvent(currTime + 1, app);
92                                         appDeployed = true;
93                                 } else
94                                         delayedApps.add(app);
95                         }
96                         if(appDeployed) {
97                                 numStarted ++;
98                                 if(manager.countCurrentlyRunningPMs() < manager.getMaxPMs()) {
99                                         Application application = federation.askToInsource();
100                                         if(application != null) {
101                                                 insertInsourcedStartEvent(currTime + 1, application);
102                                         }
103                                 }
104                         }
105                 }
106         }
107         
108         private boolean startApp(PhysicalMachine pm, Application app) {
109                 VirtualMachine vm = pm.startVirtualMachine(app.getSize(), app.getRam(),
110                                 app.getCpu(), vmType);
111                 vm.startApplication(app);
112                 insertStopEvent(currTime + app.getDuration(), app);
113                 return true;
114         }
115
116         // sorting applications by amount of resources (descending)
117         private List<Application> sortApps(LinkedList<SchedulerEvent> events) {
118                 List<Application> sortedApps = new LinkedList<Application>();
119                 for (SchedulerEvent evt : events) {
120                         sortedApps.add(evt.getApp());
121                 }
122                 Collections.sort(sortedApps, new ApplicationResourceComparator());
123                 Collections.reverse(sortedApps);
124                 return sortedApps;
125         }
126         
127         public List<Application> getApplications(LinkedList<SchedulerEvent> events) {
128                 List<Application> apps = new LinkedList<Application>();
129                 for(SchedulerEvent evt: events) {
130                         apps.add(evt.getApp());
131                 }
132                 return apps;
133         }
134
135         @Override
136         protected String getSchedulerType() {
137                 return SchedulerType.A.toString();
138         }
139
140 }