source: DCWoRMS/branches/coolemall/src/schedframe/resources/computing/ComputingResource.java @ 1423

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