package test.rewolucja.scheduling.implementation; import java.util.HashMap; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.joda.time.DateTimeUtilsExt; import schedframe.scheduling.JobInterface; import schedframe.scheduling.TaskInterface; import schedframe.scheduling.events.SchedulingEvent; import schedframe.scheduling.events.SchedulingEventType; import schedframe.scheduling.plugin.SchedulingPluginConfiguration; import schedframe.scheduling.plugin.estimation.ExecTimeEstimationPlugin; import schedframe.scheduling.plugin.grid.ModuleList; import schedframe.scheduling.plugin.local.ResourceAllocationInterface; import schedframe.scheduling.utils.ResourceParameterName; import test.rewolucja.GSSIMJobInterface; import test.rewolucja.resources.description.ExecResourceDescription; import test.rewolucja.resources.logical.LogicalResource; import test.rewolucja.resources.manager.factory.ResourceManagerFactory; import test.rewolucja.resources.manager.interfaces.ResourceManagerInterface; import test.rewolucja.scheduling.JobRegistry; import test.rewolucja.scheduling.JobRegistryInterface; import test.rewolucja.scheduling.interfaces.SchedulingPlugin; import test.rewolucja.scheduling.plan.AllocationInterfaceNew; import test.rewolucja.scheduling.plan.SchedulingPlanInterfaceNew; import test.rewolucja.scheduling.queue.GSSIMQueue; import test.rewolucja.scheduling.queue.QueueList; import eduni.simjava.Sim_event; import gridsim.GridSim; import gridsim.GridSimTags; import gridsim.Gridlet; import gridsim.IO_data; import gridsim.gssim.GssimTags; import gridsim.gssim.SubmittedTask; import gssim.schedframe.scheduling.AbstractExecutable; public abstract class ManagementSystem { private Log log = LogFactory.getLog(ManagementSystem.class); protected String name; protected ResourceManagerInterface resourceManager; protected QueueList queues; protected SchedulingPlugin schedulingPlugin; protected ExecTimeEstimationPlugin forecastFinishTimePlugin; protected ModuleList moduleList; protected JobRegistry jobRegistry; public ManagementSystem(String providerId, String entityName, String schedulingPluginClassName, ExecTimeEstimationPlugin execTimeEstimationPlugin, ExecResourceDescription resourceDescription) throws Exception { name = entityName + "@" + providerId; jobRegistry = new JobRegistry(name); forecastFinishTimePlugin = execTimeEstimationPlugin; if (forecastFinishTimePlugin == null) { throw new Exception("Can not create execution time estimation plugin instance."); } if(resourceDescription == null){ queues = createAccessQueues(); } else{ queues = resourceDescription.getAccessQueues(); // set_stat(GssimConstants.getResourcesStatisticsObject(queues.size())); for (int i = 0; i < queues.size(); i++) { GSSIMQueue q = queues.get(i); /*if (q instanceof AbstractStatsSupportingQueue) { AbstractStatsSupportingQueue queue = (AbstractStatsSupportingQueue) q; // queue.setStats(this.get_stat(), // GssimConstants.TASKS_QUEUE_LENGTH_MEASURE_NAME+"_"+Integer.toString(i)); }*/ } } } public void processEvent(Sim_event ev) { processOtherEvent(ev); } protected void processOtherEvent(Sim_event ev) { if (ev == null) { System.out.println(name + ".processOtherEvent(): " + "Error - an event is null."); return; } log.error(name + ".processOtherEvent(): Unable to " + "handle request from an event with a tag number " + ev.get_tag()); } public String getName() { return name; } public SchedulingPluginConfiguration getPluginConfiguration() { return (SchedulingPluginConfiguration)schedulingPlugin.getConfiguration(); } public ResourceManagerInterface getResourceManager() { if (resourceManager instanceof ResourceManagerInterface) return (ResourceManagerInterface) resourceManager; else return null; } public ResourceAllocationInterface getAllocationManager() { if (resourceManager instanceof ResourceAllocationInterface) return (ResourceAllocationInterface) resourceManager; else return null; } protected JobRegistryInterface getJobRegistry(){ return jobRegistry; } public abstract boolean pluginSupportsEvent(int eventType); public abstract void notifySubmittedJob(GSSIMJobInterface gssimJob, boolean ack); public abstract void notifyCanceledJob(GSSIMJobInterface gssimJob); public abstract void notifyReturnedJob(GSSIMJobInterface gssimJob); protected abstract void executeSchedulingPlan(SchedulingPlanInterfaceNew decision); protected void schedule(SchedulingEvent schedulingEvent) { try { SchedulingPlanInterfaceNew decision = schedulingPlugin.schedule( schedulingEvent, queues, getJobRegistry(), getResourceManager(), moduleList); if (decision == null) return; executeSchedulingPlan(decision); } catch (Exception e) { e.printStackTrace(); } } //POPRAWIC protected void submitJob(GSSIMJobInterface gssimJob, AllocationInterfaceNew allocation) { String providerName = allocation.getProviderName(); if (providerName == null) { return; } SubmittedTask subTask = (SubmittedTask) gssimJob; logicalResource.send(providerName, GridSimTags.SCHEDULE_NOW, GridSimTags.GRIDLET_SUBMIT, subTask.getGridlet()); //logicalResource.send(providerName, GridSimTags.SCHEDULE_NOW, GridSimTags.GRIDLET_SUBMIT, gssimJob); } protected boolean sendCancelJob(int tag, AbstractExecutable task, int executableId, int destId) { if (tag != GridSimTags.GRIDLET_CANCEL) { return false; } long taskSize = 0; if (task != null) { taskSize = task.getGridletOutputSize(); } // if no Gridlet found, then create a new Gridlet but set its status // to FAILED. Then, most importantly, set the resource parameters // because the user will search/filter based on a resource ID. else if (task == null) { try { taskSize = 100; task = jobRegistry.getTaskExecutable(executableId); task.setGridletStatus(Gridlet.FAILED); int cost = resourceManager.getResourceCharacteristic().getResourceUnits() != null ? resourceManager .getResourceCharacteristic().getResourceUnits().get(ResourceParameterName.COST).get(0).getAmount() : 1; task.setResourceParameter(logicalResource.get_id(), cost); } catch (Exception e) { // empty ... } } logicalResource.send(logicalResource.getOutputPort(), GridSimTags.SCHEDULE_NOW, tag, new IO_data(task, taskSize, destId)); return true; } protected boolean sendFinishJob(AbstractExecutable task) { IO_data obj = new IO_data(task, task.getGridletOutputSize(), GridSim.getEntityId(logicalResource.getParent().get_name())); logicalResource.send(logicalResource.getOutputPort(), 0.0, GridSimTags.GRIDLET_RETURN, obj); return true; } protected void sendJobReadyEvent(GSSIMJobInterface gssimJob) { if (gssimJob instanceof JobInterface) { logicalResource.sendInternal(Long.valueOf(0).doubleValue(), GssimTags.TASK_READY_FOR_EXECUTION, gssimJob); return; } AbstractExecutable executable = (AbstractExecutable) gssimJob; long delay = 0; try { long expectedStartTime = executable.getExecutionStartTime().getMillis() / 1000; long currentTime = DateTimeUtilsExt.currentTimeMillis() / 1000; delay = expectedStartTime - currentTime; if (delay < 0) delay = 0; } catch (NoSuchFieldException e) { delay = 0; } logicalResource.sendInternal(Long.valueOf(delay).doubleValue(), GssimTags.TASK_READY_FOR_EXECUTION, gssimJob); } protected void sendTimerEvent() { SchedulingPluginConfiguration pluginConfig = (SchedulingPluginConfiguration)schedulingPlugin.getConfiguration(); if (pluginConfig != null) { Map events = pluginConfig.getServedEvents(); if (events != null) { Object obj = events.get(SchedulingEventType.TIMER); if (obj != null) { int delay = (Integer) obj; logicalResource.sendInternal(delay, GssimTags.TIMER, null); } } } } protected boolean removeFromQueue(TaskInterface task) { for(GSSIMQueue queue : queues){ if(queue.contains(task)){ queue.remove(task); return true; } } return false; } private QueueList createAccessQueues(){ QueueList queues = new QueueList(); GSSIMQueue queue = new GSSIMQueue(false); queues.add(queue); return queues; } public Map getQueuesSize() { Map queue_size = new HashMap(); for (GSSIMQueue queue : queues) { queue_size.put(queue.getName(), queue.size()); } return queue_size; } public void init(LogicalResource logRes) { logicalResource = logRes; resourceManager = ResourceManagerFactory.createResourceManager(logicalResource); } protected LogicalResource logicalResource; public LogicalResource getLogicalResource() { return logicalResource; } }