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