source: DCWoRMS/branches/coolemall/src/simulator/reader/ResourceReader.java @ 1004

Revision 1004, 17.8 KB checked in by wojtekp, 12 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package simulator.reader;
2
3import gridsim.ResourceCalendar;
4
5import java.io.File;
6import java.io.FileNotFoundException;
7import java.io.FileReader;
8import java.io.IOException;
9import java.util.ArrayDeque;
10import java.util.ArrayList;
11import java.util.Collections;
12import java.util.Comparator;
13import java.util.Deque;
14import java.util.HashMap;
15import java.util.LinkedHashSet;
16import java.util.LinkedList;
17import java.util.List;
18import java.util.Map;
19import java.util.Set;
20
21import org.exolab.castor.xml.MarshalException;
22import org.exolab.castor.xml.ValidationException;
23import org.qcg.broker.schemas.exception.UnknownParameter;
24
25import schedframe.Initializable;
26import schedframe.Parameter;
27import schedframe.Parameters;
28import schedframe.ResourceController;
29import schedframe.exceptions.ResourceException;
30import schedframe.resources.Resource;
31import schedframe.resources.StandardResourceType;
32import schedframe.resources.computing.ComputingResource;
33import schedframe.resources.computing.ResourceFactory;
34import schedframe.resources.computing.description.AbstractResourceDescription;
35import schedframe.resources.computing.description.ComputingResourceDescription;
36import schedframe.resources.units.ResourceUnit;
37import schedframe.resources.units.ResourceUnitName;
38import schedframe.scheduling.Scheduler;
39import schedframe.scheduling.manager.resources.ManagedResources;
40import schedframe.scheduling.plugin.SchedulingPlugin;
41import schedframe.scheduling.plugin.estimation.ExecutionTimeEstimationPlugin;
42import schedframe.scheduling.queue.TaskQueue;
43import schedframe.scheduling.queue.TaskQueueList;
44import schemas.Environment;
45import schemas.ManagedComputingResources;
46import schemas.StringValueWithUnit;
47import simulator.ConfigurationOptions;
48import simulator.utils.InstanceFactory;
49
50public class ResourceReader {
51       
52        protected String resDescFileName;
53
54        protected ResourceCalendar resourceCalendar;
55        protected List<Initializable> toInit = new ArrayList<Initializable>();
56       
57        private ExecutionTimeEstimationPlugin execTimeEstimationPlugin;
58        private String globalSchedulingPluginName;
59       
60        private Set<String> compResLayers;
61       
62        public ResourceReader(ConfigurationOptions options) throws IOException {
63
64                resDescFileName = options.resdescFileName;
65                globalSchedulingPluginName = "example.globalplugin.GridFCFSRoundRobinPlugin";
66                prepareCalendar();
67                compResLayers = new LinkedHashSet<String>();
68        }
69
70        public ResourceController read() throws MarshalException, ValidationException, FileNotFoundException, Exception,
71                        UnknownParameter {
72
73                File file = new File(resDescFileName);
74                Environment env = Environment.unmarshal(new FileReader(file));
75               
76                System.out.println("started creating environment description");
77                List<ComputingResourceDescription> mainCompResDescList = createEnvironmentDescription(env);
78                System.out.println("finished creating environment description");
79
80                System.out.println("started creating resources");
81                List<ComputingResource> computingResources = createResources(mainCompResDescList);
82                System.out.println("finished creating resource");
83
84                System.out.println("started creating schedulers");
85                Scheduler mainScheduler = createSchedulers(env.getResources().getScheduler(), computingResources);
86                System.out.println("finished creating schedulers");
87
88                ResourceController rc = new ResourceController(mainScheduler, computingResources);
89                Collections.sort(toInit, new ResourceTypeComparator(new ArrayList<String>(compResLayers)));
90                rc.setInitList(toInit);
91                rc.setCompResLayers(compResLayers);
92                return rc;
93        }
94
95        protected List<ComputingResourceDescription> createEnvironmentDescription(Environment environment) throws Exception {
96
97                List<ComputingResourceDescription> mainCompResDescList = new ArrayList<ComputingResourceDescription>();
98               
99                LinkedList<ComputingResourceDescription> resDescStructure = new LinkedList<ComputingResourceDescription>();
100                LinkedList<schemas.ComputingResource> toExamine = new LinkedList<schemas.ComputingResource>();
101                EnvironmentWrapper environmentWrapper = new EnvironmentWrapper();
102                environmentWrapper.wrap(environment);
103               
104                String execTimeEstimationPluginClassName = null;
105                if(environmentWrapper.getTimeEstimationPlugin() != null){
106                        execTimeEstimationPluginClassName = environmentWrapper.getTimeEstimationPlugin().getName();
107                       
108                } else {
109                        execTimeEstimationPluginClassName = "example.timeestimation.DefaultTimeEstimationPlugin";
110                }
111               
112                execTimeEstimationPlugin = (ExecutionTimeEstimationPlugin) InstanceFactory.createInstance(
113                                execTimeEstimationPluginClassName, ExecutionTimeEstimationPlugin.class);
114                if(execTimeEstimationPlugin != null) {
115                        if(environmentWrapper.getTimeEstimationPlugin() != null){
116                                Parameters params = EnvironmentWrapper.extractParameters(environmentWrapper.getTimeEstimationPlugin().getParameter());
117                                execTimeEstimationPlugin.init(params);
118                        }
119                }
120                else {
121                        throw new Exception("Can not create execution time estimation plugin instance.");
122                }
123
124
125                if(environmentWrapper.getComputingResources() != null) {
126                        for(int i = 0; i < environmentWrapper.getComputingResources().length; i++){
127                                schemas.ComputingResource compResDef = environmentWrapper.getComputingResources()[i];
128                                toExamine.push(compResDef);
129                               
130                                ComputingResourceDescription compResDesc = new ComputingResourceDescription(compResDef);
131                                resDescStructure.push(compResDesc);
132                                mainCompResDescList.add(compResDesc);
133                        }
134                }
135                while (!toExamine.isEmpty()) {
136                        schemas.ComputingResource parentCompResDef = toExamine.pop();
137                        ComputingResourceDescription parentExecResDesc = resDescStructure.pop();
138                        if(parentCompResDef.getComputingResourceTypeChoiceSequence() != null)
139                        {
140                                int computingResourceCount = parentCompResDef.getComputingResourceTypeChoiceSequence().getComputingResourceCount();
141                                for (int i = 0; i < computingResourceCount; i++) {
142
143                                        schemas.ComputingResource compResDef = parentCompResDef.getComputingResourceTypeChoiceSequence().getComputingResource(i);
144                                        if(compResDef == null)
145                                                continue;
146                                        long compResCount = compResDef.getCount() > 1 ? compResDef.getCount() : 1;
147                                       
148                                        for(int j = 0; j < compResCount; j++){
149                                                if(compResDef.getComputingResourceTypeChoiceSequence2() != null){
150                                                        String templateId = compResDef.getComputingResourceTypeChoiceSequence2().getTemplateId();
151                                                        schemas.ComputingResourceTemplate template = environmentWrapper.getTemplate(templateId);
152                                                        environmentWrapper.initWithCompResTemplate(compResDef, template);
153                                                }
154                                                //toExamine.insertElementAt(compResDef, 0);
155                                                toExamine.addLast(compResDef);
156                                                ComputingResourceDescription resDesc = new ComputingResourceDescription(compResDef);
157                                                parentExecResDesc.addChildren(resDesc);
158                                                //resDescStructure.insertElementAt(resDesc, 0);
159                                                resDescStructure.addLast(resDesc);
160                                        }
161                                }
162                        } else
163                                continue;
164                }
165                return mainCompResDescList;
166        }
167
168        protected List<ComputingResource> createResources(List<ComputingResourceDescription> mainCompResDesList) {
169
170                List<ComputingResource> mainCompResourceList = new ArrayList<ComputingResource>();
171                Deque<ComputingResourceDescription> toExamine = new ArrayDeque<ComputingResourceDescription>();
172                Deque<ComputingResource> resStructure = new ArrayDeque<ComputingResource>();
173               
174                for(ComputingResourceDescription mainExecResDes : mainCompResDesList){
175                        ComputingResource mainResource = ResourceFactory.createResource(mainExecResDes);
176                        toExamine.push(mainExecResDes);
177                        resStructure.push(mainResource);
178                        mainCompResourceList.add(mainResource);
179                }
180
181                while (!toExamine.isEmpty()) {
182                        ComputingResourceDescription parentResDesc = toExamine.pop();
183                        ComputingResource parentResource = resStructure.pop();
184                        toInit.add(parentResource);
185                        compResLayers.add(parentResource.getType().getName());
186                        List<AbstractResourceDescription> childrenResDesc = parentResDesc.getChildren();
187                        if (childrenResDesc == null){
188                                continue;
189                        }
190                        int compResCount = childrenResDesc.size();
191                        for (int i = 0; i < compResCount; i++) {
192                                ComputingResourceDescription compResDesc = (ComputingResourceDescription) childrenResDesc.get(i);
193                                toExamine.push(compResDesc);
194                                ComputingResource resource = ResourceFactory.createResource(compResDesc);
195                                parentResource.addChild(resource);
196                                resStructure.push(resource);
197                        }
198                }
199                return mainCompResourceList;
200        }
201
202        protected Scheduler createSchedulers(schemas.Scheduler[] schedulersDef, List<ComputingResource> mainCompResourceList) throws Exception{
203
204                List<Scheduler> mainSchedulers = new ArrayList<Scheduler>();
205                       
206                Deque<schemas.Scheduler> toExamine = new ArrayDeque<schemas.Scheduler>();       
207                Deque<Scheduler> schedulersStructure = new ArrayDeque<Scheduler>();
208                for(int i = 0; i < schedulersDef.length; i++){
209                        schemas.Scheduler schedulerDef = schedulersDef[i];
210                        Scheduler mainScheduler = initScheduler(schedulerDef, mainCompResourceList);
211                        toExamine.push(schedulerDef);
212                        schedulersStructure.push(mainScheduler);
213                        mainSchedulers.add(mainScheduler);
214                }       
215       
216                while (!toExamine.isEmpty()) {
217                        schemas.Scheduler parentSchedulerDef = toExamine.pop();
218               
219                        Scheduler parentScheduler = null;
220                        if(!schedulersStructure.isEmpty())
221                                parentScheduler = schedulersStructure.pop();
222                       
223                        if(parentSchedulerDef.getSchedulerTypeChoice() == null)
224                                continue;
225                        int schedulerChoiceItemCount = parentSchedulerDef.getSchedulerTypeChoice().getSchedulerTypeChoiceItemCount();
226                        for (int i = 0; i < schedulerChoiceItemCount; i++) {
227                                schemas.Scheduler schedulerDef = parentSchedulerDef.getSchedulerTypeChoice().getSchedulerTypeChoiceItem(i).getScheduler();
228                                if(schedulerDef == null)
229                                        continue;
230                                else
231                                {
232                                        Scheduler scheduler = initScheduler(schedulerDef, mainCompResourceList);
233                                        schedulersStructure.push(scheduler);
234                                        parentScheduler.addChild(scheduler);
235                                        toExamine.push(schedulerDef);
236                                }
237                        }
238                        //necessary if children list isn't initialized in Scheduler
239                        //parentScheduler.init();
240                }
241
242                //TODO - refactor (remove - create scheduler on the basis of resource description)
243                Scheduler mainScheduler = null;
244                if(mainSchedulers.size() == 1 /*&& mainSchedulers.get(0).get_name().equals("grid")*/){
245                        mainScheduler = mainSchedulers.get(0);
246                }
247                else{
248                        SchedulingPlugin schedulingPlugin = (SchedulingPlugin) InstanceFactory.createInstance(globalSchedulingPluginName,
249                                        SchedulingPlugin.class);
250                        TaskQueueList queues = new TaskQueueList(1);
251                        TaskQueue queue = new TaskQueue(false);
252                        queues.add(queue);
253                        ManagedResources managedResources = new ManagedResources(mainCompResourceList, new HashMap<ResourceUnitName, List<ResourceUnit>>());
254                        mainScheduler = ResourceFactory.createScheduler(StandardResourceType.GS, "grid", schedulingPlugin , execTimeEstimationPlugin,  queues, managedResources);
255
256                        for(Scheduler lr: mainSchedulers){
257                                mainScheduler.addChild(lr);
258                        }
259                        //necessary if children list isn't initialized in Scheduler
260                        //mainScheduler.init();
261                }
262                return mainScheduler;
263        }
264
265        protected Scheduler initScheduler(schemas.Scheduler schedulerDef, List<ComputingResource> mainCompResourceList) throws Exception{
266                Scheduler scheduler = null;
267               
268                ManagedResources managedResources = null;
269                //List<ComputingResource> managedCompResources = new ArrayList<ComputingResource>();
270                TaskQueueList queues = new TaskQueueList(1);
271               
272                if(schedulerDef.getQueues()!= null){
273                        int queueCount = schedulerDef.getQueues().getQueueCount();
274                        for(int i = 0; i < queueCount; i++){
275                                schemas.QueueType queueDef = schedulerDef.getQueues().getQueue(i);
276                                TaskQueue queue = new TaskQueue(queueDef.getReservation());
277                                queue.setName(queueDef.getName());
278                                queue.setPriority(queueDef.getPriority());
279                                queues.add(queue);
280                        }
281                } else {
282                        TaskQueue queue = new TaskQueue(false);
283                        queues.add(queue);
284                }
285               
286                if(schedulerDef.getSchedulerTypeChoice() != null) {
287                        int schedulerChoiceItemCount = schedulerDef.getSchedulerTypeChoice().getSchedulerTypeChoiceItemCount();
288                        for (int i = 0; i < schedulerChoiceItemCount; i++) {
289                                ManagedComputingResources mcr = schedulerDef.getSchedulerTypeChoice().getSchedulerTypeChoiceItem(i).getManagedComputingResources();
290                                if(mcr == null)
291                                        continue;
292
293                                List<String> managedCompResourcesIds = new ArrayList<String>();
294                                int resourceNameCount = mcr.getResourceNameCount();
295                                for(int j = 0; j < resourceNameCount; j++) {
296                                        managedCompResourcesIds.add(mcr.getResourceName(j));
297                                }
298                                managedResources = matchResourcesForScheduler(mainCompResourceList, managedCompResourcesIds, mcr.getInclude());
299                                //managedResources = new ManagedResources(managedCompResources, new HashMap<ResourceUnitName, List<ResourceUnit>>());
300                        }
301                }
302
303                SchedulingPlugin schedulingPlugin = null;
304                if(schedulerDef.getSchedulingPlugin() != null){
305                        String schedulingPluginName = schedulerDef.getSchedulingPlugin().getName();
306                        schedulingPlugin = (SchedulingPlugin) InstanceFactory.createInstance(schedulingPluginName,
307                                        SchedulingPlugin.class);
308                        Parameters params = EnvironmentWrapper.extractParameters(schedulerDef.getSchedulingPlugin().getParameter());
309                        if(schedulerDef.getSchedulingPlugin().getFrequency() != null)
310                        {
311                                Parameter param = new Parameter("frequency");
312                                StringValueWithUnit sv = new StringValueWithUnit();
313                                sv.setContent(String.valueOf(schedulerDef.getSchedulingPlugin().getFrequency().getContent()));
314                                sv.setUnit(schedulerDef.getSchedulingPlugin().getFrequency().getUnit());
315                                param.add(sv);
316                               
317                                if(params == null)
318                                        params = new Parameters();
319                                params.put("frequency", param);
320                        }
321                        schedulingPlugin.init(params);
322                }       
323
324                //TODO - refactor (create scheduler in 1 line)
325                if(schedulerDef.getClazz().equals("GridBroker")){
326                        scheduler = ResourceFactory.createScheduler(StandardResourceType.GS, "grid", schedulingPlugin, execTimeEstimationPlugin, queues, managedResources);
327                } else {
328                        scheduler = ResourceFactory.createScheduler(StandardResourceType.LS, schedulerDef.getName(), schedulingPlugin, execTimeEstimationPlugin,  queues, managedResources);
329                }
330                return scheduler;
331        }
332       
333        /*private List<ComputingResource> matchCompResourcesForScheduler(List<ComputingResource> mainCompResourceList, List<String> resIdList, boolean include){
334                List<ComputingResource> compResources = new ArrayList<ComputingResource>();
335                for(ComputingResource mainCompRes: mainCompResourceList){
336                        for(String resourceName : resIdList){
337                                ComputingResource computingResource;
338                                try {
339                                        if(resourceName.equals(mainCompRes.getName()))
340                                                computingResource = mainCompRes;
341                                        else
342                                                computingResource = mainCompRes.getDescendantByName(resourceName);
343                                } catch (ResourceException e) {
344                                        computingResource = null;
345                                }
346                                if(computingResource != null)
347                                {
348                                        if(include){
349                                                compResources.add(computingResource);
350                                        } else{
351                                                compResources.addAll(computingResource.getChildren());
352                                        }
353                                }
354                        }
355                }
356                return compResources;
357        }*/
358       
359        private ManagedResources matchResourcesForScheduler(List<ComputingResource> mainCompResourceList, List<String> resIdList, boolean include){
360               
361                List<ComputingResource> compResources = new ArrayList<ComputingResource>();
362                Map<ResourceUnitName, List<ResourceUnit>> resourceUnits = new HashMap<ResourceUnitName, List<ResourceUnit>>();
363               
364                for(ComputingResource mainCompRes: mainCompResourceList){
365                        for(String resourceName : resIdList){
366                                ComputingResource computingResource;
367                                try {
368                                        if(resourceName.equals(mainCompRes.getName()))
369                                                computingResource = mainCompRes;
370                                        else
371                                                computingResource = mainCompRes.getDescendantByName(resourceName);
372                                } catch (ResourceException e) {
373                                        continue;
374                                }
375                                if(computingResource != null)
376                                {
377                                        if(include){
378                                                compResources.add(computingResource);
379                                        } else{
380                                                compResources.addAll(computingResource.getChildren());
381                                                resourceUnits = getSharedResourceUnits(computingResource);
382                                        }
383                                }
384                        }
385                }
386                return new ManagedResources(compResources, resourceUnits);
387        }
388       
389       
390        private Map<ResourceUnitName, List<ResourceUnit>> getSharedResourceUnits(ComputingResource compResources){
391                Map<ResourceUnitName, List<ResourceUnit>> resourceUnits = new HashMap<ResourceUnitName, List<ResourceUnit>>();
392                List<ResourceUnit> list;
393                boolean resourceNotVisited = true;
394                ComputingResource parent = compResources;
395                while(parent != null && resourceNotVisited){
396                        Map<ResourceUnitName, List<ResourceUnit>> resUnits = parent.getResourceCharacteristic().getResourceUnits();
397                        for(ResourceUnitName run : resUnits.keySet()){
398                                for(ResourceUnit resUnit : resUnits.get(run)){
399                                        if((resourceUnits.get(run) == null)){
400                                                list = new ArrayList<ResourceUnit>(1);
401                                                resourceUnits.put(resUnit.getName(), list);
402                                                list.add(resUnit);
403                                        } else if(!resourceUnits.get(run).contains(resUnit)){
404                                                list = resourceUnits.get(resUnit.getName());
405                                                list.add(resUnit);
406                                        } else {
407                                                resourceNotVisited = false;
408                                        }
409                                }
410                        }
411                        parent = parent.getParent();
412                }
413                return resourceUnits;
414        }
415       
416        private void prepareCalendar() {
417                long seed = 11L * 13L * 17L * 19L * 23L + 1L;
418                double timeZone = 0.0;
419                double peakLoad = 0.0; // the resource load during peak hour
420                double offPeakLoad = 0.0; // the resource load during off-peak hr
421                double holidayLoad = 0.0; // the resource load during holiday
422
423                // incorporates weekends so the grid resource is on 7 days a week
424                LinkedList<Integer> Weekends = new LinkedList<Integer>();
425                Weekends.add(java.util.Calendar.SATURDAY);
426                Weekends.add(java.util.Calendar.SUNDAY);
427
428                // incorporates holidays. However, no holidays are set in this example
429                LinkedList<Integer> Holidays = new LinkedList<Integer>();
430                resourceCalendar = new ResourceCalendar(timeZone, peakLoad, offPeakLoad, holidayLoad, Weekends, Holidays,
431                                seed);
432        }
433       
434        class ResourceTypeComparator implements Comparator<Initializable>{
435               
436                List<String> order;
437                ResourceTypeComparator(List<String> order) {
438                        this.order = order;
439                }
440                public int compare(Initializable init1, Initializable init2) {
441                        String type1 = ((Resource)init1).getType().getName();
442                        String type2 = ((Resource)init2).getType().getName();
443                        if(order.indexOf(type1) > order.indexOf(type2))
444                                return -1;
445                        else if (order.indexOf(type1) < order.indexOf(type2))
446                                return 1;
447                        else return 0;
448                       
449                }
450        }
451       
452}
Note: See TracBrowser for help on using the repository browser.