source: DCWoRMS/trunk/src/simulator/reader/ResourceReader.java @ 512

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