source: DCWoRMS/trunk/src/dcworms/schedframe/scheduling/Executable.java @ 540

Revision 540, 9.4 KB checked in by wojtekp, 12 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package dcworms.schedframe.scheduling;
2
3
4import gridsim.GridSim;
5import gridsim.dcworms.DCWormsTags;
6
7import java.util.ArrayList;
8import java.util.HashMap;
9import java.util.List;
10import java.util.Map;
11
12import org.apache.commons.lang.ArrayUtils;
13import org.joda.time.DateTime;
14import org.joda.time.DateTimeUtilsExt;
15import org.joda.time.ReadableDuration;
16
17
18import schedframe.resources.computing.ComputingResource;
19import schedframe.resources.units.ProcessingElements;
20import schedframe.resources.units.StandardResourceUnitName;
21import schedframe.scheduling.ResourceHistoryItem;
22import schedframe.scheduling.UsedResourcesList;
23import schedframe.scheduling.WorkloadUnitHandler;
24import schedframe.scheduling.manager.tasks.JobRegistryImpl;
25import schedframe.scheduling.tasks.AbstractProcesses;
26import schedframe.scheduling.tasks.AbstractProcessesGroup;
27import schedframe.scheduling.tasks.Task;
28import schedframe.scheduling.tasks.requirements.ResourceParameterName;
29
30/**
31 *
32 * @author Marcin Krystek
33 *
34 */
35public class Executable implements ExecTask{
36
37        protected Task task;
38        protected String processesSetId;
39       
40        protected int status;
41        protected double length;
42        protected Map<ResourceParameterName, Object> specificResources;
43       
44        protected String reservationId;
45        protected double completionPercentage;
46       
47        protected int estimatedDuration;
48        //TO DO remove and benefit from visitedResources
49        protected String schedName;
50        protected UsedResourcesList usedResources;
51        //TO DO consider removing
52        protected List<String> visitedResources;
53
54        protected double submissionTime;
55    protected double arrivalTime;
56        protected double execStartTime ;
57    protected double totalCompletionTime;
58        protected double finishTime;
59       
60        public Executable(Task t){
61                this.task = t;
62                this.status = DCWormsTags.CREATED;
63               
64                this.usedResources = new UsedResourcesList();
65                this.visitedResources = new ArrayList<String>();
66                init();
67        }
68       
69        public Executable(Task t, AbstractProcesses procesesSet){
70                this.task = t;
71                this.status = DCWormsTags.CREATED;
72                this.processesSetId = procesesSet.getId();
73               
74                this.usedResources = new UsedResourcesList();
75                this.visitedResources = new ArrayList<String>();
76                init();
77        }
78       
79        protected void init() {
80                double currentTime = DateTimeUtilsExt.currentTimeMillis() / 1000;
81                this.submissionTime =  currentTime;
82        this.totalCompletionTime = 0.0;
83         }
84       
85        public int getUserId() {
86                return task.getSenderId();
87        }
88        public String getUserDN() {
89                return task.getUserDN();
90        }
91
92        public String getJobId() {
93                return task.getJobId();
94        }
95       
96        public String getTaskId(){
97                return this.task.getId();
98        }
99       
100        public String getId() {
101                if(processesSetId == null)
102                        return task.getId();
103                else
104                        return task.getId() + "_" + processesSetId;
105        }
106       
107        public String getProcessesId(){
108                return this.processesSetId;
109        }
110       
111    public int getUniqueId(){
112        if(processesSetId == null){
113                return (task.getJobId() + "_" + task.getId()).hashCode();
114        } else {
115                return (task.getJobId() + "_" + task.getId() + "_" + processesSetId).hashCode();
116        }
117    }
118   
119        public List<AbstractProcesses> getProcesses() {
120                return task.getProcesses();
121        }
122
123        public List<AbstractProcesses> getProcesses(
124                        AbstractProcessesGroup processGroup) {
125                return task.getProcesses(processGroup);
126        }
127
128        public List<AbstractProcessesGroup> getProcessesGroups() {
129                return task.getProcessesGroups();
130        }
131
132        public org.qcg.broker.schemas.resreqs.Task getDescription() {
133                return task.getDescription();
134        }
135
136        public String getDocument() throws Exception {
137                return task.getDocument();
138        }
139       
140        public long getLength() {
141                return task.getLength();
142        }
143
144        public int getStatus() {
145                return status;
146        }
147       
148        public boolean isFinished()
149    {
150                return task.isFinished();
151    }
152       
153        public void setStatus(int newStatus) throws Exception {
154                int prevStatus = status;
155           
156        if (status == newStatus) {
157            return;
158        }
159
160        if (newStatus < DCWormsTags.CREATED || newStatus > DCWormsTags.FAILED_RESOURCE_UNAVAILABLE) {
161            throw new Exception("Executable.setStatuts() : Error - " +
162                    "Invalid integer range for Execiutable status.");
163        }
164
165        status = newStatus;
166        double currentTime = DateTimeUtilsExt.currentTimeMillis() / 1000; // time in seconds
167   
168               
169                if (newStatus == DCWormsTags.SUCCESS || newStatus == DCWormsTags.CANCELED) {
170            finishTime = DateTimeUtilsExt.currentTimeMillis() / 1000;
171        }
172               
173                if(newStatus == DCWormsTags.SUBMITTED){
174                         arrivalTime = GridSim.clock();
175                }
176
177        if (prevStatus == DCWormsTags.INEXEC) {
178            if (status == DCWormsTags.CANCELED || status == DCWormsTags.PAUSED ||
179                status == DCWormsTags.SUCCESS) {
180                totalCompletionTime += (currentTime -  execStartTime);
181            }
182        }
183
184        if (prevStatus == DCWormsTags.RESUMED && status == DCWormsTags.SUCCESS) {
185            totalCompletionTime += (currentTime -  execStartTime);
186        }
187
188        if (status == DCWormsTags.INEXEC ||
189            (prevStatus == DCWormsTags.PAUSED && status == DCWormsTags.RESUMED) ) {
190                execStartTime = currentTime;
191               
192                ProcessingElements pes = (ProcessingElements) getUsedResources().getLast().getResourceUnits().get(StandardResourceUnitName.PE);
193                for (ComputingResource resource : pes) {
194
195                        trackResource(resource.getName());
196                       
197                        ComputingResource parent = resource.getParent();
198                                List<String> visitedResource = getVisitedResources();
199                                String [] visitedResourcesArray = visitedResource.toArray(new String[visitedResource.size()]);
200                        while (parent != null && !ArrayUtils.contains(visitedResourcesArray, parent.getName())) {
201                                trackResource(parent.getName());
202                                parent = parent.getParent();
203                        }
204                }
205        }
206        }
207       
208        public void addSpecificResource(ResourceParameterName resourceName, Object value){
209                if(this.specificResources == null)
210                        this.specificResources = new HashMap<ResourceParameterName, Object>();
211               
212                this.specificResources.put(resourceName, value);
213        }
214
215        public boolean expectSpecificResource(ResourceParameterName resourceName){
216                if(this.specificResources == null)
217                        return false;
218
219                return this.specificResources.containsKey(resourceName);
220        }
221       
222        public Object getExpectedSpecificResource(ResourceParameterName resourceName){
223                if(this.specificResources == null)
224                        return null;
225               
226                return this.specificResources.get(resourceName);
227        }
228       
229        public void setReservationId(String reservationId){
230                this.reservationId = reservationId;
231        }
232       
233        public boolean requireReservation(){
234                return (reservationId != null);
235        }
236       
237        public String getReservationId(){
238                return this.reservationId;
239        }
240       
241        public double getCompletionPercentage() {
242                return completionPercentage;
243        }
244
245        public void setCompletionPercentage(double completionPercentage) {
246                this.completionPercentage = completionPercentage;
247        }
248
249        public void addUsedResources(ResourceHistoryItem usedResources){
250                this.usedResources.add(usedResources);
251        }
252       
253        public UsedResourcesList getUsedResources(){
254                return this.usedResources;
255        }
256       
257    public void setSchedulerName(int resourceId)
258    {
259        this.schedName = GridSim.getEntityName(resourceId);
260    }
261
262    public String getSchedulerName()
263    {
264        return schedName;
265    }
266       
267        public int getEstimatedDuration(){
268                return this.estimatedDuration;
269        }
270       
271        public void setEstimatedDuration(int value){
272                this.estimatedDuration = value;
273        }
274
275        public boolean isRegistered() {
276                return task.isRegistered();
277        }
278
279        public void register(JobRegistryImpl jobRegistry) {
280                task.register(jobRegistry);
281        }
282       
283        public void trackResource(String resName){
284                visitedResources.add(resName);
285        }
286       
287        public List<String> getVisitedResources(){
288                return visitedResources;
289        }
290       
291        public ReadableDuration getExpectedDuration() throws NoSuchFieldException {
292                return task.getExpectedDuration();
293        }
294
295        public double getParameterDoubleValue(ResourceParameterName parameterName)
296                        throws NoSuchFieldException, IllegalArgumentException {
297                return task.getParameterDoubleValue(parameterName);
298        }
299
300        public String getParameterStringValue(ResourceParameterName parameterName)
301                        throws NoSuchFieldException, IllegalArgumentException {
302                return task.getParameterStringValue(parameterName);
303        }
304       
305        public double getCpuCntRequest() throws NoSuchFieldException{
306                return getParameterDoubleValue(ResourceParameterName.CPUCOUNT);
307        }
308       
309        public double getMemoryRequest() throws NoSuchFieldException{
310                return getParameterDoubleValue(ResourceParameterName.MEMORY);
311        }
312       
313        public DateTime getExecutionEndTime() throws NoSuchFieldException {
314                return task.getExecutionEndTime();
315        }
316
317        public DateTime getExecutionStartTime() throws NoSuchFieldException {
318                return task.getExecutionStartTime();
319        }
320
321        public DateTime getSubmissionTimeToBroker() {
322                return task.getSubmissionTimeToBroker();
323        }
324
325        public long getWorkloadLogWaitTime() {
326                return task.getWorkloadLogWaitTime();
327        }
328
329    public double getExecStartTime() {
330        return execStartTime;
331    }
332
333    public double getFinishTime() {
334        return finishTime;
335    }
336
337    public double getSubmissionTime() {
338        return submissionTime;
339    }
340   
341    public double getWaitingTime()
342    {
343        return execStartTime - submissionTime;
344    }
345   
346    public void finalizeExecutable(){
347                try {
348                        setStatus(DCWormsTags.SUCCESS);
349                } catch (Exception e) {
350                        // TODO Auto-generated catch block
351                        e.printStackTrace();
352                }
353    }
354   
355        public void accept(WorkloadUnitHandler wuh) {
356                wuh.handleExecutable(this);
357        }
358   
359        public boolean equals(Object obj){
360                if(obj instanceof Executable){
361                        Executable t = (Executable) obj;
362                        return getId().equals(t.getId());
363                }
364                return false;
365        }
366
367}
Note: See TracBrowser for help on using the repository browser.