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 6a6d7bb  Migrate command `initnewcluster`
6a6d7bb is described below

commit 6a6d7bbd995acd55389ec209e42e099647c988fd
Author: Yong Zhang <[email protected]>
AuthorDate: Wed Apr 3 02:33:16 2019 +0800

    Migrate command `initnewcluster`
    
    Descriptions of the changes in this PR:
    
    #2046
    
    Reviewers: Sijie Guo <[email protected]>
    
    This closes #2047 from zymap/command-initnewcluster
---
 .../org/apache/bookkeeper/bookie/BookieShell.java  |  4 +-
 .../tools/cli/commands/bookies/InitCommand.java    | 61 ++++++++++++++++++++++
 .../tools/cli/commands/BookiesCommandGroup.java    |  2 +
 .../cli/commands/bookies/InitCommandTest.java      | 58 ++++++++++++++++++++
 4 files changed, 124 insertions(+), 1 deletion(-)

diff --git 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java
index 72546e4..5ad8d4f 100644
--- 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java
+++ 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java
@@ -365,7 +365,9 @@ public class BookieShell implements Tool {
 
         @Override
         int runCmd(CommandLine cmdLine) throws Exception {
-            boolean result = BookKeeperAdmin.initNewCluster(bkConf);
+            org.apache.bookkeeper.tools.cli.commands.bookies.InitCommand 
initCommand =
+                new 
org.apache.bookkeeper.tools.cli.commands.bookies.InitCommand();
+            boolean result = initCommand.apply(bkConf, new CliFlags());
             return (result) ? 0 : 1;
         }
     }
diff --git 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/bookies/InitCommand.java
 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/bookies/InitCommand.java
new file mode 100644
index 0000000..8846f54
--- /dev/null
+++ 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/bookies/InitCommand.java
@@ -0,0 +1,61 @@
+/*
+ * 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.tools.cli.commands.bookies;
+
+import com.google.common.util.concurrent.UncheckedExecutionException;
+import org.apache.bookkeeper.client.BookKeeperAdmin;
+import org.apache.bookkeeper.conf.ServerConfiguration;
+import org.apache.bookkeeper.tools.cli.helpers.BookieCommand;
+import org.apache.bookkeeper.tools.framework.CliFlags;
+import org.apache.bookkeeper.tools.framework.CliSpec;
+
+/**
+ * Intializes new cluster by creating required znodes for the cluster. If
+ * ledgersrootpath is already existing then it will error out. If for any
+ * reason it errors out while creating znodes for the cluster, then before
+ * running initnewcluster again, try nuking existing cluster by running
+ * nukeexistingcluster. This is required because ledgersrootpath znode would
+ * be created after verifying that it doesn't exist, hence during next retry
+ * of initnewcluster it would complain saying that ledgersrootpath is
+ * already existing.
+ */
+public class InitCommand extends BookieCommand<CliFlags> {
+
+    private static final String NAME = "init";
+    private static final String DESC =
+        "Initializes a new bookkeeper cluster. If initnewcluster fails then 
try nuking "
+        + "existing cluster by running nukeexistingcluster before running 
initnewcluster again";
+
+    public InitCommand() {
+        super(CliSpec.newBuilder()
+                     .withName(NAME)
+                     .withDescription(DESC)
+                     .withFlags(new CliFlags())
+                     .build());
+    }
+
+    @Override
+    public boolean apply(ServerConfiguration conf, CliFlags cmdFlags) {
+        try {
+            return BookKeeperAdmin.initNewCluster(conf);
+        } catch (Exception e) {
+            throw new UncheckedExecutionException(e.getMessage(), e);
+        }
+    }
+}
diff --git 
a/tools/ledger/src/main/java/org/apache/bookkeeper/tools/cli/commands/BookiesCommandGroup.java
 
b/tools/ledger/src/main/java/org/apache/bookkeeper/tools/cli/commands/BookiesCommandGroup.java
index a9e6fbb..e211b13 100644
--- 
a/tools/ledger/src/main/java/org/apache/bookkeeper/tools/cli/commands/BookiesCommandGroup.java
+++ 
b/tools/ledger/src/main/java/org/apache/bookkeeper/tools/cli/commands/BookiesCommandGroup.java
@@ -22,6 +22,7 @@ import static 
org.apache.bookkeeper.tools.common.BKCommandCategories.CATEGORY_IN
 
 import org.apache.bookkeeper.tools.cli.BKCtl;
 import org.apache.bookkeeper.tools.cli.commands.bookies.InfoCommand;
+import org.apache.bookkeeper.tools.cli.commands.bookies.InitCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookies.MetaFormatCommand;
 import org.apache.bookkeeper.tools.common.BKFlags;
@@ -44,6 +45,7 @@ public class BookiesCommandGroup extends 
CliCommandGroup<BKFlags> {
         .addCommand(new ListBookiesCommand())
         .addCommand(new InfoCommand())
         .addCommand(new MetaFormatCommand())
+        .addCommand(new InitCommand())
         .build();
 
     public BookiesCommandGroup() {
diff --git 
a/tools/ledger/src/test/java/org/apache/bookkeeper/tools/cli/commands/bookies/InitCommandTest.java
 
b/tools/ledger/src/test/java/org/apache/bookkeeper/tools/cli/commands/bookies/InitCommandTest.java
new file mode 100644
index 0000000..7c0f9ba
--- /dev/null
+++ 
b/tools/ledger/src/test/java/org/apache/bookkeeper/tools/cli/commands/bookies/InitCommandTest.java
@@ -0,0 +1,58 @@
+/*
+ * 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.tools.cli.commands.bookies;
+
+import static org.mockito.ArgumentMatchers.eq;
+
+import org.apache.bookkeeper.client.BookKeeperAdmin;
+import org.apache.bookkeeper.conf.ServerConfiguration;
+import org.apache.bookkeeper.tools.cli.helpers.BookieCommandTestBase;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+/**
+ * Unit test for {@link InitCommand}.
+ */
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({ InitCommand.class, BookKeeperAdmin.class })
+public class InitCommandTest extends BookieCommandTestBase {
+
+    public InitCommandTest() {
+        super(3, 0);
+    }
+
+    @Override
+    public void setup() throws Exception {
+        super.setup();
+
+        
PowerMockito.whenNew(ServerConfiguration.class).withNoArguments().thenReturn(conf);
+        PowerMockito.mockStatic(BookKeeperAdmin.class);
+        
PowerMockito.when(BookKeeperAdmin.initNewCluster(eq(conf))).thenReturn(true);
+    }
+
+    @Test
+    public void testCommand() {
+        InitCommand initCommand = new InitCommand();
+        Assert.assertTrue(initCommand.apply(bkFlags, new String[] {}));
+    }
+}

Reply via email to