[104] | 1 | package test.rewolucja.resources.manager.implementation; |
---|
| 2 | |
---|
| 3 | import gridsim.GridSim; |
---|
| 4 | |
---|
| 5 | import java.util.ArrayList; |
---|
| 6 | import java.util.HashSet; |
---|
| 7 | import java.util.Iterator; |
---|
| 8 | import java.util.List; |
---|
| 9 | import java.util.Map; |
---|
| 10 | import java.util.Properties; |
---|
| 11 | import java.util.Set; |
---|
| 12 | import java.util.Stack; |
---|
| 13 | |
---|
| 14 | import schedframe.resources.units.Memory; |
---|
| 15 | import schedframe.resources.units.ResourceUnit; |
---|
| 16 | import schedframe.scheduling.plugin.local.ResourceAllocationInterface; |
---|
| 17 | import schedframe.scheduling.utils.ResourceParameterName; |
---|
| 18 | import test.rewolucja.resources.ProcessingElements; |
---|
| 19 | import test.rewolucja.resources.ResourceCharacteristics; |
---|
| 20 | import test.rewolucja.resources.ResourceStatus; |
---|
| 21 | import test.rewolucja.resources.ResourceType; |
---|
| 22 | import test.rewolucja.resources.UnitState; |
---|
| 23 | import test.rewolucja.resources.exception.ResourceException; |
---|
[223] | 24 | import test.rewolucja.resources.logical.base.LogicalResource; |
---|
[104] | 25 | import test.rewolucja.resources.manager.interfaces.ResourceManagerInterface; |
---|
| 26 | import test.rewolucja.resources.physical.base.ComputingResource; |
---|
| 27 | import test.rewolucja.resources.utils.validator.ResourcePropertiesValidator; |
---|
| 28 | import test.rewolucja.resources.utils.validator.ResourceValidator; |
---|
| 29 | |
---|
| 30 | public class ResourceManager implements ResourceAllocationInterface, ResourceManagerInterface { |
---|
| 31 | |
---|
| 32 | //private Log log = LogFactory.getLog(ResourceManager.class); |
---|
| 33 | |
---|
[223] | 34 | protected List<ComputingResource> compResources; |
---|
| 35 | protected List<LogicalResource> logicalResources; |
---|
[104] | 36 | protected ResourceCharacteristics resourceCharacteristic; |
---|
| 37 | |
---|
[223] | 38 | public ResourceManager(List<ComputingResource> resources, List<LogicalResource> logicalRes) { |
---|
| 39 | this.compResources = resources; |
---|
| 40 | this.logicalResources = logicalRes; |
---|
[104] | 41 | this.resourceCharacteristic = new ResourceCharacteristics(); |
---|
| 42 | for(ComputingResource resource : resources){ |
---|
| 43 | Map<ResourceParameterName, List<ResourceUnit>> resUnits = resource.getResourceCharacteristic().getResourceUnits(); |
---|
| 44 | for(ResourceParameterName rpn : resUnits.keySet()){ |
---|
| 45 | resourceCharacteristic.addResourceUnit(resUnits.get(rpn).get(0)); |
---|
| 46 | } |
---|
| 47 | } |
---|
| 48 | /*(Resource parentResource = resources.get(0).getParent(); |
---|
| 49 | if (parentResource.getChildren().size() == resources.size()) { |
---|
| 50 | setResourceCharacteristic(parentResource.getResourceCharacteristic()); |
---|
| 51 | }*/ |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | public ResourceManager(ComputingResource resource) { |
---|
[223] | 55 | this.compResources = resource.getChildren(); |
---|
[104] | 56 | this.resourceCharacteristic = resource.getResourceCharacteristic(); |
---|
| 57 | // this.logicalLayers = resource.getLogaicaLayer().getChildren(); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | public List<ComputingResource> getResources() { |
---|
[223] | 61 | return compResources; |
---|
[104] | 62 | } |
---|
| 63 | |
---|
| 64 | public boolean areResourcesAchievable(ResourceType type) { |
---|
[223] | 65 | if (compResources != null) { |
---|
[104] | 66 | Stack<ComputingResource> toExamine = new Stack<ComputingResource>(); |
---|
[223] | 67 | for (ComputingResource resource : compResources) |
---|
[104] | 68 | toExamine.push(resource); |
---|
| 69 | |
---|
| 70 | while (!toExamine.isEmpty()) { |
---|
| 71 | ComputingResource resource = toExamine.pop(); |
---|
| 72 | List<ComputingResource> resources = resource.getChildren(); |
---|
| 73 | if (resources == null) |
---|
| 74 | continue; |
---|
| 75 | int numberOfResComp = resources.size(); |
---|
| 76 | for (int i = 0; i < numberOfResComp; i++) { |
---|
| 77 | ComputingResource resourceChild = resources.get(i); |
---|
| 78 | if (resourceChild.getType() == type) { |
---|
| 79 | return true; |
---|
| 80 | } else |
---|
| 81 | toExamine.push(resourceChild); |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | return false; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | |
---|
| 89 | public List<? extends ComputingResource> getResourcesOfType(ResourceType type) throws ResourceException { |
---|
| 90 | List<ComputingResource> resourcesOfType = new ArrayList<ComputingResource>(); |
---|
[223] | 91 | for (ComputingResource resource : compResources) { |
---|
[104] | 92 | if (resource.getType() == type) { |
---|
| 93 | resourcesOfType.add(resource); |
---|
| 94 | } else |
---|
| 95 | resourcesOfType.addAll(resource.getDescendantsByType(type)); |
---|
| 96 | } |
---|
| 97 | return resourcesOfType; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | public List<? extends ComputingResource> getResourcesOfTypeWithStatus(ResourceType type, ResourceStatus status) |
---|
| 101 | throws ResourceException { |
---|
| 102 | |
---|
| 103 | List<ComputingResource> resourcesOfType = new ArrayList<ComputingResource>(); |
---|
[223] | 104 | for (ComputingResource resource : compResources) { |
---|
[104] | 105 | if (resource.getType() == type) { |
---|
| 106 | if (resource.getStatus() == status) { |
---|
| 107 | resourcesOfType.add(resource); |
---|
| 108 | } |
---|
| 109 | } else |
---|
| 110 | resourcesOfType.addAll(resource.getDescendantsByTypeAndStatus(type, status)); |
---|
| 111 | } |
---|
| 112 | return resourcesOfType; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | public ComputingResource getResourceByName(String resourceName) throws ResourceException { |
---|
| 116 | |
---|
| 117 | ComputingResource resourceWithName = null; |
---|
[223] | 118 | for (int i = 0; i < compResources.size() && resourceWithName == null; i++) { |
---|
| 119 | ComputingResource resource = compResources.get(i); |
---|
[104] | 120 | if (resource.getName().compareTo(resourceName) == 0) |
---|
| 121 | resourceWithName = resource; |
---|
| 122 | else |
---|
| 123 | resourceWithName = resource.getDescendantsByName(resourceName); |
---|
| 124 | } |
---|
| 125 | return resourceWithName; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | public List<ResourceUnit> getResourceUnits(ResourceParameterName unitName) throws ResourceException { |
---|
| 129 | List<ResourceUnit> resourceUnit = new ArrayList<ResourceUnit>(); |
---|
| 130 | Stack<ComputingResource> toExamine = new Stack<ComputingResource>(); |
---|
[223] | 131 | for (ComputingResource resource : compResources) |
---|
[104] | 132 | toExamine.push(resource); |
---|
| 133 | while (!toExamine.isEmpty()) { |
---|
| 134 | ComputingResource resource = toExamine.pop(); |
---|
| 135 | ResourceCharacteristics resourceCharacteristic = resource.getResourceCharacteristic(); |
---|
| 136 | ResourceUnit unit = resourceCharacteristic.getResourceUnit(unitName); |
---|
| 137 | if (unit != null) |
---|
| 138 | resourceUnit.add(unit); |
---|
| 139 | // else { |
---|
| 140 | List<ComputingResource> resources = resource.getChildren(); |
---|
| 141 | if (resources == null) |
---|
| 142 | continue; |
---|
| 143 | int numberOfResComp = resources.size(); |
---|
| 144 | for (int i = 0; i < numberOfResComp; i++) { |
---|
| 145 | ComputingResource resourceChild = resources.get(i); |
---|
| 146 | toExamine.push(resourceChild); |
---|
| 147 | } |
---|
| 148 | // } |
---|
| 149 | } |
---|
| 150 | return resourceUnit; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | public List<ResourceUnit> getAvailableResourceUnits(String resourceName) throws Exception { |
---|
| 154 | ComputingResource resource = getResourceByName(resourceName); |
---|
| 155 | List<ResourceUnit> resourceUnits = new ArrayList<ResourceUnit>(); |
---|
| 156 | while(resource != null){ |
---|
| 157 | for(List<ResourceUnit> resUnits: resource.getResourceCharacteristic().getResourceUnits().values()) |
---|
| 158 | resUnits.addAll(resourceUnits); |
---|
| 159 | resource = resource.getParent(); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | return resourceUnits; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | public ResourceCharacteristics getResourceCharacteristic() { |
---|
| 166 | return resourceCharacteristic; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | public List<? extends ComputingResource> filterResources(Properties properties) { |
---|
| 170 | List<ComputingResource> descendants = new ArrayList<ComputingResource>(); |
---|
[223] | 171 | for (ComputingResource resource : compResources) { |
---|
[104] | 172 | ResourceValidator resourceValidator = new ResourcePropertiesValidator(properties); |
---|
| 173 | if (resourceValidator.validate(resource)) |
---|
| 174 | descendants.add(resource); |
---|
| 175 | else |
---|
| 176 | descendants.addAll(resource.filterDescendants(properties)); |
---|
| 177 | } |
---|
| 178 | return descendants; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | public List<LogicalResource> getResourceProviders() { |
---|
[223] | 182 | return logicalResources; |
---|
[104] | 183 | } |
---|
| 184 | |
---|
| 185 | public String getResourceProvider(String resName){ |
---|
| 186 | if(GridSim.getEntityId(resName) != -1){ |
---|
| 187 | return resName; |
---|
| 188 | } |
---|
| 189 | ComputingResource resourceWithName = null; |
---|
[223] | 190 | for(int i = 0 ; i < compResources.size() && resourceWithName == null; i++){ |
---|
| 191 | ComputingResource resource = compResources.get(i); |
---|
[104] | 192 | if(resource.getName().equals(resName)) |
---|
| 193 | resourceWithName = resource; |
---|
| 194 | else |
---|
| 195 | try { |
---|
| 196 | resourceWithName = resource.getDescendantsByName(resName); |
---|
| 197 | } catch (ResourceException e) { |
---|
| 198 | return null; |
---|
| 199 | } |
---|
| 200 | } |
---|
| 201 | if(resourceWithName == null) |
---|
| 202 | return null; |
---|
| 203 | List<ComputingResource> children = resourceWithName.getChildren(); |
---|
| 204 | Set<LogicalResource> childrenControllers = new HashSet<LogicalResource>(); |
---|
| 205 | for(ComputingResource child:children) { |
---|
| 206 | childrenControllers.add(child.getLogicalResource()); |
---|
| 207 | } |
---|
| 208 | Set<LogicalResource> tempChildrenControllers = new HashSet<LogicalResource>(childrenControllers); |
---|
| 209 | while(childrenControllers.size() != 1){ |
---|
| 210 | childrenControllers = new HashSet<LogicalResource>(); |
---|
| 211 | for(LogicalResource lr:tempChildrenControllers){ |
---|
| 212 | childrenControllers.add(lr.getParent()); |
---|
| 213 | } |
---|
| 214 | tempChildrenControllers = new HashSet<LogicalResource>(childrenControllers); |
---|
| 215 | } |
---|
| 216 | Iterator<LogicalResource> it = childrenControllers.iterator(); |
---|
| 217 | LogicalResource potentialLogicalResource = it.next(); |
---|
| 218 | if(potentialLogicalResource.getResources().containsAll(children)) |
---|
| 219 | return potentialLogicalResource.get_name(); |
---|
| 220 | return null; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | public boolean allocateResources(Map<ResourceParameterName, ResourceUnit> resources) { |
---|
| 224 | if (resources == null) { |
---|
| 225 | return false; |
---|
| 226 | } |
---|
| 227 | ResourceUnit processingElements = resources.get(ResourceParameterName.PROCESSINGELEMENTS); |
---|
| 228 | |
---|
| 229 | if (processingElements != null) { |
---|
| 230 | |
---|
| 231 | if (processingElements instanceof ProcessingElements) { |
---|
| 232 | ProcessingElements choosenProcessors = (ProcessingElements) processingElements; |
---|
| 233 | |
---|
| 234 | for (int i = 0; i < choosenProcessors.size(); i++) { |
---|
| 235 | choosenProcessors.get(i).setStatus(ResourceStatus.BUSY); |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | } |
---|
| 239 | Memory m = (Memory) resources.get(ResourceParameterName.MEMORY); |
---|
| 240 | if (m != null) { |
---|
| 241 | m.setState(UnitState.BUSY); |
---|
| 242 | } |
---|
| 243 | return true; |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | |
---|
| 247 | public void freeResources(Map<ResourceParameterName, ResourceUnit> lastUsedResources) { |
---|
| 248 | ResourceUnit resUnit = lastUsedResources.get(ResourceParameterName.PROCESSINGELEMENTS); |
---|
| 249 | |
---|
| 250 | if (resUnit instanceof ProcessingElements) { |
---|
| 251 | ProcessingElements processingElements = (ProcessingElements) resUnit; |
---|
| 252 | |
---|
| 253 | for (int i = 0; i < processingElements.size(); i++) { |
---|
| 254 | processingElements.get(i).setStatus(ResourceStatus.FREE); |
---|
| 255 | } |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | Memory m = (Memory) lastUsedResources.get(ResourceParameterName.MEMORY); |
---|
| 259 | if (m != null) { |
---|
| 260 | m.setState(UnitState.FREE); |
---|
| 261 | } |
---|
| 262 | } |
---|
| 263 | } |
---|