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 | |
---|
10 | import schedframe.PluginConfiguration; |
---|
11 | import schedframe.events.scheduling.SchedulingEventType; |
---|
12 | import schedframe.resources.units.StandardResourceUnitName; |
---|
13 | import schedframe.scheduling.Scheduler; |
---|
14 | import schedframe.scheduling.WorkloadUnitHandler; |
---|
15 | import schedframe.scheduling.manager.resources.ManagedResources; |
---|
16 | import schedframe.scheduling.manager.resources.ResourceManager; |
---|
17 | import schedframe.scheduling.manager.resources.ResourceManagerFactory; |
---|
18 | import schedframe.scheduling.manager.tasks.JobRegistryImpl; |
---|
19 | import schedframe.scheduling.manager.tasks.JobRegistry; |
---|
20 | import schedframe.scheduling.plan.AllocationInterface; |
---|
21 | import schedframe.scheduling.plan.SchedulingPlanInterface; |
---|
22 | import schedframe.scheduling.plugin.SchedulingPlugin; |
---|
23 | import schedframe.scheduling.plugin.estimation.ExecutionTimeEstimationPlugin; |
---|
24 | import schedframe.scheduling.plugin.grid.ModuleList; |
---|
25 | import schedframe.scheduling.plugin.local.ResourceAllocationInterface; |
---|
26 | import schedframe.scheduling.queue.TaskQueue; |
---|
27 | import schedframe.scheduling.queue.TaskQueueList; |
---|
28 | import schedframe.scheduling.tasks.Job; |
---|
29 | import schedframe.scheduling.tasks.WorkloadUnit; |
---|
30 | import simulator.WormsConstants; |
---|
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.WormsTags; |
---|
37 | import gssim.schedframe.scheduling.ExecTask; |
---|
38 | import gssim.schedframe.scheduling.Executable; |
---|
39 | import gssim.schedframe.scheduling.queues.AbstractStatsSupportingQueue; |
---|
40 | |
---|
41 | public abstract class AbstractManagementSystem { |
---|
42 | |
---|
43 | private Log log = LogFactory.getLog(AbstractManagementSystem.class); |
---|
44 | |
---|
45 | protected String name; |
---|
46 | |
---|
47 | protected ResourceManager resourceManager; |
---|
48 | |
---|
49 | protected TaskQueueList queues; |
---|
50 | |
---|
51 | protected SchedulingPlugin schedulingPlugin; |
---|
52 | |
---|
53 | protected ExecutionTimeEstimationPlugin execTimeEstimationPlugin; |
---|
54 | |
---|
55 | protected ModuleList moduleList; |
---|
56 | |
---|
57 | protected JobRegistryImpl jobRegistry; |
---|
58 | |
---|
59 | |
---|
60 | public AbstractManagementSystem(String providerId, String entityName, |
---|
61 | ExecutionTimeEstimationPlugin execTimeEstPlugin, TaskQueueList queues) { |
---|
62 | |
---|
63 | this.name = entityName + "@" + providerId; |
---|
64 | this.queues = queues; |
---|
65 | this.jobRegistry = new JobRegistryImpl(name); |
---|
66 | this.execTimeEstimationPlugin = execTimeEstPlugin; |
---|
67 | } |
---|
68 | |
---|
69 | public void processEvent(Sim_event ev) { |
---|
70 | processOtherEvent(ev); |
---|
71 | } |
---|
72 | |
---|
73 | protected void processOtherEvent(Sim_event ev) { |
---|
74 | if (ev == null) { |
---|
75 | System.out.println(name + ".processOtherEvent(): " + "Error - an event is null."); |
---|
76 | return; |
---|
77 | } |
---|
78 | |
---|
79 | log.error(name + ".processOtherEvent(): Unable to " |
---|
80 | + "handle request from an event with a tag number " + ev.get_tag()); |
---|
81 | } |
---|
82 | |
---|
83 | public String getName() { |
---|
84 | return name; |
---|
85 | } |
---|
86 | |
---|
87 | public PluginConfiguration getSchedulingPluginConfiguration() { |
---|
88 | return schedulingPlugin.getConfiguration(); |
---|
89 | } |
---|
90 | |
---|
91 | public ResourceManager getResourceManager() { |
---|
92 | if (resourceManager instanceof ResourceManager) |
---|
93 | return (ResourceManager) resourceManager; |
---|
94 | else |
---|
95 | return null; |
---|
96 | } |
---|
97 | |
---|
98 | public ResourceAllocationInterface getAllocationManager() { |
---|
99 | if (resourceManager instanceof ResourceAllocationInterface) |
---|
100 | return (ResourceAllocationInterface) resourceManager; |
---|
101 | else |
---|
102 | return null; |
---|
103 | } |
---|
104 | |
---|
105 | protected JobRegistry getJobRegistry(){ |
---|
106 | return jobRegistry; |
---|
107 | } |
---|
108 | |
---|
109 | public boolean pluginSupportsEvent(int eventType){ |
---|
110 | return true; |
---|
111 | } |
---|
112 | |
---|
113 | public abstract void notifySubmittedWorkloadUnit(WorkloadUnit<?> wu, boolean ack); |
---|
114 | |
---|
115 | public abstract void notifyCanceledWorkloadUnit(WorkloadUnit<?> wu); |
---|
116 | |
---|
117 | public abstract void notifyReturnedWorkloadUnit(WorkloadUnit<?> wu); |
---|
118 | |
---|
119 | protected abstract void executeSchedulingPlan(SchedulingPlanInterface decision); |
---|
120 | |
---|
121 | |
---|
122 | |
---|
123 | //POPRAWIC (ale co? bo teraz chyba jest ok) |
---|
124 | protected void submitWorkloadUnit(WorkloadUnit<?> wu, AllocationInterface allocation) { |
---|
125 | String providerName = allocation.getProviderName(); |
---|
126 | if (providerName == null) { |
---|
127 | return; |
---|
128 | } |
---|
129 | //Executable exec = (Executable) wu; |
---|
130 | removeFromQueue(wu); |
---|
131 | scheduler.send(providerName, GridSimTags.SCHEDULE_NOW, GridSimTags.GRIDLET_SUBMIT, wu); |
---|
132 | } |
---|
133 | |
---|
134 | protected boolean sendCanceledWorkloadUnit(int tag, Executable task, int executableId, int destId) { |
---|
135 | |
---|
136 | if (tag != GridSimTags.GRIDLET_CANCEL) { |
---|
137 | return false; |
---|
138 | } |
---|
139 | |
---|
140 | long taskSize = 0; |
---|
141 | if (task != null) { |
---|
142 | taskSize = task.getGridletOutputSize(); |
---|
143 | } |
---|
144 | |
---|
145 | // if no Gridlet found, then create a new Gridlet but set its status |
---|
146 | // to FAILED. Then, most importantly, set the resource parameters |
---|
147 | // because the user will search/filter based on a resource ID. |
---|
148 | else if (task == null) { |
---|
149 | try { |
---|
150 | taskSize = 100; |
---|
151 | task = jobRegistry.getTaskExecutable(executableId); |
---|
152 | task.setGridletStatus(Gridlet.FAILED); |
---|
153 | int cost = resourceManager.getSharedResourceUnits().get(StandardResourceUnitName.COST) != null ? resourceManager |
---|
154 | .getSharedResourceUnits().get(StandardResourceUnitName.COST).get(0).getAmount() |
---|
155 | : 1; |
---|
156 | task.setResourceParameter(scheduler.get_id(), cost); |
---|
157 | } catch (Exception e) { |
---|
158 | // empty ... |
---|
159 | } |
---|
160 | } |
---|
161 | scheduler.send(scheduler.getOutputPort(), GridSimTags.SCHEDULE_NOW, tag, new IO_data(task, taskSize, destId)); |
---|
162 | |
---|
163 | return true; |
---|
164 | } |
---|
165 | |
---|
166 | protected boolean sendFinishedWorkloadUnit(WorkloadUnit<?> wu) { |
---|
167 | |
---|
168 | Executable exec = (Executable) wu; |
---|
169 | if(scheduler.getParent() == null) |
---|
170 | { |
---|
171 | Job job = jobRegistry.get(exec.getJobId()); |
---|
172 | |
---|
173 | if(job.isFinished()){ |
---|
174 | scheduler.send(job.getSenderId(), GridSimTags.SCHEDULE_NOW, GridSimTags.GRIDLET_RETURN, job); |
---|
175 | return true; |
---|
176 | }else return true; |
---|
177 | } |
---|
178 | |
---|
179 | IO_data obj = new IO_data(exec, 0, /*task.getGridletOutputSize(),*/ GridSim.getEntityId(scheduler.getParent().get_name())); |
---|
180 | scheduler.send(scheduler.getOutputPort(), 0.0, GridSimTags.GRIDLET_RETURN, obj); |
---|
181 | return true; |
---|
182 | } |
---|
183 | |
---|
184 | protected void sendExecutableReadyEvent(ExecTask exec) { |
---|
185 | |
---|
186 | /*if (wu instanceof JobInterface) { |
---|
187 | scheduler.sendInternal(Long.valueOf(0).doubleValue(), GssimTags.TASK_READY_FOR_EXECUTION, |
---|
188 | wu); |
---|
189 | return; |
---|
190 | }*/ |
---|
191 | |
---|
192 | long delay = 0; |
---|
193 | try { |
---|
194 | long expectedStartTime = exec.getExecutionStartTime().getMillis() / 1000; |
---|
195 | long currentTime = DateTimeUtilsExt.currentTimeMillis() / 1000; |
---|
196 | delay = expectedStartTime - currentTime; |
---|
197 | if (delay < 0) |
---|
198 | delay = 0; |
---|
199 | } catch (NoSuchFieldException e) { |
---|
200 | delay = 0; |
---|
201 | } |
---|
202 | |
---|
203 | scheduler.sendInternal(Long.valueOf(delay).doubleValue(), WormsTags.TASK_READY_FOR_EXECUTION, |
---|
204 | exec); |
---|
205 | } |
---|
206 | |
---|
207 | protected void sendTimerEvent() { |
---|
208 | PluginConfiguration pluginConfig = schedulingPlugin.getConfiguration(); |
---|
209 | if (pluginConfig != null) { |
---|
210 | Map<SchedulingEventType, Object> events = pluginConfig.getServedEvents(); |
---|
211 | if (events != null) { |
---|
212 | Object obj = events.get(SchedulingEventType.TIMER); |
---|
213 | if (obj != null) { |
---|
214 | int delay = (Integer) obj; |
---|
215 | scheduler.sendInternal(delay, WormsTags.TIMER, null); |
---|
216 | } |
---|
217 | } |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | protected boolean removeFromQueue(WorkloadUnit<?> wu) { |
---|
222 | for(TaskQueue queue : queues){ |
---|
223 | if(queue.contains(wu)){ |
---|
224 | queue.remove(wu); |
---|
225 | return true; |
---|
226 | } |
---|
227 | } |
---|
228 | return false; |
---|
229 | } |
---|
230 | |
---|
231 | public TaskQueueList getAccessQueues(){ |
---|
232 | return queues; |
---|
233 | } |
---|
234 | |
---|
235 | public Map<String, Integer> getQueuesSize() { |
---|
236 | Map<String, Integer> queue_size = new HashMap<String, Integer>(); |
---|
237 | for (TaskQueue queue : queues) { |
---|
238 | queue_size.put(queue.getName(), queue.size()); |
---|
239 | } |
---|
240 | return queue_size; |
---|
241 | } |
---|
242 | |
---|
243 | public void init(Scheduler sched, ManagedResources managedResources) { |
---|
244 | scheduler = sched; |
---|
245 | resourceManager = ResourceManagerFactory.createResourceManager(scheduler, managedResources); |
---|
246 | scheduler.set_stat(WormsConstants.getResourcesStatisticsObject(queues.size())); |
---|
247 | for(int i = 0; i < queues.size(); i++){ |
---|
248 | TaskQueue q = queues.get(i); |
---|
249 | if(q instanceof AbstractStatsSupportingQueue<?>){ |
---|
250 | AbstractStatsSupportingQueue<?> queue = (AbstractStatsSupportingQueue<?>) q; |
---|
251 | queue.setStats(scheduler.get_stat(), WormsConstants.TASKS_QUEUE_LENGTH_MEASURE_NAME + "_" + Integer.toString(i)); |
---|
252 | } |
---|
253 | } |
---|
254 | } |
---|
255 | |
---|
256 | protected Scheduler scheduler; |
---|
257 | |
---|
258 | public Scheduler getScheduler() { |
---|
259 | return scheduler; |
---|
260 | } |
---|
261 | |
---|
262 | public abstract WorkloadUnitHandler getWorkloadUnitHandler(); |
---|
263 | |
---|
264 | } |
---|