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

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