bookkeeper-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From si...@apache.org
Subject bookkeeper git commit: BOOKKEEPER-892: Bookie sanity test
Date Tue, 09 Feb 2016 07:28:12 GMT
Repository: bookkeeper
Updated Branches:
  refs/heads/master 626b7e8bb -> 61390322e


BOOKKEEPER-892: Bookie sanity test

Author: Siddharth Boobna <sboobna@yahoo-inc.com>

Reviewers: Sijie Guo <sijie@apache.org>

Closes #13 from sid825/BOOKKEEPER-892


Project: http://git-wip-us.apache.org/repos/asf/bookkeeper/repo
Commit: http://git-wip-us.apache.org/repos/asf/bookkeeper/commit/61390322
Tree: http://git-wip-us.apache.org/repos/asf/bookkeeper/tree/61390322
Diff: http://git-wip-us.apache.org/repos/asf/bookkeeper/diff/61390322

Branch: refs/heads/master
Commit: 61390322ea591bcfe3186e289c27f331c86941ad
Parents: 626b7e8
Author: Siddharth Boobna <sboobna@yahoo-inc.com>
Authored: Mon Feb 8 23:28:09 2016 -0800
Committer: Sijie Guo <sijie@apache.org>
Committed: Mon Feb 8 23:28:09 2016 -0800

----------------------------------------------------------------------
 .../apache/bookkeeper/bookie/BookieShell.java   | 93 ++++++++++++++++++++
 .../LocalBookieEnsemblePlacementPolicy.java     | 71 +++++++++++++++
 2 files changed, 164 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/bookkeeper/blob/61390322/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java
----------------------------------------------------------------------
diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java
index 98d311b..fca2164 100644
--- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java
+++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java
@@ -29,6 +29,7 @@ import java.util.List;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.Collections;
+import java.util.Enumeration;
 import java.util.Collection;
 import java.util.concurrent.TimeUnit;
 
@@ -38,7 +39,9 @@ import org.apache.bookkeeper.bookie.EntryLogger.EntryLogScanner;
 import org.apache.bookkeeper.bookie.Journal.JournalScanner;
 import org.apache.bookkeeper.client.BKException;
 import org.apache.bookkeeper.client.BookKeeperAdmin;
+import org.apache.bookkeeper.client.LedgerEntry;
 import org.apache.bookkeeper.client.BookKeeper;
+import org.apache.bookkeeper.client.BookKeeper.DigestType;
 import org.apache.bookkeeper.client.LedgerHandle;
 import org.apache.bookkeeper.client.LedgerMetadata;
 import org.apache.bookkeeper.client.UpdateLedgerOp;
