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

Revision 477, 18.0 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.Deque;
12import java.util.HashMap;
13import java.util.LinkedList;
14import java.util.List;
15import java.util.Map;
16
17import org.exolab.castor.types.AnyNode;
18import org.exolab.castor.xml.MarshalException;
19import org.exolab.castor.xml.ValidationException;
20import org.qcg.broker.schemas.exception.UnknownParameter;
21
22import schedframe.Initializable;
23import schedframe.Parameter;
24import schedframe.Parameters;
25import schedframe.ResourceController;
26import schedframe.exceptions.ResourceException;
27import schedframe.resources.StandardResourceType;
28import schedframe.resources.computing.ComputingResource;
29import schedframe.resources.computing.ResourceFactory;
30import schedframe.resources.computing.description.AbstractResourceDescription;
31import schedframe.resources.computing.description.ComputingResourceDescription;
32import schedframe.resources.units.ResourceUnit;
33import schedframe.resources.units.ResourceUnitName;
34import schedframe.scheduling.Scheduler;
35import schedframe.scheduling.manager.resources.ManagedResources;
36import schedframe.scheduling.plugin.SchedulingPlugin;
37import schedframe.scheduling.plugin.estimation.ExecutionTimeEstimationPlugin;
38import schedframe.scheduling.queue.TaskQueue;
39import schedframe.scheduling.queue.TaskQueueList;
40import schemas.Environment;
41import schemas.ManagedComputingResources;
42import schemas.StringValueWithUnit;
43import simulator.ConfigurationOptions;
44import simulator.utils.InstanceFactory;
45
46public class ResourceReader {
47       
48        protected String resDescFileName;
49
50        protected ResourceCalendar resourceCalendar;
51        protected Deque<Initializable> toInit = new ArrayDeque<Initializable>();
52       
53        private ExecutionTimeEstimationPlugin execTimeEstimationPlugin;
54        private String globalSchedulingPluginName;
55       
56        public ResourceReader(ConfigurationOptions options) throws IOException {
57
58                resDescFileName = options.resdescFileName;
59                globalSchedulingPluginName = "example.globalplugin.GridFCFSRoundRobinPlugin";
60                prepareCalendar();
61        }
62
63        public ResourceController read() throws MarshalException, ValidationException, FileNotFoundException, Exception,
64                        UnknownParameter {
65
66                //File file = new File("src/test/rewolucja/schemas/example/coolemall/example4.xml");
67                File file = new File(resDescFileName);
68                //File file = new File("example/tomekp/experiment1/tomExp3.xml");
69                /*long s =System.currentTimeMillis();
70                System.out.println("start: ");
71                List<Processor> list = new ArrayList<Processor>();
72                for(int i =0;i<3000000;i++){
73                        schemas.ComputingResource compResDef = new schemas.ComputingResource();
74                        compResDef.setName("a"+i);
75                        compResDef.setClazz("Processor");
76                        CompResourceDescription resDesc = new CompResourceDescription(compResDef);
77                        Processor proc = new Processor(resDesc);
78                        list.add(proc);
79                }
80                long e = System.currentTimeMillis();
81                System.out.println("end: ");
82                System.out.println(e-s);*/
83                Environment env = Environment.unmarshal(new FileReader(file));
84               
85                System.out.println("started creating environment description");
86                List<ComputingResourceDescription> mainCompResDescList = createEnvironmentDescription(env);
87                System.out.println("finished creating environment description");
88
89                System.out.println("started creating resources");
90                List<ComputingResource> computingResources = createResources(mainCompResDescList);
91                System.out.println("finished creating resource");
92
93                System.out.println("started creating schedulers");
94                Scheduler mainScheduler = createSchedulers(env.getResources().getScheduler(), computingResources);
95                System.out.println("finished creating schedulers");
96
97                ResourceController rc = new ResourceController(mainScheduler, computingResources);
98                rc.setInitList(toInit);
99                return rc;
100        }
101
102        protected List<ComputingResourceDescription> createEnvironmentDescription(Environment environment) throws Exception {
103
104                List<ComputingResourceDescription> mainCompResDescList = new ArrayList<ComputingResourceDescription>();
105               
106                LinkedList<ComputingResourceDescription> resDescStructure = new LinkedList<ComputingResourceDescription>();
107                LinkedList<schemas.ComputingResource> toExamine = new LinkedList<schemas.ComputingResource>();
108                EnvironmentWrapper environmentWrapper = new EnvironmentWrapper();
109                environmentWrapper.wrap(environment);
110               
111                String execTimeEstimationPluginClassName = null;
112                if(environmentWrapper.getTimeEstimationPlugin() != null){
113                        execTimeEstimationPluginClassName = environmentWrapper.getTimeEstimationPlugin().getName();
114                }
115               
116                try{
117                        execTimeEstimationPlugin = (ExecutionTimeEstimationPlugin) InstanceFactory.createInstance(
118                                        execTimeEstimationPluginClassName, ExecutionTimeEstimationPlugin.class);
119                        if(execTimeEstimationPlugin == null) {
120                                execTimeEstimationPluginClassName = "example.timeestimation.DefaultTimeEstimationPlugin";
121                                execTimeEstimationPlugin = (ExecutionTimeEstimationPlugin) InstanceFactory.createInstance(
122                                                execTimeEstimationPluginClassName, ExecutionTimeEstimationPlugin.class);
123                        } else {
124                                Parameters params = EnvironmentWrapper.extractParameters(environmentWrapper.getTimeEstimationPlugin().getParameter());
125                                execTimeEstimationPlugin.init(params);
126                        }
127                } catch (Exception e){
128                        if (execTimeEstimationPlugin == null) {
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 = ((AnyNode)compResDef.getComputingResourceTypeChoiceSequence2().getTemplateId()).getFirstChild().getStringValue();
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               
180                Deque<ComputingResourceDescription> toExamine = new ArrayDeque<ComputingResourceDescription>();
181                Deque<ComputingResource> resStructure = new ArrayDeque<ComputingResource>();
182               
183                for(ComputingResourceDescription mainExecResDes : mainCompResDesList){
184                        ComputingResource mainResource = ResourceFactory.createResource(mainExecResDes);
185                        toExamine.push(mainExecResDes);
186                        resStructure.push(mainResource);
187                        mainCompResourceList.add(mainResource);
188                }
189
190                while (!toExamine.isEmpty()) {
191                        ComputingResourceDescription parentResDesc = toExamine.pop();
192                        ComputingResource parentResource = resStructure.pop();
193                        toInit.add(parentResource);
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                        /*ManagementSystem ms = new GridBroker("grid",
264                                        globalSchedulingPluginName, execTimeEstimationPlugin);
265                        mainScheduler = new Scheduler(ms, mainCompResourceList);*/
266                        for(Scheduler lr: mainSchedulers){
267                                mainScheduler.addChild(lr);
268                        }
269                        //necessary if children list isn't initialized in Scheduler
270                        //mainScheduler.init();
271                }
272                return mainScheduler;
273        }
274
275        protected Scheduler initScheduler(schemas.Scheduler schedulerDef, List<ComputingResource> mainCompResourceList) throws Exception{
276                Scheduler scheduler = null;
277               
278                ManagedResources managedResources = null;
279                //List<ComputingResource> managedCompResources = new ArrayList<ComputingResource>();
280                TaskQueueList queues = new TaskQueueList(1);
281               
282                if(schedulerDef.getQueues()!= null){
283                        int queueCount = schedulerDef.getQueues().getQueueCount();
284                        for(int i = 0; i < queueCount; i++){
285                                schemas.QueueType queueDef = schedulerDef.getQueues().getQueue(i);
286                                TaskQueue queue = new TaskQueue(queueDef.getReservation());
287                                queue.setName(queueDef.getName());
288                                queue.setPriority(queueDef.getPriority());
289                                queues.add(queue);
290                        }
291                } else {
292                        TaskQueue queue = new TaskQueue(false);
293                        queues.add(queue);
294                }
295               
296                if(schedulerDef.getSchedulerTypeChoice() != null) {
297                        int schedulerChoiceItemCount = schedulerDef.getSchedulerTypeChoice().getSchedulerTypeChoiceItemCount();
298                        for (int i = 0; i < schedulerChoiceItemCount; i++) {
299                                ManagedComputingResources mcr = schedulerDef.getSchedulerTypeChoice().getSchedulerTypeChoiceItem(i).getManagedComputingResources();
300                                if(mcr == null)
301                                        continue;
302
303                                List<String> managedCompResourcesIds = new ArrayList<String>();
304                                int resourceNameCount = mcr.getResourceNameCount();
305                                for(int j = 0; j < resourceNameCount; j++) {
306                                        managedCompResourcesIds.add(mcr.getResourceName(j));
307                                }
308                                managedResources = matchResourcesForScheduler(mainCompResourceList, managedCompResourcesIds, mcr.getInclude());
309                                //managedResources = new ManagedResources(managedCompResources, new HashMap<ResourceUnitName, List<ResourceUnit>>());
310                        }
311                }
312
313                SchedulingPlugin schedulingPlugin = null;
314                if(schedulerDef.getSchedulingPlugin() != null){
315                        String schedulingPluginName = schedulerDef.getSchedulingPlugin().getName();
316                        schedulingPlugin = (SchedulingPlugin) InstanceFactory.createInstance(schedulingPluginName,
317                                        SchedulingPlugin.class);
318                        Parameters params = EnvironmentWrapper.extractParameters(schedulerDef.getSchedulingPlugin().getParameter());
319                        if(schedulerDef.getSchedulingPlugin().getFrequency() != null)
320                        {
321                                Parameter param = new Parameter("frequency");
322                                StringValueWithUnit sv = new StringValueWithUnit();
323                                sv.setContent(String.valueOf(schedulerDef.getSchedulingPlugin().getFrequency().getContent()));
324                                sv.setUnit(schedulerDef.getSchedulingPlugin().getFrequency().getUnit());
325                                param.add(sv);
326                               
327                                if(params == null)
328                                        params = new Parameters();
329                                params.put("frequency", param);
330                        }
331                        schedulingPlugin.init(params);
332                }       
333
334                //TODO - refactor (create scheduler in 1 line)
335                if(schedulerDef.getClazz().equals("GridBroker")){
336                        scheduler = ResourceFactory.createScheduler(StandardResourceType.GS, "grid", schedulingPlugin, execTimeEstimationPlugin, queues, managedResources);
337                } else {
338                        scheduler = ResourceFactory.createScheduler(StandardResourceType.LS, schedulerDef.getName(), schedulingPlugin, execTimeEstimationPlugin,  queues, managedResources);
339                }
340                return scheduler;
341        }
342       
343        /*private List<ComputingResource> matchCompResourcesForScheduler(List<ComputingResource> mainCompResourceList, List<String> resIdList, boolean include){
344                List<ComputingResource> compResources = new ArrayList<ComputingResource>();
345                for(ComputingResource mainCompRes: mainCompResourceList){
346                        for(String resourceName : resIdList){
347                                ComputingResource computingResource;
348                                try {
349                                        if(resourceName.equals(mainCompRes.getName()))
350                                                computingResource = mainCompRes;
351                                        else
352                                                computingResource = mainCompRes.getDescendantByName(resourceName);
353                                } catch (ResourceException e) {
354                                        computingResource = null;
355                                }
356                                if(computingResource != null)
357                                {
358                                        if(include){
359                                                compResources.add(computingResource);
360                                        } else{
361                                                compResources.addAll(computingResource.getChildren());
362                                        }
363                                }
364                        }
365                }
366                return compResources;
367        }*/
368       
369        private ManagedResources matchResourcesForScheduler(List<ComputingResource> mainCompResourceList, List<String> resIdList, boolean include){
370               
371                List<ComputingResource> compResources = new ArrayList<ComputingResource>();
372                Map<ResourceUnitName, List<ResourceUnit>> resourceUnits = new HashMap<ResourceUnitName, List<ResourceUnit>>();
373               
374                for(ComputingResource mainCompRes: mainCompResourceList){
375                        for(String resourceName : resIdList){
376                                ComputingResource computingResource;
377                                try {
378                                        if(resourceName.equals(mainCompRes.getName()))
379                                                computingResource = mainCompRes;
380                                        else
381                                                computingResource = mainCompRes.getDescendantByName(resourceName);
382                                } catch (ResourceException e) {
383                                        continue;
384                                }
385                                if(computingResource != null)
386                                {
387                                        if(include){
388                                                compResources.add(computingResource);
389                                        } else{
390                                                compResources.addAll(computingResource.getChildren());
391                                                resourceUnits = getSharedResourceUnits(computingResource);
392                                        }
393                                }
394                        }
395                }
396                return new ManagedResources(compResources, resourceUnits);
397        }
398       
399       
400        private Map<ResourceUnitName, List<ResourceUnit>> getSharedResourceUnits(ComputingResource compResources){
401                Map<ResourceUnitName, List<ResourceUnit>> resourceUnits = new HashMap<ResourceUnitName, List<ResourceUnit>>();
402                List<ResourceUnit> list;
403                boolean resourceNotVisited = true;
404                ComputingResource parent = compResources;
405                while(parent != null && resourceNotVisited){
406                        Map<ResourceUnitName, List<ResourceUnit>> resUnits = parent.getResourceCharacteristic().getResourceUnits();
407                        for(ResourceUnitName run : resUnits.keySet()){
408                                for(ResourceUnit resUnit : resUnits.get(run)){
409                                        if((resourceUnits.get(run) == null)){
410                                                list = new ArrayList<ResourceUnit>(1);
411                                                resourceUnits.put(resUnit.getName(), list);
412                                                list.add(resUnit);
413                                        } else if(!resourceUnits.get(run).contains(resUnit)){
414                                                list = resourceUnits.get(resUnit.getName());
415                                                list.add(resUnit);
416                                        } else {
417                                                resourceNotVisited = false;
418                                        }
419                                }
420                        }
421                        parent = parent.getParent();
422                }
423                return resourceUnits;
424        }
425       
426        private void prepareCalendar() {
427                long seed = 11L * 13L * 17L * 19L * 23L + 1L;
428                double timeZone = 0.0;
429                double peakLoad = 0.0; // the resource load during peak hour
430                double offPeakLoad = 0.0; // the resource load during off-peak hr
431                double holidayLoad = 0.0; // the resource load during holiday
432
433                // incorporates weekends so the grid resource is on 7 days a week
434                LinkedList<Integer> Weekends = new LinkedList<Integer>();
435                Weekends.add(java.util.Calendar.SATURDAY);
436                Weekends.add(java.util.Calendar.SUNDAY);
437
438                // incorporates holidays. However, no holidays are set in this example
439                LinkedList<Integer> Holidays = new LinkedList<Integer>();
440                resourceCalendar = new ResourceCalendar(timeZone, peakLoad, offPeakLoad, holidayLoad, Weekends, Holidays,
441                                seed);
442        }
443       
444}
Note: See TracBrowser for help on using the repository browser.