You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bookkeeper.apache.org by si...@apache.org on 2019/04/03 06:16:27 UTC

[bookkeeper] branch master updated: Migrate command `nukeexistingcluster`

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 95d145a  Migrate command `nukeexistingcluster`
95d145a is described below

commit 95d145a15f5410e207ce610b6b4af8758851ab97
Author: Yong Zhang <zh...@gmail.com>
AuthorDate: Wed Apr 3 14:16:23 2019 +0800

    Migrate command `nukeexistingcluster`
    
    Descriptions of the changes in this PR:
    
    #2029
    
    
    
    Reviewers: Sijie Guo <si...@apache.org>
    
    This closes #2030 from zymap/command-nukeexistingcluster
---
 .../org/apache/bookkeeper/bookie/BookieShell.java  | 20 ++---
 .../bookies/NukeExistingClusterCommand.java        | 92 ++++++++++++++++++++++
 .../tools/cli/commands/BookiesCommandGroup.java    |  2 +
 .../bookies/NukeExistingClusterCommandTest.java    | 75 ++++++++++++++++++
 4 files changed, 176 insertions(+), 13 deletions(-)

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 6f8f1c3..725faa7 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
@@ -111,6 +111,8 @@ import org.apache.bookkeeper.tools.cli.commands.bookie.SanityTestCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookies.InfoCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookies.MetaFormatCommand;
+import org.apache.bookkeeper.tools.cli.commands.bookies.NukeExistingClusterCommand;
+import org.apache.bookkeeper.tools.cli.commands.bookies.NukeExistingClusterCommand.NukeExistingClusterFlags;
 import org.apache.bookkeeper.tools.cli.commands.client.DeleteLedgerCommand;
 import org.apache.bookkeeper.tools.cli.commands.client.SimpleTestCommand;
 import org.apache.bookkeeper.tools.cli.commands.cookie.CreateCookieCommand;
@@ -405,19 +407,11 @@ public class BookieShell implements Tool {
             String zkledgersrootpath = cmdLine.getOptionValue("zkledgersrootpath");
             String instanceid = cmdLine.getOptionValue("instanceid");
 
-            /*
-             * for NukeExistingCluster command 'zkledgersrootpath' should be provided and either force option or
-             * instanceid should be provided.
-             */
-            if ((zkledgersrootpath == null) || (force == (instanceid != null))) {
-                LOG.error(
-                        "zkledgersrootpath should be specified and either force option "
-                        + "or instanceid should be specified (but not both)");
-                printUsage();
-                return -1;
-            }
-
-            boolean result = BookKeeperAdmin.nukeExistingCluster(bkConf, zkledgersrootpath, instanceid, force);
+            NukeExistingClusterCommand cmd = new NukeExistingClusterCommand();
+            NukeExistingClusterFlags flags = new NukeExistingClusterFlags().force(force)
+                                                                           .zkLedgersRootPath(zkledgersrootpath)
+                                                                           .instandId(instanceid);
+            boolean result = cmd.apply(bkConf, flags);
             return (result) ? 0 : 1;
         }
     }
diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/bookies/NukeExistingClusterCommand.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/bookies/NukeExistingClusterCommand.java
new file mode 100644
index 0000000..4e46ccb
--- /dev/null
+++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/bookies/NukeExistingClusterCommand.java
@@ -0,0 +1,92 @@
+/*
+ * 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.beust.jcommander.Parameter;
+import com.google.common.util.concurrent.UncheckedExecutionException;
+import lombok.Setter;
+import lombok.experimental.Accessors;
+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;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Nuke bookkeeper metadata of existing cluster in zookeeper.
+ */
+public class NukeExistingClusterCommand extends BookieCommand<NukeExistingClusterCommand.NukeExistingClusterFlags> {
+
+    static final Logger LOG = LoggerFactory.getLogger(NukeExistingClusterCommand.class);
+
+    private static final String NAME = "nukeexistingcluster";
+    private static final String DESC = "Nuke bookkeeper cluster by deleting metadata.";
+
+    public NukeExistingClusterCommand() {
+        this(new NukeExistingClusterFlags());
+    }
+
+    private NukeExistingClusterCommand(NukeExistingClusterFlags flags) {
+        super(CliSpec.<NukeExistingClusterCommand.NukeExistingClusterFlags>newBuilder()
+                  .withName(NAME)
+                  .withDescription(DESC)
+                  .withFlags(flags)
+                  .build());
+    }
+
+    /**
+     * Flags for nuke existing cluster command.
+     */
+    @Accessors(fluent = true)
+    @Setter
+    public static class NukeExistingClusterFlags extends CliFlags {
+
+        @Parameter(names = {"-f", "--force"},
+            description = "If instance id is not specified, then whether to force nuke "
+                          + "the metadata without " + "validating instance id")
+        private boolean force;
+
+        @Parameter(names = {"-p", "--zkledgersrootpath"}, description = "zookeeper ledgers root path", required = true)
+        private String zkLedgersRootPath;
+
+        @Parameter(names = {"-i", "--instanceid"}, description = "instance id")
+        private String instandId;
+
+    }
+
+    @Override
+    public boolean apply(ServerConfiguration conf, NukeExistingClusterFlags cmdFlags) {
+        /*
+         * for NukeExistingCluster command 'zkledgersrootpath' should be provided and either force option or
+         * instanceid should be provided.
+         */
+        if (cmdFlags.force == (cmdFlags.instandId != null)) {
+            LOG.error("Either force option or instanceid should be specified (but no both)");
+            return false;
+        }
+        try {
+            return BookKeeperAdmin.nukeExistingCluster(conf, cmdFlags.zkLedgersRootPath,
+                                                         cmdFlags.instandId, cmdFlags.force);
+        } 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 e211b13..710a2fe 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
@@ -25,6 +25,7 @@ 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.cli.commands.bookies.NukeExistingClusterCommand;
 import org.apache.bookkeeper.tools.common.BKFlags;
 import org.apache.bookkeeper.tools.framework.CliCommandGroup;
 import org.apache.bookkeeper.tools.framework.CliSpec;
@@ -44,6 +45,7 @@ public class BookiesCommandGroup extends CliCommandGroup<BKFlags> {
         .withCategory(CATEGORY_INFRA_SERVICE)
         .addCommand(new ListBookiesCommand())
         .addCommand(new InfoCommand())
+        .addCommand(new NukeExistingClusterCommand())
         .addCommand(new MetaFormatCommand())
         .addCommand(new InitCommand())
         .build();
diff --git a/tools/ledger/src/test/java/org/apache/bookkeeper/tools/cli/commands/bookies/NukeExistingClusterCommandTest.java b/tools/ledger/src/test/java/org/apache/bookkeeper/tools/cli/commands/bookies/NukeExistingClusterCommandTest.java
new file mode 100644
index 0000000..a6ebd9e
--- /dev/null
+++ b/tools/ledger/src/test/java/org/apache/bookkeeper/tools/cli/commands/bookies/NukeExistingClusterCommandTest.java
@@ -0,0 +1,75 @@
+/*
+ * 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.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.anyString;
+
+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 NukeExistingClusterCommand}.
+ */
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({ NukeExistingClusterCommand.class, BookKeeperAdmin.class })
+public class NukeExistingClusterCommandTest extends BookieCommandTestBase {
+
+    public NukeExistingClusterCommandTest() {
+        super(3, 0);
+    }
+
+    @Override
+    public void setup() throws Exception {
+        super.setup();
+
+//        PowerMockito.mockStatic(BookKeeperAdmin.classs);
+    }
+
+    @Test
+    public void commandWithoutFlags() throws Exception {
+        NukeExistingClusterCommand cmd = new NukeExistingClusterCommand();
+        Assert.assertFalse(cmd.apply(bkFlags, new String[] { "" }));
+    }
+
+    @Test
+    public void commandWithoutForceAndInstanceId() {
+        NukeExistingClusterCommand cmd = new NukeExistingClusterCommand();
+        Assert.assertFalse(cmd.apply(bkFlags, new String[] { "-p", "" }));
+    }
+
+    @Test
+    public void testCommand() throws Exception {
+        PowerMockito.mockStatic(BookKeeperAdmin.class);
+        PowerMockito.when(
+            BookKeeperAdmin.nukeExistingCluster(any(ServerConfiguration.class), anyString(), anyString(), anyBoolean()))
+                    .thenReturn(true);
+
+        NukeExistingClusterCommand cmd = new NukeExistingClusterCommand();
+        Assert.assertTrue(cmd.apply(bkFlags, new String[] { "-p", "", "-i", "1" }));
+    }
+}