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

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