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 6b602f7 Migrate command `bookieinfo` to bkctl
6b602f7 is described below
commit 6b602f77883ceaf926e8246ba622c54d048b55a3
Author: Yong Zhang <[email protected]>
AuthorDate: Sun Mar 10 18:32:27 2019 +0800
Migrate command `bookieinfo` to bkctl
### Motivation
Migrate command `bookieinfo` to bkctl.
### Changes
You can execute as `bin/bkctl bookie info`.
Reviewers: Sijie Guo <[email protected]>
This closes #1972 from zymap/yongzhang/command_info
---
.../org/apache/bookkeeper/bookie/BookieShell.java | 44 +--------
.../tools/cli/commands/bookies/InfoCommand.java | 103 +++++++++++++++++++++
.../tools/cli/commands/BookiesCommandGroup.java | 2 +
.../cli/commands/bookies/InfoCommandTest.java | 97 +++++++++++++++++++
4 files changed, 205 insertions(+), 41 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 9df7f49..33f9f9a 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
@@ -38,7 +38,6 @@ import io.netty.util.concurrent.DefaultThreadFactory;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
-import java.math.RoundingMode;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.file.FileSystems;
@@ -47,7 +46,6 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
-import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
@@ -87,7 +85,6 @@ import
org.apache.bookkeeper.client.BKException.MetaStoreException;
import org.apache.bookkeeper.client.BookKeeper;
import org.apache.bookkeeper.client.BookKeeper.DigestType;
import org.apache.bookkeeper.client.BookKeeperAdmin;
-import org.apache.bookkeeper.client.BookieInfoReader.BookieInfo;
import org.apache.bookkeeper.client.LedgerEntry;
import org.apache.bookkeeper.client.LedgerHandle;
import org.apache.bookkeeper.client.UpdateLedgerOp;
@@ -113,6 +110,7 @@ import
org.apache.bookkeeper.replication.ReplicationException.CompatibilityExcep
import
org.apache.bookkeeper.replication.ReplicationException.UnavailableException;
import org.apache.bookkeeper.stats.NullStatsLogger;
import org.apache.bookkeeper.tools.cli.commands.bookie.LastMarkCommand;
+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.client.SimpleTestCommand;
import org.apache.bookkeeper.tools.cli.commands.cookie.CreateCookieCommand;
@@ -2538,46 +2536,10 @@ public class BookieShell implements Tool {
return lOpts;
}
- String getReadable(long val) {
- String unit[] = {"", "KB", "MB", "GB", "TB"};
- int cnt = 0;
- double d = val;
- while (d >= 1000 && cnt < unit.length - 1) {
- d = d / 1000;
- cnt++;
- }
- DecimalFormat df = new DecimalFormat("#.###");
- df.setRoundingMode(RoundingMode.DOWN);
- return cnt > 0 ? "(" + df.format(d) + unit[cnt] + ")" : unit[cnt];
- }
-
@Override
public int runCmd(CommandLine cmdLine) throws Exception {
- ClientConfiguration clientConf = new ClientConfiguration(bkConf);
- clientConf.setDiskWeightBasedPlacementEnabled(true);
- BookKeeper bk = new BookKeeper(clientConf);
-
- Map<BookieSocketAddress, BookieInfo> map = bk.getBookieInfo();
- if (map.size() == 0) {
- System.out.println("Failed to retrieve bookie information from
any of the bookies");
- bk.close();
- return 0;
- }
-
- System.out.println("Free disk space info:");
- long totalFree = 0, total = 0;
- for (Map.Entry<BookieSocketAddress, BookieInfo> e :
map.entrySet()) {
- BookieInfo bInfo = e.getValue();
- BookieSocketAddress bookieId = e.getKey();
-
System.out.println(getBookieSocketAddrStringRepresentation(bookieId) +
":\tFree: "
- + bInfo.getFreeDiskSpace() +
getReadable(bInfo.getFreeDiskSpace()) + "\tTotal: "
- + bInfo.getTotalDiskSpace() +
getReadable(bInfo.getTotalDiskSpace()));
- totalFree += bInfo.getFreeDiskSpace();
- total += bInfo.getTotalDiskSpace();
- }
- System.out.println("Total free disk space in the cluster:\t" +
totalFree + getReadable(totalFree));
- System.out.println("Total disk capacity in the cluster:\t" + total
+ getReadable(total));
- bk.close();
+ InfoCommand cmd = new InfoCommand();
+ cmd.apply(bkConf, new CliFlags());
return 0;
}
}
diff --git
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/bookies/InfoCommand.java
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/bookies/InfoCommand.java
new file mode 100644
index 0000000..2e49d39
--- /dev/null
+++
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/bookies/InfoCommand.java
@@ -0,0 +1,103 @@
+/*
+ * 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 java.io.IOException;
+import java.math.RoundingMode;
+import java.text.DecimalFormat;
+import java.util.Map;
+
+import org.apache.bookkeeper.client.BKException;
+import org.apache.bookkeeper.client.BookKeeper;
+import org.apache.bookkeeper.client.BookieInfoReader.BookieInfo;
+import org.apache.bookkeeper.conf.ClientConfiguration;
+import org.apache.bookkeeper.conf.ServerConfiguration;
+import org.apache.bookkeeper.net.BookieSocketAddress;
+import org.apache.bookkeeper.tools.cli.helpers.BookieCommand;
+import org.apache.bookkeeper.tools.cli.helpers.CommandHelpers;
+import org.apache.bookkeeper.tools.framework.CliFlags;
+import org.apache.bookkeeper.tools.framework.CliSpec;
+
+
+/**
+ * A bookie command to retrieve bookie info.
+ */
+public class InfoCommand extends BookieCommand<CliFlags> {
+
+ private static final String NAME = "info";
+ private static final String DESC = "Retrieve bookie info such as free and
total disk space.";
+
+ public InfoCommand() {
+ super(CliSpec.newBuilder()
+ .withName(NAME)
+ .withFlags(new CliFlags())
+ .withDescription(DESC)
+ .build());
+ }
+
+ String getReadable(long val) {
+ String unit[] = {"", "KB", "MB", "GB", "TB"};
+ int cnt = 0;
+ double d = val;
+ while (d >= 1000 && cnt < unit.length - 1) {
+ d = d / 1000;
+ cnt++;
+ }
+ DecimalFormat df = new DecimalFormat("#.###");
+ df.setRoundingMode(RoundingMode.DOWN);
+ return cnt > 0 ? "(" + df.format(d) + unit[cnt] + ")" : unit[cnt];
+ }
+
+
+ @Override
+ public boolean apply(ServerConfiguration conf, CliFlags cmdFlags) {
+
+ ClientConfiguration clientConf = new ClientConfiguration(conf);
+ clientConf.setDiskWeightBasedPlacementEnabled(true);
+ try (BookKeeper bk = new BookKeeper(clientConf)) {
+ Map<BookieSocketAddress, BookieInfo> map = bk.getBookieInfo();
+ if (map.size() == 0) {
+ System.out.println("Failed to retrieve bookie information from
any of the bookies");
+ bk.close();
+ return true;
+ }
+
+ System.out.println("Free disk space info:");
+ long totalFree = 0, total = 0;
+ for (Map.Entry<BookieSocketAddress, BookieInfo> e :
map.entrySet()) {
+ BookieInfo bInfo = e.getValue();
+ BookieSocketAddress bookieId = e.getKey();
+
System.out.println(CommandHelpers.getBookieSocketAddrStringRepresentation(bookieId)
+ + ":\tFree: " + bInfo.getFreeDiskSpace() +
getReadable(bInfo.getFreeDiskSpace())
+ + "\tTotal: " + bInfo.getTotalDiskSpace() +
getReadable(bInfo.getTotalDiskSpace()));
+ totalFree += bInfo.getFreeDiskSpace();
+ total += bInfo.getTotalDiskSpace();
+ }
+
+ System.out.println("Total free disk space in the cluster:\t" +
totalFree + getReadable(totalFree));
+ System.out.println("Total disk capacity in the cluster:\t" + total
+ getReadable(total));
+ bk.close();
+
+ return true;
+ } catch (IOException | InterruptedException | BKException e) {
+ e.printStackTrace();
+ }
+ return true;
+ }
+}
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 6b86de7..b299b5c 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
@@ -21,6 +21,7 @@ package org.apache.bookkeeper.tools.cli.commands;
import static
org.apache.bookkeeper.tools.common.BKCommandCategories.CATEGORY_INFRA_SERVICE;
import org.apache.bookkeeper.tools.cli.BKCtl;
+import org.apache.bookkeeper.tools.cli.commands.bookies.InfoCommand;
import org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand;
import org.apache.bookkeeper.tools.common.BKFlags;
import org.apache.bookkeeper.tools.framework.CliCommandGroup;
@@ -40,6 +41,7 @@ public class BookiesCommandGroup extends
CliCommandGroup<BKFlags> {
.withParent(BKCtl.NAME)
.withCategory(CATEGORY_INFRA_SERVICE)
.addCommand(new ListBookiesCommand())
+ .addCommand(new InfoCommand())
.build();
public BookiesCommandGroup() {
diff --git
a/tools/ledger/src/test/java/org/apache/bookkeeper/tools/cli/commands/bookies/InfoCommandTest.java
b/tools/ledger/src/test/java/org/apache/bookkeeper/tools/cli/commands/bookies/InfoCommandTest.java
new file mode 100644
index 0000000..05575cb
--- /dev/null
+++
b/tools/ledger/src/test/java/org/apache/bookkeeper/tools/cli/commands/bookies/InfoCommandTest.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.bookies;
+
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.bookkeeper.client.BookKeeper;
+import org.apache.bookkeeper.client.BookieInfoReader;
+import org.apache.bookkeeper.conf.AbstractConfiguration;
+import org.apache.bookkeeper.conf.ClientConfiguration;
+import org.apache.bookkeeper.conf.ServerConfiguration;
+import org.apache.bookkeeper.net.BookieSocketAddress;
+import org.apache.bookkeeper.tools.cli.helpers.BookieCommandTestBase;
+import org.junit.Before;
+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 of {@link InfoCommand}.
+ */
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({InfoCommand.class})
+public class InfoCommandTest extends BookieCommandTestBase {
+
+ private BookieSocketAddress bookieId;
+ private BookieInfoReader.BookieInfo bInfo;
+ private BookKeeper bk;
+ private Map<BookieSocketAddress, BookieInfoReader.BookieInfo> map = new
HashMap<>();
+
+ public InfoCommandTest() {
+ super(1, 0);
+ }
+
+ @Before
+ public void setup() throws Exception {
+ super.setup();
+
+ PowerMockito.whenNew(ServerConfiguration.class)
+ .withNoArguments()
+ .thenReturn(conf);
+
+ PowerMockito.whenNew(ClientConfiguration.class)
+ .withParameterTypes(AbstractConfiguration.class)
+ .withArguments(eq(conf))
+ .thenReturn(mock(ClientConfiguration.class));
+
+ this.bk = mock(BookKeeper.class);
+ PowerMockito.whenNew(BookKeeper.class)
+ .withParameterTypes(ClientConfiguration.class)
+ .withArguments(any(ClientConfiguration.class))
+ .thenReturn(bk);
+
+ this.bookieId = new BookieSocketAddress("localhost", 9999);
+ this.bInfo = mock(BookieInfoReader.BookieInfo.class);
+ map.put(bookieId, bInfo);
+ when(bk.getBookieInfo()).thenReturn(map);
+ }
+
+ @Test
+ public void testCommand() throws Exception {
+ InfoCommand cmd = new InfoCommand();
+ cmd.apply(bkFlags, new String[]{""});
+
+ PowerMockito.verifyNew(ClientConfiguration.class, times(1))
+ .withArguments(eq(conf));
+ PowerMockito.verifyNew(BookKeeper.class, times(1))
+ .withArguments(any(ClientConfiguration.class));
+
+ verify(bk, times(1)).getBookieInfo();
+ verify(bInfo, times(1 * 3)).getFreeDiskSpace();
+ verify(bInfo, times(1 * 3)).getTotalDiskSpace();
+ }
+}