source: DCWoRMS/trunk/src/schedframe/scheduling/policy/AbstractManagementSystem.java @ 517

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