Пример #1
0
func InitZK() (*zk.Conn, error) {
	config := GetConfig()
	if config == nil {
		log.Fatal("get config error")
	}
	conn, err := myzk.Connect(config.Zk.ZookeeperAddr, config.Zk.ZookeeperTimeout)
	if err != nil {
		log.Error("myzk.Connect() error(%v)", err)
		return nil, err
	}

	fpath := path.Join(ZOOKEEPER_CONN_PATH, config.Zk.ZookeeperNode)

	nodeInfo := ConnectorConf{
		Hostname:  config.Zk.ZookeeperNode,
		ServeAddr: config.ServeAddr,
		RpcAddr:   config.RpcAddr,
	}
	data, err := json.Marshal(nodeInfo)

	if err != nil {
		log.Error("json.Marshal() error(%v)", err)
		return conn, err
	}
	log.Debug("myzk node:\"%s\" registe data: \"%s\"", fpath, string(data))
	err = myzk.DeleteNode(conn, fpath)
	if err != nil {
		log.Error("myzk delete node error:%v", err)
	}
	if err = myzk.RegisterTemp(conn, fpath, data); err != nil {
		log.Error("myzk.RegisterTemp() error(%v)", err)
		return conn, err
	}
	return conn, nil
}
Пример #2
0
func InitZK() (*zk.Conn, error) {
	config := GetConfig()
	if config == nil {
		log.Fatal("get config error")
	}
	conn, err := myzk.Connect(config.Zk.ZookeeperAddr, config.Zk.ZookeeperTimeout)
	if err != nil {
		log.Error("myzk.Connect() error(%v)", err)
		return nil, err
	}

	fpath := path.Join(ZOOKEEPER_GATE_PATH, config.Zk.ZookeeperNode)

	err := myzk.DeleteNode(conn, fpath)
	if err != nil {
		log.Error("myzk delete node error:%v", err)
	}

	data := []byte{}
	log.Debug("myzk node:\"%s\" registe data: \"%s\"", fpath, string(data))
	if err = myzk.RegisterTemp(conn, fpath, data); err != nil {
		log.Error("myzk.RegisterTemp() error(%v)", err)
		return conn, err
	}
	go WatchConnChange(conn)

	return conn, nil
}
Пример #3
0
func loadConfig(fail bool) error {
	temp := NewDefaultConfig()
	if Conf == nil {
		configLock.Lock()
		Conf = temp
		configLock.Unlock()
	}
	data, err := ioutil.ReadFile(confFile)
	if err != nil {
		log.Error("open config: ", err)
		if fail {
			os.Exit(1)
		}
		return err
	}
	if err = yaml.Unmarshal(data, temp); err != nil {
		log.Error("yaml unmarshal error", err)
		if fail {
			os.Exit(1)
		}
		return err
	}
	log.Info("load config ok", *temp)
	configLock.Lock()
	Conf = temp
	configLock.Unlock()
	return nil
}
Пример #4
0
func GetRouters(conn *zk.Conn) []string {
	nodes, _, _, err := conn.ChildrenW(ZOOKEEPER_ROUTER_PATH)
	if err != nil {
		log.Error("get router from zk error:%v", err)
		return []string{}
	}
	return nodes
}
Пример #5
0
// RegisterTmp create a ephemeral node, and watch it, if node droped then send a SIGQUIT to self.
func RegisterTemp(conn *zk.Conn, fpath string, data []byte) error {
	tpath, err := conn.Create(path.Join(fpath), data, zk.FlagEphemeral, zk.WorldACL(zk.PermAll))
	if err != nil {
		log.Error("conn.Create(\"%s\", \"%s\", zk.FlagEphemeral) error(%v)", fpath, string(data), err)
		return err
	}
	log.Debug("create a zookeeper node:%s", tpath)
	return nil
}
Пример #6
0
func GetRoomConnector(conn *zk.Conn, roomid string) []string {
	fpath := path.Join(ZOOKEEPER_ROOM_PATH, roomid)
	nodes, _, _, err := conn.ChildrenW(fpath)
	if err != nil {
		log.Error("get roomid:%s from zk error:%v", roomid, err)
		return []string{}
	}
	return nodes
}
Пример #7
0
// Connect connect to zookeeper, and start a goroutine log the event.
func Connect(addr []string, timeout string) (*zk.Conn, error) {
	timeoutDuration, err := time.ParseDuration(timeout)
	if err != nil {
		log.Error("zk timeout error:%v", err)
		return nil, err
	}
	conn, session, err := zk.Connect(addr, timeoutDuration)
	if err != nil {
		log.Error("zk.Connect(\"%v\", %d) error(%v)", addr, timeoutDuration, err)
		return nil, err
	}
	go func() {
		for {
			event := <-session
			log.Debug("zookeeper get a event: %s", event.State.String())
		}
	}()
	return conn, nil
}
Пример #8
0
func ZkDeleteRoomConn(conn *zk.Conn, roomid string) error {
	config := GetConfig()
	fpath := path.Join(ZOOKEEPER_ROOM_PATH, roomid, config.RpcAddr)
	log.Debug("myzk delete znode:%s", fpath)
	err := myzk.DeleteNode(conn, fpath)
	if err != nil {
		log.Error("myzk delete node error:%v", err)
	}
	return err
}
Пример #9
0
func ZkAddRoomConn(conn *zk.Conn, roomid string) error {
	config := GetConfig()
	fpath := path.Join(ZOOKEEPER_ROOM_PATH, roomid, config.RpcAddr)
	data := []byte(config.RpcAddr)
	log.Debug("myzk node:\"%s\" registe data: \"%s\"", fpath, string(data))
	err := myzk.RegisterTemp(conn, fpath, data)
	if err != nil {
		log.Error("myzk.RegisterTemp() error(%v)", err)
	}
	return err
}
Пример #10
0
// GetNodes get all child from zk path.
func GetNodes(conn *zk.Conn, path string) ([]string, error) {
	nodes, stat, err := conn.Children(path)
	if err != nil {
		if err == zk.ErrNoNode {
			return nil, ErrNodeNotExist
		}
		log.Error("zk.Children(\"%s\") error(%v)", path, err)
		return nil, err
	}
	if stat == nil {
		return nil, ErrNodeNotExist
	}
	if len(nodes) == 0 {
		return nil, ErrNoChild
	}
	return nodes, nil
}
Пример #11
0
// Create create zookeeper path, if path exists ignore error
func Create(conn *zk.Conn, fpath string) error {
	// create zk root path
	tpath := ""
	for _, str := range strings.Split(fpath, "/")[1:] {
		tpath = path.Join(tpath, "/", str)
		log.Debug("create zookeeper path: \"%s\"", tpath)
		_, err := conn.Create(tpath, []byte(""), 0, zk.WorldACL(zk.PermAll))
		if err != nil {
			if err == zk.ErrNodeExists {
				log.Warning("zk.create(\"%s\") exists", tpath)
			} else {
				log.Error("zk.create(\"%s\") error(%v)", tpath, err)
				return err
			}
		}
	}

	return nil
}
Пример #12
0
func WatchRoomConnChange(conn *zk.Conn, roomid string, cancel <-chan int) {
	fpath := path.Join(ZOOKEEPER_ROOM_PATH, roomid)
	for {
		log.Info("zk path:%s set a watch", fpath)
		_, _, watch, err := conn.ChildrenW(fpath)
		if err != nil {
			log.Info("path:%s get error ,try later", fpath)
			time.Sleep(10 * time.Second)
			continue
		}
		select {
		case event := <-watch:
			log.Info("zk path:%s receive a event %v", fpath, event)
			err = GetRoomConnectorFromZk(roomid)
			if err != nil {
				log.Error("get room:%s from zk error", roomid)
			}
		case <-cancel:
			log.Info("cancel room:%s watch", roomid)
			break
		}
	}
}
Пример #13
0
// killSelf send a SIGQUIT to self.
func killSelf() {
	if err := syscall.Kill(os.Getpid(), syscall.SIGQUIT); err != nil {
		log.Error("syscall.Kill(%d, SIGQUIT) error(%v)", os.Getpid(), err)
	}
}