You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bookkeeper.apache.org by va...@apache.org on 2021/11/19 10:57:39 UTC

[bookkeeper] branch master updated: Reduce flakiness of TestBKDistributedLogManager (#2892)

This is an automated email from the ASF dual-hosted git repository.

vanlightly pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/bookkeeper.git


The following commit(s) were added to refs/heads/master by this push:
     new e9e9bf9  Reduce flakiness of TestBKDistributedLogManager (#2892)
e9e9bf9 is described below

commit e9e9bf92f7c124fb11d8de57fdfec860441ac8a8
Author: Andrey Yegorov <86...@users.noreply.github.com>
AuthorDate: Fri Nov 19 02:56:55 2021 -0800

    Reduce flakiness of TestBKDistributedLogManager (#2892)
    
    * Got rid of OODME by ensuring the cleanup of resources.
    
    * TestNonBlockingReadsMultiReader: load test base config
    
    * localize max num of bookies effect to one test suite
---
 .../TestBKDistributedLogManager.java               | 79 +++++++++++++++++++---
 .../distributedlog/TestDistributedLogBase.java     | 11 ++-
 .../TestNonBlockingReadsMultiReader.java           |  1 +
 3 files changed, 79 insertions(+), 12 deletions(-)

diff --git a/stream/distributedlog/core/src/test/java/org/apache/distributedlog/TestBKDistributedLogManager.java b/stream/distributedlog/core/src/test/java/org/apache/distributedlog/TestBKDistributedLogManager.java
index 28efe89..548c4ca 100644
--- a/stream/distributedlog/core/src/test/java/org/apache/distributedlog/TestBKDistributedLogManager.java
+++ b/stream/distributedlog/core/src/test/java/org/apache/distributedlog/TestBKDistributedLogManager.java
@@ -37,6 +37,8 @@ import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicReference;
+
+import lombok.extern.slf4j.Slf4j;
 import org.apache.bookkeeper.client.BKException;
 import org.apache.bookkeeper.client.BookKeeper;
 import org.apache.bookkeeper.client.LedgerHandle;
@@ -69,6 +71,8 @@ import org.apache.distributedlog.metadata.LogMetadata;
 import org.apache.distributedlog.metadata.LogSegmentMetadataStoreUpdater;
 import org.apache.distributedlog.metadata.MetadataUpdater;
 import org.apache.distributedlog.util.Utils;
+import org.junit.Assert;
+import org.junit.BeforeClass;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TestName;
@@ -78,16 +82,29 @@ import org.slf4j.LoggerFactory;
 /**
  * Test Cases for {@link DistributedLogManager}.
  */
+@Slf4j
 public class TestBKDistributedLogManager extends TestDistributedLogBase {
     static final Logger LOG = LoggerFactory.getLogger(TestBKDistributedLogManager.class);
 
     private static final Random RAND = new Random(System.currentTimeMillis());
 
+    protected static int numBookies = 1;
+    static {
+        conf.setEnsembleSize(numBookies)
+            .setAckQuorumSize(numBookies)
+            .setWriteQuorumSize(numBookies);
+    }
+
     @Rule
     public TestName testNames = new TestName();
 
     private static final long DEFAULT_SEGMENT_SIZE = 1000;
 
+    @BeforeClass
+    public static void setupCluster() throws Exception {
+        setupCluster(numBookies);
+    }
+
     private void testNonPartitionedWritesInternal(String name, DistributedLogConfiguration conf) throws Exception {
         BKDistributedLogManager dlm = createNewDLM(conf, name);
 
@@ -113,6 +130,7 @@ public class TestBKDistributedLogManager extends TestDistributedLogBase {
         writer.flush();
         writer.commit();
         writer.close();
+        assertEquals(txid - 1, dlm.getLastTxId());
 
         LogReader reader = dlm.getInputStream(1);
         long numTrans = 0;
@@ -127,6 +145,7 @@ public class TestBKDistributedLogManager extends TestDistributedLogBase {
         }
         reader.close();
         assertEquals((txid - 1), numTrans);
+        dlm.close();
     }
 
     @Test(timeout = 60000)
@@ -144,6 +163,7 @@ public class TestBKDistributedLogManager extends TestDistributedLogBase {
         assertNotNull(zkc.exists(blplm.completedLedgerZNode(1, 100,
                 perStreamLogWriter.getLogSegmentSequenceNumber()), false));
         Utils.ioResult(blplm.asyncClose());
+        dlm.close();
     }
 
     @Test(timeout = 60000)
@@ -157,7 +177,10 @@ public class TestBKDistributedLogManager extends TestDistributedLogBase {
         }
         out.closeAndComplete();
 
-        long numTrans = DLMTestUtil.getNumberofLogRecords(createNewDLM(conf, name), 1);
+        dlm.close();
+        dlm = createNewDLM(conf, name);
+
+        long numTrans = DLMTestUtil.getNumberofLogRecords(dlm, 1);
         assertEquals(100, numTrans);
         dlm.close();
     }
@@ -192,6 +215,8 @@ public class TestBKDistributedLogManager extends TestDistributedLogBase {
         out.flush();
         out.commit();
         out.close();
+
+        assertEquals(txid - 1, dlm.getLastTxId());
         dlm.close();
 
         dlm = createNewDLM(conf, name);
@@ -269,18 +294,20 @@ public class TestBKDistributedLogManager extends TestDistributedLogBase {
         dlm.close();
     }
 
-    @Test(timeout = 60000)
+    @Test(timeout = 90000)
     public void testTwoWritersOnLockDisabled() throws Exception {
         DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
         confLocal.addConfiguration(conf);
         confLocal.setOutputBufferSize(0);
         confLocal.setWriteLockEnabled(false);
         String name = "distrlog-two-writers-lock-disabled";
-        DistributedLogManager manager = createNewDLM(confLocal, name);
+        BKDistributedLogManager manager = createNewDLM(confLocal, name);
         AsyncLogWriter writer1 = Utils.ioResult(manager.openAsyncLogWriter());
         Utils.ioResult(writer1.write(DLMTestUtil.getLogRecordInstance(1L)));
+        Assert.assertEquals(1L, writer1.getLastTxId());
         AsyncLogWriter writer2 = Utils.ioResult(manager.openAsyncLogWriter());
         Utils.ioResult(writer2.write(DLMTestUtil.getLogRecordInstance(2L)));
+        Assert.assertEquals(2L, writer2.getLastTxId());
 
         // write a record to writer 1 again
         try {
@@ -289,6 +316,9 @@ public class TestBKDistributedLogManager extends TestDistributedLogBase {
         } catch (BKTransmitException bkte) {
             assertEquals(BKException.Code.LedgerFencedException, bkte.getBKResultCode());
         }
+        Utils.ioResult(writer1.asyncClose());
+        Utils.ioResult(writer2.asyncClose());
+        manager.close();
     }
 
     @Test(timeout = 60000)
@@ -302,8 +332,11 @@ public class TestBKDistributedLogManager extends TestDistributedLogBase {
             out.write(op);
         }
         out.closeAndComplete();
+        dlm.close();
 
-        assertEquals(numTransactions, DLMTestUtil.getNumberofLogRecords(createNewDLM(conf, name), 1));
+        dlm = createNewDLM(conf, name);
+
+        assertEquals(numTransactions, DLMTestUtil.getNumberofLogRecords(dlm, 1));
         dlm.close();
     }
 
@@ -334,10 +367,16 @@ public class TestBKDistributedLogManager extends TestDistributedLogBase {
         }
         out.flush();
         out.commit();
-        out.close();
+        out.closeAndComplete();
+
+        assertEquals(txid - 1, dlm.getLastTxId());
+        dlm.close();
+
+        dlm = createNewDLM(conf, name);
 
-        long numTrans = DLMTestUtil.getNumberofLogRecords(createNewDLM(conf, name), 1);
+        long numTrans = DLMTestUtil.getNumberofLogRecords(dlm, 1);
         assertEquals((txid - 1), numTrans);
+        dlm.close();
     }
 
     @Test(timeout = 60000)
@@ -351,6 +390,8 @@ public class TestBKDistributedLogManager extends TestDistributedLogBase {
                 LogRecord op = DLMTestUtil.getLogRecordInstance(txid++);
                 out.write(op);
             }
+            out.flush();
+            out.commit();
             out.closeAndComplete();
         }
 
@@ -361,7 +402,8 @@ public class TestBKDistributedLogManager extends TestDistributedLogBase {
         }
         out.flush();
         out.commit();
-        out.close();
+        out.closeAndComplete();
+        assertEquals(txid - 1, dlm.getLastTxId());
         dlm.close();
 
         dlm = createNewDLM(conf, name);
@@ -381,6 +423,7 @@ public class TestBKDistributedLogManager extends TestDistributedLogBase {
         }
         reader.close();
         assertEquals((txid - 1), numTrans);
+        dlm.close();
     }
 
     @Test(timeout = 60000)
@@ -418,7 +461,8 @@ public class TestBKDistributedLogManager extends TestDistributedLogBase {
         }
         out.flush();
         out.commit();
-        out.close();
+        out.closeAndComplete();
+        assertEquals(txid - 1, dlm.getLastTxId());
         dlm.close();
 
         dlm = createNewDLM(conf, name);
@@ -470,6 +514,7 @@ public class TestBKDistributedLogManager extends TestDistributedLogBase {
         writer.flush();
         writer.commit();
         writer.close();
+        assertEquals(txid - 1, dlm.getLastTxId());
         dlm.close();
 
         URI uri = createDLMURI("/" + name);
@@ -589,11 +634,13 @@ public class TestBKDistributedLogManager extends TestDistributedLogBase {
                 Utils.ioResult(blplm.asyncClose());
             } else {
                 writer.markEndOfStream();
+                writer.closeAndComplete();
                 BKLogWriteHandler blplm = ((BKDistributedLogManager) (dlm)).createWriteHandler(true);
                 assertNotNull(zkc.exists(blplm.completedLedgerZNode(start, DistributedLogConstants.MAX_TXID,
                         perStreamLogWriter.getLogSegmentSequenceNumber()), false));
                 Utils.ioResult(blplm.asyncClose());
             }
+            ((BKDistributedLogManager) dlm).getScheduler().submit(() -> {}).get();
         }
         return txid;
     }
@@ -634,6 +681,7 @@ public class TestBKDistributedLogManager extends TestDistributedLogBase {
         }
         assertTrue(exceptionEncountered);
         reader.close();
+        dlm.close();
     }
 
     @Test(timeout = 60000)
