source: DCWoRMS/branches/coolemall/src/dcworms/schedframe/scheduling/Executable.java @ 883

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