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 bf66235  Migrate command `deleteledger`
bf66235 is described below

commit bf66235e5bfa2aa094d17b7e555d0dd5661a6f4d
Author: Yong Zhang <[email protected]>
AuthorDate: Tue Apr 2 16:32:23 2019 +0800

    Migrate command `deleteledger`
    
    Descriptions of the changes in this PR:
    
    #2025
    
    
    
    Reviewers: Sijie Guo <[email protected]>
    
    This closes #2026 from zymap/command-deleteledger
---
 .../org/apache/bookkeeper/bookie/BookieShell.java  |  28 +----
 .../cli/commands/client/DeleteLedgerCommand.java   | 130 +++++++++++++++++++++
 .../tools/cli/commands/LedgerCommandGroup.java     |   2 +
 .../commands/client/DeleteLedgerCommandTest.java   |  97 +++++++++++++++
 4 files changed, 234 insertions(+), 23 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 487a5a3..8ec97c7 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
@@ -112,6 +112,7 @@ 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.client.DeleteLedgerCommand;
 import org.apache.bookkeeper.tools.cli.commands.client.SimpleTestCommand;
 import org.apache.bookkeeper.tools.cli.commands.cookie.CreateCookieCommand;
 import org.apache.bookkeeper.tools.cli.commands.cookie.DeleteCookieCommand;
