Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use loopback network interface for testcases. #1097

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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() + ":"
Expand Down Expand Up @@ -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() + ":"
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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() + ":"
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));

Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));

Expand Down
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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" });

Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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() });
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));

Expand Down
Expand Up @@ -53,7 +53,6 @@ public class UpdateLedgerCmdTest extends BookKeeperClusterTestCase {

public UpdateLedgerCmdTest() {
super(3);
baseConf.setAllowLoopback(true);
baseConf.setGcWaitTime(100000);
}

Expand Down
Expand Up @@ -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.
*
* <p>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<NetworkInterface> 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;
}
}
Expand Up @@ -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();

Expand All @@ -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
Expand Down