]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/sched/SchedulerA.java
Implemented Scheduler A and utility classes
[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.Iterator;
6 import java.util.LinkedList;
7 import java.util.List;
8
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import edu.emory.mathcs.backport.java.util.Collections;
13
14 import at.ac.tuwien.lsdc.exception.ActiveApplicationsException;
15 import at.ac.tuwien.lsdc.types.Application;
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.SchedulerEvent.EventType;
20 import at.ac.tuwien.lsdc.types.SchedulerType;
21 import at.ac.tuwien.lsdc.types.SortedApplication;
22 import at.ac.tuwien.lsdc.types.SortedPhysicalMachine;
23 import at.ac.tuwien.lsdc.types.VirtualMachine;
24
25
26 public class SchedulerA extends AbstractScheduler {
27
28         private static final Logger log = LoggerFactory.getLogger(SchedulerA.class);
29         
30         public SchedulerA(File logFile, SchedulerType schedulerType, ScenarioType scenario, int numPMs, int numCloudPartners) throws IOException {
31                 super(logFile, schedulerType, scenario, numPMs, numCloudPartners);
32                 this.VMType = VMType.NonResizable;
33         }
34
35         @Override
36         protected void handleEvents(LinkedList<SchedulerEvent> events) {
37                 
38                 for(SchedulerEvent evt : events) {
39                         if(evt.getType() == EventType.endApplication) {
40                                 VirtualMachine vm = evt.getApp().getRunningOn();
41                                 vm.stopApplication(evt.getApp());
42                                 PhysicalMachine pm = vm.getRunningOn();
43                                 try {
44                                         pm.stopVirtualMachine(vm);
45                                 } catch (ActiveApplicationsException e) {
46                                         log.warn("VM "+vm.getId()+"could not be stopped, "+e.getMessage());
47                                 }
48                         }
49                 }
50                 
51                 // sorting applications by amount of resources (descending)
52                 List<SortedApplication> sortedApps = sortApps(events);
53                                 
54                 for(Iterator<SortedApplication> iter = sortedApps.iterator(); iter.hasNext(); ) {
55                         boolean appDeployed = false;
56                         Application app = iter.next().getApp();
57                         
58                         if(machineManager.getPMs().size() == 0) {
59                                 PhysicalMachine pm = machineManager.startPhysicalMachine();
60                                 boolean enoughResources = pm.checkVM(app.getSize(), app.getRam(), app.getCpu());
61                                 
62                                 if(enoughResources) {
63                                         pm.startVirtualMachine(app.getSize(), app.getRam(), app.getCpu(), VMType);
64                                         appDeployed = true;
65                                         log.debug("Application "+app.toString()+" started on new pm "+pm.getId());
66                                 }
67                                 else {
68                                         log.warn("Application "+app.toString()+" cannot be run on empty pm "+pm.getId());
69                                 }
70                         }
71                         else {
72                                 // sorting physical machines by resource utilization (descending)
73                                 List<SortedPhysicalMachine> sortedPMs = sortPMs();
74                                 
75                                 for(Iterator<SortedPhysicalMachine> it = sortedPMs.iterator(); iter.hasNext(); ) {
76                                         PhysicalMachine pm = it.next().getPm();
77                                         boolean enoughResources = pm.checkVM(app.getSize(), app.getRam(), app.getCpu());
78                                         
79                                         if(enoughResources) {
80                                                 VirtualMachine vm = pm.startVirtualMachine(app.getSize(), app.getRam(), app.getCpu(), VMType);
81                                                 vm.startApplication(app);
82                                                 appDeployed = true;
83                                                 log.debug("Application "+app.toString()+" started on new pm "+pm.getId());
84                                                 break;
85                                         }
86                                 }
87                                 if(!appDeployed  &&  (machineManager.getPMs().size() < machineManager.getMaxPMs())) {
88                                         
89                                         PhysicalMachine pm = machineManager.startPhysicalMachine();
90                                         boolean enoughResources = pm.checkVM(app.getSize(), app.getRam(), app.getCpu());
91                                         
92                                         if(enoughResources) {
93                                                 VirtualMachine vm = pm.startVirtualMachine(app.getSize(), app.getRam(), app.getCpu(), VMType);
94                                                 vm.startApplication(app);
95                                                 appDeployed = true;
96                                                 log.debug("Application "+app.toString()+" started on new pm "+pm.getId());
97                                         }
98                                         else {
99                                                 log.warn("Application "+app.toString()+" cannot be run on empty pm "+pm.getId());
100                                         }
101                                 }
102                         }
103                         if(!appDeployed) 
104                                 log.warn("Application "+app.toString()+" could not be deployed on any pm");
105                 }
106         }
107         
108         // sorting applications by amount of resources (descending)
109         private List<SortedApplication> sortApps(LinkedList<SchedulerEvent> events) {
110                 List<SortedApplication> sortedApps = new LinkedList<SortedApplication>();
111                 for(SchedulerEvent evt : events) {
112                         if(evt.getType() == EventType.startApplication)
113                                 sortedApps.add(new SortedApplication(evt.getApp()));
114                 }
115                 Collections.sort(sortedApps);
116                 Collections.reverse(sortedApps);
117                 return sortedApps;
118         }
119         
120         // sorting physical machines by resource utilization (descending)
121         private List<SortedPhysicalMachine> sortPMs() {
122                 List<SortedPhysicalMachine> sortedPMs = new LinkedList<SortedPhysicalMachine>();
123                 for(PhysicalMachine pm  : machineManager.getPMs())
124                         sortedPMs.add(new SortedPhysicalMachine(pm));
125                 
126                 Collections.sort(sortedPMs);
127                 Collections.reverse(sortedPMs);
128                 return sortedPMs;
129         }
130
131 }