You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@zookeeper.apache.org by eo...@apache.org on 2019/07/25 21:43:33 UTC

[zookeeper] branch master updated: ZOOKEEPER-3386: Add admin command to display voting view

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

eolivelli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/zookeeper.git


The following commit(s) were added to refs/heads/master by this push:
     new 6281a86  ZOOKEEPER-3386: Add admin command to display voting view
6281a86 is described below

commit 6281a8693c287fadca72321178023ee78cf9c818
Author: Brian Nixon <ni...@fb.com>
AuthorDate: Thu Jul 25 23:43:26 2019 +0200

    ZOOKEEPER-3386: Add admin command to display voting view
    
    Author: Brian Nixon <ni...@fb.com>
    
    Reviewers: maoling <ma...@sina.com>, Enrico Olivelli <eo...@apache.org>, Andor Molnar <an...@apache.org>
    
    Closes #940 from enixon/cmd-vote and squashes the following commits:
    
    466394264 [Brian Nixon] fix serialization logic of VotingView
    98413e38c [Brian Nixon] add getter to VotingView for serialization
    b22ccc61a [Brian Nixon] introduce a static form for voting view
    6a19ed178 [Brian Nixon] ZOOKEEPER-3386: Add admin command to display voting view
---
 .../apache/zookeeper/server/admin/Commands.java    | 121 +++++++++++++++------
 .../apache/zookeeper/server/quorum/QuorumPeer.java |   2 +-
 2 files changed, 88 insertions(+), 35 deletions(-)

diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/Commands.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/Commands.java
index 097d9b7..dede57e 100644
--- a/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/Commands.java
+++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/Commands.java
@@ -28,7 +28,9 @@ import java.util.Properties;
 import java.util.Set;
 import java.util.SortedMap;
 import java.util.TreeMap;
+import java.util.stream.Collectors;
 
+import com.fasterxml.jackson.annotation.JsonAnyGetter;
 import org.apache.zookeeper.Environment;
 import org.apache.zookeeper.Environment.Entry;
 import org.apache.zookeeper.Version;
