zookeeper-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From f..@apache.org
Subject svn commit: r1488138 - in /zookeeper/bookkeeper/trunk: ./ bookkeeper-server/src/test/java/org/apache/bookkeeper/replication/ bookkeeper-server/src/test/java/org/apache/bookkeeper/test/
Date Fri, 31 May 2013 10:03:00 GMT
Author: fpj
Date: Fri May 31 10:02:59 2013
New Revision: 1488138

URL: http://svn.apache.org/r1488138
Log:
BOOKKEEPER-611: Speed up bookkeeper tests (ivank via fpj)


Modified:
    zookeeper/bookkeeper/trunk/CHANGES.txt
    zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/replication/AuditorLedgerCheckerTest.java
    zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/replication/AuditorPeriodicCheckTest.java
    zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/replication/TestLedgerUnderreplicationManager.java
    zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/replication/TestReplicationWorker.java
    zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookieJournalRollingTest.java
    zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookieReadWriteTest.java
    zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/LedgerDeleteTest.java

Modified: zookeeper/bookkeeper/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/trunk/CHANGES.txt?rev=1488138&r1=1488137&r2=1488138&view=diff
==============================================================================
--- zookeeper/bookkeeper/trunk/CHANGES.txt (original)
+++ zookeeper/bookkeeper/trunk/CHANGES.txt Fri May 31 10:02:59 2013
@@ -80,6 +80,8 @@ Trunk (unreleased changes)
 
       BOOKKEEPER-583: Read from a ReadOnlyBookie fails if index fileinfo is not in ledger
cache (vinay via sijie)
 
+      BOOKKEEPER-611: Speed up bookkeeper tests (ivank via fpj)
+
     NEW FEATURE:
 
       BOOKKEEPER-562: Ability to tell if a ledger is closed or not (fpj)

Modified: zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/replication/AuditorLedgerCheckerTest.java
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/replication/AuditorLedgerCheckerTest.java?rev=1488138&r1=1488137&r2=1488138&view=diff
==============================================================================
--- zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/replication/AuditorLedgerCheckerTest.java
(original)
+++ zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/replication/AuditorLedgerCheckerTest.java
Fri May 31 10:02:59 2013
@@ -30,9 +30,11 @@ import java.util.List;
 import java.util.Map;
 import java.util.Random;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
+import org.apache.bookkeeper.client.AsyncCallback.AddCallback;
 import org.apache.bookkeeper.client.BKException;
 import org.apache.bookkeeper.client.LedgerHandle;
 import org.apache.bookkeeper.client.BookKeeper.DigestType;
@@ -174,10 +176,7 @@ public class AuditorLedgerCheckerTest ex
         ledgerList.add(lh2.getId());
         LOG.debug("Created following ledgers : " + ledgerList);
 
-        // 2 is added to the latch, since after the ledger reformation, again
-        // the reformed bookie is stopped. So auditor will modify the zk
-        // underreplicated metadata
-        int count = ledgerList.size() + 2;
+        int count = ledgerList.size();
         final CountDownLatch underReplicaLatch = registerUrLedgerWatcher(count);
 
         int bkShutdownIndex = bs.size() - 1;
@@ -190,7 +189,7 @@ public class AuditorLedgerCheckerTest ex
 
         // grace period for publishing the bk-ledger
         LOG.debug("Waiting for ledgers to be marked as under replicated");
-        underReplicaLatch.await(5, TimeUnit.SECONDS);
+        assertTrue("latch should have completed", underReplicaLatch.await(5, TimeUnit.SECONDS));
         Map<Long, String> urLedgerData = getUrLedgerData(urLedgerList);
 
         assertEquals("Missed identifying under replicated ledgers", 2,
@@ -272,7 +271,7 @@ public class AuditorLedgerCheckerTest ex
         shutdownBookieList.add(shutdownBookie(bs.size() - 1));
 
         assertFalse("Ledger replication is not disabled!", urReplicaLatch
-                .await(5, TimeUnit.SECONDS));
+                .await(1, TimeUnit.SECONDS));
 
         // enabling ledger replication
         urLedgerMgr.enableLedgerReplication();
