1 | package schedframe.scheduling; |
---|
2 | |
---|
3 | import org.qcg.broker.schemas.resreqs.ComputingResource; |
---|
4 | import org.qcg.broker.schemas.resreqs.ComputingResourceBaseTypeItem; |
---|
5 | import org.qcg.broker.schemas.resreqs.ComputingResourceExtType; |
---|
6 | import org.qcg.broker.schemas.resreqs.ComputingResourceParameterType; |
---|
7 | import org.qcg.broker.schemas.resreqs.ExecutionTimeType; |
---|
8 | import org.qcg.broker.schemas.resreqs.ProcessesResourceRequirements; |
---|
9 | import org.qcg.broker.schemas.resreqs.Requirements; |
---|
10 | import org.qcg.broker.schemas.resreqs.TaskResourceRequirements; |
---|
11 | import org.qcg.broker.schemas.resreqs.TimePeriod; |
---|
12 | import org.qcg.broker.schemas.resreqs.TimePeriodChoice; |
---|
13 | import org.qcg.broker.schemas.resreqs.Topology; |
---|
14 | import org.qcg.broker.schemas.resreqs.types.ComputingResourceParameterTypeNameType; |
---|
15 | |
---|
16 | import java.io.StringReader; |
---|
17 | import java.io.StringWriter; |
---|
18 | import java.util.ArrayList; |
---|
19 | import java.util.List; |
---|
20 | |
---|
21 | import org.exolab.castor.xml.Marshaller; |
---|
22 | import org.exolab.castor.xml.ResolverException; |
---|
23 | import org.exolab.castor.xml.Unmarshaller; |
---|
24 | import org.exolab.castor.xml.XMLContext; |
---|
25 | import org.joda.time.DateTime; |
---|
26 | import org.joda.time.Duration; |
---|
27 | import org.joda.time.MutableDateTime; |
---|
28 | import org.joda.time.ReadableDuration; |
---|
29 | |
---|
30 | import schedframe.scheduling.utils.ResourceParameterName; |
---|
31 | |
---|
32 | /** |
---|
33 | * |
---|
34 | * @author Marcin Krystek |
---|
35 | * |
---|
36 | */ |
---|
37 | public class Task /*extends AbstractTask*/ implements TaskInterface<org.qcg.broker.schemas.resreqs.Task> { |
---|
38 | |
---|
39 | protected static Unmarshaller unmarshaller; |
---|
40 | protected static Marshaller marshaller; |
---|
41 | static { |
---|
42 | XMLContext context = new XMLContext(); |
---|
43 | try { |
---|
44 | context.addClass(org.qcg.broker.schemas.resreqs.Task.class); |
---|
45 | unmarshaller = context.createUnmarshaller(); |
---|
46 | marshaller = context.createMarshaller(); |
---|
47 | } catch (ResolverException e) { |
---|
48 | e.printStackTrace(); |
---|
49 | unmarshaller = null; |
---|
50 | marshaller = null; |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | protected org.qcg.broker.schemas.resreqs.Task task; |
---|
55 | /* |
---|
56 | * The values for following variables are obtained from native Task |
---|
57 | * object. This should significantly speed up access to task details. |
---|
58 | */ |
---|
59 | private DateTime startTime; |
---|
60 | private DateTime endTime; |
---|
61 | private DateTime brokerSubmitTime; |
---|
62 | private ReadableDuration duration; |
---|
63 | private List<AbstractProcessesGroup> groups; |
---|
64 | private List<AbstractProcesses> processes; |
---|
65 | private long length; |
---|
66 | private int status; |
---|
67 | private int senderId; |
---|
68 | private long workloadLogWaitTime; |
---|
69 | //String resPathHistory; |
---|
70 | |
---|
71 | public Task(org.qcg.broker.schemas.resreqs.Task task){ |
---|
72 | this.task = task; |
---|
73 | this.startTime = null; |
---|
74 | this.endTime = null; |
---|
75 | this.brokerSubmitTime = null; |
---|
76 | this.duration = null; |
---|
77 | // this.gridletID_ = (getJobId() + "_" + getId()).hashCode(); |
---|
78 | prepareTopology(); |
---|
79 | } |
---|
80 | |
---|
81 | public Task(String task) throws Exception{ |
---|
82 | StringReader reader = new StringReader(task); |
---|
83 | this.task = (org.qcg.broker.schemas.resreqs.Task) unmarshaller.unmarshal(reader); |
---|
84 | this.startTime = null; |
---|
85 | this.endTime = null; |
---|
86 | this.brokerSubmitTime = null; |
---|
87 | this.duration = null; |
---|
88 | // this.gridletID_ = (getJobId() + getId()).hashCode(); |
---|
89 | prepareTopology(); |
---|
90 | } |
---|
91 | |
---|
92 | public DateTime getExecutionStartTime() throws NoSuchFieldException { |
---|
93 | if(this.startTime != null) |
---|
94 | return this.startTime; |
---|
95 | |
---|
96 | ExecutionTimeType execTime = this.task.getExecutionTime(); |
---|
97 | if(execTime == null) |
---|
98 | throw new NoSuchFieldException("Execution Time for job " + getJobId() |
---|
99 | + " task "+ getId() + " is not defined."); |
---|
100 | |
---|
101 | TimePeriod timePeriod = execTime.getTimePeriod(); |
---|
102 | if(timePeriod == null) |
---|
103 | throw new NoSuchFieldException("Time Period for job " + getJobId() |
---|
104 | + " task "+ getId() + " is not defined."); |
---|
105 | |
---|
106 | this.startTime = new DateTime(timePeriod.getPeriodStart()); |
---|
107 | return this.startTime; |
---|
108 | } |
---|
109 | |
---|
110 | public DateTime getExecutionEndTime() throws NoSuchFieldException { |
---|
111 | if(this.endTime != null) |
---|
112 | return this.endTime; |
---|
113 | |
---|
114 | ExecutionTimeType execTime = this.task.getExecutionTime(); |
---|
115 | if(execTime == null) |
---|
116 | throw new NoSuchFieldException("Execution Time for job " + getJobId() |
---|
117 | + " task "+ getId() + " is not defined."); |
---|
118 | |
---|
119 | TimePeriod timePeriod = execTime.getTimePeriod(); |
---|
120 | if(timePeriod == null) |
---|
121 | throw new NoSuchFieldException("Time Period for job " + getJobId() |
---|
122 | + " task "+ getId() + " is not defined."); |
---|
123 | |
---|
124 | TimePeriodChoice periodChoice = timePeriod.getTimePeriodChoice(); |
---|
125 | if(periodChoice == null) |
---|
126 | throw new NoSuchFieldException("Period End and Period Duration for job " + getJobId() |
---|
127 | + " task "+ getId() + " are not defined."); |
---|
128 | |
---|
129 | java.util.Date periodEnd = periodChoice.getPeriodEnd(); |
---|
130 | if(periodEnd != null) { |
---|
131 | this.endTime = new DateTime(periodEnd); |
---|
132 | return this.endTime; |
---|
133 | } |
---|
134 | |
---|
135 | org.exolab.castor.types.Duration duration = periodChoice.getPeriodDuration(); |
---|
136 | if(duration == null) |
---|
137 | throw new NoSuchFieldException("Period Duration for job " + getJobId() |
---|
138 | + " task "+ getId() + " is not defined."); |
---|
139 | |
---|
140 | DateTime periodStart = getExecutionStartTime(); |
---|
141 | MutableDateTime m_periodEnd = periodStart.toMutableDateTime(); |
---|
142 | m_periodEnd.add(duration.toLong()); |
---|
143 | |
---|
144 | this.endTime = m_periodEnd.toDateTime(); |
---|
145 | periodChoice.setPeriodDuration(null); |
---|
146 | periodChoice.setPeriodEnd(this.endTime.toDate()); |
---|
147 | |
---|
148 | return this.endTime; |
---|
149 | } |
---|
150 | |
---|
151 | public ReadableDuration getExpectedDuration() throws NoSuchFieldException { |
---|
152 | if(this.duration != null) |
---|
153 | return this.duration; |
---|
154 | |
---|
155 | ExecutionTimeType execTime = this.task.getExecutionTime(); |
---|
156 | if(execTime == null) |
---|
157 | throw new NoSuchFieldException("Execution Time for job " + getJobId() |
---|
158 | + " task "+ getId() + " is not defined."); |
---|
159 | |
---|
160 | org.exolab.castor.types.Duration d = execTime.getExecutionDuration(); |
---|
161 | if(d == null) |
---|
162 | throw new NoSuchFieldException("Execution Duration for job " + getJobId() |
---|
163 | + " task "+ getId() + " is not defined."); |
---|
164 | |
---|
165 | this.duration = new Duration(d.toLong()); |
---|
166 | return this.duration; |
---|
167 | } |
---|
168 | |
---|
169 | public String getJobId() { |
---|
170 | return this.task.getJobId(); |
---|
171 | } |
---|
172 | |
---|
173 | public double getParameterDoubleValue(ResourceParameterName parameterName) |
---|
174 | throws NoSuchFieldException, IllegalArgumentException { |
---|
175 | |
---|
176 | ComputingResourceParameterTypeNameType name = ComputingResourceParameterTypeNameType.valueOf(parameterName.value().toUpperCase()); |
---|
177 | |
---|
178 | switch (name) { |
---|
179 | case APPLICATION: |
---|
180 | case CPUARCH: |
---|
181 | case HOSTNAME: |
---|
182 | case LOCALRESOURCEMANAGER: |
---|
183 | case OSNAME: |
---|
184 | case OSRELEASE: |
---|
185 | case OSTYPE: |
---|
186 | case OSVERSION: |
---|
187 | case REMOTESUBMISSIONINTERFACE: |
---|
188 | throw new IllegalArgumentException("For " + parameterName + " use getParameterStringValue() method."); |
---|
189 | } |
---|
190 | |
---|
191 | ComputingResourceBaseTypeItem item[] = getComputingResourceRequirements(); |
---|
192 | |
---|
193 | double returnValue = 0; |
---|
194 | boolean notFound = true; |
---|
195 | |
---|
196 | for(int i = 0; i < item.length && notFound; i++){ |
---|
197 | ComputingResourceParameterType hostParameter = item[i].getHostParameter(); |
---|
198 | if(hostParameter == null) |
---|
199 | continue; |
---|
200 | |
---|
201 | if(name == hostParameter.getName()) { |
---|
202 | returnValue = hostParameter.getParameterTypeChoice().getParameterTypeChoiceItem(0).getParameterValue().getContent(); |
---|
203 | notFound = false; |
---|
204 | } |
---|
205 | } |
---|
206 | |
---|
207 | if(notFound) |
---|
208 | throw new NoSuchFieldException(parameterName + " for job " + getJobId() |
---|
209 | + " task "+ getId() + " is not defined."); |
---|
210 | |
---|
211 | return returnValue; |
---|
212 | } |
---|
213 | |
---|
214 | public String getParameterStringValue(ResourceParameterName parameterName) |
---|
215 | throws NoSuchFieldException, IllegalArgumentException { |
---|
216 | ComputingResourceParameterTypeNameType name = ComputingResourceParameterTypeNameType.valueOf(parameterName.value().toUpperCase()); |
---|
217 | |
---|
218 | switch (name) { |
---|
219 | case CPUCOUNT: |
---|
220 | case GPUCOUNT: |
---|
221 | case CPUSPEED: |
---|
222 | case DISKSPACE: |
---|
223 | case FREECPUS: |
---|
224 | case FREEDISKSPACE: |
---|
225 | case FREEMEMORY: |
---|
226 | case MEMORY: |
---|
227 | throw new IllegalArgumentException("For " + parameterName + " use getParameterDoubleValue() method."); |
---|
228 | } |
---|
229 | |
---|
230 | ComputingResourceBaseTypeItem item[] = getComputingResourceRequirements(); |
---|
231 | |
---|
232 | String returnValue = null; |
---|
233 | boolean notFound = true; |
---|
234 | |
---|
235 | for(int i = 0; i < item.length && notFound; i++){ |
---|
236 | ComputingResourceParameterType hostParameter = item[i].getHostParameter(); |
---|
237 | if(hostParameter == null) |
---|
238 | continue; |
---|
239 | |
---|
240 | if(name == hostParameter.getName()) { |
---|
241 | returnValue = hostParameter.getStringValue(0).getValue(); |
---|
242 | notFound = false; |
---|
243 | } |
---|
244 | } |
---|
245 | |
---|
246 | if(notFound) |
---|
247 | throw new NoSuchFieldException(parameterName + " for job " + getJobId() |
---|
248 | + " task "+ getId() + " is not defined."); |
---|
249 | |
---|
250 | return returnValue; |
---|
251 | } |
---|
252 | |
---|
253 | public DateTime getSubmissionTimeToBroker() { |
---|
254 | if(this.brokerSubmitTime != null) |
---|
255 | return this.brokerSubmitTime; |
---|
256 | |
---|
257 | this.brokerSubmitTime = new DateTime(this.task.getSubmissionTime()); |
---|
258 | |
---|
259 | return this.brokerSubmitTime; |
---|
260 | } |
---|
261 | |
---|
262 | public String getId() { |
---|
263 | return this.task.getTaskId(); |
---|
264 | } |
---|
265 | |
---|
266 | public String getUserDn() { |
---|
267 | return this.task.getUserDN(); |
---|
268 | } |
---|
269 | |
---|
270 | public org.qcg.broker.schemas.resreqs.Task getDescription() { |
---|
271 | return this.task; |
---|
272 | } |
---|
273 | |
---|
274 | public String getDocument() throws Exception { |
---|
275 | StringWriter writer = new StringWriter(); |
---|
276 | |
---|
277 | marshaller.marshal(this.task, writer); |
---|
278 | |
---|
279 | return writer.toString(); |
---|
280 | } |
---|
281 | |
---|
282 | protected ComputingResourceBaseTypeItem[] getComputingResourceRequirements() throws NoSuchFieldException{ |
---|
283 | |
---|
284 | Requirements req = this.task.getRequirements(); |
---|
285 | if(req == null) |
---|
286 | throw new NoSuchFieldException("Requierements section for job " + getJobId() |
---|
287 | + " task "+ getId() + " is not defined."); |
---|
288 | |
---|
289 | TaskResourceRequirements taskReq = req.getTaskResourceRequirements(); |
---|
290 | if(taskReq == null) |
---|
291 | throw new NoSuchFieldException("Task resource requirements section for job " + getJobId() |
---|
292 | + " task "+ getId() + " is not defined."); |
---|
293 | |
---|
294 | ComputingResource computingResource = taskReq.getComputingResource(0); |
---|
295 | if(computingResource == null) |
---|
296 | throw new NoSuchFieldException("Computing resource requirement for job " + getJobId() |
---|
297 | + " task "+ getId() + " is not defined."); |
---|
298 | |
---|
299 | ComputingResourceBaseTypeItem item[] = computingResource.getComputingResourceBaseTypeItem(); |
---|
300 | if(item == null || item.length == 0) |
---|
301 | throw new NoSuchFieldException("Computing resource requirement is empty for job " + getJobId() |
---|
302 | + " task "+ getId()); |
---|
303 | |
---|
304 | return item; |
---|
305 | } |
---|
306 | |
---|
307 | public void setValue(ResourceParameterName parameterName, Object newValue) throws NoSuchFieldException{ |
---|
308 | boolean notFound = true; |
---|
309 | |
---|
310 | ComputingResourceParameterTypeNameType name = ComputingResourceParameterTypeNameType.valueOf(parameterName.value().toUpperCase()); |
---|
311 | |
---|
312 | ComputingResourceBaseTypeItem item[] = getComputingResourceRequirements(); |
---|
313 | |
---|
314 | for(int i = 0; i < item.length && notFound; i++){ |
---|
315 | ComputingResourceParameterType hostParameter = item[i].getHostParameter(); |
---|
316 | if(hostParameter == null) |
---|
317 | continue; |
---|
318 | |
---|
319 | if(name == hostParameter.getName()) { |
---|
320 | hostParameter. |
---|
321 | getParameterTypeChoice(). |
---|
322 | getParameterTypeChoiceItem(0). |
---|
323 | getParameterValue(). |
---|
324 | setContent(((Integer)newValue).doubleValue()); |
---|
325 | notFound = false; |
---|
326 | } |
---|
327 | } |
---|
328 | |
---|
329 | if(notFound) |
---|
330 | throw new NoSuchFieldException(parameterName + " for job " + getJobId() |
---|
331 | + " task "+ getId() + " is not defined."); |
---|
332 | } |
---|
333 | |
---|
334 | public List<AbstractProcessesGroup> getProcessesGroups() { |
---|
335 | return this.groups; |
---|
336 | } |
---|
337 | |
---|
338 | public List<AbstractProcesses> getProcesses(){ |
---|
339 | return this.processes; |
---|
340 | } |
---|
341 | |
---|
342 | public List<AbstractProcesses> getProcesses(AbstractProcessesGroup processGroup){ |
---|
343 | if(this.processes == null) |
---|
344 | return null; |
---|
345 | |
---|
346 | List<AbstractProcesses> ret = new ArrayList<AbstractProcesses>(); |
---|
347 | |
---|
348 | for(int i = 0; i < processes.size(); i++){ |
---|
349 | AbstractProcesses p = processes.get(i); |
---|
350 | if(p.belongsTo(processGroup)) |
---|
351 | ret.add(p); |
---|
352 | } |
---|
353 | |
---|
354 | return ret; |
---|
355 | } |
---|
356 | |
---|
357 | protected void prepareTopology(){ |
---|
358 | if(this.task.getRequirements() == null) |
---|
359 | return; |
---|
360 | |
---|
361 | if(this.task.getRequirements().getTopologyCount() < 1) |
---|
362 | return; |
---|
363 | |
---|
364 | Topology topology = this.task.getRequirements().getTopology(0); |
---|
365 | |
---|
366 | if(topology.getGroupCount() > 0){ |
---|
367 | this.groups = new ArrayList<AbstractProcessesGroup>(topology.getGroupCount()); |
---|
368 | } |
---|
369 | |
---|
370 | for(int i = 0; i < topology.getGroupCount(); i++){ |
---|
371 | this.groups.add(new ProcessesGroup(topology.getGroup(i))); |
---|
372 | } |
---|
373 | |
---|
374 | if(topology.getProcessesCount() > 0){ |
---|
375 | this.processes = new ArrayList<AbstractProcesses>(topology.getProcessesCount()); |
---|
376 | } |
---|
377 | |
---|
378 | for(int i = 0; i < topology.getProcessesCount(); i++){ |
---|
379 | org.qcg.broker.schemas.resreqs.Processes p = topology.getProcesses(i); |
---|
380 | if(p.getProcessesResourceRequirements() == null){ |
---|
381 | TaskResourceRequirements trr = this.task.getRequirements().getTaskResourceRequirements(); |
---|
382 | if(trr != null) { |
---|
383 | ProcessesResourceRequirements prr = new ProcessesResourceRequirements(); |
---|
384 | |
---|
385 | for(int cridx = 0; cridx < trr.getComputingResourceCount(); cridx++){ |
---|
386 | ComputingResourceExtType cre = new ComputingResourceExtType(); |
---|
387 | ComputingResource cr = trr.getComputingResource(cridx); |
---|
388 | |
---|
389 | for(int j = 0; j < cr.getComputingResourceBaseTypeItemCount(); j++){ |
---|
390 | cre.addComputingResourceBaseTypeItem(cr.getComputingResourceBaseTypeItem(j)); |
---|
391 | } |
---|
392 | |
---|
393 | prr.addComputingResource(cre); |
---|
394 | } |
---|
395 | |
---|
396 | p.setProcessesResourceRequirements(prr); |
---|
397 | } |
---|
398 | } |
---|
399 | |
---|
400 | this.processes.add(new Processes(p)); |
---|
401 | } |
---|
402 | } |
---|
403 | |
---|
404 | public double getCpuCntRequest() throws NoSuchFieldException { |
---|
405 | return getParameterDoubleValue(ResourceParameterName.CPUCOUNT); |
---|
406 | } |
---|
407 | |
---|
408 | public double getMemoryRequest() throws NoSuchFieldException { |
---|
409 | return getParameterDoubleValue(ResourceParameterName.MEMORY); |
---|
410 | } |
---|
411 | |
---|
412 | public long getLength() { |
---|
413 | return this.length; |
---|
414 | } |
---|
415 | |
---|
416 | public int getStatus() { |
---|
417 | return this.status; |
---|
418 | } |
---|
419 | |
---|
420 | public void setLength(long length) { |
---|
421 | this.length = length; |
---|
422 | } |
---|
423 | |
---|
424 | public void setStatus(int status) throws Exception{ |
---|
425 | this.status = status; |
---|
426 | } |
---|
427 | |
---|
428 | public void setSenderId(int id){ |
---|
429 | this.senderId = id; |
---|
430 | } |
---|
431 | |
---|
432 | public int getSenderId(){ |
---|
433 | return this.senderId; |
---|
434 | } |
---|
435 | |
---|
436 | public boolean isFinished(){ |
---|
437 | if(processes == null) |
---|
438 | return (status > 3 && status <= 6); |
---|
439 | |
---|
440 | for(int i = 0; i < processes.size(); i++){ |
---|
441 | if(!processes.get(i).isFinished()) |
---|
442 | return false; |
---|
443 | } |
---|
444 | |
---|
445 | return true; |
---|
446 | } |
---|
447 | |
---|
448 | public long getWorkloadLogWaitTime() { |
---|
449 | return workloadLogWaitTime; |
---|
450 | } |
---|
451 | |
---|
452 | public void setWorkloadLogWaitTime(long waitTime) { |
---|
453 | this.workloadLogWaitTime = waitTime; |
---|
454 | } |
---|
455 | |
---|
456 | /*public void addToResPath(String resName){ |
---|
457 | if(resPathHistory == null) |
---|
458 | resPathHistory = new String(); |
---|
459 | resPathHistory = new StringBuffer(resPathHistory).append(resName).append("_").toString(); |
---|
460 | |
---|
461 | } |
---|
462 | |
---|
463 | public String getResPath(){ |
---|
464 | return this.resPathHistory; |
---|
465 | |
---|
466 | }*/ |
---|
467 | |
---|
468 | @Override |
---|
469 | public List<? extends TaskInterface<?>> getTask() { |
---|
470 | // TODO Auto-generated method stub |
---|
471 | return null; |
---|
472 | } |
---|
473 | |
---|
474 | @Override |
---|
475 | public TaskInterface<?> getTask(String taskId) throws NoSuchFieldException { |
---|
476 | // TODO Auto-generated method stub |
---|
477 | return null; |
---|
478 | } |
---|
479 | |
---|
480 | @Override |
---|
481 | public int getTaskCount() { |
---|
482 | // TODO Auto-generated method stub |
---|
483 | return 0; |
---|
484 | } |
---|
485 | |
---|
486 | @Override |
---|
487 | public int getUserID() { |
---|
488 | // TODO Auto-generated method stub |
---|
489 | return 0; |
---|
490 | } |
---|
491 | } |
---|