package test.rewolucja.resources.reader; import gridsim.ResourceCalendar; import gridsim.gssim.GssimConstants; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.LinkedList; import java.util.List; import java.util.Stack; import org.exolab.castor.xml.MarshalException; import org.exolab.castor.xml.ValidationException; import org.qcg.broker.schemas.exception.UnknownParameter; import org.qcg.broker.schemas.hostparams.HostParameters; import org.qcg.broker.schemas.hostparams.wrapper.HostParametersWrapper; import org.qcg.broker.schemas.hostparams.wrapper.impl.HostParametersWrapperImpl; import schedframe.scheduling.plugin.estimation.ExecTimeEstimationPlugin; import simulator.ConfigurationOptions; import simulator.utils.InstanceFactory; import test.rewolucja.resources.ResourceType; import test.rewolucja.resources.description.AbstractResourceDescription; import test.rewolucja.resources.description.ExecResourceDescription; import test.rewolucja.resources.logical.LogicalResource; import test.rewolucja.resources.physical.base.ComputingResource; import test.rewolucja.resources.physical.factory.ResourceFactory; import test.rewolucja.resources.reader.test.TempResourceDescriptionReader; import test.rewolucja.resources.utils.ResourceController; import test.rewolucja.scheduling.implementation.GridBrokerNew; import test.rewolucja.scheduling.implementation.LocalManagementSystem; import test.rewolucja.scheduling.implementation.ManagementSystem; public class ResourceReaderNew { protected String forecastFinishTimePluginName; protected String globalSchedulingPluginName; protected String localSchedulingPluginName; protected String resDescFileName; protected String envDescFileName; protected ResourceCalendar resourceCalendar; protected ExecTimeEstimationPlugin execTimeEstimationPlugin; private List resourceLayers; public ResourceReaderNew(ConfigurationOptions options) throws IOException { forecastFinishTimePluginName = options.exectimeestimationplugin; globalSchedulingPluginName = options.gridSchedulingPluginName; localSchedulingPluginName = options.localAllocPolicyPluginName; resDescFileName = options.resdescFileName; envDescFileName = options.envDescFileName; prepareCalendar(); } public ResourceController read() throws MarshalException, ValidationException, FileNotFoundException, UnknownParameter { File file = new File(resDescFileName); HostParameters resourceDesription = HostParameters.unmarshalHostParameters(new FileReader(file)); HostParametersWrapper wrapper = new HostParametersWrapperImpl(); wrapper.wrap(resourceDesription); org.qcg.broker.schemas.hostparams.ComputingResource compResources[] = null; compResources = wrapper.getStandaloneComputingResources(); TempResourceDescriptionReader resourceDescriptionReader = TempResourceDescriptionReader .getConfiguration(envDescFileName); System.out.println("started creating environment description" + System.currentTimeMillis()); ExecResourceDescription headExecResDesc = createEnvironmentDescription(resourceDescriptionReader, compResources[0]); System.out.println("finished creating environment description" + System.currentTimeMillis()); System.out.println("started creating resources" + System.currentTimeMillis()); ComputingResource resource = createResources(headExecResDesc); System.out.println("finished creating resource" + System.currentTimeMillis()); execTimeEstimationPlugin = (ExecTimeEstimationPlugin) InstanceFactory.createInstance( forecastFinishTimePluginName, ExecTimeEstimationPlugin.class); //List resources = createResourcesAlternative(resourceDescriptionReader, arc, resourcesDescriptions); LogicalResource lr = createLogicalResources(resource); //LogicalResource lr = createLogicalLayersStructureAlternative(resources); ResourceController rc = null; try { rc = new ResourceController("rc", lr, resource); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return rc; } private ExecResourceDescription createEnvironmentDescription(TempResourceDescriptionReader resourceDescriptionReader, org.qcg.broker.schemas.hostparams.ComputingResource compResources) { resourceLayers = resourceDescriptionReader.resources; String resLayerName = resourceLayers.get(0); ExecResourceDescription headExecResDesc = new ExecResourceDescription(null, compResources, resLayerName); Stack toExamine = new Stack(); toExamine.push(headExecResDesc); ExecResourceDescription perentResDesc; while (!toExamine.isEmpty()) { perentResDesc = toExamine.pop(); resLayerName = getNextLayer(perentResDesc.getType().toString()); if (resLayerName == null) continue; int resAmount = Integer.parseInt(resourceDescriptionReader.res_amount.get(resLayerName)); for (int i = 0; i < resAmount; i++) { ExecResourceDescription resDesc = new ExecResourceDescription(perentResDesc, compResources, resLayerName); perentResDesc.addChildren(resDesc); toExamine.insertElementAt(resDesc, 0); } } return headExecResDesc; } private String getNextLayer(String previousLayer) { String layer = null; for (int i = 0; i < resourceLayers.size(); i++) { if (resourceLayers.get(i).compareTo(previousLayer) == 0) { if (i < resourceLayers.size() - 1) { layer = resourceLayers.get(i + 1); break; } } } return layer; } private ComputingResource createResources(ExecResourceDescription headExecResDes) { ComputingResource headResource = ResourceFactory.createResource(headExecResDes); Stack toExamine = new Stack(); toExamine.push(headExecResDes); Stack resStructure = new Stack(); resStructure.push(headResource); while (!toExamine.isEmpty()) { ComputingResource parentResource = resStructure.pop(); ExecResourceDescription parentResDesc = toExamine.pop(); List childrenResDesc = parentResDesc.getChildren(); if (childrenResDesc == null){ continue; } int resAmount = childrenResDesc.size(); for (int i = 0; i < resAmount; i++) { ExecResourceDescription execResDesc = (ExecResourceDescription) childrenResDesc.get(i); ComputingResource resource = ResourceFactory.createResource(execResDesc); parentResource.addChild(resource); toExamine.push(execResDesc); resStructure.push(resource); } } return headResource; } private LogicalResource createLogicalResources(ComputingResource headResource) { Stack toExamine = new Stack(); toExamine.push(headResource); LogicalResource headLogicalResource = null; Stack logicalResStructure = new Stack(); LogicalResource parentLogicalResource = null; while (!toExamine.isEmpty()) { ComputingResource parentResource = toExamine.pop(); if (parentResource.getType() == ResourceType.COMPUTING_GRID) try { ManagementSystem ms = new GridBrokerNew(parentResource.getName(), globalSchedulingPluginName, execTimeEstimationPlugin, null); headLogicalResource = new LogicalResource(ms, parentResource.getChildren()); logicalResStructure.push(headLogicalResource); } catch (Exception e) { e.printStackTrace(); } if(parentResource.getType() == ResourceType.RACK || parentResource.getType() == ResourceType.COMPUTING_NODE || parentResource.getType() == ResourceType.CPU || parentResource.getType() == ResourceType.CORE ) { } else parentLogicalResource = logicalResStructure.pop(); List resChildren = parentResource.getChildren(); if (resChildren == null) continue; int resAmount = resChildren.size(); for (int i = 0; i < resAmount; i++) { ComputingResource resource = resChildren.get(i); if (resource.getType() == ResourceType.COMPUTING_RESOURCE) { try { ManagementSystem ms = new LocalManagementSystem(resource.getName(), GssimConstants.MANAGEMENT_SYSTEM, localSchedulingPluginName, execTimeEstimationPlugin, null); LogicalResource logicalResource = new LogicalResource( ms, resource.getChildren()); parentLogicalResource.addChild(logicalResource); logicalResStructure.push(logicalResource); } catch (Exception e) { e.printStackTrace(); } } /*else if (resource.getType() == ResourceType.COMPUTING_NODE) { try { ManagementSystem ms = new LocalManagementSystem(resource.getName(), GssimConstants.MANAGEMENT_SYSTEM, "example.localplugin.FCFSLocalPlugin", execTimeEstimationPlugin, null); LogicalResource logicalResource = new LogicalResource(ms, resource.getChildren()); parentLogicalResource.addChild(logicalResource); logicalResStructure.push(logicalResource); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }*/ toExamine.push(resource); } } return headLogicalResource; } 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); } /*private List createResourcesAlternative(TempResourceDescriptionReader resourceDescriptionReader, ExecResourceDescription arcParent, ArrayList resourcesDescriptions) { List resources = new ArrayList(); for(AbstractResourceDescription arc:arcParent.getChildren()) { ExecResourceDescription erc = (ExecResourceDescription)arc; Resource resource = ResourceFactory.createResource(erc); createResourcesIterative(resourceDescriptionReader, resource, erc); resources.add(resource); } return resources; } private LogicalResource createLogicalLayersStructureAlternative(List resources) { LogicalResource logicalLayerWithMs = null; Stack tree = new Stack(); ManagementSystem ms = null; try { ms = new GridBrokerNew("COMPUTING_GRID_0", globalSchedulingPluginName, execTimeEstimationPlugin, null); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { logicalLayerWithMs = new LogicalResource(ms, resources); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } tree.push(logicalLayerWithMs); Stack toExamine = new Stack(); for(Resource resource:resources) toExamine.push(resource); LogicalResource ll = null; while (!toExamine.isEmpty()) { Resource resourceFromTop = toExamine.pop(); if (resourceFromTop.getType() == ResourceType.COMPUTING_RESOURCE) try { ms = new LocalManagementSystem(resourceFromTop.getName(), GssimConstants.MANAGEMENT_SYSTEM, localSchedulingPluginName, execTimeEstimationPlugin, null); LogicalResource logicalLayerWithMsChild = new LogicalResource(ms, resourceFromTop.getChildren()); tree.push(logicalLayerWithMs); logicalLayerWithMs.addChild(logicalLayerWithMsChild); // llstruct.add(new String(""), logicalLayerWithMs.get_name()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } List children = resourceFromTop.getChildren(); if (children == null) continue; int size = children.size(); if(resourceFromTop.getType() == ResourceType.RACK || resourceFromTop.getType() == ResourceType.COMPUTING_NODE || resourceFromTop.getType() == ResourceType.CPU || resourceFromTop.getType() == ResourceType.CORE ) { } else ll = tree.pop(); for (int j = 0; j < size; j++) { Resource resourceChild = children.get(j); if (resourceChild.getType() == ResourceType.COMPUTING_RESOURCE) { try { ms = new LocalManagementSystem(resourceChild.getName(), GssimConstants.MANAGEMENT_SYSTEM, localSchedulingPluginName, execTimeEstimationPlugin, null); LogicalResource logicalLayerWithMsChild = new LogicalResource( ms, resourceChild.getChildren()); ll.addChild(logicalLayerWithMsChild); tree.push(logicalLayerWithMsChild); /// llstruct.add(logicalLayerWithMs.get_name(), logicalLayerWithMsChild.get_name()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if (resourceChild.getType() == ResourceType.COMPUTING_NODE) { try { ms = new LocalManagementSystem(resourceChild.getName(), GssimConstants.DEFAULT_RESOURCE_MANAGER_NAME, "example.localplugin.FCFSLocalPlugin", execTimeEstimationPlugin, null); LogicalResource logicalLayerWithMsChild = new LogicalResource( ms, resourceChild.getChildren()); ll.addChild(logicalLayerWithMsChild); tree.push(logicalLayerWithMsChild); /// llstruct.add(logicalLayerWithMs.get_name(), logicalLayerWithMsChild.get_name()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } toExamine.push(resourceChild); } } return logicalLayerWithMs; } */ /*private ExecResourceDescription createEnvironmentDescription( TempResourceDescriptionReader resourceDescriptionReader, ComputingResource compResources) { physicalLayers = resourceDescriptionReader.resources; String headResName = physicalLayers.get(0); ExecResourceDescription arc = new ExecResourceDescription(null, compResources, ResourceType.valueOf(headResName), String.valueOf(0)); createEnvironmentDescriptionHierarchy(physicalLayers.get(1), arc, resourceDescriptionReader.layer_amount, compResources); return arc; } private void createEnvironmentDescriptionHierarchy(String layer, ExecResourceDescription arc, Map layer_amount, ComputingResource compResources) { Stack toExamine = new Stack(); toExamine.push(arc); while (!toExamine.isEmpty()) { arc = toExamine.pop(); layer = getNextLayer(arc.getType().toString()); if (layer == null) continue; int amount = Integer.parseInt(layer_amount.get(layer)); ExecResourceDescription child; for (int j = 0; j < amount; j++) { child = new ExecResourceDescription(arc, compResources, ResourceType.valueOf(layer), String.valueOf(ResourceIDGenerator.getID(ResourceType.valueOf(layer)))); arc.addChildren(child); toExamine.insertElementAt(child, 0); } } } private Resource createResources(TempResourceDescriptionReader resourceDescriptionReader, ExecResourceDescription arcParent) { Resource resource = ResourceFactory.createResource(arcParent); createResourcesIterative(resourceDescriptionReader, resource, arcParent); return resource; } private void createResourcesIterative(TempResourceDescriptionReader resourceDescriptionReader, Resource resourceParent, ExecResourceDescription arcParent) { Stack toExamine = new Stack(); Stack tree = new Stack(); toExamine.push(arcParent); tree.push(resourceParent); Resource parentResource; while (!toExamine.isEmpty()) { AbstractResourceDescription node = toExamine.pop(); parentResource = tree.pop(); List children = node.getChildren(); if (children == null){ //parentResource.handleEvent(new EnergyEvent(EnergyEventType.POWER_STATE_CHANGED, null)); continue; } int size = children.size(); for (int j = 0; j < size; j++) { ExecResourceDescription arc = (ExecResourceDescription) children.get(j); Resource resource = ResourceFactory.createResource(arc); //resource.setParent(parentResource); parentResource.addChild(resource); toExamine.push(arc); tree.push(resource); } } }*/ }