@@ -345,12 +344,25 @@ public class AuditorLedgerCheckerTest ex
 
     private void addEntry(int numEntriesToWrite, LedgerHandle lh)
             throws InterruptedException, BKException {
+        final CountDownLatch completeLatch = new CountDownLatch(numEntriesToWrite);
+        final AtomicInteger rc = new AtomicInteger(BKException.Code.OK);
+
         for (int i = 0; i < numEntriesToWrite; i++) {
             ByteBuffer entry = ByteBuffer.allocate(4);
             entry.putInt(rng.nextInt(Integer.MAX_VALUE));
             entry.position(0);
-            lh.addEntry(entry.array());
+            lh.asyncAddEntry(entry.array(), new AddCallback() {
+                    public void addComplete(int rc2, LedgerHandle lh, long entryId, Object
ctx) {
+                        rc.compareAndSet(BKException.Code.OK, rc2);
+                        completeLatch.countDown();
+                    }
+                }, null);
+        }
+        completeLatch.await();
+        if (rc.get() != BKException.Code.OK) {
+            throw BKException.create(rc.get());
         }
+
     }
 
     private Map<Long, String> getUrLedgerData(Set<Long> urLedgerList)

Modified: zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/replication/AuditorPeriodicCheckTest.java
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/replication/AuditorPeriodicCheckTest.java?rev=1488138&r1=1488137&r2=1488138&view=diff
==============================================================================
--- zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/replication/AuditorPeriodicCheckTest.java
(original)
+++ zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/replication/AuditorPeriodicCheckTest.java
Fri May 31 10:02:59 2013
@@ -27,6 +27,7 @@ import java.util.concurrent.atomic.Atomi
 import java.util.concurrent.CountDownLatch;
 import java.util.HashMap;
 import java.util.List;
+import java.util.ArrayList;
 import java.util.LinkedList;
 import java.io.File;
 import java.io.FileOutputStream;
@@ -39,6 +40,8 @@ import org.apache.bookkeeper.util.String
 import org.apache.bookkeeper.zookeeper.ZooKeeperWatcherBase;
 import org.apache.bookkeeper.client.BookKeeper.DigestType;
 import org.apache.bookkeeper.client.LedgerHandle;
+import org.apache.bookkeeper.client.BKException;
+import org.apache.bookkeeper.client.AsyncCallback.AddCallback;
 import org.apache.bookkeeper.conf.ServerConfiguration;
 import org.apache.bookkeeper.meta.LedgerManagerFactory;
 import org.apache.bookkeeper.meta.LedgerUnderreplicationManager;
@@ -66,7 +69,7 @@ public class AuditorPeriodicCheckTest ex
     private HashMap<String, AuditorElector> auditorElectors = new HashMap<String,
AuditorElector>();
     private List<ZooKeeper> zkClients = new LinkedList<ZooKeeper>();
 
