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

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