package schedframe.scheduling; import java.io.StringWriter; import java.io.Writer; import java.util.Iterator; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.joda.time.DateTime; import org.joda.time.Duration; import org.joda.time.ReadableDuration; import org.qcg.broker.schemas.resreqs.ExecutionTimeType; import org.qcg.broker.schemas.resreqs.Task; /** * * @author Marcin Krystek * */ public class TimeRequirements extends AbstractTimeRequirements{ protected static Log log = LogFactory.getLog(TimeRequirements.class); private static final long serialVersionUID = -4011141375443906415L; protected TimeRequirements(){ } public TimeRequirements(ExecutionTimeType execTimeType) throws NoSuchFieldException{ this.timeRequirements = execTimeType; DateTime startTime = null; DateTime endTime = null; org.exolab.castor.types.Duration cd = execTimeType.getExecutionDuration(); this.expectedDuration = new Duration(cd.toLong()); try { startTime = new DateTime(execTimeType.getTimePeriod().getPeriodStart().getTime()); } catch (Exception e){ startTime = new DateTime(); log.warn("Period start is not defined.\n" + e.getMessage() + " Using current time: " + startTime); } try { endTime = new DateTime(execTimeType.getTimePeriod().getTimePeriodChoice().getPeriodEnd().getTime()); } catch (Exception e){ log.warn("Period end is not defined. Trying period duration...\n" + e.getMessage()); try { org.exolab.castor.types.Duration duration = execTimeType.getTimePeriod(). getTimePeriodChoice().getPeriodDuration(); endTime = new DateTime(startTime.getMillis() + duration.toLong()); } catch(Exception e1){ endTime = new DateTime(Long.MAX_VALUE); log.warn("Period duration is not defined.\n" + e1.getMessage() + " Using infinity: " + endTime); } } setEnd(endTime); setStart(startTime); } public TimeRequirements(TaskInterface task) throws NoSuchFieldException{ this.expectedDuration = task.getExpectedDuration(); DateTime startTime = null; DateTime endTime = null; try { startTime = task.getExecutionStartTime(); } catch (NoSuchFieldException e) { startTime = new DateTime(); log.warn(e.getMessage() + " Using current time as Start Time " + startTime); } try { endTime = task.getExecutionEndTime(); } catch(NoSuchFieldException e){ endTime = new DateTime(Long.MAX_VALUE); log.warn(e.getMessage() + " Using infinity as End Time " + endTime); } setEnd(endTime); setStart(startTime); this.timeRequirements = ((Task)task.getDescription()).getExecutionTime(); } public TimeRequirements(TaskInterface task, long startInterval, long endInterval) throws NoSuchFieldException{ long taskLength = task.getLength(); long reqCpu = (long)task.getCpuCntRequest(); long taskDuration = taskLength/reqCpu; long workloadLogWaitTime = task.getWorkloadLogWaitTime(); long workloadLogStartTime = task.getSubmissionTimeToBroker().getMillis()/1000 + workloadLogWaitTime; long possibleStartTime = workloadLogStartTime - Math.min(workloadLogWaitTime, startInterval); DateTime startTime = null; DateTime endTime = null; try { startTime = task.getExecutionStartTime(); } catch (NoSuchFieldException e) { startTime = new DateTime(possibleStartTime * 1000); log.warn(e.getMessage() + " Using current time as Start Time " + startTime); } try { endTime = task.getExecutionEndTime(); } catch(NoSuchFieldException e){ endTime = new DateTime(startTime.getMillis() + (int)taskDuration * 1000 + endInterval * 1000); log.warn(e.getMessage() + " Using " + endTime + " as End Time"); } setEnd(endTime); setStart(startTime); this.timeRequirements = ((Task)task.getDescription()).getExecutionTime(); } public void setExpectedDuration(ReadableDuration duration){ this.expectedDuration = duration; } public ExecutionTimeType getDescription() { return this.timeRequirements; } public String getDocument() throws Exception { Writer w = new StringWriter(); this.timeRequirements.marshal(w); return w.toString(); } public Iterator> iterator(long interval){ return new TimeIntervalItr(this, interval); } protected class LocalTimeRequirements extends AbstractTimeRequirements { private static final long serialVersionUID = 5132710437273933809L; public LocalTimeRequirements(DateTime startTime, DateTime endTime, ReadableDuration duration){ setEnd(endTime); setStart(startTime); this.expectedDuration = duration; } public Object getDescription() { throw new RuntimeException("Not implementd."); } public String getDocument() throws Exception { throw new RuntimeException("Not implemented."); } } protected class TimeIntervalItr implements Iterator> { protected long interval; protected ReadableDuration duration; protected long endTime; protected AbstractTimeRequirements next; public TimeIntervalItr(AbstractTimeRequirements timeReq, long interval){ this.interval = interval; this.endTime = timeReq.getEndMillis(); this.duration = timeReq.getExpectedDuration(); DateTime start = timeReq.getStart(); DateTime end = start.plus(timeReq.getExpectedDuration()); this.next = new LocalTimeRequirements(start, end, this.duration); } public boolean hasNext() { long intervalEnd = this.next.getEndMillis(); return (intervalEnd <= endTime); } public AbstractTimeRequirements next() { AbstractTimeRequirements ret = this.next; if(interval == 0) { endTime = 0; } else { DateTime start = this.next.getStart().plus(interval); DateTime end = this.next.getEnd().plus(interval); this.next = new LocalTimeRequirements(start, end, this.duration); } return ret; } public void remove() { throw new RuntimeException("Not implemented."); } } }