From c25ebe141c30c746bd8f4e73996d68554e4145b9 Mon Sep 17 00:00:00 2001 From: cguttapalem Date: Wed, 31 Jan 2018 18:08:43 -0800 Subject: [PATCH] ISSUE #1107: Use loopback network interface for testcases. - tests would be more reliable irrespective of the environment, if the loopback address is used in testsuites. - unless loopback address is set explicitly, in my env (while on company's VPN), UpdateCookieCmdTest is failing most of the times. - currently allowLoopback is set to true in BookKeeperClusterTestCase, it doesn't make Bookies to use loopback interface address. - loopback network interface should be set explicitly as listening interface to use loopback address. --- .../bookie/BookieInitializationTest.java | 11 ++--- .../storage/ldb/ConversionRollbackTest.java | 4 +- .../bookie/storage/ldb/ConversionTest.java | 4 +- .../storage/ldb/DbLedgerStorageTest.java | 7 ++-- .../ldb/DbLedgerStorageWriteCacheTest.java | 4 +- .../ldb/LocationsIndexRebuildTest.java | 4 +- .../client/UpdateLedgerCmdTest.java | 1 - .../bookkeeper/conf/TestBKConfiguration.java | 40 ++++++++++++++++++- .../test/BookKeeperClusterTestCase.java | 6 ++- 9 files changed, 61 insertions(+), 20 deletions(-) diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/BookieInitializationTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/BookieInitializationTest.java index dbfb9abf137..36406ce749f 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/BookieInitializationTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/BookieInitializationTest.java @@ -155,7 +155,7 @@ public void testBookieRegistrationWithSameZooKeeperClient() throws Exception { final ServerConfiguration conf = TestBKConfiguration.newServerConfiguration(); conf.setJournalDirName(tmpDir.getPath()) .setLedgerDirNames(new String[] { tmpDir.getPath() }) - .setZkServers(null); + .setZkServers(null).setListeningInterface(null); final String bkRegPath = conf.getZkAvailableBookiesPath() + "/" + InetAddress.getLocalHost().getHostAddress() + ":" @@ -188,7 +188,7 @@ public void testBookieRegistration() throws Exception { final ServerConfiguration conf = TestBKConfiguration.newServerConfiguration(); conf.setJournalDirName(tmpDir.getPath()) .setLedgerDirNames(new String[] { tmpDir.getPath() }) - .setZkServers(null); + .setZkServers(null).setListeningInterface(null); final String bkRegPath = conf.getZkAvailableBookiesPath() + "/" + InetAddress.getLocalHost().getHostAddress() + ":" @@ -250,7 +250,7 @@ public void testBookieRegistrationWithFQDNHostNameAsBookieID() throws Exception final ServerConfiguration conf = TestBKConfiguration.newServerConfiguration().setZkServers(null) .setJournalDirName(tmpDir.getPath()).setLedgerDirNames(new String[] { tmpDir.getPath() }) - .setUseHostNameAsBookieID(true); + .setUseHostNameAsBookieID(true).setListeningInterface(null); final String bkRegPath = conf.getZkAvailableBookiesPath() + "/" + InetAddress.getLocalHost().getCanonicalHostName() + ":" + conf.getBookiePort(); @@ -271,7 +271,7 @@ public void testBookieRegistrationWithShortHostNameAsBookieID() throws Exception final ServerConfiguration conf = TestBKConfiguration.newServerConfiguration().setZkServers(null) .setJournalDirName(tmpDir.getPath()).setLedgerDirNames(new String[] { tmpDir.getPath() }) - .setUseHostNameAsBookieID(true).setUseShortHostName(true); + .setUseHostNameAsBookieID(true).setUseShortHostName(true).setListeningInterface(null); final String bkRegPath = conf.getZkAvailableBookiesPath() + "/" + (InetAddress.getLocalHost().getCanonicalHostName().split("\\.", 2)[0]) + ":" + conf.getBookiePort(); @@ -298,7 +298,8 @@ public void testRegNodeExistsAfterSessionTimeOut() throws Exception { final ServerConfiguration conf = TestBKConfiguration.newServerConfiguration(); conf.setJournalDirName(tmpDir.getPath()) .setLedgerDirNames(new String[] { tmpDir.getPath() }) - .setZkServers(zkUtil.getZooKeeperConnectString()); + .setZkServers(zkUtil.getZooKeeperConnectString()) + .setListeningInterface(null); String bkRegPath = conf.getZkAvailableBookiesPath() + "/" + InetAddress.getLocalHost().getHostAddress() + ":" diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/ConversionRollbackTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/ConversionRollbackTest.java index 75e061a27b7..e3cec7c3ce0 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/ConversionRollbackTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/ConversionRollbackTest.java @@ -40,6 +40,7 @@ import org.apache.bookkeeper.bookie.InterleavedLedgerStorage; import org.apache.bookkeeper.bookie.LedgerDirsManager; import org.apache.bookkeeper.conf.ServerConfiguration; +import org.apache.bookkeeper.conf.TestBKConfiguration; import org.apache.bookkeeper.stats.NullStatsLogger; import org.apache.bookkeeper.util.DiskChecker; import org.apache.commons.io.FileUtils; @@ -81,9 +82,8 @@ public void convertFromDbStorageToInterleaved() throws Exception { log.info("Using temp directory: {}", tmpDir); - ServerConfiguration conf = new ServerConfiguration(); + ServerConfiguration conf = TestBKConfiguration.newServerConfiguration(); conf.setLedgerDirNames(new String[] { tmpDir.toString() }); - conf.setAllowLoopback(true); LedgerDirsManager ledgerDirsManager = new LedgerDirsManager(conf, conf.getLedgerDirs(), new DiskChecker(conf.getDiskUsageThreshold(), conf.getDiskUsageWarnThreshold())); diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/ConversionTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/ConversionTest.java index 1816945d7be..dbc3c970296 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/ConversionTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/ConversionTest.java @@ -39,6 +39,7 @@ import org.apache.bookkeeper.bookie.InterleavedLedgerStorage; import org.apache.bookkeeper.bookie.LedgerDirsManager; import org.apache.bookkeeper.conf.ServerConfiguration; +import org.apache.bookkeeper.conf.TestBKConfiguration; import org.apache.bookkeeper.stats.NullStatsLogger; import org.apache.bookkeeper.util.DiskChecker; import org.apache.commons.io.FileUtils; @@ -78,9 +79,8 @@ public void test() throws Exception { System.out.println(tmpDir); - ServerConfiguration conf = new ServerConfiguration(); + ServerConfiguration conf = TestBKConfiguration.newServerConfiguration(); conf.setLedgerDirNames(new String[] { tmpDir.toString() }); - conf.setAllowLoopback(true); LedgerDirsManager ledgerDirsManager = new LedgerDirsManager(conf, conf.getLedgerDirs(), new DiskChecker(conf.getDiskUsageThreshold(), conf.getDiskUsageWarnThreshold())); diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/DbLedgerStorageTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/DbLedgerStorageTest.java index 1e91c2ce831..4b61a657f63 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/DbLedgerStorageTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/DbLedgerStorageTest.java @@ -40,6 +40,7 @@ import org.apache.bookkeeper.bookie.EntryLocation; import org.apache.bookkeeper.bookie.EntryLogger; import org.apache.bookkeeper.conf.ServerConfiguration; +import org.apache.bookkeeper.conf.TestBKConfiguration; import org.apache.bookkeeper.proto.BookieProtocol; import org.junit.After; import org.junit.Before; @@ -62,9 +63,8 @@ public void setup() throws Exception { Bookie.checkDirectoryStructure(curDir); int gcWaitTime = 1000; - ServerConfiguration conf = new ServerConfiguration(); + ServerConfiguration conf = TestBKConfiguration.newServerConfiguration(); conf.setGcWaitTime(gcWaitTime); - conf.setAllowLoopback(true); conf.setLedgerStorageClass(DbLedgerStorage.class.getName()); conf.setLedgerDirNames(new String[] { tmpDir.toString() }); Bookie bookie = new Bookie(conf); @@ -229,9 +229,8 @@ public void testBookieCompaction() throws Exception { @Test public void doubleDirectoryError() throws Exception { int gcWaitTime = 1000; - ServerConfiguration conf = new ServerConfiguration(); + ServerConfiguration conf = TestBKConfiguration.newServerConfiguration(); conf.setGcWaitTime(gcWaitTime); - conf.setAllowLoopback(true); conf.setLedgerStorageClass(DbLedgerStorage.class.getName()); conf.setLedgerDirNames(new String[] { "dir1", "dir2" }); diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/DbLedgerStorageWriteCacheTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/DbLedgerStorageWriteCacheTest.java index 0d3f5bbc86e..c2281a7dc2f 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/DbLedgerStorageWriteCacheTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/DbLedgerStorageWriteCacheTest.java @@ -31,6 +31,7 @@ import org.apache.bookkeeper.bookie.Bookie; import org.apache.bookkeeper.conf.ServerConfiguration; +import org.apache.bookkeeper.conf.TestBKConfiguration; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -79,9 +80,8 @@ public void setup() throws Exception { Bookie.checkDirectoryStructure(curDir); int gcWaitTime = 1000; - ServerConfiguration conf = new ServerConfiguration(); + ServerConfiguration conf = TestBKConfiguration.newServerConfiguration(); conf.setGcWaitTime(gcWaitTime); - conf.setAllowLoopback(true); conf.setLedgerStorageClass(MockedDbLedgerStorage.class.getName()); conf.setProperty(DbLedgerStorage.WRITE_CACHE_MAX_SIZE_MB, 1); conf.setLedgerDirNames(new String[] { tmpDir.toString() }); diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/LocationsIndexRebuildTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/LocationsIndexRebuildTest.java index c1f7e397134..9919dce8c24 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/LocationsIndexRebuildTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/LocationsIndexRebuildTest.java @@ -39,6 +39,7 @@ import org.apache.bookkeeper.bookie.Checkpointer; import org.apache.bookkeeper.bookie.LedgerDirsManager; import org.apache.bookkeeper.conf.ServerConfiguration; +import org.apache.bookkeeper.conf.TestBKConfiguration; import org.apache.bookkeeper.stats.NullStatsLogger; import org.apache.bookkeeper.util.DiskChecker; import org.apache.commons.io.FileUtils; @@ -78,10 +79,9 @@ public void test() throws Exception { System.out.println(tmpDir); - ServerConfiguration conf = new ServerConfiguration(); + ServerConfiguration conf = TestBKConfiguration.newServerConfiguration(); conf.setLedgerDirNames(new String[] { tmpDir.toString() }); conf.setLedgerStorageClass(DbLedgerStorage.class.getName()); - conf.setAllowLoopback(true); LedgerDirsManager ledgerDirsManager = new LedgerDirsManager(conf, conf.getLedgerDirs(), new DiskChecker(conf.getDiskUsageThreshold(), conf.getDiskUsageWarnThreshold())); diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/UpdateLedgerCmdTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/UpdateLedgerCmdTest.java index 14c4b61132d..d773bda6eff 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/UpdateLedgerCmdTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/UpdateLedgerCmdTest.java @@ -53,7 +53,6 @@ public class UpdateLedgerCmdTest extends BookKeeperClusterTestCase { public UpdateLedgerCmdTest() { super(3); - baseConf.setAllowLoopback(true); baseConf.setGcWaitTime(100000); } diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/conf/TestBKConfiguration.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/conf/TestBKConfiguration.java index 961cecc66e8..aba12a748ce 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/conf/TestBKConfiguration.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/conf/TestBKConfiguration.java @@ -21,23 +21,61 @@ package org.apache.bookkeeper.conf; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.util.Collections; +import java.util.Enumeration; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * Test the BK configuration object. */ public class TestBKConfiguration { + static final Logger LOG = LoggerFactory.getLogger(TestBKConfiguration.class); + + /** + * Loopback interface is set as the listening interface and allowloopback is + * set to true in this server config. + * + *

