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

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