1 | package example.localplugin.coolemall.recs; |
---|
2 | |
---|
3 | import java.util.List; |
---|
4 | |
---|
5 | import schedframe.events.scheduling.SchedulingEvent; |
---|
6 | import schedframe.exceptions.ResourceException; |
---|
7 | import schedframe.resources.UserResourceType; |
---|
8 | import schedframe.resources.computing.ComputingResource; |
---|
9 | import schedframe.resources.computing.recs.Node; |
---|
10 | import schedframe.scheduling.manager.resources.ClusterResourceManager; |
---|
11 | import schedframe.scheduling.manager.resources.ResourceManager; |
---|
12 | import schedframe.scheduling.manager.tasks.JobRegistry; |
---|
13 | import schedframe.scheduling.manager.tasks.JobRegistryImpl; |
---|
14 | import schedframe.scheduling.plan.SchedulingPlanInterface; |
---|
15 | import schedframe.scheduling.plan.impl.SchedulingPlan; |
---|
16 | import schedframe.scheduling.plugin.grid.ModuleList; |
---|
17 | import schedframe.scheduling.queue.TaskQueue; |
---|
18 | import schedframe.scheduling.queue.TaskQueueList; |
---|
19 | import schedframe.scheduling.tasks.TaskInterface; |
---|
20 | import example.localplugin.BaseLocalSchedulingPlugin; |
---|
21 | import gridsim.dcworms.DCWormsTags; |
---|
22 | |
---|
23 | public 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 | |
---|