source: DCWoRMS/branches/coolemall/src/schedframe/scheduling/policy/AbstractManagementSystem.java @ 1447

Revision 1447, 6.6 KB checked in by wojtekp, 11 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package schedframe.scheduling.policy;
2
3import java.util.HashMap;
4import java.util.Map;
5
6import org.apache.commons.logging.Log;
7import org.apache.commons.logging.LogFactory;
8import org.joda.time.DateTimeUtilsExt;
9
10import dcworms.schedframe.scheduling.ExecTask;
11import dcworms.schedframe.scheduling.Executable;
12import dcworms.schedframe.scheduling.queues.AbstractStatsSupportingQueue;
13
14import schedframe.PluginConfiguration;
15import schedframe.events.EventType;
16import schedframe.events.scheduling.SchedulingEventType;
17import schedframe.scheduling.Scheduler;
18import schedframe.scheduling.WorkloadUnitHandler;
19import schedframe.scheduling.manager.resources.ManagedResources;
20import schedframe.scheduling.manager.resources.ResourceManager;
21import schedframe.scheduling.manager.resources.ResourceManagerFactory;
22import schedframe.scheduling.manager.tasks.JobRegistry;
23import schedframe.scheduling.manager.tasks.JobRegistryImpl;
24import schedframe.scheduling.plan.AllocationInterface;
25import schedframe.scheduling.plan.SchedulingPlanInterface;
26import schedframe.scheduling.plugin.ModuleList;
27import schedframe.scheduling.plugin.SchedulingPlugin;
28import schedframe.scheduling.plugin.estimation.ExecutionTimeEstimationPlugin;
29import schedframe.scheduling.manager.resources.ResourceAllocation;
30import schedframe.scheduling.queue.TaskQueue;
31import schedframe.scheduling.queue.TaskQueueList;
32import schedframe.scheduling.tasks.Job;
33import schedframe.scheduling.tasks.TaskInterface;
34import schedframe.scheduling.tasks.WorkloadUnit;
35import simulator.DCWormsConstants;
36import eduni.simjava.Sim_event;
37import gridsim.GridSim;
38import gridsim.GridSimTags;
39import gridsim.IO_data;
40import gridsim.dcworms.DCWormsTags;
41
42public abstract class AbstractManagementSystem {
43
44        private Log log = LogFactory.getLog(AbstractManagementSystem.class);
45       
46        protected String name;
47
48        protected TaskQueueList queues;
49        protected ResourceManager resourceManager;
50        protected JobRegistryImpl jobRegistry;
51        protected ModuleList moduleList;
52       
53        protected SchedulingPlugin schedulingPlugin;
54        protected ExecutionTimeEstimationPlugin execTimeEstimationPlugin;
55
56        protected Scheduler scheduler;
57       
58
59        public AbstractManagementSystem(String providerId, String entityName,
60                        ExecutionTimeEstimationPlugin execTimeEstPlugin, TaskQueueList queues) {
61               
62                //this.name = entityName + "@" + providerId;
63                this.name = providerId;
64                this.queues = queues;
65                this.jobRegistry = new JobRegistryImpl(name);
66                this.execTimeEstimationPlugin = execTimeEstPlugin;
67        }
68       
69        public void init(Scheduler sched, ManagedResources managedResources) {
70                scheduler = sched;
71                resourceManager = ResourceManagerFactory.createResourceManager(scheduler, managedResources);
72                scheduler.set_stat(DCWormsConstants.getResourcesStatisticsObject(queues.size()));
73                for(int i = 0; i < queues.size(); i++){
74                        TaskQueue q = queues.get(i);
75                        if(q instanceof AbstractStatsSupportingQueue<?>){
76                                AbstractStatsSupportingQueue<?> queue = (AbstractStatsSupportingQueue<?>) q;
77                                queue.setStats(scheduler.get_stat(), DCWormsConstants.TASKS_QUEUE_LENGTH_MEASURE_NAME + "_" + Integer.toString(i));
78                        }
79                }
80        }
81       
82        public void processEvent(Sim_event ev) {
83                processOtherEvent(ev);
84        }
85
86        protected void processOtherEvent(Sim_event ev) {
87                if (ev == null) {
88                        System.out.println(name + ".processOtherEvent(): " + "Error - an event is null.");
89                        return;
90                }
91
92                log.error(name + ".processOtherEvent(): Unable to "
93                                + "handle request from an event with a tag number " + ev.get_tag());
94        }
95       
96       
97        public String getName() {
98                return name;
99        }
100       
101        public ResourceManager getResourceManager() {
102                if (resourceManager instanceof ResourceManager)
103                        return (ResourceManager) resourceManager;
104                else
105                        return null;
106        }
107
108        public ResourceAllocation getAllocationManager() {
109                if (resourceManager instanceof ResourceAllocation)
110                        return (ResourceAllocation) resourceManager;
111                else
112                        return null;
113        }
114
115        protected JobRegistry getJobRegistry(){
116                return jobRegistry;
117        }
118       
119        public Scheduler getScheduler() {
120                return scheduler;
121        }
122       
123        public PluginConfiguration getSchedulingPluginConfiguration() {
124                return schedulingPlugin.getConfiguration();
125        }
126
127        public TaskQueueList getQueues(){
128                return queues;
129        }
130       
131        public Map<String, Integer> getQueuesSize() {
132                Map<String, Integer> queue_size = new HashMap<String, Integer>();
133                for (TaskQueue queue : queues) {
134                        queue_size.put(queue.getName(), queue.size());
135                }
136                return queue_size;
137        }
138       
139        //POPRAWIC  (ale co? bo teraz chyba jest ok)
140        protected void submitTask(TaskInterface<?> task, AllocationInterface<?> allocation) {
141                String providerName = allocation.getProviderName();
142                if (providerName == null) {
143                        return;
144                }
145                removeFromQueue(task);
146                scheduler.send(providerName, GridSimTags.SCHEDULE_NOW, GridSimTags.GRIDLET_SUBMIT, task);       
147        }
148
149        protected boolean sendFinishedWorkloadUnit(WorkloadUnit wu) {
150               
151                Executable exec = (Executable) wu;
152                if(scheduler.getParent() == null)
153                {
154                        Job job = jobRegistry.getJob(exec.getJobId());
155
156                        if(job.isFinished()){
157                                scheduler.send(job.getSenderId(), GridSimTags.SCHEDULE_NOW, GridSimTags.GRIDLET_RETURN, job);
158                                return true;
159                        }
160                        else return true;
161                }
162       
163        IO_data obj = new IO_data(exec, 0, /*task.getGridletOutputSize(),*/ GridSim.getEntityId(scheduler.getParent().get_name()));
164        scheduler.send(scheduler.getOutputPort(), 0.0, GridSimTags.GRIDLET_RETURN, obj);
165                return true;
166        }
167
168        protected void sendExecutableReadyEvent(ExecTask exec) {
169
170                long delay = 0;
171                try {
172                        long expectedStartTime = exec.getExecutionStartTime().getMillis() / 1000;
173                        long currentTime = DateTimeUtilsExt.currentTimeMillis() / 1000;
174                        delay = expectedStartTime - currentTime;
175                        if (delay < 0)
176                                delay = 0;
177                } catch (NoSuchFieldException e) {
178                        delay = 0;
179                }
180
181                scheduler.sendInternal(Long.valueOf(delay).doubleValue(), DCWormsTags.TASK_READY_FOR_EXECUTION,
182                                exec);
183        }
184       
185        protected void sendTimerEvent() {
186                PluginConfiguration pluginConfig = schedulingPlugin.getConfiguration();
187                if (pluginConfig != null) {
188                        Map<EventType, Object> events = pluginConfig.getServedEvents();
189                        if (events != null) {
190                                Object obj = events.get(SchedulingEventType.TIMER);
191                                if (obj != null) {
192                                        int delay = (Integer) obj;
193                                        scheduler.sendInternal(delay, DCWormsTags.TIMER, null);
194                                }
195                        }
196                }
197        }
198       
199        protected boolean removeFromQueue(TaskInterface<?> task) {
200                for(TaskQueue queue : queues){
201                        if(queue.contains(task)){
202                                queue.remove(task);
203                                return true;
204                        }
205                }
206                return false;
207        }
208       
209        public abstract WorkloadUnitHandler getWorkloadUnitHandler();
210       
211        public abstract void notifySubmittedWorkloadUnit(WorkloadUnit wu);
212
213        public abstract void notifyReturnedWorkloadUnit(WorkloadUnit wu);
214
215        protected abstract void executeSchedulingPlan(SchedulingPlanInterface<?> decision);
216
217}
Note: See TracBrowser for help on using the repository browser.