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