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

sijie 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 c106859  ISSUE #1166: Create `readonly` path for LocalBookKeeper and 
BookKeeperClusterTestCase
c106859 is described below

commit c106859d5a756768fc44738b2b1c60b1743909f5
Author: Sijie Guo <[email protected]>
AuthorDate: Tue Feb 20 02:20:29 2018 -0800

    ISSUE #1166: Create `readonly` path for LocalBookKeeper and 
BookKeeperClusterTestCase
    
    Descriptions of the changes in this PR:
    
    This fixes #1166
    
    Author: Sijie Guo <[email protected]>
    Author: Sijie Guo <[email protected]>
    
    Reviewers: Enrico Olivelli <[email protected]>, Venkateswararao Jujjuri 
(JV) <None>
    
    This closes #1167 from sijie/create_readonly_znode, closes #1166
---
 .../bookkeeper/discover/ZKRegistrationManager.java | 29 +++++++++++++++++-----
 .../bookkeeper/util/BookKeeperConstants.java       |  2 ++
 .../apache/bookkeeper/util/LocalBookKeeper.java    | 21 +++++++++++-----
 .../bookie/EnableZkSecurityBasicTest.java          |  5 ++++
 .../bookkeeper/client/BookKeeperAdminTest.java     |  6 ++++-
 .../org/apache/bookkeeper/test/ZooKeeperUtil.java  |  7 +++++-
 6 files changed, 56 insertions(+), 14 deletions(-)

diff --git 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/discover/ZKRegistrationManager.java
 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/discover/ZKRegistrationManager.java
index 66f2509..a4ecdd6 100644
--- 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/discover/ZKRegistrationManager.java
+++ 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/discover/ZKRegistrationManager.java
@@ -21,12 +21,14 @@ package org.apache.bookkeeper.discover;
 import static java.nio.charset.StandardCharsets.UTF_8;
 import static org.apache.bookkeeper.bookie.BookKeeperServerStats.BOOKIE_SCOPE;
 import static org.apache.bookkeeper.util.BookKeeperConstants.COOKIE_NODE;
+import static org.apache.bookkeeper.util.BookKeeperConstants.EMPTY_BYTE_ARRAY;
 import static org.apache.bookkeeper.util.BookKeeperConstants.INSTANCEID;
 import static org.apache.bookkeeper.util.BookKeeperConstants.READONLY;
 
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Charsets;
 
+import com.google.common.collect.Lists;
 import java.io.IOException;
 import java.util.Collection;
 import java.util.HashSet;
@@ -69,6 +71,7 @@ import org.apache.zookeeper.CreateMode;
 import org.apache.zookeeper.KeeperException;
 import org.apache.zookeeper.KeeperException.NoNodeException;
 import org.apache.zookeeper.KeeperException.NodeExistsException;
+import org.apache.zookeeper.Op;
 import org.apache.zookeeper.WatchedEvent;
 import org.apache.zookeeper.Watcher;
 import org.apache.zookeeper.Watcher.Event.EventType;
