source: DCWoRMS/branches/coolemall/src/example/globalplugin/GridFCFSRandomPlugin.java @ 1562

Revision 1562, 2.6 KB checked in by wojtekp, 9 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package example.globalplugin;
2
3import java.util.Comparator;
4import java.util.List;
5import java.util.Random;
6
7import schedframe.events.scheduling.SchedulingEvent;
8import schedframe.scheduling.SchedulerDescription;
9import schedframe.scheduling.manager.resources.ResourceManager;
10import schedframe.scheduling.manager.tasks.JobRegistry;
11import schedframe.scheduling.plan.SchedulingPlanInterface;
12import schedframe.scheduling.plan.impl.Allocation;
13import schedframe.scheduling.plan.impl.ScheduledTask;
14import schedframe.scheduling.plan.impl.SchedulingPlan;
15import schedframe.scheduling.plugin.Module;
16import schedframe.scheduling.plugin.ModuleList;
17import schedframe.scheduling.plugin.grid.ResourceDiscovery;
18import schedframe.scheduling.queue.TaskQueue;
19import schedframe.scheduling.queue.TaskQueueList;
20import schedframe.scheduling.tasks.TaskInterface;
21
22public class GridFCFSRandomPlugin extends BaseGlobalPlugin {
23
24        private Random rand;
25       
26        public GridFCFSRandomPlugin() {
27                rand = new Random(5);
28        }
29       
30        public SchedulingPlanInterface<?> schedule(SchedulingEvent event,
31                        TaskQueueList queues,
32                        JobRegistry jobRegistry,
33                        ResourceManager resourceManager, ModuleList modules)  {
34               
35                ResourceDiscovery resources = null;
36                for(int i = 0; i < modules.size(); i++){
37                        Module m = modules.get(i);
38                        switch(m.getType()){
39                                case RESOURCE_DISCOVERY: resources = (ResourceDiscovery) m;
40                                        break;
41                        }
42                }
43               
44                SchedulingPlan plan = new SchedulingPlan();
45               
46                TaskQueue q = queues.get(0);
47                int size = q.size();
48               
49                // order of the resources on this list is not determined
50                List<SchedulerDescription> availableResources = resources.getResources(null);
51                availableResources.sort(new SchedulerDescriptionComparator());
52                int resourceIdx = -1;
53               
54                for(int i = 0; i < size; i++) {
55
56                        TaskInterface<?> task = q.remove(0);
57                       
58                        resourceIdx = rand.nextInt(availableResources.size());
59                        SchedulerDescription sd = availableResources.get(resourceIdx);
60                        Allocation allocation = new Allocation();
61                        allocation.setProcessesCount(1);
62                        allocation.setProviderName(sd.getProvider().getProviderId());
63                       
64                        ScheduledTask scheduledTask = new ScheduledTask(task);
65                        scheduledTask.setTaskId(task.getId());
66                        scheduledTask.setJobId(task.getJobId());
67                        scheduledTask.addAllocation(allocation);               
68                       
69                        plan.addTask(scheduledTask);
70
71                }
72                return plan;
73               
74        }
75
76}
77
78class SchedulerDescriptionComparator implements Comparator<SchedulerDescription>{
79
80        @Override
81        public int compare(SchedulerDescription o1, SchedulerDescription o2) {
82                return(o1.getProvider().getProviderId().compareTo(o2.getProvider().getProviderId()));
83        }
84}
Note: See TracBrowser for help on using the repository browser.