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

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