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

Revision 493, 6.7 KB checked in by wojtekp, 13 years ago (diff)
  • Property svn:mime-type set to text/plain
RevLine 
[477]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
[490]10import dcworms.schedframe.scheduling.ExecTask;
11import dcworms.schedframe.scheduling.Executable;
12import dcworms.schedframe.scheduling.queues.AbstractStatsSupportingQueue;
13
[477]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;
[481]21import schedframe.scheduling.manager.tasks.JobRegistry;
[477]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.ResourceAllocationInterface;
29import schedframe.scheduling.queue.TaskQueue;
30import schedframe.scheduling.queue.TaskQueueList;
31import schedframe.scheduling.tasks.Job;
[481]32import schedframe.scheduling.tasks.TaskInterface;
[477]33import schedframe.scheduling.tasks.WorkloadUnit;
[481]34import simulator.DCWormsConstants;
[477]35import eduni.simjava.Sim_event;
36import gridsim.GridSim;
37import gridsim.GridSimTags;
38import gridsim.IO_data;
[493]39import gridsim.dcworms.DCWormsTags;
[477]40
41public abstract class AbstractManagementSystem {
42
43        private Log log = LogFactory.getLog(AbstractManagementSystem.class);
44       
45        protected String name;
46
[481]47        protected TaskQueueList queues;
[477]48        protected ResourceManager resourceManager;
[481]49        protected JobRegistryImpl jobRegistry;
50        protected ModuleList moduleList;
51       
[477]52        protected SchedulingPlugin schedulingPlugin;
53        protected ExecutionTimeEstimationPlugin execTimeEstimationPlugin;
54
[481]55        protected Scheduler scheduler;
[477]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       
[481]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       
[477]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       
[481]94       
[477]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 ResourceAllocationInterface getAllocationManager() {
107                if (resourceManager instanceof ResourceAllocationInterface)
108                        return (ResourceAllocationInterface) resourceManager;
109                else
110                        return null;
111        }
112
113        protected JobRegistry getJobRegistry(){
114                return jobRegistry;
115        }
[481]116       
117        public Scheduler getScheduler() {
118                return scheduler;
119        }
120       
121        public PluginConfiguration getSchedulingPluginConfiguration() {
122                return schedulingPlugin.getConfiguration();
123        }
[477]124
125        public boolean pluginSupportsEvent(int eventType){
126                return true;
127        }
128
[481]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       
[477]141        //POPRAWIC  (ale co? bo teraz chyba jest ok)
[481]142        protected void submitTask(TaskInterface<?> task, AllocationInterface<?> allocation) {
[477]143                String providerName = allocation.getProviderName();
144                if (providerName == null) {
145                        return;
146                }
[481]147                removeFromQueue(task);
148                scheduler.send(providerName, GridSimTags.SCHEDULE_NOW, GridSimTags.GRIDLET_SUBMIT, task);       
[477]149        }
150
[478]151        protected boolean sendFinishedWorkloadUnit(WorkloadUnit wu) {
[477]152               
153                Executable exec = (Executable) wu;
154                if(scheduler.getParent() == null)
155                {
[480]156                        Job job = jobRegistry.getJob(exec.getJobId());
[477]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
[481]183                scheduler.sendInternal(Long.valueOf(delay).doubleValue(), DCWormsTags.TASK_READY_FOR_EXECUTION,
[477]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;
[481]195                                        scheduler.sendInternal(delay, DCWormsTags.TIMER, null);
[477]196                                }
197                        }
198                }
199        }
200       
[490]201        protected boolean removeFromQueue(TaskInterface<?> task) {
[477]202                for(TaskQueue queue : queues){
[490]203                        if(queue.contains(task)){
204                                queue.remove(task);
[477]205                                return true;
206                        }
207                }
208                return false;
209        }
210       
[481]211        public abstract WorkloadUnitHandler getWorkloadUnitHandler();
212       
213        public abstract void notifySubmittedWorkloadUnit(WorkloadUnit wu, boolean ack);
[477]214
[481]215        public abstract void notifyReturnedWorkloadUnit(WorkloadUnit wu);
[477]216
[481]217        protected abstract void executeSchedulingPlan(SchedulingPlanInterface<?> decision);
[477]218
219}
Note: See TracBrowser for help on using the repository browser.