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

Revision 1464, 17.5 KB checked in by wojtekp, 10 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                                long compResCount = compResDef.getCount() > 1 ? compResDef.getCount() : 1;
151                                for(int j = 0; j < compResCount; j++){
152                                        toExamine.push(compResDef);
153                                       
154                                        ComputingResourceDescription compResDesc = new ComputingResourceDescription(compResDef);
155                                        resDescStructure.push(compResDesc);
156                                        mainCompResDescList.add(compResDesc);
157                                }
158                        }
159                }
160                while (!toExamine.isEmpty()) {
161                        schemas.ComputingResource parentCompResDef = toExamine.pop();
162                        ComputingResourceDescription parentExecResDesc = resDescStructure.pop();
163                        if(parentCompResDef.getComputingResourceTypeChoiceSequence() != null)
164                        {
165                                int computingResourceCount = parentCompResDef.getComputingResourceTypeChoiceSequence().getComputingResourceCount();
166                                for (int i = 0; i < computingResourceCount; i++) {
167
168                                        schemas.ComputingResource compResDef = parentCompResDef.getComputingResourceTypeChoiceSequence().getComputingResource(i);
169                                        if(compResDef == null)
170                                                continue;
171                                        long compResCount = compResDef.getCount() > 1 ? compResDef.getCount() : 1;
172                                       
173                                        for(int j = 0; j < compResCount; j++){
174                                                if(compResDef.getComputingResourceTypeChoiceSequence2() != null){
175                                                        String templateId = compResDef.getComputingResourceTypeChoiceSequence2().getTemplateId();
176                                                        schemas.ComputingResourceTemplate template = environmentWrapper.getTemplate(templateId);
177                                                        environmentWrapper.initWithCompResTemplate(compResDef, template);
178                                                }
179                                                //toExamine.insertElementAt(compResDef, 0);
180                                                toExamine.addLast(compResDef);
181                                                ComputingResourceDescription resDesc = new ComputingResourceDescription(compResDef);
182                                                parentExecResDesc.addChildren(resDesc);
183                                                //resDescStructure.insertElementAt(resDesc, 0);
184                                                resDescStructure.addLast(resDesc);
185                                        }
186                                }
187                        } else
188                                continue;
189                }
190                return mainCompResDescList;
191        }
192
193        protected List<ComputingResource> createResources(List<ComputingResourceDescription> mainCompResDesList) {
194
195                List<ComputingResource> mainCompResourceList = new ArrayList<ComputingResource>();
196                Deque<ComputingResourceDescription> toExamine = new ArrayDeque<ComputingResourceDescription>();
197                Deque<ComputingResource> resStructure = new ArrayDeque<ComputingResource>();
198               
199                for(ComputingResourceDescription mainExecResDes : mainCompResDesList){
200                        ComputingResource mainResource = resFactory.createComputingResource(mainExecResDes);
201                        toExamine.push(mainExecResDes);
202                        resStructure.push(mainResource);
203                        mainCompResourceList.add(mainResource);
204                }
205
206                while (!toExamine.isEmpty()) {
207                        ComputingResourceDescription parentResDesc = toExamine.pop();
208                        ComputingResource parentResource = resStructure.pop();
209                        toInit.add(parentResource);
210                        compResLayers.add(parentResource.getType().getName());
211                        List<ResourceDescription> childrenResDesc = parentResDesc.getChildren();
212                        if (childrenResDesc == null){
213                                continue;
214                        }
215                        int compResCount = childrenResDesc.size();
216                        for (int i = 0; i < compResCount; i++) {
217                                ComputingResourceDescription compResDesc = (ComputingResourceDescription) childrenResDesc.get(i);
218                                toExamine.push(compResDesc);
219                                ComputingResource resource = resFactory.createComputingResource(compResDesc);
220                                parentResource.addChild(resource);
221                                resStructure.push(resource);
222                        }
223                }
224                return mainCompResourceList;
225        }
226
227        protected Scheduler createSchedulers(schemas.Scheduler[] schedulersDef, List<ComputingResource> mainCompResourceList) throws Exception{
228
229                List<Scheduler> mainSchedulers = new ArrayList<Scheduler>();
230                       
231                Deque<schemas.Scheduler> toExamine = new ArrayDeque<schemas.Scheduler>();       
232                Deque<Scheduler> schedulersStructure = new ArrayDeque<Scheduler>();
233                for(int i = 0; i < schedulersDef.length; i++){
234                        schemas.Scheduler schedulerDef = schedulersDef[i];
235                        Scheduler mainScheduler = initScheduler(schedulerDef, mainCompResourceList);
236                        toExamine.push(schedulerDef);
237                        schedulersStructure.push(mainScheduler);
238                        mainSchedulers.add(mainScheduler);
239                }       
240       
241                while (!toExamine.isEmpty()) {
242                        schemas.Scheduler parentSchedulerDef = toExamine.pop();
243               
244                        Scheduler parentScheduler = null;
245                        if(!schedulersStructure.isEmpty())
246                                parentScheduler = schedulersStructure.pop();
247                       
248                        if(parentSchedulerDef.getSchedulerTypeChoice() == null)
249                                continue;
250                        int schedulerChoiceItemCount = parentSchedulerDef.getSchedulerTypeChoice().getSchedulerTypeChoiceItemCount();
251                        for (int i = 0; i < schedulerChoiceItemCount; i++) {
252                                schemas.Scheduler schedulerDef = parentSchedulerDef.getSchedulerTypeChoice().getSchedulerTypeChoiceItem(i).getScheduler();
253                                if(schedulerDef == null)
254                                        continue;
255                                else
256                                {
257                                        Scheduler scheduler = initScheduler(schedulerDef, mainCompResourceList);
258                                        schedulersStructure.push(scheduler);
259                                        parentScheduler.addChild(scheduler);
260                                        toExamine.push(schedulerDef);
261                                }
262                        }
263                        //necessary if children list isn't initialized in Scheduler
264                        //parentScheduler.init();
265                }
266
267                //TODO - refactor (remove - create scheduler on the basis of resource description)
268                Scheduler mainScheduler = null;
269                if(mainSchedulers.size() == 1 /*&& mainSchedulers.get(0).get_name().equals("grid")*/){
270                        mainScheduler = mainSchedulers.get(0);
271                }
272                else{
273                        throw new Exception("Worng schedule definition");
274                }
275                return mainScheduler;
276        }
277
278        protected Scheduler initScheduler(schemas.Scheduler schedulerDef, List<ComputingResource> mainCompResourceList) throws Exception{
279                Scheduler scheduler = null;
280               
281                ManagedResources managedResources = null;
282                //List<ComputingResource> managedCompResources = new ArrayList<ComputingResource>();
283                TaskQueueList queues = new TaskQueueList(1);
284               
285                if(schedulerDef.getQueues()!= null){
286                        int queueCount = schedulerDef.getQueues().getQueueCount();
287                        for(int i = 0; i < queueCount; i++){
288                                schemas.QueueType queueDef = schedulerDef.getQueues().getQueue(i);
289                                TaskQueue queue = new TaskQueue(queueDef.getReservation());
290                                queue.setName(queueDef.getName());
291                                queue.setPriority(queueDef.getPriority());
292                                queues.add(queue);
293                        }
294                } else {
295                        TaskQueue queue = new TaskQueue(false);
296                        queues.add(queue);
297                }
298               
299                if(schedulerDef.getSchedulerTypeChoice() != null) {
300                        int schedulerChoiceItemCount = schedulerDef.getSchedulerTypeChoice().getSchedulerTypeChoiceItemCount();
301                        for (int i = 0; i < schedulerChoiceItemCount; i++) {
302                                ManagedComputingResources mcr = schedulerDef.getSchedulerTypeChoice().getSchedulerTypeChoiceItem(i).getManagedComputingResources();
303                                if(mcr == null)
304                                        continue;
305
306                                List<String> managedCompResourcesIds = new ArrayList<String>();
307                                int resourceNameCount = mcr.getResourceNameCount();
308                                for(int j = 0; j < resourceNameCount; j++) {
309                                        managedCompResourcesIds.add(mcr.getResourceName(j));
310                                }
311                                managedResources = matchResourcesForScheduler(mainCompResourceList, managedCompResourcesIds, mcr.getInclude());
312                                //managedResources = new ManagedResources(managedCompResources, new HashMap<ResourceUnitName, List<ResourceUnit>>());
313                        }
314                }
315
316                SchedulingPlugin schedulingPlugin = null;
317                if(schedulerDef.getSchedulingPlugin() != null){
318                        String schedulingPluginName = schedulerDef.getSchedulingPlugin().getName();
319                        schedulingPlugin = (SchedulingPlugin) InstanceFactory.createInstance(schedulingPluginName,
320                                        SchedulingPlugin.class);
321                        Parameters params = EnvironmentWrapper.extractParameters(schedulerDef.getSchedulingPlugin().getParameter());
322                        if(schedulerDef.getSchedulingPlugin().getFrequency() != null)
323                        {
324                                Parameter param = new Parameter("frequency");
325                                StringValueWithUnit sv = new StringValueWithUnit();
326                                sv.setContent(String.valueOf(schedulerDef.getSchedulingPlugin().getFrequency().getContent()));
327                                sv.setUnit(schedulerDef.getSchedulingPlugin().getFrequency().getUnit());
328                                param.add(sv);
329                               
330                                if(params == null)
331                                        params = new Parameters(2);
332                                params.put("frequency", param);
333                        }
334                        schedulingPlugin.init(params);
335                }       
336
337                //TODO - refactor (create scheduler in 1 line)
338                if(schedulerDef.getClazz().equals("GridBroker")){
339                        scheduler = resFactory.createScheduler(StandardResourceType.GS, "grid", schedulingPlugin, execTimeEstimationPlugin, queues, managedResources);
340                } else {
341                        scheduler = resFactory.createScheduler(StandardResourceType.LS, schedulerDef.getName(), schedulingPlugin, execTimeEstimationPlugin,  queues, managedResources);
342                }
343                return scheduler;
344        }
345       
346        /*private List<ComputingResource> matchCompResourcesForScheduler(List<ComputingResource> mainCompResourceList, List<String> resIdList, boolean include){
347                List<ComputingResource> compResources = new ArrayList<ComputingResource>();
348                for(ComputingResource mainCompRes: mainCompResourceList){
349                        for(String resourceName : resIdList){
350                                ComputingResource computingResource;
351                                try {
352                                        if(resourceName.equals(mainCompRes.getName()))
353                                                computingResource = mainCompRes;
354                                        else
355                                                computingResource = mainCompRes.getDescendantByName(resourceName);
356                                } catch (ResourceException e) {
357                                        computingResource = null;
358                                }
359                                if(computingResource != null)
360                                {
361                                        if(include){
362                                                compResources.add(computingResource);
363                                        } else{
364                                                compResources.addAll(computingResource.getChildren());
365                                        }
366                                }
367                        }
368                }
369                return compResources;
370        }*/
371       
372        private ManagedResources matchResourcesForScheduler(List<ComputingResource> mainCompResourceList, List<String> resIdList, boolean include){
373               
374                List<ComputingResource> compResources = new ArrayList<ComputingResource>();
375                Map<ResourceUnitName, List<ResourceUnit>> resourceUnits = new HashMap<ResourceUnitName, List<ResourceUnit>>();
376               
377                for(ComputingResource mainCompRes: mainCompResourceList){
378                        for(String resourceName : resIdList){
379                                ComputingResource computingResource;
380
381                                if(mainCompRes.getFullName().contains(resourceName))
382                                        computingResource = mainCompRes;
383                                else
384                                        computingResource = mainCompRes.getDescendantByName(resourceName);
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.