zookeeper-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From iv...@apache.org
Subject svn commit: r1480978 - in /zookeeper/bookkeeper/trunk: CHANGES.txt bookkeeper-server/src/main/java/org/apache/bookkeeper/client/BookKeeperAdmin.java bookkeeper-server/src/test/java/org/apache/bookkeeper/client/ListLedgersTest.java
Date Fri, 10 May 2013 10:45:59 GMT
Author: ivank
Date: Fri May 10 10:45:58 2013
New Revision: 1480978

URL: http://svn.apache.org/r1480978
Log:
BOOKKEEPER-257: Ability to list all ledgers (fpj via ivank)

Added:
    zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/ListLedgersTest.java
Modified:
    zookeeper/bookkeeper/trunk/CHANGES.txt
    zookeeper/bookkeeper/trunk/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/BookKeeperAdmin.java

Modified: zookeeper/bookkeeper/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/trunk/CHANGES.txt?rev=1480978&r1=1480977&r2=1480978&view=diff
==============================================================================
--- zookeeper/bookkeeper/trunk/CHANGES.txt (original)
+++ zookeeper/bookkeeper/trunk/CHANGES.txt Fri May 10 10:45:58 2013
@@ -84,6 +84,8 @@ Trunk (unreleased changes)
 
       BOOKKEEPER-562: Ability to tell if a ledger is closed or not (fpj)
 
+      BOOKKEEPER-257: Ability to list all ledgers (fpj via ivank)
+
 Release 4.2.0 - 2013-01-14
 
   Non-backward compatible changes:

Modified: zookeeper/bookkeeper/trunk/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/BookKeeperAdmin.java
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/trunk/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/BookKeeperAdmin.java?rev=1480978&r1=1480977&r2=1480978&view=diff
==============================================================================
--- zookeeper/bookkeeper/trunk/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/BookKeeperAdmin.java
(original)
+++ zookeeper/bookkeeper/trunk/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/BookKeeperAdmin.java
Fri May 10 10:45:58 2013
@@ -24,9 +24,12 @@ import java.io.IOException;
 import java.net.InetSocketAddress;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.NoSuchElementException;
 import java.util.Random;
 import java.util.UUID;
 import java.util.Collection;
@@ -36,6 +39,8 @@ import org.apache.bookkeeper.client.Asyn
 import org.apache.bookkeeper.client.BookKeeper.SyncOpenCallback;
 import org.apache.bookkeeper.client.LedgerFragmentReplicator.SingleFragmentCallback;
 import org.apache.bookkeeper.conf.ClientConfiguration;
+import org.apache.bookkeeper.meta.LedgerManager.LedgerRange;
+import org.apache.bookkeeper.meta.LedgerManager.LedgerRangeIterator;
 import org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.MultiCallback;
 import org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.Processor;
 import org.apache.bookkeeper.util.BookKeeperConstants;
@@ -789,4 +794,62 @@ public class BookKeeperAdmin {
         }
         return true;
     }
+
+    /**
+     * This method returns an iterable object for the list of ledger identifiers of
+     * the ledgers currently available.
+     *
+     * @return an iterable object for the list of ledger identifiers
+     * @throws IOException  if the list of ledger identifiers cannot be read from the
+     *  metadata store
+     */
+    public Iterable<Long> listLedgers()
+    throws IOException {
+        final LedgerRangeIterator iterator = bkc.getLedgerManager().getLedgerRanges();
+        return new Iterable<Long>() {
+            public Iterator<Long> iterator() {
+                return new Iterator<Long>() {
+                    Iterator<Long> currentRange = null;
+
+                    @Override
+                    public boolean hasNext() {
+                        try {
+                            if (iterator.hasNext()) {
+                                return true;
+                            } else if (currentRange != null) {
+                                if (currentRange.hasNext()) {
+                                    return true;
+                                }
+                            }
+                        } catch (IOException e) {
+                            LOG.error("Error while checking if there is a next element",
e);
+                        }
+
+                        return false;
+                    }
+
+                    @Override
+                    public Long next()
+                    throws NoSuchElementException {
+                        try{
+                            if (currentRange == null) {
+                                currentRange = iterator.next().getLedgers().iterator();
+                            }
+                        } catch (IOException e) {
+                            LOG.error("Error while reading the next element", e);
+                            throw new NoSuchElementException(e.getMessage());
+                        }
+
+                        return currentRange.next();
+                    }
+
+                    @Override
+                    public void remove()
+                    throws UnsupportedOperationException {
+                        throw new UnsupportedOperationException();
+                    }
+                };
+            }
+        };
+    }
 }

