Skip to content

Commit b90bc11

Browse files
daschlMichael Nitschinger
authored and
Michael Nitschinger
committed
SPY-121: Make NOOP send optional on connect.
This changeset makes the NOOP sending before AUTH on connect optional, because this may have issues with other systems and therefore lead to non-working systems. The check can be activated by setting the net.spy.verifyAliveOnConnect system property to true. Change-Id: If26439c024c3c50f02e992aaa80ba39fa683aed0 Reviewed-on: http://review.couchbase.org/26151 Reviewed-by: Matt Ingenthron <[email protected]> Reviewed-by: Deepti Dawar <[email protected]> Tested-by: Michael Nitschinger <[email protected]>
1 parent 87c002a commit b90bc11

File tree

2 files changed

+55
-37
lines changed

2 files changed

+55
-37
lines changed

src/main/java/net/spy/memcached/MemcachedClient.java

+7
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@
124124
* // Do other timeout related stuff
125125
* }
126126
* </pre>
127+
*
128+
* <p>Optionally, it is possible to activate a check that makes sure that
129+
* the node is alive and responding before running actual operations (even
130+
* before authentication. Only enable this if you are sure that you do not
131+
* run into issues during connection (some memcached services have problems
132+
* with it). You can enable it by setting the net.spy.verifyAliveOnConnect
133+
* System Property to "true".</p>
127134
*/
128135
public class MemcachedClient extends SpyObject implements MemcachedClientIF,
129136
ConnectionObserver {

src/main/java/net/spy/memcached/MemcachedConnection.java

+48-37
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public class MemcachedConnection extends SpyThread {
105105
private final int timeoutExceptionThreshold;
106106
private final Collection<Operation> retryOps;
107107
protected final ConcurrentLinkedQueue<MemcachedNode> nodesToShutdown;
108+
private final boolean verifyAliveOnConnect;
108109

109110
/**
110111
* Construct a memcached connection.
@@ -131,6 +132,14 @@ public MemcachedConnection(int bufSize, ConnectionFactory f,
131132
nodesToShutdown = new ConcurrentLinkedQueue<MemcachedNode>();
132133
this.bufSize = bufSize;
133134
this.connectionFactory = f;
135+
136+
String verifyAlive = System.getProperty("net.spy.verifyAliveOnConnect");
137+
if(verifyAlive != null && verifyAlive.equals("true")) {
138+
verifyAliveOnConnect = true;
139+
} else {
140+
verifyAliveOnConnect = false;
141+
}
142+
134143
List<MemcachedNode> connections = createConnections(a);
135144
locator = f.createLocator(connections);
136145
setName("Memcached IO over " + this);
@@ -412,47 +421,49 @@ private void handleIO(SelectionKey sk) {
412421
final SocketChannel channel = node.getChannel();
413422
if (channel.finishConnect()) {
414423

415-
// Test to see if it's truly alive, could be a hung process, OS
416-
final CountDownLatch latch = new CountDownLatch(1);
417-
final OperationFuture<Boolean> rv =
418-
new OperationFuture<Boolean>("noop", latch, 2500);
419-
NoopOperation testOp = opFact.noop(new OperationCallback() {
420-
public void receivedStatus(OperationStatus status) {
421-
rv.set(status.isSuccess(), status);
422-
}
424+
if(verifyAliveOnConnect) {
425+
// Test to see if it's truly alive, could be a hung process, OS
426+
final CountDownLatch latch = new CountDownLatch(1);
427+
final OperationFuture<Boolean> rv =
428+
new OperationFuture<Boolean>("noop", latch, 2500);
429+
NoopOperation testOp = opFact.noop(new OperationCallback() {
430+
public void receivedStatus(OperationStatus status) {
431+
rv.set(status.isSuccess(), status);
432+
}
423433

424-
@Override
425-
public void complete() {
426-
latch.countDown();
427-
}
428-
});
429-
430-
testOp.setHandlingNode(node);
431-
testOp.initialize();
432-
433-
checkState();
434-
insertOperation(node, testOp);
435-
node.copyInputQueue();
436-
437-
boolean done = false;
438-
if(sk.isValid()) {
439-
long timeout = TimeUnit.MILLISECONDS.toNanos(
440-
connectionFactory.getOperationTimeout());
441-
for(long stop = System.nanoTime() + timeout;
442-
stop > System.nanoTime();) {
443-
handleWrites(sk, node);
444-
handleReads(sk, node);
445-
if(done = (latch.getCount() == 0)) {
446-
break;
434+
@Override
435+
public void complete() {
436+
latch.countDown();
437+
}
438+
});
439+
440+
testOp.setHandlingNode(node);
441+
testOp.initialize();
442+
443+
checkState();
444+
insertOperation(node, testOp);
445+
node.copyInputQueue();
446+
447+
boolean done = false;
448+
if(sk.isValid()) {
449+
long timeout = TimeUnit.MILLISECONDS.toNanos(
450+
connectionFactory.getOperationTimeout());
451+
for(long stop = System.nanoTime() + timeout;
452+
stop > System.nanoTime();) {
453+
handleWrites(sk, node);
454+
handleReads(sk, node);
455+
if(done = (latch.getCount() == 0)) {
456+
break;
457+
}
447458
}
448459
}
449-
}
450460

451-
if (!done || testOp.isCancelled() || testOp.hasErrored()
452-
|| testOp.isTimedOut()) {
453-
throw new ConnectException("Could not send noop upon connect! "
454-
+ "This may indicate a running, but not responding memcached "
455-
+ "instance.");
461+
if (!done || testOp.isCancelled() || testOp.hasErrored()
462+
|| testOp.isTimedOut()) {
463+
throw new ConnectException("Could not send noop upon connect! "
464+
+ "This may indicate a running, but not responding memcached "
465+
+ "instance.");
466+
}
456467
}
457468

458469
connected(node);

0 commit comments

Comments
 (0)