source: DCWoRMS/branches/toulouse/src/schedframe/scheduling/policy/AbstractManagementSystem.java @ 579

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