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

Revision 168, 9.2 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        //TO DO - change data structure
42        protected static final List<ExecTaskInterface> submittedTasks = Collections.synchronizedList(new ArrayList<ExecTaskInterface>());;
43        //protected static final List<ExecTaskInterface> submittedTasks = new CopyOnWriteArrayList<ExecTaskInterface>();
44
45        public JobRegistry(String context_) {
46                context = context_;
47        }
48
49        /*protected void setContext(String context_) {
50                context = context_;
51        }*/
52
53        public boolean addTask(ExecTaskInterface newTask) {
54                if(getSubmittedTask(newTask.getJobId(), newTask.getId()) == null)
55                {
56                        synchronized (submittedTasks)  {
57                                submittedTasks.add(newTask);
58                        }
59                        return true;
60                }
61                return false;
62        }
63
64        public List<ExecTaskInterface> getTasks(int status) {
65                List<ExecTaskInterface> taskList = new ArrayList<ExecTaskInterface>();
66                synchronized (submittedTasks)  {
67                        for (ExecTaskInterface task : submittedTasks) {
68                                if (task.getStatus() == status) {
69                                        SubmittedTask subTask = (SubmittedTask) task;
70                                        if(subTask.getVisitedResources().contains(context)){
71                                                taskList.add(subTask);
72                                        }
73                                        /*if (subTask.getResPath().contains(context)) {
74                                                taskList.add(subTask);
75                                        }*/
76                                }
77                        }
78                }
79                return taskList;
80        }
81
82        public List<ExecTaskInterface> getQueuedTasks() {
83                return getTasks(Gridlet.QUEUED);
84        }
85       
86        public List<ExecTaskInterface> getRunningTasks() {
87                return getTasks(Gridlet.INEXEC);
88        }
89       
90        public List<ExecTaskInterface> getReadyTasks() {
91                return getTasks(Gridlet.READY);
92        }
93       
94        public List<ExecTaskInterface> getFinishedTasks() {
95                return getTasks(Gridlet.SUCCESS);
96        }
97       
98       
99        public List<ExecTaskInterface> getAllSubmittedTasks() {
100                List<ExecTaskInterface> taskList;;
101                synchronized (submittedTasks)  {
102                        taskList = new ArrayList<ExecTaskInterface>(submittedTasks);
103                }
104                return taskList;
105        }
106
107        public List<SubmittedTask> getSubmittedTasks() {
108                List<SubmittedTask> taskList = new ArrayList<SubmittedTask>();
109                synchronized (submittedTasks)  {
110                        for (ExecTaskInterface task : submittedTasks) {
111                                SubmittedTask subTask = (SubmittedTask) task;
112                                if(subTask.getVisitedResources().contains(context)){
113                                        taskList.add(subTask);
114                                }
115                                /*if (subTask.getResPath().contains(context)) {
116                                        taskList.add(subTask);
117                                }*/
118                        }
119                }
120                return taskList;
121        }
122       
123        public SubmittedTask getSubmittedTask(String jobId, String taskId){
124                synchronized (submittedTasks)  {
125                        for (ExecTaskInterface task : submittedTasks) {
126                                if (task.getJobId().compareTo(jobId) == 0 && task.getId().compareTo(taskId)==0) {
127                                        return (SubmittedTask)task;
128                                }
129                        }
130                }
131                return null;
132        }
133       
134        public List<TaskInterface<?>> getReadyTasks(List<JobInterface<?>> jobsList) {
135                List<TaskInterface<?>> tasks = new ArrayList<TaskInterface<?>>();
136                TaskQueue waitingTasks = new TaskQueue();
137               
138                for(int i = 0; i < jobsList.size(); i++){
139                        Job newJob = (Job)jobsList.get(i);
140                        waitingTasks.addAll(newJob.getTask());
141                }
142
143                tasks.addAll(waitingTasks.getReadyTasks(this));
144                return tasks;
145        }
146       
147
148
149        public AbstractExecutable getTaskExecutable(Integer executableId){
150                synchronized (submittedTasks)  {
151                        for (ExecTaskInterface task : submittedTasks) {
152                                SubmittedTask subTask = (SubmittedTask) task;
153                                AbstractExecutable exec = (AbstractExecutable)subTask.getGridlet();
154                                if (exec.getGridletID() == executableId) {
155                                        return exec;
156                                }
157                        }
158                }
159                return null;
160        }
161        public List<AbstractExecutable> getJobExecutables(String jobId){
162                List<AbstractExecutable> list = new ArrayList<AbstractExecutable>();
163                synchronized (submittedTasks)  {
164                        for(int i = 0; i < submittedTasks.size(); i++){
165                                SubmittedTask subTask = (SubmittedTask) submittedTasks.get(i);
166                                AbstractExecutable exec = (AbstractExecutable)subTask.getGridlet();
167                               
168                                if(exec.getJobId().equals(jobId))
169                                        list.add(exec);
170                        }
171                }
172               
173                return list;
174        }
175       
176        /*public AbstractExecutable getTaskExecutabls(String jobId, String taskId){
177                List<AbstractExecutable> list = new ArrayList<AbstractExecutable>();
178                synchronized (submittedTasks)  {
179                        for(int i = 0; i < size(); i++){
180                                SubmittedTask subTask = (SubmittedTask) submittedTasks.get(i);
181                                AbstractExecutable exec = (AbstractExecutable)subTask.getGridlet();
182                               
183                                if(exec.getJobId().equals(jobId) && exec.getId().equals(taskId))
184                                        return exec;
185                        }
186                }
187                return null;
188        }*/
189       
190       
191        public Executable createExecutable(Task task, AllocationInterfaceNew allocation) {
192
193                String refersTo = allocation.getProcessGroupId(); // null;//allocation.getRefersTo();
194                if(refersTo == null)
195                        refersTo = task.getId();
196                       
197                Executable exec = null;
198
199                if(refersTo.equals(task.getId())){
200                        exec = new Executable(task);
201                } else {
202                        List<AbstractProcesses> processes = task.getProcesses();
203                        if(processes == null) {
204                                try {
205                                        log.error("Allocation: " + allocation.getDocument() + "\nrefers to unknown task or processes set." +
206                                                        " Set correct value (task id or prcesses set id) for allocation refersTo attribute.");
207                                } catch (Exception e) {
208                                        e.printStackTrace();
209                                }
210                        }
211                        boolean found = false;
212                        for(int j = 0; j < processes.size() && !found; j++){
213                                AbstractProcesses procesesSet = processes.get(j);
214                                if(refersTo.equals(procesesSet.getId())){
215                                        exec = new Executable(task, procesesSet);
216                                        found = true;
217                                }
218                        }
219                        if(!found){
220                                log.error("Allocation refers to unknown proceses set.");
221                        }
222                               
223                }
224               
225                exec.setUserID(task.getSenderId());
226                exec.setLength(task.getLength());
227                exec.setReservationId(allocation.getReservationId());
228                       
229                /*HostInterface<?> host = allocation.getHost();
230                ComputingResourceTypeInterface<?> crt = host.getMachineParameters();
231                if(crt != null){
232                        ComputingResourceTypeItemInterface<?> crti = crt.getComputingResourceTypeItem(0);
233                        if(crti != null){
234                                ParameterPropertyInterface<?> properties[] = crti.getHostParameter().getProperty();
235                                for(int p = 0; p < properties.length; p++){
236                                        ParameterPropertyInterface<?> property = properties[p];
237                                        if("chosenCPUs".equals(property.getName())){
238                                                Object cpuNames = property.getValue();
239                                                exec.addSpecificResource(ResourceParameterName.FREECPUS, cpuNames);
240                                        }
241                                }
242                        }
243                }*/
244                return exec;
245        }
246
247
248        /**************************************/
249        protected static Map<Integer, Map<String, Object>> history = new HashMap<Integer, Map<String,Object>>();
250       
251        public static Map<Integer, Map<String, Object>> getAllocationHistory(){
252                return history;
253        }
254       
255        public void saveHistory (SubmittedTask submittedTask, int estimatedTime, Map<ResourceParameterName, ResourceUnit> choosenResources){
256               
257        /*      submittedTask.setEstimatedDuration(estimatedTime);
258
259                DateTime currentTime = new DateTime();
260                ResourceHistoryItem resHistItem = new ResourceHistoryItem(choosenResources, currentTime);
261                submittedTask.addUsedResources(resHistItem);*/
262
263                ResourceHistoryItem resHistItem = submittedTask.getUsedResources().get(submittedTask.getUsedResources().size()-1);
264                DateTime currentTime = new DateTime();
265                Map<String, Object> historyItem = new HashMap<String, Object>();
266                List<ResourceHistoryItem> list = new ArrayList<ResourceHistoryItem>(1);
267                list.add(resHistItem);
268                historyItem.put(GssimConstants.RESOURCES, list);
269                historyItem.put(GssimConstants.START_TIME, currentTime);
270                currentTime = currentTime.plusSeconds(estimatedTime);
271                historyItem.put(GssimConstants.END_TIME, currentTime);
272
273                history.put(Integer.valueOf(submittedTask.getGridletID()), historyItem);
274                /*ProcessingElements pes = (ProcessingElements) choosenResources.get(ResourceParameterName.PROCESSINGELEMENTS);
275                for (ComputingResource resource : pes) {
276                        //submittedTask.addToResPath(resource.getName());
277                        submittedTask.visitResource(resource.getName());
278                        ComputingResource parent = resource.getParent();
279                        while (parent != null && !submittedTask.getResPath().contains(parent.getName() + "_")) {
280                                submittedTask.addToResPath(parent.getName());
281                                parent = parent.getParent();
282                        }
283                        while (parent != null && !submittedTask.getVisitedResources().contains(parent.getName() + "_")) {
284                                submittedTask.visitResource(parent.getName());
285                                parent = parent.getParent();
286                        }
287                }*/
288        }
289
290
291}
Note: See TracBrowser for help on using the repository browser.