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 5b8e097  Migrate command `Whatisinstanceid`
5b8e097 is described below

commit 5b8e0971a35f0c0ee3e237b612a52c12a8c4c923
Author: Yong Zhang <[email protected]>
AuthorDate: Thu Apr 11 10:19:47 2019 +0800

    Migrate command `Whatisinstanceid`
    
    Descriptions of the changes in this PR:
    
    #2027
    
    
    
    Reviewers: Enrico Olivelli <[email protected]>, Sijie Guo 
<[email protected]>
    
    This closes #2028 from zymap/whatisinstanceid
---
 .../org/apache/bookkeeper/bookie/BookieShell.java  | 14 +----
 .../cli/commands/bookies/InstanceIdCommand.java    | 65 ++++++++++++++++++++
 .../tools/cli/commands/BookiesCommandGroup.java    |  2 +
 .../commands/bookies/InstanceIdCommandTest.java    | 69 ++++++++++++++++++++++
 4 files changed, 139 insertions(+), 11 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 f87eb0d..2177aa6 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
@@ -83,6 +83,7 @@ import 
org.apache.bookkeeper.tools.cli.commands.bookie.RegenerateInterleavedStor
 import org.apache.bookkeeper.tools.cli.commands.bookie.SanityTestCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookies.DecommissionCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookies.InfoCommand;
+import org.apache.bookkeeper.tools.cli.commands.bookies.InstanceIdCommand;
 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;
@@ -1458,17 +1459,8 @@ public class BookieShell implements Tool {
 
         @Override
         int runCmd(CommandLine cmdLine) throws Exception {
-            runFunctionWithRegistrationManager(bkConf, rm -> {
-                String readInstanceId = null;
-                try {
-                    readInstanceId = rm.getClusterInstanceId();
-                } catch (BookieException e) {
-                    throw new UncheckedExecutionException(e);
-                }
-                LOG.info("Metadata Service Uri: {} InstanceId: {}",
-                    bkConf.getMetadataServiceUriUnchecked(), readInstanceId);
-                return null;
-            });
+            InstanceIdCommand cmd = new InstanceIdCommand();
+            cmd.apply(bkConf, new CliFlags());
             return 0;
         }
     }
diff --git 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/bookies/InstanceIdCommand.java
 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/bookies/InstanceIdCommand.java
new file mode 100644
index 0000000..6fd60e3
--- /dev/null
+++ 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/bookies/InstanceIdCommand.java
@@ -0,0 +1,65 @@
+/*
+ * 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.apache.bookkeeper.meta.MetadataDrivers.runFunctionWithRegistrationManager;
+
+import com.google.common.util.concurrent.UncheckedExecutionException;
+import org.apache.bookkeeper.bookie.BookieException;
+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 print instance id of the cluster.
+ */
+public class InstanceIdCommand extends BookieCommand<CliFlags> {
+
+    static final Logger LOG = LoggerFactory.getLogger(InstanceIdCommand.class);
+
+    private static final String NAME = "instanceid";
+    private static final String DESC = "Print the instanceid of the cluster";
+
+    public InstanceIdCommand() {
+        
super(CliSpec.newBuilder().withName(NAME).withDescription(DESC).withFlags(new 
CliFlags()).build());
+    }
+
+    @Override
+    public boolean apply(ServerConfiguration conf, CliFlags cmdFlags) {
+        try {
+            runFunctionWithRegistrationManager(conf, rm -> {
+                String readInstanceId = null;
+                try {
+                    readInstanceId = rm.getClusterInstanceId();
+                } catch (BookieException e) {
+                    throw new UncheckedExecutionException(e);
+                }
+                LOG.info("Metadata Service Uri: {} InstanceId: {}",
+                         conf.getMetadataServiceUriUnchecked(), 
readInstanceId);
+                return null;
+            });
+        } catch (Exception e) {
+            throw new UncheckedExecutionException(e.getMessage(), e);
+        }
+        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 cb161f5..c7a44d4 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
@@ -24,6 +24,7 @@ import org.apache.bookkeeper.tools.cli.BKCtl;
 import org.apache.bookkeeper.tools.cli.commands.bookies.DecommissionCommand;
 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.InstanceIdCommand;
 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;
@@ -52,6 +53,7 @@ public class BookiesCommandGroup extends 
CliCommandGroup<BKFlags> {
         .addCommand(new DecommissionCommand())
         .addCommand(new InitCommand())
         .addCommand(new RecoverCommand())
+        .addCommand(new InstanceIdCommand())
         .build();
 
     public BookiesCommandGroup() {
diff --git 
a/tools/ledger/src/test/java/org/apache/bookkeeper/tools/cli/commands/bookies/InstanceIdCommandTest.java
 
b/tools/ledger/src/test/java/org/apache/bookkeeper/tools/cli/commands/bookies/InstanceIdCommandTest.java
new file mode 100644
index 0000000..5f2e716
--- /dev/null
+++ 
b/tools/ledger/src/test/java/org/apache/bookkeeper/tools/cli/commands/bookies/InstanceIdCommandTest.java
@@ -0,0 +1,69 @@
+/*
+ * 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.powermock.api.mockito.PowerMockito.mock;
+import static org.powermock.api.mockito.PowerMockito.when;
+
+import java.util.function.Function;
+import org.apache.bookkeeper.conf.ServerConfiguration;
+import org.apache.bookkeeper.discover.RegistrationManager;
+import org.apache.bookkeeper.meta.MetadataDrivers;
+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 InstanceIdCommand}.
+ */
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({ InstanceIdCommand.class, MetadataDrivers.class, 
RegistrationManager.class })
+public class InstanceIdCommandTest extends BookieCommandTestBase {
+
+    public InstanceIdCommandTest() {
+        super(3, 0);
+    }
+
+    @Override
+    public void setup() throws Exception {
+        super.setup();
+
+        PowerMockito.mockStatic(MetadataDrivers.class);
+
+        RegistrationManager manager = mock(RegistrationManager.class);
+        PowerMockito.doAnswer(invocationOnMock -> {
+            Function<RegistrationManager, ?> function = 
invocationOnMock.getArgument(1);
+            function.apply(manager);
+            return null;
+        }).when(MetadataDrivers.class, "runFunctionWithRegistrationManager",
+                any(ServerConfiguration.class), any(Function.class));
+        when(manager.getClusterInstanceId()).thenReturn("");
+    }
+
+    @Test
+    public void testCommand() {
+        InstanceIdCommand cmd = new InstanceIdCommand();
+        Assert.assertTrue(cmd.apply(bkFlags, new String[] {}));
+    }
+}

Reply via email to