source: DCWoRMS/trunk/build/classes/simulator/reader/ResourceReader.java @ 539

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