java - Is there any master related to zookeeper? Use ZKClient to create a node, but it is not displayed on the server?
仅有的幸福
仅有的幸福 2017-05-17 09:58:50
0
1
760

I created the node according to the code on the zk authoritative guide:

package com.zkstudy;

import java.io.IOException;
import java.util.Random;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

public class Master implements Watcher {
    private ZooKeeper zk = null;
    private String host;
    private String serverId = null;
    private boolean isLeader = false;

    public Master(String host) {
        this.host = host;
        Random radom = new Random();
        serverId = Integer.toHexString(radom.nextInt());
    }

    public void start() {

        try {
            this.zk = new ZooKeeper(host, 15000, this);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void createMaster() {

        while (true) {

            try {
                zk.create("/master", serverId.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
                isLeader = true;
                break;
            } catch (KeeperException e) {
                e.printStackTrace();
                isLeader = false;
                break;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (checkMaster()) {
                break;
            }

        }

    }

    public boolean checkMaster() {

        while (true) {
            Stat stat = new Stat();
            try {
                byte[] data = zk.getData("/master", false, stat);
                isLeader = new String(data).equals(serverId);
                return true;
            } catch (KeeperException e) {
                e.printStackTrace();
                return false;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void process(WatchedEvent event) {
        System.out.println("watcher thread:---------->" + Thread.currentThread().getId());
        System.out.println("event:---------->" + event);
    }

    public static void main(String[] args) throws InterruptedException {
        Master master = new Master("123.206.28.37:2181");
        master.start();
        master.createMaster();
        if (master.isLeader) {
            System.out.println("i am  master");
        }
        System.out.println("main thread:---------->" + Thread.currentThread().getId());
        Thread.sleep(5000);
    }
}

Then I ran it twice. The following problem occurred:

watcher thread:---------->10
event:---------->WatchedEvent state:SyncConnected type:None path:null
org.apache.zookeeper.KeeperException$NodeExistsException: KeeperErrorCode = NodeExists for /master
main thread:---------->1
    at org.apache.zookeeper.KeeperException.create(KeeperException.java:119)
    at org.apache.zookeeper.KeeperException.create(KeeperException.java:51)
    at org.apache.zookeeper.ZooKeeper.create(ZooKeeper.java:783)
    at com.zkstudy.Master.createMaster(Master.java:40)
    at com.zkstudy.Master.main(Master.java:83)

Prompt that the master node already exists.
But I didn’t find the master node when I checked it on the command line:

[zk: localhost:2181(CONNECTED) 10] ls
[zk: localhost:2181(CONNECTED) 11] ls  /
[zk, zookeeper]
[zk: localhost:2181(CONNECTED) 12] 

Why is this? zk uses a single node, not a pseudo cluster.

仅有的幸福
仅有的幸福

reply all(1)
Peter_Zhu

Ask and answer your own questions. Because what I created is a temporary node, the temporary node will be automatically deleted after the painting is disconnected. It is estimated that this ls / command should be the listed persistent node.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template