@@ -125,6 +127,7 @@ public class Commands {
         registerCommand(new DumpCommand());
         registerCommand(new EnvCommand());
         registerCommand(new GetTraceMaskCommand());
+        registerCommand(new InitialConfigurationCommand());
         registerCommand(new IsroCommand());
         registerCommand(new LastSnapshotCommand());
         registerCommand(new LeaderCommand());
@@ -136,12 +139,12 @@ public class Commands {
         registerCommand(new StatCommand());
         registerCommand(new StatResetCommand());
         registerCommand(new SyncedObserverConsCommand());
+        registerCommand(new SystemPropertiesCommand());
+        registerCommand(new VotingViewCommand());
         registerCommand(new WatchCommand());
         registerCommand(new WatchesByPathCommand());
         registerCommand(new WatchSummaryCommand());
         registerCommand(new ZabStateCommand());
-        registerCommand(new SystemPropertiesCommand());
-        registerCommand(new InitialConfigurationCommand());
     }
 
     /**
@@ -282,6 +285,19 @@ public class Commands {
         }
     }
 
+    public static class InitialConfigurationCommand extends CommandBase {
+        public InitialConfigurationCommand() {
+            super(Arrays.asList("initial_configuration", "icfg"));
+        }
+
+        @Override
+        public CommandResponse run(ZooKeeperServer zkServer, Map<String, String> kwargs) {
+            CommandResponse response = initializeResponse();
+            response.put("initial_configuration", zkServer.getInitialConfig());
+            return response;
+        }
+    }
+
     /**
      * Is this server in read-only mode. Returned map contains:
      *   - "is_read_only": Boolean
@@ -563,6 +579,75 @@ public class Commands {
     }
 
     /**
+     * All defined system properties.
+     */
+    public static class SystemPropertiesCommand extends CommandBase {
+        public SystemPropertiesCommand() {
+            super(Arrays.asList("system_properties", "sysp"), false);
+        }
+
+        @Override
+        public CommandResponse run(ZooKeeperServer zkServer, Map<String, String> kwargs) {
+            CommandResponse response = initializeResponse();
+            Properties systemProperties = System.getProperties();
+            SortedMap<String, String> sortedSystemProperties = new TreeMap<>();
+            systemProperties.forEach((k, v) -> sortedSystemProperties.put(k.toString(), v.toString()));
+            response.putAll(sortedSystemProperties);
+            return response;
+        }
+    }
+
+    /**
+     * Returns the current ensemble configuration information.
+     * It provides list of current voting members in the ensemble.
+     */
+    public static class VotingViewCommand extends CommandBase {
+        public VotingViewCommand() {
+            super(Arrays.asList("voting_view"));
+        }
+
+        @Override
+        public CommandResponse run(ZooKeeperServer zkServer, Map<String, String> kwargs) {
+            CommandResponse response = initializeResponse();
+            if (zkServer instanceof QuorumZooKeeperServer) {
+                QuorumPeer peer = ((QuorumZooKeeperServer) zkServer).self;
+                VotingView votingView = new VotingView(peer.getVotingView());
+                response.put("current_config", votingView);
+            } else {
+                response.put("current_config", Collections.emptyMap());
+            }
+            return response;
+        }
+
+
+        private static class VotingView {
+            private final Map<Long, String> view;
+
+            VotingView(Map<Long,QuorumPeer.QuorumServer> view) {
+                this.view = view.entrySet().stream()
+                        .filter(e -> e.getValue().addr != null)
+                        .collect(Collectors.toMap(Map.Entry::getKey,
+                                e -> String.format("%s:%d%s:%s%s",
+                                        QuorumPeer.QuorumServer.delimitedHostString(e.getValue().addr),
+                                        e.getValue().addr.getPort(),
+                                        e.getValue().electionAddr == null ? "" : ":" + e.getValue().electionAddr.getPort(),
+                                        e.getValue().type.equals(QuorumPeer.LearnerType.PARTICIPANT) ? "participant" : "observer",
+                                        e.getValue().clientAddr ==null || e.getValue().isClientAddrFromStatic ? "" :
+                                                String.format(";%s:%d",
+                                                        QuorumPeer.QuorumServer.delimitedHostString(e.getValue().clientAddr),
+                                                        e.getValue().clientAddr.getPort())),
+                                (v1, v2) -> v1, // cannot get duplicates as this straight draws from the other map
+                                TreeMap::new));
+            }
+
+            @JsonAnyGetter
+            public Map<Long, String> getView() {
+                return view;
+            }
+        }
+    }
+
+    /**
      * Watch information aggregated by session. Returned Map contains:
      *   - "session_id_to_watched_paths": Map&lt;Long, Set&lt;String&gt;&gt; session ID -&gt; watched paths
      * @see DataTree#getWatches()
@@ -651,37 +736,5 @@ public class Commands {
         }
     }
 
-    /**
-     * All defined system properties.
-     */
-    public static class SystemPropertiesCommand extends CommandBase {
-        public SystemPropertiesCommand() {
-            super(Arrays.asList("system_properties", "sysp"), false);
-        }
-
-        @Override
-        public CommandResponse run(ZooKeeperServer zkServer, Map<String, String> kwargs) {
-            CommandResponse response = initializeResponse();
-            Properties systemProperties = System.getProperties();
-            SortedMap<String, String> sortedSystemProperties = new TreeMap<>();
-            systemProperties.forEach((k, v) -> sortedSystemProperties.put(k.toString(), v.toString()));
-            response.putAll(sortedSystemProperties);
-            return response;
-        }
-    }
-
-    public static class InitialConfigurationCommand extends CommandBase {
-        public InitialConfigurationCommand() {
-            super(Arrays.asList("initial_configuration", "icfg"));
-        }
-
-        @Override
-        public CommandResponse run(ZooKeeperServer zkServer, Map<String, String> kwargs) {
-            CommandResponse response = initializeResponse();
-            response.put("initial_configuration", zkServer.getInitialConfig());
-            return response;
-        }
-    }
-
     private Commands() {}
 }
diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeer.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeer.java
index 46f144b..521f6d4 100644
--- a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeer.java
+++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeer.java
@@ -319,7 +319,7 @@ public class QuorumPeer extends ZooKeeperThread implements QuorumStats.Provider
             this.myAddrs = excludedSpecialAddresses(this.myAddrs);
         }
 
-        private static String delimitedHostString(InetSocketAddress addr)
+        public static String delimitedHostString(InetSocketAddress addr)
         {
             String host = addr.getHostString();
             if (host.contains(":")) {