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