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