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 90c7944  Migrate command `rebuild-db-ledger-locations-index`
90c7944 is described below

commit 90c79444d6211cd66f4259c04c4e246097821b75
Author: Yong Zhang <[email protected]>
AuthorDate: Wed Apr 10 19:18:17 2019 +0800

    Migrate command `rebuild-db-ledger-locations-index`
    
    Descriptions of the changes in this PR:
    
    #2036
    
    
    Reviewers: Sijie Guo <[email protected]>
    
    This closes #2034 from zymap/command-RDBLLI
---
 .../org/apache/bookkeeper/bookie/BookieShell.java  |  8 +--
 .../RebuildDBLedgerLocationsIndexCommand.java      | 56 +++++++++++++++++
 .../tools/cli/commands/BookieCommandGroup.java     |  2 +
 .../RebuildDBLedgerLocationsIndexCommandTest.java  | 71 ++++++++++++++++++++++
 4 files changed, 132 insertions(+), 5 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 f3f83a3..f87eb0d 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
@@ -49,7 +49,6 @@ import java.util.Optional;
 import java.util.stream.Collectors;
 import org.apache.bookkeeper.bookie.BookieException.CookieNotFoundException;
 import org.apache.bookkeeper.bookie.BookieException.InvalidCookieException;
-import org.apache.bookkeeper.bookie.storage.ldb.LocationsIndexRebuildOp;
 import org.apache.bookkeeper.client.BookKeeperAdmin;
 import org.apache.bookkeeper.client.LedgerEntry;
 import org.apache.bookkeeper.client.api.LedgerMetadata;
@@ -79,6 +78,7 @@ import 
org.apache.bookkeeper.tools.cli.commands.bookie.ReadJournalCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookie.ReadLedgerCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookie.ReadLogCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookie.ReadLogMetadataCommand;
+import 
org.apache.bookkeeper.tools.cli.commands.bookie.RebuildDBLedgerLocationsIndexCommand;
 import 
org.apache.bookkeeper.tools.cli.commands.bookie.RegenerateInterleavedStorageIndexFileCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookie.SanityTestCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookies.DecommissionCommand;
@@ -2083,10 +2083,8 @@ public class BookieShell implements Tool {
 
         @Override
         int runCmd(CommandLine cmdLine) throws Exception {
-            LOG.info("=== Rebuilding bookie index ===");
-            ServerConfiguration conf = new ServerConfiguration(bkConf);
-            new LocationsIndexRebuildOp(conf).initiate();
-            LOG.info("-- Done rebuilding bookie index --");
+            RebuildDBLedgerLocationsIndexCommand cmd = new 
RebuildDBLedgerLocationsIndexCommand();
+            cmd.apply(bkConf, new CliFlags());
             return 0;
         }
     }
diff --git 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/bookie/RebuildDBLedgerLocationsIndexCommand.java
 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/bookie/RebuildDBLedgerLocationsIndexCommand.java
