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

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