Przeglądaj źródła

ZOOKEEPER-358. Throw exception when ledger does not exist

git-svn-id: https://svn.apache.org/repos/asf/hadoop/zookeeper/trunk@781173 13f79535-47bb-0310-9956-ffa450edef68
Benjamin Reed 16 lat temu
rodzic
commit
01aee31cd6

+ 2 - 0
CHANGES.txt

@@ -181,6 +181,8 @@ IMPROVEMENTS:
   include in release (phunt via breed)
 
   ZOOKEEPER-383. Asynchronous version of createLedger(). (flavio via mahadev)
+
+  ZOOKEEPER-358. Throw exception when ledger does not exist. (flavio via breed)
  
 NEW FEATURES:
 

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

@@ -48,6 +48,8 @@ public abstract class BKException extends Exception {
             return new BKDigestNotInitializedException();
         case Code.DigestMatchException:
             return new BKDigestMatchException();
+        case Code.NoSuchLedgerExistsException:
+            return new BKNoSuchLedgerExistsException();
         default:
             return new BKIllegalOpException();
         }
@@ -60,6 +62,7 @@ public abstract class BKException extends Exception {
         int NoBookieAvailableException = -3;
         int DigestNotInitializedException = -4;
         int DigestMatchException = -5;
+        int NoSuchLedgerExistsException = -6;
         
         int IllegalOpException = -100;
     }
@@ -86,6 +89,8 @@ public abstract class BKException extends Exception {
             return "Digest engine not initialized";
         case Code.DigestMatchException:
             return "Entry digest does not match";
+        case Code.NoSuchLedgerExistsException:
+            return "No such ledger exists";
         default:
             return "Invalid operation";
         }
@@ -126,5 +131,11 @@ public abstract class BKException extends Exception {
             super(Code.IllegalOpException);
         }   
     }
+    
+    public static class BKNoSuchLedgerExistsException extends BKException {
+        public BKNoSuchLedgerExistsException(){
+            super(Code.NoSuchLedgerExistsException);
+        }   
+    }
 }
     

+ 1 - 1
src/contrib/bookkeeper/src/java/org/apache/bookkeeper/client/BookKeeper.java

@@ -303,7 +303,7 @@ implements Watcher {
          */
         if(zk.exists(BKDefs.prefix + getZKStringId(lId), false) == null){
             LOG.error("Ledger " + getZKStringId(lId) + " doesn't exist.");
-            return null;
+            throw BKException.create(Code.NoSuchLedgerExistsException);
         }
         
         /*

+ 12 - 0
src/contrib/bookkeeper/test/org/apache/bookkeeper/test/BookieReadWriteTest.java

@@ -111,6 +111,18 @@ public class BookieReadWriteTest
 		}    	
     }
     
+    @Test
+    public void testOpenException() 
+    throws KeeperException, IOException, InterruptedException {
+        bkc = new BookKeeper("127.0.0.1");
+        try{
+            lh = bkc.openLedger(0, ledgerPassword);
+            fail("Haven't thrown exception");
+        } catch (BKException e) {
+            LOG.warn("Successfully thrown and caught exception:", e);
+        }
+    }
+    
     /**
      * test the streaming api for reading
      * and writing