@@ -474,6 +477,8 @@ public class ZKRegistrationManager implements 
RegistrationManager {
         String zkLedgersRootPath = conf.getZkLedgersRootPath();
         String zkServers = conf.getZkServers();
         String zkAvailableBookiesPath = conf.getZkAvailableBookiesPath();
+        String zkReadonlyBookiesPath = zkAvailableBookiesPath + "/" + READONLY;
+        String instanceIdPath = zkLedgersRootPath + "/" + INSTANCEID;
         log.info("Initializing ZooKeeper metadata for new cluster, ZKServers: 
{} ledger root path: {}", zkServers,
                 zkLedgersRootPath);
 
@@ -484,19 +489,31 @@ public class ZKRegistrationManager implements 
RegistrationManager {
             return false;
         }
 
+        List<Op> multiOps = Lists.newArrayListWithExpectedSize(4);
+
         // Create ledgers root node
-        zk.create(zkLedgersRootPath, "".getBytes(UTF_8), zkAcls, 
CreateMode.PERSISTENT);
+        multiOps.add(Op.create(zkLedgersRootPath, EMPTY_BYTE_ARRAY, zkAcls, 
CreateMode.PERSISTENT));
 
         // create available bookies node
-        zk.create(zkAvailableBookiesPath, "".getBytes(UTF_8), zkAcls, 
CreateMode.PERSISTENT);
+        multiOps.add(Op.create(zkAvailableBookiesPath, EMPTY_BYTE_ARRAY, 
zkAcls, CreateMode.PERSISTENT));
 
-        // creates the new layout and stores in zookeeper
-        AbstractZkLedgerManagerFactory.newLedgerManagerFactory(conf, 
layoutManager);
+        // create readonly bookies node
+        multiOps.add(Op.create(
+            zkReadonlyBookiesPath,
+            EMPTY_BYTE_ARRAY,
+            zkAcls,
+            CreateMode.PERSISTENT));
 
         // create INSTANCEID
         String instanceId = UUID.randomUUID().toString();
-        zk.create(conf.getZkLedgersRootPath() + "/" + 
BookKeeperConstants.INSTANCEID, instanceId.getBytes(UTF_8),
-                zkAcls, CreateMode.PERSISTENT);
+        multiOps.add(Op.create(instanceIdPath, instanceId.getBytes(UTF_8),
+                zkAcls, CreateMode.PERSISTENT));
+
+        // execute the multi ops
+        zk.multi(multiOps);
+
+        // creates the new layout and stores in zookeeper
+        AbstractZkLedgerManagerFactory.newLedgerManagerFactory(conf, 
layoutManager);
 
         log.info("Successfully initiated cluster. ZKServers: {} ledger root 
path: {} instanceId: {}", zkServers,
                 zkLedgersRootPath, instanceId);
diff --git 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/BookKeeperConstants.java
 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/BookKeeperConstants.java
index f799314..bd6801f 100644
--- 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/BookKeeperConstants.java
+++ 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/BookKeeperConstants.java
@@ -55,4 +55,6 @@ public class BookKeeperConstants {
 
     public static final String FEATURE_REPP_DISABLE_DURABILITY_ENFORCEMENT = 
"repp_disable_durability_enforcement";
     public static final String FEATURE_DISABLE_ENSEMBLE_CHANGE = 
"disable_ensemble_change";
+
+    public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
 }
diff --git 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/LocalBookKeeper.java
 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/LocalBookKeeper.java
index a735ed8..0d83413 100644
--- 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/LocalBookKeeper.java
+++ 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/LocalBookKeeper.java
@@ -18,7 +18,9 @@
 package org.apache.bookkeeper.util;
 
 import static com.google.common.base.Charsets.UTF_8;
+import static org.apache.bookkeeper.util.BookKeeperConstants.READONLY;
 
+import com.google.common.collect.Lists;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.IOException;
@@ -46,6 +48,7 @@ import org.apache.bookkeeper.zookeeper.ZooKeeperClient;
 import org.apache.commons.io.FileUtils;
 import org.apache.zookeeper.CreateMode;
 import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.Op;
 import org.apache.zookeeper.ZooDefs.Ids;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -114,14 +117,20 @@ public class LocalBookKeeper {
     private void initializeZookeeper(AbstractConfiguration conf, String 
zkHost, int zkPort) throws IOException {
         LOG.info("Instantiate ZK Client");
         //initialize the zk client with values
-        ZooKeeperClient zkc = null;
-        try {
-            zkc = ZooKeeperClient.newBuilder()
+        try (ZooKeeperClient zkc = ZooKeeperClient.newBuilder()
                     .connectString(zkHost + ":" + zkPort)
                     .sessionTimeoutMs(zkSessionTimeOut)
-                    .build();
-            zkc.create(conf.getZkLedgersRootPath(), new byte[0], 
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
-            zkc.create(conf.getZkAvailableBookiesPath(), new byte[0], 
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+                    .build()) {
+            List<Op> multiOps = Lists.newArrayListWithExpectedSize(3);
+            multiOps.add(
+                Op.create(conf.getZkLedgersRootPath(), new byte[0], 
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT));
+            multiOps.add(
+                Op.create(conf.getZkAvailableBookiesPath(), new byte[0], 
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT));
+            multiOps.add(
+                Op.create(conf.getZkAvailableBookiesPath() + "/" + READONLY,
+                    new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT));
+
+            zkc.multi(multiOps);
             // No need to create an entry for each requested bookie anymore as 
the
             // BookieServers will register themselves with ZooKeeper on 
startup.
         } catch (KeeperException e) {
diff --git 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/EnableZkSecurityBasicTest.java
 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/EnableZkSecurityBasicTest.java
index c7a7f65..8a9a8f5 100644
--- 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/EnableZkSecurityBasicTest.java
+++ 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/EnableZkSecurityBasicTest.java
@@ -15,6 +15,7 @@
  */
 package org.apache.bookkeeper.bookie;
 
+import static org.apache.bookkeeper.util.BookKeeperConstants.READONLY;
 import static org.junit.Assert.assertEquals;
 
 import java.io.File;
@@ -107,6 +108,10 @@ public class EnableZkSecurityBasicTest extends 
BookKeeperClusterTestCase {
     private void checkACls(ZooKeeper zk, String path) throws KeeperException, 
InterruptedException {
         List<String> children = zk.getChildren(path, null);
         for (String child : children) {
+            if (child.equals(READONLY)) {
+                continue;
+            }
+
             String fullPath = path.equals("/") ? path + child : path + "/" + 
child;
             List<ACL> acls = zk.getACL(fullPath, new Stat());
             checkACls(zk, fullPath);
diff --git 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BookKeeperAdminTest.java
 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BookKeeperAdminTest.java
index 4112592..a6a8676 100644
--- 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BookKeeperAdminTest.java
+++ 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BookKeeperAdminTest.java
@@ -21,6 +21,7 @@
 package org.apache.bookkeeper.client;
 
 import static com.google.common.base.Charsets.UTF_8;
+import static org.apache.bookkeeper.util.BookKeeperConstants.READONLY;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
@@ -172,6 +173,9 @@ public class BookKeeperAdminTest extends 
BookKeeperClusterTestCase {
         String availableBookiesPath = newConfig.getZkAvailableBookiesPath();
         Assert.assertTrue("AvailableBookiesPath should have been created 
successfully " + availableBookiesPath,
                 (zkc.exists(availableBookiesPath, false) != null));
+        String readonlyBookiesPath = availableBookiesPath + "/" + READONLY;
+        Assert.assertTrue("ReadonlyBookiesPath should have been created 
successfully " + readonlyBookiesPath,
+            (zkc.exists(readonlyBookiesPath, false) != null));
         String instanceIdPath = newConfig.getZkLedgersRootPath() + "/" + 
BookKeeperConstants.INSTANCEID;
         Assert.assertTrue("InstanceId node should have been created 
successfully" + instanceIdPath,
                 (zkc.exists(instanceIdPath, false) != null));
@@ -274,7 +278,7 @@ public class BookKeeperAdminTest extends 
BookKeeperClusterTestCase {
          * register a RO bookie
          */
         String ipString = InetAddresses.fromInteger((new 
Random()).nextInt()).getHostAddress();
-        String roBookieRegPath = newConfig.getZkAvailableBookiesPath() + "/" + 
BookKeeperConstants.READONLY + "/"
+        String roBookieRegPath = newConfig.getZkAvailableBookiesPath() + "/" + 
READONLY + "/"
                 + ipString + ":3181";
         zkc.create(roBookieRegPath, new byte[0], Ids.OPEN_ACL_UNSAFE, 
CreateMode.EPHEMERAL);
 
diff --git 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/ZooKeeperUtil.java 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/ZooKeeperUtil.java
index cab18b8..4df0dcc 100644
--- 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/ZooKeeperUtil.java
+++ 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/test/ZooKeeperUtil.java
@@ -21,6 +21,8 @@
 
 package org.apache.bookkeeper.test;
 
+import static org.apache.bookkeeper.util.BookKeeperConstants.AVAILABLE_NODE;
+import static org.apache.bookkeeper.util.BookKeeperConstants.READONLY;
 import static org.junit.Assert.assertTrue;
 
 import java.io.File;
@@ -98,7 +100,10 @@ public class ZooKeeperUtil {
     public void createBKEnsemble(String ledgersPath) throws KeeperException, 
InterruptedException {
         Transaction txn = zkc.transaction();
         txn.create(ledgersPath, new byte[0], Ids.OPEN_ACL_UNSAFE, 
CreateMode.PERSISTENT);
-        txn.create(ledgersPath + "/available", new byte[0], 
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+        txn.create(ledgersPath + "/" + AVAILABLE_NODE,
+            new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+        txn.create(ledgersPath + "/" + AVAILABLE_NODE + "/" + READONLY,
+            new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
         txn.commit();
     }
 

-- 
To stop receiving notification emails like this one, please contact
[email protected].

Reply via email to