source: gssim/trunk/src/test/guiexp/MixedAllSlots.java @ 21

Revision 21, 14.6 KB checked in by wojtekp, 14 years ago (diff)

adjustment of time requirements to experiment specific and modification of resource allocation violation policy

  • Property svn:executable set to *
  • Property svn:mime-type set to text/plain
RevLine 
[13]1package test.guiexp;
2
3
4
5import java.util.ArrayList;
6import java.util.List;
7import java.util.Properties;
8
9import org.joda.time.DateTime;
10
11import schedframe.exceptions.ReservationException;
12import schedframe.resources.providers.LocalSystem;
13import schedframe.resources.units.Processors;
14import schedframe.scheduling.AbstractResourceRequirements;
15import schedframe.scheduling.AbstractTimeRequirements;
16import schedframe.scheduling.Offer;
17import schedframe.scheduling.Queue;
18import schedframe.scheduling.Reservation;
19import schedframe.scheduling.ReservationList;
20import schedframe.scheduling.ResourceUsage;
21import schedframe.scheduling.TaskInterface;
22import schedframe.scheduling.TimeResourceAllocation;
23import schedframe.scheduling.events.ReservationActiveEvent;
24import schedframe.scheduling.events.SchedulingEvent;
25import schedframe.scheduling.events.SchedulingEventType;
[21]26import schedframe.scheduling.events.SchedulingResponseType;
[13]27import schedframe.scheduling.events.StartTaskExecutionEvent;
28import schedframe.scheduling.plugin.SchedulingPluginConfiguration;
29import schedframe.scheduling.plugin.configuration.DefaultConfiguration;
30import schedframe.scheduling.plugin.local.LocalReservationManager;
31import schedframe.scheduling.plugin.local.ResourceUnitsManagerInterface;
32import example.localplugin.BaseLocalARPlugin;
33import gridsim.Gridlet;
34import gridsim.gssim.ResourceUnitsManagerImpl;
35import gridsim.gssim.SubmittedTask;
36import gssim.schedframe.scheduling.plugin.local.LocalReservationManagerImpl;
37
38/**
39 *
40 * @author Marcin Krystek
41 * @author Wojciech Piatek
42 *
43 *
44 */
45public class MixedAllSlots extends BaseLocalARPlugin {
46
47        /*
48         * This implementation of getOffers() method returns detail state of the resource in
49         * requested time. Imagine the result as a gantt diagram of resource usage between
50         * start and end time.
51         * Grid scheduling plugin must analyze this result, decide if there are enough
52         * resource units and create reservation of this units.
53         */
54        public List<Offer> getOffers(AbstractTimeRequirements<?> timeReqs,
55                        AbstractResourceRequirements<?> resReqs,
56                        List<? extends TaskInterface<?>> inExecution,
57                        List<Queue<? extends TaskInterface<?>>> queues,
58                        ResourceUnitsManagerInterface unitsManagerInterface,
59                        LocalReservationManager reservManager)
60                        throws ReservationException {
61
62                LocalReservationManagerImpl reservationManager = (LocalReservationManagerImpl) reservManager;
63                ResourceUnitsManagerImpl unitsManager = (ResourceUnitsManagerImpl) unitsManagerInterface;
64               
65                List<TimeResourceAllocation> usageList = null;
66                int reqCpuCnt = 0;
67               
68                try {
69
70                        // get number of processors requested by the task
71                        reqCpuCnt = Double.valueOf(resReqs.getCpuCntRequest()).intValue();
72               
73                        // create unit for which resource usage will be created
74                        Processors processors = new Processors(unitsManager.getResourceName(), unitsManager.getNumPE(), reqCpuCnt);
75
76                        DateTime startTime = timeReqs.getStart();
77                        DateTime endTime = timeReqs.getEnd();
78
79                        // get resource usage
80                        usageList = reservationManager.resourceUsageList(processors, inExecution, startTime, endTime);
81                       
82                } catch (NoSuchFieldException e) {
83                        e.printStackTrace();
84                        return null;
85                }
86               
87                // prepare offer
88                LocalSystem provider = new LocalSystem(unitsManager.getResourceName(), null, null);
89                Offer offer = new Offer();
90                offer.setProvider(provider);
91
92                for(int i = 0; i < usageList.size(); i++){
93                        offer.add(usageList.get(i));
94                }
95                List<Offer> list = new ArrayList<Offer>(1);
96                list.add(offer);
97               
98                return list;
99        }
100
101        public String getPluginName() {
102                return getClass().getName();
103        }
104
105        public void initPlugin(Properties properties) {
106                getConfiguration().getServedEvents().put(SchedulingEventType.TASK_ARRIVED, null);
107                // TODO Auto-generated constructor stub
108        }
109       
110        public Reservation commitReservation(Reservation initialReservation,
111                        List<? extends TaskInterface<?>> inExecution,
112                        List<Queue<? extends TaskInterface<?>>> queues,
113                        ResourceUnitsManagerInterface unitsManager,
114                        LocalReservationManager reservationManager)
115                        throws ReservationException {
116               
117                // this simple implementation changes status of the reservation
118                // from INITIAL to COMMITED
119               
120                // list of reservations is managed by reservation manager
121                List<Reservation> list = reservationManager.getReservations();
122               
123                for(int i = 0; i < list.size(); i++){
124                        Reservation r = list.get(i);
125
126                        // find correct reservation in reservations list
127                        if(initialReservation.getId().equals(r.getId())){
128                       
129                                // change status to COMMITED
130                                r.setStatus(Reservation.Status.COMMITTED);
131                               
132                                // set job and task id on behalf which this reservation is
133                                // created. This is mostly for debugging purpose.
134                                r.setJobId(initialReservation.getJobId());
135                                r.setTaskId(initialReservation.getTaskId());
136                               
137                                // change status of initial reservation to COMMITED.
138                                // This allows grid plugin to notice, that reservation is
139                                // successfully committed.
140                                initialReservation.setStatus(Reservation.Status.COMMITTED);
141                                return initialReservation;
142                        }
143                }
144               
145                return null;
146        }
147
148
149        public List<Reservation> createReservation(ResourceUsage resourceUsage,
150                        List<? extends TaskInterface<?>> inExecution,
151                        List<Queue<? extends TaskInterface<?>>> queues,
152                        ResourceUnitsManagerInterface unitsManager,
153                        LocalReservationManager reservManager)
154                        throws ReservationException {
155               
156                LocalReservationManagerImpl reservationManager = (LocalReservationManagerImpl) reservManager;
157                TimeResourceAllocation allocation = resourceUsage.get(0);
158               
159                Reservation reservation = reservationManager.
160                                                                        getReservations().add(allocation, Reservation.Status.INITIAL);
161                List<Reservation> list = new ArrayList<Reservation>(1);
162                list.add(reservation);
163               
164                return list;
165        }
166
167        /*
168         * This example implementation puts all new tasks in first queue.
169         * Tasks are served in the same order they appear in newTasks list.
170         */
171        public int placeTasksInQueues(List<? extends TaskInterface<?>> newTasks,
172                        List<? extends Queue<? extends TaskInterface<?>>> queues,
173                        ResourceUnitsManagerInterface unitsManager,
174                        LocalReservationManager reservationManager)
175                        throws ReservationException {
176               
177                // get the first queue from all available queues.
178                Queue<? extends TaskInterface<?>> q = queues.get(0);
179
180                // do this trick to enable access to add() method.
181                Queue<TaskInterface<?>> queue = (Queue<TaskInterface<?>>) q;
182               
183                // move tasks from newTask list to the queue.
184                for(int i = 0; i < newTasks.size(); i++){
185                        TaskInterface<?> task = newTasks.remove(0);
186                        queue.add(task);
187                }
188                return 0;
189        }
190
191
192        public void schedule(SchedulingEvent event,
193                        List<? extends TaskInterface<?>> inExecution,
194                        List<? extends Queue<? extends TaskInterface<?>>> queues,
195                        ResourceUnitsManagerInterface unitsManager,
196                        LocalReservationManager reservationManager) {
197
198                // Example implementation move tasks to execution in the same order
199                // they appear in task queue.
200               
201                // choose handle method for event:
202                switch(event.getType()){
203                        case START_TASK_EXECUTION:
204                                // check if reservation is active and, if so, start task execution
205                                startTask((StartTaskExecutionEvent)event, inExecution,
206                                                queues, unitsManager, reservationManager);
207                                break;
208                               
209                        case RESERVATION_ACTIVE:
210                                // check if task is ready and, if so, start task execution
211                                startTask((ReservationActiveEvent)event, inExecution,
212                                                queues, unitsManager, reservationManager);
213                                break;
214                       
215                        case TASK_FINISHED:
216                                // if one task is finished, look for next task which can be started
217                                startNextTask(inExecution, queues, unitsManager, reservationManager);
218                                break;
219               
220                }
221               
222        }
223       
224        /*
225         * This method starts task execution. Task can be executed if its status
226         * is READY and reservation created for this task is ACTIVE.
227         * Method is called each time task changes its status to READY.
228         *
229         * By default, all tasks are placed in first queue - see
230         * BaseLocalARPlugin.placeTasksInQueues() method.
231         */
232        protected void startTask(StartTaskExecutionEvent event,
233                        List<? extends TaskInterface<?>> inExecution,
234                        List<? extends Queue<? extends TaskInterface<?>>> queues,
235                        ResourceUnitsManagerInterface unitsManager,
236                        LocalReservationManager reservationManager){
237
238                String jobId = event.getJobId();
239                String taskId = event.getTaskId();
240               
241                List<TaskInterface<?>> execute = (List<TaskInterface<?>>) inExecution;
242                Queue<TaskInterface<?>> queue = (Queue<TaskInterface<?>>) queues.get(0);
243               
244                // In queue, look for task this event corresponds too.
245                for(int i = 0; i < queue.size(); i++){
246                        TaskInterface<?> task = queue.get(i);
247               
248                        // check task id, job id and status
249                        if(task.getStatus() == Gridlet.READY &&
250                                jobId.equals(task.getJobId()) &&
251                                taskId.equals(task.getId())){
252                               
253                                ReservationList reservations = reservationManager.getReservations();
254                       
255                                // look for the reservation created for the task
256                                for(int j = 0; j < reservations.size(); j++){
257                                        Reservation r = reservations.get(j);
258                               
259                                        // check task id, job id and reservation status
260                                        if(r.getStatus() == Reservation.Status.ACTIVE &&
261                                                        jobId.equals(r.getJobId()) &&
262                                                        taskId.equals(r.getTaskId())){
263                                               
264                                                // try to start task execution
265                                                if(execute.add(task)){
266                                                        // if executions starts successfully, then remove task from queue
267                                                        queue.remove(i);
268                                                }
269                                               
270                                                // return from method, when all job is done.
271                                                return;
272                                        }
273                                }
274                        }
275                }
276       
277        }
278       
279        /*
280         * This method starts task execution. Task can be executed if its status
281         * is READY and reservation created for this task is ACTIVE.
282         * Method is called each time reservation changes its status to ACTIVE.
283         *
284         * By default, all tasks are placed in first queue - see
285         * BaseLocalARPlugin.placeTasksInQueues() method.
286         */
287        protected void startTask(ReservationActiveEvent event,
288                                List<? extends TaskInterface<?>> inExecution,
289                                List<? extends Queue<? extends TaskInterface<?>>> queues,
290                                ResourceUnitsManagerInterface unitsManager,
291                                LocalReservationManager reservationManager){
292       
293                String jobId = event.getReservation().getJobId();
294                String taskId = event.getReservation().getTaskId();
295               
296                List<TaskInterface<?>> execute = (List<TaskInterface<?>>) inExecution;
297                Queue<TaskInterface<?>> queue = (Queue<TaskInterface<?>>) queues.get(0);
298               
299                // look for the task, for which this reservation is created
300                for(int i = 0; i < queue.size(); i++){
301                        TaskInterface<?> task = queue.get(i);
302               
303                        // check task id, job id and task status
304                        if(task.getStatus() == Gridlet.READY &&
305                                jobId.equals(task.getJobId()) &&
306                                taskId.equals(task.getId())){
307                               
308                                // try to start task execution
309                                if(execute.add(task)){
310                                       
311                                        // if task execution starts successfully, then remove task from queue
312                                        queue.remove(i);
313                                }
314                                // return from method, when all job is done
315                                return;
316                        }
317                }
318       
319        }
320       
321        /*
322         * After finish task execution there is place for executing next task.
323         * This method looks for READY task which reservation has status ACTIVE
324         * and tries to execute it.
325         *
326         * Method is called each time task execution is finished.
327         */
328        protected void startNextTask(List<? extends TaskInterface<?>> inExecution,
329                                List<? extends Queue<? extends TaskInterface<?>>> queues,
330                                ResourceUnitsManagerInterface unitsManager,
331                                LocalReservationManager reservationManager){
332       
333                List<TaskInterface<?>> execute = (List<TaskInterface<?>>) inExecution;
334                Queue<TaskInterface<?>> queue = (Queue<TaskInterface<?>>) queues.get(0);
335               
336                // for each task in queue
337                for(int i = 0; i < queue.size(); i++){
338                        TaskInterface<?> task = queue.get(i);
339               
340                        // look for task with status READY
341                        if(task.getStatus() == Gridlet.READY){
342                                ReservationList reservations = reservationManager.getReservations();
343                       
344                                // look for the reservation created for this task
345                                for(int j = 0; j < reservations.size(); j++){
346                                        Reservation r = reservations.get(j);
347                               
348                                        // check task id, job id and reservation status
349                                        if(r.getStatus() == Reservation.Status.ACTIVE &&
350                                                        task.getJobId().equals(r.getJobId()) &&
351                                                        task.getId().equals(r.getTaskId())){
352                                               
353                                                // try to execute this task
354                                                if(execute.add(task)){
355                                                       
356                                                        // if task execution starts successfully, then remove task from queue
357                                                        queue.remove(i);
358                                                        i--; // modify queue index to get next task correctly
359                                                }
360                                               
361                                                // if reservation is found and all job is done, try to execute next
362                                                // READY task
363                                                break;
364                                        }
365                                }
366                        }
367                }
368        }
369       
370        public SchedulingPluginConfiguration getConfiguration() {
371                return DefaultConfiguration.forLocalARPlugin();
372        }
373
374
375        public void schedule(SchedulingEvent event,
376                        List<? extends TaskInterface<?>> inExecution,
377                        List<? extends Queue<? extends TaskInterface<?>>> queues,
378                        ResourceUnitsManagerInterface unitsManager) {
379                // do this trick to make add() method available
380                List <TaskInterface<?>> execute = (List<TaskInterface<?>>) inExecution;
381               
382                // chose the events types to serve.
383                // Different actions for different events are possible.
384                switch(event.getType()){
385                        case START_TASK_EXECUTION:
386                        case TASK_FINISHED:
387                        //case TIMER:
388                                // our tasks are placed only in first queue (see BaseLocalPlugin.placeTasksInQueues() method)
389                                Queue<? extends TaskInterface<?>> q = queues.get(0);
390                                // check all tasks in queue
391                                for(int i = 0; i < q.size(); i++){
392                                        TaskInterface<?> task = q.get(i);
393                                        // if status of the tasks in READY
394                                       
395                                        SubmittedTask submittedTask = (SubmittedTask)task;
396                                        if(submittedTask.getReservationID() != -1)
397                                                continue;
398                                        if(task.getStatus() == Gridlet.READY ){
399                                                // then try to execute this task. Add it the execute list.
400                                                if(execute.add(task)){
401                                                        // if task started successfully, then remove it from the queue.
402                                                        q.remove(i);
403                                                        i--; // index trick to get the right position in the queue after task removal.
404                                                }
405                                        }
406                                }
407                                break;
408                }
409               
410        }
411       
[21]412        public SchedulingResponseType handleResourceAllocationViolation(SchedulingEvent event,
413                        List<? extends TaskInterface<?>> inExecution,
414                                        List<? extends Queue<? extends TaskInterface<?>>> queues,
415                                        ResourceUnitsManagerInterface unitsManager,
416                                        LocalReservationManager reservationManager){
417                SchedulingResponseType timeEvent = null;
418                switch(event.getType()){
419                        case TASK_REQUESTED_TIME_EXPIRED:
420                                timeEvent = SchedulingResponseType.STOP_AND_RESUME_FROM_CHECKPOINT;
421                                break; 
422                }
423                return timeEvent;
424        }
425       
426        public SchedulingResponseType handleResourceAllocationViolation(SchedulingEvent event,
427                        List<? extends TaskInterface<?>> inExecution,
428                                        List<? extends Queue<? extends TaskInterface<?>>> queues,
429                                        ResourceUnitsManagerInterface unitsManager){
430                SchedulingResponseType timeEvent = null;
431                switch(event.getType()){
432                        case TASK_REQUESTED_TIME_EXPIRED:
433                                timeEvent = SchedulingResponseType.STOP_AND_RESUME_FROM_CHECKPOINT;
434                                break; 
435                }
436                return timeEvent;
437        }
438
[13]439}
Note: See TracBrowser for help on using the repository browser.