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

Revision 1362, 8.5 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                /*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                        // process the received event
139                        processRequest(ev);
140                        sim_get_next(ev);
141                }
142        }
143       
144        protected void processRequest(Sim_event ev) {
145                switch (ev.get_tag()) {
146
147                case GridSimTags.GRIDLET_SUBMIT:
148                        processWorkloadUnitSubmit(ev);
149                        break;
150
151
152                case GridSimTags.GRIDLET_RETURN:
153                        processWorkloadUnitReturn(ev);
154                        break;
155
156                default:
157                        processOtherRequest(ev);
158                        break;
159                }
160        }
161
162        protected void processOtherRequest(Sim_event ev) {
163                switch (ev.get_tag()) {
164                case DCWormsTags.QUERY_RESOURCE_DESC:
165                        SchedulerDescription desc = new SchedulerDescription(new LocalSystem(get_name(), null, null));
166                        Map<ResourceUnitName, List<ResourceUnit>> units = managementSystem.getResourceManager().getSharedResourceUnits();
167                        desc.addResourceUnitList(units);
168                        desc.addQueuesDescription(getQueuesDescription());
169
170                        IO_data data = new IO_data(desc, 0, ev.get_src());
171                        send(ev.get_src(), GridSimTags.SCHEDULE_NOW, DCWormsTags.QUERY_RESOURCE_DESC_RESULT, data);
172                        break;
173
174                default:
175                        managementSystem.processEvent(ev);
176                        break;
177                }
178        }
179
180        protected void processWorkloadUnitReturn(Sim_event ev) {
181                WorkloadUnit job = (WorkloadUnit) ev.get_data();
182                managementSystem.notifyReturnedWorkloadUnit(job);
183        }
184
185        protected void processWorkloadUnitSubmit(Sim_event ev) {
186                WorkloadUnit job = (WorkloadUnit) ev.get_data();
187                managementSystem.notifySubmittedWorkloadUnit(job);
188        }
189       
190        public void send(String entityName, double delay, int tag, Object data){
191                super.send(entityName, delay, tag, data);
192        }
193       
194        public void send(int destId, double delay, int tag){
195                super.send(destId, delay, tag);
196        }
197       
198        public void send(int destId, double delay, int tag, Object data){
199                super.send(destId, delay, tag, data);
200        }
201       
202        public void send(Sim_port destPort, double delay, int tag, Object data) {
203                super.send(destPort, delay, tag, data);
204        }
205
206        public void sendInternal(double delay, int tag, Object data) {
207                this.send(this.get_id(), delay, tag, data);
208        }
209       
210        public Sim_port getOutputPort() {
211                return output;
212        }
213
214        /*public Scheduler getScheduler(String resourceName){
215                ComputingResource resourceWithName = null;
216                for(int i = 0 ; i < compResources.size() && resourceWithName == null; i++){
217                        ComputingResource resource = compResources.get(i);
218                        if(resource.getName().equals(resourceName))
219                                resourceWithName = resource;
220                        else
221                                try {
222                                        resourceWithName = resource.getDescendantByName(resourceName);
223                                } catch (ResourceException e) {
224                                        return null;
225                                }
226                }
227                if(resourceWithName == null)
228                        return null;
229                List<ComputingResource> children = resourceWithName.getChildren();
230                Set<Scheduler> childrenSchedulers = new HashSet<Scheduler>();
231                for(ComputingResource child:children) {
232                        childrenSchedulers.add(child.getScheduler());
233                }
234                Set<Scheduler> tempChildrenSchedulers = new HashSet<Scheduler>(childrenSchedulers);
235                while(childrenSchedulers.size() != 1){
236                        childrenSchedulers = new HashSet<Scheduler>();
237                        for(Scheduler s: tempChildrenSchedulers){
238                                childrenSchedulers.add(s.getParent());
239                        }
240                        tempChildrenSchedulers = new HashSet<Scheduler>(childrenSchedulers);
241                }
242                Iterator<Scheduler> it = childrenSchedulers.iterator();
243                Scheduler potentialScheduler = it.next();
244                if(potentialScheduler.getResources().containsAll(children))
245                        return potentialScheduler;
246                return null;
247
248        }*/
249       
250        public List<QueueDescription> getQueuesDescription(){
251                List<QueueDescription> queues = new ArrayList<QueueDescription>();
252                for(TaskQueue queue: managementSystem.getQueues()){
253                        QueueDescription qd;
254                        try {
255                                qd = new QueueDescription(queue.getName(), queue.getPriority(), queue.supportReservations(), getQueueLoad(queue.getName()));
256                                queues.add(qd);
257                        } catch (NoSuchFieldException e) {;
258                        }
259                }
260                return queues;
261
262        }
263        private long getQueueLoad(String queueName) throws NoSuchFieldException{
264                Map<String, Integer> queue_size = managementSystem.getQueuesSize();
265                long load = 0;
266                if(queueName == null){
267                        for(String queue: queue_size.keySet()){
268                                load += queue_size.get(queue);
269                        }
270                        return load;
271                }
272                else if(queue_size.containsKey(queueName))
273                        return queue_size.get(queueName);
274                else
275                        throw new NoSuchFieldException("Queue " + queueName +
276                                                        " is not available in resource " + get_name());
277        }
278
279        public ResourceType getType() {
280                return this.type;
281        }
282
283        public String getFullName() {
284                if(parent!= null){
285                        return parent.getFullName() + "/" + name;
286                } else
287                        return name;
288        }
289       
290        public ResourceStatus getStatus() {
291                return status;
292        }
293       
294        public void setStatus(ResourceStatus newStatus) {
295                status = newStatus;
296        }
297       
298        public void getResourceDescription(){
299               
300        }
301
302        public void initiate() {
303
304        }
305}
Note: See TracBrowser for help on using the repository browser.