|
19 | 19 | package org.apache.zookeeper.server.admin; |
20 | 20 |
|
21 | 21 | import java.net.InetSocketAddress; |
22 | | -import java.util.*; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.HashSet; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Properties; |
| 29 | +import java.util.Set; |
| 30 | +import java.util.SortedMap; |
| 31 | +import java.util.TreeMap; |
23 | 32 | import java.util.stream.Collectors; |
24 | 33 |
|
25 | | -import com.fasterxml.jackson.annotation.JsonAnyGetter; |
| 34 | +import com.fasterxml.jackson.annotation.JsonProperty; |
26 | 35 | import org.apache.zookeeper.Environment; |
27 | 36 | import org.apache.zookeeper.Environment.Entry; |
28 | 37 | import org.apache.zookeeper.Version; |
|
36 | 45 | import org.apache.zookeeper.server.quorum.FollowerZooKeeperServer; |
37 | 46 | import org.apache.zookeeper.server.quorum.Leader; |
38 | 47 | import org.apache.zookeeper.server.quorum.LeaderZooKeeperServer; |
| 48 | +import org.apache.zookeeper.server.quorum.MultipleAddresses; |
39 | 49 | import org.apache.zookeeper.server.quorum.QuorumPeer; |
| 50 | +import org.apache.zookeeper.server.quorum.QuorumPeer.LearnerType; |
40 | 51 | import org.apache.zookeeper.server.quorum.QuorumZooKeeperServer; |
41 | 52 | import org.apache.zookeeper.server.quorum.flexible.QuorumVerifier; |
42 | 53 | import org.apache.zookeeper.server.quorum.ReadOnlyZooKeeperServer; |
@@ -620,57 +631,53 @@ public CommandResponse run(ZooKeeperServer zkServer, Map<String, String> kwargs) |
620 | 631 | CommandResponse response = initializeResponse(); |
621 | 632 | if (zkServer instanceof QuorumZooKeeperServer) { |
622 | 633 | QuorumPeer peer = ((QuorumZooKeeperServer) zkServer).self; |
623 | | - VotingView votingView = new VotingView(peer.getVotingView()); |
| 634 | + Map<Long, QuorumServerView> votingView = peer.getVotingView().entrySet().stream() |
| 635 | + .collect(Collectors.toMap(Map.Entry::getKey, e -> new QuorumServerView(e.getValue()))); |
624 | 636 | response.put("current_config", votingView); |
625 | 637 | } else { |
626 | 638 | response.put("current_config", Collections.emptyMap()); |
627 | 639 | } |
628 | 640 | return response; |
629 | 641 | } |
630 | 642 |
|
| 643 | + private static class QuorumServerView { |
631 | 644 |
|
632 | | - private static class VotingView { |
633 | | - private final Map<Long, String> view; |
| 645 | + @JsonProperty |
| 646 | + private List<String> serverAddresses; |
634 | 647 |
|
635 | | - VotingView(Map<Long,QuorumPeer.QuorumServer> view) { |
636 | | - this.view = view.entrySet().stream() |
637 | | - .filter(e -> e.getValue().addr != null) |
638 | | - .collect(Collectors.toMap(Map.Entry::getKey, |
639 | | - e -> String.format("%s:%s%s", |
640 | | - getMultiAddressString(e.getValue()), |
641 | | - e.getValue().type.equals(QuorumPeer.LearnerType.PARTICIPANT) ? "participant" : "observer", |
642 | | - e.getValue().clientAddr ==null || e.getValue().isClientAddrFromStatic ? "" : |
643 | | - String.format(";%s:%d", |
644 | | - QuorumPeer.QuorumServer.delimitedHostString(e.getValue().clientAddr), |
645 | | - e.getValue().clientAddr.getPort())), |
646 | | - (v1, v2) -> v1, // cannot get duplicates as this straight draws from the other map |
647 | | - TreeMap::new)); |
648 | | - } |
649 | | - |
650 | | - private String getMultiAddressString(QuorumPeer.QuorumServer qs) { |
651 | | - return qs.addr.getAllAddresses().stream() |
652 | | - .map(address -> getSingleAddressString(qs, address)) |
653 | | - .collect(Collectors.joining(",")); |
654 | | - } |
| 648 | + @JsonProperty |
| 649 | + private List<String> electionAddresses; |
655 | 650 |
|
656 | | - private String getSingleAddressString(QuorumPeer.QuorumServer qs, InetSocketAddress address) { |
657 | | - final String addressHostString = address.getHostString(); |
658 | | - final String delimitedHostString = QuorumPeer.QuorumServer.delimitedHostString(address); |
| 651 | + @JsonProperty |
| 652 | + private String clientAddress; |
659 | 653 |
|
660 | | - Optional<InetSocketAddress> matchingElectionAddress = qs.electionAddr.getAllAddresses().stream() |
661 | | - .filter(electionAddress -> electionAddress.getHostString().equals(addressHostString)) |
662 | | - .findFirst(); |
663 | | - final String electionPort = matchingElectionAddress.map(e-> ":" + e.getPort()).orElse(""); |
| 654 | + @JsonProperty |
| 655 | + private String learnerType; |
664 | 656 |
|
665 | | - return String.format("%s:%d%s", delimitedHostString, address.getPort(), electionPort); |
| 657 | + public QuorumServerView(QuorumPeer.QuorumServer quorumServer) { |
| 658 | + this.serverAddresses = getMultiAddressString(quorumServer.addr); |
| 659 | + this.electionAddresses = getMultiAddressString(quorumServer.electionAddr); |
| 660 | + this.learnerType = quorumServer.type.equals(LearnerType.PARTICIPANT) ? "participant" : "observer"; |
| 661 | + this.clientAddress = getAddressString(quorumServer.clientAddr); |
666 | 662 | } |
667 | 663 |
|
668 | | - @JsonAnyGetter |
669 | | - public Map<Long, String> getView() { |
670 | | - return view; |
| 664 | + private static List<String> getMultiAddressString(MultipleAddresses multipleAddresses) { |
| 665 | + if(multipleAddresses == null) { |
| 666 | + return Collections.emptyList(); |
| 667 | + } |
| 668 | + |
| 669 | + return multipleAddresses.getAllAddresses().stream() |
| 670 | + .map(QuorumServerView::getAddressString) |
| 671 | + .collect(Collectors.toList()); |
671 | 672 | } |
672 | | - } |
673 | 673 |
|
| 674 | + private static String getAddressString(InetSocketAddress address) { |
| 675 | + if(address == null) { |
| 676 | + return ""; |
| 677 | + } |
| 678 | + return String.format("%s:%d", QuorumPeer.QuorumServer.delimitedHostString(address), address.getPort()); |
| 679 | + } |
| 680 | + } |
674 | 681 |
|
675 | 682 | } |
676 | 683 |
|
|
0 commit comments