1 | package dcworms.schedframe.scheduling; |
---|
2 | |
---|
3 | |
---|
4 | import gridsim.GridSim; |
---|
5 | import gridsim.dcworms.DCWormsTags; |
---|
6 | |
---|
7 | import java.util.ArrayList; |
---|
8 | import java.util.HashMap; |
---|
9 | import java.util.List; |
---|
10 | import java.util.Map; |
---|
11 | |
---|
12 | import org.apache.commons.lang.ArrayUtils; |
---|
13 | import org.joda.time.DateTime; |
---|
14 | import org.joda.time.DateTimeUtilsExt; |
---|
15 | import org.joda.time.ReadableDuration; |
---|
16 | |
---|
17 | import qcg.shared.constants.BrokerConstants; |
---|
18 | |
---|
19 | import schedframe.resources.computing.ComputingResource; |
---|
20 | import schedframe.resources.units.ProcessingElements; |
---|
21 | import schedframe.resources.units.StandardResourceUnitName; |
---|
22 | import schedframe.scheduling.ResourceHistoryItem; |
---|
23 | import schedframe.scheduling.UsedResourcesList; |
---|
24 | import schedframe.scheduling.WorkloadUnitHandler; |
---|
25 | import schedframe.scheduling.manager.tasks.JobRegistryImpl; |
---|
26 | import schedframe.scheduling.tasks.AbstractProcesses; |
---|
27 | import schedframe.scheduling.tasks.AbstractProcessesGroup; |
---|
28 | import schedframe.scheduling.tasks.Task; |
---|
29 | import schedframe.scheduling.tasks.phases.ResourceConsumption; |
---|
30 | import schedframe.scheduling.tasks.phases.ResourceConsumptionProfile; |
---|
31 | import schedframe.scheduling.tasks.requirements.ResourceParameterName; |
---|
32 | |
---|
33 | /** |
---|
34 | * |
---|
35 | * @author Marcin Krystek |
---|
36 | * |
---|
37 | */ |
---|
38 | public 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 | } else if(status == DCWormsTags.QUEUED){ |
---|
172 | task.setStatus((int) BrokerConstants.TASK_STATUS_QUEUED); |
---|
173 | } |
---|
174 | |
---|
175 | double currentTime = DateTimeUtilsExt.currentTimeMillis() / 1000; // time in seconds |
---|
176 | |
---|
177 | |
---|
178 | if (newStatus == DCWormsTags.SUCCESS || newStatus == DCWormsTags.CANCELED) { |
---|
179 | finishTime = DateTimeUtilsExt.currentTimeMillis() / 1000; |
---|
180 | } |
---|
181 | |
---|
182 | if(newStatus == DCWormsTags.SUBMITTED){ |
---|
183 | arrivalTime = GridSim.clock(); |
---|
184 | } |
---|
185 | |
---|
186 | if (prevStatus == DCWormsTags.INEXEC) { |
---|
187 | if (status == DCWormsTags.CANCELED || status == DCWormsTags.PAUSED || |
---|
188 | status == DCWormsTags.SUCCESS) { |
---|
189 | totalCompletionTime += (currentTime - execStartTime); |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | if (prevStatus == DCWormsTags.RESUMED && status == DCWormsTags.SUCCESS) { |
---|
194 | totalCompletionTime += (currentTime - execStartTime); |
---|
195 | } |
---|
196 | |
---|
197 | if (status == DCWormsTags.INEXEC || |
---|
198 | (prevStatus == DCWormsTags.PAUSED && status == DCWormsTags.RESUMED) ) { |
---|
199 | execStartTime = currentTime; |
---|
200 | |
---|
201 | ProcessingElements pes = (ProcessingElements) getUsedResources().getLast().getResourceUnits().get(StandardResourceUnitName.PE); |
---|
202 | for (ComputingResource resource : pes) { |
---|
203 | |
---|
204 | trackResource(resource.getName()); |
---|
205 | |
---|
206 | ComputingResource parent = resource.getParent(); |
---|
207 | List<String> visitedResource = getVisitedResources(); |
---|
208 | String [] visitedResourcesArray = visitedResource.toArray(new String[visitedResource.size()]); |
---|
209 | while (parent != null && !ArrayUtils.contains(visitedResourcesArray, parent.getName())) { |
---|
210 | trackResource(parent.getName()); |
---|
211 | parent = parent.getParent(); |
---|
212 | } |
---|
213 | } |
---|
214 | } |
---|
215 | |
---|
216 | if(status == DCWormsTags.NEW_EXEC_PHASE){ |
---|
217 | if(prevStatus == DCWormsTags.INEXEC){ |
---|
218 | status = DCWormsTags.INEXEC; |
---|
219 | setCompletionPercentage(0); |
---|
220 | task.getResourceConsumptionProfile().setCurrentPhase(task.getResourceConsumptionProfile().getCurrentPhase() + 1); |
---|
221 | |
---|
222 | DateTime currentDateTime = new DateTime(); |
---|
223 | |
---|
224 | if(getUsedResources().getLast().getTimeStamp().getMillis() == currentDateTime.getMillis()){ |
---|
225 | return; |
---|
226 | } |
---|
227 | ResourceHistoryItem resHistItem = new ResourceHistoryItem(getUsedResources().getLast().getResourceUnits(), currentDateTime); |
---|
228 | addUsedResources(resHistItem); |
---|
229 | } |
---|
230 | } |
---|
231 | } |
---|
232 | |
---|
233 | public void addSpecificResource(ResourceParameterName resourceName, Object value){ |
---|
234 | if(this.specificResources == null) |
---|
235 | this.specificResources = new HashMap<ResourceParameterName, Object>(); |
---|
236 | this.specificResources.put(resourceName, value); |
---|
237 | } |
---|
238 | |
---|
239 | public boolean expectSpecificResource(ResourceParameterName resourceName){ |
---|
240 | if(this.specificResources == null) |
---|
241 | return false; |
---|
242 | return this.specificResources.containsKey(resourceName); |
---|
243 | } |
---|
244 | |
---|
245 | public Object getExpectedSpecificResource(ResourceParameterName resourceName){ |
---|
246 | if(this.specificResources == null) |
---|
247 | return null; |
---|
248 | |
---|
249 | return this.specificResources.get(resourceName); |
---|
250 | } |
---|
251 | |
---|
252 | public void setReservationId(String reservationId){ |
---|
253 | this.reservationId = reservationId; |
---|
254 | } |
---|
255 | |
---|
256 | public boolean requireReservation(){ |
---|
257 | return (reservationId != null); |
---|
258 | } |
---|
259 | |
---|
260 | public String getReservationId(){ |
---|
261 | return this.reservationId; |
---|
262 | } |
---|
263 | |
---|
264 | public double getCompletionPercentage() { |
---|
265 | return completionPercentage; |
---|
266 | } |
---|
267 | |
---|
268 | public void setCompletionPercentage(double completionPercentage) { |
---|
269 | this.completionPercentage = completionPercentage; |
---|
270 | } |
---|
271 | |
---|
272 | public void addUsedResources(ResourceHistoryItem usedResources){ |
---|
273 | this.usedResources.add(usedResources); |
---|
274 | } |
---|
275 | |
---|
276 | public UsedResourcesList getUsedResources(){ |
---|
277 | return this.usedResources; |
---|
278 | } |
---|
279 | |
---|
280 | public void setSchedulerName(int resourceId){ |
---|
281 | this.schedName = GridSim.getEntityName(resourceId); |
---|
282 | } |
---|
283 | |
---|
284 | public String getSchedulerName(){ |
---|
285 | return schedName; |
---|
286 | } |
---|
287 | |
---|
288 | public int getEstimatedDuration(){ |
---|
289 | return this.estimatedDuration; |
---|
290 | } |
---|
291 | |
---|
292 | public void setEstimatedDuration(int value){ |
---|
293 | this.estimatedDuration = value; |
---|
294 | } |
---|
295 | |
---|
296 | public boolean isRegistered() { |
---|
297 | return task.isRegistered(); |
---|
298 | } |
---|
299 | |
---|
300 | public void register(JobRegistryImpl jobRegistry) { |
---|
301 | task.register(jobRegistry); |
---|
302 | } |
---|
303 | |
---|
304 | public void trackResource(String resName){ |
---|
305 | visitedResources.add(resName); |
---|
306 | } |
---|
307 | |
---|
308 | public List<String> getVisitedResources(){ |
---|
309 | return visitedResources; |
---|
310 | } |
---|
311 | |
---|
312 | public ReadableDuration getExpectedDuration() throws NoSuchFieldException { |
---|
313 | return task.getExpectedDuration(); |
---|
314 | } |
---|
315 | |
---|
316 | public double getParameterDoubleValue(ResourceParameterName parameterName) |
---|
317 | throws NoSuchFieldException, IllegalArgumentException { |
---|
318 | return task.getParameterDoubleValue(parameterName); |
---|
319 | } |
---|
320 | |
---|
321 | public String getParameterStringValue(ResourceParameterName parameterName) |
---|
322 | throws NoSuchFieldException, IllegalArgumentException { |
---|
323 | return task.getParameterStringValue(parameterName); |
---|
324 | } |
---|
325 | |
---|
326 | public double getCpuCntRequest() throws NoSuchFieldException{ |
---|
327 | return getParameterDoubleValue(ResourceParameterName.CPUCOUNT); |
---|
328 | } |
---|
329 | |
---|
330 | public double getMemoryRequest() throws NoSuchFieldException{ |
---|
331 | return getParameterDoubleValue(ResourceParameterName.MEMORY); |
---|
332 | } |
---|
333 | |
---|
334 | public DateTime getExecutionEndTime() throws NoSuchFieldException { |
---|
335 | return task.getExecutionEndTime(); |
---|
336 | } |
---|
337 | |
---|
338 | public DateTime getExecutionStartTime() throws NoSuchFieldException { |
---|
339 | return task.getExecutionStartTime(); |
---|
340 | } |
---|
341 | |
---|
342 | public DateTime getSubmissionTimeToBroker() { |
---|
343 | return task.getSubmissionTimeToBroker(); |
---|
344 | } |
---|
345 | |
---|
346 | public long getWorkloadLogWaitTime() { |
---|
347 | return task.getWorkloadLogWaitTime(); |
---|
348 | } |
---|
349 | |
---|
350 | public double getExecStartTime() { |
---|
351 | return execStartTime; |
---|
352 | } |
---|
353 | |
---|
354 | public double getFinishTime() { |
---|
355 | return finishTime; |
---|
356 | } |
---|
357 | |
---|
358 | public double getSubmissionTime() { |
---|
359 | return submissionTime; |
---|
360 | } |
---|
361 | |
---|
362 | public double getWaitingTime() { |
---|
363 | return execStartTime - submissionTime; |
---|
364 | } |
---|
365 | |
---|
366 | public void finalizeExecutable(){ |
---|
367 | try { |
---|
368 | setStatus(DCWormsTags.SUCCESS); |
---|
369 | } catch (Exception e) { |
---|
370 | // TODO Auto-generated catch block |
---|
371 | e.printStackTrace(); |
---|
372 | } |
---|
373 | } |
---|
374 | |
---|
375 | public void accept(WorkloadUnitHandler wuh) { |
---|
376 | wuh.handleExecutable(this); |
---|
377 | } |
---|
378 | |
---|
379 | /*public boolean equals(Object obj){ |
---|
380 | if(obj instanceof Executable){ |
---|
381 | Executable t = (Executable) obj; |
---|
382 | return getId().equals(t.getId()) && getJobId().equals(t.getJobId()); |
---|
383 | } |
---|
384 | return false; |
---|
385 | }*/ |
---|
386 | |
---|
387 | public ResourceConsumptionProfile getResourceConsumptionProfile(){ |
---|
388 | return task.getResourceConsumptionProfile(); |
---|
389 | } |
---|
390 | |
---|
391 | public ResourceConsumption getCurrentResourceConsumption(){ |
---|
392 | return task.getResourceConsumptionProfile().getCurrentResourceConsumption(); |
---|
393 | } |
---|
394 | |
---|
395 | public String getApplicationName(){ |
---|
396 | return task.getApplicationName(); |
---|
397 | } |
---|
398 | |
---|
399 | } |
---|