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

Revision 1396, 17.3 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.resources.CoolEmAllResourceFactory;
29import schedframe.resources.Resource;
30import schedframe.resources.StandardResourceType;
31import schedframe.resources.computing.ComputingResource;
32import schedframe.resources.computing.ResourceFactory;
33import schedframe.resources.computing.StandardResourceFactory;
34import schedframe.resources.computing.description.ComputingResourceDescription;
35import schedframe.resources.computing.description.ResourceDescription;
36import schedframe.resources.units.ResourceUnit;
37import schedframe.resources.units.ResourceUnitName;
38import schedframe.scheduling.Scheduler;
39import schedframe.scheduling.manager.resources.ManagedResources;
40import schedframe.scheduling.plugin.SchedulingPlugin;
41import schedframe.scheduling.plugin.estimation.ExecutionTimeEstimationPlugin;
42import schedframe.scheduling.queue.TaskQueue;
43import schedframe.scheduling.queue.TaskQueueList;
44import schemas.Environment;
45import schemas.ManagedComputingResources;
46import schemas.StringValueWithUnit;
47import simulator.DataCenterWorkloadSimulator;
48import simulator.utils.InstanceFactory;
49import test.DEBBTranslator.src.PLMXMLTranslator;
50
51public class ResourceReader {
52       
53        private static String COOLEMALL_RESDESC_PREFIX = "DCWORMS";
54        private String resDescFileName;
55        private ResourceFactory resFactory;
56               
57        private ExecutionTimeEstimationPlugin execTimeEstimationPlugin;
58       
59        private Set<String> compResLayers;
60        private List<Initializable> toInit;
61       
62        public ResourceReader(String resdescFileName) throws IOException {
63
64                this.resDescFileName = resdescFileName;
65                this.resFactory = new StandardResourceFactory();
66                this.compResLayers = new LinkedHashSet<String>();
67                this.toInit = new ArrayList<Initializable>();
68        }
69
70        public SimulatedEnvironment read() throws MarshalException, ValidationException, FileNotFoundException, Exception,
71                        UnknownParameter {
72
73                File file = new File(resDescFileName);
74
75                Environment env = null;
76                try{
77                        env = Environment.unmarshal(new FileReader(file));
78                       
79                } catch (Exception e){ 
80                        //e.printStackTrace();
81                        try{
82                                File dcwormsFile = new File(FilenameUtils.getFullPath(file.getAbsolutePath()) + COOLEMALL_RESDESC_PREFIX + "_" + file.getName());
83                                env = Environment.unmarshal(new FileReader(dcwormsFile));
84                        } catch (Exception e2){
85                                PLMXMLTranslator plmxmlTranslator = new PLMXMLTranslator();
86                                String trasnlatedFileName = plmxmlTranslator.translate(new String[]{file.getAbsolutePath()});
87                                File dcwormsFile = new File(trasnlatedFileName);                       
88                                //File dcwormsFile = new File(FilenameUtils.getFullPath(file.getAbsolutePath()) + COOLEMALL_RESDESC_PREFIX + "_" + file.getName());
89
90                                env = Environment.unmarshal(new FileReader(dcwormsFile));
91                        }
92                }
93               
94                if(env.getResources().getMode().equals("CoolEmAll")){
95                        DataCenterWorkloadSimulator.MODE = "CoolEmAll";
96                        resFactory = new CoolEmAllResourceFactory();
97                }
98                System.out.println("started creating environment description");
99                List<ComputingResourceDescription> mainCompResDescList = createEnvironmentDescription(env);
100                System.out.println("finished creating environment description");
101
102                System.out.println("started creating resources");
103                List<ComputingResource> computingResources = createResources(mainCompResDescList);
104                System.out.println("finished creating resource");
105
106                System.out.println("started creating scheduling entities");
107                Scheduler mainScheduler = createSchedulers(env.getResources().getScheduler(), computingResources);
108                System.out.println("finished creating scheduling entities");
109
110                SimulatedEnvironment simEnv = new SimulatedEnvironment(mainScheduler, computingResources);
111                Collections.sort(toInit, new ResourceTypeComparator(new ArrayList<String>(compResLayers)));
112                simEnv.setInitList(toInit);
113                simEnv.setCompResLayers(compResLayers);
114                return simEnv;
115        }
116
117        protected List<ComputingResourceDescription> createEnvironmentDescription(Environment environment) throws Exception {
118
119                List<ComputingResourceDescription> mainCompResDescList = new ArrayList<ComputingResourceDescription>();
120               
121                LinkedList<ComputingResourceDescription> resDescStructure = new LinkedList<ComputingResourceDescription>();
122                LinkedList<schemas.ComputingResource> toExamine = new LinkedList<schemas.ComputingResource>();
123                EnvironmentWrapper environmentWrapper = new EnvironmentWrapper();
124                environmentWrapper.wrap(environment);
125               
126                String execTimeEstimationPluginClassName = null;
127                if(environmentWrapper.getTimeEstimationPlugin() != null){
128                        execTimeEstimationPluginClassName = environmentWrapper.getTimeEstimationPlugin().getName();
129                       
130                } else {
131                        execTimeEstimationPluginClassName = "example.timeestimation.DefaultTimeEstimationPlugin";
132                }
133               
134                execTimeEstimationPlugin = (ExecutionTimeEstimationPlugin) InstanceFactory.createInstance(
135                                execTimeEstimationPluginClassName, ExecutionTimeEstimationPlugin.class);
136                if(execTimeEstimationPlugin != null) {
137                        if(environmentWrapper.getTimeEstimationPlugin() != null){
138                                Parameters params = EnvironmentWrapper.extractParameters(environmentWrapper.getTimeEstimationPlugin().getParameter());
139                                execTimeEstimationPlugin.init(params);
140                        }
141                }
142                else {
143                        throw new Exception("Can not create execution time estimation plugin instance.");
144                }
145
146
147                if(environmentWrapper.getComputingResources() != null) {
148                        for(int i = 0; i < environmentWrapper.getComputingResources().length; i++){
149                                schemas.ComputingResource compResDef = environmentWrapper.getComputingResources()[i];
150                                toExamine.push(compResDef);
151                               
152                                ComputingResourceDescription compResDesc = new ComputingResourceDescription(compResDef);
153                                resDescStructure.push(compResDesc);
154                                mainCompResDescList.add(compResDesc);
155                        }
156                }
157                while (!toExamine.isEmpty()) {
158                        schemas.ComputingResource parentCompResDef = toExamine.pop();
159                        ComputingResourceDescription parentExecResDesc = resDescStructure.pop();
160                        if(parentCompResDef.getComputingResourceTypeChoiceSequence() != null)
161                        {
162                                int computingResourceCount = parentCompResDef.getComputingResourceTypeChoiceSequence().getComputingResourceCount();
163                                for (int i = 0; i < computingResourceCount; i++) {
164
165                                        schemas.ComputingResource compResDef = parentCompResDef.getComputingResourceTypeChoiceSequence().getComputingResource(i);
166                                        if(compResDef == null)
167                                                continue;
168                                        long compResCount = compResDef.getCount() > 1 ? compResDef.getCount() : 1;
169                                       
170                                        for(int j = 0; j < compResCount; j++){
171                                                if(compResDef.getComputingResourceTypeChoiceSequence2() != null){
172                                                        String templateId = compResDef.getComputingResourceTypeChoiceSequence2().getTemplateId();
173                                                        schemas.ComputingResourceTemplate template = environmentWrapper.getTemplate(templateId);
174                                                        environmentWrapper.initWithCompResTemplate(compResDef, template);
175                                                }
176                                                //toExamine.insertElementAt(compResDef, 0);
177                                                toExamine.addLast(compResDef);
178                                                ComputingResourceDescription resDesc = new ComputingResourceDescription(compResDef);
179                                                parentExecResDesc.addChildren(resDesc);
180                                                //resDescStructure.insertElementAt(resDesc, 0);
181                                                resDescStructure.addLast(resDesc);
182                                        }
183                                }
184                        } else
185                                continue;
186                }
187                return mainCompResDescList;
188        }
189
190        protected List<ComputingResource> createResources(List<ComputingResourceDescription> mainCompResDesList) {
191
192                List<ComputingResource> mainCompResourceList = new ArrayList<ComputingResource>();
193                Deque<ComputingResourceDescription> toExamine = new ArrayDeque<ComputingResourceDescription>();
194                Deque<ComputingResource> resStructure = new ArrayDeque<ComputingResource>();
195               
196                for(ComputingResourceDescription mainExecResDes : mainCompResDesList){
197                        ComputingResource mainResource = resFactory.createComputingResource(mainExecResDes);
198                        toExamine.push(mainExecResDes);
199                        resStructure.push(mainResource);
200                        mainCompResourceList.add(mainResource);
201                }
202
203                while (!toExamine.isEmpty()) {
204                        ComputingResourceDescription parentResDesc = toExamine.pop();
205                        ComputingResource parentResource = resStructure.pop();
206                        toInit.add(parentResource);
207                        compResLayers.add(parentResource.getType().getName());
208                        List<ResourceDescription> childrenResDesc = parentResDesc.getChildren();
209                        if (childrenResDesc == null){
210                                continue;
211                        }
212                        int compResCount = childrenResDesc.size();
213                        for (int i = 0; i < compResCount; i++) {
214                                ComputingResourceDescription compResDesc = (ComputingResourceDescription) childrenResDesc.get(i);
215                                toExamine.push(compResDesc);
216                                ComputingResource resource = resFactory.createComputingResource(compResDesc);
217                                parentResource.addChild(resource);
218                                resStructure.push(resource);
219                        }
220                }
221                return mainCompResourceList;
222        }
223
224        protected Scheduler createSchedulers(schemas.Scheduler[] schedulersDef, List<ComputingResource> mainCompResourceList) throws Exception{
225
226                List<Scheduler> mainSchedulers = new ArrayList<Scheduler>();
227                       
228                Deque<schemas.Scheduler> toExamine = new ArrayDeque<schemas.Scheduler>();       
229                Deque<Scheduler> schedulersStructure = new ArrayDeque<Scheduler>();
230                for(int i = 0; i < schedulersDef.length; i++){
231                        schemas.Scheduler schedulerDef = schedulersDef[i];
232                        Scheduler mainScheduler = initScheduler(schedulerDef, mainCompResourceList);
233                        toExamine.push(schedulerDef);
234                        schedulersStructure.push(mainScheduler);
235                        mainSchedulers.add(mainScheduler);
236                }       
237       
238                while (!toExamine.isEmpty()) {
239                        schemas.Scheduler parentSchedulerDef = toExamine.pop();
240               
241                        Scheduler parentScheduler = null;
242                        if(!schedulersStructure.isEmpty())
243                                parentScheduler = schedulersStructure.pop();
244                       
245                        if(parentSchedulerDef.getSchedulerTypeChoice() == null)
246                                continue;
247                        int schedulerChoiceItemCount = parentSchedulerDef.getSchedulerTypeChoice().getSchedulerTypeChoiceItemCount();
248                        for (int i = 0; i < schedulerChoiceItemCount; i++) {
249                                schemas.Scheduler schedulerDef = parentSchedulerDef.getSchedulerTypeChoice().getSchedulerTypeChoiceItem(i).getScheduler();
250                                if(schedulerDef == null)
251                                        continue;
252                                else
253                                {
254                                        Scheduler scheduler = initScheduler(schedulerDef, mainCompResourceList);
255                                        schedulersStructure.push(scheduler);
256                                        parentScheduler.addChild(scheduler);
257                                        toExamine.push(schedulerDef);
258                                }
259                        }
260                        //necessary if children list isn't initialized in Scheduler
261                        //parentScheduler.init();
262                }
263
264                //TODO - refactor (remove - create scheduler on the basis of resource description)
265                Scheduler mainScheduler = null;
266                if(mainSchedulers.size() == 1 /*&& mainSchedulers.get(0).get_name().equals("grid")*/){
267                        mainScheduler = mainSchedulers.get(0);
268                }
269                else{
270                        throw new Exception("Worng schedule definition");
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 = resFactory.createScheduler(StandardResourceType.GS, "grid", schedulingPlugin, execTimeEstimationPlugin, queues, managedResources);
337                } else {
338                        scheduler = resFactory.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
378                                if(mainCompRes.getFullName().contains(resourceName))
379                                        computingResource = mainCompRes;
380                                else
381                                        computingResource = mainCompRes.getDescendantByName(resourceName);
382
383                                if(computingResource != null)
384                                {
385                                        if(include){
386                                                compResources.add(computingResource);
387                                        } else{
388                                                compResources.addAll(computingResource.getChildren());
389                                                resourceUnits = getSharedResourceUnits(computingResource);
390                                        }
391                                }
392                        }
393                }
394                return new ManagedResources(compResources, resourceUnits);
395        }
396       
397       
398        private Map<ResourceUnitName, List<ResourceUnit>> getSharedResourceUnits(ComputingResource compResources){
399                Map<ResourceUnitName, List<ResourceUnit>> resourceUnits = new HashMap<ResourceUnitName, List<ResourceUnit>>();
400                List<ResourceUnit> list;
401                boolean resourceNotVisited = true;
402                ComputingResource parent = compResources;
403                while(parent != null && resourceNotVisited){
404                        Map<ResourceUnitName, List<ResourceUnit>> resUnits = parent.getResourceCharacteristic().getResourceUnits();
405                        for(ResourceUnitName run : resUnits.keySet()){
406                                for(ResourceUnit resUnit : resUnits.get(run)){
407                                        if((resourceUnits.get(run) == null)){
408                                                list = new ArrayList<ResourceUnit>(1);
409                                                resourceUnits.put(resUnit.getName(), list);
410                                                list.add(resUnit);
411                                        } else if(!resourceUnits.get(run).contains(resUnit)){
412                                                list = resourceUnits.get(resUnit.getName());
413                                                list.add(resUnit);
414                                        } else {
415                                                resourceNotVisited = false;
416                                        }
417                                }
418                        }
419                        parent = parent.getParent();
420                }
421                return resourceUnits;
422        }
423
424       
425        class ResourceTypeComparator implements Comparator<Initializable>{
426               
427                List<String> order;
428                ResourceTypeComparator(List<String> order) {
429                        this.order = order;
430                }
431                public int compare(Initializable init1, Initializable init2) {
432                        String type1 = ((Resource)init1).getType().getName();
433                        String type2 = ((Resource)init2).getType().getName();
434                        if(order.indexOf(type1) > order.indexOf(type2))
435                                return -1;
436                        else if (order.indexOf(type1) < order.indexOf(type2))
437                                return 1;
438                        else return 0;
439                       
440                }
441        }
442       
443}
Note: See TracBrowser for help on using the repository browser.