source: DCWoRMS/branches/coolemall/src/schedframe/scheduling/Scheduler.java @ 1415

Revision 1415, 8.4 KB checked in by wojtekp, 11 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package schedframe.scheduling;
2
3import eduni.simjava.Sim_event;
4import eduni.simjava.Sim_port;
5import eduni.simjava.Sim_system;
6import gridsim.GridSim;
7import gridsim.GridSimCore;
8import gridsim.GridSimTags;
9import gridsim.IO_data;
10import gridsim.dcworms.DCWormsTags;
11
12import java.util.ArrayList;
13import java.util.List;
14import java.util.Map;
15import java.util.Properties;
16
17import schedframe.Initializable;
18import schedframe.PluginConfiguration;
19import schedframe.events.scheduling.SchedulingEventType;
20import schedframe.resources.Resource;
21import schedframe.resources.ResourceStatus;
22import schedframe.resources.ResourceType;
23import schedframe.resources.computing.ComputingResource;
24import schedframe.resources.providers.LocalSystem;
25import schedframe.resources.units.ResourceUnit;
26import schedframe.resources.units.ResourceUnitName;
27import schedframe.scheduling.manager.resources.ManagedComputingResources;
28import schedframe.scheduling.manager.resources.ManagedResources;
29import schedframe.scheduling.policy.AbstractManagementSystem;
30import schedframe.scheduling.queue.QueueDescription;
31import schedframe.scheduling.queue.TaskQueue;
32import schedframe.scheduling.tasks.WorkloadUnit;
33
34public class Scheduler extends GridSimCore implements Resource, Initializable{
35       
36        protected ResourceStatus status;
37        protected ResourceType type;
38       
39        protected Scheduler parent;
40        protected List<Scheduler> children;
41       
42        protected AbstractManagementSystem managementSystem;
43       
44        protected ManagedComputingResources compResources;     
45
46        public Scheduler(AbstractManagementSystem manSys, ResourceType schedType, ManagedResources managedResources) throws Exception {
47                super(manSys.getName(), 1);
48                this.managementSystem = manSys;
49                this.compResources = new ManagedComputingResources();
50                this.status = ResourceStatus.AVAILABLE;
51                this.children = new ArrayList<Scheduler>(1);
52                this.type = schedType;
53                init(managedResources);
54        }
55
56        public void init(ManagedResources managedResources){
57                if(managedResources != null)
58                        addResources(managedResources.getComputingResources());
59                managementSystem.init(this, managedResources);
60               
61                //required to terminate all scheduling entities after the end of simulation
62                Integer schedulerIdObj = new Integer(get_id());
63                List<Integer> schedRes = GridSim.getGridResourceList();
64                schedRes.add(schedulerIdObj);
65        }
66       
67        public void addChild(Scheduler child) {
68                child.setParent(this);
69                if (children == null) {
70                        children = new ArrayList<Scheduler>(1);
71                }
72                children.add(child);
73        }
74
75        public List<Scheduler> getChildren() {
76                return children;
77        }
78
79        public void setParent(Scheduler newParent) {
80                this.parent = newParent;
81        }
82
83        public Scheduler getParent() {
84                return parent;
85        }
86
87        /*public void addResource(ComputingResource resource) {
88                compResources.add(resource);
89        }*/
90
91        public void addResources(List<ComputingResource> res) {
92                for(ComputingResource resource:res){
93                        compResources.add(resource);
94                        resource.setScheduler(this);
95                        for(ComputingResource child: resource.filterDescendants(new Properties())){
96                                child.setScheduler(this);
97                        }
98                }
99        }
100
101        public ManagedComputingResources getCompResources() {
102                return compResources;
103        }
104
105        public void body() {
106
107                PluginConfiguration pluginConfig = managementSystem.getSchedulingPluginConfiguration();
108                if (pluginConfig != null) {
109                        Map<SchedulingEventType, Object> events = pluginConfig.getServedEvents();
110                        if (events != null) {
111                                Object obj = events.get(SchedulingEventType.TIMER);
112                                if (obj != null) {
113                                        int delay = (Integer) obj;
114                                        send(this.get_id(), delay, DCWormsTags.TIMER);
115                                }
116                        }
117                }
118
119                // Process events until END_OF_SIMULATION is received from the
120                // GridSimShutdown Entity
121                Sim_event ev = new Sim_event();
122                sim_get_next(ev);
123                boolean run = true;
124                while (Sim_system.running()  && run) {
125                        // sim_get_next(ev);
126                        // if the simulation finishes then exit the loop
127                        if (ev.get_tag() == GridSimTags.END_OF_SIMULATION) {
128                                // managemetnSystem_.setEndSimulation();
129                                run = false;
130                                break;
131                        }
132                        // process the received event
133                        processRequest(ev);
134                        sim_get_next(ev);
135                }
136        }
137       
138        protected void processRequest(Sim_event ev) {
139                switch (ev.get_tag()) {
140
141                case GridSimTags.GRIDLET_SUBMIT:
142                        processWorkloadUnitSubmit(ev);
143                        break;
144
145
146                case GridSimTags.GRIDLET_RETURN:
147                        processWorkloadUnitReturn(ev);
148                        break;
149
150                default:
151                        processOtherRequest(ev);
152                        break;
153                }
154        }
155
156        protected void processOtherRequest(Sim_event ev) {
157                switch (ev.get_tag()) {
158                case DCWormsTags.QUERY_RESOURCE_DESC:
159                        SchedulerDescription desc = new SchedulerDescription(new LocalSystem(get_name(), null, null));
160                        Map<ResourceUnitName, List<ResourceUnit>> units = managementSystem.getResourceManager().getSharedResourceUnits();
161                        desc.addResourceUnitList(units);
162                        desc.addQueuesDescription(getQueuesDescription());
163
164                        IO_data data = new IO_data(desc, 0, ev.get_src());
165                        send(ev.get_src(), GridSimTags.SCHEDULE_NOW, DCWormsTags.QUERY_RESOURCE_DESC_RESULT, data);
166                        break;
167
168                default:
169                        managementSystem.processEvent(ev);
170                        break;
171                }
172        }
173
174        protected void processWorkloadUnitReturn(Sim_event ev) {
175                WorkloadUnit job = (WorkloadUnit) ev.get_data();
176                managementSystem.notifyReturnedWorkloadUnit(job);
177        }
178
179        protected void processWorkloadUnitSubmit(Sim_event ev) {
180                WorkloadUnit job = (WorkloadUnit) ev.get_data();
181                managementSystem.notifySubmittedWorkloadUnit(job);
182        }
183       
184        public void send(String entityName, double delay, int tag, Object data){
185                super.send(entityName, delay, tag, data);
186        }
187       
188        public void send(int destId, double delay, int tag){
189                super.send(destId, delay, tag);
190        }
191       
192        public void send(int destId, double delay, int tag, Object data){
193                super.send(destId, delay, tag, data);
194        }
195       
196        public void send(Sim_port destPort, double delay, int tag, Object data) {
197                super.send(destPort, delay, tag, data);
198        }
199
200        public void sendInternal(double delay, int tag, Object data) {
201                this.send(this.get_id(), delay, tag, data);
202        }
203       
204        public Sim_port getOutputPort() {
205                return output;
206        }
207
208        /*public Scheduler getScheduler(String resourceName){
209                ComputingResource resourceWithName = null;
210                for(int i = 0 ; i < compResources.size() && resourceWithName == null; i++){
211                        ComputingResource resource = compResources.get(i);
212                        if(resource.getName().equals(resourceName))
213                                resourceWithName = resource;
214                        else
215                                try {
216                                        resourceWithName = resource.getDescendantByName(resourceName);
217                                } catch (ResourceException e) {
218                                        return null;
219                                }
220                }
221                if(resourceWithName == null)
222                        return null;
223                List<ComputingResource> children = resourceWithName.getChildren();
224                Set<Scheduler> childrenSchedulers = new HashSet<Scheduler>();
225                for(ComputingResource child:children) {
226                        childrenSchedulers.add(child.getScheduler());
227                }
228                Set<Scheduler> tempChildrenSchedulers = new HashSet<Scheduler>(childrenSchedulers);
229                while(childrenSchedulers.size() != 1){
230                        childrenSchedulers = new HashSet<Scheduler>();
231                        for(Scheduler s: tempChildrenSchedulers){
232                                childrenSchedulers.add(s.getParent());
233                        }
234                        tempChildrenSchedulers = new HashSet<Scheduler>(childrenSchedulers);
235                }
236                Iterator<Scheduler> it = childrenSchedulers.iterator();
237                Scheduler potentialScheduler = it.next();
238                if(potentialScheduler.getResources().containsAll(children))
239                        return potentialScheduler;
240                return null;
241
242        }*/
243       
244        public List<QueueDescription> getQueuesDescription(){
245                List<QueueDescription> queues = new ArrayList<QueueDescription>();
246                for(TaskQueue queue: managementSystem.getQueues()){
247                        QueueDescription qd;
248                        try {
249                                qd = new QueueDescription(queue.getName(), queue.getPriority(), queue.supportReservations(), getQueueLoad(queue.getName()));
250                                queues.add(qd);
251                        } catch (NoSuchFieldException e) {;
252                        }
253                }
254                return queues;
255
256        }
257        private long getQueueLoad(String queueName) throws NoSuchFieldException{
258                Map<String, Integer> queue_size = managementSystem.getQueuesSize();
259                long load = 0;
260                if(queueName == null){
261                        for(String queue: queue_size.keySet()){
262                                load += queue_size.get(queue);
263                        }
264                        return load;
265                }
266                else if(queue_size.containsKey(queueName))
267                        return queue_size.get(queueName);
268                else
269                        throw new NoSuchFieldException("Queue " + queueName +
270                                                        " is not available in resource " + get_name());
271        }
272
273        public ResourceType getType() {
274                return this.type;
275        }
276
277        public String getFullName() {
278                if(parent!= null){
279                        return parent.getFullName() + "/" + name;
280                } else
281                        return name;
282        }
283       
284        public ResourceStatus getStatus() {
285                return status;
286        }
287       
288        public void setStatus(ResourceStatus newStatus) {
289                status = newStatus;
290        }
291       
292        public void getResourceDescription(){
293               
294        }
295
296        public void initiate() {
297
298        }
299}
Note: See TracBrowser for help on using the repository browser.