Added: zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/ListLedgersTest.java
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/ListLedgersTest.java?rev=1480978&view=auto
==============================================================================
--- zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/ListLedgersTest.java
(added)
+++ zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/ListLedgersTest.java
Fri May 10 10:45:58 2013
@@ -0,0 +1,111 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package org.apache.bookkeeper.client;
+
+import java.util.Iterator;
+
+import org.apache.bookkeeper.client.BookKeeper.DigestType;
+import org.apache.bookkeeper.conf.ClientConfiguration;
+import org.apache.bookkeeper.test.BaseTestCase;
+
+import org.apache.zookeeper.KeeperException;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ListLedgersTest extends BaseTestCase {
+    static Logger LOG = LoggerFactory.getLogger(ListLedgersTest.class);
+
+    DigestType digestType;
+
+    public ListLedgersTest (DigestType digestType) {
+        super(4);
+        this.digestType = digestType;
+    }
+
+    @Test(timeout=60000)
+    public void testListLedgers()
+    throws Exception {
+        int numOfLedgers = 10;
+
+        ClientConfiguration conf = new ClientConfiguration()
+        .setZkServers(zkUtil.getZooKeeperConnectString());
+
+        BookKeeper bkc = new BookKeeper(conf);
+        for (int i = 0; i < numOfLedgers ; i++) {
+            bkc.createLedger(digestType, "testPasswd".
+                    getBytes()).close();
+        }
+
+        BookKeeperAdmin admin = new BookKeeperAdmin(zkUtil.
+                getZooKeeperConnectString());
+        Iterable<Long> iterable = admin.listLedgers();
+
+        int counter = 0;
+        for (Long lId: iterable) {
+            counter++;
+        }
+
+        Assert.assertTrue("Wrong number of ledgers: " + numOfLedgers,
+                counter == numOfLedgers);
+    }
+
+    @Test(timeout=60000)
+    public void testEmptyList()
+    throws Exception {
+        ClientConfiguration conf = new ClientConfiguration()
+        .setZkServers(zkUtil.getZooKeeperConnectString());
+
+        BookKeeperAdmin admin = new BookKeeperAdmin(zkUtil.
+                getZooKeeperConnectString());
+        Iterable<Long> iterable = admin.listLedgers();
+
+        Assert.assertFalse("There should be no ledger", iterable.iterator().hasNext());
+    }
+
+    @Test(timeout=60000)
+    public void testRemoveNotSupported()
+    throws Exception {
+        int numOfLedgers = 1;
+
+        ClientConfiguration conf = new ClientConfiguration()
+        .setZkServers(zkUtil.getZooKeeperConnectString());
+
+        BookKeeper bkc = new BookKeeper(conf);
+        for (int i = 0; i < numOfLedgers ; i++) {
+            bkc.createLedger(digestType, "testPasswd".
+                    getBytes()).close();
+        }
+
+        BookKeeperAdmin admin = new BookKeeperAdmin(zkUtil.
+                getZooKeeperConnectString());
+        Iterator<Long> iterator = admin.listLedgers().iterator();
+        iterator.next();
+        try{
+            iterator.remove();
+        } catch (UnsupportedOperationException e) {
+            // This exception is expected
+            return;
+        }
+
+        Assert.fail("Remove is not supported, we shouln't have reached this point");
+
+    }
+}



Mime
View raw message