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

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