@@ -662,6 +710,7 @@ public class TestBKDistributedLogManager extends TestDistributedLogBase {
         }
         writer.close();
         assertTrue(exceptionEncountered);
+        dlm.close();
     }
 
     @Test(timeout = 60000)
@@ -682,6 +731,7 @@ public class TestBKDistributedLogManager extends TestDistributedLogBase {
 
         BKSyncLogWriter writer = (BKSyncLogWriter) dlm.startLogSegmentNonPartitioned();
         writer.markEndOfStream();
+        writer.closeAndComplete();
 
         LogReader reader = dlm.getInputStream(1);
         long numTrans = 0;
@@ -709,6 +759,7 @@ public class TestBKDistributedLogManager extends TestDistributedLogBase {
         }
         assertTrue(exceptionEncountered);
         reader.close();
+        dlm.close();
     }
 
     @Test(timeout = 60000, expected = LogRecordTooLongException.class)
@@ -717,6 +768,7 @@ public class TestBKDistributedLogManager extends TestDistributedLogBase {
         AsyncLogWriter writer = Utils.ioResult(dlm.openAsyncLogWriter());
         Utils.ioResult(writer.write(new LogRecord(1L, DLMTestUtil.repeatString(
                                 DLMTestUtil.repeatString("abcdefgh", 256), 512).getBytes())));
+        dlm.close();
     }
 
     @Test(timeout = 60000)
