1 | package schedframe.resources.computing; |
---|
2 | |
---|
3 | import gridsim.GridSimTags; |
---|
4 | import gridsim.dcworms.DCWormsTags; |
---|
5 | |
---|
6 | import java.util.ArrayList; |
---|
7 | import java.util.LinkedList; |
---|
8 | import java.util.List; |
---|
9 | import java.util.Properties; |
---|
10 | |
---|
11 | import schedframe.events.Event; |
---|
12 | import schedframe.events.EventHandler; |
---|
13 | import schedframe.events.ResourceEventCommand; |
---|
14 | import schedframe.events.scheduling.EventReason; |
---|
15 | import schedframe.events.scheduling.ResourceStateChangedEvent; |
---|
16 | import schedframe.events.scheduling.SchedulingEvent; |
---|
17 | import schedframe.events.scheduling.SchedulingEventCommand; |
---|
18 | import schedframe.exceptions.ResourceException; |
---|
19 | import schedframe.resources.ResourceStatus; |
---|
20 | import schedframe.resources.ResourceType; |
---|
21 | import schedframe.resources.computing.description.ComputingResourceDescription; |
---|
22 | import schedframe.resources.computing.extensions.Extension; |
---|
23 | import schedframe.resources.computing.extensions.ExtensionListImpl; |
---|
24 | import schedframe.resources.computing.profiles.energy.EnergyEvent; |
---|
25 | import schedframe.resources.computing.profiles.energy.EnergyEventType; |
---|
26 | import schedframe.resources.computing.profiles.energy.EnergyExtension; |
---|
27 | import schedframe.resources.computing.profiles.load.LoadExtension; |
---|
28 | import schedframe.resources.computing.profiles.load.ResourceLoadCalendar; |
---|
29 | import schedframe.resources.computing.profiles.load.TimestampUtilization; |
---|
30 | import schedframe.resources.computing.profiles.load.ui.LoadInterface; |
---|
31 | import schedframe.resources.computing.properties.DefaultPropertiesBuilder; |
---|
32 | import schedframe.resources.computing.properties.PropertiesDirector; |
---|
33 | import schedframe.resources.computing.validator.ResourceNameValidator; |
---|
34 | import schedframe.resources.computing.validator.ResourcePropertiesValidator; |
---|
35 | import schedframe.resources.computing.validator.ResourceStatusValidator; |
---|
36 | import schedframe.resources.computing.validator.ResourceTypeValidator; |
---|
37 | import schedframe.resources.computing.validator.ResourceValidator; |
---|
38 | import schedframe.resources.devices.Device; |
---|
39 | import schedframe.resources.devices.PhysicalResource; |
---|
40 | import schedframe.scheduling.Scheduler; |
---|
41 | import simulator.DataCenterWorkloadSimulator; |
---|
42 | |
---|
43 | public class ComputingResource extends PhysicalResource{ |
---|
44 | |
---|
45 | protected ComputingResource parent; |
---|
46 | protected List<ComputingResource> children; |
---|
47 | |
---|
48 | protected Scheduler scheduler; |
---|
49 | //protected ResourceCharacteristics resourceCharacteristic; |
---|
50 | |
---|
51 | |
---|
52 | public ComputingResource(ComputingResourceDescription resDesc) { |
---|
53 | this.type = resDesc.getType(); |
---|
54 | this.name = resDesc.getId(); |
---|
55 | this.category = resDesc.getCategory(); |
---|
56 | this.status = ResourceStatus.FREE; |
---|
57 | this.extensionList = new ExtensionListImpl(1); |
---|
58 | initCharacteristics(resDesc); |
---|
59 | accept(new LoadExtension(resDesc.getLoadProfile(), this)); |
---|
60 | accept(new EnergyExtension.Builder().resource(this).powerProfile(resDesc.getPowerProfile()).airFlowProfile(resDesc.getAirThroughputProfile()).thermalProfile(resDesc.getThermalProfile()).build()); |
---|
61 | } |
---|
62 | |
---|
63 | protected void initCharacteristics(ComputingResourceDescription resDesc){ |
---|
64 | resourceCharacteristic = ComputingResourceCharacteristics.builder().resourceUnits(resDesc.getResourceUnits()).location(resDesc.getLocation()).parameters(resDesc.getParameters()).device(resDesc.getDevices()).build(); |
---|
65 | for(Device device: ((ComputingResourceCharacteristics)resourceCharacteristic).getDevices()){ |
---|
66 | device.setComputingResource(this); |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | public ComputingResource getParent() { |
---|
71 | return parent; |
---|
72 | } |
---|
73 | |
---|
74 | public void setParent(ComputingResource newParent) { |
---|
75 | parent = newParent; |
---|
76 | /*if(this.getLoadInterface().getLoadCalendar().getLoadDistribution().size() == 0){ |
---|
77 | this.getLoadInterface().getLoadCalendar().getLoadDistribution().addAll(parent.getLoadInterface().getLoadCalendar().getLoadDistribution()); |
---|
78 | }*/ |
---|
79 | } |
---|
80 | |
---|
81 | public List<ComputingResource> getChildren() { |
---|
82 | if (children == null) |
---|
83 | return new ArrayList<ComputingResource>(1); |
---|
84 | return children; |
---|
85 | } |
---|
86 | |
---|
87 | public void addChild(ComputingResource child) { |
---|
88 | child.setParent(this); |
---|
89 | if (children == null) |
---|
90 | children = new ArrayList<ComputingResource>(); |
---|
91 | children.add(child); |
---|
92 | } |
---|
93 | |
---|
94 | public String getFullName() { |
---|
95 | if(this.getResourceCharacteristic().getParameters().get("fullPath") != null){ |
---|
96 | String fullPath = this.getResourceCharacteristic().getParameters().get("fullPath").get(0).getContent(); |
---|
97 | return fullPath; |
---|
98 | } |
---|
99 | if(parent!= null){ |
---|
100 | return parent.getFullName() + "/" + name; |
---|
101 | } else |
---|
102 | return name; |
---|
103 | } |
---|
104 | |
---|
105 | public ComputingResourceCharacteristics getResourceCharacteristic() { |
---|
106 | return (ComputingResourceCharacteristics)resourceCharacteristic; |
---|
107 | } |
---|
108 | |
---|
109 | public void setStatus(ResourceStatus newStatus) { |
---|
110 | if(newStatus != status){ |
---|
111 | status = newStatus; |
---|
112 | if(children != null) { |
---|
113 | for(ComputingResource child: children){ |
---|
114 | child.setStatus(status); |
---|
115 | } |
---|
116 | } |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | private void triggerEventUp(Event event) { |
---|
121 | if(parent != null) |
---|
122 | parent.handleEvent(event); |
---|
123 | } |
---|
124 | |
---|
125 | public void handleEvent(Event event){ |
---|
126 | ResourceEventCommand rec = new ResourceEventCommand(this); |
---|
127 | rec.execute(event); |
---|
128 | SchedulingEventCommand sec = new SchedulingEventCommand(this); |
---|
129 | sec.execute(event); |
---|
130 | |
---|
131 | //old, correctly working method |
---|
132 | /*if (extensionList != null) { |
---|
133 | for (Extension extension : extensionList) { |
---|
134 | if (extension.supportsEvent(event)) { |
---|
135 | extension.handleEvent(event); |
---|
136 | } |
---|
137 | } |
---|
138 | }*/ |
---|
139 | //TODO - delete, check in advance |
---|
140 | //if(scheduler != null && (parent != null && scheduler != parent.getScheduler())/*scheduler.getResources().contains(this)*/){ |
---|
141 | // String src = event.getSource() != null ? event.getSource() : name; |
---|
142 | // scheduler.sendInternal(GridSimTags.SCHEDULE_NOW, DCWormsTags.UPDATE, src); |
---|
143 | //} |
---|
144 | //triggerEventUp(event); |
---|
145 | } |
---|
146 | |
---|
147 | public List <? extends ComputingResource> getDescendantsByType(ResourceType type) { |
---|
148 | List<ResourceValidator> validators = new ArrayList<ResourceValidator>(); |
---|
149 | validators.add(new ResourceTypeValidator(type)); |
---|
150 | return searchDescendants(validators, true); |
---|
151 | } |
---|
152 | |
---|
153 | public List<? extends ComputingResource> getDescendantsByTypeAndStatus(ResourceType type, ResourceStatus status) { |
---|
154 | List<ResourceValidator> validators = new ArrayList<ResourceValidator>(); |
---|
155 | validators.add(new ResourceStatusValidator(status)); |
---|
156 | validators.add(new ResourceTypeValidator(type)); |
---|
157 | return searchDescendants(validators, true); |
---|
158 | } |
---|
159 | |
---|
160 | public ComputingResource getDescendantByName(String resourceName){ |
---|
161 | List<ResourceValidator> validators = new ArrayList<ResourceValidator>(); |
---|
162 | validators.add(new ResourceNameValidator(resourceName)); |
---|
163 | List<? extends ComputingResource> resources = searchDescendants(validators, true); |
---|
164 | return resources.size() == 0 ? null : resources.get(0); |
---|
165 | } |
---|
166 | |
---|
167 | public List<? extends ComputingResource> filterDescendants(Properties properties) { |
---|
168 | List<ResourceValidator> validators = new ArrayList<ResourceValidator>(); |
---|
169 | validators.add(new ResourcePropertiesValidator(properties)); |
---|
170 | return searchDescendants(validators, false); |
---|
171 | } |
---|
172 | |
---|
173 | protected List<? extends ComputingResource> searchDescendants(List<ResourceValidator> validators, boolean cutOff) { |
---|
174 | |
---|
175 | List<ComputingResource> descendants = new ArrayList<ComputingResource>(); |
---|
176 | if (children != null) { |
---|
177 | LinkedList<ComputingResource> toExamine = new LinkedList<ComputingResource>(); |
---|
178 | toExamine.push(this); |
---|
179 | |
---|
180 | while (!toExamine.isEmpty()) { |
---|
181 | ComputingResource resource = toExamine.pop(); |
---|
182 | List<ComputingResource> resources = resource.getChildren(); |
---|
183 | int numberOfRes = resources.size(); |
---|
184 | for (int i = 0; i < numberOfRes; i++) { |
---|
185 | ComputingResource resourceChild = resources.get(i); |
---|
186 | if (resourceChild.match(validators)) { |
---|
187 | descendants.add(resourceChild); |
---|
188 | if(cutOff == false) { |
---|
189 | toExamine.addLast(resourceChild); |
---|
190 | } |
---|
191 | } else |
---|
192 | //toExamine.insertElementAt(resourceChild, 0); |
---|
193 | toExamine.addLast(resourceChild); |
---|
194 | } |
---|
195 | } |
---|
196 | } |
---|
197 | return descendants; |
---|
198 | } |
---|
199 | |
---|
200 | protected boolean match(List<ResourceValidator> validators){ |
---|
201 | for(ResourceValidator validator: validators){ |
---|
202 | if(validator.validate(this) == false) |
---|
203 | return false; |
---|
204 | } |
---|
205 | return true; |
---|
206 | } |
---|
207 | |
---|
208 | public Properties getProperties(){ |
---|
209 | PropertiesDirector propDirector = new PropertiesDirector(); |
---|
210 | propDirector.setPropertiesBuilder(new DefaultPropertiesBuilder()); |
---|
211 | propDirector.constructProperties(this); |
---|
212 | return propDirector.getProperties(); |
---|
213 | } |
---|
214 | |
---|
215 | public Scheduler getScheduler() { |
---|
216 | return scheduler; |
---|
217 | } |
---|
218 | |
---|
219 | public void setScheduler(Scheduler scheduler) { |
---|
220 | this.scheduler = scheduler; |
---|
221 | if(children != null){ |
---|
222 | for(ComputingResource child: children){ |
---|
223 | child.setScheduler(scheduler); |
---|
224 | } |
---|
225 | } |
---|
226 | } |
---|
227 | |
---|
228 | class ComputingResourceEventHandler implements EventHandler{ |
---|
229 | |
---|
230 | public void handleResourceEvent(Event event){ |
---|
231 | if (extensionList != null) { |
---|
232 | for (Extension extension: extensionList) { |
---|
233 | if (extension.supportsEvent(event)) { |
---|
234 | extension.handleEvent(event); |
---|
235 | } |
---|
236 | } |
---|
237 | |
---|
238 | for (Device device: ((ComputingResourceCharacteristics)resourceCharacteristic).getDevices()) { |
---|
239 | for (Extension extension: device.getExtensionList()) { |
---|
240 | if (extension.supportsEvent(event)) { |
---|
241 | extension.handleEvent(event); |
---|
242 | } |
---|
243 | } |
---|
244 | } |
---|
245 | } |
---|
246 | if(event.getReason() != EventReason.SIM_INIT) |
---|
247 | triggerEventUp(event); |
---|
248 | } |
---|
249 | |
---|
250 | public void handleSchedulingEvent(SchedulingEvent event){ |
---|
251 | |
---|
252 | if(scheduler != null && (parent != null && scheduler != parent.getScheduler())/*scheduler.getResources().contains(this)*/){ |
---|
253 | String src = event.getSource() != null ? event.getSource() : name; |
---|
254 | //ResourceEventFilter filter = new ResourceEventFilter(src.hashCode(), DCWormsTags.UPDATE_PROCESSING); |
---|
255 | //scheduler.sim_cancel(filter, null); |
---|
256 | ResourceStateChangedEvent rscEvent = (ResourceStateChangedEvent) event; |
---|
257 | EnergyEventType eeType = (EnergyEventType)rscEvent.getEventType(); |
---|
258 | scheduler.sendInternal(GridSimTags.SCHEDULE_NOW, DCWormsTags.RESOURCE_STATE_CHANGED, src); |
---|
259 | switch(eeType){ |
---|
260 | |
---|
261 | case FREQUENCY_CHANGED: { |
---|
262 | scheduler.sendInternal(GridSimTags.SCHEDULE_NOW, DCWormsTags.UPDATE_PROCESSING, src); |
---|
263 | break; |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | } else if(parent != null) |
---|
268 | parent.getEventHandler().handleSchedulingEvent(event); |
---|
269 | |
---|
270 | //TODO - check if needed |
---|
271 | //if(event.getReason() != EventReason.SIM_INIT) |
---|
272 | // triggerEventUp(event); |
---|
273 | } |
---|
274 | } |
---|
275 | |
---|
276 | public EventHandler getEventHandler(){ |
---|
277 | return new ComputingResourceEventHandler(); |
---|
278 | } |
---|
279 | |
---|
280 | public final void initiate(){ |
---|
281 | for(Device dev: this.getResourceCharacteristic().getDevices()){ |
---|
282 | dev.initiate(); |
---|
283 | } |
---|
284 | ResourceEventCommand rec = new ResourceEventCommand(this); |
---|
285 | EnergyEvent event = new EnergyEvent(EnergyEventType.AIRFLOW_STATE_CHANGED, "Resource controller"); |
---|
286 | event.setReason(EventReason.SIM_INIT); |
---|
287 | rec.execute(event); |
---|
288 | /*ResourceEventCommand*/ rec = new ResourceEventCommand(this); |
---|
289 | /*EnergyEvent*/ event = new EnergyEvent(EnergyEventType.POWER_STATE_CHANGED, "Resource controller"); |
---|
290 | event.setReason(EventReason.SIM_INIT); |
---|
291 | rec.execute(event); |
---|
292 | |
---|
293 | rec = new ResourceEventCommand(this); |
---|
294 | event = new EnergyEvent(EnergyEventType.TEMPERATURE_CHANGED, "Resource controller"); |
---|
295 | event.setReason(EventReason.SIM_INIT); |
---|
296 | rec.execute(event); |
---|
297 | |
---|
298 | LoadInterface li = getLoadInterface(); |
---|
299 | if(li != null){ |
---|
300 | ResourceLoadCalendar rlc = li.getLoadCalendar(); |
---|
301 | LinkedList<TimestampUtilization> ll = rlc.getLoadDistribution(); |
---|
302 | for(TimestampUtilization tu: ll){ |
---|
303 | DataCenterWorkloadSimulator.getEventManager().sendToResource(getFullName(), tu.getStartTime(), new EnergyEvent(EnergyEventType.RESOURCE_UTILIZATION_CHANGED, getFullName())); |
---|
304 | DataCenterWorkloadSimulator.getEventManager().sendToResource(getFullName(), tu.getEndTime(), new EnergyEvent(EnergyEventType.RESOURCE_UTILIZATION_CHANGED, getFullName())); |
---|
305 | } |
---|
306 | } |
---|
307 | //alternative way |
---|
308 | //getEventHandler().handleResourceEvent(new EnergyEvent(EnergyEventType.AIRFLOW_STATE_CHANGED, "Resource controller")); |
---|
309 | //getEventHandler().handleResourceEvent(new EnergyEvent(EnergyEventType.POWER_STATE_CHANGED, "Resource controller")); |
---|
310 | } |
---|
311 | |
---|
312 | |
---|
313 | } |
---|