You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@zookeeper.apache.org by iv...@apache.org on 2013/07/03 16:52:46 UTC
svn commit: r1499429 - in /zookeeper/bookkeeper/branches/branch-4.2:
CHANGES.txt
bookkeeper-server/src/main/java/org/apache/bookkeeper/client/BookKeeperAdmin.java
bookkeeper-server/src/test/java/org/apache/bookkeeper/client/ListLedgersTest.java
Author: ivank
Date: Wed Jul 3 14:52:46 2013
New Revision: 1499429
URL: http://svn.apache.org/r1499429
Log:
BOOKKEEPER-257: Ability to list all ledgers (fpj via ivank)
Added:
zookeeper/bookkeeper/branches/branch-4.2/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/ListLedgersTest.java
Modified:
zookeeper/bookkeeper/branches/branch-4.2/CHANGES.txt
zookeeper/bookkeeper/branches/branch-4.2/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/BookKeeperAdmin.java
Modified: zookeeper/bookkeeper/branches/branch-4.2/CHANGES.txt
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/branches/branch-4.2/CHANGES.txt?rev=1499429&r1=1499428&r2=1499429&view=diff
==============================================================================
--- zookeeper/bookkeeper/branches/branch-4.2/CHANGES.txt (original)
+++ zookeeper/bookkeeper/branches/branch-4.2/CHANGES.txt Wed Jul 3 14:52:46 2013
@@ -36,6 +36,8 @@ Release 4.2.2 - Unreleased
BOOKKEEPER-637: NoSuchEntry exception when reading an entry from a bookie should not print ERROR level message (mmerli via ivank)
+ BOOKKEEPER-257: Ability to list all ledgers (fpj via ivank)
+
hedwig-server:
BOOKKEEPER-579: TestSubAfterCloseSub was put in a wrong package (sijie via ivank)
Modified: zookeeper/bookkeeper/branches/branch-4.2/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/BookKeeperAdmin.java
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/branches/branch-4.2/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/BookKeeperAdmin.java?rev=1499429&r1=1499428&r2=1499429&view=diff
==============================================================================
--- zookeeper/bookkeeper/branches/branch-4.2/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/BookKeeperAdmin.java (original)
+++ zookeeper/bookkeeper/branches/branch-4.2/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/BookKeeperAdmin.java Wed Jul 3 14:52:46 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;
@@ -35,6 +38,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;
@@ -777,4 +782,63 @@ 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()) {
+ LOG.info("I'm in this part of");
+ 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/branches/branch-4.2/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/ListLedgersTest.java
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/branches/branch-4.2/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/ListLedgersTest.java?rev=1499429&view=auto
==============================================================================
--- zookeeper/bookkeeper/branches/branch-4.2/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/ListLedgersTest.java (added)
+++ zookeeper/bookkeeper/branches/branch-4.2/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/ListLedgersTest.java Wed Jul 3 14:52:46 2013
@@ -0,0 +1,112 @@
+/**
+ * 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();
+
+ LOG.info("Empty list assertion");
+ 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");
+
+ }
+}