Browse Source

ZOOKEEPER-500. Async methods shouldnt throw exceptions

git-svn-id: https://svn.apache.org/repos/asf/zookeeper/trunk@1055924 13f79535-47bb-0310-9956-ffa450edef68
Benjamin Reed 14 years ago
parent
commit
c93433291d

+ 2 - 0
CHANGES.txt

@@ -223,6 +223,8 @@ IMPROVEMENTS:
 
   ZOOKEEPER-963. Make Forrest work with	JDK6 (Carl Steinbach via henryr)
 
+  ZOOKEEPER-500. Async methods shouldnt throw exceptions (fpj via breed)
+
 NEW FEATURES:
   ZOOKEEPER-729. Java client API to recursively delete a subtree.
   (Kay Kay via henry)

+ 11 - 0
src/contrib/bookkeeper/src/java/org/apache/bookkeeper/client/BKException.java

@@ -72,6 +72,8 @@ public abstract class BKException extends Exception {
             return new BKNoSuchEntryException();
         case Code.IncorrectParameterException:
             return new BKIncorrectParameterException();
+        case Code.InterruptedException:
+            return new BKInterruptedException();
         default:
             return new BKIllegalOpException();
         }
@@ -97,6 +99,7 @@ public abstract class BKException extends Exception {
         int WriteException = -12;
         int NoSuchEntryException = -13;
         int IncorrectParameterException = -14;
+        int InterruptedException = -15;
         
         int IllegalOpException = -100;
     }
@@ -141,6 +144,8 @@ public abstract class BKException extends Exception {
             return "No such entry";
         case Code.IncorrectParameterException:
             return "Incorrect parameter input";
+        case Code.InterruptedException:
+            return "Interrupted while waiting for permit";
         default:
             return "Invalid operation";
         }
@@ -235,4 +240,10 @@ public abstract class BKException extends Exception {
             super(Code.IncorrectParameterException);
         }
     }
+    
+    public static class BKInterruptedException extends BKException {
+        public BKInterruptedException() {
+            super(Code.InterruptedException);
+        }
+    }
 }

+ 14 - 4
src/contrib/bookkeeper/src/java/org/apache/bookkeeper/client/LedgerHandle.java

@@ -277,7 +277,7 @@ public class LedgerHandle implements ReadCallback, AddCallback, CloseCallback {
    *          control object
    */
   public void asyncReadEntries(long firstEntry, long lastEntry,
-      ReadCallback cb, Object ctx) throws InterruptedException {
+      ReadCallback cb, Object ctx) {
     // Little sanity check
     if (firstEntry < 0 || lastEntry > lastAddConfirmed
         || firstEntry > lastEntry) {
@@ -285,7 +285,12 @@ public class LedgerHandle implements ReadCallback, AddCallback, CloseCallback {
       return;
     }
 
-    new PendingReadOp(this, firstEntry, lastEntry, cb, ctx).initiate();
+    try{
+        new PendingReadOp(this, firstEntry, lastEntry, cb, ctx).initiate();
+  
+    } catch (InterruptedException e) {
+        cb.readComplete(BKException.Code.InterruptedException, this, null, ctx);
+    }
   }
 
   /**
@@ -317,8 +322,13 @@ public class LedgerHandle implements ReadCallback, AddCallback, CloseCallback {
    *          some control object
    */
   public void asyncAddEntry(final byte[] data, final AddCallback cb,
-      final Object ctx) throws InterruptedException {
-      opCounterSem.acquire();
+      final Object ctx) {
+      try{
+          opCounterSem.acquire();
+      } catch (InterruptedException e) {
+          cb.addComplete(BKException.Code.InterruptedException,
+                  LedgerHandle.this, -1, ctx);
+      }
       
       try{
           bk.mainWorkerPool.submitOrdered(ledgerId, new SafeRunnable() {

+ 3 - 13
src/contrib/bookkeeper/src/java/org/apache/bookkeeper/client/LedgerRecoveryOp.java

@@ -117,13 +117,8 @@ class LedgerRecoveryOp implements ReadEntryCallback, ReadCallback, AddCallback {
      * Try to read past the last confirmed.
      */
     private void doRecoveryRead() {
-        try{
-            lh.lastAddConfirmed++;
-            lh.asyncReadEntries(lh.lastAddConfirmed, lh.lastAddConfirmed, this, null);
-        } catch (InterruptedException e) {
-            LOG.error("Interrupted while trying to read entry.", e);
-            Thread.currentThread().interrupt();
-        }
+        lh.lastAddConfirmed++;
+        lh.asyncReadEntries(lh.lastAddConfirmed, lh.lastAddConfirmed, this, null);
     }
 
     @Override
@@ -131,12 +126,7 @@ class LedgerRecoveryOp implements ReadEntryCallback, ReadCallback, AddCallback {
         // get back to prev value
         lh.lastAddConfirmed--;
         if (rc == BKException.Code.OK) {
-            try{
-                lh.asyncAddEntry(seq.nextElement().getEntry(), this, null);
-            } catch (InterruptedException e) {
-                LOG.error("Interrupted while adding entry.", e);
-                Thread.currentThread().interrupt();
-            }
+            lh.asyncAddEntry(seq.nextElement().getEntry(), this, null);
             return;
         }