source: DCWoRMS/trunk/src/schedframe/scheduling/tasks/requirements/TimeRequirements.java @ 477

Revision 477, 6.0 KB checked in by wojtekp, 13 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package schedframe.scheduling.tasks.requirements;
2
3import java.io.StringWriter;
4import java.io.Writer;
5import java.util.Iterator;
6
7import org.apache.commons.logging.Log;
8import org.apache.commons.logging.LogFactory;
9import org.joda.time.DateTime;
10import org.joda.time.Duration;
11import org.joda.time.ReadableDuration;
12
13
14import org.qcg.broker.schemas.resreqs.ExecutionTimeType;
15import org.qcg.broker.schemas.resreqs.Task;
16
17import schedframe.scheduling.tasks.TaskInterface;
18
19/**
20 *
21 * @author Marcin Krystek
22 *
23 */
24public class TimeRequirements extends AbstractTimeRequirements<ExecutionTimeType>{
25       
26        protected static Log log = LogFactory.getLog(TimeRequirements.class);
27
28        private static final long serialVersionUID = -4011141375443906415L;
29
30        protected TimeRequirements(){
31        }
32       
33        public TimeRequirements(ExecutionTimeType execTimeType) throws NoSuchFieldException{
34                this.timeRequirements = execTimeType;
35                DateTime startTime = null;
36                DateTime endTime = null;
37
38                org.exolab.castor.types.Duration cd = execTimeType.getExecutionDuration();
39                this.expectedDuration = new Duration(cd.toLong());
40
41               
42                try {
43                        startTime = new DateTime(execTimeType.getTimePeriod().getPeriodStart().getTime());
44                } catch (Exception e){
45                        startTime = new DateTime();
46                        log.warn("Period start is not defined.\n" + e.getMessage() + " Using current time: " + startTime);
47                }
48               
49                try {
50                        endTime = new DateTime(execTimeType.getTimePeriod().getTimePeriodChoice().getPeriodEnd().getTime());
51                } catch (Exception e){
52                        log.warn("Period end is not defined. Trying period duration...\n" + e.getMessage());
53                        try {
54                                org.exolab.castor.types.Duration duration = execTimeType.getTimePeriod().
55                                                                                                                getTimePeriodChoice().getPeriodDuration();
56                                endTime = new DateTime(startTime.getMillis() + duration.toLong());
57                        } catch(Exception e1){
58                                endTime = new DateTime(Long.MAX_VALUE);
59                                log.warn("Period duration is not defined.\n" + e1.getMessage() + " Using infinity: " + endTime);
60                        }
61                }
62               
63                setEnd(endTime);
64                setStart(startTime);
65               
66        }
67       
68        public TimeRequirements(TaskInterface<?> task) throws NoSuchFieldException{
69
70                this.expectedDuration = task.getExpectedDuration();
71               
72                DateTime startTime = null;
73                DateTime endTime = null;
74               
75                try {
76                        startTime = task.getExecutionStartTime();
77                } catch (NoSuchFieldException e) {
78                        startTime = new DateTime();
79                        log.warn(e.getMessage() + " Using current time as Start Time " + startTime);
80                }
81               
82                try {
83                        endTime = task.getExecutionEndTime();
84                } catch(NoSuchFieldException e){
85                        endTime = new DateTime(Long.MAX_VALUE);
86                        log.warn(e.getMessage() + " Using infinity as End Time " + endTime);
87                }
88               
89                setEnd(endTime);
90                setStart(startTime);
91               
92                this.timeRequirements = ((Task)task.getDescription()).getExecutionTime();
93               
94        }
95       
96        public TimeRequirements(TaskInterface<?> task, long startInterval, long endInterval) throws NoSuchFieldException{
97                long taskLength = task.getLength();
98                long reqCpu = (long)task.getCpuCntRequest();
99                long taskDuration = taskLength/reqCpu;
100                long workloadLogWaitTime = task.getWorkloadLogWaitTime();
101                long workloadLogStartTime = task.getSubmissionTimeToBroker().getMillis()/1000 + workloadLogWaitTime;
102                long possibleStartTime = workloadLogStartTime - Math.min(workloadLogWaitTime, startInterval);
103
104                DateTime startTime = null;
105                DateTime endTime = null;
106                               
107                try {
108                        startTime = task.getExecutionStartTime();
109                } catch (NoSuchFieldException e) {
110                        startTime = new DateTime(possibleStartTime * 1000);
111                        log.warn(e.getMessage() + " Using current time as Start Time " + startTime);
112                }
113
114                try {
115                        endTime = task.getExecutionEndTime();
116                } catch(NoSuchFieldException e){
117                        endTime = new DateTime(startTime.getMillis() + (int)taskDuration * 1000 + endInterval * 1000);
118                        log.warn(e.getMessage() + " Using " + endTime + " as End Time");
119                }
120               
121                setEnd(endTime);
122                setStart(startTime);
123               
124                this.timeRequirements = ((Task)task.getDescription()).getExecutionTime();
125               
126        }
127
128        public void setExpectedDuration(ReadableDuration duration){
129                this.expectedDuration = duration;
130        }
131       
132        public ExecutionTimeType getDescription() {
133                return this.timeRequirements;
134        }
135       
136        public String getDocument() throws Exception {
137                Writer w = new StringWriter();
138                this.timeRequirements.marshal(w);
139                return w.toString();
140        }
141       
142        public Iterator<AbstractTimeRequirements<?>> iterator(long interval){
143                return new TimeIntervalItr(this, interval);
144        }
145       
146       
147        protected class LocalTimeRequirements extends AbstractTimeRequirements<Object> {
148
149                private static final long serialVersionUID = 5132710437273933809L;
150
151                public LocalTimeRequirements(DateTime startTime, DateTime endTime, ReadableDuration duration){
152                        setEnd(endTime);
153                        setStart(startTime);
154                        this.expectedDuration = duration;
155                }
156               
157                public Object getDescription() {
158                        throw new RuntimeException("Not implementd.");
159                }
160
161                public String getDocument() throws Exception {
162                        throw new RuntimeException("Not implemented.");
163                }
164               
165        }
166       
167        protected class TimeIntervalItr implements Iterator<AbstractTimeRequirements<?>> {
168
169                protected long interval;
170                protected ReadableDuration duration;
171                protected long endTime;
172                protected AbstractTimeRequirements<?> next;
173               
174                public TimeIntervalItr(AbstractTimeRequirements<?> timeReq, long interval){
175                        this.interval = interval;
176                        this.endTime = timeReq.getEndMillis();
177                        this.duration = timeReq.getExpectedDuration();
178                       
179                        DateTime start = timeReq.getStart();
180                        DateTime end = start.plus(timeReq.getExpectedDuration());
181                       
182                        this.next = new LocalTimeRequirements(start, end, this.duration);
183                }
184               
185                public boolean hasNext() {
186                       
187                        long intervalEnd = this.next.getEndMillis();
188                       
189                        return (intervalEnd <= endTime);
190                       
191                }
192
193                public AbstractTimeRequirements<?> next() {
194                        AbstractTimeRequirements<?> ret = this.next;
195                       
196                        if(interval == 0) {
197                                endTime = 0;
198                        } else {
199                                DateTime start = this.next.getStart().plus(interval);
200                                DateTime end = this.next.getEnd().plus(interval);
201                               
202                                this.next = new LocalTimeRequirements(start, end, this.duration);
203                        }
204                       
205                        return ret;
206                       
207                }
208
209                public void remove() {
210                        throw new RuntimeException("Not implemented.");
211                }
212               
213        }
214
215}
Note: See TracBrowser for help on using the repository browser.