@@ -2199,31 +2200,12 @@ public class BookieShell implements Tool {
         @Override
         public int runCmd(CommandLine cmdLine) throws Exception {
             final long lid = getOptionLedgerIdValue(cmdLine, "ledgerid", -1);
-            if (lid == -1) {
-                System.err.println("Must specify a ledger id");
-                return -1;
-            }
 
             boolean force = cmdLine.hasOption("f");
-            boolean confirm = false;
-            if (!force) {
-                confirm = IOUtils.confirmPrompt(
-                        "Are you sure to delete Ledger : " + 
ledgerIdFormatter.formatLedgerId(lid) + "?");
-            }
-
-            BookKeeper bk = null;
-            try {
-                if (force || confirm) {
-                    ClientConfiguration conf = new ClientConfiguration();
-                    conf.addConfiguration(bkConf);
-                    bk = new BookKeeper(conf);
-                    bk.deleteLedger(lid);
-                }
-            } finally {
-                if (bk != null) {
-                    bk.close();
-                }
-            }
+            DeleteLedgerCommand cmd = new 
DeleteLedgerCommand(ledgerIdFormatter);
+            DeleteLedgerCommand.DeleteLedgerFlags flags = new 
DeleteLedgerCommand.DeleteLedgerFlags()
+                .ledgerId(lid).force(force);
+            cmd.apply(bkConf, flags);
 
             return 0;
         }
diff --git 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/client/DeleteLedgerCommand.java
 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/client/DeleteLedgerCommand.java
new file mode 100644
index 0000000..70ffee6
--- /dev/null
+++ 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/client/DeleteLedgerCommand.java
@@ -0,0 +1,130 @@
+/*
+ * 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.client;
+
+import com.beust.jcommander.Parameter;
+import com.google.common.util.concurrent.UncheckedExecutionException;
+import java.io.IOException;
+import lombok.Setter;
+import lombok.experimental.Accessors;
+import org.apache.bookkeeper.client.BKException;
+import org.apache.bookkeeper.client.BookKeeper;
+import org.apache.bookkeeper.conf.ClientConfiguration;
+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.apache.bookkeeper.util.IOUtils;
+import org.apache.bookkeeper.util.LedgerIdFormatter;
+
+/**
+ * Command to delete a given ledger.
+ */
+public class DeleteLedgerCommand extends 
BookieCommand<DeleteLedgerCommand.DeleteLedgerFlags> {
+
+    private static final String NAME = "deleteledger";
+    private static final String DESC = "Delete a ledger.";
+    private static final String DEFAULT = "";
+
+    private LedgerIdFormatter ledgerIdFormatter;
+
+    public DeleteLedgerCommand() {
+        this(new DeleteLedgerFlags());
+    }
+
+    public DeleteLedgerCommand(LedgerIdFormatter ledgerIdFormatter) {
+        this(new DeleteLedgerFlags());
+        this.ledgerIdFormatter = ledgerIdFormatter;
+    }
+
+    private DeleteLedgerCommand(DeleteLedgerFlags flags) {
+        super(CliSpec.<DeleteLedgerCommand.DeleteLedgerFlags>newBuilder()
+                  .withName(NAME)
+                  .withDescription(DESC)
+                  .withFlags(flags)
+                  .build());
+    }
+
+    /**
+     * Flags for delete ledger command.
+     */
+    @Accessors(fluent = true)
+    @Setter
+    public static class DeleteLedgerFlags extends CliFlags {
+
+        @Parameter(names = { "-l", "--ledgerid" }, description = "Ledger ID", 
required = true)
+        private long ledgerId;
+
+        @Parameter(names = { "-f", "--force" }, description = "Whether to 
force delete the Ledger without prompt..?")
+        private boolean force;
+
+        @Parameter(names = { "-lf", "--ledgeridformatter" }, description = 
"Set ledger id formatter")
+        private String ledgerIdFormatter = DEFAULT;
+
+    }
+
+    @Override
+    public boolean apply(ServerConfiguration conf, DeleteLedgerFlags cmdFlags) 
{
+        initLedgerIdFormatter(conf, cmdFlags);
+        try {
+            return deleteLedger(conf, cmdFlags);
+        } catch (Exception e) {
+            throw new UncheckedExecutionException(e.getMessage(), e);
+        }
+    }
+
+    private void initLedgerIdFormatter(ServerConfiguration conf, 
DeleteLedgerFlags flags) {
+        if (null == ledgerIdFormatter && 
!flags.ledgerIdFormatter.equals(DEFAULT)) {
+            this.ledgerIdFormatter = 
LedgerIdFormatter.newLedgerIdFormatter(flags.ledgerIdFormatter, conf);
+        } else if (null == ledgerIdFormatter && 
flags.ledgerIdFormatter.equals(DEFAULT)) {
+            this.ledgerIdFormatter = 
LedgerIdFormatter.newLedgerIdFormatter(conf);
+        }
+    }
+
+    private boolean deleteLedger(ServerConfiguration conf, DeleteLedgerFlags 
flags)
+        throws IOException, BKException, InterruptedException {
+
+        if (flags.ledgerId < 0) {
+            System.err.println("Ledger id error.");
+            return false;
+        }
+
+        boolean confirm = false;
+        if (!flags.force) {
+            confirm = IOUtils.confirmPrompt(
+                "Are your sure to delete Ledger : " + 
ledgerIdFormatter.formatLedgerId(flags.ledgerId) + "?");
+        }
+
+        BookKeeper bookKeeper = null;
+        try {
+            if (flags.force || confirm) {
+                ClientConfiguration configuration = new ClientConfiguration();
+                configuration.addConfiguration(conf);
+                bookKeeper = new BookKeeper(configuration);
+                bookKeeper.deleteLedger(flags.ledgerId);
+            }
+        } finally {
+            if (bookKeeper != null) {
+                bookKeeper.close();
+            }
+        }
+
+        return true;
+    }
+}
diff --git 
a/tools/ledger/src/main/java/org/apache/bookkeeper/tools/cli/commands/LedgerCommandGroup.java
 
b/tools/ledger/src/main/java/org/apache/bookkeeper/tools/cli/commands/LedgerCommandGroup.java
index c528656..86734bc 100644
--- 
a/tools/ledger/src/main/java/org/apache/bookkeeper/tools/cli/commands/LedgerCommandGroup.java
+++ 
b/tools/ledger/src/main/java/org/apache/bookkeeper/tools/cli/commands/LedgerCommandGroup.java
@@ -21,6 +21,7 @@ package org.apache.bookkeeper.tools.cli.commands;
 import static 
org.apache.bookkeeper.tools.common.BKCommandCategories.CATEGORY_LEDGER_SERVICE;
 
 import org.apache.bookkeeper.tools.cli.BKCtl;
+import org.apache.bookkeeper.tools.cli.commands.client.DeleteLedgerCommand;
 import org.apache.bookkeeper.tools.cli.commands.client.SimpleTestCommand;
 import org.apache.bookkeeper.tools.common.BKFlags;
 import org.apache.bookkeeper.tools.framework.CliCommandGroup;
@@ -40,6 +41,7 @@ public class LedgerCommandGroup extends 
CliCommandGroup<BKFlags> {
         .withParent(BKCtl.NAME)
         .withCategory(CATEGORY_LEDGER_SERVICE)
         .addCommand(new SimpleTestCommand())
+        .addCommand(new DeleteLedgerCommand())
         .build();
 
     public LedgerCommandGroup() {
diff --git 
a/tools/ledger/src/test/java/org/apache/bookkeeper/tools/cli/commands/client/DeleteLedgerCommandTest.java
 
b/tools/ledger/src/test/java/org/apache/bookkeeper/tools/cli/commands/client/DeleteLedgerCommandTest.java
new file mode 100644
index 0000000..b95725a
--- /dev/null
+++ 
b/tools/ledger/src/test/java/org/apache/bookkeeper/tools/cli/commands/client/DeleteLedgerCommandTest.java
@@ -0,0 +1,97 @@
+/*
+ * 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.client;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import org.apache.bookkeeper.client.BookKeeper;
+import org.apache.bookkeeper.conf.ClientConfiguration;
+import org.apache.bookkeeper.conf.ServerConfiguration;
+import org.apache.bookkeeper.tools.cli.helpers.BookieCommandTestBase;
+import org.apache.bookkeeper.util.IOUtils;
+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 DeleteLedgerCommand}.
+ */
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({ DeleteLedgerCommand.class, BookKeeper.class, IOUtils.class, 
ClientConfiguration.class })
+public class DeleteLedgerCommandTest extends BookieCommandTestBase {
+
+    private BookKeeper bookKeeper;
+    private ClientConfiguration clientConf;
+
+    public DeleteLedgerCommandTest() {
+        super(3, 0);
+    }
+
+    @Override
+    public void setup() throws Exception {
+
+        this.clientConf = mock(ClientConfiguration.class);
+        
PowerMockito.whenNew(ClientConfiguration.class).withNoArguments().thenReturn(clientConf);
+        PowerMockito.doNothing().when(clientConf).addConfiguration(eq(conf));
+
+        this.bookKeeper = mock(BookKeeper.class);
+        
PowerMockito.whenNew(BookKeeper.class).withParameterTypes(ClientConfiguration.class)
+                    .withArguments(eq(clientConf)).thenReturn(bookKeeper);
+        PowerMockito.doNothing().when(bookKeeper).deleteLedger(anyLong());
+        PowerMockito.doNothing().when(bookKeeper).close();
+
+        PowerMockito.mockStatic(IOUtils.class);
+
+    }
+
+    @Test
+    public void testCommandWithoutForce() throws Exception {
+        PowerMockito.when(IOUtils.class, "confirmPrompt", 
anyString()).thenReturn(false);
+
+        DeleteLedgerCommand cmd = new DeleteLedgerCommand();
+        Assert.assertTrue(cmd.apply(bkFlags, new String[] { "-l", "1" }));
+
+        PowerMockito.verifyNew(ClientConfiguration.class, 
never()).withNoArguments();
+        verify(clientConf, never()).addConfiguration(conf);
+        PowerMockito.verifyNew(BookKeeper.class, 
never()).withArguments(eq(clientConf));
+        verify(bookKeeper, never()).deleteLedger(1);
+    }
+
+    @Test
+    public void testCommandWithForce() throws Exception {
+        DeleteLedgerCommand cmd = new DeleteLedgerCommand();
+        Assert.assertTrue(cmd.apply(bkFlags, new String[] { "-l", "1", "-f" 
}));
+
+        PowerMockito.verifyNew(ClientConfiguration.class, 
times(1)).withNoArguments();
+        verify(clientConf, 
times(1)).addConfiguration(any(ServerConfiguration.class));
+        PowerMockito.verifyNew(BookKeeper.class, 
times(1)).withArguments(eq(clientConf));
+        verify(bookKeeper, times(1)).deleteLedger(1);
+    }
+
+}

Reply via email to