Skip to content

Commit 3d40931

Browse files
committed
add connection status check.
Thanks for iqtao's great work.
1 parent 0ce3e80 commit 3d40931

File tree

6 files changed

+133
-34
lines changed

6 files changed

+133
-34
lines changed

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<groupId>com.whalin</groupId>
55
<artifactId>Memcached-Java-Client</artifactId>
6-
<version>3.0.1-SNAPSHOT</version>
6+
<version>3.0.1</version>
77
<packaging>jar</packaging>
88

99
<name>Memcached-Java-Client</name>
@@ -96,6 +96,7 @@
9696
<optimize>true</optimize>
9797
<source>1.6</source>
9898
<target>1.6</target>
99+
<encoding>UTF-8</encoding>
99100
</configuration>
100101
</plugin>
101102
<plugin>

src/main/java/com/schooner/MemCached/SchoonerSockIO.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,29 @@ public int getBufferSize() {
8181
return bufferSize;
8282
}
8383

84+
/**
85+
* check if the connection is working
86+
* this version fix the bugs of isAlive() as follows:
87+
* 1. some servers doesn't support version command
88+
* 2. readLine shouldn't stop until
89+
* @return true if working
90+
*/
91+
public boolean isAlive() {
92+
if (!isConnected()) {
93+
return false;
94+
}
95+
96+
try {
97+
getByteChannel().write(ByteBuffer.wrap("get dummy\r\n".getBytes()));
98+
99+
String line;
100+
do {
101+
line = this.readLine();
102+
} while (!line.equals("END"));
103+
} catch (Exception e) {
104+
return false;
105+
}
106+
107+
return true;
108+
}
84109
}

