]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/sched/SchedulerA.java
added one todo,small style changes
[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
6 import java.util.Iterator;
7 import java.util.LinkedList;
8 import java.util.List;
9 import java.util.Collections;
10
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
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 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, "
50                                                         + 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
59                                 .hasNext();) {
60                         boolean appDeployed = false;
61                         Application app = iter.next().getApp();
62
63                         if (manager.getPMs().size() == 0) {
64                                 PhysicalMachine pm = manager.startPhysicalMachine();
65                                 boolean enoughResources = pm.checkVM(app.getSize(),
66                                                 app.getRam(), app.getCpu());
67
68                                 if (enoughResources) {
69                                         pm.startVirtualMachine(app.getSize(), app.getRam(),
70                                                         app.getCpu(), vmType);
71                                         insertStopEvent(currTime + app.getDuration(), app);
72                                         appDeployed = true;
73                                         log.debug("Application " + app.toString()
74                                                         + " started on new pm " + pm.getId());
75                                 } else {
76                                         log.warn("Application " + app.toString()
77                                                         + " cannot be run on empty pm " + pm.getId());
78                                 }
79                         } else {
80                                 // sorting physical machines by resource utilization
81                                 // (descending)
82                                 List<SortedPhysicalMachine> sortedPMs = sortPMs();
83
84                                 for (Iterator<SortedPhysicalMachine> it = sortedPMs.iterator(); iter
85                                                 .hasNext();) {
86                                         PhysicalMachine pm = it.next().getPm();
87                                         boolean enoughResources = pm.checkVM(app.getSize(),
88                                                         app.getRam(), app.getCpu());
89
90                                         if (enoughResources) {
91                                                 VirtualMachine vm = pm.startVirtualMachine(
92                                                                 app.getSize(), app.getRam(), app.getCpu(),
93                                                                 vmType);
94                                                 vm.startApplication(app);
95                                                 insertStopEvent(currTime + app.getDuration(), app);
96                                                 appDeployed = true;
97                                                 log.debug("Application " + app.toString()
98                                                                 + " started on new pm " + pm.getId());
99                                                 break;
100                                         }
101                                 }
102                                 if (!appDeployed
103                                                 && (manager.getPMs().size() < manager.getMaxPMs())) {
104
105                                         PhysicalMachine pm = manager.startPhysicalMachine();
106                                         boolean enoughResources = pm.checkVM(app.getSize(),
107                                                         app.getRam(), app.getCpu());
108
109                                         if (enoughResources) {
110                                                 VirtualMachine vm = pm.startVirtualMachine(
111                                                                 app.getSize(), app.getRam(), app.getCpu(),
112                                                                 vmType);
113                                                 vm.startApplication(app);
114                                                 insertStopEvent(currTime + app.getDuration(), app);
115                                                 appDeployed = true;
116                                                 log.debug("Application " + app.toString()
117                                                                 + " started on new pm " + pm.getId());
118                                         } else {
119                                                 log.warn("Application " + app.toString()
120                                                                 + " cannot be run on empty pm " + pm.getId());
121                                         }
122                                 }
123                         }
124                         if (!appDeployed)
125                                 log.warn("Application " + app.toString()
126                                                 + " could not be deployed on any pm");
127                         //TODO: save app in a List and try to deploy with next handled events
128                 }
129         }
130
131         // sorting applications by amount of resources (descending)
132         private List<SortedApplication> sortApps(LinkedList<SchedulerEvent> events) {
133                 List<SortedApplication> sortedApps = new LinkedList<SortedApplication>();
134                 for (SchedulerEvent evt : events) {
135                         if (evt.getType() == EventType.startApplication)
136                                 sortedApps.add(new SortedApplication(evt.getApp()));
137                 }
138                 Collections.sort(sortedApps);
139                 Collections.reverse(sortedApps);
140                 return sortedApps;
141         }
142
143         // sorting physical machines by resource utilization (descending)
144         private List<SortedPhysicalMachine> sortPMs() {
145                 List<SortedPhysicalMachine> sortedPMs = new LinkedList<SortedPhysicalMachine>();
146                 for (PhysicalMachine pm : manager.getPMs())
147                         sortedPMs.add(new SortedPhysicalMachine(pm));
148
149                 Collections.sort(sortedPMs);
150                 Collections.reverse(sortedPMs);
151                 return sortedPMs;
152         }
153
154 }