1 | package simulator; |
---|
2 | |
---|
3 | import eduni.simjava.Sim_event; |
---|
4 | import eduni.simjava.Sim_system; |
---|
5 | import gridsim.GridSim; |
---|
6 | import gridsim.GridSimTags; |
---|
7 | import gridsim.Gridlet; |
---|
8 | import gridsim.IO_data; |
---|
9 | import gridsim.net.InfoPacket; |
---|
10 | import gssim.schedframe.scheduling.utils.JobDescription; |
---|
11 | import gssim.schedframe.scheduling.utils.TaskDescription; |
---|
12 | |
---|
13 | import java.util.ArrayList; |
---|
14 | import java.util.HashSet; |
---|
15 | import java.util.Iterator; |
---|
16 | import java.util.List; |
---|
17 | import java.util.Map; |
---|
18 | import java.util.Set; |
---|
19 | import java.util.TreeMap; |
---|
20 | |
---|
21 | import org.apache.commons.logging.Log; |
---|
22 | import org.apache.commons.logging.LogFactory; |
---|
23 | import org.joda.time.DateTime; |
---|
24 | |
---|
25 | import qcg.shared.constants.BrokerConstants; |
---|
26 | import schedframe.scheduling.tasks.Job; |
---|
27 | import schedframe.scheduling.tasks.JobInterface; |
---|
28 | import schedframe.scheduling.tasks.Task; |
---|
29 | import schedframe.scheduling.tasks.TaskInterface; |
---|
30 | import simulator.utils.XsltTransformations; |
---|
31 | import simulator.workload.WorkloadLoader; |
---|
32 | |
---|
33 | public class WormsUsers extends GridSim implements GenericUser { |
---|
34 | |
---|
35 | /**A job generator, which produces jobs and tasks. These jobs are then sent by this entity */ |
---|
36 | protected WorkloadLoader workloadLoader; |
---|
37 | |
---|
38 | /** The name of the entity, to which the created tasks will be sent */ |
---|
39 | protected String destName; |
---|
40 | |
---|
41 | /** Indicates, that all tasks that returned to this object are finished */ |
---|
42 | protected boolean allTasksAreFinished; |
---|
43 | |
---|
44 | /** Stores the list of jobs, that have been returned to this object */ |
---|
45 | protected List<JobInterface<?>> returnedJobs; |
---|
46 | protected Set<String> sendJobsIds; |
---|
47 | protected Set<String> returnedJobsIds; |
---|
48 | |
---|
49 | protected XsltTransformations xsltTransformer; |
---|
50 | |
---|
51 | /** |
---|
52 | * Indicates, that an error has occurred - it is used for debug purposes |
---|
53 | */ |
---|
54 | protected boolean error; |
---|
55 | |
---|
56 | private static Log log = LogFactory.getLog(WormsUsers.class); |
---|
57 | |
---|
58 | /** |
---|
59 | * Constructs the users object with the given parameters |
---|
60 | * @param name the name of the users entity (must be unique across all entities in the whole simulation) |
---|
61 | * @param destinationName the name of the entity, to which the created tasks will be sent |
---|
62 | * @param jobGenerator the job generator, which produces jobs and tasks, that will be sent by this class |
---|
63 | * @throws Exception if any occurs (see {@link GridSim#GridSim(String, double)}) |
---|
64 | */ |
---|
65 | public WormsUsers(String name, String destinationName, WorkloadLoader workload) throws Exception { |
---|
66 | super(name, WormsConstants.DEFAULT_BAUD_RATE); |
---|
67 | this.workloadLoader = workload; |
---|
68 | destName = destinationName; |
---|
69 | allTasksAreFinished = true; |
---|
70 | error = false; |
---|
71 | |
---|
72 | sendJobsIds = new HashSet<String>(); |
---|
73 | returnedJobsIds = new HashSet<String>(); |
---|
74 | |
---|
75 | this.xsltTransformer = new XsltTransformations(); |
---|
76 | } |
---|
77 | |
---|
78 | @Override |
---|
79 | public void body() { |
---|
80 | sendJobs(); |
---|
81 | collectJobs(); |
---|
82 | |
---|
83 | //GridSim dependent code for shutting down the simulation |
---|
84 | shutdownGridStatisticsEntity(); |
---|
85 | terminateIOEntities(); |
---|
86 | shutdownUserEntity(); |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * Collects the jobs sent to broker(s) |
---|
91 | */ |
---|
92 | protected void collectJobs() { |
---|
93 | final int FACTOR = Math.min(10, workloadLoader.getTaskCount()); //the refresh rate of the gauge: at most 10 times |
---|
94 | final int denominator = workloadLoader.getTaskCount() / FACTOR; |
---|
95 | allTasksAreFinished = true; |
---|
96 | returnedJobs = new ArrayList<JobInterface<?>>(); |
---|
97 | int counter = 0; |
---|
98 | int oldRemeinder = 0; |
---|
99 | boolean hundredPercent = false; |
---|
100 | |
---|
101 | Sim_event ev = new Sim_event(); |
---|
102 | sim_get_next(ev); |
---|
103 | while (Sim_system.running()) { |
---|
104 | switch (ev.get_tag()) { |
---|
105 | case GridSimTags.END_OF_SIMULATION: |
---|
106 | //no action |
---|
107 | break; |
---|
108 | |
---|
109 | case GridSimTags.INFOPKT_SUBMIT: |
---|
110 | processPingRequest(ev); |
---|
111 | break; |
---|
112 | |
---|
113 | case GridSimTags.GRIDLET_RETURN: |
---|
114 | JobInterface<?> returnedJob = (JobInterface<?>) ev.get_data(); |
---|
115 | |
---|
116 | String jobId = null; |
---|
117 | try { |
---|
118 | jobId = returnedJob.getId(); |
---|
119 | } catch (NoSuchFieldException e) { |
---|
120 | // TODO Auto-generated catch block |
---|
121 | e.printStackTrace(); |
---|
122 | } |
---|
123 | |
---|
124 | if (returnedJobs.contains(returnedJob)) { |
---|
125 | if(log.isErrorEnabled()) |
---|
126 | log.error("Received the same job twice (job ID " + jobId + ")"); |
---|
127 | error = true; |
---|
128 | break; |
---|
129 | } |
---|
130 | |
---|
131 | returnedJobs.add(returnedJob); |
---|
132 | returnedJobsIds.add(jobId); |
---|
133 | |
---|
134 | if(returnedJob.getStatus() == BrokerConstants.JOB_STATUS_FINISHED) { |
---|
135 | if(log.isDebugEnabled()) |
---|
136 | log.debug("Received finished job " + jobId); |
---|
137 | |
---|
138 | } else { |
---|
139 | if(returnedJob.getStatus() == BrokerConstants.JOB_STATUS_CANCELED) { |
---|
140 | if(log.isWarnEnabled()){ |
---|
141 | String str = "Warning! An uncomplished job (Job ID: "+jobId+") has returned to users. Job was canceled."; |
---|
142 | log.warn(str); |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | allTasksAreFinished = false; |
---|
147 | } |
---|
148 | |
---|
149 | counter += returnedJob.getTaskCount(); |
---|
150 | int remainder = (counter / denominator); |
---|
151 | if (remainder != oldRemeinder) { |
---|
152 | int gauge = ((counter * 100) / (workloadLoader.getTaskCount())); |
---|
153 | if(log.isInfoEnabled()) |
---|
154 | log.info(gauge + "% "); |
---|
155 | |
---|
156 | oldRemeinder = remainder; |
---|
157 | if (gauge == 100) |
---|
158 | hundredPercent = true; |
---|
159 | } |
---|
160 | |
---|
161 | break; |
---|
162 | } |
---|
163 | |
---|
164 | //if all the Gridlets have been collected |
---|
165 | if (counter == workloadLoader.getTaskCount()) { |
---|
166 | break; |
---|
167 | } |
---|
168 | |
---|
169 | sim_get_next(ev); |
---|
170 | } |
---|
171 | |
---|
172 | //if all the Gridlets have been collected |
---|
173 | if (counter == workloadLoader.getTaskCount()) { |
---|
174 | if (! hundredPercent) { |
---|
175 | if(log.isInfoEnabled()) |
---|
176 | log.info("100%"); |
---|
177 | } |
---|
178 | if(log.isInfoEnabled()) |
---|
179 | log.info(get_name() + ": Received all "+workloadLoader.getJobCount()+" jobs and " + counter + " tasks"); |
---|
180 | } else { |
---|
181 | if(log.isErrorEnabled()) |
---|
182 | log.error(get_name() + ": ERROR DID NOT RECEIVED all tasks - some tasks were not finished! (received "+counter+" of "+workloadLoader.getTaskCount()+")"); |
---|
183 | } |
---|
184 | |
---|
185 | Iterator <String> itr = sendJobsIds.iterator(); |
---|
186 | String jobId; |
---|
187 | if(log.isInfoEnabled()){ |
---|
188 | log.info("Missing tasks: "); |
---|
189 | while(itr.hasNext()){ |
---|
190 | jobId = itr.next(); |
---|
191 | if(!returnedJobsIds.contains(jobId)){ |
---|
192 | log.info(jobId + ", "); |
---|
193 | } |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | } |
---|
198 | |
---|
199 | /** |
---|
200 | * Sends jobs to broker entities |
---|
201 | */ |
---|
202 | protected void sendJobs() { |
---|
203 | |
---|
204 | Map<Long, List<Job>> jobTimes = new TreeMap<Long, List<Job>>(); |
---|
205 | int destID = GridSim.getEntityId(destName); |
---|
206 | //destID=GridSim.getEntityId("BROKER@COMPUTING_GRID_0"); |
---|
207 | List<JobDescription> jobs = workloadLoader.getJobs(); |
---|
208 | //TaskRequirements taskReq = new TaskRequirementsImpl(); |
---|
209 | //double values[] = null; |
---|
210 | |
---|
211 | for (JobDescription job : jobs) { |
---|
212 | long l_submissionTime = Long.MAX_VALUE; |
---|
213 | //pick the lowest submission time |
---|
214 | for (TaskDescription task : job) { |
---|
215 | if (task.getSubmissionTime() < l_submissionTime) |
---|
216 | l_submissionTime = task.getSubmissionTime(); |
---|
217 | } |
---|
218 | |
---|
219 | //store the submission time expressed in seconds after the simulation start time |
---|
220 | long submissionTime = l_submissionTime; |
---|
221 | |
---|
222 | Job newJob = prepareJob(job, submissionTime); |
---|
223 | |
---|
224 | List<Job> list = jobTimes.get(submissionTime); |
---|
225 | if (list == null) { |
---|
226 | list = new ArrayList<Job>(); |
---|
227 | jobTimes.put(submissionTime, list); |
---|
228 | } |
---|
229 | list.add(newJob); |
---|
230 | } |
---|
231 | for (Long submissionTime : jobTimes.keySet()) { |
---|
232 | List<Job> list = jobTimes.get(submissionTime); |
---|
233 | for(int i = 0; i < list.size(); i++){ |
---|
234 | this.sendJobsIds.add(list.get(i).getId()); |
---|
235 | send(destID, submissionTime, GridSimTags.GRIDLET_SUBMIT, list.get(i)); |
---|
236 | } |
---|
237 | |
---|
238 | //send(output, submissionTime, GridSimTags.GRIDLET_SUBMIT, new IO_data(list, GssConstants.DEFAULT_GRIDLET_SIZE, destID)); |
---|
239 | //send(destID, submissionTime, GridSimTags.GRIDLET_SUBMIT, list); |
---|
240 | |
---|
241 | } |
---|
242 | System.out.println("finished sending jobs"); |
---|
243 | } |
---|
244 | |
---|
245 | public List<JobDescription> getAllSentJobs() { |
---|
246 | return (List<JobDescription>) workloadLoader.getJobs(); |
---|
247 | } |
---|
248 | |
---|
249 | public List<TaskDescription> getAllSentTasks() { |
---|
250 | List<TaskDescription> result = new ArrayList<TaskDescription>(); |
---|
251 | List<JobDescription> sentJobs = getAllSentJobs(); |
---|
252 | for (JobDescription job : sentJobs) { |
---|
253 | result.addAll(job); |
---|
254 | } |
---|
255 | return result; |
---|
256 | } |
---|
257 | |
---|
258 | public List<JobInterface<?>> getAllReceivedJobs() { |
---|
259 | return returnedJobs; |
---|
260 | } |
---|
261 | |
---|
262 | public int getFinishedTasksCount() { |
---|
263 | int result = 0; |
---|
264 | for (JobInterface<?> job : returnedJobs) { |
---|
265 | for (TaskInterface<?> task: job.getTask()) { |
---|
266 | if(task.getStatus() == Gridlet.SUCCESS) |
---|
267 | result++; |
---|
268 | } |
---|
269 | } |
---|
270 | return result; |
---|
271 | } |
---|
272 | |
---|
273 | public String getUserName() { |
---|
274 | return get_name(); |
---|
275 | } |
---|
276 | |
---|
277 | public boolean isError() { |
---|
278 | return error; |
---|
279 | } |
---|
280 | |
---|
281 | /** |
---|
282 | * Performs action concerning a ping request to this entity |
---|
283 | * @param ev the event object |
---|
284 | */ |
---|
285 | protected void processPingRequest(Sim_event ev) { |
---|
286 | InfoPacket pkt = (InfoPacket) ev.get_data(); |
---|
287 | pkt.setTag(GridSimTags.INFOPKT_RETURN); |
---|
288 | pkt.setDestID(pkt.getSrcID()); |
---|
289 | |
---|
290 | // sends back to the sender |
---|
291 | send(output, GridSimTags.SCHEDULE_NOW, GridSimTags.INFOPKT_RETURN, |
---|
292 | new IO_data(pkt, pkt.getSize(), pkt.getSrcID())); |
---|
293 | } |
---|
294 | |
---|
295 | protected Job prepareJob(JobDescription jobDescription, long submissionTime){ |
---|
296 | |
---|
297 | Job newJob = new Job(jobDescription.getJobId()); |
---|
298 | DateTime submitionTime = new DateTime(); |
---|
299 | submitionTime = submitionTime.plusMillis((int)submissionTime*1000); |
---|
300 | try { |
---|
301 | |
---|
302 | // transform job description to resource requirements |
---|
303 | |
---|
304 | newJob.setSenderId(this.get_id()); |
---|
305 | |
---|
306 | for (TaskDescription taskDescription : jobDescription) { |
---|
307 | |
---|
308 | String xmlResReq = this.xsltTransformer.taskToResourceRequirements( |
---|
309 | taskDescription.getDocument(), |
---|
310 | jobDescription.getJobId(), |
---|
311 | taskDescription.getUserDn(), |
---|
312 | submitionTime); |
---|
313 | |
---|
314 | Task newTask = new Task(xmlResReq); |
---|
315 | newTask.setSenderId(this.get_id()); |
---|
316 | newTask.setStatus((int)BrokerConstants.TASK_STATUS_UNSUBMITTED); |
---|
317 | newTask.setLength(taskDescription.getTaskLength()); |
---|
318 | newTask.setWorkloadLogWaitTime(taskDescription.getWorkloadLogWaitTime()); |
---|
319 | // newTask.setSubmissionTime(taskDescription.getSubmissionTime()); |
---|
320 | |
---|
321 | newJob.add(newTask); |
---|
322 | } |
---|
323 | |
---|
324 | jobDescription.discardUnused(); |
---|
325 | |
---|
326 | } catch (Exception e){ |
---|
327 | log.error(e.getMessage()); |
---|
328 | e.printStackTrace(); |
---|
329 | } |
---|
330 | |
---|
331 | return newJob; |
---|
332 | } |
---|
333 | } |
---|