src/main/java/com/schooner/MemCached/SchoonerSockIOFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void destroyObject(Object obj) throws Exception {
5555

5656
@Override
5757
public boolean validateObject(Object obj) {
58-
return super.validateObject(obj);
58+
return ((SchoonerSockIO) obj).isAlive();
5959
}
6060

6161
/**

src/main/java/com/schooner/MemCached/SchoonerSockIOPool.java

Lines changed: 101 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,17 @@ protected final MessageDigest initialValue() {
161161
@SuppressWarnings("unused")
162162
private static int recBufferSize = 128;// bufsize
163163

164-
private long maxIdle = 1000; // max idle time for avail sockets
164+
private long maxWait = 1000; // max wait time for avail sockets
165+
private int maxIdle = maxConn;
166+
private int minIdle = GenericObjectPool.DEFAULT_MIN_IDLE;
167+
private boolean testOnBorrow = GenericObjectPool.DEFAULT_TEST_ON_BORROW;
168+
private boolean testOnReturn = GenericObjectPool.DEFAULT_TEST_ON_RETURN;
169+
private long timeBetweenEvictionRunsMillis = GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS;
170+
private int numTestsPerEvictionRun = GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN;
171+
private long minEvictableIdleTimeMillis = GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS;
172+
private boolean testWhileIdle = true;
173+
private long softMinEvictableIdleTimeMillis = GenericObjectPool.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS;
174+
private boolean lifo = GenericObjectPool.DEFAULT_LIFO;
165175

166176
private boolean aliveCheck = false; // default to not check each connection
167177
// for being alive
@@ -337,7 +347,9 @@ private void populateBuckets() {
337347
} else {
338348
factory = new SchoonerSockIOFactory(servers[i], isTcp, bufferSize, socketTO, socketConnectTO, nagle);
339349
}
340-
gop = new GenericObjectPool(factory, maxConn, GenericObjectPool.WHEN_EXHAUSTED_BLOCK, maxIdle, maxConn);
350+
gop = new GenericObjectPool(factory, maxConn, GenericObjectPool.WHEN_EXHAUSTED_BLOCK, maxWait, maxIdle, minIdle, testOnBorrow,
351+
testOnReturn, timeBetweenEvictionRunsMillis, numTestsPerEvictionRun, minEvictableIdleTimeMillis, testWhileIdle,
352+
this.softMinEvictableIdleTimeMillis, this.lifo);
341353
factory.setSockets(gop);
342354
socketPool.put(servers[i], gop);
343355
}
@@ -382,7 +394,10 @@ private void populateConsistentBuckets() {
382394
} else {
383395
factory = new SchoonerSockIOFactory(servers[i], isTcp, bufferSize, socketTO, socketConnectTO, nagle);
384396
}
385-
gop = new GenericObjectPool(factory, maxConn, GenericObjectPool.WHEN_EXHAUSTED_BLOCK, maxIdle, maxConn);
397+
398+
gop = new GenericObjectPool(factory, maxConn, GenericObjectPool.WHEN_EXHAUSTED_BLOCK, maxWait, maxIdle, minIdle, testOnBorrow,
399+
testOnReturn, timeBetweenEvictionRunsMillis, numTestsPerEvictionRun, minEvictableIdleTimeMillis, testWhileIdle,
400+
this.softMinEvictableIdleTimeMillis, this.lifo);
386401
factory.setSockets(gop);
387402
socketPool.put(servers[i], gop);
388403
}
@@ -758,7 +773,7 @@ public final int getSocketConnectTO() {
758773
* @param maxIdle
759774
* idle time in ms
760775
*/
761-
public void setMaxIdle(long maxIdle) {
776+
public void setMaxIdle(int maxIdle) {
762777
this.maxIdle = maxIdle;
763778
}
764779

@@ -767,10 +782,89 @@ public void setMaxIdle(long maxIdle) {
767782
*
768783
* @return max idle setting in ms
769784
*/
770-
public long getMaxIdle() {
785+
public int getMaxIdle() {
771786
return this.maxIdle;
772787
}
773-
788+
789+
public final long getMaxWait() {
790+
return this.maxWait;
791+
}
792+
793+
public final void setMaxWait(long maxWait) {
794+
this.maxWait = maxWait;
795+
}
796+
797+
public final int getMinIdle() {
798+
return minIdle;
799+
}
800+
801+
public final void setMinIdle(int minIdle) {
802+
this.minIdle = minIdle;
803+
}
804+
805+
public final boolean getTestOnBorrow() {
806+
return testOnBorrow;
807+
}
808+
809+
public final void setTestOnBorrow(boolean testOnBorrow) {
810+
this.testOnBorrow = testOnBorrow;
811+
}
812+
813+
public final boolean getTestOnReturn() {
814+
return testOnReturn;
815+
}
816+
817+
public final void setTestOnReturn(boolean testOnReturn) {
818+
this.testOnReturn = testOnReturn;
819+
}
820+
821+
public final long getTimeBetweenEvictionRunsMillis() {
822+
return this.timeBetweenEvictionRunsMillis;
823+
}
824+
825+
public final void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) {
826+
this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
827+
}
828+
829+
public final int getNumTestsPerEvictionRun() {
830+
return this.numTestsPerEvictionRun;
831+
}
832+
833+
public final void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) {
834+
this.numTestsPerEvictionRun = numTestsPerEvictionRun;
835+
}
836+
837+
public final long getMinEvictableIdleTimeMillis() {
838+
return this.minEvictableIdleTimeMillis;
839+
}
840+
841+
public final void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {
842+
this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
843+
}
844+
845+
public final boolean getTestWhileIdle() {
846+
return this.testWhileIdle;
847+
}
848+
849+
public final void setTestWhileIdle(boolean testWhileIdle) {
850+
this.testWhileIdle = testWhileIdle;
851+
}
852+
853+
public final long getSoftMinEvictableIdleTimeMillis(long softMinEvictableIdleTimeMillis) {
854+
return this.softMinEvictableIdleTimeMillis;
855+
}
856+
857+
public final void setSoftMinEvictableIdleTimeMillis(long softMinEvictableIdleTimeMillis) {
858+
this.softMinEvictableIdleTimeMillis = softMinEvictableIdleTimeMillis;
859+
}
860+
861+
public final boolean getLifo() {
862+
return this.lifo;
863+
}
864+
865+
public final void setLifo(boolean lifo) {
866+
this.lifo = lifo;
867+
}
774868
/**
775869
* Sets the failover flag for the pool.
776870
*
@@ -1479,27 +1573,6 @@ public boolean isConnected() {
14791573
return (sock != null && sock.isConnected());
14801574
}
14811575

1482-
/**
1483-
* checks to see that the connection is still working
1484-
*
1485-
* @return true if still alive
1486-
*/
1487-
public final boolean isAlive() {
1488-
if (!isConnected())
1489-
return false;
1490-
1491-
// try to talk to the server w/ a dumb query to ask its version
1492-
try {
1493-
write("version\r\n".getBytes());
1494-
readBuf.clear();
1495-
sockChannel.read(readBuf);
1496-
} catch (IOException ex) {
1497-
return false;
1498-
}
1499-
1500-
return true;
1501-
}
1502-
15031576
/**
15041577
* read fix length data from server and store it in the readBuf
15051578
*
@@ -1691,7 +1764,7 @@ public void trueClose(boolean addToDeadPool) throws IOException {
16911764
@Override
16921765
public ByteChannel getByteChannel() {
16931766
// TODO Auto-generated method stub
1694-
return null;
1767+
return sockChannel;
16951768
}
16961769
}
16971770

src/main/java/com/whalin/MemCached/SockIOPool.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public class SockIOPool {
177177
private int poolMultiplier = 3;
178178
private int minConn = 5;
179179
private int maxConn = 100;
180-
private long maxIdle = 1000 * 60 * 5; // max idle time for avail sockets
180+
private int maxIdle = 1000 * 60 * 5; // max idle time for avail sockets
181181
private long maxBusyTime = 1000 * 30; // max idle time for avail sockets
182182
private int socketTO = 1000 * 3; // default timeout of socket reads
183183
private int socketConnectTO = 1000 * 3; // default timeout of socket
@@ -427,7 +427,7 @@ public int getSocketConnectTO() {
427427
* @param maxIdle
428428
* idle time in ms
429429
*/
430-
public void setMaxIdle(long maxIdle) {
430+
public void setMaxIdle(int maxIdle) {
431431
schoonerSockIOPool.setMaxIdle(maxIdle);
432432
}
433433

@@ -436,7 +436,7 @@ public void setMaxIdle(long maxIdle) {
436436
*
437437
* @return max idle setting in ms
438438
*/
439-
public long getMaxIdle() {
439+
public int getMaxIdle() {
440440
return schoonerSockIOPool.getMaxIdle();
441441
}
442442

src/test/java/com/whalin/MemCached/test/MemcachedBench.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static void main(String[] args) {
2626

2727
int runs = Integer.parseInt(args[0]);
2828

29-
String[] serverlist = { "localhost:11211" };
29+
String[] serverlist = { "localhost:11211", "localhost:11212" };
3030

3131
// initialize the pool for memcache servers
3232
SockIOPool pool = SockIOPool.getInstance("test");

0 commit comments

Comments
 (0)