@@ -97,6 +100,7 @@ public class BookieShell implements Tool {
     static final String CMD_LISTUNDERREPLICATED = "listunderreplicated";
     static final String CMD_WHOISAUDITOR = "whoisauditor";
     static final String CMD_SIMPLETEST = "simpletest";
+    static final String CMD_BOOKIESANITYTEST = "bookiesanity";
     static final String CMD_READLOG = "readlog";
     static final String CMD_READJOURNAL = "readjournal";
     static final String CMD_LASTMARK = "lastmark";
@@ -684,6 +688,94 @@ public class BookieShell implements Tool {
     }
 
     /**
+     * Command to run a bookie sanity test
+     */
+    class BookieSanityTestCmd extends MyCommand {
+        Options lOpts = new Options();
+
+        BookieSanityTestCmd() {
+            super(CMD_BOOKIESANITYTEST);
+            lOpts.addOption("e", "entries", true, "Total entries to be added for the test
(default 10)");
+            lOpts.addOption("t", "timeout", true, "Timeout for write/read operations in seconds
(default 1)");
+        }
+
+        @Override
+        Options getOptions() {
+            return lOpts;
+        }
+
+        @Override
+        String getDescription() {
+            return "Sanity test for local bookie. Create ledger and write/reads entries on
local bookie.";
+        }
+
+        @Override
+        String getUsage() {
+            return "bookiesanity [-entries N] [-timeout N]";
+        }
+
+        @Override
+        int runCmd(CommandLine cmdLine) throws Exception {
+            int numberOfEntries = getOptionIntValue(cmdLine, "entries", 10);
+            int timeoutSecs= getOptionIntValue(cmdLine, "timeout", 1);
+
+            ClientConfiguration conf = new ClientConfiguration();
+            conf.addConfiguration(bkConf);
+            conf.setEnsemblePlacementPolicy(LocalBookieEnsemblePlacementPolicy.class);
+            conf.setAddEntryTimeout(timeoutSecs);
+            conf.setReadEntryTimeout(timeoutSecs);
+
+            BookKeeper bk = new BookKeeper(conf);
+            LedgerHandle lh = null;
+            try {
+                lh = bk.createLedger(1, 1, DigestType.MAC, new byte[0]);
+                LOG.info("Created ledger {}", lh.getId());
+
+                for (int i = 0; i < numberOfEntries; i++) {
+                    String content = "entry-" + i;
+                    lh.addEntry(content.getBytes());
+                }
+
+                LOG.info("Written {} entries in ledger {}", numberOfEntries, lh.getId());
+
+                // Reopen the ledger and read entries
+                lh = bk.openLedger(lh.getId(), DigestType.MAC, new byte[0]);
+                if (lh.getLastAddConfirmed() != (numberOfEntries - 1)) {
+                    throw new Exception("Invalid last entry found on ledger. expecting: "
+ (numberOfEntries - 1)
+                            + " -- found: " + lh.getLastAddConfirmed());
+                }
+
+                Enumeration<LedgerEntry> entries = lh.readEntries(0, numberOfEntries
- 1);
+                int i = 0;
+                while (entries.hasMoreElements()) {
+                    LedgerEntry entry = entries.nextElement();
+                    String actualMsg = new String(entry.getEntry());
+                    String expectedMsg = "entry-" + (i++);
+                    if (!expectedMsg.equals(actualMsg)) {
+                        throw new Exception("Failed validation of received message - Expected:
" + expectedMsg
+                                + ", Actual: " + actualMsg);
+                    }
+                }
+
+                LOG.info("Read {} entries from ledger {}", entries, lh.getId());
+            } catch (Exception e) {
+                LOG.warn("Error in bookie sanity test", e);
+                return -1;
+            } finally {
+                if (lh != null) {
+                    bk.deleteLedger(lh.getId());
+                    LOG.info("Deleted ledger {}", lh.getId());
+                }
+
+                bk.close();
+            }
+
+            LOG.info("Bookie sanity test succeeded");
+            return 0;
+        }
+    }
+
+    /**
      * Command to read entry log files.
      */
     class ReadLogCmd extends MyCommand {
@@ -1338,6 +1430,7 @@ public class BookieShell implements Tool {
         commands.put(CMD_WHOISAUDITOR, new WhoIsAuditorCmd());
         commands.put(CMD_LEDGERMETADATA, new LedgerMetadataCmd());
         commands.put(CMD_SIMPLETEST, new SimpleTestCmd());
+        commands.put(CMD_BOOKIESANITYTEST, new BookieSanityTestCmd());
         commands.put(CMD_READLOG, new ReadLogCmd());
         commands.put(CMD_READJOURNAL, new ReadJournalCmd());
         commands.put(CMD_LASTMARK, new LastMarkCmd());

http://git-wip-us.apache.org/repos/asf/bookkeeper/blob/61390322/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/LocalBookieEnsemblePlacementPolicy.java
----------------------------------------------------------------------
diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/LocalBookieEnsemblePlacementPolicy.java
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/LocalBookieEnsemblePlacementPolicy.java
new file mode 100644
index 0000000..d1c41dd
--- /dev/null
+++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/LocalBookieEnsemblePlacementPolicy.java
@@ -0,0 +1,71 @@
+package org.apache.bookkeeper.bookie;
+
+import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Set;
+
+import org.apache.bookkeeper.client.BKException;
+import org.apache.bookkeeper.client.EnsemblePlacementPolicy;
+import org.apache.bookkeeper.client.BKException.BKNotEnoughBookiesException;
+import org.apache.bookkeeper.conf.ServerConfiguration;
+import org.apache.bookkeeper.net.BookieSocketAddress;
+import org.apache.commons.configuration.Configuration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.collect.Lists;
+
+/**
+ * Special ensemble placement policy that always return local bookie. Only works with ledgers
with ensemble=1.
+ */
+public class LocalBookieEnsemblePlacementPolicy implements EnsemblePlacementPolicy {
+
+    static final Logger LOG = LoggerFactory.getLogger(LocalBookieEnsemblePlacementPolicy.class);
+
+    private BookieSocketAddress bookieAddress;
+
+    @Override
+    public EnsemblePlacementPolicy initialize(Configuration conf) {
+
+        // Configuration will have already the bookie configuration inserted
+        ServerConfiguration serverConf = new ServerConfiguration();
+        serverConf.addConfiguration(conf);
+
+        try {
+            bookieAddress = Bookie.getBookieAddress(serverConf);
+        } catch (UnknownHostException e) {
+            LOG.warn("Unable to get bookie address", e);
+            throw new RuntimeException(e);
+        }
+        return this;
+    }
+
+    @Override
+    public void uninitalize() {
+        // do nothing
+    }
+
+    @Override
+    public Set<BookieSocketAddress> onClusterChanged(Set<BookieSocketAddress>
writableBookies,
+            Set<BookieSocketAddress> readOnlyBookies) {
+        return Collections.emptySet();
+    }
+
+    @Override
+    public ArrayList<BookieSocketAddress> newEnsemble(int ensembleSize, int writeQuorumSize,
+            Set<BookieSocketAddress> excludeBookies) throws BKNotEnoughBookiesException
{
+        if (ensembleSize > 1) {
+            throw new IllegalArgumentException("Local ensemble policy can only return 1 bookie");
+        }
+
+        return Lists.newArrayList(bookieAddress);
+    }
+
+    @Override
+    public BookieSocketAddress replaceBookie(BookieSocketAddress bookieToReplace,
+            Set<BookieSocketAddress> excludeBookies) throws BKNotEnoughBookiesException
{
+        throw new BKNotEnoughBookiesException();
+    }
+
+}


Mime
View raw message