zookeeper-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From f..@apache.org
Subject svn commit: r1479649 - in /zookeeper/bookkeeper/trunk: ./ bookkeeper-server/src/main/java/org/apache/bookkeeper/client/ bookkeeper-server/src/test/java/org/apache/bookkeeper/client/
Date Mon, 06 May 2013 17:32:45 GMT
Author: fpj
Date: Mon May  6 17:32:45 2013
New Revision: 1479649

URL: http://svn.apache.org/r1479649
Log:
BOOKKEEPER-562: Ability to tell if a ledger is closed or not (fpj)


Modified:
    zookeeper/bookkeeper/trunk/CHANGES.txt
    zookeeper/bookkeeper/trunk/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/AsyncCallback.java
    zookeeper/bookkeeper/trunk/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/BookKeeper.java
    zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BookKeeperTest.java

Modified: zookeeper/bookkeeper/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/trunk/CHANGES.txt?rev=1479649&r1=1479648&r2=1479649&view=diff
==============================================================================
--- zookeeper/bookkeeper/trunk/CHANGES.txt (original)
+++ zookeeper/bookkeeper/trunk/CHANGES.txt Mon May  6 17:32:45 2013
@@ -76,6 +76,10 @@ Trunk (unreleased changes)
 
       BOOKKEEPER-583: Read from a ReadOnlyBookie fails if index fileinfo is not in ledger
cache (vinay via sijie)
 
+    NEW FEATURE:
+
+      BOOKKEEPER-562: Ability to tell if a ledger is closed or not (fpj)
+
 Release 4.2.0 - 2013-01-14
 
   Non-backward compatible changes:

Modified: zookeeper/bookkeeper/trunk/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/AsyncCallback.java
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/trunk/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/AsyncCallback.java?rev=1479649&r1=1479648&r2=1479649&view=diff
==============================================================================
--- zookeeper/bookkeeper/trunk/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/AsyncCallback.java
(original)
+++ zookeeper/bookkeeper/trunk/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/AsyncCallback.java
Mon May  6 17:32:45 2013
@@ -136,4 +136,16 @@ public interface AsyncCallback {
          */
         void recoverComplete(int rc, Object ctx);
     }
+    
+    public interface IsClosedCallback {
+        /**
+         * Callback definition for isClosed operation
+         *
+         * @param rc
+         *          return code
+         * @param isClosed
+         *          true if ledger is closed
+         */
+        void isClosedComplete(int rc, boolean isClosed, Object ctx);
+    }
 }

Modified: zookeeper/bookkeeper/trunk/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/BookKeeper.java
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/trunk/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/BookKeeper.java?rev=1479649&r1=1479648&r2=1479649&view=diff
==============================================================================
--- zookeeper/bookkeeper/trunk/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/BookKeeper.java
(original)
+++ zookeeper/bookkeeper/trunk/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/BookKeeper.java
Mon May  6 17:32:45 2013
@@ -29,11 +29,13 @@ import java.util.concurrent.TimeUnit;
 import org.apache.bookkeeper.client.AsyncCallback.CreateCallback;
 import org.apache.bookkeeper.client.AsyncCallback.DeleteCallback;
 import org.apache.bookkeeper.client.AsyncCallback.OpenCallback;
+import org.apache.bookkeeper.client.AsyncCallback.IsClosedCallback;
 import org.apache.bookkeeper.client.BKException.Code;
 import org.apache.bookkeeper.conf.ClientConfiguration;
 import org.apache.bookkeeper.meta.LedgerManager;
 import org.apache.bookkeeper.meta.LedgerManagerFactory;
 import org.apache.bookkeeper.proto.BookieClient;
+import org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GenericCallback;
 import org.apache.bookkeeper.util.OrderedSafeExecutor;
 import org.apache.bookkeeper.util.ZkUtils;
 import org.apache.bookkeeper.zookeeper.ZooKeeperWatcherBase;
@@ -555,6 +557,68 @@ public class BookKeeper {
             throw BKException.create(counter.getrc());
         }
     }
+    
+    /**
+     * Check asynchronously whether the ledger with identifier <i>lId</i>
+     * has been closed.
+     * 
+     * @param lId   ledger identifier
+     * @param cb    callback method
+     */
+    public void asyncIsClosed(long lId, final IsClosedCallback cb, final Object ctx){
+        ledgerManager.readLedgerMetadata(lId, new GenericCallback<LedgerMetadata>(){
+            public void operationComplete(int rc, LedgerMetadata lm){
+                if (rc == BKException.Code.OK) {
+                    cb.isClosedComplete(rc, lm.isClosed(), ctx);
+                } else {
+                    cb.isClosedComplete(rc, false, ctx);
+                }
+            }
+        });
+    }
+    
+    /**
+     * Check whether the ledger with identifier <i>lId</i>
+     * has been closed.
+     * 
+     * @param lId
+     * @return boolean true if ledger has been closed
+     * @throws BKException
+     */
+    public boolean isClosed(long lId)
+    throws BKException, InterruptedException {
+        final class Result {
+            int rc;
+            boolean isClosed;
+            final CountDownLatch notifier = new CountDownLatch(1);
+        }
+
+        final Result result = new Result();
+
+        final IsClosedCallback cb = new IsClosedCallback(){
+            public void isClosedComplete(int rc, boolean isClosed, Object ctx){
+                    result.isClosed = isClosed;
+                    result.rc = rc;
+                    result.notifier.countDown();
+            }
+        };
+
+        /*
+         * Call asynchronous version of isClosed
+         */
+        asyncIsClosed(lId, cb, null);
+        
+        /*
+         * Wait for callback
+         */
+        result.notifier.await();
+        
+        if (result.rc != BKException.Code.OK) {
+            throw BKException.create(result.rc);
+        }
+        
+        return result.isClosed;
+    }
 
     /**
      * Shuts down client.

Modified: zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BookKeeperTest.java
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BookKeeperTest.java?rev=1479649&r1=1479648&r2=1479649&view=diff
==============================================================================
--- zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BookKeeperTest.java
(original)
+++ zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BookKeeperTest.java
Mon May  6 17:32:45 2013
@@ -218,4 +218,24 @@ public class BookKeeperTest extends Base
             assertTrue("Close was not successful", success.get());
         }
     }
+    
+    @Test(timeout=60000)
+    public void testIsClosed() throws Exception {
+        ClientConfiguration conf = new ClientConfiguration()
+        .setZkServers(zkUtil.getZooKeeperConnectString());
+
+        BookKeeper bkc = new BookKeeper(conf);
+        LedgerHandle lh = bkc.createLedger(digestType, "testPasswd".getBytes());
+        Long lId = lh.getId();
+
+        lh.addEntry("000".getBytes());
+        boolean result = bkc.isClosed(lId);
+        Assert.assertTrue("Ledger shouldn't be flagged as closed!",!result);
+
+        lh.close();
+        result = bkc.isClosed(lId);
+        Assert.assertTrue("Ledger should be flagged as closed!",result);
+
+        bkc.close();
+    }
 }



Mime
View raw message