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

Revision 896, 10.6 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
17import schedframe.resources.computing.ComputingResource;
18import schedframe.resources.units.ProcessingElements;
19import schedframe.resources.units.StandardResourceUnitName;
20import schedframe.scheduling.ResourceHistoryItem;
21import schedframe.scheduling.UsedResourcesList;
22import schedframe.scheduling.WorkloadUnitHandler;
23import schedframe.scheduling.manager.tasks.JobRegistryImpl;
24import schedframe.scheduling.tasks.AbstractProcesses;
25import schedframe.scheduling.tasks.AbstractProcessesGroup;
26import schedframe.scheduling.tasks.Task;
27import schedframe.scheduling.tasks.phases.ResourceConsumption;
28import schedframe.scheduling.tasks.phases.ResourceConsumptionProfile;
29import schedframe.scheduling.tasks.requirements.ResourceParameterName;
30
31/**
32 *
33 * @author Marcin Krystek
34 *
35 */
36public class Executable implements ExecTask{
37
38        protected Task task;
39        protected String processesSetId;
40       
41        protected int status;
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.NEW_EXEC_PHASE) {
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        if(status == DCWormsTags.NEW_EXEC_PHASE){
208                if(prevStatus == DCWormsTags.INEXEC){
209                        status = DCWormsTags.INEXEC;
210                        setCompletionPercentage(0);
211                        task.getResourceConsumptionProfile().setCurrentPhase(task.getResourceConsumptionProfile().getCurrentPhase() + 1);
212                       
213                        DateTime currentDateTime = new DateTime();
214                       
215                        if(getUsedResources().getLast().getTimeStamp().getMillis() == currentDateTime.getMillis()){
216                                return;
217                        }
218                        ResourceHistoryItem resHistItem = new ResourceHistoryItem(getUsedResources().getLast().getResourceUnits(), currentDateTime);
219                        addUsedResources(resHistItem);
220                }
221        }
222        }
223       
224        public void addSpecificResource(ResourceParameterName resourceName, Object value){
225                if(this.specificResources == null)
226                        this.specificResources = new HashMap<ResourceParameterName, Object>();
227                this.specificResources.put(resourceName, value);
228        }
229
230        public boolean expectSpecificResource(ResourceParameterName resourceName){
231                if(this.specificResources == null)
232                        return false;
233                return this.specificResources.containsKey(resourceName);
234        }
235       
236        public Object getExpectedSpecificResource(ResourceParameterName resourceName){
237                if(this.specificResources == null)
238                        return null;
239               
240                return this.specificResources.get(resourceName);
241        }
242       
243        public void setReservationId(String reservationId){
244                this.reservationId = reservationId;
245        }
246       
247        public boolean requireReservation(){
248                return (reservationId != null);
249        }
250       
251        public String getReservationId(){
252                return this.reservationId;
253        }
254       
255        public double getCompletionPercentage() {
256                return completionPercentage;
257        }
258
259        public void setCompletionPercentage(double completionPercentage) {
260                this.completionPercentage = completionPercentage;
261        }
262
263        public void addUsedResources(ResourceHistoryItem usedResources){
264                this.usedResources.add(usedResources);
265        }
266       
267        public UsedResourcesList getUsedResources(){
268                return this.usedResources;
269        }
270       
271    public void setSchedulerName(int resourceId){
272        this.schedName = GridSim.getEntityName(resourceId);
273    }
274
275    public String getSchedulerName(){
276        return schedName;
277    }
278       
279        public int getEstimatedDuration(){
280                return this.estimatedDuration;
281        }
282       
283        public void setEstimatedDuration(int value){
284                this.estimatedDuration = value;
285        }
286
287        public boolean isRegistered() {
288                return task.isRegistered();
289        }
290
291        public void register(JobRegistryImpl jobRegistry) {
292                task.register(jobRegistry);
293        }
294       
295        public void trackResource(String resName){
296                visitedResources.add(resName);
297        }
298       
299        public List<String> getVisitedResources(){
300                return visitedResources;
301        }
302       
303        public ReadableDuration getExpectedDuration() throws NoSuchFieldException {
304                return task.getExpectedDuration();
305        }
306
307        public double getParameterDoubleValue(ResourceParameterName parameterName)
308                        throws NoSuchFieldException, IllegalArgumentException {
309                return task.getParameterDoubleValue(parameterName);
310        }
311
312        public String getParameterStringValue(ResourceParameterName parameterName)
313                        throws NoSuchFieldException, IllegalArgumentException {
314                return task.getParameterStringValue(parameterName);
315        }
316       
317        public double getCpuCntRequest() throws NoSuchFieldException{
318                return getParameterDoubleValue(ResourceParameterName.CPUCOUNT);
319        }
320       
321        public double getMemoryRequest() throws NoSuchFieldException{
322                return getParameterDoubleValue(ResourceParameterName.MEMORY);
323        }
324       
325        public DateTime getExecutionEndTime() throws NoSuchFieldException {
326                return task.getExecutionEndTime();
327        }
328
329        public DateTime getExecutionStartTime() throws NoSuchFieldException {
330                return task.getExecutionStartTime();
331        }
332
333        public DateTime getSubmissionTimeToBroker() {
334                return task.getSubmissionTimeToBroker();
335        }
336
337        public long getWorkloadLogWaitTime() {
338                return task.getWorkloadLogWaitTime();
339        }
340
341    public double getExecStartTime() {
342        return execStartTime;
343    }
344
345    public double getFinishTime() {
346        return finishTime;
347    }
348
349    public double getSubmissionTime() {
350        return submissionTime;
351    }
352   
353    public double getWaitingTime() {
354        return execStartTime - submissionTime;
355    }
356   
357    public void finalizeExecutable(){
358                try {
359                        setStatus(DCWormsTags.SUCCESS);
360                } catch (Exception e) {
361                        // TODO Auto-generated catch block
362                        e.printStackTrace();
363                }
364    }
365   
366        public void accept(WorkloadUnitHandler wuh) {
367                wuh.handleExecutable(this);
368        }
369   
370        /*public boolean equals(Object obj){
371                if(obj instanceof Executable){
372                        Executable t = (Executable) obj;
373                        return getId().equals(t.getId()) && getJobId().equals(t.getJobId());
374                }
375                return false;
376        }*/
377       
378        public ResourceConsumptionProfile getResourceConsumptionProfile(){
379                return task.getResourceConsumptionProfile();
380        }
381
382        public ResourceConsumption getCurrentResourceConsumption(){
383                return task.getResourceConsumptionProfile().getCurrentResourceConsumption();
384        }
385       
386        public String getApplicationName(){
387                return task.getApplicationName();
388        }
389
390}
Note: See TracBrowser for help on using the repository browser.