You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bookkeeper.apache.org by mm...@apache.org on 2017/08/03 21:20:54 UTC

[bookkeeper] branch master updated: Issue #372: Allow to configure advertised address in bookies

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

mmerli 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 464fa55  Issue #372: Allow to configure advertised address in bookies
464fa55 is described below

commit 464fa55c6debf5fdc835077135846e89729321c6
Author: Matteo Merli <mm...@apache.org>
AuthorDate: Thu Aug 3 14:20:47 2017 -0700

    Issue #372: Allow to configure advertised address in bookies
    
    Add option to configure advertised address for bookies.
    
    Reasons for this are outlined in #372
    
    Author: Matteo Merli <mm...@apache.org>
    
    Reviewers: Enrico Olivelli <None>, Sijie Guo <None>
    
    This closes #373 from merlimat/advertised-address, closes #372
---
 bookkeeper-server/conf/bk_server.conf              |   5 ++
 .../java/org/apache/bookkeeper/bookie/Bookie.java  |   8 ++
 .../bookkeeper/conf/ServerConfiguration.java       |  38 ++++++++
 .../bookkeeper/bookie/AdvertisedAddressTest.java   | 100 +++++++++++++++++++++
 site/_data/config/bk_server.yaml                   |   6 ++
 5 files changed, 157 insertions(+)

diff --git a/bookkeeper-server/conf/bk_server.conf b/bookkeeper-server/conf/bk_server.conf
old mode 100644
new mode 100755
index bb27b8a..8b942a7
--- a/bookkeeper-server/conf/bk_server.conf
+++ b/bookkeeper-server/conf/bk_server.conf
@@ -56,6 +56,11 @@ journalDirectory=/tmp/bk-txn
 # If not set, the bookie will listen on all interfaces.
 # listeningInterface=eth0
 
+# Configure a specific hostname or IP address that the bookie should use to advertise itself to
+# clients. If not set, bookie will advertised its own IP address or hostname, depending on the
+# listeningInterface and `seHostNameAsBookieID settings.
+# advertisedAddress=
+
 # Whether the bookie allowed to use a loopback interface as its primary
 # interface(i.e. the interface it uses to establish its identity)?
 # By default, loopback interfaces are not allowed as the primary
diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/Bookie.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/Bookie.java
index c05d25d..220aa4c 100644
--- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/Bookie.java
+++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/Bookie.java
@@ -572,6 +572,13 @@ public class Bookie extends BookieCriticalThread {
      */
     public static BookieSocketAddress getBookieAddress(ServerConfiguration conf)
             throws UnknownHostException {
+        // Advertised address takes precedence over the listening interface and the
+        // useHostNameAsBookieID settings
+        if (conf.getAdvertisedAddress() != null && conf.getAdvertisedAddress().trim().length() > 0) {
+            String hostAddress = conf.getAdvertisedAddress().trim();
+            return new BookieSocketAddress(hostAddress, conf.getBookiePort());
+        }
+
         String iface = conf.getListeningInterface();
         if (iface == null) {
             iface = "default";
@@ -586,6 +593,7 @@ public class Bookie extends BookieCriticalThread {
         if (conf.getUseHostNameAsBookieID()) {
             hostAddress = inetAddr.getAddress().getCanonicalHostName();
         }
+
         BookieSocketAddress addr =
                 new BookieSocketAddress(hostAddress, conf.getBookiePort());
         if (addr.getSocketAddress().getAddress().isLoopbackAddress()
diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/conf/ServerConfiguration.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/conf/ServerConfiguration.java
index b2dfd52..733e0f7 100644
--- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/conf/ServerConfiguration.java
+++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/conf/ServerConfiguration.java
@@ -82,6 +82,7 @@ public class ServerConfiguration extends AbstractConfiguration {
     protected final static String BOOKIE_PORT = "bookiePort";
     protected final static String LISTENING_INTERFACE = "listeningInterface";
     protected final static String ALLOW_LOOPBACK = "allowLoopback";
+    protected final static String ADVERTISED_ADDRESS = "advertisedAddress";
     protected final static String ALLOW_EPHEMERAL_PORTS = "allowEphemeralPorts";
 
     protected final static String JOURNAL_DIR = "journalDirectory";
@@ -579,6 +580,43 @@ public class ServerConfiguration extends AbstractConfiguration {
     }
 
     /**
+     * Get the configured advertised address for the bookie.
+     *
+     * If present, this setting will take precedence over the
+     * {@link #setListeningInterface(String)} and
+     * {@link #setUseHostNameAsBookieID(boolean)}.
+     *
+     * @see #setAdvertisedAddress(String)
+     * @return the configure address to be advertised
+     */
+    public String getAdvertisedAddress() {
+        return this.getString(ADVERTISED_ADDRESS, null);
+    }
+
+    /**
+     * Configure the bookie to advertise a specific address.
+     *
+     * By default, a bookie will advertise either its own IP or hostname,
+     * depending on the {@link getUseHostNameAsBookieID()} setting.
+     *
+     * When the advertised is set to a non-empty string, the bookie will
+     * register and advertise using this address.
+     *
+     * If present, this setting will take precedence over the
+     * {@link #setListeningInterface(String)} and
+     * {@link #setUseHostNameAsBookieID(boolean)}.
+     *
+     * @see #getAdvertisedAddress()
+     * @param allow
+     *            whether to allow loopback interfaces
+     * @return server configuration
+     */
+    public ServerConfiguration setAdvertisedAddress(String advertisedAddress) {
+        this.setProperty(ADVERTISED_ADDRESS, advertisedAddress);
+        return this;
+    }
+
+    /**
      * Is the bookie allowed to use an ephemeral port (port 0) as its server port.
      *
      * <p>By default, an ephemeral port is not allowed. Using an ephemeral port
diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/AdvertisedAddressTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/AdvertisedAddressTest.java
new file mode 100644
index 0000000..fe33886
--- /dev/null
+++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/AdvertisedAddressTest.java
@@ -0,0 +1,100 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+package org.apache.bookkeeper.bookie;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collection;
+
+import org.apache.bookkeeper.client.BookKeeperAdmin;
+import org.apache.bookkeeper.conf.ServerConfiguration;
+import org.apache.bookkeeper.conf.TestBKConfiguration;
+import org.apache.bookkeeper.net.BookieSocketAddress;
+import org.apache.bookkeeper.test.BookKeeperClusterTestCase;
+import org.apache.bookkeeper.test.PortManager;
+import org.apache.bookkeeper.util.IOUtils;
+import org.junit.Test;
+
+public class AdvertisedAddressTest extends BookKeeperClusterTestCase {
+    final int bookiePort = PortManager.nextFreePort();
+
+    public AdvertisedAddressTest() {
+        super(0);
+    }
+
+    private String newDirectory(boolean createCurDir) throws IOException {
+        File d = IOUtils.createTempDir("cookie", "tmpdir");
+        if (createCurDir) {
+            new File(d, "current").mkdirs();
+        }
+        tmpDirs.add(d);
+        return d.getPath();
+    }
+
+    /**
+     * Test starting bookie with clean state.
+     */
+    @Test(timeout = 60000)
+    public void testSetAdvertisedAddress() throws Exception {
+        ServerConfiguration conf = TestBKConfiguration.newServerConfiguration()
+                .setZkServers(zkUtil.getZooKeeperConnectString()).setJournalDirName(newDirectory(false))
+                .setLedgerDirNames(new String[] { newDirectory(false) }).setBookiePort(bookiePort);
+
+        conf.setAdvertisedAddress("10.0.0.1");
+        assertEquals("10.0.0.1", conf.getAdvertisedAddress());
+
+        BookieSocketAddress bkAddress = new BookieSocketAddress("10.0.0.1", bookiePort);
+        assertEquals(bkAddress, Bookie.getBookieAddress(conf));
+
+        Bookie b = new Bookie(conf);
+        b.start();
+
+        BookKeeperAdmin bka = new BookKeeperAdmin(baseClientConf);
+        Collection<BookieSocketAddress> bookies = bka.getAvailableBookies();
+
+        assertEquals(1, bookies.size());
+        BookieSocketAddress address = bookies.iterator().next();
+        assertEquals(bkAddress, address);
+
+        b.shutdown();
+        bka.close();
+    }
+
+    /**
+     * When advertised address is specified, it should override the use
+     */
+    @Test(timeout = 60000)
+    public void testBothUseHostnameAndAdvertisedAddress() throws Exception {
+        ServerConfiguration conf = new ServerConfiguration().setBookiePort(bookiePort);
+
+        conf.setAdvertisedAddress("10.0.0.1");
+        conf.setUseHostNameAsBookieID(true);
+
+        assertEquals("10.0.0.1", conf.getAdvertisedAddress());
+
+        BookieSocketAddress bkAddress = new BookieSocketAddress("10.0.0.1", bookiePort);
+        assertEquals(bkAddress, Bookie.getBookieAddress(conf));
+    }
+
+}
diff --git a/site/_data/config/bk_server.yaml b/site/_data/config/bk_server.yaml
index 6a2355d..161d491 100644
--- a/site/_data/config/bk_server.yaml
+++ b/site/_data/config/bk_server.yaml
@@ -26,6 +26,12 @@ groups:
   - param: listeningInterface
     description: The network interface that the bookie should listen on. If not set, the bookie will listen on all interfaces.
     default: eth0
+  - param: advertisedAddress
+    description: |
+      Configure a specific hostname or IP address that the bookie should use to advertise itself to
+      clients. If not set, bookie will advertised its own IP address or hostname, depending on the
+      `listeningInterface` and `useHostNameAsBookieID` settings.
+    default: eth0
   - param: allowLoopback
     description: |
       Whether the bookie is allowed to use a loopback interface as its primary

-- 
To stop receiving notification emails like this one, please contact
['"commits@bookkeeper.apache.org" <co...@bookkeeper.apache.org>'].