1 //******************************************************************************
2 //
3 // File: JobSchedulerMessage.java
4 // Package: edu.rit.pj.cluster
5 // Unit: Class edu.rit.pj.cluster.JobSchedulerMessage
6 //
7 // This Java source file is copyright (C) 2012 by Alan Kaminsky. All rights
8 // reserved. For further information, contact the author, Alan Kaminsky, at
9 // ark@cs.rit.edu.
10 //
11 // This Java source file is part of the Parallel Java Library ("PJ"). PJ is free
12 // software; you can redistribute it and/or modify it under the terms of the GNU
13 // General Public License as published by the Free Software Foundation; either
14 // version 3 of the License, or (at your option) any later version.
15 //
16 // PJ is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 // A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19 //
20 // Linking this library statically or dynamically with other modules is making a
21 // combined work based on this library. Thus, the terms and conditions of the GNU
22 // General Public License cover the whole combination.
23 //
24 // As a special exception, the copyright holders of this library give you
25 // permission to link this library with independent modules to produce an
26 // executable, regardless of the license terms of these independent modules, and
27 // to copy and distribute the resulting executable under terms of your choice,
28 // provided that you also meet, for each linked independent module, the terms
29 // and conditions of the license of that module. An independent module is a module
30 // which is not derived from or based on this library. If you modify this library,
31 // you may extend this exception to your version of the library, but you are not
32 // obligated to do so. If you do not wish to do so, delete this exception
33 // statement from your version.
34 //
35 // A copy of the GNU General Public License is provided in the file gpl.txt. You
36 // may also obtain a copy of the GNU General Public License on the World Wide
37 // Web at http://www.gnu.org/licenses/gpl.html.
38 //
39 //******************************************************************************
40 package edu.rit.pj.cluster;
41
42 import java.io.Externalizable;
43 import java.io.IOException;
44 import java.io.ObjectInput;
45 import java.io.ObjectOutput;
46
47 /**
48 * Class JobSchedulerMessage provides a message sent to a Job Scheduler process
49 * (interface {@linkplain JobSchedulerRef}) in the PJ cluster middleware.
50 *
51 * @author Alan Kaminsky
52 * @version 24-Jan-2012
53 */
54 public abstract class JobSchedulerMessage
55 extends Message
56 implements Externalizable {
57
58 // Hidden data members.
59 private static final long serialVersionUID = -7379945472003527741L;
60
61 // Exported constructors.
62 /**
63 * Construct a new job scheduler message.
64 */
65 public JobSchedulerMessage() {
66 }
67
68 /**
69 * Construct a new job scheduler message with the given message tag.
70 *
71 * @param theTag Message tag to use when sending this message.
72 */
73 public JobSchedulerMessage(int theTag) {
74 super(theTag);
75 }
76
77 // Exported operations.
78 /**
79 * Construct a new "backend failed" message.
80 *
81 * @param theJobFrontend Job frontend that is calling this method.
82 * @param name Backend node name.
83 * @return a {@link edu.rit.pj.cluster.JobSchedulerMessage} object.
84 */
85 public static JobSchedulerMessage backendFailed(JobFrontendRef theJobFrontend,
86 String name) {
87 return new BackendFailedMessage(theJobFrontend, name);
88 }
89
90 /**
91 * Construct a new "cancel job" message.
92 *
93 * @param theJobFrontend Job frontend that is calling this method.
94 * @param errmsg Error message string.
95 * @return a {@link edu.rit.pj.cluster.JobSchedulerMessage} object.
96 */
97 public static JobSchedulerMessage cancelJob(JobFrontendRef theJobFrontend,
98 String errmsg) {
99 return new CancelJobMessage(theJobFrontend, errmsg);
100 }
101
102 /**
103 * Construct a new "job finished" message.
104 *
105 * @param theJobFrontend Job frontend that is calling this method.
106 * @return a {@link edu.rit.pj.cluster.JobSchedulerMessage} object.
107 */
108 public static JobSchedulerMessage jobFinished(JobFrontendRef theJobFrontend) {
109 return new JobFinishedMessage(theJobFrontend);
110 }
111
112 /**
113 * Construct a new "renew lease" message.
114 *
115 * @param theJobFrontend Job frontend that is calling this method.
116 * @return a {@link edu.rit.pj.cluster.JobSchedulerMessage} object.
117 */
118 public static JobSchedulerMessage renewLease(JobFrontendRef theJobFrontend) {
119 return new RenewLeaseMessage(theJobFrontend);
120 }
121
122 /**
123 * Construct a new "report comment" message.
124 *
125 * @param theJobFrontend Job frontend that is calling this method.
126 * @param rank Process rank.
127 * @param comment Comment string.
128 * @return a {@link edu.rit.pj.cluster.JobSchedulerMessage} object.
129 */
130 public static JobSchedulerMessage reportComment(JobFrontendRef theJobFrontend,
131 int rank,
132 String comment) {
133 return new ReportCommentMessage(theJobFrontend, rank, comment);
134 }
135
136 /**
137 * Construct a new "request job" message.
138 *
139 * @param theJobFrontend Job frontend that is calling this method.
140 * @param username User name.
141 * @param Nn Number of backend nodes.
142 * @param Np Number of processes.
143 * @param Nt Number of CPUs per process. 0 means "all CPUs."
144 * @return a {@link edu.rit.pj.cluster.JobSchedulerMessage} object.
145 */
146 public static JobSchedulerMessage requestJob(JobFrontendRef theJobFrontend,
147 String username,
148 int Nn,
149 int Np,
150 int Nt) {
151 return new RequestJobMessage(theJobFrontend, username, Nn, Np, Nt);
152 }
153
154 /**
155 * Invoke the method corresponding to this job scheduler message on the
156 * given Job Scheduler object. The method arguments come from the fields of
157 * this job scheduler message object.
158 *
159 * @param theJobScheduler Job Scheduler on which to invoke the method.
160 * @param theJobFrontend Job Frontend that is calling the method.
161 * @exception IOException Thrown if an I/O error occurred.
162 * @throws java.io.IOException if any.
163 */
164 public void invoke(JobSchedulerRef theJobScheduler,
165 JobFrontendRef theJobFrontend)
166 throws IOException {
167 throw new UnsupportedOperationException();
168 }
169
170 /**
171 * {@inheritDoc}
172 *
173 * Write this job scheduler message to the given object output stream.
174 * @exception IOException Thrown if an I/O error occurred.
175 */
176 public void writeExternal(ObjectOutput out)
177 throws IOException {
178 }
179
180 /**
181 * {@inheritDoc}
182 *
183 * Read this job scheduler message from the given object input stream.
184 * @exception IOException Thrown if an I/O error occurred.
185 */
186 public void readExternal(ObjectInput in)
187 throws IOException {
188 }
189
190 // Hidden subclasses.
191 /**
192 * Class BackendFailedMessage provides the Job Scheduler "backend failed"
193 * message in the PJ cluster middleware.
194 *
195 * @author Alan Kaminsky
196 * @version 13-Oct-2006
197 */
198 private static class BackendFailedMessage
199 extends JobSchedulerMessage {
200
201 private static final long serialVersionUID = 6495614788809259018L;
202
203 private String name;
204
205 public BackendFailedMessage() {
206 }
207
208 public BackendFailedMessage(JobFrontendRef theJobFrontend,
209 String name) {
210 super(Message.FROM_JOB_FRONTEND);
211 this.name = name;
212 }
213
214 public void invoke(JobSchedulerRef theJobScheduler,
215 JobFrontendRef theJobFrontend)
216 throws IOException {
217 theJobScheduler.backendFailed(theJobFrontend, name);
218 }
219
220 public void writeExternal(ObjectOutput out)
221 throws IOException {
222 out.writeUTF(name);
223 }
224
225 public void readExternal(ObjectInput in)
226 throws IOException {
227 name = in.readUTF();
228 }
229 }
230
231 /**
232 * Class CancelJobMessage provides the Job Scheduler "cancel job" message in
233 * the PJ cluster middleware.
234 *
235 * @author Alan Kaminsky
236 * @version 12-Oct-2006
237 */
238 private static class CancelJobMessage
239 extends JobSchedulerMessage {
240
241 private static final long serialVersionUID = 2902818757044365344L;
242
243 private String errmsg;
244
245 public CancelJobMessage() {
246 }
247
248 public CancelJobMessage(JobFrontendRef theJobFrontend,
249 String errmsg) {
250 super(Message.FROM_JOB_FRONTEND);
251 this.errmsg = errmsg;
252 }
253
254 public void invoke(JobSchedulerRef theJobScheduler,
255 JobFrontendRef theJobFrontend)
256 throws IOException {
257 theJobScheduler.cancelJob(theJobFrontend, errmsg);
258 }
259
260 public void writeExternal(ObjectOutput out)
261 throws IOException {
262 out.writeUTF(errmsg);
263 }
264
265 public void readExternal(ObjectInput in)
266 throws IOException {
267 errmsg = in.readUTF();
268 }
269 }
270
271 /**
272 * Class JobFinishedMessage provides the Job Scheduler "job finished"
273 * message in the PJ cluster middleware.
274 *
275 * @author Alan Kaminsky
276 * @version 13-Oct-2006
277 */
278 private static class JobFinishedMessage
279 extends JobSchedulerMessage {
280
281 private static final long serialVersionUID = -1179228962545666153L;
282
283 public JobFinishedMessage() {
284 }
285
286 public JobFinishedMessage(JobFrontendRef theJobFrontend) {
287 super(Message.FROM_JOB_FRONTEND);
288 }
289
290 public void invoke(JobSchedulerRef theJobScheduler,
291 JobFrontendRef theJobFrontend)
292 throws IOException {
293 theJobScheduler.jobFinished(theJobFrontend);
294 }
295 }
296
297 /**
298 * Class RenewLeaseMessage provides the Job Scheduler "renew lease" message
299 * in the PJ cluster middleware.
300 *
301 * @author Alan Kaminsky
302 * @version 12-Oct-2006
303 */
304 private static class RenewLeaseMessage
305 extends JobSchedulerMessage {
306
307 private static final long serialVersionUID = 8547605668292095227L;
308
309 public RenewLeaseMessage() {
310 }
311
312 public RenewLeaseMessage(JobFrontendRef theJobFrontend) {
313 super(Message.FROM_JOB_FRONTEND);
314 }
315
316 public void invoke(JobSchedulerRef theJobScheduler,
317 JobFrontendRef theJobFrontend)
318 throws IOException {
319 theJobScheduler.renewLease(theJobFrontend);
320 }
321 }
322
323 /**
324 * Class ReportCommentMessage provides the Job Scheduler "report comment"
325 * message in the PJ cluster middleware.
326 *
327 * @author Alan Kaminsky
328 * @version 24-Jan-2012
329 */
330 private static class ReportCommentMessage
331 extends JobSchedulerMessage {
332
333 private static final long serialVersionUID = -7431990305653172900L;
334
335 private int rank;
336 private String comment;
337
338 public ReportCommentMessage() {
339 }
340
341 public ReportCommentMessage(JobFrontendRef theJobFrontend,
342 int rank,
343 String comment) {
344 super(Message.FROM_JOB_FRONTEND);
345 this.rank = rank;
346 this.comment = comment == null ? "" : comment;
347 }
348
349 public void invoke(JobSchedulerRef theJobScheduler,
350 JobFrontendRef theJobFrontend)
351 throws IOException {
352 theJobScheduler.reportComment(theJobFrontend, rank, comment);
353 }
354
355 public void writeExternal(ObjectOutput out)
356 throws IOException {
357 out.writeInt(rank);
358 out.writeUTF(comment);
359 }
360
361 public void readExternal(ObjectInput in)
362 throws IOException {
363 rank = in.readInt();
364 comment = in.readUTF();
365 }
366 }
367
368 /**
369 * Class RequestJobMessage provides the Job Scheduler "request job" message
370 * in the PJ cluster middleware.
371 *
372 * @author Alan Kaminsky
373 * @version 21-May-2008
374 */
375 private static class RequestJobMessage
376 extends JobSchedulerMessage {
377
378 private static final long serialVersionUID = -6712799261136980645L;
379
380 private String username;
381 private int Nn;
382 private int Np;
383 private int Nt;
384
385 public RequestJobMessage() {
386 }
387
388 public RequestJobMessage(JobFrontendRef theJobFrontend,
389 String username,
390 int Nn,
391 int Np,
392 int Nt) {
393 super(Message.FROM_JOB_FRONTEND);
394 this.username = username;
395 this.Nn = Nn;
396 this.Np = Np;
397 this.Nt = Nt;
398 }
399
400 public void invoke(JobSchedulerRef theJobScheduler,
401 JobFrontendRef theJobFrontend)
402 throws IOException {
403 theJobScheduler.requestJob(theJobFrontend, username, Nn, Np, Nt);
404 }
405
406 public void writeExternal(ObjectOutput out)
407 throws IOException {
408 out.writeUTF(username);
409 out.writeInt(Nn);
410 out.writeInt(Np);
411 out.writeInt(Nt);
412 }
413
414 public void readExternal(ObjectInput in)
415 throws IOException {
416 username = in.readUTF();
417 Nn = in.readInt();
418 Np = in.readInt();
419 Nt = in.readInt();
420 }
421 }
422
423 }