sijie closed pull request #1167: Issue 1166: Create `readonly` path for 
LocalBookKeeper and BookKeeperClusterTestCase
URL: https://github.com/apache/bookkeeper/pull/1167
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

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 66f25096a..a4ecdd6b3 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 @@
 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.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 boolean initNewCluster() throws Exception {
         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 boolean initNewCluster() throws Exception {
             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 f799314ff..bd6801f94 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 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 a735ed8f8..0d8341343 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.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 static ZooKeeperServerShim runZookeeper(int maxCC, 
int zookeeperPort, Fil
     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 c7a7f6533..8a9a8f5df 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 @@ private void checkAllAcls() throws IOException, 
InterruptedException, KeeperExce
     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 4112592ec..a6a86763e 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 void testInitNewCluster() throws Exception {
         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 void tryNukingExistingClustersWithInvalidParams() 
throws Exception {
          * 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 cab18b8a2..4df0dcca9 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 void startServer() throws Exception {
     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();
     }
 


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to