-    private final static int CHECK_INTERVAL = 1000; // run every second
+    private final static int CHECK_INTERVAL = 100; // run every 100ms
 
     public AuditorPeriodicCheckTest() {
         super(3);
@@ -215,15 +218,32 @@ public class AuditorPeriodicCheckTest ex
         LedgerManagerFactory mFactory = LedgerManagerFactory.newLedgerManagerFactory(bsConfs.get(0),
zkc);
         final LedgerUnderreplicationManager underReplicationManager = mFactory.newLedgerUnderreplicationManager();
         final int numLedgers = 100;
+        final int numMsgs = 100;
+        final CountDownLatch completeLatch = new CountDownLatch(numMsgs*numLedgers);
+        final AtomicInteger rc = new AtomicInteger(BKException.Code.OK);
 
+        List<LedgerHandle> lhs = new ArrayList<LedgerHandle>();
         for (int i = 0; i < numLedgers; i++) {
             LedgerHandle lh = bkc.createLedger(3, 3, DigestType.CRC32, "passwd".getBytes());
-
+            lhs.add(lh);
             for (int j = 0; j < 100; j++) {
-                lh.addEntry("testdata".getBytes());
+                lh.asyncAddEntry("testdata".getBytes(), new AddCallback() {
+                        public void addComplete(int rc2, LedgerHandle lh, long entryId, Object
ctx) {
+                            rc.compareAndSet(BKException.Code.OK, rc2);
+                            completeLatch.countDown();
+                        }
+                    }, null);
             }
+        }
+        completeLatch.await();
+        if (rc.get() != BKException.Code.OK) {
+            throw BKException.create(rc.get());
+        }
+
+        for (LedgerHandle lh : lhs) {
             lh.close();
         }
+
         underReplicationManager.disableLedgerReplication();
 
         final AtomicInteger numReads = new AtomicInteger(0);

Modified: zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/replication/TestLedgerUnderreplicationManager.java
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/replication/TestLedgerUnderreplicationManager.java?rev=1488138&r1=1488137&r2=1488138&view=diff
==============================================================================
--- zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/replication/TestLedgerUnderreplicationManager.java
(original)
+++ zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/replication/TestLedgerUnderreplicationManager.java
Fri May 31 10:02:59 2013
@@ -186,7 +186,7 @@ public class TestLedgerUnderreplicationM
 
         Future<Long> f = getLedgerToReplicate(m);
         try {
-            f.get(5, TimeUnit.SECONDS);
+            f.get(1, TimeUnit.SECONDS);
             fail("Shouldn't be able to find a ledger to replicate");
         } catch (TimeoutException te) {
             // correct behaviour
@@ -218,7 +218,7 @@ public class TestLedgerUnderreplicationM
 
         f = getLedgerToReplicate(m2);
         try {
-            f.get(5, TimeUnit.SECONDS);
+            f.get(1, TimeUnit.SECONDS);
             fail("Shouldn't be able to find a ledger to replicate");
         } catch (TimeoutException te) {
             // correct behaviour
@@ -264,7 +264,7 @@ public class TestLedgerUnderreplicationM
 
         Future<Long> f = getLedgerToReplicate(m2);
         try {
-            f.get(5, TimeUnit.SECONDS);
+            f.get(1, TimeUnit.SECONDS);
             fail("Shouldn't be able to find a ledger to replicate");
         } catch (TimeoutException te) {
             // correct behaviour
@@ -308,7 +308,7 @@ public class TestLedgerUnderreplicationM
 
         Future<Long> f = getLedgerToReplicate(m2);
         try {
-            f.get(5, TimeUnit.SECONDS);
+            f.get(1, TimeUnit.SECONDS);
             fail("Shouldn't be able to find a ledger to replicate");
         } catch (TimeoutException te) {
             // correct behaviour
@@ -388,7 +388,7 @@ public class TestLedgerUnderreplicationM
 
         Future<Long> f = getLedgerToReplicate(m2);
         try {
-            f.get(5, TimeUnit.SECONDS);
+            f.get(1, TimeUnit.SECONDS);
             fail("Shouldn't be able to find a ledger to replicate");
         } catch (TimeoutException te) {
             // correct behaviour
@@ -506,7 +506,7 @@ public class TestLedgerUnderreplicationM
 
         Future<Long> fA = getLedgerToReplicate(replicaMgr);
         try {
-            fA.get(5, TimeUnit.SECONDS);
+            fA.get(1, TimeUnit.SECONDS);
             fail("Shouldn't be able to find a ledger to replicate");
         } catch (TimeoutException te) {
             // expected behaviour, as the replication is disabled
@@ -522,7 +522,7 @@ public class TestLedgerUnderreplicationM
      * should continue getLedgerToRereplicate() task
      */
     @Test(timeout = 20000)
-    public void testEnableLedegerReplication() throws Exception {
+    public void testEnableLedgerReplication() throws Exception {
         isLedgerReplicationDisabled = true;
         final LedgerUnderreplicationManager replicaMgr = lmf1
                 .newLedgerUnderreplicationManager();
@@ -574,7 +574,7 @@ public class TestLedgerUnderreplicationM
         thread1.start();
 
         try {
-            znodeLatch.await(5, TimeUnit.SECONDS);
+            assertFalse("shouldn't complete", znodeLatch.await(1, TimeUnit.SECONDS));
             assertTrue("Ledger replication is not disabled!",
                     isLedgerReplicationDisabled);
             assertEquals("Failed to disable ledger replication!", 2, znodeLatch
@@ -645,7 +645,7 @@ public class TestLedgerUnderreplicationM
         final LedgerUnderreplicationManager replicaMgr2 = lmf2
             .newLedgerUnderreplicationManager();
 
-        final int iterations = 1000;
+        final int iterations = 100;
         final AtomicBoolean threadFailed = new AtomicBoolean(false);
         Thread markUnder = new Thread() {
                 public void run() {

Modified: zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/replication/TestReplicationWorker.java
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/replication/TestReplicationWorker.java?rev=1488138&r1=1488137&r2=1488138&view=diff
==============================================================================
--- zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/replication/TestReplicationWorker.java
(original)
+++ zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/replication/TestReplicationWorker.java
Fri May 31 10:02:59 2013
@@ -173,7 +173,7 @@ public class TestReplicationWorker exten
         try {
             underReplicationManager.markLedgerUnderreplicated(lh.getId(),
                     replicaToKill.toString());
-            int counter = 100;
+            int counter = 30;
             while (counter-- > 0) {
                 assertTrue("Expecting that replication should not complete",
                         ReplicationTestUtil.isLedgerInUnderReplication(zkc, lh
@@ -398,6 +398,8 @@ public class TestReplicationWorker exten
                 .getLocalHost().getHostAddress(), startNewBookie);
         LOG.info("New Bookie addr :" + newBkAddr);
 
+        // set to 3s instead of default 30s
+        baseConf.setOpenLedgerRereplicationGracePeriod("3000");
         ReplicationWorker rw = new ReplicationWorker(zkc, baseConf, newBkAddr);
 
         LedgerManagerFactory mFactory = LedgerManagerFactory

Modified: zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookieJournalRollingTest.java
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookieJournalRollingTest.java?rev=1488138&r1=1488137&r2=1488138&view=diff
==============================================================================
--- zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookieJournalRollingTest.java
(original)
+++ zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookieJournalRollingTest.java
Fri May 31 10:02:59 2013
@@ -22,14 +22,17 @@ package org.apache.bookkeeper.test;
  */
 import java.io.File;
 import java.util.Enumeration;
-import java.util.List;
+
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.CountDownLatch;
 
 import org.apache.bookkeeper.conf.ServerConfiguration;
 import org.apache.bookkeeper.bookie.Bookie;
 import org.apache.bookkeeper.client.LedgerEntry;
 import org.apache.bookkeeper.client.LedgerHandle;
 import org.apache.bookkeeper.client.BookKeeper.DigestType;
-import org.apache.bookkeeper.proto.BookieServer;
+import org.apache.bookkeeper.client.BKException;
+import org.apache.bookkeeper.client.AsyncCallback.AddCallback;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -48,7 +51,7 @@ public class BookieJournalRollingTest ex
     DigestType digestType;
 
     public BookieJournalRollingTest() {
-        super(3);
+        super(1);
         this.digestType = DigestType.CRC32;
     }
 
@@ -75,7 +78,7 @@ public class BookieJournalRollingTest ex
         LedgerHandle[] lhs = new LedgerHandle[numLedgers];
         long[] ledgerIds = new long[numLedgers];
         for (int i = 0; i < numLedgers; i++) {
-            lhs[i] = bkc.createLedger(digestType, "".getBytes());
+            lhs[i] = bkc.createLedger(1, 1, digestType, "".getBytes());
             ledgerIds[i] = lhs[i].getId();
         }
         writeLedgerEntries(lhs, msgSize, numMsgs);
@@ -91,15 +94,27 @@ public class BookieJournalRollingTest ex
         }
         String msg = msgSB.toString();
 
+        final CountDownLatch completeLatch = new CountDownLatch(numMsgs*lhs.length);
+        final AtomicInteger rc = new AtomicInteger(BKException.Code.OK);
+
         // Write all of the entries for all of the ledgers
         for (int i = 0; i < numMsgs; i++) {
             for (int j = 0; j < lhs.length; j++) {
                 StringBuilder sb = new StringBuilder();
                 sb.append(lhs[j].getId()).append('-').append(i).append('-')
                   .append(msg);
-                lhs[j].addEntry(sb.toString().getBytes());
+                lhs[j].asyncAddEntry(sb.toString().getBytes(), new AddCallback() {
+                        public void addComplete(int rc2, LedgerHandle lh, long entryId, Object
ctx) {
+                            rc.compareAndSet(BKException.Code.OK, rc2);
+                            completeLatch.countDown();
+                        }
+                    }, null);
             }
         }
+        completeLatch.await();
+        if (rc.get() != BKException.Code.OK) {
+            throw BKException.create(rc.get());
+        }
     }
 
     private void validLedgerEntries(long[] ledgerIds, int msgSize, int numMsgs) throws Exception
{
@@ -163,6 +178,7 @@ public class BookieJournalRollingTest ex
         long[] ledgerIds = new long[lhs.length];
         for (int i=0; i<lhs.length; i++) {
             ledgerIds[i] = lhs[i].getId();
+            lhs[i].close();
         }
 
         // Sleep for a while to ensure data are flushed
@@ -210,6 +226,7 @@ public class BookieJournalRollingTest ex
         long[] ledgerIds = new long[lhs.length];
         for (int i=0; i<lhs.length; i++) {
             ledgerIds[i] = lhs[i].getId();
+            lhs[i].close();
         }
 
         // ledger indexes are not flushed

Modified: zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookieReadWriteTest.java
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookieReadWriteTest.java?rev=1488138&r1=1488137&r2=1488138&view=diff
==============================================================================
--- zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookieReadWriteTest.java
(original)
+++ zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookieReadWriteTest.java
Fri May 31 10:02:59 2013
@@ -34,6 +34,7 @@ import java.util.Random;
 import java.util.Set;
 import java.util.Arrays;
 import java.util.concurrent.Semaphore;
+import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.bookkeeper.client.AsyncCallback.AddCallback;
@@ -541,8 +542,20 @@ public class BookieReadWriteTest extends
             // bkc.initMessageDigest("SHA1");
             ledgerId = lh.getId();
             LOG.info("Ledger ID: " + lh.getId());
+            final CountDownLatch completeLatch = new CountDownLatch(numEntriesToWrite);
+            final AtomicInteger rc = new AtomicInteger(BKException.Code.OK);
+
             for (int i = 0; i < numEntriesToWrite; i++) {
-                lh.addEntry(new byte[0]);
+                lh.asyncAddEntry(new byte[0], new AddCallback() {
+                        public void addComplete(int rccb, LedgerHandle lh, long entryId,
Object ctx) {
+                            rc.compareAndSet(BKException.Code.OK, rccb);
+                            completeLatch.countDown();
+                        }
+                    }, null);
+            }
+            completeLatch.await();
+            if (rc.get() != BKException.Code.OK) {
+                throw BKException.create(rc.get());
             }
 
             /*
@@ -587,11 +600,28 @@ public class BookieReadWriteTest extends
             long ledgerId = lh.getId();
             long ledgerId2 = lh2.getId();
 
+            final CountDownLatch completeLatch = new CountDownLatch(numEntriesToWrite*2);
+            final AtomicInteger rc = new AtomicInteger(BKException.Code.OK);
+
             // bkc.initMessageDigest("SHA1");
             LOG.info("Ledger ID 1: " + lh.getId() + ", Ledger ID 2: " + lh2.getId());
             for (int i = 0; i < numEntriesToWrite; i++) {
-                lh.addEntry(new byte[0]);
-                lh2.addEntry(new byte[0]);
+                lh.asyncAddEntry(new byte[0], new AddCallback() {
+                        public void addComplete(int rc2, LedgerHandle lh, long entryId, Object
ctx) {
+                            rc.compareAndSet(BKException.Code.OK, rc2);
+                            completeLatch.countDown();
+                        }
+                    }, null);
+                lh2.asyncAddEntry(new byte[0], new AddCallback() {
+                        public void addComplete(int rc2, LedgerHandle lh, long entryId, Object
ctx) {
+                            rc.compareAndSet(BKException.Code.OK, rc2);
+                            completeLatch.countDown();
+                        }
+                    }, null);
+            }
+            completeLatch.await();
+            if (rc.get() != BKException.Code.OK) {
+                throw BKException.create(rc.get());
             }
 
             lh.close();
@@ -684,49 +714,81 @@ public class BookieReadWriteTest extends
         }
     }
 
+    private long writeNEntriesLastWriteSync(LedgerHandle lh, int numToWrite) throws Exception
{
+        final CountDownLatch completeLatch = new CountDownLatch(numToWrite - 1);
+        final AtomicInteger rc = new AtomicInteger(BKException.Code.OK);
+
+        ByteBuffer entry = ByteBuffer.allocate(4);
+        for (int i = 0; i < numToWrite - 1; i++) {
+            entry = ByteBuffer.allocate(4);
+            entry.putInt(rng.nextInt(maxInt));
+            entry.position(0);
+
+            entries.add(entry.array());
+            entriesSize.add(entry.array().length);
+            lh.asyncAddEntry(entry.array(), new AddCallback() {
+                    public void addComplete(int rccb, LedgerHandle lh, long entryId, Object
ctx) {
+                        rc.compareAndSet(BKException.Code.OK, rccb);
+                        completeLatch.countDown();
+                    }
+                }, null);
+        }
+        completeLatch.await();
+        if (rc.get() != BKException.Code.OK) {
+            throw BKException.create(rc.get());
+        }
+
+        entry = ByteBuffer.allocate(4);
+        entry.putInt(rng.nextInt(maxInt));
+        entry.position(0);
+
+        entries.add(entry.array());
+        entriesSize.add(entry.array().length);
+        lh.addEntry(entry.array());
+        return lh.getLastAddConfirmed();
+    }
+
     @Test(timeout=60000)
-    public void testReadFromOpenLedger() throws IOException {
+    public void testReadFromOpenLedger() throws Exception {
         try {
             // Create a ledger
             lh = bkc.createLedger(digestType, ledgerPassword);
             // bkc.initMessageDigest("SHA1");
             ledgerId = lh.getId();
             LOG.info("Ledger ID: " + lh.getId());
-            for (int i = 0; i < numEntriesToWrite; i++) {
+
+            long lac = writeNEntriesLastWriteSync(lh, numEntriesToWrite);
+
+            LedgerHandle lhOpen = bkc.openLedgerNoRecovery(ledgerId, digestType, ledgerPassword);
+            // no recovery opened ledger 's last confirmed entry id is less than written
+            // and it just can read until (i-1)
+            long toRead = lac - 1;
+
+            Enumeration<LedgerEntry> readEntry = lhOpen.readEntries(toRead, toRead);
+            assertTrue("Enumeration of ledger entries has no element", readEntry.hasMoreElements()
== true);
+            LedgerEntry e = readEntry.nextElement();
+            assertEquals(toRead, e.getEntryId());
+            Assert.assertArrayEquals(entries.get((int)toRead), e.getEntry());
+            // should not written to a read only ledger
+            try {
                 ByteBuffer entry = ByteBuffer.allocate(4);
                 entry.putInt(rng.nextInt(maxInt));
                 entry.position(0);
 
-                entries.add(entry.array());
-                entriesSize.add(entry.array().length);
-                lh.addEntry(entry.array());
-                if(i == numEntriesToWrite/2) {
-                    LedgerHandle lhOpen = bkc.openLedgerNoRecovery(ledgerId, digestType,
ledgerPassword);
-                    // no recovery opened ledger 's last confirmed entry id is less than
written
-                    // and it just can read until (i-1)
-                    int toRead = i - 1;
-                    Enumeration<LedgerEntry> readEntry = lhOpen.readEntries(toRead,
toRead);
-                    assertTrue("Enumeration of ledger entries has no element", readEntry.hasMoreElements()
== true);
-                    LedgerEntry e = readEntry.nextElement();
-                    assertEquals(toRead, e.getEntryId());
-                    Assert.assertArrayEquals(entries.get(toRead), e.getEntry());
-                    // should not written to a read only ledger
-                    try {
-                        lhOpen.addEntry(entry.array());
-                        fail("Should have thrown an exception here");
-                    } catch (BKException.BKIllegalOpException bkioe) {
-                        // this is the correct response
-                    } catch (Exception ex) {
-                        LOG.error("Unexpected exception", ex);
-                        fail("Unexpected exception");
-                    }
-                    // close read only ledger should not change metadata
-                    lhOpen.close();
-                }
+                lhOpen.addEntry(entry.array());
+                fail("Should have thrown an exception here");
+            } catch (BKException.BKIllegalOpException bkioe) {
+                // this is the correct response
+            } catch (Exception ex) {
+                LOG.error("Unexpected exception", ex);
+                fail("Unexpected exception");
             }
+            // close read only ledger should not change metadata
+            lhOpen.close();
 
-            long last = lh.readLastConfirmed();
-            assertTrue("Last confirmed add: " + last, last == (numEntriesToWrite - 2));
+            lac = writeNEntriesLastWriteSync(lh, numEntriesToWrite);
+
+            assertEquals("Last confirmed add: ", lac, (numEntriesToWrite * 2) - 1);
 
             LOG.debug("*** WRITE COMPLETE ***");
             // close ledger
@@ -737,17 +799,8 @@ public class BookieReadWriteTest extends
             lh = bkc.createLedger(digestType, ledgerPassword);
             // bkc.initMessageDigest("SHA1");
             ledgerId = lh.getId();
-            LOG.info("Ledger ID: " + lh.getId());
-            for (int i = 0; i < numEntriesToWrite; i++) {
-                ByteBuffer entry = ByteBuffer.allocate(4);
-                entry.putInt(rng.nextInt(maxInt));
-                entry.position(0);
-
-                entries.add(entry.array());
-                entriesSize.add(entry.array().length);
-                lh.addEntry(entry.array());
-            }
 
+            writeNEntriesLastWriteSync(lh, numEntriesToWrite);
 
             SyncObj sync = new SyncObj();
             lh.asyncReadLastConfirmed(this, sync);
@@ -761,7 +814,7 @@ public class BookieReadWriteTest extends
                 assertEquals("Error reading", BKException.Code.OK, sync.getReturnCode());
             }
 
-            assertTrue("Last confirmed add: " + sync.lastConfirmed, sync.lastConfirmed ==
(numEntriesToWrite - 2));
+            assertEquals("Last confirmed add", sync.lastConfirmed, (numEntriesToWrite - 2));
 
             LOG.debug("*** WRITE COMPLETE ***");
             // close ledger
@@ -784,40 +837,36 @@ public class BookieReadWriteTest extends
             ledgerId = lh.getId();
             LOG.info("Ledger ID: " + lh.getId());
             LedgerHandle lhOpen = bkc.openLedgerNoRecovery(ledgerId, digestType, ledgerPassword);
-            for (int i = 0; i < numEntriesToWrite; i++) {
-                ByteBuffer entry = ByteBuffer.allocate(4);
-                entry.putInt(rng.nextInt(maxInt));
-                entry.position(0);
+            writeNEntriesLastWriteSync(lh, numEntriesToWrite/2);
 
-                entries.add(entry.array());
-                entriesSize.add(entry.array().length);
-                lh.addEntry(entry.array());
-                if (i == numEntriesToWrite / 2) {
-                    // no recovery opened ledger 's last confirmed entry id is
-                    // less than written
-                    // and it just can read until (i-1)
-                    int toRead = i - 1;
-
-                    long readLastConfirmed = lhOpen.readLastConfirmed();
-                    assertTrue(readLastConfirmed != 0);
-                    Enumeration<LedgerEntry> readEntry = lhOpen.readEntries(toRead,
toRead);
-                    assertTrue("Enumeration of ledger entries has no element", readEntry.hasMoreElements()
== true);
-                    LedgerEntry e = readEntry.nextElement();
-                    assertEquals(toRead, e.getEntryId());
-                    Assert.assertArrayEquals(entries.get(toRead), e.getEntry());
-                    // should not written to a read only ledger
-                    try {
-                        lhOpen.addEntry(entry.array());
-                        fail("Should have thrown an exception here");
-                    } catch (BKException.BKIllegalOpException bkioe) {
-                        // this is the correct response
-                    } catch (Exception ex) {
-                        LOG.error("Unexpected exception", ex);
-                        fail("Unexpected exception");
-                    }
+            ByteBuffer entry = ByteBuffer.allocate(4);
+            entry.putInt(rng.nextInt(maxInt));
+            entry.position(0);
 
-                }
+            // no recovery opened ledger 's last confirmed entry id is
+            // less than written
+            // and it just can read until (i-1)
+            int toRead = numEntriesToWrite/2 - 2;
+
+            long readLastConfirmed = lhOpen.readLastConfirmed();
+            assertTrue(readLastConfirmed != 0);
+            Enumeration<LedgerEntry> readEntry = lhOpen.readEntries(toRead, toRead);
+            assertTrue("Enumeration of ledger entries has no element", readEntry.hasMoreElements()
== true);
+            LedgerEntry e = readEntry.nextElement();
+            assertEquals(toRead, e.getEntryId());
+            Assert.assertArrayEquals(entries.get(toRead), e.getEntry());
+            // should not written to a read only ledger
+            try {
+                lhOpen.addEntry(entry.array());
+                fail("Should have thrown an exception here");
+            } catch (BKException.BKIllegalOpException bkioe) {
+                // this is the correct response
+            } catch (Exception ex) {
+                LOG.error("Unexpected exception", ex);
+                fail("Unexpected exception");
             }
+            writeNEntriesLastWriteSync(lh, numEntriesToWrite/2);
+
             long last = lh.readLastConfirmed();
             assertTrue("Last confirmed add: " + last, last == (numEntriesToWrite - 2));
 
@@ -904,23 +953,15 @@ public class BookieReadWriteTest extends
 
 
     @Test(timeout=60000)
-    public void testLastConfirmedAdd() throws IOException {
+    public void testLastConfirmedAdd() throws Exception {
         try {
             // Create a ledger
             lh = bkc.createLedger(digestType, ledgerPassword);
             // bkc.initMessageDigest("SHA1");
             ledgerId = lh.getId();
             LOG.info("Ledger ID: " + lh.getId());
-            for (int i = 0; i < numEntriesToWrite; i++) {
-                ByteBuffer entry = ByteBuffer.allocate(4);
-                entry.putInt(rng.nextInt(maxInt));
-                entry.position(0);
-
-                entries.add(entry.array());
-                entriesSize.add(entry.array().length);
-                lh.addEntry(entry.array());
-            }
 
+            writeNEntriesLastWriteSync(lh, numEntriesToWrite);
             long last = lh.readLastConfirmed();
             assertTrue("Last confirmed add: " + last, last == (numEntriesToWrite - 2));
 
@@ -934,16 +975,7 @@ public class BookieReadWriteTest extends
             // bkc.initMessageDigest("SHA1");
             ledgerId = lh.getId();
             LOG.info("Ledger ID: " + lh.getId());
-            for (int i = 0; i < numEntriesToWrite; i++) {
-                ByteBuffer entry = ByteBuffer.allocate(4);
-                entry.putInt(rng.nextInt(maxInt));
-                entry.position(0);
-
-                entries.add(entry.array());
-                entriesSize.add(entry.array().length);
-                lh.addEntry(entry.array());
-            }
-
+            writeNEntriesLastWriteSync(lh, numEntriesToWrite);
 
             SyncObj sync = new SyncObj();
             lh.asyncReadLastConfirmed(this, sync);

Modified: zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/LedgerDeleteTest.java
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/LedgerDeleteTest.java?rev=1488138&r1=1488137&r2=1488138&view=diff
==============================================================================
--- zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/LedgerDeleteTest.java
(original)
+++ zookeeper/bookkeeper/trunk/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/LedgerDeleteTest.java
Fri May 31 10:02:59 2013
@@ -22,7 +22,11 @@ package org.apache.bookkeeper.test;
  */
 
 import java.io.File;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.CountDownLatch;
+import org.apache.bookkeeper.client.BKException;
 import org.apache.bookkeeper.client.LedgerHandle;
+import org.apache.bookkeeper.client.AsyncCallback.AddCallback;
 import org.apache.bookkeeper.client.BookKeeper.DigestType;
 import org.apache.bookkeeper.util.TestUtils;
 import org.slf4j.Logger;
@@ -39,7 +43,7 @@ public class LedgerDeleteTest extends Mu
     DigestType digestType;
 
     public LedgerDeleteTest(String ledgerManagerFactory) {
-        super(3);
+        super(1);
         LOG.info("Running test case using ledger manager : " + ledgerManagerFactory);
         this.digestType = DigestType.CRC32;
         // set ledger manager name
@@ -63,7 +67,7 @@ public class LedgerDeleteTest extends Mu
         // Create the ledgers
         LedgerHandle[] lhs = new LedgerHandle[numLedgers];
         for (int i = 0; i < numLedgers; i++) {
-            lhs[i] = bkc.createLedger(digestType, "".getBytes());
+            lhs[i] = bkc.createLedger(1, 1, digestType, "".getBytes());
         }
 
         // Create a dummy message string to write as ledger entries
@@ -72,13 +76,23 @@ public class LedgerDeleteTest extends Mu
             msgSB.append("a");
         }
         String msg = msgSB.toString();
-
+        final CountDownLatch completeLatch = new CountDownLatch(numMsgs*numLedgers);
+        final AtomicInteger rc = new AtomicInteger(BKException.Code.OK);
         // Write all of the entries for all of the ledgers
         for (int i = 0; i < numMsgs; i++) {
             for (int j = 0; j < numLedgers; j++) {
-                lhs[j].addEntry(msg.getBytes());
+                lhs[j].asyncAddEntry(msg.getBytes(), new AddCallback() {
+                    public void addComplete(int rc2, LedgerHandle lh, long entryId, Object
ctx) {
+                        rc.compareAndSet(BKException.Code.OK, rc2);
+                        completeLatch.countDown();
+                    }
+                }, null);
             }
         }
+        completeLatch.await();
+        if (rc.get() != BKException.Code.OK) {
+            throw BKException.create(rc.get());
+        }
 
         // Return the ledger handles to the inserted ledgers and entries
         return lhs;



Mime
View raw message