This is an automated email from the ASF dual-hosted git repository.

zhaijia 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 ed008f2  Migrate command `whoisauditor`
ed008f2 is described below

commit ed008f27807a75ba384246fc7fae6ca3a5a6971f
Author: Yong Zhang <[email protected]>
AuthorDate: Mon Apr 15 14:00:56 2019 +0800

    Migrate command `whoisauditor`
    
    Descriptions of the changes in this PR:
    
    - Using `bkctl` run command `whoisauditor`
    
    ### Motivation
    
    #2007
    
    
    Reviewers: Sijie Guo <[email protected]>, Jia Zhai <[email protected]>
    
    This closes #2008 from zymap/command-whoisauditor
---
 .../org/apache/bookkeeper/bookie/BookieShell.java  | 32 ++------
 .../commands/autorecovery/WhoIsAuditorCommand.java | 90 ++++++++++++++++++++++
 .../cli/commands/AutoRecoveryCommandGroup.java     | 16 ++--
 tools/ledger/src/main/resources/commands           |  2 +-
 .../autorecovery/WhoIsAuditorCommandTest.java      | 88 +++++++++++++++++++++
 5 files changed, 191 insertions(+), 37 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 2177aa6..3f307f5 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
@@ -21,7 +21,6 @@ package org.apache.bookkeeper.bookie;
 import static 
org.apache.bookkeeper.meta.MetadataDrivers.runFunctionWithLedgerManagerFactory;
 import static 
org.apache.bookkeeper.meta.MetadataDrivers.runFunctionWithMetadataBookieDriver;
 import static 
org.apache.bookkeeper.meta.MetadataDrivers.runFunctionWithRegistrationManager;
-import static 
org.apache.bookkeeper.tools.cli.helpers.CommandHelpers.getBookieSocketAddrStringRepresentation;
 
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.Lists;
@@ -29,7 +28,6 @@ import 
com.google.common.util.concurrent.UncheckedExecutionException;
 import java.io.File;
 import java.io.IOException;
 import java.io.Serializable;
-import java.net.URI;
 import java.nio.file.FileSystems;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -58,12 +56,11 @@ import org.apache.bookkeeper.conf.ServerConfiguration;
 import org.apache.bookkeeper.meta.LedgerManager;
 import org.apache.bookkeeper.meta.LedgerMetadataSerDe;
 import org.apache.bookkeeper.meta.LedgerUnderreplicationManager;
-import org.apache.bookkeeper.meta.zk.ZKMetadataDriverBase;
 import org.apache.bookkeeper.net.BookieSocketAddress;
-import org.apache.bookkeeper.replication.AuditorElector;
 import org.apache.bookkeeper.replication.ReplicationException;
 import 
org.apache.bookkeeper.tools.cli.commands.autorecovery.ListUnderReplicatedCommand;
 import 
org.apache.bookkeeper.tools.cli.commands.autorecovery.LostBookieRecoveryDelayCommand;
+import 
org.apache.bookkeeper.tools.cli.commands.autorecovery.WhoIsAuditorCommand;
 import 
org.apache.bookkeeper.tools.cli.commands.bookie.ConvertToDBStorageCommand;
 import 
org.apache.bookkeeper.tools.cli.commands.bookie.ConvertToInterleavedStorageCommand;
 import org.apache.bookkeeper.tools.cli.commands.bookie.FlipBookieIdCommand;
@@ -104,7 +101,6 @@ import org.apache.bookkeeper.util.LedgerIdFormatter;
 import org.apache.bookkeeper.util.Tool;
 import org.apache.bookkeeper.versioning.Version;
 import org.apache.bookkeeper.versioning.Versioned;
-import org.apache.bookkeeper.zookeeper.ZooKeeperClient;
 import org.apache.commons.cli.BasicParser;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.HelpFormatter;
@@ -119,7 +115,6 @@ import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.lang3.ArrayUtils;
 import org.apache.zookeeper.KeeperException;
-import org.apache.zookeeper.ZooKeeper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -1408,27 +1403,10 @@ public class BookieShell implements Tool {
 
         @Override
         int runCmd(CommandLine cmdLine) throws Exception {
-            ZooKeeper zk = null;
-            try {
-                String metadataServiceUri = bkConf.getMetadataServiceUri();
-                String zkServers = 
ZKMetadataDriverBase.getZKServersFromServiceUri(URI.create(metadataServiceUri));
-                zk = ZooKeeperClient.newBuilder()
-                        .connectString(zkServers)
-                        .sessionTimeoutMs(bkConf.getZkTimeout())
-                        .build();
-                BookieSocketAddress bookieId = 
AuditorElector.getCurrentAuditor(bkConf, zk);
-                if (bookieId == null) {
-                    LOG.info("No auditor elected");
-                    return -1;
-                }
-                LOG.info("Auditor: " + 
getBookieSocketAddrStringRepresentation(bookieId));
-            } finally {
-                if (zk != null) {
-                    zk.close();
-                }
-            }
-
-            return 0;
+            CliFlags flags = new CliFlags();
+            WhoIsAuditorCommand cmd = new WhoIsAuditorCommand();
+            boolean result = cmd.apply(bkConf, flags);
+            return result ? 0 : -1;
         }
     }
 
