1 | package schedframe.scheduling.manager.resources; |
---|
2 | |
---|
3 | import gridsim.GridSim; |
---|
4 | |
---|
5 | import java.util.ArrayDeque; |
---|
6 | import java.util.ArrayList; |
---|
7 | import java.util.Deque; |
---|
8 | import java.util.HashMap; |
---|
9 | import java.util.HashSet; |
---|
10 | import java.util.Iterator; |
---|
11 | import java.util.List; |
---|
12 | import java.util.Map; |
---|
13 | import java.util.Properties; |
---|
14 | import java.util.Set; |
---|
15 | |
---|
16 | import schedframe.exceptions.ResourceException; |
---|
17 | import schedframe.resources.ResourceStatus; |
---|
18 | import schedframe.resources.ResourceType; |
---|
19 | import schedframe.resources.StandardResourceType; |
---|
20 | import schedframe.resources.computing.ComputingResource; |
---|
21 | import schedframe.resources.computing.ResourceCharacteristics; |
---|
22 | import schedframe.resources.computing.validator.ResourcePropertiesValidator; |
---|
23 | import schedframe.resources.computing.validator.ResourceValidator; |
---|
24 | import schedframe.resources.units.PEUnit; |
---|
25 | import schedframe.resources.units.ProcessingElements; |
---|
26 | import schedframe.resources.units.ResourceUnit; |
---|
27 | import schedframe.resources.units.ResourceUnitName; |
---|
28 | import schedframe.resources.units.ResourceUnitState; |
---|
29 | import schedframe.resources.units.StandardResourceUnitName; |
---|
30 | import schedframe.scheduling.Scheduler; |
---|
31 | import schedframe.scheduling.plugin.local.ResourceAllocation; |
---|
32 | |
---|
33 | |
---|
34 | public class LocalResourceManager implements ResourceAllocation, ResourceManager { |
---|
35 | |
---|
36 | //private Log log = LogFactory.getLog(ResourceManager.class); |
---|
37 | |
---|
38 | protected List<ComputingResource> computingResources; |
---|
39 | protected List<Scheduler> schedulers; |
---|
40 | protected Map<ResourceUnitName, List<ResourceUnit>> resourceUnits; |
---|
41 | |
---|
42 | public LocalResourceManager(List<ComputingResource> resources, List<Scheduler> schedulers, Map<ResourceUnitName, List<ResourceUnit>> resourceUnits) { |
---|
43 | this.computingResources = resources; |
---|
44 | this.schedulers = schedulers; |
---|
45 | this.resourceUnits = resourceUnits; |
---|
46 | |
---|
47 | //initSharedResourceUnits(computingResources); |
---|
48 | } |
---|
49 | |
---|
50 | public LocalResourceManager(ComputingResource compResource) { |
---|
51 | this.computingResources = compResource.getChildren(); |
---|
52 | |
---|
53 | initSharedResourceUnits(compResource); |
---|
54 | } |
---|
55 | |
---|
56 | public void initSharedResourceUnits(ComputingResource compResource){ |
---|
57 | this.resourceUnits = new HashMap<ResourceUnitName, List<ResourceUnit>>(); |
---|
58 | List<ResourceUnit> list; |
---|
59 | |
---|
60 | ComputingResource parent = compResource.getParent(); |
---|
61 | while(parent != null){ |
---|
62 | Map<ResourceUnitName, List<ResourceUnit>> resUnits = parent.getResourceCharacteristic().getResourceUnits(); |
---|
63 | for(ResourceUnitName run : resUnits.keySet()){ |
---|
64 | for(ResourceUnit resUnit : resUnits.get(run)){ |
---|
65 | if((resourceUnits.get(run) == null)){ |
---|
66 | list = new ArrayList<ResourceUnit>(1); |
---|
67 | resourceUnits.put(resUnit.getName(), list); |
---|
68 | list.add(resUnit); |
---|
69 | } else if(!resourceUnits.get(run).contains(resUnit)){ |
---|
70 | list = resourceUnits.get(resUnit.getName()); |
---|
71 | list.add(resUnit); |
---|
72 | } |
---|
73 | } |
---|
74 | } |
---|
75 | parent = parent.getParent(); |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | public List<ComputingResource> getResources() { |
---|
80 | return computingResources; |
---|
81 | } |
---|
82 | |
---|
83 | public boolean areResourcesAchievable(ResourceType type) { |
---|
84 | if (computingResources != null) { |
---|
85 | Deque<ComputingResource> toExamine = new ArrayDeque<ComputingResource>(); |
---|
86 | for (ComputingResource resource : computingResources){ |
---|
87 | toExamine.push(resource); |
---|
88 | } |
---|
89 | |
---|
90 | while (!toExamine.isEmpty()) { |
---|
91 | ComputingResource resource = toExamine.pop(); |
---|
92 | if(resource.getType() == type) |
---|
93 | return true; |
---|
94 | List<ComputingResource> resources = resource.getChildren(); |
---|
95 | /*if (resources == null) |
---|
96 | continue;*/ |
---|
97 | int numberOfResComp = resources.size(); |
---|
98 | for (int i = 0; i < numberOfResComp; i++) { |
---|
99 | ComputingResource resourceChild = resources.get(i); |
---|
100 | if (resourceChild.getType() == type) { |
---|
101 | return true; |
---|
102 | } else |
---|
103 | toExamine.push(resourceChild); |
---|
104 | } |
---|
105 | } |
---|
106 | } |
---|
107 | return false; |
---|
108 | } |
---|
109 | |
---|
110 | public List<? extends ComputingResource> getResourcesOfType(ResourceType type) throws ResourceException { |
---|
111 | List<ComputingResource> resourcesOfType = new ArrayList<ComputingResource>(); |
---|
112 | for (ComputingResource resource : computingResources) { |
---|
113 | if (resource.getType() == type) { |
---|
114 | resourcesOfType.add(resource); |
---|
115 | } else |
---|
116 | resourcesOfType.addAll(resource.getDescendantsByType(type)); |
---|
117 | } |
---|
118 | return resourcesOfType; |
---|
119 | } |
---|
120 | |
---|
121 | public List<? extends ComputingResource> getResourcesByTypeWithStatus(ResourceType type, ResourceStatus status) |
---|
122 | throws ResourceException { |
---|
123 | |
---|
124 | List<ComputingResource> resourcesOfType = new ArrayList<ComputingResource>(); |
---|
125 | for (ComputingResource resource : computingResources) { |
---|
126 | if (resource.getType() == type) { |
---|
127 | if (resource.getStatus() == status) { |
---|
128 | resourcesOfType.add(resource); |
---|
129 | } |
---|
130 | } else |
---|
131 | resourcesOfType.addAll(resource.getDescendantsByTypeAndStatus(type, status)); |
---|
132 | } |
---|
133 | return resourcesOfType; |
---|
134 | } |
---|
135 | |
---|
136 | public ComputingResource getResourceByName(String resourceName) throws ResourceException { |
---|
137 | ComputingResource resourceWithName = null; |
---|
138 | for (int i = 0; i < computingResources.size() && resourceWithName == null; i++) { |
---|
139 | ComputingResource resource = computingResources.get(i); |
---|
140 | if (resource.getName().equals(resourceName)) |
---|
141 | resourceWithName = resource; |
---|
142 | else |
---|
143 | resourceWithName = resource.getDescendantByName(resourceName); |
---|
144 | } |
---|
145 | return resourceWithName; |
---|
146 | } |
---|
147 | |
---|
148 | public List<ResourceUnit> getDistributedResourceUnits(ResourceUnitName unitName) { |
---|
149 | List<ResourceUnit> resourceUnit = new ArrayList<ResourceUnit>(); |
---|
150 | if (computingResources != null) { |
---|
151 | Deque<ComputingResource> toExamine = new ArrayDeque<ComputingResource>(); |
---|
152 | for (ComputingResource resource : computingResources) |
---|
153 | toExamine.push(resource); |
---|
154 | while (!toExamine.isEmpty()) { |
---|
155 | ComputingResource resource = toExamine.pop(); |
---|
156 | ResourceCharacteristics resourceCharacteristic = resource.getResourceCharacteristic(); |
---|
157 | List<ResourceUnit> units = null; |
---|
158 | units = resourceCharacteristic.getResourceUnits().get(unitName); |
---|
159 | if (units != null) |
---|
160 | resourceUnit.addAll(units); |
---|
161 | // else { |
---|
162 | List<ComputingResource> resources = resource.getChildren(); |
---|
163 | /*if (resources == null) |
---|
164 | continue;*/ |
---|
165 | int numberOfResComp = resources.size(); |
---|
166 | for (int i = 0; i < numberOfResComp; i++) { |
---|
167 | ComputingResource resourceChild = resources.get(i); |
---|
168 | toExamine.push(resourceChild); |
---|
169 | } |
---|
170 | // } |
---|
171 | } |
---|
172 | } |
---|
173 | return resourceUnit; |
---|
174 | } |
---|
175 | |
---|
176 | public List<? extends ComputingResource> filterResources(Properties properties) { |
---|
177 | List<ComputingResource> descendants = new ArrayList<ComputingResource>(); |
---|
178 | for (ComputingResource resource : computingResources) { |
---|
179 | ResourceValidator resourceValidator = new ResourcePropertiesValidator(properties); |
---|
180 | if (resourceValidator.validate(resource)) |
---|
181 | descendants.add(resource); |
---|
182 | else |
---|
183 | descendants.addAll(resource.filterDescendants(properties)); |
---|
184 | } |
---|
185 | return descendants; |
---|
186 | } |
---|
187 | |
---|
188 | public Map<ResourceUnitName, List<ResourceUnit>> getSharedResourceUnits() { |
---|
189 | |
---|
190 | /*Map<ResourceUnitName, List<ResourceUnit>> sharedResourceUnits = new HashMap<ResourceUnitName, List<ResourceUnit>>(); |
---|
191 | List<ResourceUnit> list; |
---|
192 | for(ComputingResource resource : compResources){ |
---|
193 | boolean resourceNotVisited = true; |
---|
194 | ComputingResource parent = resource.getParent(); |
---|
195 | while(parent != null && resourceNotVisited){ |
---|
196 | Map<ResourceUnitName, List<ResourceUnit>> resUnits = parent.getResourceCharacteristic().getResourceUnits(); |
---|
197 | for(ResourceUnitName run : resUnits.keySet()){ |
---|
198 | for(ResourceUnit resUnit : resUnits.get(run)){ |
---|
199 | if((sharedResourceUnits.get(run) == null)){ |
---|
200 | list = new ArrayList<ResourceUnit>(1); |
---|
201 | sharedResourceUnits.put(resUnit.getName(), list); |
---|
202 | list.add(resUnit); |
---|
203 | } else if(!sharedResourceUnits.get(run).contains(resUnit)){ |
---|
204 | list = sharedResourceUnits.get(resUnit.getName()); |
---|
205 | list.add(resUnit); |
---|
206 | } else { |
---|
207 | resourceNotVisited = false; |
---|
208 | } |
---|
209 | } |
---|
210 | } |
---|
211 | parent = parent.getParent(); |
---|
212 | } |
---|
213 | } |
---|
214 | return sharedResourceUnits;*/ |
---|
215 | return resourceUnits; |
---|
216 | } |
---|
217 | |
---|
218 | public List<Scheduler> getSchedulers() { |
---|
219 | return schedulers; |
---|
220 | } |
---|
221 | |
---|
222 | @SuppressWarnings("unchecked") |
---|
223 | public List<ResourceUnit> getPE() throws ResourceException{ |
---|
224 | |
---|
225 | List<ResourceUnit> peUnits = null; |
---|
226 | List<ComputingResource> computingResources = null; |
---|
227 | if(areResourcesAchievable(StandardResourceType.Core)){ |
---|
228 | try { |
---|
229 | computingResources = (List<ComputingResource>) getResourcesOfType(StandardResourceType.Core); |
---|
230 | } catch (ResourceException e) { |
---|
231 | throw new RuntimeException("DCWorms internal error"); |
---|
232 | } |
---|
233 | PEUnit peUnit = new ProcessingElements(computingResources); |
---|
234 | peUnits = new ArrayList<ResourceUnit>(); |
---|
235 | peUnits.add(peUnit); |
---|
236 | } |
---|
237 | |
---|
238 | else if(areResourcesAchievable(StandardResourceType.Processor)){ |
---|
239 | try { |
---|
240 | computingResources = (List<ComputingResource>) getResourcesOfType(StandardResourceType.Processor); |
---|
241 | } catch (ResourceException e) { |
---|
242 | throw new RuntimeException("DCWorms internal error"); |
---|
243 | } |
---|
244 | PEUnit peUnit = new ProcessingElements(computingResources); |
---|
245 | peUnits = new ArrayList<ResourceUnit>(); |
---|
246 | peUnits.add(peUnit); |
---|
247 | } |
---|
248 | |
---|
249 | else if (getDistributedResourceUnits(StandardResourceUnitName.PE).size() > 0){ |
---|
250 | peUnits = getDistributedResourceUnits(StandardResourceUnitName.PE); |
---|
251 | } |
---|
252 | |
---|
253 | else if(getSharedResourceUnits().get(StandardResourceUnitName.PE) != null){ |
---|
254 | peUnits = getSharedResourceUnits().get(StandardResourceUnitName.PE); |
---|
255 | } |
---|
256 | if(peUnits == null) |
---|
257 | throw new ResourceException("Processing Elements are not defined"); |
---|
258 | return peUnits; |
---|
259 | } |
---|
260 | |
---|
261 | public String getSchedulerName(String resName){ |
---|
262 | if(GridSim.getEntityId(resName) != -1){ |
---|
263 | return resName; |
---|
264 | } |
---|
265 | ComputingResource resourceWithName = null; |
---|
266 | for(int i = 0 ; i < computingResources.size() && resourceWithName == null; i++){ |
---|
267 | ComputingResource resource = computingResources.get(i); |
---|
268 | if(resource.getName().equals(resName)) |
---|
269 | resourceWithName = resource; |
---|
270 | else |
---|
271 | try { |
---|
272 | resourceWithName = resource.getDescendantByName(resName); |
---|
273 | } catch (ResourceException e) { |
---|
274 | return null; |
---|
275 | } |
---|
276 | } |
---|
277 | if(resourceWithName == null) |
---|
278 | return null; |
---|
279 | List<ComputingResource> children = resourceWithName.getChildren(); |
---|
280 | Set<Scheduler> childrenSchedulers = new HashSet<Scheduler>(); |
---|
281 | if(children.isEmpty()) |
---|
282 | return null; |
---|
283 | for(ComputingResource child: children) { |
---|
284 | childrenSchedulers.add(child.getScheduler()); |
---|
285 | } |
---|
286 | |
---|
287 | Set<Scheduler> tempChildrenSchedulers = new HashSet<Scheduler>(childrenSchedulers); |
---|
288 | while(childrenSchedulers.size() > 1){ |
---|
289 | childrenSchedulers = new HashSet<Scheduler>(); |
---|
290 | for(Scheduler s: tempChildrenSchedulers){ |
---|
291 | childrenSchedulers.add(s.getParent()); |
---|
292 | } |
---|
293 | tempChildrenSchedulers = new HashSet<Scheduler>(childrenSchedulers); |
---|
294 | } |
---|
295 | Iterator<Scheduler> it = childrenSchedulers.iterator(); |
---|
296 | Scheduler potentialScheduler = it.next(); |
---|
297 | if(potentialScheduler.getCompResources().containsAll(children)) |
---|
298 | return potentialScheduler.get_name(); |
---|
299 | return null; |
---|
300 | } |
---|
301 | |
---|
302 | public boolean allocateResources(Map<ResourceUnitName, ResourceUnit> resources) { |
---|
303 | |
---|
304 | if (resources == null) { |
---|
305 | return false; |
---|
306 | } |
---|
307 | |
---|
308 | for(ResourceUnitName resUnitName: resources.keySet()){ |
---|
309 | ResourceUnit resUnit = resources.get(resUnitName); |
---|
310 | resUnit.getProvisioner().setState(ResourceUnitState.BUSY); |
---|
311 | } |
---|
312 | return true; |
---|
313 | } |
---|
314 | |
---|
315 | |
---|
316 | public void freeResources(Map<ResourceUnitName, ResourceUnit> resources) { |
---|
317 | |
---|
318 | for(ResourceUnitName resUnitName: resources.keySet()){ |
---|
319 | ResourceUnit resUnit = resources.get(resUnitName); |
---|
320 | resUnit.getProvisioner().setState(ResourceUnitState.FREE); |
---|
321 | } |
---|
322 | } |
---|
323 | |
---|
324 | } |
---|