[490] | 1 | package dcworms.schedframe.scheduling; |
---|
[477] | 2 | |
---|
| 3 | |
---|
[481] | 4 | import gridsim.GridSim; |
---|
[493] | 5 | import gridsim.dcworms.DCWormsTags; |
---|
[477] | 6 | |
---|
| 7 | import java.util.ArrayList; |
---|
| 8 | import java.util.HashMap; |
---|
| 9 | import java.util.List; |
---|
| 10 | import java.util.Map; |
---|
| 11 | |
---|
[481] | 12 | import org.apache.commons.lang.ArrayUtils; |
---|
[477] | 13 | import org.joda.time.DateTime; |
---|
| 14 | import org.joda.time.DateTimeUtilsExt; |
---|
| 15 | import org.joda.time.ReadableDuration; |
---|
| 16 | |
---|
[490] | 17 | |
---|
[481] | 18 | import schedframe.resources.computing.ComputingResource; |
---|
| 19 | import schedframe.resources.units.ProcessingElements; |
---|
| 20 | import schedframe.resources.units.StandardResourceUnitName; |
---|
| 21 | import schedframe.scheduling.ResourceHistoryItem; |
---|
[490] | 22 | import schedframe.scheduling.UsedResourcesList; |
---|
[477] | 23 | import schedframe.scheduling.WorkloadUnitHandler; |
---|
| 24 | import schedframe.scheduling.manager.tasks.JobRegistryImpl; |
---|
| 25 | import schedframe.scheduling.tasks.AbstractProcesses; |
---|
| 26 | import schedframe.scheduling.tasks.AbstractProcessesGroup; |
---|
| 27 | import schedframe.scheduling.tasks.Task; |
---|
| 28 | import schedframe.scheduling.tasks.requirements.ResourceParameterName; |
---|
| 29 | |
---|
| 30 | /** |
---|
| 31 | * |
---|
| 32 | * @author Marcin Krystek |
---|
| 33 | * |
---|
| 34 | */ |
---|
[481] | 35 | public class Executable implements ExecTask{ |
---|
[477] | 36 | |
---|
| 37 | protected Task task; |
---|
| 38 | protected String processesSetId; |
---|
| 39 | |
---|
[481] | 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; |
---|
[490] | 50 | protected UsedResourcesList usedResources; |
---|
[481] | 51 | //TO DO consider removing |
---|
[477] | 52 | protected List<String> visitedResources; |
---|
| 53 | |
---|
[481] | 54 | protected double submissionTime; |
---|
| 55 | protected double arrivalTime; |
---|
| 56 | protected double execStartTime ; |
---|
| 57 | protected double totalCompletionTime; |
---|
| 58 | protected double finishTime; |
---|
[477] | 59 | |
---|
| 60 | public Executable(Task t){ |
---|
| 61 | this.task = t; |
---|
[481] | 62 | this.status = DCWormsTags.CREATED; |
---|
| 63 | |
---|
[490] | 64 | this.usedResources = new UsedResourcesList(); |
---|
[477] | 65 | this.visitedResources = new ArrayList<String>(); |
---|
[481] | 66 | init(); |
---|
[477] | 67 | } |
---|
| 68 | |
---|
| 69 | public Executable(Task t, AbstractProcesses procesesSet){ |
---|
| 70 | this.task = t; |
---|
[481] | 71 | this.status = DCWormsTags.CREATED; |
---|
[477] | 72 | this.processesSetId = procesesSet.getId(); |
---|
[481] | 73 | |
---|
[490] | 74 | this.usedResources = new UsedResourcesList(); |
---|
[477] | 75 | this.visitedResources = new ArrayList<String>(); |
---|
[481] | 76 | init(); |
---|
[477] | 77 | } |
---|
| 78 | |
---|
[481] | 79 | protected void init() { |
---|
[540] | 80 | double currentTime = DateTimeUtilsExt.currentTimeMillis() / 1000; |
---|
[481] | 81 | this.submissionTime = currentTime; |
---|
| 82 | this.totalCompletionTime = 0.0; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | public int getUserId() { |
---|
| 86 | return task.getSenderId(); |
---|
[477] | 87 | } |
---|
[481] | 88 | public String getUserDN() { |
---|
| 89 | return task.getUserDN(); |
---|
[477] | 90 | } |
---|
| 91 | |
---|
[481] | 92 | public String getJobId() { |
---|
| 93 | return task.getJobId(); |
---|
[477] | 94 | } |
---|
[481] | 95 | |
---|
| 96 | public String getTaskId(){ |
---|
| 97 | return this.task.getId(); |
---|
| 98 | } |
---|
| 99 | |
---|
[477] | 100 | public String getId() { |
---|
| 101 | if(processesSetId == null) |
---|
| 102 | return task.getId(); |
---|
| 103 | else |
---|
| 104 | return task.getId() + "_" + processesSetId; |
---|
| 105 | } |
---|
[481] | 106 | |
---|
| 107 | public String getProcessesId(){ |
---|
| 108 | return this.processesSetId; |
---|
[477] | 109 | } |
---|
[481] | 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 | |
---|
[477] | 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 | |
---|
[481] | 140 | public long getLength() { |
---|
| 141 | return task.getLength(); |
---|
[477] | 142 | } |
---|
[481] | 143 | |
---|
| 144 | public int getStatus() { |
---|
| 145 | return status; |
---|
[477] | 146 | } |
---|
| 147 | |
---|
[481] | 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 | |
---|
[490] | 160 | if (newStatus < DCWormsTags.CREATED || newStatus > DCWormsTags.FAILED_RESOURCE_UNAVAILABLE) { |
---|
| 161 | throw new Exception("Executable.setStatuts() : Error - " + |
---|
| 162 | "Invalid integer range for Execiutable status."); |
---|
[481] | 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 | |
---|
[490] | 177 | if (prevStatus == DCWormsTags.INEXEC) { |
---|
[481] | 178 | if (status == DCWormsTags.CANCELED || status == DCWormsTags.PAUSED || |
---|
[490] | 179 | status == DCWormsTags.SUCCESS) { |
---|
[481] | 180 | totalCompletionTime += (currentTime - execStartTime); |
---|
| 181 | } |
---|
| 182 | } |
---|
| 183 | |
---|
[490] | 184 | if (prevStatus == DCWormsTags.RESUMED && status == DCWormsTags.SUCCESS) { |
---|
[481] | 185 | totalCompletionTime += (currentTime - execStartTime); |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | if (status == DCWormsTags.INEXEC || |
---|
[490] | 189 | (prevStatus == DCWormsTags.PAUSED && status == DCWormsTags.RESUMED) ) { |
---|
[481] | 190 | execStartTime = currentTime; |
---|
[490] | 191 | |
---|
[481] | 192 | ProcessingElements pes = (ProcessingElements) getUsedResources().getLast().getResourceUnits().get(StandardResourceUnitName.PE); |
---|
| 193 | for (ComputingResource resource : pes) { |
---|
| 194 | |
---|
[490] | 195 | trackResource(resource.getName()); |
---|
| 196 | |
---|
[481] | 197 | ComputingResource parent = resource.getParent(); |
---|
| 198 | List<String> visitedResource = getVisitedResources(); |
---|
[490] | 199 | String [] visitedResourcesArray = visitedResource.toArray(new String[visitedResource.size()]); |
---|
| 200 | while (parent != null && !ArrayUtils.contains(visitedResourcesArray, parent.getName())) { |
---|
| 201 | trackResource(parent.getName()); |
---|
[481] | 202 | parent = parent.getParent(); |
---|
| 203 | } |
---|
| 204 | } |
---|
| 205 | } |
---|
[477] | 206 | } |
---|
| 207 | |
---|
| 208 | public void addSpecificResource(ResourceParameterName resourceName, Object value){ |
---|
[481] | 209 | if(this.specificResources == null) |
---|
| 210 | this.specificResources = new HashMap<ResourceParameterName, Object>(); |
---|
[477] | 211 | |
---|
[481] | 212 | this.specificResources.put(resourceName, value); |
---|
[477] | 213 | } |
---|
| 214 | |
---|
[481] | 215 | public boolean expectSpecificResource(ResourceParameterName resourceName){ |
---|
| 216 | if(this.specificResources == null) |
---|
| 217 | return false; |
---|
| 218 | |
---|
| 219 | return this.specificResources.containsKey(resourceName); |
---|
[477] | 220 | } |
---|
| 221 | |
---|
[481] | 222 | public Object getExpectedSpecificResource(ResourceParameterName resourceName){ |
---|
| 223 | if(this.specificResources == null) |
---|
| 224 | return null; |
---|
| 225 | |
---|
| 226 | return this.specificResources.get(resourceName); |
---|
[477] | 227 | } |
---|
| 228 | |
---|
[481] | 229 | public void setReservationId(String reservationId){ |
---|
| 230 | this.reservationId = reservationId; |
---|
[477] | 231 | } |
---|
| 232 | |
---|
| 233 | public boolean requireReservation(){ |
---|
| 234 | return (reservationId != null); |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | public String getReservationId(){ |
---|
| 238 | return this.reservationId; |
---|
| 239 | } |
---|
| 240 | |
---|
[481] | 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 | } |
---|
[477] | 252 | |
---|
[490] | 253 | public UsedResourcesList getUsedResources(){ |
---|
[481] | 254 | return this.usedResources; |
---|
[477] | 255 | } |
---|
| 256 | |
---|
[481] | 257 | public void setSchedulerName(int resourceId) |
---|
| 258 | { |
---|
| 259 | this.schedName = GridSim.getEntityName(resourceId); |
---|
| 260 | } |
---|
[477] | 261 | |
---|
[481] | 262 | public String getSchedulerName() |
---|
| 263 | { |
---|
| 264 | return schedName; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | public int getEstimatedDuration(){ |
---|
| 268 | return this.estimatedDuration; |
---|
[477] | 269 | } |
---|
| 270 | |
---|
[481] | 271 | public void setEstimatedDuration(int value){ |
---|
| 272 | this.estimatedDuration = value; |
---|
| 273 | } |
---|
[477] | 274 | |
---|
[481] | 275 | public boolean isRegistered() { |
---|
| 276 | return task.isRegistered(); |
---|
| 277 | } |
---|
[477] | 278 | |
---|
[481] | 279 | public void register(JobRegistryImpl jobRegistry) { |
---|
| 280 | task.register(jobRegistry); |
---|
[477] | 281 | } |
---|
| 282 | |
---|
[490] | 283 | public void trackResource(String resName){ |
---|
[481] | 284 | visitedResources.add(resName); |
---|
[477] | 285 | } |
---|
| 286 | |
---|
[481] | 287 | public List<String> getVisitedResources(){ |
---|
| 288 | return visitedResources; |
---|
[477] | 289 | } |
---|
| 290 | |
---|
[481] | 291 | public ReadableDuration getExpectedDuration() throws NoSuchFieldException { |
---|
| 292 | return task.getExpectedDuration(); |
---|
[477] | 293 | } |
---|
[481] | 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 | } |
---|
[477] | 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 | |
---|
[481] | 313 | public DateTime getExecutionEndTime() throws NoSuchFieldException { |
---|
| 314 | return task.getExecutionEndTime(); |
---|
| 315 | } |
---|
[477] | 316 | |
---|
[481] | 317 | public DateTime getExecutionStartTime() throws NoSuchFieldException { |
---|
| 318 | return task.getExecutionStartTime(); |
---|
| 319 | } |
---|
[477] | 320 | |
---|
[481] | 321 | public DateTime getSubmissionTimeToBroker() { |
---|
| 322 | return task.getSubmissionTimeToBroker(); |
---|
| 323 | } |
---|
[477] | 324 | |
---|
[481] | 325 | public long getWorkloadLogWaitTime() { |
---|
| 326 | return task.getWorkloadLogWaitTime(); |
---|
| 327 | } |
---|
| 328 | |
---|
| 329 | public double getExecStartTime() { |
---|
| 330 | return execStartTime; |
---|
[477] | 331 | } |
---|
| 332 | |
---|
[481] | 333 | public double getFinishTime() { |
---|
| 334 | return finishTime; |
---|
| 335 | } |
---|
[477] | 336 | |
---|
[481] | 337 | public double getSubmissionTime() { |
---|
| 338 | return submissionTime; |
---|
| 339 | } |
---|
| 340 | |
---|
[676] | 341 | public double getWaitingTime() { |
---|
[481] | 342 | return execStartTime - submissionTime; |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | public void finalizeExecutable(){ |
---|
[490] | 346 | try { |
---|
| 347 | setStatus(DCWormsTags.SUCCESS); |
---|
| 348 | } catch (Exception e) { |
---|
| 349 | // TODO Auto-generated catch block |
---|
| 350 | e.printStackTrace(); |
---|
| 351 | } |
---|
[481] | 352 | } |
---|
| 353 | |
---|
[477] | 354 | public void accept(WorkloadUnitHandler wuh) { |
---|
| 355 | wuh.handleExecutable(this); |
---|
| 356 | } |
---|
[481] | 357 | |
---|
[676] | 358 | /*public boolean equals(Object obj){ |
---|
[481] | 359 | if(obj instanceof Executable){ |
---|
| 360 | Executable t = (Executable) obj; |
---|
[676] | 361 | return getId().equals(t.getId()) && getJobId().equals(t.getJobId()); |
---|
[481] | 362 | } |
---|
| 363 | return false; |
---|
[676] | 364 | }*/ |
---|
[481] | 365 | |
---|
[477] | 366 | } |
---|