diff --git 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/autorecovery/WhoIsAuditorCommand.java
 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/autorecovery/WhoIsAuditorCommand.java
new file mode 100644
index 0000000..9653db8
--- /dev/null
+++ 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/autorecovery/WhoIsAuditorCommand.java
@@ -0,0 +1,90 @@
+/*
+ * 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.autorecovery;
+
+import static 
org.apache.bookkeeper.tools.cli.helpers.CommandHelpers.getBookieSocketAddrStringRepresentation;
+
+import com.google.common.util.concurrent.UncheckedExecutionException;
+import java.io.IOException;
+import java.net.URI;
+import org.apache.bookkeeper.conf.ServerConfiguration;
+import org.apache.bookkeeper.meta.zk.ZKMetadataDriverBase;
+import org.apache.bookkeeper.net.BookieSocketAddress;
+import org.apache.bookkeeper.replication.AuditorElector;
+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.zookeeper.ZooKeeperClient;
+import org.apache.commons.configuration.ConfigurationException;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.ZooKeeper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Command to print which node has the auditor lock.
+ */
+public class WhoIsAuditorCommand extends BookieCommand<CliFlags> {
+
+    static final Logger LOG = 
LoggerFactory.getLogger(WhoIsAuditorCommand.class);
+
+    private static final String NAME = "whoisauditor";
+    private static final String DESC = "Print the node which holds the auditor 
lock.";
+
+    public WhoIsAuditorCommand() {
+        super(CliSpec.newBuilder()
+                     .withName(NAME)
+                     .withDescription(DESC)
+                     .withFlags(new CliFlags())
+                     .build());
+    }
+
+    @Override
+    public boolean apply(ServerConfiguration conf, CliFlags cmdFlags) {
+        try {
+            return getAuditor(conf);
+        } catch (Exception e) {
+            throw new UncheckedExecutionException(e.getMessage(), e);
+        }
+    }
+
+    public boolean getAuditor(ServerConfiguration conf)
+        throws ConfigurationException, InterruptedException, IOException, 
KeeperException {
+        ZooKeeper zk = null;
+        try {
+            String metadataServiceUri = conf.getMetadataServiceUri();
+            String zkServers = 
ZKMetadataDriverBase.getZKServersFromServiceUri(URI.create(metadataServiceUri));
+            zk = ZooKeeperClient.newBuilder()
+                                .connectString(zkServers)
+                                .sessionTimeoutMs(conf.getZkTimeout())
+                                .build();
+            BookieSocketAddress bookieId = 
AuditorElector.getCurrentAuditor(conf, zk);
+            if (bookieId == null) {
+                LOG.info("No auditor elected");
+                return false;
+            }
+            LOG.info("Auditor: " + 
getBookieSocketAddrStringRepresentation(bookieId));
+        } finally {
+            if (zk != null) {
+                zk.close();
+            }
+        }
+        return true;
+    }
+}
diff --git 
a/tools/ledger/src/main/java/org/apache/bookkeeper/tools/cli/commands/AutoRecoveryCommandGroup.java
 
b/tools/ledger/src/main/java/org/apache/bookkeeper/tools/cli/commands/AutoRecoveryCommandGroup.java
index 9cee7ff..814976a 100644
--- 
a/tools/ledger/src/main/java/org/apache/bookkeeper/tools/cli/commands/AutoRecoveryCommandGroup.java
+++ 
b/tools/ledger/src/main/java/org/apache/bookkeeper/tools/cli/commands/AutoRecoveryCommandGroup.java
@@ -20,8 +20,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.commands.autorecovery.ListUnderReplicatedCommand;
-import 
org.apache.bookkeeper.tools.cli.commands.autorecovery.LostBookieRecoveryDelayCommand;
+import 
org.apache.bookkeeper.tools.cli.commands.autorecovery.WhoIsAuditorCommand;
 import org.apache.bookkeeper.tools.common.BKFlags;
 import org.apache.bookkeeper.tools.framework.CliCommandGroup;
 import org.apache.bookkeeper.tools.framework.CliSpec;
