Skip to content

Commit ca7887a

Browse files
committed
Re-implement connection pool based on apache commons pool. Resolve
OutOfMemoryError. Change-Id: Id456ef73ce4f12bdcfcea9e4d524056b351690d4
1 parent 83bd6cc commit ca7887a

File tree

10 files changed

+298
-405
lines changed

10 files changed

+298
-405
lines changed

.classpath

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<classpath>
3-
<classpathentry kind="src" path="src/main"/>
4-
<classpathentry kind="src" path="src/test"/>
5-
<classpathentry kind="lib" path="lib/emma_ant.jar"/>
6-
<classpathentry kind="lib" path="lib/emma.jar"/>
7-
<classpathentry kind="lib" path="lib/junit.jar"/>
8-
<classpathentry kind="lib" path="lib/slf4j-api-1.6.1.jar"/>
9-
<classpathentry kind="lib" path="lib/slf4j-simple-1.6.1.jar"/>
10-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
11-
<classpathentry kind="output" path="bin"/>
12-
</classpath>
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src/main"/>
4+
<classpathentry kind="src" path="src/test"/>
5+
<classpathentry kind="lib" path="lib/emma_ant.jar"/>
6+
<classpathentry kind="lib" path="lib/emma.jar"/>
7+
<classpathentry kind="lib" path="lib/junit.jar"/>
8+
<classpathentry kind="lib" path="lib/slf4j-api-1.6.1.jar"/>
9+
<classpathentry kind="lib" path="lib/slf4j-simple-1.6.1.jar"/>
10+
<classpathentry kind="lib" path="lib/commons-pool-1.5.6.jar"/>
11+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
12+
<classpathentry kind="output" path="bin"/>
13+
</classpath>

lib/commons-pool-1.5.6.jar