If the caller doesn't want loopback address, then listeningInterface + * should be set back to null. + */ public static ServerConfiguration newServerConfiguration() { ServerConfiguration confReturn = new ServerConfiguration(); confReturn.setJournalFlushWhenQueueEmpty(true); // enable journal format version confReturn.setJournalFormatVersionToWrite(5); - confReturn.setAllowLoopback(true); confReturn.setAllowEphemeralPorts(true); confReturn.setBookiePort(0); confReturn.setGcWaitTime(1000); confReturn.setDiskUsageThreshold(0.999f); confReturn.setDiskUsageWarnThreshold(0.99f); + setLoopbackInterfaceAndAllowLoopback(confReturn); return confReturn; } + private static String getLoopbackInterfaceName() { + try { + Enumeration nifs = NetworkInterface.getNetworkInterfaces(); + for (NetworkInterface nif : Collections.list(nifs)) { + if (nif.isLoopback()) { + return nif.getName(); + } + } + } catch (SocketException se) { + LOG.warn("Exception while figuring out loopback interface. Will use null.", se); + return null; + } + LOG.warn("Unable to deduce loopback interface. Will use null"); + return null; + } + + public static ServerConfiguration setLoopbackInterfaceAndAllowLoopback(ServerConfiguration serverConf) { + serverConf.setListeningInterface(getLoopbackInterfaceName()); + serverConf.setAllowLoopback(true); + return serverConf; + } } diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookKeeperClusterTestCase.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookKeeperClusterTestCase.java index 526a4c7b1c9..6365c157e14 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookKeeperClusterTestCase.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/BookKeeperClusterTestCase.java @@ -85,6 +85,11 @@ public abstract class BookKeeperClusterTestCase { protected int numBookies; protected BookKeeperTestClient bkc; + /* + * Loopback interface is set as the listening interface and allowloopback is + * set to true in this server config. So bookies in this test process would + * bind to loopback address. + */ protected final ServerConfiguration baseConf = TestBKConfiguration.newServerConfiguration(); protected final ClientConfiguration baseClientConf = new ClientConfiguration(); @@ -99,7 +104,6 @@ public BookKeeperClusterTestCase(int numBookies) { public BookKeeperClusterTestCase(int numBookies, int testTimeoutSecs) { this.numBookies = numBookies; this.globalTimeout = Timeout.seconds(testTimeoutSecs); - baseConf.setAllowLoopback(true); } @Before