new file mode 100644
index 0000000..fda5945
--- /dev/null
+++ 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/bookie/RebuildDBLedgerLocationsIndexCommand.java
@@ -0,0 +1,56 @@
+/*
+ * 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.bookie;
+
+import java.io.IOException;
+import org.apache.bookkeeper.bookie.storage.ldb.LocationsIndexRebuildOp;
+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;
+
+/**
+ * Command to rebuild DBLedgerStorage locations index.
+ */
+public class RebuildDBLedgerLocationsIndexCommand extends 
BookieCommand<CliFlags> {
+
+    static final Logger LOG = 
LoggerFactory.getLogger(RebuildDBLedgerLocationsIndexCommand.class);
+
+    private static final String NAME = "rebuild-db-ledger-locations-index";
+    private static final String DESC = "Rbuild DBLedgerStorage locations index 
by scanning the entry logs";
+
+    public RebuildDBLedgerLocationsIndexCommand() {
+        
super(CliSpec.newBuilder().withName(NAME).withDescription(DESC).withFlags(new 
CliFlags()).build());
+    }
+
+    @Override
+    public boolean apply(ServerConfiguration conf, CliFlags cmdFlags) {
+        LOG.info("=== Rebuilding bookie index ===");
+        ServerConfiguration serverConfiguration = new 
ServerConfiguration(conf);
+        try {
+            new LocationsIndexRebuildOp(serverConfiguration).initiate();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        LOG.info("-- Done rebuilding bookie index --");
+        return true;
+    }
+}
diff --git 
a/tools/ledger/src/main/java/org/apache/bookkeeper/tools/cli/commands/BookieCommandGroup.java
 
b/tools/ledger/src/main/java/org/apache/bookkeeper/tools/cli/commands/BookieCommandGroup.java
index d7eca51..02ccf47 100644
--- 
a/tools/ledger/src/main/java/org/apache/bookkeeper/tools/cli/commands/BookieCommandGroup.java
+++ 
b/tools/ledger/src/main/java/org/apache/bookkeeper/tools/cli/commands/BookieCommandGroup.java
@@ -35,6 +35,7 @@ import 
org.apache.bookkeeper.tools.cli.commands.bookie.ReadJournalCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookie.ReadLedgerCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookie.ReadLogCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookie.ReadLogMetadataCommand;
+import 
org.apache.bookkeeper.tools.cli.commands.bookie.RebuildDBLedgerLocationsIndexCommand;
 import 
org.apache.bookkeeper.tools.cli.commands.bookie.RegenerateInterleavedStorageIndexFileCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookie.SanityTestCommand;
 import org.apache.bookkeeper.tools.common.BKFlags;
@@ -64,6 +65,7 @@ public class BookieCommandGroup extends 
CliCommandGroup<BKFlags> {
         .addCommand(new ListLedgersCommand())
         .addCommand(new ConvertToInterleavedStorageCommand())
         .addCommand(new ReadJournalCommand())
+        .addCommand(new RebuildDBLedgerLocationsIndexCommand())
         .addCommand(new ReadLedgerCommand())
         .addCommand(new ReadLogCommand())
         .addCommand(new ReadLogMetadataCommand())
diff --git 
a/tools/ledger/src/test/java/org/apache/bookkeeper/tools/cli/commands/bookie/RebuildDBLedgerLocationsIndexCommandTest.java
 
b/tools/ledger/src/test/java/org/apache/bookkeeper/tools/cli/commands/bookie/RebuildDBLedgerLocationsIndexCommandTest.java
new file mode 100644
index 0000000..658d156
--- /dev/null
+++ 
b/tools/ledger/src/test/java/org/apache/bookkeeper/tools/cli/commands/bookie/RebuildDBLedgerLocationsIndexCommandTest.java
@@ -0,0 +1,71 @@
+/*
+ * 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.bookie;
+
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import org.apache.bookkeeper.bookie.storage.ldb.LocationsIndexRebuildOp;
+import org.apache.bookkeeper.conf.AbstractConfiguration;
+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.mockito.Mock;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+/**
+ * Unit test for {@link RebuildDBLedgerLocationsIndexCommand}.
+ */
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({ RebuildDBLedgerLocationsIndexCommand.class })
+public class RebuildDBLedgerLocationsIndexCommandTest extends 
BookieCommandTestBase {
+
+    @Mock
+    private LocationsIndexRebuildOp locationsIndexRebuildOp;
+
+    public RebuildDBLedgerLocationsIndexCommandTest() {
+        super(3, 0);
+    }
+
+    @Override
+    public void setup() throws Exception {
+        
PowerMockito.whenNew(ServerConfiguration.class).withNoArguments().thenReturn(conf);
+        
PowerMockito.whenNew(ServerConfiguration.class).withParameterTypes(AbstractConfiguration.class)
+                    .withArguments(eq(conf)).thenReturn(conf);
+
+        
PowerMockito.whenNew(LocationsIndexRebuildOp.class).withParameterTypes(ServerConfiguration.class)
+                    
.withArguments(eq(conf)).thenReturn(locationsIndexRebuildOp);
+        PowerMockito.doNothing().when(locationsIndexRebuildOp).initiate();
+    }
+
+    @Test
+    public void testCommand() throws Exception {
+        RebuildDBLedgerLocationsIndexCommand command = new 
RebuildDBLedgerLocationsIndexCommand();
+        Assert.assertTrue(command.apply(bkFlags, new String[] { "" }));
+
+        PowerMockito.verifyNew(ServerConfiguration.class, 
times(1)).withArguments(eq(conf));
+        PowerMockito.verifyNew(LocationsIndexRebuildOp.class, 
times(1)).withArguments(eq(conf));
+        verify(locationsIndexRebuildOp, times(1)).initiate();
+    }
+}

Reply via email to