Skip to content

Commit 87d6563

Browse files
elambertChristopher Suter
authored andcommitted
merge GearmanClient style,pmd, and findbugs corrections
------------------------------------------------------------ Use --include-merges or -n0 to see merged revisions.
1 parent 10acfa7 commit 87d6563

File tree

6 files changed

+96
-59
lines changed

6 files changed

+96
-59
lines changed

.bzrignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ bin
22
build
33
dist
44
.classpath
5+
.DS_Store
56
.project
67
.settings
78
nbbuild.xml

ChangeLog

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
0.03 - 2009-XX-XX
2+
* Cleanup findbugs, pmd, and checkstyle warnings
3+
* Changed signature of the addServerMethod in GearmanClient and
4+
GearmanWorker (as well as their implementations) to return a
5+
boolean to indicate success or failure of attempt. Attmepts to
6+
add a server that can not be contacted will now return false as
7+
opposed to throwing a runtime exception.
8+
* ClientImpl driveRequestTillState now drivesIO on all sessions that
9+
are selected for IO instead of driving IO for only the session to
10+
which the request belongs.
11+
112
0.02 - 2009-08-06
213
* Significantly improved worker and client performance.
314
* Fixed bug #400466. (Client leaks memory when submitting attached jobs).

src/org/gearman/client/GearmanClient.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import java.io.IOException;
99
import java.util.List;
1010
import java.util.concurrent.ExecutorService;
11-
import java.util.concurrent.Future;
1211

1312
import org.gearman.common.GearmanJobServerConnection;
1413

