1 | package example.localplugin.coolemall.recs; |
---|
2 | |
---|
3 | import java.util.List; |
---|
4 | import java.util.Random; |
---|
5 | |
---|
6 | import schedframe.events.scheduling.SchedulingEvent; |
---|
7 | import schedframe.exceptions.ResourceException; |
---|
8 | import schedframe.resources.UserResourceType; |
---|
9 | import schedframe.resources.computing.ComputingResource; |
---|
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.plan.SchedulingPlanInterface; |
---|
14 | import schedframe.scheduling.plan.impl.SchedulingPlan; |
---|
15 | import schedframe.scheduling.plugin.grid.ModuleList; |
---|
16 | import schedframe.scheduling.queue.TaskQueue; |
---|
17 | import schedframe.scheduling.queue.TaskQueueList; |
---|
18 | import schedframe.scheduling.tasks.TaskInterface; |
---|
19 | import example.localplugin.BaseLocalSchedulingPlugin; |
---|
20 | import gridsim.dcworms.DCWormsTags; |
---|
21 | |
---|
22 | public class RECS_Random_SP extends BaseLocalSchedulingPlugin { |
---|
23 | |
---|
24 | private Random rand; |
---|
25 | |
---|
26 | public RECS_Random_SP () { |
---|
27 | rand = new Random(47); |
---|
28 | } |
---|
29 | |
---|
30 | public SchedulingPlanInterface<?> schedule(SchedulingEvent event, TaskQueueList queues, JobRegistry jobRegistry, |
---|
31 | ResourceManager resManager, ModuleList modules) { |
---|
32 | |
---|
33 | ClusterResourceManager resourceManager = (ClusterResourceManager) resManager; |
---|
34 | SchedulingPlan plan = new SchedulingPlan(); |
---|
35 | // choose the events types to serve. |
---|
36 | // Different actions for different events are possible. |
---|
37 | switch (event.getType()) { |
---|
38 | case START_TASK_EXECUTION: |
---|
39 | case TASK_FINISHED: |
---|
40 | //case TIMER: |
---|
41 | // our tasks are placed only in first queue (see |
---|
42 | // BaseLocalSchedulingPlugin.placeJobsInQueues() method) |
---|
43 | TaskQueue q = queues.get(0); |
---|
44 | |
---|
45 | // check all tasks in queue |
---|
46 | for (int i = 0; i < q.size(); i++) { |
---|
47 | TaskInterface<?> task = q.get(i); |
---|
48 | // if status of the tasks in READY |
---|
49 | if (task.getStatus() == DCWormsTags.READY) { |
---|
50 | |
---|
51 | String nodeName = chooseRandomProvider(resourceManager); |
---|
52 | if (nodeName != null) { |
---|
53 | addToSchedulingPlan(plan, task, nodeName); |
---|
54 | } |
---|
55 | } |
---|
56 | } |
---|
57 | break; |
---|
58 | } |
---|
59 | return plan; |
---|
60 | } |
---|
61 | |
---|
62 | @SuppressWarnings("unchecked") |
---|
63 | private String chooseRandomProvider(ClusterResourceManager resourceManager) { |
---|
64 | List<ComputingResource> nodes = null; |
---|
65 | try { |
---|
66 | nodes = (List<ComputingResource>) resourceManager.getResourcesOfType(new UserResourceType("Node")); |
---|
67 | } catch (ResourceException e) { |
---|
68 | // TODO Auto-generated catch block |
---|
69 | e.printStackTrace(); |
---|
70 | } |
---|
71 | int nodeIdx = rand.nextInt(nodes.size()); |
---|
72 | return nodes.get(nodeIdx).getName(); |
---|
73 | } |
---|
74 | |
---|
75 | } |
---|
76 | |
---|