Sent before finishing the previous email. Only to complement, the
findLeader() could have been as below, but this change is only a nitty
detail and totally irrelevant to the questions you are asking. :)
/**
* Returns the address of the node we think is the leader.
*/
protected QuorumServer findLeader() {
// Find the leader by id
long currentLeader = self.getCurrentVote().getId();
QuorumServer leaderServer = self.getView().get(currentLeader);
if (leaderServer == null) {
LOG.warn("Couldn't find the leader with id = {}", currentLeader);
}
return leaderServer;
}
Edward
On Wed, May 9, 2018 at 9:29 AM, Edward Ribeiro <[email protected]>
wrote:
> Hi Enrico,
>
> Well, I am not an expert on QuorumPeer either (not an expert on anything,
> really), but maybe it's the variable and method below?
>
> ----------------- QuorumPeer ------------------
>
> /**
> * This is who I think the leader currently is.
> */
> volatile private Vote currentVote;
>
> public synchronized Vote getCurrentVote(){
> return currentVote;
> }
>
> ---------------------------------------
>
>
> Then it's a matter of calling quorumPeer.getCurrentVote().getId() and
> quorumPeer.getServerState()?
>
> Btw, the Learner class has this handy method below (self is a QuorumPeer):
>
> ---------------- Learner --------------------
>
> /**
> * Returns the address of the node we think is the leader.
> */
> protected QuorumServer findLeader() {
> QuorumServer leaderServer = null;
> // Find the leader by id
> Vote current = self.getCurrentVote();
> for (QuorumServer s : self.getView().values()) {
> if (s.id == current.getId()) {
> leaderServer = s;
> break;
> }
> }
> if (leaderServer == null) {
> LOG.warn("Couldn't find the leader with id = "
> + current.getId());
> }
> return leaderServer;
> }
>
> ---------------------------------------
>
> By the way, as a side note, the map traversal could be changed by:
>
> ----------------------------
>
> if (self.getView().contains(current.getId()) {
>
> }
>
> ---------------------------
>
>
>
> You can see above this method the quorumPeer.getView() returns a Map<sid,
> QuorumServer> as below:
>
> -------------QuorumPeer ---------
>
> /**
> * A 'view' is a node's current opinion of the membership of the entire
> * ensemble.
> */
> public Map<Long,QuorumPeer.QuorumServer> getView() {
> return Collections.unmodifiableMap(getQuorumVerifier().
> getAllMembers());
> }
>
> -----------------------------
>
>
> And then it retrieves the QuorumServer that has many more information
> about the node besides the sid (InetSocketAddress, hostname, etc). :)
>
>
> Cheers,
> Edward
>
> On Wed, May 9, 2018 at 8:50 AM, Enrico Olivelli <[email protected]>
> wrote:
>
>> So I am trying to create a patch in order to expose on JMX the id of the
>> current "leader" (on the JVM of a follower)
>>
>> I am trying to find in ZK which is the variable which holds the ID of the
>> current leader.
>> I am new to the internal of QuorumPeer
>>
>> Can someone give me some hint ?
>>
>> Enrico
>>
>> Il giorno mar 8 mag 2018 alle ore 10:08 Ansel Zandegran <
>> [email protected]> ha scritto:
>>
>> > Hi,
>> > That is possible with 4 letter commands. We are using it now. In 3.5.x
>> it
>> > is going to be removed in favour of admin server (embedded web server).
>> > We are running in an environment where it’s not possible to run JMX or
>> > embedded web servers.
>> >
>> > So I am wondering if there is another way? It would be nice to have this
>> > info as a znode.
>> >
>> > Best regards,
>> > Ansel
>> >
>> > > On 8 May 2018, at 09:55, Flavio Junqueira <[email protected]> wrote:
>> > >
>> > > Hi Enrico,
>> > >
>> > > You can determine the state of a server it via 4-letter commands.
>> Would
>> > that work for you?
>> > >
>> > > -Flavio
>> > >
>> > >> On 8 May 2018, at 09:09, Enrico Olivelli <[email protected]>
>> wrote:
>> > >>
>> > >> Hi,
>> > >> is there any way to see in JMX which is the leader of a ZooKeeper
>> > cluster?
>> > >>
>> > >> My problem is: given access to any of the nodes of the cluster I
>> want to
>> > >> know from JMX which is the current leader.
>> > >> It seems to me that this information is not available, you can know
>> > only if
>> > >> the local node is Leader or Follower.
>> > >>
>> > >> Cheers
>> > >> Enrico
>> > >
>> >
>> >
>>
>
>