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

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