[104] | 1 | package test.rewolucja.scheduling; |
---|
| 2 | |
---|
| 3 | import gridsim.Gridlet; |
---|
| 4 | import gridsim.gssim.GssimConstants; |
---|
| 5 | import gridsim.gssim.ResourceHistoryItem; |
---|
| 6 | import gridsim.gssim.SubmittedTask; |
---|
| 7 | import gssim.schedframe.scheduling.AbstractExecutable; |
---|
| 8 | import gssim.schedframe.scheduling.ExecTaskInterface; |
---|
| 9 | import gssim.schedframe.scheduling.Executable; |
---|
| 10 | import gssim.schedframe.scheduling.queues.TaskQueue; |
---|
| 11 | |
---|
| 12 | import java.util.ArrayList; |
---|
| 13 | import java.util.Collections; |
---|
| 14 | import java.util.HashMap; |
---|
| 15 | import java.util.List; |
---|
| 16 | import java.util.Map; |
---|
| 17 | |
---|
| 18 | import org.apache.commons.logging.Log; |
---|
| 19 | import org.apache.commons.logging.LogFactory; |
---|
| 20 | import org.joda.time.DateTime; |
---|
| 21 | |
---|
| 22 | import schedframe.resources.units.ResourceUnit; |
---|
| 23 | import schedframe.scheduling.AbstractProcesses; |
---|
| 24 | import schedframe.scheduling.Job; |
---|
| 25 | import schedframe.scheduling.JobInterface; |
---|
| 26 | import schedframe.scheduling.Task; |
---|
| 27 | import schedframe.scheduling.TaskInterface; |
---|
| 28 | import schedframe.scheduling.utils.ResourceParameterName; |
---|
| 29 | import test.rewolucja.resources.ProcessingElements; |
---|
| 30 | import test.rewolucja.resources.physical.base.ComputingResource; |
---|
| 31 | import test.rewolucja.scheduling.plan.AllocationInterfaceNew; |
---|
| 32 | |
---|
| 33 | public class JobRegistry extends AbstractJobRegistry /*implements Cloneable*/ { |
---|
| 34 | |
---|
| 35 | private static final long serialVersionUID = 8030555906990767342L; |
---|
| 36 | |
---|
| 37 | private static Log log = LogFactory.getLog(JobRegistry.class); |
---|
| 38 | |
---|
| 39 | private String context; |
---|
| 40 | |
---|
| 41 | protected static final List<ExecTaskInterface> submittedTasks = Collections.synchronizedList(new ArrayList<ExecTaskInterface>());; |
---|
| 42 | //protected static final List<ExecTaskInterface> submittedTasks = new CopyOnWriteArrayList<ExecTaskInterface>(); |
---|
| 43 | |
---|
| 44 | public JobRegistry(String context_) { |
---|
| 45 | context = context_; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | /*protected void setContext(String context_) { |
---|
| 49 | context = context_; |
---|
| 50 | }*/ |
---|
| 51 | |
---|
| 52 | public boolean addTask(ExecTaskInterface newTask) { |
---|
| 53 | if(getSubmittedTask(newTask.getJobId(), newTask.getId()) == null) |
---|
| 54 | { |
---|
| 55 | synchronized (submittedTasks) { |
---|
| 56 | submittedTasks.add(newTask); |
---|
| 57 | } |
---|
| 58 | return true; |
---|
| 59 | } |
---|
| 60 | return false; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | public List<ExecTaskInterface> getTasks(int status) { |
---|
| 64 | List<ExecTaskInterface> taskList = new ArrayList<ExecTaskInterface>(); |
---|
| 65 | synchronized (submittedTasks) { |
---|
| 66 | for (ExecTaskInterface task : submittedTasks) { |
---|
| 67 | if (task.getStatus() == status) { |
---|
| 68 | SubmittedTask subTask = (SubmittedTask) task; |
---|
[134] | 69 | if(subTask.getVisitedResources().contains(context)){ |
---|
[104] | 70 | taskList.add(subTask); |
---|
| 71 | } |
---|
[134] | 72 | /*if (subTask.getResPath().contains(context)) { |
---|
| 73 | taskList.add(subTask); |
---|
| 74 | }*/ |
---|
[104] | 75 | } |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | return taskList; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | public List<ExecTaskInterface> getQueuedTasks() { |
---|
| 82 | return getTasks(Gridlet.QUEUED); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | public List<ExecTaskInterface> getRunningTasks() { |
---|
| 86 | return getTasks(Gridlet.INEXEC); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | public List<ExecTaskInterface> getReadyTasks() { |
---|
| 90 | return getTasks(Gridlet.READY); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | public List<ExecTaskInterface> getFinishedTasks() { |
---|
| 94 | return getTasks(Gridlet.SUCCESS); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | |
---|
| 98 | public List<ExecTaskInterface> getAllSubmittedTasks() { |
---|
| 99 | List<ExecTaskInterface> taskList;; |
---|
| 100 | synchronized (submittedTasks) { |
---|
| 101 | taskList = new ArrayList<ExecTaskInterface>(submittedTasks); |
---|
| 102 | } |
---|
| 103 | return taskList; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | public List<SubmittedTask> getSubmittedTasks() { |
---|
| 107 | List<SubmittedTask> taskList = new ArrayList<SubmittedTask>(); |
---|
| 108 | synchronized (submittedTasks) { |
---|
| 109 | for (ExecTaskInterface task : submittedTasks) { |
---|
| 110 | SubmittedTask subTask = (SubmittedTask) task; |
---|
[134] | 111 | if(subTask.getVisitedResources().contains(context)){ |
---|
[104] | 112 | taskList.add(subTask); |
---|
| 113 | } |
---|
[134] | 114 | /*if (subTask.getResPath().contains(context)) { |
---|
| 115 | taskList.add(subTask); |
---|
| 116 | }*/ |
---|
[104] | 117 | } |
---|
| 118 | } |
---|
| 119 | return taskList; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | public SubmittedTask getSubmittedTask(String jobId, String taskId){ |
---|
| 123 | synchronized (submittedTasks) { |
---|
| 124 | for (ExecTaskInterface task : submittedTasks) { |
---|
| 125 | if (task.getJobId().compareTo(jobId) == 0 && task.getId().compareTo(taskId)==0) { |
---|
| 126 | return (SubmittedTask)task; |
---|
| 127 | } |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | return null; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | public List<TaskInterface<?>> getReadyTasks(List<JobInterface<?>> jobsList) { |
---|
| 134 | List<TaskInterface<?>> tasks = new ArrayList<TaskInterface<?>>(); |
---|
| 135 | TaskQueue waitingTasks = new TaskQueue(); |
---|
| 136 | |
---|
| 137 | for(int i = 0; i < jobsList.size(); i++){ |
---|
| 138 | Job newJob = (Job)jobsList.get(i); |
---|
| 139 | waitingTasks.addAll(newJob.getTask()); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | tasks.addAll(waitingTasks.getReadyTasks(this)); |
---|
| 143 | return tasks; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | |
---|
| 147 | |
---|
| 148 | public AbstractExecutable getTaskExecutable(Integer executableId){ |
---|
| 149 | synchronized (submittedTasks) { |
---|
| 150 | for (ExecTaskInterface task : submittedTasks) { |
---|
| 151 | SubmittedTask subTask = (SubmittedTask) task; |
---|
| 152 | AbstractExecutable exec = (AbstractExecutable)subTask.getGridlet(); |
---|
| 153 | if (exec.getGridletID() == executableId) { |
---|
| 154 | return exec; |
---|
| 155 | } |
---|
| 156 | } |
---|
| 157 | } |
---|
| 158 | return null; |
---|
| 159 | } |
---|
| 160 | public List<AbstractExecutable> getJobExecutables(String jobId){ |
---|
| 161 | List<AbstractExecutable> list = new ArrayList<AbstractExecutable>(); |
---|
| 162 | synchronized (submittedTasks) { |
---|
| 163 | for(int i = 0; i < submittedTasks.size(); i++){ |
---|
| 164 | SubmittedTask subTask = (SubmittedTask) submittedTasks.get(i); |
---|
| 165 | AbstractExecutable exec = (AbstractExecutable)subTask.getGridlet(); |
---|
| 166 | |
---|
| 167 | if(exec.getJobId().equals(jobId)) |
---|
| 168 | list.add(exec); |
---|
| 169 | } |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | return list; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | /*public AbstractExecutable getTaskExecutabls(String jobId, String taskId){ |
---|
| 176 | List<AbstractExecutable> list = new ArrayList<AbstractExecutable>(); |
---|
| 177 | synchronized (submittedTasks) { |
---|
| 178 | for(int i = 0; i < size(); i++){ |
---|
| 179 | SubmittedTask subTask = (SubmittedTask) submittedTasks.get(i); |
---|
| 180 | AbstractExecutable exec = (AbstractExecutable)subTask.getGridlet(); |
---|
| 181 | |
---|
| 182 | if(exec.getJobId().equals(jobId) && exec.getId().equals(taskId)) |
---|
| 183 | return exec; |
---|
| 184 | } |
---|
| 185 | } |
---|
| 186 | return null; |
---|
| 187 | }*/ |
---|
| 188 | |
---|
| 189 | |
---|
| 190 | public Executable createExecutable(Task task, AllocationInterfaceNew allocation) { |
---|
| 191 | |
---|
| 192 | String refersTo = allocation.getProcessGroupId(); // null;//allocation.getRefersTo(); |
---|
| 193 | if(refersTo == null) |
---|
| 194 | refersTo = task.getId(); |
---|
| 195 | |
---|
| 196 | Executable exec = null; |
---|
| 197 | |
---|
| 198 | if(refersTo.equals(task.getId())){ |
---|
| 199 | exec = new Executable(task); |
---|
| 200 | } else { |
---|
| 201 | List<AbstractProcesses> processes = task.getProcesses(); |
---|
| 202 | if(processes == null) { |
---|
| 203 | try { |
---|
| 204 | log.error("Allocation: " + allocation.getDocument() + "\nrefers to unknown task or processes set." + |
---|
| 205 | " Set correct value (task id or prcesses set id) for allocation refersTo attribute."); |
---|
| 206 | } catch (Exception e) { |
---|
| 207 | e.printStackTrace(); |
---|
| 208 | } |
---|
| 209 | } |
---|
| 210 | boolean found = false; |
---|
| 211 | for(int j = 0; j < processes.size() && !found; j++){ |
---|
| 212 | AbstractProcesses procesesSet = processes.get(j); |
---|
| 213 | if(refersTo.equals(procesesSet.getId())){ |
---|
| 214 | exec = new Executable(task, procesesSet); |
---|
| 215 | found = true; |
---|
| 216 | } |
---|
| 217 | } |
---|
| 218 | if(!found){ |
---|
| 219 | log.error("Allocation refers to unknown proceses set."); |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | exec.setUserID(task.getSenderId()); |
---|
| 225 | exec.setLength(task.getLength()); |
---|
| 226 | exec.setReservationId(allocation.getReservationId()); |
---|
| 227 | |
---|
| 228 | /*HostInterface<?> host = allocation.getHost(); |
---|
| 229 | ComputingResourceTypeInterface<?> crt = host.getMachineParameters(); |
---|
| 230 | if(crt != null){ |
---|
| 231 | ComputingResourceTypeItemInterface<?> crti = crt.getComputingResourceTypeItem(0); |
---|
| 232 | if(crti != null){ |
---|
| 233 | ParameterPropertyInterface<?> properties[] = crti.getHostParameter().getProperty(); |
---|
| 234 | for(int p = 0; p < properties.length; p++){ |
---|
| 235 | ParameterPropertyInterface<?> property = properties[p]; |
---|
| 236 | if("chosenCPUs".equals(property.getName())){ |
---|
| 237 | Object cpuNames = property.getValue(); |
---|
| 238 | exec.addSpecificResource(ResourceParameterName.FREECPUS, cpuNames); |
---|
| 239 | } |
---|
| 240 | } |
---|
| 241 | } |
---|
| 242 | }*/ |
---|
| 243 | return exec; |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | |
---|
| 247 | /**************************************/ |
---|
| 248 | protected static Map<Integer, Map<String, Object>> history = new HashMap<Integer, Map<String,Object>>(); |
---|
| 249 | |
---|
| 250 | public static Map<Integer, Map<String, Object>> getAllocationHistory(){ |
---|
| 251 | return history; |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | public void saveHistory (SubmittedTask submittedTask, int estimatedTime, Map<ResourceParameterName, ResourceUnit> choosenResources){ |
---|
| 255 | |
---|
[134] | 256 | /* submittedTask.setEstimatedDuration(estimatedTime); |
---|
[104] | 257 | |
---|
| 258 | DateTime currentTime = new DateTime(); |
---|
| 259 | ResourceHistoryItem resHistItem = new ResourceHistoryItem(choosenResources, currentTime); |
---|
[134] | 260 | submittedTask.addUsedResources(resHistItem);*/ |
---|
[104] | 261 | |
---|
[134] | 262 | ResourceHistoryItem resHistItem = submittedTask.getUsedResources().get(submittedTask.getUsedResources().size()-1); |
---|
| 263 | DateTime currentTime = new DateTime(); |
---|
[104] | 264 | Map<String, Object> historyItem = new HashMap<String, Object>(); |
---|
| 265 | List<ResourceHistoryItem> list = new ArrayList<ResourceHistoryItem>(1); |
---|
| 266 | list.add(resHistItem); |
---|
| 267 | historyItem.put(GssimConstants.RESOURCES, list); |
---|
| 268 | historyItem.put(GssimConstants.START_TIME, currentTime); |
---|
| 269 | currentTime = currentTime.plusSeconds(estimatedTime); |
---|
| 270 | historyItem.put(GssimConstants.END_TIME, currentTime); |
---|
| 271 | |
---|
| 272 | history.put(Integer.valueOf(submittedTask.getGridletID()), historyItem); |
---|
[134] | 273 | /*ProcessingElements pes = (ProcessingElements) choosenResources.get(ResourceParameterName.PROCESSINGELEMENTS); |
---|
[104] | 274 | for (ComputingResource resource : pes) { |
---|
[134] | 275 | //submittedTask.addToResPath(resource.getName()); |
---|
| 276 | submittedTask.visitResource(resource.getName()); |
---|
[104] | 277 | ComputingResource parent = resource.getParent(); |
---|
| 278 | while (parent != null && !submittedTask.getResPath().contains(parent.getName() + "_")) { |
---|
| 279 | submittedTask.addToResPath(parent.getName()); |
---|
| 280 | parent = parent.getParent(); |
---|
| 281 | } |
---|
[134] | 282 | while (parent != null && !submittedTask.getVisitedResources().contains(parent.getName() + "_")) { |
---|
| 283 | submittedTask.visitResource(parent.getName()); |
---|
| 284 | parent = parent.getParent(); |
---|
| 285 | } |
---|
| 286 | }*/ |
---|
[104] | 287 | } |
---|
| 288 | |
---|
| 289 | |
---|
| 290 | } |
---|