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

Revision 1440, 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.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.ModuleList;
26import schedframe.scheduling.plugin.SchedulingPlugin;
27import schedframe.scheduling.plugin.estimation.ExecutionTimeEstimationPlugin;
28import schedframe.scheduling.manager.resources.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 TaskQueueList getQueues(){
127                return queues;
128        }
129       
130        public Map<String, Integer> getQueuesSize() {
131                Map<String, Integer> queue_size = new HashMap<String, Integer>();
132                for (TaskQueue queue : queues) {
133                        queue_size.put(queue.getName(), queue.size());
134                }
135                return queue_size;
136        }
137       
138        //POPRAWIC  (ale co? bo teraz chyba jest ok)
139        protected void submitTask(TaskInterface<?> task, AllocationInterface<?> allocation) {
140                String providerName = allocation.getProviderName();
141                if (providerName == null) {
142                        return;
143                }
144                removeFromQueue(task);
145                scheduler.send(providerName, GridSimTags.SCHEDULE_NOW, GridSimTags.GRIDLET_SUBMIT, task);       
146        }
147
148        protected boolean sendFinishedWorkloadUnit(WorkloadUnit wu) {
149               
150                Executable exec = (Executable) wu;
151                if(scheduler.getParent() == null)
152                {
153                        Job job = jobRegistry.getJob(exec.getJobId());
154
155                        if(job.isFinished()){
156                                scheduler.send(job.getSenderId(), GridSimTags.SCHEDULE_NOW, GridSimTags.GRIDLET_RETURN, job);
157                                return true;
158                        }
159                        else return true;
160                }
161       
162        IO_data obj = new IO_data(exec, 0, /*task.getGridletOutputSize(),*/ GridSim.getEntityId(scheduler.getParent().get_name()));
163        scheduler.send(scheduler.getOutputPort(), 0.0, GridSimTags.GRIDLET_RETURN, obj);
164                return true;
165        }
166
167        protected void sendExecutableReadyEvent(ExecTask exec) {
168
169                long delay = 0;
170                try {
171                        long expectedStartTime = exec.getExecutionStartTime().getMillis() / 1000;
172                        long currentTime = DateTimeUtilsExt.currentTimeMillis() / 1000;
173                        delay = expectedStartTime - currentTime;
174                        if (delay < 0)
175                                delay = 0;
176                } catch (NoSuchFieldException e) {
177                        delay = 0;
178                }
179
180                scheduler.sendInternal(Long.valueOf(delay).doubleValue(), DCWormsTags.TASK_READY_FOR_EXECUTION,
181                                exec);
182        }
183       
184        protected void sendTimerEvent() {
185                PluginConfiguration pluginConfig = schedulingPlugin.getConfiguration();
186                if (pluginConfig != null) {
187                        Map<SchedulingEventType, Object> events = pluginConfig.getServedEvents();
188                        if (events != null) {
189                                Object obj = events.get(SchedulingEventType.TIMER);
190                                if (obj != null) {
191                                        int delay = (Integer) obj;
192                                        scheduler.sendInternal(delay, DCWormsTags.TIMER, null);
193                                }
194                        }
195                }
196        }
197       
198        protected boolean removeFromQueue(TaskInterface<?> task) {
199                for(TaskQueue queue : queues){
200                        if(queue.contains(task)){
201                                queue.remove(task);
202                                return true;
203                        }
204                }
205                return false;
206        }
207       
208        public abstract WorkloadUnitHandler getWorkloadUnitHandler();
209       
210        public abstract void notifySubmittedWorkloadUnit(WorkloadUnit wu);
211
212        public abstract void notifyReturnedWorkloadUnit(WorkloadUnit wu);
213
214        protected abstract void executeSchedulingPlan(SchedulingPlanInterface<?> decision);
215
216}
Note: See TracBrowser for help on using the repository browser.