@@ -30,7 +29,7 @@
3029
* passing a {@link GearmanJob} to either the
3130
* {@link GearmanClient#submit(java.util.concurrent.Callable) } or the
3231
* {@link GearmanClient#invokeAll(java.util.Collection) } methods. Both of
33-
* these methods return a {@link Future} object which can be used to track
32+
* these methods return a {@link java.util.concurrent.Future} object which can be used to track
3433
* the state of that job.
3534
*
3635
* <p>
@@ -71,11 +70,14 @@ public interface GearmanClient extends ExecutorService {
7170
*
7271
* @param conn The connection to the Gearman Job Server.
7372
*
73+
* @return returns true if a connection to the server was established and
74+
* the server was added to the client, else false.
75+
*
7476
* @throws IllegalArgumentException If an invalid connection has been
7577
* specified.
7678
* @throws IllegalStateException If the client has already been stopped.
7779
*/
78-
void addJobServer(GearmanJobServerConnection conn)
80+
boolean addJobServer(GearmanJobServerConnection conn)
7981
throws IllegalArgumentException, IllegalStateException;
8082

8183
/**

src/org/gearman/client/GearmanClientImpl.java

Lines changed: 66 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,38 @@
88
import java.io.IOException;
99
import java.nio.channels.SelectionKey;
1010
import java.nio.channels.Selector;
11-
import java.util.Arrays;
1211
import java.util.ArrayList;
12+
import java.util.Arrays;
1313
import java.util.Collection;
1414
import java.util.HashMap;
1515
import java.util.Iterator;
1616
import java.util.List;
17+
import java.util.Map;
1718
import java.util.Set;
1819
import java.util.Timer;
1920
import java.util.TimerTask;
20-
import java.util.logging.Level;
21-
import java.util.logging.Logger;
2221
import java.util.concurrent.Callable;
2322
import java.util.concurrent.ExecutionException;
2423
import java.util.concurrent.Future;
2524
import java.util.concurrent.RejectedExecutionException;
2625
import java.util.concurrent.TimeUnit;
2726
import java.util.concurrent.TimeoutException;
2827
import java.util.concurrent.atomic.AtomicBoolean;
28+
import java.util.logging.Level;
29+
import java.util.logging.Logger;
2930
import org.gearman.common.Constants;
3031
import org.gearman.common.GearmanException;
3132
import org.gearman.common.GearmanJobServerConnection;
32-
import org.gearman.common.GearmanPacket;
3333
import org.gearman.common.GearmanJobServerSession;
3434
import org.gearman.common.GearmanNIOJobServerConnection;
35-
import org.gearman.common.GearmanTask;
36-
import org.gearman.common.GearmanServerResponseHandler;
37-
import org.gearman.common.GearmanSessionEvent;
38-
import org.gearman.common.GearmanSessionEventHandler;
35+
import org.gearman.common.GearmanPacket;
3936
import org.gearman.common.GearmanPacketImpl;
4037
import org.gearman.common.GearmanPacketMagic;
4138
import org.gearman.common.GearmanPacketType;
39+
import org.gearman.common.GearmanServerResponseHandler;
40+
import org.gearman.common.GearmanSessionEvent;
41+
import org.gearman.common.GearmanSessionEventHandler;
42+
import org.gearman.common.GearmanTask;
4243
import org.gearman.util.ByteUtils;
4344

4445
//TODO change the server selection to use open connection
@@ -66,19 +67,21 @@ private static enum state {
6667
}
6768

6869
private static final String DESCRIPION_PREFIX = "GearmanClient";
69-
private final String DESCRIPTION;
70-
private HashMap<SelectionKey, GearmanJobServerSession> sessionsMap = null;
71-
private Selector ioAvailable = null;
7270
private static final Logger LOG = Logger.getLogger(
7371
Constants.GEARMAN_CLIENT_LOGGER_NAME);
72+
private static final String CLIENT_NOT_ACTIVE = "Client is not active";
73+
private final String DESCRIPTION;
74+
private Map<SelectionKey, GearmanJobServerSession> sessionsMap = null;
75+
private Selector ioAvailable = null;
7476
private state runState = state.RUNNING;
75-
private HashMap<JobHandle, GearmanJobImpl> jobsMaps = null;
76-
private HashMap<GearmanJobServerSession, GearmanJobImpl> submitJobMap = null;
77-
private Timer timer = new Timer();
77+
private final Map<JobHandle, GearmanJobImpl> jobsMaps;
78+
private final Map<GearmanJobServerSession, GearmanJobImpl> submitJobMap;
79+
private final Timer timer = new Timer();
80+
7881

79-
private class Alarm extends TimerTask {
82+
private static class Alarm extends TimerTask {
8083

81-
private AtomicBoolean timesUp = new AtomicBoolean(false);
84+
private final AtomicBoolean timesUp = new AtomicBoolean(false);
8285

8386
@Override
8487
public void run() {
@@ -90,11 +93,11 @@ public boolean hasFired() {
9093
}
9194
}
9295

93-
private class JobHandle {
96+
private static class JobHandle {
9497

9598
private final byte[] handle;
9699

97-
private JobHandle(byte[] handle) {
100+
JobHandle(byte[] handle) {
98101
this.handle = new byte[handle.length];
99102
System.arraycopy(handle, 0, this.handle, 0, handle.length);
100103
}
@@ -125,13 +128,11 @@ public GearmanClientImpl() {
125128
sessionsMap = new HashMap<SelectionKey, GearmanJobServerSession>();
126129
jobsMaps = new HashMap<JobHandle, GearmanJobImpl>();
127130
submitJobMap = new HashMap<GearmanJobServerSession, GearmanJobImpl>();
128-
DESCRIPTION = new String(DESCRIPION_PREFIX + ":" +
129-
Thread.currentThread().getId());
131+
DESCRIPTION = DESCRIPION_PREFIX + ":" + Thread.currentThread().getId();
130132
}
131133

132-
public void addJobServer(GearmanJobServerConnection newconn)
133-
throws IllegalArgumentException,
134-
IllegalStateException {
134+
public boolean addJobServer(GearmanJobServerConnection newconn)
135+
throws IllegalArgumentException,IllegalStateException {
135136

136137
//TODO remove this restriction
137138
if (!(newconn instanceof GearmanNIOJobServerConnection)) {
@@ -150,7 +151,9 @@ public void addJobServer(GearmanJobServerConnection newconn)
150151

151152
GearmanJobServerSession session = new GearmanJobServerSession(conn);
152153
if (sessionsMap.values().contains(session)) {
153-
return;
154+
LOG.log(Level.FINE,"The server " + newconn + " was previously " +
155+
"added to the client. Ignoring add request.");
156+
return true;
154157
}
155158

156159
try {
@@ -162,10 +165,13 @@ public void addJobServer(GearmanJobServerConnection newconn)
162165
SelectionKey key = session.getSelectionKey();
163166
sessionsMap.put(key, session);
164167
} catch (IOException ioe) {
165-
throw new RuntimeException(ioe);
168+
LOG.log(Level.WARNING,"Failed to connect to job server "
169+
+ newconn + ".",ioe);
170+
return false;
166171
}
167172

168173
LOG.log(Level.FINE, "Added connection " + conn + " to client " + this);
174+
return true;
169175
}
170176

171177
public boolean hasConnection(GearmanJobServerConnection conn) {
@@ -180,7 +186,7 @@ public boolean hasConnection(GearmanJobServerConnection conn) {
180186
public List<GearmanJobServerConnection> getSetOfJobServers()
181187
throws IllegalStateException {
182188
if (!runState.equals(state.RUNNING)) {
183-
throw new IllegalStateException("Client is not active");
189+
throw new IllegalStateException(CLIENT_NOT_ACTIVE);
184190
}
185191

186192
ArrayList<GearmanJobServerConnection> retSet =
@@ -222,7 +228,7 @@ public void removeJobServer(GearmanJobServerConnection conn)
222228
public <T> Future<T> submit(Callable<T> task) {
223229

224230
if (task == null) {
225-
throw new NullPointerException("Null task was submitted to " +
231+
throw new IllegalStateException("Null task was submitted to " +
226232
"gearman client");
227233
}
228234

@@ -255,19 +261,19 @@ public <T> Future<T> submit(Callable<T> task) {
255261
GearmanTask submittedJob =
256262
new GearmanTask(handler, submitRequest);
257263
session.submitTask(submittedJob);
258-
LOG.log(Level.FINE, "Client " + this + " has submitted job " + job +
259-
" to session " + session + ". Job has been added to the " +
264+
LOG.log(Level.FINE, "Client " + this + " has submitted job " + job + //NOPMD
265+
" to session " + session + ". Job has been added to the " + //NOPMD
260266
"active job queue");
261267
try {
262268
submitJobMap.put(session, job);
263-
if (!(driveRequestTillState(session,
264-
submittedJob, GearmanTask.State.RUNNING))) {
265-
throw new RuntimeException("Timed out waiting for submission" +
266-
" of " + job + " to complete");
269+
if (!(driveRequestTillState(submittedJob,
270+
GearmanTask.State.RUNNING))) {
271+
throw new RejectedExecutionException("Timed out waiting for" +
272+
" submission of " + job + " to complete");
267273
}
268274
} catch (IOException ioe) {
269275
LOG.log(Level.WARNING, "Client " + this + " encounted an " +
270-
"IOException while drivingIO");
276+
"IOException while drivingIO",ioe);
271277
} finally {
272278
submitJobMap.remove(session);
273279
}
@@ -296,11 +302,9 @@ public void execute(Runnable command) {
296302
// specified generic types), otherwise we will not be able to compile this
297303
// class using both compilers.
298304

299-
@SuppressWarnings("unchecked")
300-
public List invokeAll(Collection tasks)
301-
throws InterruptedException {
305+
@SuppressWarnings("unchecked") //NOPMD
306+
public List invokeAll(Collection tasks) throws InterruptedException {
302307
ArrayList<Future> futures = new ArrayList<Future>();
303-
304308
Iterator<Callable<Future>> iter = tasks.iterator();
305309
while (iter.hasNext()) {
306310
Callable<Future> curTask = iter.next();
@@ -310,7 +314,8 @@ public List invokeAll(Collection tasks)
310314
try {
311315
results.get();
312316
} catch (ExecutionException ee) {
313-
//TODO
317+
LOG.log(Level.WARNING,"Failed to execute task " +
318+
results + ".",ee);
314319
}
315320
}
316321
return futures;
@@ -349,7 +354,7 @@ public GearmanJobStatus getJobStatus(GearmanJob job) throws IOException,
349354

350355
public byte[] echo(byte[] data) throws IOException, GearmanException {
351356
if (!runState.equals(state.RUNNING)) {
352-
throw new IllegalStateException("Client is not active");
357+
throw new IllegalStateException(CLIENT_NOT_ACTIVE);
353358
}
354359
GearmanPacket echoRequest = new GearmanPacketImpl(GearmanPacketMagic.REQ,
355360
GearmanPacketType.ECHO_REQ,data);
@@ -360,7 +365,7 @@ public byte[] echo(byte[] data) throws IOException, GearmanException {
360365
LOG.log(Level.FINE, "Client " + this + " has submitted echo request " +
361366
"(payload = " + ByteUtils.toHex(data) + " to session " +
362367
session);
363-
if (!driveRequestTillState(session, t, GearmanTask.State.FINISHED)) {
368+
if (!driveRequestTillState(t, GearmanTask.State.FINISHED)) {
364369
throw new GearmanException("Failed to execute echo request " + t +
365370
" to session " + session);
366371
}
@@ -371,7 +376,7 @@ public byte[] echo(byte[] data) throws IOException, GearmanException {
371376

372377
public int getNumberofActiveJobs() throws IllegalStateException {
373378
if (runState.equals(state.TERMINATED)) {
374-
throw new IllegalStateException("Client is not active");
379+
throw new IllegalStateException(CLIENT_NOT_ACTIVE);
375380
}
376381
return jobsMaps == null ? 0 : jobsMaps.size();
377382
}
@@ -397,11 +402,15 @@ public void handleSessionEvent(GearmanSessionEvent event)
397402
JobHandle handle = new JobHandle(p.getDataComponentValue(
398403
GearmanPacket.DataComponentName.JOB_HANDLE));
399404
GearmanJobImpl job = jobsMaps.get(handle);
400-
if (job != null) {
405+
if (job == null) {
406+
LOG.log(Level.WARNING,"Client received packet from server" +
407+
" for unknown job ( job_handle = " + handle +
408+
" packet = " + t +" )");
409+
} else {
401410
job.handleEvent(p);
402-
}
403-
if (job.isDone()) {
404-
jobsMaps.remove(handle);
411+
if (job.isDone()) {
412+
jobsMaps.remove(handle);
413+
}
405414
}
406415
break;
407416
case ERROR:
@@ -520,6 +529,13 @@ public String toString() {
520529
}
521530

522531
private void driveClientIO() throws IOException, GearmanException {
532+
for (GearmanJobServerSession sess : sessionsMap.values()) {
533+
int interestOps = SelectionKey.OP_READ;
534+
if (sess.sessionHasDataToWrite()) {
535+
interestOps |= SelectionKey.OP_WRITE;
536+
}
537+
sess.getSelectionKey().interestOps(interestOps);
538+
}
523539
ioAvailable.selectNow();
524540
Set<SelectionKey> keys = ioAvailable.selectedKeys();
525541
LOG.log(Level.FINEST, "Driving IO for client " + this + ". " +
@@ -536,7 +552,7 @@ private GearmanJobStatus updateJobStatus(byte[] jobhandle,
536552
GearmanJobServerSession session) throws IOException,
537553
IllegalStateException, GearmanException {
538554
if (!runState.equals(state.RUNNING)) {
539-
throw new IllegalStateException("Client is not active");
555+
throw new IllegalStateException(CLIENT_NOT_ACTIVE);
540556
}
541557

542558
if (jobhandle == null || jobhandle.length == 0) {
@@ -550,7 +566,7 @@ private GearmanJobStatus updateJobStatus(byte[] jobhandle,
550566
GearmanTask t = new GearmanTask(
551567
handler, statusRequest);
552568
session.submitTask(t);
553-
if (!driveRequestTillState(session, t,GearmanTask.State.FINISHED)) {
569+
if (!driveRequestTillState(t,GearmanTask.State.FINISHED)) {
554570
throw new GearmanException("Failed to execute jobstatus request " +
555571
t + " to session " + session);
556572
}
@@ -575,13 +591,12 @@ private GearmanJobServerSession getSessionForTask() throws IOException {
575591
return session;
576592
}
577593

578-
private boolean driveRequestTillState(GearmanJobServerSession session,
579-
GearmanTask r, GearmanTask.State state)
594+
private boolean driveRequestTillState(GearmanTask r, GearmanTask.State state)
580595
throws IOException, GearmanException {
581596
Alarm alarm = new Alarm();
582597
timer.schedule(alarm, 2000);
583598
while (r.getState().compareTo(state) < 0 && !(alarm.hasFired())) {
584-
session.driveSessionIO();
599+
driveClientIO();
585600
}
586601
return r.getState().compareTo(state) >= 0;
587602
}

src/org/gearman/worker/GearmanWorker.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,15 @@ public interface GearmanWorker {
3939
* @param conn {@link GearmanJobServerConnection} connected to the
4040
* Gearman Job Server
4141
*
42+
* @return returns true if a connection to the server was established and
43+
* the server was added to the worker, else false.
44+
*
4245
* @throws IllegalArgumentException If an invalid connection has been
4346
* specified.
4447
*
4548
* @throws IllegalStateException If the worker has already been stopped.
4649
*/
47-
void addServer(GearmanJobServerConnection conn);
50+
boolean addServer(GearmanJobServerConnection conn);
4851

4952
/**
5053
* Has a connection to the specified Gearman Job Server been registerd with

0 commit comments

Comments
 (0)