1 | package schedframe.scheduling; |
---|
2 | |
---|
3 | import eduni.simjava.Sim_event; |
---|
4 | import eduni.simjava.Sim_port; |
---|
5 | import eduni.simjava.Sim_system; |
---|
6 | import gridsim.GridSim; |
---|
7 | import gridsim.GridSimCore; |
---|
8 | import gridsim.GridSimTags; |
---|
9 | import gridsim.IO_data; |
---|
10 | import gridsim.dcworms.DCWormsTags; |
---|
11 | |
---|
12 | import java.util.ArrayList; |
---|
13 | import java.util.List; |
---|
14 | import java.util.Map; |
---|
15 | import java.util.Properties; |
---|
16 | |
---|
17 | import schedframe.Initializable; |
---|
18 | import schedframe.PluginConfiguration; |
---|
19 | import schedframe.events.scheduling.SchedulingEventType; |
---|
20 | import schedframe.resources.Resource; |
---|
21 | import schedframe.resources.ResourceStatus; |
---|
22 | import schedframe.resources.ResourceType; |
---|
23 | import schedframe.resources.computing.ComputingResource; |
---|
24 | import schedframe.resources.providers.LocalSystem; |
---|
25 | import schedframe.resources.units.ResourceUnit; |
---|
26 | import schedframe.resources.units.ResourceUnitName; |
---|
27 | import schedframe.scheduling.manager.resources.ManagedComputingResources; |
---|
28 | import schedframe.scheduling.manager.resources.ManagedResources; |
---|
29 | import schedframe.scheduling.policy.AbstractManagementSystem; |
---|
30 | import schedframe.scheduling.queue.QueueDescription; |
---|
31 | import schedframe.scheduling.queue.TaskQueue; |
---|
32 | import schedframe.scheduling.tasks.WorkloadUnit; |
---|
33 | |
---|
34 | public class Scheduler extends GridSimCore implements Resource, Initializable{ |
---|
35 | |
---|
36 | protected ResourceStatus status; |
---|
37 | protected ResourceType type; |
---|
38 | |
---|
39 | protected Scheduler parent; |
---|
40 | protected List<Scheduler> children; |
---|
41 | |
---|
42 | protected AbstractManagementSystem managementSystem; |
---|
43 | |
---|
44 | protected ManagedComputingResources compResources; |
---|
45 | |
---|
46 | public Scheduler(AbstractManagementSystem manSys, ResourceType schedType, ManagedResources managedResources) throws Exception { |
---|
47 | super(manSys.getName(), 1); |
---|
48 | this.managementSystem = manSys; |
---|
49 | this.compResources = new ManagedComputingResources(); |
---|
50 | this.status = ResourceStatus.AVAILABLE; |
---|
51 | this.children = new ArrayList<Scheduler>(1); |
---|
52 | this.type = schedType; |
---|
53 | init(managedResources); |
---|
54 | } |
---|
55 | |
---|
56 | public void init(ManagedResources managedResources){ |
---|
57 | addResources(managedResources.getComputingResources()); |
---|
58 | managementSystem.init(this, managedResources); |
---|
59 | |
---|
60 | //required to terminate all scheduling entities after the end of simulation |
---|
61 | Integer schedulerIdObj = new Integer(get_id()); |
---|
62 | List<Integer> schedRes = GridSim.getGridResourceList(); |
---|
63 | schedRes.add(schedulerIdObj); |
---|
64 | |
---|
65 | /*if (supportsAR) { |
---|
66 | res = GridSim.getAdvancedReservationList(); |
---|
67 | res.add(resIdObj); |
---|
68 | } */ |
---|
69 | |
---|
70 | } |
---|
71 | |
---|
72 | public void addChild(Scheduler child) { |
---|
73 | child.setParent(this); |
---|
74 | if (children == null) { |
---|
75 | children = new ArrayList<Scheduler>(1); |
---|
76 | } |
---|
77 | children.add(child); |
---|
78 | } |
---|
79 | |
---|
80 | public List<Scheduler> getChildren() { |
---|
81 | return children; |
---|
82 | } |
---|
83 | |
---|
84 | public void setParent(Scheduler newParent) { |
---|
85 | this.parent = newParent; |
---|
86 | } |
---|
87 | |
---|
88 | public Scheduler getParent() { |
---|
89 | return parent; |
---|
90 | } |
---|
91 | |
---|
92 | /*public void addResource(ComputingResource resource) { |
---|
93 | compResources.add(resource); |
---|
94 | }*/ |
---|
95 | |
---|
96 | public void addResources(List<ComputingResource> res) { |
---|
97 | for(ComputingResource resource:res){ |
---|
98 | compResources.add(resource); |
---|
99 | resource.setScheduler(this); |
---|
100 | for(ComputingResource child: resource.filterDescendants(new Properties())){ |
---|
101 | child.setScheduler(this); |
---|
102 | } |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | public ManagedComputingResources getCompResources() { |
---|
107 | return compResources; |
---|
108 | } |
---|
109 | |
---|
110 | public void body() { |
---|
111 | |
---|
112 | PluginConfiguration pluginConfig = managementSystem.getSchedulingPluginConfiguration(); |
---|
113 | if (pluginConfig != null) { |
---|
114 | Map<SchedulingEventType, Object> events = pluginConfig.getServedEvents(); |
---|
115 | if (events != null) { |
---|
116 | Object obj = events.get(SchedulingEventType.TIMER); |
---|
117 | if (obj != null) { |
---|
118 | int delay = (Integer) obj; |
---|
119 | send(this.get_id(), delay, DCWormsTags.TIMER); |
---|
120 | } |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | // Process events until END_OF_SIMULATION is received from the |
---|
125 | // GridSimShutdown Entity |
---|
126 | Sim_event ev = new Sim_event(); |
---|
127 | sim_get_next(ev); |
---|
128 | boolean run = true; |
---|
129 | while (Sim_system.running() && run) { |
---|
130 | // sim_get_next(ev); |
---|
131 | // if the simulation finishes then exit the loop |
---|
132 | if (ev.get_tag() == GridSimTags.END_OF_SIMULATION) { |
---|
133 | // managemetnSystem_.setEndSimulation(); |
---|
134 | run = false; |
---|
135 | break; |
---|
136 | } |
---|
137 | |
---|
138 | // process the received event |
---|
139 | processRequest(ev); |
---|
140 | sim_get_next(ev); |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | protected void processRequest(Sim_event ev) { |
---|
145 | switch (ev.get_tag()) { |
---|
146 | |
---|
147 | case GridSimTags.GRIDLET_SUBMIT: |
---|
148 | processWorkloadUnitSubmit(ev, false); |
---|
149 | break; |
---|
150 | |
---|
151 | case GridSimTags.GRIDLET_SUBMIT_ACK: |
---|
152 | processWorkloadUnitSubmit(ev, true); |
---|
153 | break; |
---|
154 | |
---|
155 | case GridSimTags.GRIDLET_RETURN: |
---|
156 | processWorkloadUnitReturn(ev); |
---|
157 | break; |
---|
158 | |
---|
159 | default: |
---|
160 | processOtherRequest(ev); |
---|
161 | break; |
---|
162 | } |
---|
163 | } |
---|
164 | |
---|
165 | protected void processOtherRequest(Sim_event ev) { |
---|
166 | switch (ev.get_tag()) { |
---|
167 | case DCWormsTags.QUERY_RESOURCE_DESC: |
---|
168 | SchedulerDescription desc = new SchedulerDescription(new LocalSystem(get_name(), null, null)); |
---|
169 | Map<ResourceUnitName, List<ResourceUnit>> units = managementSystem.getResourceManager().getSharedResourceUnits(); |
---|
170 | desc.addResourceUnitList(units); |
---|
171 | desc.addQueuesDescription(getQueuesDescription()); |
---|
172 | |
---|
173 | IO_data data = new IO_data(desc, 0, ev.get_src()); |
---|
174 | send(ev.get_src(), GridSimTags.SCHEDULE_NOW, DCWormsTags.QUERY_RESOURCE_DESC_RESULT, data); |
---|
175 | break; |
---|
176 | |
---|
177 | default: |
---|
178 | managementSystem.processEvent(ev); |
---|
179 | break; |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | protected void processWorkloadUnitReturn(Sim_event ev) { |
---|
184 | WorkloadUnit job = (WorkloadUnit) ev.get_data(); |
---|
185 | managementSystem.notifyReturnedWorkloadUnit(job); |
---|
186 | } |
---|
187 | |
---|
188 | protected void processWorkloadUnitSubmit(Sim_event ev, boolean ack) { |
---|
189 | WorkloadUnit job = (WorkloadUnit) ev.get_data(); |
---|
190 | managementSystem.notifySubmittedWorkloadUnit(job, ack); |
---|
191 | } |
---|
192 | |
---|
193 | public void send(String entityName, double delay, int tag, Object data){ |
---|
194 | super.send(entityName, delay, tag, data); |
---|
195 | } |
---|
196 | |
---|
197 | public void send(int destId, double delay, int tag){ |
---|
198 | super.send(destId, delay, tag); |
---|
199 | } |
---|
200 | |
---|
201 | public void send(int destId, double delay, int tag, Object data){ |
---|
202 | super.send(destId, delay, tag, data); |
---|
203 | } |
---|
204 | |
---|
205 | public void send(Sim_port destPort, double delay, int tag, Object data) { |
---|
206 | super.send(destPort, delay, tag, data); |
---|
207 | } |
---|
208 | |
---|
209 | public void sendInternal(double delay, int tag, Object data) { |
---|
210 | this.send(this.get_id(), delay, tag, data); |
---|
211 | } |
---|
212 | |
---|
213 | public Sim_port getOutputPort() { |
---|
214 | return output; |
---|
215 | } |
---|
216 | |
---|
217 | /*public Scheduler getScheduler(String resourceName){ |
---|
218 | ComputingResource resourceWithName = null; |
---|
219 | for(int i = 0 ; i < compResources.size() && resourceWithName == null; i++){ |
---|
220 | ComputingResource resource = compResources.get(i); |
---|
221 | if(resource.getName().equals(resourceName)) |
---|
222 | resourceWithName = resource; |
---|
223 | else |
---|
224 | try { |
---|
225 | resourceWithName = resource.getDescendantByName(resourceName); |
---|
226 | } catch (ResourceException e) { |
---|
227 | return null; |
---|
228 | } |
---|
229 | } |
---|
230 | if(resourceWithName == null) |
---|
231 | return null; |
---|
232 | List<ComputingResource> children = resourceWithName.getChildren(); |
---|
233 | Set<Scheduler> childrenSchedulers = new HashSet<Scheduler>(); |
---|
234 | for(ComputingResource child:children) { |
---|
235 | childrenSchedulers.add(child.getScheduler()); |
---|
236 | } |
---|
237 | Set<Scheduler> tempChildrenSchedulers = new HashSet<Scheduler>(childrenSchedulers); |
---|
238 | while(childrenSchedulers.size() != 1){ |
---|
239 | childrenSchedulers = new HashSet<Scheduler>(); |
---|
240 | for(Scheduler s: tempChildrenSchedulers){ |
---|
241 | childrenSchedulers.add(s.getParent()); |
---|
242 | } |
---|
243 | tempChildrenSchedulers = new HashSet<Scheduler>(childrenSchedulers); |
---|
244 | } |
---|
245 | Iterator<Scheduler> it = childrenSchedulers.iterator(); |
---|
246 | Scheduler potentialScheduler = it.next(); |
---|
247 | if(potentialScheduler.getResources().containsAll(children)) |
---|
248 | return potentialScheduler; |
---|
249 | return null; |
---|
250 | |
---|
251 | }*/ |
---|
252 | |
---|
253 | public List<QueueDescription> getQueuesDescription(){ |
---|
254 | List<QueueDescription> queues = new ArrayList<QueueDescription>(); |
---|
255 | for(TaskQueue queue: managementSystem.getQueues()){ |
---|
256 | QueueDescription qd; |
---|
257 | try { |
---|
258 | qd = new QueueDescription(queue.getName(), queue.getPriority(), queue.supportReservations(), getQueueLoad(queue.getName())); |
---|
259 | queues.add(qd); |
---|
260 | } catch (NoSuchFieldException e) {; |
---|
261 | } |
---|
262 | } |
---|
263 | return queues; |
---|
264 | |
---|
265 | } |
---|
266 | private long getQueueLoad(String queueName) throws NoSuchFieldException{ |
---|
267 | Map<String, Integer> queue_size = managementSystem.getQueuesSize(); |
---|
268 | long load = 0; |
---|
269 | if(queueName == null){ |
---|
270 | for(String queue: queue_size.keySet()){ |
---|
271 | load += queue_size.get(queue); |
---|
272 | } |
---|
273 | return load; |
---|
274 | } |
---|
275 | else if(queue_size.containsKey(queueName)) |
---|
276 | return queue_size.get(queueName); |
---|
277 | else |
---|
278 | throw new NoSuchFieldException("Queue " + queueName + |
---|
279 | " is not available in resource " + get_name()); |
---|
280 | } |
---|
281 | |
---|
282 | public ResourceType getType() { |
---|
283 | return this.type; |
---|
284 | } |
---|
285 | |
---|
286 | public ResourceStatus getStatus() { |
---|
287 | return status; |
---|
288 | } |
---|
289 | |
---|
290 | public void setStatus(ResourceStatus newStatus) { |
---|
291 | status = newStatus; |
---|
292 | } |
---|
293 | |
---|
294 | public void getResourceDescription(){ |
---|
295 | |
---|
296 | } |
---|
297 | |
---|
298 | public void initiate() { |
---|
299 | |
---|
300 | } |
---|
301 | } |
---|