source: DCWoRMS/trunk/src/schedframe/scheduling/Scheduler.java @ 967

Revision 967, 8.5 KB checked in by wojtekp, 12 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                /*if (supportsAR) {
67                        res = GridSim.getAdvancedReservationList();
68                        res.add(resIdObj);
69                } */
70
71        }
72       
73        public void addChild(Scheduler child) {
74                child.setParent(this);
75                if (children == null) {
76                        children = new ArrayList<Scheduler>(1);
77                }
78                children.add(child);
79        }
80
81        public List<Scheduler> getChildren() {
82                return children;
83        }
84
85        public void setParent(Scheduler newParent) {
86                this.parent = newParent;
87        }
88
89        public Scheduler getParent() {
90                return parent;
91        }
92
93        /*public void addResource(ComputingResource resource) {
94                compResources.add(resource);
95        }*/
96
97        public void addResources(List<ComputingResource> res) {
98                for(ComputingResource resource:res){
99                        compResources.add(resource);
100                        resource.setScheduler(this);
101                        for(ComputingResource child: resource.filterDescendants(new Properties())){
102                                child.setScheduler(this);
103                        }
104                }
105        }
106
107        public ManagedComputingResources getCompResources() {
108                return compResources;
109        }
110
111        public void body() {
112
113                PluginConfiguration pluginConfig = managementSystem.getSchedulingPluginConfiguration();
114                if (pluginConfig != null) {
115                        Map<SchedulingEventType, Object> events = pluginConfig.getServedEvents();
116                        if (events != null) {
117                                Object obj = events.get(SchedulingEventType.TIMER);
118                                if (obj != null) {
119                                        int delay = (Integer) obj;
120                                        send(this.get_id(), delay, DCWormsTags.TIMER);
121                                }
122                        }
123                }
124
125                // Process events until END_OF_SIMULATION is received from the
126                // GridSimShutdown Entity
127                Sim_event ev = new Sim_event();
128                sim_get_next(ev);
129                boolean run = true;
130                while (Sim_system.running()  && run) {
131                        // sim_get_next(ev);
132                        // if the simulation finishes then exit the loop
133                        if (ev.get_tag() == GridSimTags.END_OF_SIMULATION) {
134                                // managemetnSystem_.setEndSimulation();
135                                run = false;
136                                break;
137                        }
138                       
139                        // process the received event
140                        processRequest(ev);
141                        sim_get_next(ev);
142                }
143        }
144       
145        protected void processRequest(Sim_event ev) {
146                switch (ev.get_tag()) {
147
148                case GridSimTags.GRIDLET_SUBMIT:
149                        processWorkloadUnitSubmit(ev, false);
150                        break;
151
152                case GridSimTags.GRIDLET_SUBMIT_ACK:
153                        processWorkloadUnitSubmit(ev, true);
154                        break;
155
156                case GridSimTags.GRIDLET_RETURN:
157                        processWorkloadUnitReturn(ev);
158                        break;
159
160                default:
161                        processOtherRequest(ev);
162                        break;
163                }
164        }
165
166        protected void processOtherRequest(Sim_event ev) {
167                switch (ev.get_tag()) {
168                case DCWormsTags.QUERY_RESOURCE_DESC:
169                        SchedulerDescription desc = new SchedulerDescription(new LocalSystem(get_name(), null, null));
170                        Map<ResourceUnitName, List<ResourceUnit>> units = managementSystem.getResourceManager().getSharedResourceUnits();
171                        desc.addResourceUnitList(units);
172                        desc.addQueuesDescription(getQueuesDescription());
173
174                        IO_data data = new IO_data(desc, 0, ev.get_src());
175                        send(ev.get_src(), GridSimTags.SCHEDULE_NOW, DCWormsTags.QUERY_RESOURCE_DESC_RESULT, data);
176                        break;
177
178                default:
179                        managementSystem.processEvent(ev);
180                        break;
181                }
182        }
183
184        protected void processWorkloadUnitReturn(Sim_event ev) {
185                WorkloadUnit job = (WorkloadUnit) ev.get_data();
186                managementSystem.notifyReturnedWorkloadUnit(job);
187        }
188
189        protected void processWorkloadUnitSubmit(Sim_event ev, boolean ack) {
190                WorkloadUnit job = (WorkloadUnit) ev.get_data();
191                managementSystem.notifySubmittedWorkloadUnit(job, ack);
192        }
193       
194        public void send(String entityName, double delay, int tag, Object data){
195                super.send(entityName, delay, tag, data);
196        }
197       
198        public void send(int destId, double delay, int tag){
199                super.send(destId, delay, tag);
200        }
201       
202        public void send(int destId, double delay, int tag, Object data){
203                super.send(destId, delay, tag, data);
204        }
205       
206        public void send(Sim_port destPort, double delay, int tag, Object data) {
207                super.send(destPort, delay, tag, data);
208        }
209
210        public void sendInternal(double delay, int tag, Object data) {
211                this.send(this.get_id(), delay, tag, data);
212        }
213       
214        public Sim_port getOutputPort() {
215                return output;
216        }
217
218        /*public Scheduler getScheduler(String resourceName){
219                ComputingResource resourceWithName = null;
220                for(int i = 0 ; i < compResources.size() && resourceWithName == null; i++){
221                        ComputingResource resource = compResources.get(i);
222                        if(resource.getName().equals(resourceName))
223                                resourceWithName = resource;
224                        else
225                                try {
226                                        resourceWithName = resource.getDescendantByName(resourceName);
227                                } catch (ResourceException e) {
228                                        return null;
229                                }
230                }
231                if(resourceWithName == null)
232                        return null;
233                List<ComputingResource> children = resourceWithName.getChildren();
234                Set<Scheduler> childrenSchedulers = new HashSet<Scheduler>();
235                for(ComputingResource child:children) {
236                        childrenSchedulers.add(child.getScheduler());
237                }
238                Set<Scheduler> tempChildrenSchedulers = new HashSet<Scheduler>(childrenSchedulers);
239                while(childrenSchedulers.size() != 1){
240                        childrenSchedulers = new HashSet<Scheduler>();
241                        for(Scheduler s: tempChildrenSchedulers){
242                                childrenSchedulers.add(s.getParent());
243                        }
244                        tempChildrenSchedulers = new HashSet<Scheduler>(childrenSchedulers);
245                }
246                Iterator<Scheduler> it = childrenSchedulers.iterator();
247                Scheduler potentialScheduler = it.next();
248                if(potentialScheduler.getResources().containsAll(children))
249                        return potentialScheduler;
250                return null;
251
252        }*/
253       
254        public List<QueueDescription> getQueuesDescription(){
255                List<QueueDescription> queues = new ArrayList<QueueDescription>();
256                for(TaskQueue queue: managementSystem.getQueues()){
257                        QueueDescription qd;
258                        try {
259                                qd = new QueueDescription(queue.getName(), queue.getPriority(), queue.supportReservations(), getQueueLoad(queue.getName()));
260                                queues.add(qd);
261                        } catch (NoSuchFieldException e) {;
262                        }
263                }
264                return queues;
265
266        }
267        private long getQueueLoad(String queueName) throws NoSuchFieldException{
268                Map<String, Integer> queue_size = managementSystem.getQueuesSize();
269                long load = 0;
270                if(queueName == null){
271                        for(String queue: queue_size.keySet()){
272                                load += queue_size.get(queue);
273                        }
274                        return load;
275                }
276                else if(queue_size.containsKey(queueName))
277                        return queue_size.get(queueName);
278                else
279                        throw new NoSuchFieldException("Queue " + queueName +
280                                                        " is not available in resource " + get_name());
281        }
282
283        public ResourceType getType() {
284                return this.type;
285        }
286
287        public ResourceStatus getStatus() {
288                return status;
289        }
290       
291        public void setStatus(ResourceStatus newStatus) {
292                status = newStatus;
293        }
294       
295        public void getResourceDescription(){
296               
297        }
298
299        public void initiate() {
300
301        }
302}
Note: See TracBrowser for help on using the repository browser.