source: DCWoRMS/trunk/src/example/localplugin/coolemall/recs/RECS_LB_SP.java @ 851

Revision 851, 2.9 KB checked in by wojtekp, 12 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package example.localplugin.coolemall.recs;
2
3import java.util.List;
4
5import schedframe.events.scheduling.SchedulingEvent;
6import schedframe.exceptions.ResourceException;
7import schedframe.resources.UserResourceType;
8import schedframe.resources.computing.ComputingResource;
9import schedframe.resources.computing.recs.Node;
10import schedframe.scheduling.manager.resources.ClusterResourceManager;
11import schedframe.scheduling.manager.resources.ResourceManager;
12import schedframe.scheduling.manager.tasks.JobRegistry;
13import schedframe.scheduling.manager.tasks.JobRegistryImpl;
14import schedframe.scheduling.plan.SchedulingPlanInterface;
15import schedframe.scheduling.plan.impl.SchedulingPlan;
16import schedframe.scheduling.plugin.grid.ModuleList;
17import schedframe.scheduling.queue.TaskQueue;
18import schedframe.scheduling.queue.TaskQueueList;
19import schedframe.scheduling.tasks.TaskInterface;
20import example.localplugin.BaseLocalSchedulingPlugin;
21import gridsim.dcworms.DCWormsTags;
22
23public class RECS_LB_SP extends BaseLocalSchedulingPlugin {
24
25        public RECS_LB_SP () {
26        }
27
28        public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry,
29                         ResourceManager resManager, ModuleList modules) {
30
31                ClusterResourceManager resourceManager = (ClusterResourceManager) resManager;
32                SchedulingPlan plan = new SchedulingPlan();
33                // choose the events types to serve.
34                // Different actions for different events are possible.
35                switch (event.getType()) {
36                case START_TASK_EXECUTION:
37                case TASK_FINISHED:
38                //case TIMER:
39                        // our tasks are placed only in first queue (see
40                        // BaseLocalSchedulingPlugin.placeJobsInQueues() method)
41                        TaskQueue q = queues.get(0);
42                       
43                        // check all tasks in queue
44                        for (int i = 0; i < q.size(); i++) {
45                                TaskInterface<?> task = q.get(i);
46                                // if status of the tasks in READY
47                                if (task.getStatus() == DCWormsTags.READY) {
48
49                                        String nodeName = chooseProvider(resourceManager);
50                                        if (nodeName != null) {
51                                                addToSchedulingPlan(plan, task, nodeName);
52                                        }
53                                }
54                        }
55                        break;
56                }
57                return plan;
58        }
59
60        @SuppressWarnings("unchecked")
61        private String chooseProvider(ClusterResourceManager resourceManager) {
62                List<ComputingResource> nodes = null;
63                try {
64                        nodes = (List<ComputingResource>) resourceManager.getResourcesOfType(new UserResourceType("Node"));
65                } catch (ResourceException e) {
66                        // TODO Auto-generated catch block
67                        e.printStackTrace();
68                }
69                int nodeIdx = findLeastLoadedResourceIdx(nodes);
70                return nodes.get(nodeIdx).getName();
71        }
72       
73        private int findLeastLoadedResourceIdx(List<ComputingResource> nodes ){
74                int resourceIdx = -1;
75                int minLoad = Integer.MAX_VALUE;
76               
77                for(int i = 0; i < nodes.size(); i++){
78                        Node node = (Node) nodes.get(i);
79                        JobRegistry jr = new JobRegistryImpl(node.getName());
80                        int totalLoad = jr.getRunningTasks().size();
81                        if(totalLoad < minLoad){
82                                resourceIdx = i;
83                                minLoad = totalLoad;
84                        }
85                }
86                return resourceIdx;
87        }
88
89}
90
Note: See TracBrowser for help on using the repository browser.