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

eolivelli 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 594a056  Issue 2795: Bookkeeper upgrade using Bookie ID may fail due 
to cookie mismatch (#2796)
594a056 is described below

commit 594a0568528120ccffb07450c2c28144efb683d6
Author: Raúl Gracia <[email protected]>
AuthorDate: Wed Oct 13 17:52:37 2021 +0200

    Issue 2795: Bookkeeper upgrade using Bookie ID may fail due to cookie 
mismatch (#2796)
---
 .../org/apache/bookkeeper/bookie/BookieImpl.java   | 37 +++++++++++-----------
 .../bookie/BookieInitializationTest.java           | 36 +++++++++++++++++++++
 2 files changed, 55 insertions(+), 18 deletions(-)

diff --git 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieImpl.java 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieImpl.java
index 2dfa1a1..d07c8f5 100644
--- 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieImpl.java
+++ 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieImpl.java
@@ -286,26 +286,27 @@ public class BookieImpl extends BookieCriticalThread 
implements Bookie {
         // we are checking all possibilities here, so we don't need to fail if 
we can only get
         // loopback address. it will fail anyway when the bookie attempts to 
listen on loopback address.
         try {
-            // ip address
-            addresses.add(getBookieAddress(
-                new ServerConfiguration(conf)
-                    .setUseHostNameAsBookieID(false)
-                    .setAdvertisedAddress(null)
-                    .setAllowLoopback(true)
-            ).toBookieId());
-            // host name
-            addresses.add(getBookieAddress(
-                new ServerConfiguration(conf)
-                    .setUseHostNameAsBookieID(true)
-                    .setAdvertisedAddress(null)
-                    .setAllowLoopback(true)
-            ).toBookieId());
-            // advertised address
-            if (null != conf.getAdvertisedAddress()) {
-                addresses.add(getBookieAddress(conf).toBookieId());
-            }
             if (null != conf.getBookieId()) {
                 addresses.add(BookieId.parse(conf.getBookieId()));
+            } else {
+                // ip address
+                addresses.add(getBookieAddress(
+                    new ServerConfiguration(conf)
+                        .setUseHostNameAsBookieID(false)
+                        .setAdvertisedAddress(null)
+                        .setAllowLoopback(true)
+                ).toBookieId());
+                // host name
+                addresses.add(getBookieAddress(
+                    new ServerConfiguration(conf)
+                        .setUseHostNameAsBookieID(true)
+                        .setAdvertisedAddress(null)
+                        .setAllowLoopback(true)
+                ).toBookieId());
+                // advertised address
+                if (null != conf.getAdvertisedAddress()) {
+                    addresses.add(getBookieAddress(conf).toBookieId());
+                }
             }
         } catch (UnknownHostException e) {
             throw new UnknownBookieIdException(e);
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 11af989..8bf781f 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
@@ -1614,4 +1614,40 @@ public class BookieInitializationTest extends 
BookKeeperClusterTestCase {
             // ok
         }
     }
+
+    @Test
+    public void testBookieIdSetting() throws Exception {
+        String customBookieId = "customId";
+        // If BookieID is set, it takes precedence over network info.
+        final ServerConfiguration conf = 
newServerConfiguration().setBookieId(customBookieId);
+        BookieServer server = new MockBookieServer(conf);
+        server.start();
+        assertEquals(customBookieId, server.getBookieId().toString());
+        server.shutdown();
+    }
+
+    @Test
+    public void testBookieIdChange() throws Exception {
+        // By default, network info is set as Bookie Id and it is stored in 
the Cookie.
+        final ServerConfiguration conf = newServerConfiguration();
+        BookieServer server = new MockBookieServer(conf);
+        server.start();
+        assertNotNull(server.getBookieId().toString());
+        server.shutdown();
+
+        // If BookieID is set, it takes precedence over network info. Because 
of that, the new Bookie start
+        // should fail with an InvalidCookieException, as now the custom 
BookieID takes precedence.
+        String customBookieId = "customId";
+        conf.setBookieId(customBookieId);
+        try {
+            server = new MockBookieServer(conf);
+        } catch (BookieException.InvalidCookieException e) {
+            // This is the expected case, as the customBookieId prevails over 
the default one.
+        } catch (Exception e) {
+            // Unexpected exception, failing.
+            Assert.fail();
+        }
+        assertEquals(customBookieId, server.getBookieId().toString());
+        server.shutdown();
+    }
 }

Reply via email to