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

Revision 1415, 11.6 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(1);
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        protected void initCharacteristics(ComputingResourceDescription resDesc){
62                resourceCharacteristic = ComputingResourceCharacteristics.builder().resourceUnits(resDesc.getResourceUnits()).location(resDesc.getLocation()).parameters(resDesc.getParameters()).device(resDesc.getDevices()).build();
63                for(Device device: ((ComputingResourceCharacteristics)resourceCharacteristic).getDevices()){
64                        device.setComputingResource(this);
65                }
66        }
67       
68        public ComputingResource getParent() {
69                return parent;
70        }
71
72        public void setParent(ComputingResource newParent) {
73                parent = newParent;
74                /*if(this.getLoadInterface().getLoadCalendar().getLoadDistribution().size() == 0){
75                        this.getLoadInterface().getLoadCalendar().getLoadDistribution().addAll(parent.getLoadInterface().getLoadCalendar().getLoadDistribution());
76                }*/
77        }
78
79        public List<ComputingResource> getChildren() {
80                if (children == null)
81                        return new ArrayList<ComputingResource>(1);
82                return children;
83        }
84
85        public void addChild(ComputingResource child) {
86                child.setParent(this);
87                if (children == null)
88                        children = new ArrayList<ComputingResource>();
89                children.add(child);
90        }
91
92        public String getFullName() {
93                if(this.getResourceCharacteristic().getParameters().get("fullPath") != null){
94                        String fullPath = this.getResourceCharacteristic().getParameters().get("fullPath").get(0).getContent();
95                        return fullPath;
96                }
97                if(parent!= null){
98                        return parent.getFullName() + "/" + name;
99                } else
100                        return name;
101        }
102
103        public ComputingResourceCharacteristics getResourceCharacteristic() {
104                return (ComputingResourceCharacteristics)resourceCharacteristic;
105        }
106
107        public void setStatus(ResourceStatus newStatus) {
108                if(newStatus != status){
109                        status = newStatus;
110                        if(children != null) {
111                                for(ComputingResource child: children){
112                                        child.setStatus(status);
113                                }
114                        }       
115                }
116        }
117       
118        private void triggerEventUp(ResourceEvent event) {
119                if(parent != null)
120                        parent.handleEvent(event);
121        }
122
123        public void handleEvent(ResourceEvent event){
124                ResourceEventCommand rec = new ResourceEventCommand(this);
125                rec.execute(event);
126                SchedulingEventCommand sec = new SchedulingEventCommand(this);
127                sec.execute(event);
128
129                //old, correctly working method
130                /*if (extensionList != null) {
131                        for (Extension extension : extensionList) {
132                                if (extension.supportsEvent(event)) {
133                                        extension.handleEvent(event);
134                                }
135                        }
136                }*/
137                //TODO - delete, check in advance
138                //if(scheduler != null && (parent != null && scheduler != parent.getScheduler())/*scheduler.getResources().contains(this)*/){
139                //      String src = event.getSource() != null ? event.getSource() : name;
140                //      scheduler.sendInternal(GridSimTags.SCHEDULE_NOW, DCWormsTags.UPDATE, src);
141                //}
142                //triggerEventUp(event);
143        }
144       
145        public List <? extends ComputingResource> getDescendantsByType(ResourceType type) {
146                List<ResourceValidator> validators = new ArrayList<ResourceValidator>();
147                validators.add(new ResourceTypeValidator(type));
148                return searchDescendants(validators, true);
149        }
150
151        public List<? extends ComputingResource> getDescendantsByTypeAndStatus(ResourceType type, ResourceStatus status) {
152                List<ResourceValidator> validators = new ArrayList<ResourceValidator>();
153                validators.add(new ResourceStatusValidator(status));
154                validators.add(new ResourceTypeValidator(type));
155                return searchDescendants(validators, true);
156        }
157
158        public ComputingResource getDescendantByName(String resourceName){
159                List<ResourceValidator> validators = new ArrayList<ResourceValidator>();
160                validators.add(new ResourceNameValidator(resourceName));
161                List<? extends ComputingResource> resources = searchDescendants(validators, true);
162                return resources.size() == 0 ? null : resources.get(0);
163        }
164       
165        public List<? extends ComputingResource> filterDescendants(Properties properties)  {
166                List<ResourceValidator> validators = new ArrayList<ResourceValidator>();
167                validators.add(new ResourcePropertiesValidator(properties));
168                return searchDescendants(validators, false);
169        }
170       
171        protected List<? extends ComputingResource> searchDescendants(List<ResourceValidator> validators, boolean cutOff) {
172
173                List<ComputingResource> descendants = new ArrayList<ComputingResource>();
174                if (children != null) {
175                        LinkedList<ComputingResource> toExamine = new LinkedList<ComputingResource>();
176                        toExamine.push(this);
177
178                        while (!toExamine.isEmpty()) {
179                                ComputingResource resource = toExamine.pop();
180                                List<ComputingResource> resources = resource.getChildren();
181                                int numberOfRes = resources.size();
182                                for (int i = 0; i < numberOfRes; i++) {
183                                        ComputingResource resourceChild = resources.get(i);
184                                        if (resourceChild.match(validators)) {
185                                                descendants.add(resourceChild);
186                                                if(cutOff == false) {
187                                                        toExamine.addLast(resourceChild);
188                                                }
189                                        } else
190                                                //toExamine.insertElementAt(resourceChild, 0);
191                                                toExamine.addLast(resourceChild);
192                                }
193                        }
194                }
195                return descendants;
196        }
197       
198        protected boolean match(List<ResourceValidator> validators){
199                for(ResourceValidator validator: validators){
200                        if(validator.validate(this) == false)
201                                return false;
202                }
203                return true;
204        }
205       
206        public Properties getProperties(){
207                PropertiesDirector propDirector = new PropertiesDirector();
208                propDirector.setPropertiesBuilder(new DefaultPropertiesBuilder());
209                propDirector.constructProperties(this);
210                return propDirector.getProperties();
211        }
212       
213        public Scheduler getScheduler() {
214                return scheduler;
215        }
216       
217        public void setScheduler(Scheduler scheduler) {
218                this.scheduler = scheduler;
219                if(children != null){
220                        for(ComputingResource child: children){
221                                child.setScheduler(scheduler);
222                        }       
223                }
224        }
225
226        class ComputingResourceEventHandler implements EventHandler{
227               
228                public void handleResourceEvent(ResourceEvent event){
229                        if (extensionList != null) {
230                                for (Extension extension: extensionList) {
231                                        if (extension.supportsEvent(event)) {
232                                                extension.handleEvent(event);
233                                        }
234                                }
235                               
236                                for (Device device: ((ComputingResourceCharacteristics)resourceCharacteristic).getDevices()) {
237                                        for (Extension extension: device.getExtensionList()) {
238                                                if (extension.supportsEvent(event)) {
239                                                        extension.handleEvent(event);
240                                                }
241                                        }
242                                }
243                        }
244                        if(event.getReason() != EventReason.SIM_INIT)
245                                triggerEventUp(event);
246                }
247               
248                public void handleSchedulingEvent(SchedulingEvent event){
249
250                        if(scheduler != null && (parent != null && scheduler != parent.getScheduler())/*scheduler.getResources().contains(this)*/){
251                                String src = event.getSource() != null ? event.getSource() : name;
252                                //ResourceEventFilter filter = new ResourceEventFilter(src.hashCode(), DCWormsTags.UPDATE_PROCESSING);
253                                //scheduler.sim_cancel(filter, null);
254                                ResourceStateChangedEvent rscEvent = (ResourceStateChangedEvent) event;
255                                ResourceEventType reType = rscEvent.getResourceEventType();
256                                //scheduler.sendInternal(GridSimTags.SCHEDULE_NOW, DCWormsTags.UPDATE_PROCESSING, src);
257                                switch(reType){
258                               
259                                        case UTILIZATION_CHANGED: {
260                                                scheduler.sendInternal(GridSimTags.SCHEDULE_NOW, DCWormsTags.UPDATE_PROCESSING, src);
261                                                break;
262                                        }
263                                        case CPU_FREQUENCY_CHANGED: {
264                                                scheduler.sendInternal(GridSimTags.SCHEDULE_NOW, DCWormsTags.UPDATE_PROCESSING, src);
265                                                break;
266                                        }
267                                }
268
269                        } else if(parent != null)
270                                parent.getEventHandler().handleSchedulingEvent(event);
271                       
272                        //TODO - check if needed
273                        //if(event.getReason() != EventReason.SIM_INIT)
274                        //      triggerEventUp(event);
275                }
276        }
277       
278        public EventHandler getEventHandler(){
279                return new ComputingResourceEventHandler();
280        }
281       
282        public final void initiate(){
283                for(Device dev: this.getResourceCharacteristic().getDevices()){
284                        dev.initiate();
285                }
286                ResourceEventCommand rec = new ResourceEventCommand(this);
287                ResourceEvent event = new ResourceEvent(ResourceEventType.AIRFLOW_STATE_CHANGED, "Resource controller");
288                event.setReason(EventReason.SIM_INIT);
289                rec.execute(event);
290                /*ResourceEventCommand*/ rec = new ResourceEventCommand(this);
291                /*ResourceEvent*/ event = new ResourceEvent(ResourceEventType.POWER_STATE_CHANGED, "Resource controller");
292                event.setReason(EventReason.SIM_INIT);
293                rec.execute(event);
294               
295                rec = new ResourceEventCommand(this);
296                event = new ResourceEvent(ResourceEventType.TEMPERATURE_CHANGED, "Resource controller");
297                event.setReason(EventReason.SIM_INIT);
298                rec.execute(event);
299               
300                LoadInterface li = getLoadInterface();
301                if(li != null){
302                        ResourceLoadCalendar rlc = li.getLoadCalendar();
303                        LinkedList<TimestampUtilization> ld = rlc.getLoadDistribution();
304                        for(TimestampUtilization tu: ld){
305                                DataCenterWorkloadSimulator.getEventManager().sendToResource(getFullName(), tu.getStartTime(), new ResourceEvent(ResourceEventType.UTILIZATION_CHANGED, getFullName()));
306                                DataCenterWorkloadSimulator.getEventManager().sendToResource(getFullName(), tu.getEndTime(), new ResourceEvent(ResourceEventType.UTILIZATION_CHANGED, getFullName()));
307                        }                                       
308                }
309                //alternative way
310                //getEventHandler().handleResourceEvent(new EnergyEvent(EnergyEventType.AIRFLOW_STATE_CHANGED, "Resource controller"));
311                //getEventHandler().handleResourceEvent(new EnergyEvent(EnergyEventType.POWER_STATE_CHANGED, "Resource controller"));
312        }
313
314}
Note: See TracBrowser for help on using the repository browser.