source: DCWoRMS/branches/coolemall/src/schedframe/scheduling/tasks/Task.java @ 1362

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