package simulator.reader; import gridsim.ResourceCalendar; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Deque; import java.util.HashMap; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; import org.exolab.castor.types.AnyNode; import org.exolab.castor.xml.MarshalException; import org.exolab.castor.xml.ValidationException; import org.qcg.broker.schemas.exception.UnknownParameter; import schedframe.Initializable; import schedframe.Parameter; import schedframe.Parameters; import schedframe.ResourceController; import schedframe.exceptions.ResourceException; import schedframe.resources.Resource; import schedframe.resources.StandardResourceType; import schedframe.resources.computing.ComputingResource; import schedframe.resources.computing.ResourceFactory; import schedframe.resources.computing.description.AbstractResourceDescription; import schedframe.resources.computing.description.ComputingResourceDescription; import schedframe.resources.units.ResourceUnit; import schedframe.resources.units.ResourceUnitName; import schedframe.scheduling.Scheduler; import schedframe.scheduling.manager.resources.ManagedResources; import schedframe.scheduling.plugin.SchedulingPlugin; import schedframe.scheduling.plugin.estimation.ExecutionTimeEstimationPlugin; import schedframe.scheduling.queue.TaskQueue; import schedframe.scheduling.queue.TaskQueueList; import schemas.Environment; import schemas.ManagedComputingResources; import schemas.StringValueWithUnit; import simulator.ConfigurationOptions; import simulator.utils.InstanceFactory; public class ResourceReader { protected String resDescFileName; protected ResourceCalendar resourceCalendar; protected List toInit = new ArrayList(); private ExecutionTimeEstimationPlugin execTimeEstimationPlugin; private String globalSchedulingPluginName; private Set compResLayers; public ResourceReader(ConfigurationOptions options) throws IOException { resDescFileName = options.resdescFileName; globalSchedulingPluginName = "example.globalplugin.GridFCFSRoundRobinPlugin"; prepareCalendar(); compResLayers = new LinkedHashSet(); } public ResourceController read() throws MarshalException, ValidationException, FileNotFoundException, Exception, UnknownParameter { File file = new File(resDescFileName); Environment env = Environment.unmarshal(new FileReader(file)); System.out.println("started creating environment description"); List mainCompResDescList = createEnvironmentDescription(env); System.out.println("finished creating environment description"); System.out.println("started creating resources"); List computingResources = createResources(mainCompResDescList); System.out.println("finished creating resource"); System.out.println("started creating schedulers"); Scheduler mainScheduler = createSchedulers(env.getResources().getScheduler(), computingResources); System.out.println("finished creating schedulers"); ResourceController rc = new ResourceController(mainScheduler, computingResources); Collections.sort(toInit, new ResourceTypeComparator(new ArrayList(compResLayers))); rc.setInitList(toInit); rc.setCompResLayers(compResLayers); return rc; } protected List createEnvironmentDescription(Environment environment) throws Exception { List mainCompResDescList = new ArrayList(); LinkedList resDescStructure = new LinkedList(); LinkedList toExamine = new LinkedList(); EnvironmentWrapper environmentWrapper = new EnvironmentWrapper(); environmentWrapper.wrap(environment); String execTimeEstimationPluginClassName = null; if(environmentWrapper.getTimeEstimationPlugin() != null){ execTimeEstimationPluginClassName = environmentWrapper.getTimeEstimationPlugin().getName(); } try{ execTimeEstimationPlugin = (ExecutionTimeEstimationPlugin) InstanceFactory.createInstance( execTimeEstimationPluginClassName, ExecutionTimeEstimationPlugin.class); if(execTimeEstimationPlugin == null) { execTimeEstimationPluginClassName = "example.timeestimation.DefaultTimeEstimationPlugin"; execTimeEstimationPlugin = (ExecutionTimeEstimationPlugin) InstanceFactory.createInstance( execTimeEstimationPluginClassName, ExecutionTimeEstimationPlugin.class); } else { Parameters params = EnvironmentWrapper.extractParameters(environmentWrapper.getTimeEstimationPlugin().getParameter()); execTimeEstimationPlugin.init(params); } } catch (Exception e){ if (execTimeEstimationPlugin == null) { throw new Exception("Can not create execution time estimation plugin instance."); } } if(environmentWrapper.getComputingResources() != null) { for(int i = 0; i < environmentWrapper.getComputingResources().length; i++){ schemas.ComputingResource compResDef = environmentWrapper.getComputingResources()[i]; toExamine.push(compResDef); ComputingResourceDescription compResDesc = new ComputingResourceDescription(compResDef); resDescStructure.push(compResDesc); mainCompResDescList.add(compResDesc); } } while (!toExamine.isEmpty()) { schemas.ComputingResource parentCompResDef = toExamine.pop(); ComputingResourceDescription parentExecResDesc = resDescStructure.pop(); if(parentCompResDef.getComputingResourceTypeChoiceSequence() != null) { int computingResourceCount = parentCompResDef.getComputingResourceTypeChoiceSequence().getComputingResourceCount(); for (int i = 0; i < computingResourceCount; i++) { schemas.ComputingResource compResDef = parentCompResDef.getComputingResourceTypeChoiceSequence().getComputingResource(i); if(compResDef == null) continue; long compResCount = compResDef.getCount() > 1 ? compResDef.getCount() : 1; for(int j = 0; j < compResCount; j++){ if(compResDef.getComputingResourceTypeChoiceSequence2() != null){ String templateId = compResDef.getComputingResourceTypeChoiceSequence2().getTemplateId(); schemas.ComputingResourceTemplate template = environmentWrapper.getTemplate(templateId); environmentWrapper.initWithCompResTemplate(compResDef, template); } //toExamine.insertElementAt(compResDef, 0); toExamine.addLast(compResDef); ComputingResourceDescription resDesc = new ComputingResourceDescription(compResDef); parentExecResDesc.addChildren(resDesc); //resDescStructure.insertElementAt(resDesc, 0); resDescStructure.addLast(resDesc); } } } else continue; } return mainCompResDescList; } protected List createResources(List mainCompResDesList) { List mainCompResourceList = new ArrayList(); Deque toExamine = new ArrayDeque(); Deque resStructure = new ArrayDeque(); for(ComputingResourceDescription mainExecResDes : mainCompResDesList){ ComputingResource mainResource = ResourceFactory.createResource(mainExecResDes); toExamine.push(mainExecResDes); resStructure.push(mainResource); mainCompResourceList.add(mainResource); } while (!toExamine.isEmpty()) { ComputingResourceDescription parentResDesc = toExamine.pop(); ComputingResource parentResource = resStructure.pop(); toInit.add(parentResource); compResLayers.add(parentResource.getType().getName()); List childrenResDesc = parentResDesc.getChildren(); if (childrenResDesc == null){ continue; } int compResCount = childrenResDesc.size(); for (int i = 0; i < compResCount; i++) { ComputingResourceDescription compResDesc = (ComputingResourceDescription) childrenResDesc.get(i); toExamine.push(compResDesc); ComputingResource resource = ResourceFactory.createResource(compResDesc); parentResource.addChild(resource); resStructure.push(resource); } } return mainCompResourceList; } protected Scheduler createSchedulers(schemas.Scheduler[] schedulersDef, List mainCompResourceList) throws Exception{ List mainSchedulers = new ArrayList(); Deque toExamine = new ArrayDeque(); Deque schedulersStructure = new ArrayDeque(); for(int i = 0; i < schedulersDef.length; i++){ schemas.Scheduler schedulerDef = schedulersDef[i]; Scheduler mainScheduler = initScheduler(schedulerDef, mainCompResourceList); toExamine.push(schedulerDef); schedulersStructure.push(mainScheduler); mainSchedulers.add(mainScheduler); } while (!toExamine.isEmpty()) { schemas.Scheduler parentSchedulerDef = toExamine.pop(); Scheduler parentScheduler = null; if(!schedulersStructure.isEmpty()) parentScheduler = schedulersStructure.pop(); if(parentSchedulerDef.getSchedulerTypeChoice() == null) continue; int schedulerChoiceItemCount = parentSchedulerDef.getSchedulerTypeChoice().getSchedulerTypeChoiceItemCount(); for (int i = 0; i < schedulerChoiceItemCount; i++) { schemas.Scheduler schedulerDef = parentSchedulerDef.getSchedulerTypeChoice().getSchedulerTypeChoiceItem(i).getScheduler(); if(schedulerDef == null) continue; else { Scheduler scheduler = initScheduler(schedulerDef, mainCompResourceList); schedulersStructure.push(scheduler); parentScheduler.addChild(scheduler); toExamine.push(schedulerDef); } } //necessary if children list isn't initialized in Scheduler //parentScheduler.init(); } //TODO - refactor (remove - create scheduler on the basis of resource description) Scheduler mainScheduler = null; if(mainSchedulers.size() == 1 /*&& mainSchedulers.get(0).get_name().equals("grid")*/){ mainScheduler = mainSchedulers.get(0); } else{ SchedulingPlugin schedulingPlugin = (SchedulingPlugin) InstanceFactory.createInstance(globalSchedulingPluginName, SchedulingPlugin.class); TaskQueueList queues = new TaskQueueList(1); TaskQueue queue = new TaskQueue(false); queues.add(queue); ManagedResources managedResources = new ManagedResources(mainCompResourceList, new HashMap>()); mainScheduler = ResourceFactory.createScheduler(StandardResourceType.GS, "grid", schedulingPlugin , execTimeEstimationPlugin, queues, managedResources); for(Scheduler lr: mainSchedulers){ mainScheduler.addChild(lr); } //necessary if children list isn't initialized in Scheduler //mainScheduler.init(); } return mainScheduler; } protected Scheduler initScheduler(schemas.Scheduler schedulerDef, List mainCompResourceList) throws Exception{ Scheduler scheduler = null; ManagedResources managedResources = null; //List managedCompResources = new ArrayList(); TaskQueueList queues = new TaskQueueList(1); if(schedulerDef.getQueues()!= null){ int queueCount = schedulerDef.getQueues().getQueueCount(); for(int i = 0; i < queueCount; i++){ schemas.QueueType queueDef = schedulerDef.getQueues().getQueue(i); TaskQueue queue = new TaskQueue(queueDef.getReservation()); queue.setName(queueDef.getName()); queue.setPriority(queueDef.getPriority()); queues.add(queue); } } else { TaskQueue queue = new TaskQueue(false); queues.add(queue); } if(schedulerDef.getSchedulerTypeChoice() != null) { int schedulerChoiceItemCount = schedulerDef.getSchedulerTypeChoice().getSchedulerTypeChoiceItemCount(); for (int i = 0; i < schedulerChoiceItemCount; i++) { ManagedComputingResources mcr = schedulerDef.getSchedulerTypeChoice().getSchedulerTypeChoiceItem(i).getManagedComputingResources(); if(mcr == null) continue; List managedCompResourcesIds = new ArrayList(); int resourceNameCount = mcr.getResourceNameCount(); for(int j = 0; j < resourceNameCount; j++) { managedCompResourcesIds.add(mcr.getResourceName(j)); } managedResources = matchResourcesForScheduler(mainCompResourceList, managedCompResourcesIds, mcr.getInclude()); //managedResources = new ManagedResources(managedCompResources, new HashMap>()); } } SchedulingPlugin schedulingPlugin = null; if(schedulerDef.getSchedulingPlugin() != null){ String schedulingPluginName = schedulerDef.getSchedulingPlugin().getName(); schedulingPlugin = (SchedulingPlugin) InstanceFactory.createInstance(schedulingPluginName, SchedulingPlugin.class); Parameters params = EnvironmentWrapper.extractParameters(schedulerDef.getSchedulingPlugin().getParameter()); if(schedulerDef.getSchedulingPlugin().getFrequency() != null) { Parameter param = new Parameter("frequency"); StringValueWithUnit sv = new StringValueWithUnit(); sv.setContent(String.valueOf(schedulerDef.getSchedulingPlugin().getFrequency().getContent())); sv.setUnit(schedulerDef.getSchedulingPlugin().getFrequency().getUnit()); param.add(sv); if(params == null) params = new Parameters(); params.put("frequency", param); } schedulingPlugin.init(params); } //TODO - refactor (create scheduler in 1 line) if(schedulerDef.getClazz().equals("GridBroker")){ scheduler = ResourceFactory.createScheduler(StandardResourceType.GS, "grid", schedulingPlugin, execTimeEstimationPlugin, queues, managedResources); } else { scheduler = ResourceFactory.createScheduler(StandardResourceType.LS, schedulerDef.getName(), schedulingPlugin, execTimeEstimationPlugin, queues, managedResources); } return scheduler; } /*private List matchCompResourcesForScheduler(List mainCompResourceList, List resIdList, boolean include){ List compResources = new ArrayList(); for(ComputingResource mainCompRes: mainCompResourceList){ for(String resourceName : resIdList){ ComputingResource computingResource; try { if(resourceName.equals(mainCompRes.getName())) computingResource = mainCompRes; else computingResource = mainCompRes.getDescendantByName(resourceName); } catch (ResourceException e) { computingResource = null; } if(computingResource != null) { if(include){ compResources.add(computingResource); } else{ compResources.addAll(computingResource.getChildren()); } } } } return compResources; }*/ private ManagedResources matchResourcesForScheduler(List mainCompResourceList, List resIdList, boolean include){ List compResources = new ArrayList(); Map> resourceUnits = new HashMap>(); for(ComputingResource mainCompRes: mainCompResourceList){ for(String resourceName : resIdList){ ComputingResource computingResource; try { if(resourceName.equals(mainCompRes.getName())) computingResource = mainCompRes; else computingResource = mainCompRes.getDescendantByName(resourceName); } catch (ResourceException e) { continue; } if(computingResource != null) { if(include){ compResources.add(computingResource); } else{ compResources.addAll(computingResource.getChildren()); resourceUnits = getSharedResourceUnits(computingResource); } } } } return new ManagedResources(compResources, resourceUnits); } private Map> getSharedResourceUnits(ComputingResource compResources){ Map> resourceUnits = new HashMap>(); List list; boolean resourceNotVisited = true; ComputingResource parent = compResources; while(parent != null && resourceNotVisited){ Map> resUnits = parent.getResourceCharacteristic().getResourceUnits(); for(ResourceUnitName run : resUnits.keySet()){ for(ResourceUnit resUnit : resUnits.get(run)){ if((resourceUnits.get(run) == null)){ list = new ArrayList(1); resourceUnits.put(resUnit.getName(), list); list.add(resUnit); } else if(!resourceUnits.get(run).contains(resUnit)){ list = resourceUnits.get(resUnit.getName()); list.add(resUnit); } else { resourceNotVisited = false; } } } parent = parent.getParent(); } return resourceUnits; } private void prepareCalendar() { long seed = 11L * 13L * 17L * 19L * 23L + 1L; double timeZone = 0.0; double peakLoad = 0.0; // the resource load during peak hour double offPeakLoad = 0.0; // the resource load during off-peak hr double holidayLoad = 0.0; // the resource load during holiday // incorporates weekends so the grid resource is on 7 days a week LinkedList Weekends = new LinkedList(); Weekends.add(java.util.Calendar.SATURDAY); Weekends.add(java.util.Calendar.SUNDAY); // incorporates holidays. However, no holidays are set in this example LinkedList Holidays = new LinkedList(); resourceCalendar = new ResourceCalendar(timeZone, peakLoad, offPeakLoad, holidayLoad, Weekends, Holidays, seed); } class ResourceTypeComparator implements Comparator{ List order; ResourceTypeComparator(List order) { this.order = order; } public int compare(Initializable init1, Initializable init2) { String type1 = ((Resource)init1).getType().getName(); String type2 = ((Resource)init2).getType().getName(); if(order.indexOf(type1) > order.indexOf(type2)) return -1; else if (order.indexOf(type1) < order.indexOf(type2)) return 1; else return 0; } } }