@@ -35,14 +34,13 @@ public class AutoRecoveryCommandGroup extends 
CliCommandGroup<BKFlags> {
     private static final String DESC = "Command on some specific operation.";
 
     private static final CliSpec<BKFlags> spec = CliSpec.<BKFlags>newBuilder()
-                                                     .withName(NAME)
-                                                     .withDescription(DESC)
-                                                     
.withCategory(CATEGORY_INFRA_SERVICE)
-                                                     .addCommand(new 
ListUnderReplicatedCommand())
-                                                     .addCommand(new 
LostBookieRecoveryDelayCommand())
-                                                     .build();
+        .withName(NAME)
+        .withDescription(DESC)
+        .withCategory(CATEGORY_INFRA_SERVICE)
+        .addCommand(new WhoIsAuditorCommand())
+        .build();
 
     public AutoRecoveryCommandGroup() {
         super(spec);
     }
-}
\ No newline at end of file
+}
diff --git a/tools/ledger/src/main/resources/commands 
b/tools/ledger/src/main/resources/commands
index c4d6412..d81ba5a 100644
--- a/tools/ledger/src/main/resources/commands
+++ b/tools/ledger/src/main/resources/commands
@@ -21,4 +21,4 @@ org.apache.bookkeeper.tools.cli.commands.BookieIdCommandGroup
 org.apache.bookkeeper.tools.cli.commands.BookiesCommandGroup
 org.apache.bookkeeper.tools.cli.commands.CookieCommandGroup
 org.apache.bookkeeper.tools.cli.commands.LedgerCommandGroup
-org.apache.bookkeeper.tools.cli.commands.AutoRecoveryCommandGroup
\ No newline at end of file
+org.apache.bookkeeper.tools.cli.commands.AutoRecoveryCommandGroup
diff --git 
a/tools/ledger/src/test/java/org/apache/bookkeeper/tools/cli/commands/autorecovery/WhoIsAuditorCommandTest.java
 
b/tools/ledger/src/test/java/org/apache/bookkeeper/tools/cli/commands/autorecovery/WhoIsAuditorCommandTest.java
new file mode 100644
index 0000000..b4f248a
--- /dev/null
+++ 
b/tools/ledger/src/test/java/org/apache/bookkeeper/tools/cli/commands/autorecovery/WhoIsAuditorCommandTest.java
@@ -0,0 +1,88 @@
+/*
+ * 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.autorecovery;
+
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.powermock.api.mockito.PowerMockito.mock;
+import static org.powermock.api.mockito.PowerMockito.when;
+
+import java.net.URI;
+import org.apache.bookkeeper.conf.ServerConfiguration;
+import org.apache.bookkeeper.meta.zk.ZKMetadataDriverBase;
+import org.apache.bookkeeper.net.BookieSocketAddress;
+import org.apache.bookkeeper.replication.AuditorElector;
+import org.apache.bookkeeper.tools.cli.helpers.BookieCommandTestBase;
+import org.apache.bookkeeper.tools.cli.helpers.CommandHelpers;
+import org.apache.bookkeeper.zookeeper.ZooKeeperClient;
+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 WhoIsAuditorCommand}.
+ */
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({ WhoIsAuditorCommand.class, ZKMetadataDriverBase.class, 
ZooKeeperClient.class, AuditorElector.class,
+    CommandHelpers.class
+})
+public class WhoIsAuditorCommandTest extends BookieCommandTestBase {
+
+    public WhoIsAuditorCommandTest() {
+        super(3, 0);
+    }
+
+    @Override
+    public void setup() throws Exception {
+        super.setup();
+
+        
PowerMockito.whenNew(ServerConfiguration.class).withNoArguments().thenReturn(conf);
+
+        PowerMockito.mockStatic(ZKMetadataDriverBase.class);
+        
PowerMockito.when(ZKMetadataDriverBase.getZKServersFromServiceUri(eq(URI.create(conf.getMetadataServiceUri()))))
+                    .thenReturn("");
+
+        ZooKeeperClient zk = mock(ZooKeeperClient.class);
+        ZooKeeperClient.Builder builder = mock(ZooKeeperClient.Builder.class);
+        PowerMockito.mockStatic(ZooKeeperClient.class);
+        PowerMockito.when(ZooKeeperClient.newBuilder()).thenReturn(builder);
+        when(builder.connectString(anyString())).thenReturn(builder);
+        when(builder.sessionTimeoutMs(anyInt())).thenReturn(builder);
+        when(builder.build()).thenReturn(zk);
+
+        BookieSocketAddress bookieId = mock(BookieSocketAddress.class);
+
+        PowerMockito.mockStatic(AuditorElector.class);
+        PowerMockito.when(AuditorElector.getCurrentAuditor(eq(conf), eq(zk)))
+                    .thenReturn(bookieId);
+
+        PowerMockito.mockStatic(CommandHelpers.class);
+        
PowerMockito.when(CommandHelpers.getBookieSocketAddrStringRepresentation(eq(bookieId))).thenReturn("");
+    }
+
+    @Test
+    public void testCommand() {
+        WhoIsAuditorCommand cmd = new WhoIsAuditorCommand();
+        Assert.assertTrue(cmd.apply(bkFlags, new String[] { "" }));
+    }
+}

Reply via email to