[104] | 1 | package test.rewolucja.scheduling.implementation; |
---|
| 2 | |
---|
| 3 | import java.util.HashMap; |
---|
| 4 | import java.util.Map; |
---|
| 5 | |
---|
| 6 | import org.apache.commons.logging.Log; |
---|
| 7 | import org.apache.commons.logging.LogFactory; |
---|
| 8 | import org.joda.time.DateTimeUtilsExt; |
---|
| 9 | |
---|
| 10 | import schedframe.scheduling.JobInterface; |
---|
| 11 | import schedframe.scheduling.TaskInterface; |
---|
| 12 | import schedframe.scheduling.events.SchedulingEvent; |
---|
| 13 | import schedframe.scheduling.events.SchedulingEventType; |
---|
| 14 | import schedframe.scheduling.plugin.SchedulingPluginConfiguration; |
---|
| 15 | import schedframe.scheduling.plugin.estimation.ExecTimeEstimationPlugin; |
---|
| 16 | import schedframe.scheduling.plugin.grid.ModuleList; |
---|
| 17 | import schedframe.scheduling.plugin.local.ResourceAllocationInterface; |
---|
| 18 | import schedframe.scheduling.utils.ResourceParameterName; |
---|
| 19 | import test.rewolucja.GSSIMJobInterface; |
---|
| 20 | import test.rewolucja.resources.description.ExecResourceDescription; |
---|
| 21 | import test.rewolucja.resources.logical.LogicalResource; |
---|
| 22 | import test.rewolucja.resources.manager.factory.ResourceManagerFactory; |
---|
| 23 | import test.rewolucja.resources.manager.interfaces.ResourceManagerInterface; |
---|
| 24 | import test.rewolucja.scheduling.JobRegistry; |
---|
| 25 | import test.rewolucja.scheduling.JobRegistryInterface; |
---|
| 26 | import test.rewolucja.scheduling.interfaces.SchedulingPlugin; |
---|
| 27 | import test.rewolucja.scheduling.plan.AllocationInterfaceNew; |
---|
| 28 | import test.rewolucja.scheduling.plan.SchedulingPlanInterfaceNew; |
---|
| 29 | import test.rewolucja.scheduling.queue.GSSIMQueue; |
---|
| 30 | import test.rewolucja.scheduling.queue.QueueList; |
---|
| 31 | import eduni.simjava.Sim_event; |
---|
| 32 | import gridsim.GridSim; |
---|
| 33 | import gridsim.GridSimTags; |
---|
| 34 | import gridsim.Gridlet; |
---|
| 35 | import gridsim.IO_data; |
---|
| 36 | import gridsim.gssim.GssimTags; |
---|
| 37 | import gridsim.gssim.SubmittedTask; |
---|
| 38 | import gssim.schedframe.scheduling.AbstractExecutable; |
---|
| 39 | |
---|
| 40 | public abstract class ManagementSystem { |
---|
| 41 | |
---|
| 42 | private Log log = LogFactory.getLog(ManagementSystem.class); |
---|
| 43 | |
---|
| 44 | protected String name; |
---|
| 45 | |
---|
| 46 | protected ResourceManagerInterface resourceManager; |
---|
| 47 | |
---|
| 48 | protected QueueList queues; |
---|
| 49 | |
---|
| 50 | protected SchedulingPlugin schedulingPlugin; |
---|
| 51 | |
---|
| 52 | protected ExecTimeEstimationPlugin forecastFinishTimePlugin; |
---|
| 53 | |
---|
| 54 | protected ModuleList moduleList; |
---|
| 55 | |
---|
| 56 | protected JobRegistry jobRegistry; |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | public ManagementSystem(String providerId, String entityName, String schedulingPluginClassName, |
---|
| 60 | ExecTimeEstimationPlugin execTimeEstimationPlugin, ExecResourceDescription resourceDescription) throws Exception { |
---|
| 61 | name = entityName + "@" + providerId; |
---|
| 62 | |
---|
| 63 | jobRegistry = new JobRegistry(name); |
---|
| 64 | forecastFinishTimePlugin = execTimeEstimationPlugin; |
---|
| 65 | if (forecastFinishTimePlugin == null) { |
---|
| 66 | throw new Exception("Can not create execution time estimation plugin instance."); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | if(resourceDescription == null){ |
---|
| 70 | queues = createAccessQueues(); |
---|
| 71 | } |
---|
| 72 | else{ |
---|
| 73 | queues = resourceDescription.getAccessQueues(); |
---|
| 74 | // set_stat(GssimConstants.getResourcesStatisticsObject(queues.size())); |
---|
| 75 | for (int i = 0; i < queues.size(); i++) { |
---|
| 76 | GSSIMQueue q = queues.get(i); |
---|
| 77 | /*if (q instanceof AbstractStatsSupportingQueue<?>) { |
---|
| 78 | AbstractStatsSupportingQueue<?> queue = (AbstractStatsSupportingQueue<?>) q; |
---|
| 79 | // queue.setStats(this.get_stat(), |
---|
| 80 | // GssimConstants.TASKS_QUEUE_LENGTH_MEASURE_NAME+"_"+Integer.toString(i)); |
---|
| 81 | }*/ |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | public void processEvent(Sim_event ev) { |
---|
| 89 | processOtherEvent(ev); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | protected void processOtherEvent(Sim_event ev) { |
---|
| 93 | if (ev == null) { |
---|
| 94 | System.out.println(name + ".processOtherEvent(): " + "Error - an event is null."); |
---|
| 95 | return; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | log.error(name + ".processOtherEvent(): Unable to " |
---|
| 99 | + "handle request from an event with a tag number " + ev.get_tag()); |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | public String getName() { |
---|
| 103 | return name; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | public SchedulingPluginConfiguration getPluginConfiguration() { |
---|
| 107 | return (SchedulingPluginConfiguration)schedulingPlugin.getConfiguration(); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | public ResourceManagerInterface getResourceManager() { |
---|
| 111 | if (resourceManager instanceof ResourceManagerInterface) |
---|
| 112 | return (ResourceManagerInterface) resourceManager; |
---|
| 113 | else |
---|
| 114 | return null; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | public ResourceAllocationInterface getAllocationManager() { |
---|
| 118 | if (resourceManager instanceof ResourceAllocationInterface) |
---|
| 119 | return (ResourceAllocationInterface) resourceManager; |
---|
| 120 | else |
---|
| 121 | return null; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | protected JobRegistryInterface getJobRegistry(){ |
---|
| 125 | return jobRegistry; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | public abstract boolean pluginSupportsEvent(int eventType); |
---|
| 129 | |
---|
| 130 | public abstract void notifySubmittedJob(GSSIMJobInterface<?> gssimJob, boolean ack); |
---|
| 131 | |
---|
| 132 | public abstract void notifyCanceledJob(GSSIMJobInterface<?> gssimJob); |
---|
| 133 | |
---|
| 134 | public abstract void notifyReturnedJob(GSSIMJobInterface<?> gssimJob); |
---|
| 135 | |
---|
| 136 | protected abstract void executeSchedulingPlan(SchedulingPlanInterfaceNew decision); |
---|
| 137 | |
---|
| 138 | protected void schedule(SchedulingEvent schedulingEvent) { |
---|
| 139 | |
---|
| 140 | try { |
---|
| 141 | SchedulingPlanInterfaceNew decision = schedulingPlugin.schedule( |
---|
| 142 | schedulingEvent, queues, getJobRegistry(), getResourceManager(), moduleList); |
---|
| 143 | if (decision == null) |
---|
| 144 | return; |
---|
| 145 | |
---|
| 146 | executeSchedulingPlan(decision); |
---|
| 147 | |
---|
| 148 | } catch (Exception e) { |
---|
| 149 | e.printStackTrace(); |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | //POPRAWIC |
---|
| 154 | protected void submitJob(GSSIMJobInterface<?> gssimJob, AllocationInterfaceNew allocation) { |
---|
| 155 | String providerName = allocation.getProviderName(); |
---|
| 156 | if (providerName == null) { |
---|
| 157 | return; |
---|
| 158 | } |
---|
| 159 | SubmittedTask subTask = (SubmittedTask) gssimJob; |
---|
| 160 | logicalResource.send(providerName, GridSimTags.SCHEDULE_NOW, GridSimTags.GRIDLET_SUBMIT, subTask.getGridlet()); |
---|
| 161 | //logicalResource.send(providerName, GridSimTags.SCHEDULE_NOW, GridSimTags.GRIDLET_SUBMIT, gssimJob); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | protected boolean sendCancelJob(int tag, AbstractExecutable task, int executableId, int destId) { |
---|
| 165 | |
---|
| 166 | if (tag != GridSimTags.GRIDLET_CANCEL) { |
---|
| 167 | return false; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | long taskSize = 0; |
---|
| 171 | if (task != null) { |
---|
| 172 | taskSize = task.getGridletOutputSize(); |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | // if no Gridlet found, then create a new Gridlet but set its status |
---|
| 176 | // to FAILED. Then, most importantly, set the resource parameters |
---|
| 177 | // because the user will search/filter based on a resource ID. |
---|
| 178 | else if (task == null) { |
---|
| 179 | try { |
---|
| 180 | taskSize = 100; |
---|
| 181 | task = jobRegistry.getTaskExecutable(executableId); |
---|
| 182 | task.setGridletStatus(Gridlet.FAILED); |
---|
| 183 | int cost = resourceManager.getResourceCharacteristic().getResourceUnits() != null ? resourceManager |
---|
| 184 | .getResourceCharacteristic().getResourceUnits().get(ResourceParameterName.COST).get(0).getAmount() |
---|
| 185 | : 1; |
---|
| 186 | task.setResourceParameter(logicalResource.get_id(), cost); |
---|
| 187 | } catch (Exception e) { |
---|
| 188 | // empty ... |
---|
| 189 | } |
---|
| 190 | } |
---|
| 191 | logicalResource.send(logicalResource.getOutputPort(), GridSimTags.SCHEDULE_NOW, tag, new IO_data(task, taskSize, destId)); |
---|
| 192 | |
---|
| 193 | return true; |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | protected boolean sendFinishJob(AbstractExecutable task) { |
---|
| 197 | IO_data obj = new IO_data(task, task.getGridletOutputSize(), GridSim.getEntityId(logicalResource.getParent().get_name())); |
---|
| 198 | logicalResource.send(logicalResource.getOutputPort(), 0.0, GridSimTags.GRIDLET_RETURN, obj); |
---|
| 199 | return true; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | protected void sendJobReadyEvent(GSSIMJobInterface<?> gssimJob) { |
---|
| 203 | |
---|
| 204 | if (gssimJob instanceof JobInterface) { |
---|
| 205 | logicalResource.sendInternal(Long.valueOf(0).doubleValue(), GssimTags.TASK_READY_FOR_EXECUTION, |
---|
| 206 | gssimJob); |
---|
| 207 | return; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | AbstractExecutable executable = (AbstractExecutable) gssimJob; |
---|
| 211 | long delay = 0; |
---|
| 212 | try { |
---|
| 213 | long expectedStartTime = executable.getExecutionStartTime().getMillis() / 1000; |
---|
| 214 | long currentTime = DateTimeUtilsExt.currentTimeMillis() / 1000; |
---|
| 215 | delay = expectedStartTime - currentTime; |
---|
| 216 | if (delay < 0) |
---|
| 217 | delay = 0; |
---|
| 218 | } catch (NoSuchFieldException e) { |
---|
| 219 | delay = 0; |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | logicalResource.sendInternal(Long.valueOf(delay).doubleValue(), GssimTags.TASK_READY_FOR_EXECUTION, |
---|
| 223 | gssimJob); |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | protected void sendTimerEvent() { |
---|
| 227 | SchedulingPluginConfiguration pluginConfig = (SchedulingPluginConfiguration)schedulingPlugin.getConfiguration(); |
---|
| 228 | if (pluginConfig != null) { |
---|
| 229 | Map<SchedulingEventType, Object> events = pluginConfig.getServedEvents(); |
---|
| 230 | if (events != null) { |
---|
| 231 | Object obj = events.get(SchedulingEventType.TIMER); |
---|
| 232 | if (obj != null) { |
---|
| 233 | int delay = (Integer) obj; |
---|
| 234 | logicalResource.sendInternal(delay, GssimTags.TIMER, null); |
---|
| 235 | } |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | protected boolean removeFromQueue(TaskInterface<?> task) { |
---|
| 241 | for(GSSIMQueue queue : queues){ |
---|
| 242 | if(queue.contains(task)){ |
---|
| 243 | queue.remove(task); |
---|
| 244 | return true; |
---|
| 245 | } |
---|
| 246 | } |
---|
| 247 | return false; |
---|
| 248 | } |
---|
| 249 | private QueueList createAccessQueues(){ |
---|
| 250 | QueueList queues = new QueueList(); |
---|
| 251 | GSSIMQueue queue = new GSSIMQueue(false); |
---|
| 252 | queues.add(queue); |
---|
| 253 | return queues; |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | public Map<String, Integer> getQueuesSize() { |
---|
| 257 | Map<String, Integer> queue_size = new HashMap<String, Integer>(); |
---|
| 258 | for (GSSIMQueue queue : queues) { |
---|
| 259 | queue_size.put(queue.getName(), queue.size()); |
---|
| 260 | } |
---|
| 261 | return queue_size; |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | public void init(LogicalResource logRes) { |
---|
| 265 | logicalResource = logRes; |
---|
| 266 | resourceManager = ResourceManagerFactory.createResourceManager(logicalResource); |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | |
---|
| 270 | protected LogicalResource logicalResource; |
---|
| 271 | |
---|
| 272 | |
---|
| 273 | public LogicalResource getLogicalResource() { |
---|
| 274 | return logicalResource; |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | } |
---|