@@ -795,6 +847,7 @@ public class TestBKDistributedLogManager extends TestDistributedLogBase {
         }
         assertTrue(exceptionEncountered);
         reader.close();
+        dlm.close();
     }
 
     @Test(timeout = 60000)
@@ -867,7 +920,7 @@ public class TestBKDistributedLogManager extends TestDistributedLogBase {
         final AtomicReference<Collection<LogSegmentMetadata>> receivedStreams =
                 new AtomicReference<Collection<LogSegmentMetadata>>();
 
-        BKDistributedLogManager dlm = (BKDistributedLogManager) createNewDLM(conf, name);
+        BKDistributedLogManager dlm = createNewDLM(conf, name);
 
         Utils.ioResult(dlm.getWriterMetadataStore().getLog(dlm.getUri(), name, true, true));
         dlm.registerListener(new LogSegmentListener() {
@@ -1209,6 +1262,7 @@ public class TestBKDistributedLogManager extends TestDistributedLogBase {
             Utils.close(reader);
         }
 
+        dlm.close();
         zookeeperClient.close();
     }
 
@@ -1249,6 +1303,7 @@ public class TestBKDistributedLogManager extends TestDistributedLogBase {
         } catch (IOException ioe) {
             fail("Delete log twice should not throw any exception");
         }
+        dlm.close();
     }
 
     @Test(timeout = 60000)
@@ -1274,6 +1329,7 @@ public class TestBKDistributedLogManager extends TestDistributedLogBase {
         assertEquals(custom, new String(customMeta.get("custom"), UTF_8));
 
         sync.closeAndComplete();
+        dlm.close();
     }
 
     @Test(timeout = 60000)
@@ -1283,7 +1339,7 @@ public class TestBKDistributedLogManager extends TestDistributedLogBase {
         confLocal.setOutputBufferSize(0);
         confLocal.setWriteLockEnabled(false);
 
-        BKDistributedLogManager dlm = createNewDLM(confLocal, "distrlog-writemetadata");
+        BKDistributedLogManager dlm = createNewDLM(confLocal, "distrlog-writemetadata-async");
 
         String application = "myapplication";
         String custom = "mycustommetadata";
@@ -1299,5 +1355,8 @@ public class TestBKDistributedLogManager extends TestDistributedLogBase {
         assertEquals(application, new String(customMeta.get("application"), UTF_8));
         assertNull(customMeta.get("component"));
         assertEquals(custom, new String(customMeta.get("custom"), UTF_8));
+
+        Utils.ioResult(async.asyncClose());
+        dlm.close();
     }
 }
diff --git a/stream/distributedlog/core/src/test/java/org/apache/distributedlog/TestDistributedLogBase.java b/stream/distributedlog/core/src/test/java/org/apache/distributedlog/TestDistributedLogBase.java
index 2856c25..dde80bc 100644
--- a/stream/distributedlog/core/src/test/java/org/apache/distributedlog/TestDistributedLogBase.java
+++ b/stream/distributedlog/core/src/test/java/org/apache/distributedlog/TestDistributedLogBase.java
@@ -81,6 +81,8 @@ public class TestDistributedLogBase {
         System.setProperty("zookeeper.4lw.commands.whitelist", "*");
     }
 
+    protected static int numBookies = 3;
+
     // Num worker threads should be one, since the exec service is used for the ordered
     // future pool in test cases, and setting to > 1 will therefore result in unordered
     // write ops.
@@ -93,24 +95,29 @@ public class TestDistributedLogBase {
                 .setNumWorkerThreads(1)
                 .setReadAheadNoSuchLedgerExceptionOnReadLACErrorThresholdMillis(20)
                 .setSchedulerShutdownTimeoutMs(0)
+                .setLockTimeout(120)
+                .setZKSessionTimeoutSeconds(60)
                 .setDLLedgerMetadataLayoutVersion(LogSegmentMetadata.LEDGER_METADATA_CURRENT_LAYOUT_VERSION);
     protected ZooKeeper zkc;
     protected static LocalDLMEmulator bkutil;
     protected static ZooKeeperServerShim zks;
     protected static String zkServers;
     protected static int zkPort;
-    protected static int numBookies = 3;
     protected static final List<File> TMP_DIRS = new ArrayList<File>();
 
     @BeforeClass
     public static void setupCluster() throws Exception {
+        setupCluster(numBookies);
+    }
+
+    protected static void setupCluster(int nBookies) throws Exception {
         File zkTmpDir = IOUtils.createTempDir("zookeeper", "distrlog");
         TMP_DIRS.add(zkTmpDir);
         Pair<ZooKeeperServerShim, Integer> serverAndPort = LocalDLMEmulator.runZookeeperOnAnyPort(zkTmpDir);
         zks = serverAndPort.getLeft();
         zkPort = serverAndPort.getRight();
         bkutil = LocalDLMEmulator.newBuilder()
-                .numBookies(numBookies)
+                .numBookies(nBookies)
                 .zkHost("127.0.0.1")
                 .zkPort(zkPort)
                 .serverConf(DLMTestUtil.loadTestBkConf())
diff --git a/stream/distributedlog/core/src/test/java/org/apache/distributedlog/TestNonBlockingReadsMultiReader.java b/stream/distributedlog/core/src/test/java/org/apache/distributedlog/TestNonBlockingReadsMultiReader.java
index faa31e5..98baa4a 100644
--- a/stream/distributedlog/core/src/test/java/org/apache/distributedlog/TestNonBlockingReadsMultiReader.java
+++ b/stream/distributedlog/core/src/test/java/org/apache/distributedlog/TestNonBlockingReadsMultiReader.java
@@ -93,6 +93,7 @@ public class TestNonBlockingReadsMultiReader extends TestDistributedLogBase {
         final RateLimiter limiter = RateLimiter.create(1000);
 
         DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
+        confLocal.loadConf(conf);
         confLocal.setOutputBufferSize(0);
         confLocal.setImmediateFlushEnabled(true);