source: xssim/src/schedframe/scheduling/TimeRequirements.java @ 104

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