Skip to content
This repository
Browse code

Added a test to test socket issue with commons-pool-1.5.1

  • Loading branch information...
commit bbcb6e2ec7b5e7f1a44a24d6a92df7b764415164 1 parent dcec38a
bbansal authored June 30, 2009
94  test/integration/voldemort/CommonsPoolDeadlockTest.java
... ...
@@ -0,0 +1,94 @@
  1
+package voldemort;
  2
+
  3
+import java.util.Date;
  4
+import java.util.concurrent.ExecutorService;
  5
+import java.util.concurrent.Executors;
  6
+import java.util.concurrent.TimeUnit;
  7
+import java.util.concurrent.atomic.AtomicInteger;
  8
+
  9
+import org.apache.commons.pool.KeyedObjectPool;
  10
+import org.apache.commons.pool.KeyedPoolableObjectFactory;
  11
+import org.apache.commons.pool.impl.GenericKeyedObjectPool;
  12
+
  13
+/**
  14
+ * Commons-pool-1.5.1 is causing some socket exceptions at Linkedin duiring test
  15
+ * trial.<br>
  16
+ * The test scenario is
  17
+ * 
  18
+ * <br>
  19
+ * Client --> socket call to server A and B (preferred reads = 2) <br>
  20
+ * B is slow , A is fast to response <br>
  21
+ * Common-Pools-1.4 : Client starts seeing Socket exception from B but continue
  22
+ * to serve from A (As I expect) <br>
  23
+ * Common-Pools-1.5 : both A and B start throwing socket exception and client
  24
+ * see request failures.
  25
+ * 
  26
+ * @author bbansal
  27
+ * 
  28
+ */
  29
+public class CommonsPoolDeadlockTest {
  30
+
  31
+    public static void main(String[] args) throws InterruptedException {
  32
+        GenericKeyedObjectPool.Config config = new GenericKeyedObjectPool.Config();
  33
+        config.maxActive = 5;
  34
+        config.maxTotal = 10;
  35
+        config.maxIdle = 5;
  36
+        config.maxWait = 500;
  37
+        config.testOnBorrow = true;
  38
+        config.testOnReturn = true;
  39
+        config.whenExhaustedAction = GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK;
  40
+        KeyedPoolableObjectFactory objFactory = new DummyPoolableObjectFactory();
  41
+        final KeyedObjectPool pool = new GenericKeyedObjectPool(objFactory, config);
  42
+        final AtomicInteger completed = new AtomicInteger(0);
  43
+        ExecutorService executor = Executors.newFixedThreadPool(20);
  44
+        System.out.println("You should now see 20 quick checkouts in rapid succession:");
  45
+        for(int i = 0; i < 20; i++) {
  46
+            final String key = Integer.toString(i);
  47
+            executor.execute(new Runnable() {
  48
+
  49
+                public void run() {
  50
+                    try {
  51
+                        System.out.println(new Date() + " borrow " + key);
  52
+                        Object obj = pool.borrowObject(key);
  53
+                        if(Integer.parseInt(key) % 2 == 0) {
  54
+                            Thread.sleep(5000);
  55
+                        }
  56
+                        pool.returnObject(key, obj);
  57
+                        System.out.println(new Date() + " Returned " + key);
  58
+                    } catch(Exception e) {
  59
+                        synchronized(this) {
  60
+                            System.out.println("Exception for key:" + key);
  61
+                            e.printStackTrace();
  62
+                        }
  63
+                    } finally {
  64
+                        System.out.println(completed.incrementAndGet());
  65
+                    }
  66
+                }
  67
+            });
  68
+        }
  69
+
  70
+        executor.awaitTermination(10, TimeUnit.MINUTES);
  71
+        executor.shutdown();
  72
+        if(completed.get() == 20) {
  73
+            System.out.println("Test completed.");
  74
+        }
  75
+    }
  76
+
  77
+    private static class DummyPoolableObjectFactory implements KeyedPoolableObjectFactory {
  78
+
  79
+        public void activateObject(Object k, Object v) throws Exception {}
  80
+
  81
+        public void destroyObject(Object k, Object v) throws Exception {}
  82
+
  83
+        // build fast in each case
  84
+        public Object makeObject(Object k) throws Exception {
  85
+            return new Object();
  86
+        }
  87
+
  88
+        public void passivateObject(Object k, Object v) throws Exception {}
  89
+
  90
+        public boolean validateObject(Object k, Object v) {
  91
+            return true;
  92
+        }
  93
+    }
  94
+}

0 notes on commit bbcb6e2

Please sign in to comment.
Something went wrong with that request. Please try again.