[477] | 1 | package schedframe.scheduling.policy; |
---|
| 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 | |
---|
[490] | 10 | import dcworms.schedframe.scheduling.ExecTask; |
---|
| 11 | import dcworms.schedframe.scheduling.Executable; |
---|
| 12 | import dcworms.schedframe.scheduling.queues.AbstractStatsSupportingQueue; |
---|
| 13 | |
---|
[477] | 14 | import schedframe.PluginConfiguration; |
---|
| 15 | import schedframe.events.scheduling.SchedulingEventType; |
---|
| 16 | import schedframe.scheduling.Scheduler; |
---|
| 17 | import schedframe.scheduling.WorkloadUnitHandler; |
---|
| 18 | import schedframe.scheduling.manager.resources.ManagedResources; |
---|
| 19 | import schedframe.scheduling.manager.resources.ResourceManager; |
---|
| 20 | import schedframe.scheduling.manager.resources.ResourceManagerFactory; |
---|
[481] | 21 | import schedframe.scheduling.manager.tasks.JobRegistry; |
---|
[477] | 22 | import schedframe.scheduling.manager.tasks.JobRegistryImpl; |
---|
| 23 | import schedframe.scheduling.plan.AllocationInterface; |
---|
| 24 | import schedframe.scheduling.plan.SchedulingPlanInterface; |
---|
| 25 | import schedframe.scheduling.plugin.SchedulingPlugin; |
---|
| 26 | import schedframe.scheduling.plugin.estimation.ExecutionTimeEstimationPlugin; |
---|
| 27 | import schedframe.scheduling.plugin.grid.ModuleList; |
---|
| 28 | import schedframe.scheduling.plugin.local.ResourceAllocationInterface; |
---|
| 29 | import schedframe.scheduling.queue.TaskQueue; |
---|
| 30 | import schedframe.scheduling.queue.TaskQueueList; |
---|
| 31 | import schedframe.scheduling.tasks.Job; |
---|
[481] | 32 | import schedframe.scheduling.tasks.TaskInterface; |
---|
[477] | 33 | import schedframe.scheduling.tasks.WorkloadUnit; |
---|
[481] | 34 | import simulator.DCWormsConstants; |
---|
[477] | 35 | import eduni.simjava.Sim_event; |
---|
| 36 | import gridsim.GridSim; |
---|
| 37 | import gridsim.GridSimTags; |
---|
| 38 | import gridsim.IO_data; |
---|
[493] | 39 | import gridsim.dcworms.DCWormsTags; |
---|
[477] | 40 | |
---|
| 41 | public abstract class AbstractManagementSystem { |
---|
| 42 | |
---|
| 43 | private Log log = LogFactory.getLog(AbstractManagementSystem.class); |
---|
| 44 | |
---|
| 45 | protected String name; |
---|
| 46 | |
---|
[481] | 47 | protected TaskQueueList queues; |
---|
[477] | 48 | protected ResourceManager resourceManager; |
---|
[481] | 49 | protected JobRegistryImpl jobRegistry; |
---|
| 50 | protected ModuleList moduleList; |
---|
| 51 | |
---|
[477] | 52 | protected SchedulingPlugin schedulingPlugin; |
---|
| 53 | protected ExecutionTimeEstimationPlugin execTimeEstimationPlugin; |
---|
| 54 | |
---|
[481] | 55 | protected Scheduler scheduler; |
---|
[477] | 56 | |
---|
| 57 | |
---|
| 58 | public AbstractManagementSystem(String providerId, String entityName, |
---|
| 59 | ExecutionTimeEstimationPlugin execTimeEstPlugin, TaskQueueList queues) { |
---|
| 60 | |
---|
| 61 | this.name = entityName + "@" + providerId; |
---|
| 62 | this.queues = queues; |
---|
| 63 | this.jobRegistry = new JobRegistryImpl(name); |
---|
| 64 | this.execTimeEstimationPlugin = execTimeEstPlugin; |
---|
| 65 | } |
---|
| 66 | |
---|
[481] | 67 | public void init(Scheduler sched, ManagedResources managedResources) { |
---|
| 68 | scheduler = sched; |
---|
| 69 | resourceManager = ResourceManagerFactory.createResourceManager(scheduler, managedResources); |
---|
| 70 | scheduler.set_stat(DCWormsConstants.getResourcesStatisticsObject(queues.size())); |
---|
| 71 | for(int i = 0; i < queues.size(); i++){ |
---|
| 72 | TaskQueue q = queues.get(i); |
---|
| 73 | if(q instanceof AbstractStatsSupportingQueue<?>){ |
---|
| 74 | AbstractStatsSupportingQueue<?> queue = (AbstractStatsSupportingQueue<?>) q; |
---|
| 75 | queue.setStats(scheduler.get_stat(), DCWormsConstants.TASKS_QUEUE_LENGTH_MEASURE_NAME + "_" + Integer.toString(i)); |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | } |
---|
| 79 | |
---|
[477] | 80 | public void processEvent(Sim_event ev) { |
---|
| 81 | processOtherEvent(ev); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | protected void processOtherEvent(Sim_event ev) { |
---|
| 85 | if (ev == null) { |
---|
| 86 | System.out.println(name + ".processOtherEvent(): " + "Error - an event is null."); |
---|
| 87 | return; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | log.error(name + ".processOtherEvent(): Unable to " |
---|
| 91 | + "handle request from an event with a tag number " + ev.get_tag()); |
---|
| 92 | } |
---|
| 93 | |
---|
[481] | 94 | |
---|
[477] | 95 | public String getName() { |
---|
| 96 | return name; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | public ResourceManager getResourceManager() { |
---|
| 100 | if (resourceManager instanceof ResourceManager) |
---|
| 101 | return (ResourceManager) resourceManager; |
---|
| 102 | else |
---|
| 103 | return null; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | public ResourceAllocationInterface getAllocationManager() { |
---|
| 107 | if (resourceManager instanceof ResourceAllocationInterface) |
---|
| 108 | return (ResourceAllocationInterface) resourceManager; |
---|
| 109 | else |
---|
| 110 | return null; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | protected JobRegistry getJobRegistry(){ |
---|
| 114 | return jobRegistry; |
---|
| 115 | } |
---|
[481] | 116 | |
---|
| 117 | public Scheduler getScheduler() { |
---|
| 118 | return scheduler; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | public PluginConfiguration getSchedulingPluginConfiguration() { |
---|
| 122 | return schedulingPlugin.getConfiguration(); |
---|
| 123 | } |
---|
[477] | 124 | |
---|
| 125 | public boolean pluginSupportsEvent(int eventType){ |
---|
| 126 | return true; |
---|
| 127 | } |
---|
| 128 | |
---|
[481] | 129 | public TaskQueueList getQueues(){ |
---|
| 130 | return queues; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | public Map<String, Integer> getQueuesSize() { |
---|
| 134 | Map<String, Integer> queue_size = new HashMap<String, Integer>(); |
---|
| 135 | for (TaskQueue queue : queues) { |
---|
| 136 | queue_size.put(queue.getName(), queue.size()); |
---|
| 137 | } |
---|
| 138 | return queue_size; |
---|
| 139 | } |
---|
| 140 | |
---|
[477] | 141 | //POPRAWIC (ale co? bo teraz chyba jest ok) |
---|
[481] | 142 | protected void submitTask(TaskInterface<?> task, AllocationInterface<?> allocation) { |
---|
[477] | 143 | String providerName = allocation.getProviderName(); |
---|
| 144 | if (providerName == null) { |
---|
| 145 | return; |
---|
| 146 | } |
---|
[481] | 147 | removeFromQueue(task); |
---|
| 148 | scheduler.send(providerName, GridSimTags.SCHEDULE_NOW, GridSimTags.GRIDLET_SUBMIT, task); |
---|
[477] | 149 | } |
---|
| 150 | |
---|
[478] | 151 | protected boolean sendFinishedWorkloadUnit(WorkloadUnit wu) { |
---|
[477] | 152 | |
---|
| 153 | Executable exec = (Executable) wu; |
---|
| 154 | if(scheduler.getParent() == null) |
---|
| 155 | { |
---|
[480] | 156 | Job job = jobRegistry.getJob(exec.getJobId()); |
---|
[477] | 157 | |
---|
| 158 | if(job.isFinished()){ |
---|
| 159 | scheduler.send(job.getSenderId(), GridSimTags.SCHEDULE_NOW, GridSimTags.GRIDLET_RETURN, job); |
---|
| 160 | return true; |
---|
| 161 | } |
---|
| 162 | else return true; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | IO_data obj = new IO_data(exec, 0, /*task.getGridletOutputSize(),*/ GridSim.getEntityId(scheduler.getParent().get_name())); |
---|
| 166 | scheduler.send(scheduler.getOutputPort(), 0.0, GridSimTags.GRIDLET_RETURN, obj); |
---|
| 167 | return true; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | protected void sendExecutableReadyEvent(ExecTask exec) { |
---|
| 171 | |
---|
| 172 | long delay = 0; |
---|
| 173 | try { |
---|
| 174 | long expectedStartTime = exec.getExecutionStartTime().getMillis() / 1000; |
---|
| 175 | long currentTime = DateTimeUtilsExt.currentTimeMillis() / 1000; |
---|
| 176 | delay = expectedStartTime - currentTime; |
---|
| 177 | if (delay < 0) |
---|
| 178 | delay = 0; |
---|
| 179 | } catch (NoSuchFieldException e) { |
---|
| 180 | delay = 0; |
---|
| 181 | } |
---|
| 182 | |
---|
[481] | 183 | scheduler.sendInternal(Long.valueOf(delay).doubleValue(), DCWormsTags.TASK_READY_FOR_EXECUTION, |
---|
[477] | 184 | exec); |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | protected void sendTimerEvent() { |
---|
| 188 | PluginConfiguration pluginConfig = schedulingPlugin.getConfiguration(); |
---|
| 189 | if (pluginConfig != null) { |
---|
| 190 | Map<SchedulingEventType, Object> events = pluginConfig.getServedEvents(); |
---|
| 191 | if (events != null) { |
---|
| 192 | Object obj = events.get(SchedulingEventType.TIMER); |
---|
| 193 | if (obj != null) { |
---|
| 194 | int delay = (Integer) obj; |
---|
[481] | 195 | scheduler.sendInternal(delay, DCWormsTags.TIMER, null); |
---|
[477] | 196 | } |
---|
| 197 | } |
---|
| 198 | } |
---|
| 199 | } |
---|
| 200 | |
---|
[490] | 201 | protected boolean removeFromQueue(TaskInterface<?> task) { |
---|
[477] | 202 | for(TaskQueue queue : queues){ |
---|
[490] | 203 | if(queue.contains(task)){ |
---|
| 204 | queue.remove(task); |
---|
[477] | 205 | return true; |
---|
| 206 | } |
---|
| 207 | } |
---|
| 208 | return false; |
---|
| 209 | } |
---|
| 210 | |
---|
[481] | 211 | public abstract WorkloadUnitHandler getWorkloadUnitHandler(); |
---|
| 212 | |
---|
| 213 | public abstract void notifySubmittedWorkloadUnit(WorkloadUnit wu, boolean ack); |
---|
[477] | 214 | |
---|
[481] | 215 | public abstract void notifyReturnedWorkloadUnit(WorkloadUnit wu); |
---|
[477] | 216 | |
---|
[481] | 217 | protected abstract void executeSchedulingPlan(SchedulingPlanInterface<?> decision); |
---|
[477] | 218 | |
---|
| 219 | } |
---|