source: xssim/trunk/src/test/rewolucja/scheduling/JobRegistry.java @ 104

Revision 104, 8.6 KB checked in by wojtekp, 13 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package test.rewolucja.scheduling;
2
3import gridsim.Gridlet;
4import gridsim.gssim.GssimConstants;
5import gridsim.gssim.ResourceHistoryItem;
6import gridsim.gssim.SubmittedTask;
7import gssim.schedframe.scheduling.AbstractExecutable;
8import gssim.schedframe.scheduling.ExecTaskInterface;
9import gssim.schedframe.scheduling.Executable;
10import gssim.schedframe.scheduling.queues.TaskQueue;
11
12import java.util.ArrayList;
13import java.util.Collections;
14import java.util.HashMap;
15import java.util.List;
16import java.util.Map;
17
18import org.apache.commons.logging.Log;
19import org.apache.commons.logging.LogFactory;
20import org.joda.time.DateTime;
21
22import schedframe.resources.units.ResourceUnit;
23import schedframe.scheduling.AbstractProcesses;
24import schedframe.scheduling.Job;
25import schedframe.scheduling.JobInterface;
26import schedframe.scheduling.Task;
27import schedframe.scheduling.TaskInterface;
28import schedframe.scheduling.utils.ResourceParameterName;
29import test.rewolucja.resources.ProcessingElements;
30import test.rewolucja.resources.physical.base.ComputingResource;
31import test.rewolucja.scheduling.plan.AllocationInterfaceNew;
32
33public class JobRegistry extends AbstractJobRegistry /*implements Cloneable*/ {
34       
35        private static final long serialVersionUID = 8030555906990767342L;
36
37        private static Log log = LogFactory.getLog(JobRegistry.class);
38
39        private String context;
40
41        protected static final List<ExecTaskInterface> submittedTasks = Collections.synchronizedList(new ArrayList<ExecTaskInterface>());;
42        //protected static final List<ExecTaskInterface> submittedTasks = new CopyOnWriteArrayList<ExecTaskInterface>();
43
44        public JobRegistry(String context_) {
45                context = context_;
46        }
47
48        /*protected void setContext(String context_) {
49                context = context_;
50        }*/
51
52        public boolean addTask(ExecTaskInterface newTask) {
53                if(getSubmittedTask(newTask.getJobId(), newTask.getId()) == null)
54                {
55                        synchronized (submittedTasks)  {
56                                submittedTasks.add(newTask);
57                        }
58                        return true;
59                }
60                return false;
61        }
62
63        public List<ExecTaskInterface> getTasks(int status) {
64                List<ExecTaskInterface> taskList = new ArrayList<ExecTaskInterface>();
65                synchronized (submittedTasks)  {
66                        for (ExecTaskInterface task : submittedTasks) {
67                                if (task.getStatus() == status) {
68                                        SubmittedTask subTask = (SubmittedTask) task;
69                                        if (subTask.getResPath().contains(context)) {
70                                                taskList.add(subTask);
71                                        }
72                                }
73                        }
74                }
75                return taskList;
76        }
77
78        public List<ExecTaskInterface> getQueuedTasks() {
79                return getTasks(Gridlet.QUEUED);
80        }
81       
82        public List<ExecTaskInterface> getRunningTasks() {
83                return getTasks(Gridlet.INEXEC);
84        }
85       
86        public List<ExecTaskInterface> getReadyTasks() {
87                return getTasks(Gridlet.READY);
88        }
89       
90        public List<ExecTaskInterface> getFinishedTasks() {
91                return getTasks(Gridlet.SUCCESS);
92        }
93       
94       
95        public List<ExecTaskInterface> getAllSubmittedTasks() {
96                List<ExecTaskInterface> taskList;;
97                synchronized (submittedTasks)  {
98                        taskList = new ArrayList<ExecTaskInterface>(submittedTasks);
99                }
100                return taskList;
101        }
102
103        public List<SubmittedTask> getSubmittedTasks() {
104                List<SubmittedTask> taskList = new ArrayList<SubmittedTask>();
105                synchronized (submittedTasks)  {
106                        for (ExecTaskInterface task : submittedTasks) {
107                                SubmittedTask subTask = (SubmittedTask) task;
108                                if (subTask.getResPath().contains(context)) {
109                                        taskList.add(subTask);
110                                }
111                        }
112                }
113                return taskList;
114        }
115       
116        public SubmittedTask getSubmittedTask(String jobId, String taskId){
117                synchronized (submittedTasks)  {
118                        for (ExecTaskInterface task : submittedTasks) {
119                                if (task.getJobId().compareTo(jobId) == 0 && task.getId().compareTo(taskId)==0) {
120                                        return (SubmittedTask)task;
121                                }
122                        }
123                }
124                return null;
125        }
126       
127        public List<TaskInterface<?>> getReadyTasks(List<JobInterface<?>> jobsList) {
128                List<TaskInterface<?>> tasks = new ArrayList<TaskInterface<?>>();
129                TaskQueue waitingTasks = new TaskQueue();
130               
131                for(int i = 0; i < jobsList.size(); i++){
132                        Job newJob = (Job)jobsList.get(i);
133                        waitingTasks.addAll(newJob.getTask());
134                }
135
136                tasks.addAll(waitingTasks.getReadyTasks(this));
137                return tasks;
138        }
139       
140
141
142        public AbstractExecutable getTaskExecutable(Integer executableId){
143                synchronized (submittedTasks)  {
144                        for (ExecTaskInterface task : submittedTasks) {
145                                SubmittedTask subTask = (SubmittedTask) task;
146                                AbstractExecutable exec = (AbstractExecutable)subTask.getGridlet();
147                                if (exec.getGridletID() == executableId) {
148                                        return exec;
149                                }
150                        }
151                }
152                return null;
153        }
154        public List<AbstractExecutable> getJobExecutables(String jobId){
155                List<AbstractExecutable> list = new ArrayList<AbstractExecutable>();
156                synchronized (submittedTasks)  {
157                        for(int i = 0; i < submittedTasks.size(); i++){
158                                SubmittedTask subTask = (SubmittedTask) submittedTasks.get(i);
159                                AbstractExecutable exec = (AbstractExecutable)subTask.getGridlet();
160                               
161                                if(exec.getJobId().equals(jobId))
162                                        list.add(exec);
163                        }
164                }
165               
166                return list;
167        }
168       
169        /*public AbstractExecutable getTaskExecutabls(String jobId, String taskId){
170                List<AbstractExecutable> list = new ArrayList<AbstractExecutable>();
171                synchronized (submittedTasks)  {
172                        for(int i = 0; i < size(); i++){
173                                SubmittedTask subTask = (SubmittedTask) submittedTasks.get(i);
174                                AbstractExecutable exec = (AbstractExecutable)subTask.getGridlet();
175                               
176                                if(exec.getJobId().equals(jobId) && exec.getId().equals(taskId))
177                                        return exec;
178                        }
179                }
180                return null;
181        }*/
182       
183       
184        public Executable createExecutable(Task task, AllocationInterfaceNew allocation) {
185
186                String refersTo = allocation.getProcessGroupId(); // null;//allocation.getRefersTo();
187                if(refersTo == null)
188                        refersTo = task.getId();
189                       
190                Executable exec = null;
191
192                if(refersTo.equals(task.getId())){
193                        exec = new Executable(task);
194                } else {
195                        List<AbstractProcesses> processes = task.getProcesses();
196                        if(processes == null) {
197                                try {
198                                        log.error("Allocation: " + allocation.getDocument() + "\nrefers to unknown task or processes set." +
199                                                        " Set correct value (task id or prcesses set id) for allocation refersTo attribute.");
200                                } catch (Exception e) {
201                                        e.printStackTrace();
202                                }
203                        }
204                        boolean found = false;
205                        for(int j = 0; j < processes.size() && !found; j++){
206                                AbstractProcesses procesesSet = processes.get(j);
207                                if(refersTo.equals(procesesSet.getId())){
208                                        exec = new Executable(task, procesesSet);
209                                        found = true;
210                                }
211                        }
212                        if(!found){
213                                log.error("Allocation refers to unknown proceses set.");
214                        }
215                               
216                }
217               
218                exec.setUserID(task.getSenderId());
219                exec.setLength(task.getLength());
220                exec.setReservationId(allocation.getReservationId());
221                       
222                /*HostInterface<?> host = allocation.getHost();
223                ComputingResourceTypeInterface<?> crt = host.getMachineParameters();
224                if(crt != null){
225                        ComputingResourceTypeItemInterface<?> crti = crt.getComputingResourceTypeItem(0);
226                        if(crti != null){
227                                ParameterPropertyInterface<?> properties[] = crti.getHostParameter().getProperty();
228                                for(int p = 0; p < properties.length; p++){
229                                        ParameterPropertyInterface<?> property = properties[p];
230                                        if("chosenCPUs".equals(property.getName())){
231                                                Object cpuNames = property.getValue();
232                                                exec.addSpecificResource(ResourceParameterName.FREECPUS, cpuNames);
233                                        }
234                                }
235                        }
236                }*/
237                return exec;
238        }
239
240
241        /**************************************/
242        protected static Map<Integer, Map<String, Object>> history = new HashMap<Integer, Map<String,Object>>();
243       
244        public static Map<Integer, Map<String, Object>> getAllocationHistory(){
245                return history;
246        }
247       
248        public void saveHistory (SubmittedTask submittedTask, int estimatedTime, Map<ResourceParameterName, ResourceUnit> choosenResources){
249               
250                submittedTask.setEstimatedDuration(estimatedTime);
251
252                DateTime currentTime = new DateTime();
253                ResourceHistoryItem resHistItem = new ResourceHistoryItem(choosenResources, currentTime);
254                submittedTask.addUsedResources(resHistItem);
255
256                Map<String, Object> historyItem = new HashMap<String, Object>();
257                List<ResourceHistoryItem> list = new ArrayList<ResourceHistoryItem>(1);
258                list.add(resHistItem);
259                historyItem.put(GssimConstants.RESOURCES, list);
260                historyItem.put(GssimConstants.START_TIME, currentTime);
261                currentTime = currentTime.plusSeconds(estimatedTime);
262                historyItem.put(GssimConstants.END_TIME, currentTime);
263
264                submittedTask.setFinishTime(currentTime.getMillis() / 1000);
265               
266                history.put(Integer.valueOf(submittedTask.getGridletID()), historyItem);
267                ProcessingElements pes = (ProcessingElements) choosenResources.get(ResourceParameterName.PROCESSINGELEMENTS);
268                for (ComputingResource resource : pes) {
269                        submittedTask.addToResPath(resource.getName());
270                        ComputingResource parent = resource.getParent();
271                        while (parent != null && !submittedTask.getResPath().contains(parent.getName() + "_")) {
272                                submittedTask.addToResPath(parent.getName());
273                                parent = parent.getParent();
274                        }
275                }
276        }
277
278
279}
Note: See TracBrowser for help on using the repository browser.