98.1 KB
Binary file not shown.

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

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,9 @@ public boolean delete(String key, Integer hashCode, Date expiry) {
207207
log.error(e.getMessage(), e);
208208

209209
try {
210-
sock.trueClose();
211-
} catch (IOException ioe) {
212-
log.error(new StringBuffer().append("++++ failed to close socket : ").append(sock.toString())
213-
.toString());
210+
sock.sockets.invalidateObject(sock);
211+
} catch (Exception e1) {
212+
log.error("++++ failed to close socket : " + sock.toString());
214213
}
215214

216215
sock = null;
@@ -436,10 +435,9 @@ private boolean set(String cmdname, String key, Object value, Date expiry, Integ
436435
log.error(e.getMessage(), e);
437436

438437
try {
439-
sock.trueClose();
440-
} catch (IOException ioe) {
441-
log.error(new StringBuffer().append("++++ failed to close socket : ").append(sock.toString())
442-
.toString());
438+
sock.sockets.invalidateObject(sock);
439+
} catch (Exception e1) {
440+
log.error("++++ failed to close socket : " + sock.toString());
443441
}
444442

445443
sock = null;
@@ -587,10 +585,9 @@ private long incrdecr(String cmdname, String key, long inc, Integer hashCode) {
587585
log.error(e.getMessage(), e);
588586

589587
try {
590-
sock.trueClose();
591-
} catch (IOException ioe) {
592-
log.error(new StringBuffer().append("++++ failed to close socket : ").append(sock.toString())
593-
.toString());
588+
sock.sockets.invalidateObject(sock);
589+
} catch (Exception e1) {
590+
log.error("++++ failed to close socket : " + sock.toString());
594591
}
595592

596593
sock = null;
@@ -779,8 +776,8 @@ private Object get(String cmd, String key, Integer hashCode, boolean asString) {
779776
log.error(ce.getMessage(), ce);
780777

781778
try {
782-
sock.trueClose();
783-
} catch (IOException e) {
779+
sock.sockets.invalidateObject(sock);
780+
} catch (Exception e1) {
784781
log.error("++++ failed to close socket : " + sock.toString());
785782
}
786783

@@ -923,8 +920,8 @@ public MemcachedItem gets(String cmd, String key, Integer hashCode, boolean asSt
923920
log.error(ce.getMessage(), ce);
924921

925922
try {
926-
sock.trueClose();
927-
} catch (IOException e) {
923+
sock.sockets.invalidateObject(sock);
924+
} catch (Exception e1) {
928925
log.error("++++ failed to close socket : " + sock.toString());
929926
}
930927

@@ -1093,8 +1090,8 @@ public Map<String, Object> getMulti(String[] keys, Integer[] hashCodes, boolean
10931090
}
10941091

10951092
// backfill missing keys w/ null value
1096-
// if (!ret.containsKey(keys[i]))
1097-
// ret.put(keys[i], null);
1093+
// if (!ret.containsKey(keys[i]))
1094+
// ret.put(keys[i], null);
10981095
}
10991096

11001097
if (log.isDebugEnabled())
@@ -1229,8 +1226,8 @@ public boolean flushAll(String[] servers) {
12291226
log.error(e.getMessage(), e);
12301227

12311228
try {
1232-
sock.trueClose();
1233-
} catch (IOException ioe) {
1229+
sock.sockets.invalidateObject(sock);
1230+
} catch (Exception e1) {
12341231
log.error("++++ failed to close socket : " + sock.toString());
12351232
}
12361233

@@ -1346,8 +1343,8 @@ private Map<String, Map<String, String>> stats(String[] servers, String command,
13461343
log.error(e.getMessage(), e);
13471344

13481345
try {
1349-
sock.trueClose();
1350-
} catch (IOException ioe) {
1346+
sock.sockets.invalidateObject(sock);
1347+
} catch (Exception e1) {
13511348
log.error("++++ failed to close socket : " + sock.toString());
13521349
}
13531350

@@ -1405,9 +1402,9 @@ public void close() {
14051402
}
14061403

14071404
try {
1408-
channel.close();
1409-
sock.trueClose();
1410-
} catch (IOException ignoreMe) {
1405+
sock.sockets.invalidateObject(sock);
1406+
} catch (Exception e1) {
1407+
log.error("++++ failed to close socket : " + sock.toString());
14111408
}
14121409
}
14131410

@@ -1623,10 +1620,9 @@ public boolean sync(String key, Integer hashCode) {
16231620
log.error(e.getMessage(), e);
16241621

16251622
try {
1626-
sock.trueClose();
1627-
} catch (IOException ioe) {
1628-
log.error(new StringBuffer().append("++++ failed to close socket : ").append(sock.toString())
1629-
.toString());
1623+
sock.sockets.invalidateObject(sock);
1624+
} catch (Exception e1) {
1625+
log.error("++++ failed to close socket : " + sock.toString());
16301626
}
16311627

16321628
sock = null;
@@ -1692,8 +1688,8 @@ public boolean syncAll(String[] servers) {
16921688
log.error(e.getMessage(), e);
16931689

16941690
try {
1695-
sock.trueClose();
1696-
} catch (IOException ioe) {
1691+
sock.sockets.invalidateObject(sock);
1692+
} catch (Exception e1) {
16971693
log.error("++++ failed to close socket : " + sock.toString());
16981694
}
16991695

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

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,9 @@ private boolean set(String cmdname, String key, Object value, Date expiry, Integ
286286
log.error(e.getMessage(), e);
287287

288288
try {
289-
sock.trueClose();
290-
} catch (IOException ioe) {
291-
log.error(new StringBuffer().append("++++ failed to close socket : ").append(sock.toString())
292-
.toString());
289+
sock.sockets.invalidateObject(sock);
290+
} catch (Exception e1) {
291+
log.error("++++ failed to close socket : " + sock.toString());
293292
}
294293
sock = null;
295294
} finally {
@@ -419,8 +418,8 @@ public boolean flushAll(String[] servers) {
419418
log.error(e.getMessage(), e);
420419

421420
try {
422-
sock.trueClose();
423-
} catch (IOException ioe) {
421+
sock.sockets.invalidateObject(sock);
422+
} catch (Exception e1) {
424423
log.error("++++ failed to close socket : " + sock.toString());
425424
}
426425

@@ -588,10 +587,9 @@ private MemcachedItem get(String cmd, String key, Integer hashCode) {
588587
return retrieval.response(sock, transCoder, rid);
589588
} catch (IOException e) {
590589
try {
591-
sock.trueClose();
592-
} catch (IOException ioe) {
593-
log.error(new StringBuffer().append("++++ failed to close socket : ").append(sock.toString())
594-
.toString());
590+
sock.sockets.invalidateObject(sock);
591+
} catch (Exception e1) {
592+
log.error("++++ failed to close socket : " + sock.toString());
595593
}
596594
sock = null;
597595
} finally {
@@ -676,10 +674,9 @@ public boolean sync(String key, Integer hashCode) {
676674
log.error(e.getMessage(), e);
677675

678676
try {
679-
sock.trueClose();
680-
} catch (IOException ioe) {
681-
log.error(new StringBuffer().append("++++ failed to close socket : ").append(sock.toString())
682-
.toString());
677+
sock.sockets.invalidateObject(sock);
678+
} catch (Exception e1) {
679+
log.error("++++ failed to close socket : " + sock.toString());
683680
}
684681

685682
sock = null;
@@ -742,8 +739,8 @@ public boolean syncAll(String[] servers) {
742739
log.error(e.getMessage(), e);
743740

744741
try {
745-
sock.trueClose();
746-
} catch (IOException ioe) {
742+
sock.sockets.invalidateObject(sock);
743+
} catch (Exception e1) {
747744
log.error("++++ failed to close socket : " + sock.toString());
748745
}
749746

@@ -789,10 +786,9 @@ public boolean delete(String key, Integer hashCode, Date expiry) {
789786
log.error("++++ exception thrown while writing bytes to server on delete");
790787
log.error(e.getMessage(), e);
791788
try {
792-
sock.trueClose();
793-
} catch (IOException ioe) {
794-
log.error(new StringBuffer().append("++++ failed to close socket : ").append(sock.toString())
795-
.toString());
789+
sock.sockets.invalidateObject(sock);
790+
} catch (Exception e1) {
791+
log.error("++++ failed to close socket : " + sock.toString());
796792
}
797793
sock = null;
798794
} finally {
@@ -868,10 +864,9 @@ private long incrdecr(String cmdname, String key, long inc, Integer hashCode) {
868864
log.error(e.getMessage(), e);
869865

870866
try {
871-
sock.trueClose();
872-
} catch (IOException ioe) {
873-
log.error(new StringBuffer().append("++++ failed to close socket : ").append(sock.toString())
874-
.toString());
867+
sock.sockets.invalidateObject(sock);
868+
} catch (Exception e1) {
869+
log.error("++++ failed to close socket : " + sock.toString());
875870
}
876871

877872
sock = null;
@@ -928,8 +923,8 @@ private Map<String, Map<String, String>> stats(String[] servers, String command,
928923
log.error(e.getMessage(), e);
929924

930925
try {
931-
sock.trueClose();
932-
} catch (IOException ioe) {
926+
sock.sockets.invalidateObject(sock);
927+
} catch (Exception e1) {
933928
log.error("++++ failed to close socket : " + sock.toString());
934929
}
935930

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

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,9 @@ public boolean delete(String key, Integer hashCode, Date expiry) {
203203
log.error(e.getMessage(), e);
204204

205205
try {
206-
sock.trueClose();
207-
} catch (IOException ioe) {
208-
log.error(new StringBuffer().append("++++ failed to close socket : ").append(sock.toString())
209-
.toString());
206+
sock.sockets.invalidateObject(sock);
207+
} catch (Exception e1) {
208+
log.error("++++ failed to close socket : " + sock.toString());
210209
}
211210

212211
sock = null;
@@ -433,10 +432,9 @@ private boolean set(byte opcode, String key, Object value, Date expiry, Integer
433432
log.error(e.getMessage(), e);
434433

435434
try {
436-
sock.trueClose();
437-
} catch (IOException ioe) {
438-
log.error(new StringBuffer().append("++++ failed to close socket : ").append(sock.toString())
439-
.toString());
435+
sock.sockets.invalidateObject(sock);
436+
} catch (Exception e1) {
437+
log.error("++++ failed to close socket : " + sock.toString());
440438
}
441439

442440
sock = null;
@@ -550,10 +548,9 @@ private boolean apPrepend(byte opcode, String key, Object value, Integer hashCod
550548
log.error(e.getMessage(), e);
551549

552550
try {
553-
sock.trueClose();
554-
} catch (IOException ioe) {
555-
log.error(new StringBuffer().append("++++ failed to close socket : ").append(sock.toString())
556-
.toString());
551+
sock.sockets.invalidateObject(sock);
552+
} catch (Exception e1) {
553+
log.error("++++ failed to close socket : " + sock.toString());
557554
}
558555

559556
sock = null;
@@ -716,10 +713,9 @@ private long incrdecr(byte opcode, String key, long inc, Integer hashCode) {
716713
log.error(e.getMessage(), e);
717714

718715
try {
719-
sock.trueClose();
720-
} catch (IOException ioe) {
721-
log.error(new StringBuffer().append("++++ failed to close socket : ").append(sock.toString())
722-
.toString());
716+
sock.sockets.invalidateObject(sock);
717+
} catch (Exception e1) {
718+
log.error("++++ failed to close socket : " + sock.toString());
723719
}
724720

725721
sock = null;
@@ -868,8 +864,8 @@ public Map<String, Object> getMulti(String[] keys, Integer[] hashCodes, boolean
868864
}
869865

870866
// backfill missing keys w/ null value
871-
// if (!ret.containsKey(keys[i]))
872-
// ret.put(keys[i], null);
867+
// if (!ret.containsKey(keys[i]))
868+
// ret.put(keys[i], null);
873869
}
874870

875871
if (log.isDebugEnabled())
@@ -1015,8 +1011,8 @@ public boolean flushAll(String[] servers) {
10151011
log.error(e.getMessage(), e);
10161012

10171013
try {
1018-
sock.trueClose();
1019-
} catch (IOException ioe) {
1014+
sock.sockets.invalidateObject(sock);
1015+
} catch (Exception e1) {
10201016
log.error("++++ failed to close socket : " + sock.toString());
10211017
}
10221018

@@ -1146,8 +1142,8 @@ private Map<String, Map<String, String>> stats(String[] servers, byte opcode, by
11461142
log.error(e.getMessage(), e);
11471143

11481144
try {
1149-
sock.trueClose();
1150-
} catch (IOException ioe) {
1145+
sock.sockets.invalidateObject(sock);
1146+
} catch (Exception e1) {
11511147
log.error("++++ failed to close socket : " + sock.toString());
11521148
}
11531149

@@ -1233,9 +1229,9 @@ public void close() {
12331229
}
12341230

12351231
try {
1236-
channel.close();
1237-
sock.trueClose();
1238-
} catch (IOException ignoreMe) {
1232+
sock.sockets.invalidateObject(sock);
1233+
} catch (Exception e1) {
1234+
log.error("++++ failed to close socket : " + sock.toString());
12391235
}
12401236
}
12411237

@@ -1543,10 +1539,9 @@ private Object get(byte opCode, String key, Integer hashCode, boolean asString)
15431539
log.error("++++ exception thrown while writing bytes to server on get");
15441540
log.error(e.getMessage(), e);
15451541
try {
1546-
sock.trueClose();
1547-
} catch (IOException ioe) {
1548-
log.error(new StringBuffer().append("++++ failed to close socket : ").append(sock.toString())
1549-
.toString());
1542+
sock.sockets.invalidateObject(sock);
1543+
} catch (Exception e1) {
1544+
log.error("++++ failed to close socket : " + sock.toString());
15501545
}
15511546
sock = null;
15521547
} catch (RuntimeException e) {
@@ -1661,10 +1656,9 @@ private MemcachedItem gets(byte opCode, String key, Integer hashCode, boolean as
16611656
log.error("++++ exception thrown while writing bytes to server on get");
16621657
log.error(e.getMessage(), e);
16631658
try {
1664-
sock.trueClose();
1665-
} catch (IOException ioe) {
1666-
log.error(new StringBuffer().append("++++ failed to close socket : ").append(sock.toString())
1667-
.toString());
1659+
sock.sockets.invalidateObject(sock);
1660+
} catch (Exception e1) {
1661+
log.error("++++ failed to close socket : " + sock.toString());
16681662
}
16691663
sock = null;
16701664
} finally {

0 commit comments

Comments
 (0)