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

Revision 13, 13.5 KB checked in by wojtekp, 14 years ago (diff)

Example experiment for GSSIM UI tests

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