• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang zk.Conn类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/samuel/go-zookeeper/zk.Conn的典型用法代码示例。如果您正苦于以下问题:Golang Conn类的具体用法?Golang Conn怎么用?Golang Conn使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Conn类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: createZPath

func (zs *ZKStorage) createZPath(conn *zk.Conn, zpath string) error {
	_, err := conn.Create(zpath, nil, 0, zk.WorldACL(zk.PermAll))
	if err != nil {
		if zk.ErrNodeExists == err {
			return nil
		} else {
			parent, _ := path.Split(zpath)
			if len(parent) == 0 {
				return errors.New("Specified blank path")
			}
			err = zs.createZPath(conn, parent[:len(parent)-1])
			if err != nil {
				return err
			}

			_, err = conn.Create(zpath, nil, 0, zk.WorldACL(zk.PermAll))
			if err == zk.ErrNodeExists {
				err = nil
			}
		}
	}

	if zk.ErrNodeExists == err {
		return nil
	} else {
		return err
	}
}
开发者ID:elodina,项目名称:stack-deploy,代码行数:28,代码来源:storage.go


示例2: CreateTempW

// 创建一个临时节点,并监听自己
func CreateTempW(conn *zk.Conn, fpath, data string, watchFunc func(bool, zk.Event)) error {
	tpath, err := conn.Create(path.Join(fpath)+"/", []byte(data), zk.FlagEphemeral|zk.FlagSequence, zk.WorldACL(zk.PermAll))
	if err != nil {
		glog.Errorf("conn.Create(\"%s\", \"%s\", zk.FlagEphemeral|zk.FlagSequence) error(%v)", fpath, data, err)
		return err
	}
	glog.V(1).Infof("create a zookeeper node:%s", tpath)
	// watch self
	if watchFunc != nil {
		go func() {
			for {
				glog.Infof("zk path: \"%s\" set a watch", tpath)
				exist, _, watch, err := conn.ExistsW(tpath)
				if err != nil {
					glog.Errorf("zk.ExistsW(%s) error(%v)", tpath, err)
					return
				}
				event := <-watch
				watchFunc(exist, event)
				glog.Infof("zk path: \"%s\" receive a event %v", tpath, event)
			}
		}()
	}
	return nil
}
开发者ID:jackwanger,项目名称:cloud-base,代码行数:26,代码来源:zk.go


示例3: processChildrenAndWait

func (a *HostAgent) processChildrenAndWait(conn *zk.Conn) bool {
	processing := make(map[string]chan int)
	ssDone := make(chan stateResult, 25)

	hostPath := zzk.HostPath(a.hostId)

	for {

		children, _, zkEvent, err := conn.ChildrenW(hostPath)
		if err != nil {
			glog.V(0).Infoln("Unable to read children, retrying.")
			time.Sleep(3 * time.Second)
			return true
		}
		a.startMissingChildren(conn, children, processing, ssDone)

		select {

		case errc := <-a.closing:
			glog.V(1).Info("Agent received interrupt")
			err = waitForSsNodes(processing, ssDone)
			errc <- err
			return false

		case ssResult := <-ssDone:
			glog.V(1).Infof("Goroutine finished %s", ssResult.id)
			delete(processing, ssResult.id)

		case evt := <-zkEvent:
			glog.V(1).Info("Agent event: ", evt)
		}
	}
}
开发者ID:jhuie-zenoss,项目名称:serviced,代码行数:33,代码来源:agent.go


示例4: NewExplorer

// NewExplorer creates a new Explorer instance with a name,
// application and balancer finders and zookeeper connection
// to persist versioning information
func NewExplorer(name string, af application.Finder, bf balancer.Finder, zc *zk.Conn, zp string) (*Explorer, error) {
	s := state.State{}

	ss, _, err := zc.Get(zp)
	if err != nil {
		if err != zk.ErrNoNode {
			return nil, err
		}

		s.Versions = map[string]state.Versions{}
	} else {
		err := json.Unmarshal(ss, &s)
		if err != nil {
			return nil, err
		}
	}

	return &Explorer{
		name:      name,
		af:        af,
		bf:        bf,
		zookeeper: zc,
		zp:        zp,
		state:     s,
		mutex:     sync.Mutex{},
	}, nil
}
开发者ID:abraithwaite,项目名称:zoidberg,代码行数:30,代码来源:explorer.go


示例5: NewExplorer

func NewExplorer(d Discoverer, zc *zk.Conn, zp string, location ExplorerLocation) (*Explorer, error) {
	state := State{}

	ss, _, err := zc.Get(zp)
	if err != nil {
		if err != zk.ErrNoNode {
			return nil, err
		}

		state.Versions = map[string]Versions{}
	} else {
		err := json.Unmarshal(ss, &state)
		if err != nil {
			return nil, err
		}
	}

	return &Explorer{
		discoverer: d,
		zookeeper:  zc,
		zp:         zp,
		state:      state,
		location:   location,
		mutex:      sync.Mutex{},
	}, nil
}
开发者ID:alberts,项目名称:zoidberg,代码行数:26,代码来源:explorer.go


示例6: All

func All(conn *zk.Conn, zkConf conf.Zookeeper) (map[string]Service, error) {

	err := ensurePathExists(conn, zkConf.Path)
	if err != nil {
		return nil, err
	}

	services := map[string]Service{}
	keys, _, err2 := conn.Children(zkConf.Path)

	if err2 != nil {
		return nil, err2
	}

	for _, childPath := range keys {
		bite, _, e := conn.Get(zkConf.Path + "/" + childPath)
		if e != nil {
			return nil, e
			break
		}
		appId, _ := unescapeSlashes(childPath)
		services[appId] = Service{Id: appId, Acl: string(bite)}
	}
	return services, nil
}
开发者ID:rthomas,项目名称:bamboo,代码行数:25,代码来源:service.go


示例7: RegisterTemp

// RegisterTmp create a ephemeral node, and watch it, if node droped then send a SIGQUIT to self.
func RegisterTemp(conn *zk.Conn, fpath, data string) error {
	tpath, err := conn.Create(path.Join(fpath)+"/", []byte(data), zk.FlagEphemeral|zk.FlagSequence, zk.WorldACL(zk.PermAll))
	if err != nil {
		glog.Errorf("conn.Create(\"%s\", \"%s\", zk.FlagEphemeral|zk.FlagSequence) error(%v)", fpath, data, err)
		return err
	}
	glog.V(1).Infof("create a zookeeper node:%s", tpath)
	// watch self
	go func() {
		for {
			glog.Infof("zk path: \"%s\" set a watch", tpath)
			exist, _, watch, err := conn.ExistsW(tpath)
			if err != nil {
				glog.Errorf("zk.ExistsW(\"%s\") error(%v)", tpath, err)
				glog.Warningf("zk path: \"%s\" set watch failed, kill itself", tpath)
				killSelf()
				return
			}
			if !exist {
				glog.Warningf("zk path: \"%s\" not exist, kill itself", tpath)
				killSelf()
				return
			}
			event := <-watch
			glog.Infof("zk path: \"%s\" receive a event %v", tpath, event)
		}
	}()
	return nil
}
开发者ID:nosix-me,项目名称:gopush-cluster,代码行数:30,代码来源:zk.go


示例8: registerNode

func registerNode(conn *zk.Conn, node, path string) (*NodeInfo, error) {
	path = path + "/" + node
	data, _, err := conn.Get(path)
	if err != nil {
		Log.Error("zk.Get(\"%s\") error(%v)", path, err)
		return nil, err
	}
	// fetch and parse comet info
	addr, err := parseNode(string(data))
	if err != nil {
		Log.Error("parseNode(\"%s\") error(%v)", string(data), err)
		return nil, err
	}
	info := &NodeInfo{Addr: addr}
	rpcAddr, ok := addr[ProtocolRPC]
	if !ok || len(rpcAddr) == 0 {
		Log.Crit("zk nodes: \"%s\" don't have rpc addr", path)
		return nil, ErrCometRPC
	}
	// init comet rpc
	// TODO support many rpc
	r, err := rpc.Dial("tcp", rpcAddr[0])
	if err != nil {
		Log.Crit("rpc.Dial(\"%s\") error(%v)", rpcAddr[0], err)
		return nil, ErrCometRPC
	}
	info.PubRPC = r
	Log.Info("zk path: \"%s\" register nodes: \"%s\"", path, node)
	return info, nil
}
开发者ID:Jnnnh,项目名称:gopush-cluster,代码行数:30,代码来源:zk.go


示例9: mirrorNode

func mirrorNode(conn *zk.Conn, node *NodeTree) error {
	var err error
	var children []string

	nodePath := appendPath(node.PathPrefix, node.Name)

	if children, _, err = conn.Children(nodePath); err != nil {
		return err
	}

	for _, child := range children {
		var data []byte

		childPath := appendPath(nodePath, child)

		if data, _, err = conn.Get(childPath); err != nil {
			return err
		}

		childNode := NewNodeTree(child, nodePath, data)

		node.AddChild(childNode)

		if err := mirrorNode(conn, childNode); err != nil {
			return err
		}
	}

	return nil
}
开发者ID:cmsd2,项目名称:zk,代码行数:30,代码来源:commands.go


示例10: GetRouters

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
}
开发者ID:hzhzh007,项目名称:RoomChat,代码行数:8,代码来源:zk.go


示例11: Create

/*
   Read ZK ACL:
   http://zookeeper.apache.org/doc/trunk/zookeeperProgrammers.html#sc_ACLPermissions
*/
func Create(conn *zk.Conn, zkConf conf.Zookeeper, appId string, domainValue string) (string, error) {
	path := concatPath(zkConf.Path, appId)
	resPath, err := conn.Create(path, []byte(domainValue), 0, defaultACL())
	if err != nil {
		return "", err
	}

	return resPath, nil
}
开发者ID:rthomas,项目名称:bamboo,代码行数:13,代码来源:service.go


示例12: GetRoomConnector

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
}
开发者ID:hzhzh007,项目名称:RoomChat,代码行数:9,代码来源:zk.go


示例13: syncConnections

func syncConnections(conf *bloorConfig, conn *zk.Conn) {
	// Sync up again
	_, err := conn.Sync(conf.rootZnode)
	if err != nil {
		log.Fatal(err)
	} else {
		log.Printf("Synced connection")
	}
}
开发者ID:ccollicutt,项目名称:bloor,代码行数:9,代码来源:bloor.go


示例14: RegisterTemp

// 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
}
开发者ID:hzhzh007,项目名称:RoomChat,代码行数:10,代码来源:zk.go


示例15: watchService

func watchService(cpDao dao.ControlPlane, conn *zk.Conn, shutdown <-chan int, done chan<- string, serviceId string) {
	defer func() {
		glog.V(3).Info("Exiting function watchService ", serviceId)
		done <- serviceId
	}()
	for {
		var service dao.Service
		_, zkEvent, err := zzk.LoadServiceW(conn, serviceId, &service)
		if err != nil {
			glog.Errorf("Unable to load service %s: %v", serviceId, err)
			return
		}
		_, _, childEvent, err := conn.ChildrenW(zzk.ServicePath(serviceId))

		glog.V(1).Info("Leader watching for changes to service ", service.Name)

		// check current state
		var serviceStates []*dao.ServiceState
		err = zzk.GetServiceStates(conn, &serviceStates, serviceId)
		if err != nil {
			glog.Error("Unable to retrieve running service states: ", err)
			return
		}

		// Is the service supposed to be running at all?
		switch {
		case service.DesiredState == dao.SVC_STOP:
			shutdownServiceInstances(conn, serviceStates, len(serviceStates))
		case service.DesiredState == dao.SVC_RUN:
			updateServiceInstances(cpDao, conn, &service, serviceStates)
		default:
			glog.Warningf("Unexpected desired state %d for service %s", service.DesiredState, service.Name)
		}

		select {
		case evt := <-zkEvent:
			if evt.Type == zk.EventNodeDeleted {
				glog.V(0).Info("Shutting down due to node delete ", serviceId)
				shutdownServiceInstances(conn, serviceStates, len(serviceStates))
				return
			}
			glog.V(1).Infof("Service %s received event: %v", service.Name, evt)
			continue

		case evt := <-childEvent:
			glog.V(1).Infof("Service %s received child event: %v", service.Name, evt)
			continue

		case <-shutdown:
			glog.V(1).Info("Leader stopping watch on ", service.Name)
			return

		}
	}

}
开发者ID:jhuie-zenoss,项目名称:serviced,代码行数:56,代码来源:scheduler_random.go


示例16: watchMessageRoot

// watchMessageRoot watch the message root path.
func watchMessageRoot(conn *zk.Conn, fpath string, ch chan *MessageNodeEvent) error {
	for {
		nodes, watch, err := myzk.GetNodesW(conn, fpath)
		if err == myzk.ErrNodeNotExist {
			glog.Warningf("zk don't have node \"%s\", retry in %d second", fpath, waitNodeDelay)
			time.Sleep(waitNodeDelaySecond)
			continue
		} else if err == myzk.ErrNoChild {
			glog.Warningf("zk don't have any children in \"%s\", retry in %d second", fpath, waitNodeDelay)
			// all child died, kick all the nodes
			for _, client := range MessageRPC.Clients {
				glog.V(1).Infof("node: \"%s\" send del node event", client.Addr)
				ch <- &MessageNodeEvent{Event: eventNodeDel, Key: &RPCClient{Addr: client.Addr, Weight: client.Weight}}
			}
			time.Sleep(waitNodeDelaySecond)
			continue
		} else if err != nil {
			glog.Errorf("getNodes error(%v), retry in %d second", err, waitNodeDelay)
			time.Sleep(waitNodeDelaySecond)
			continue
		}
		nodesMap := map[string]bool{}
		// handle new add nodes
		for _, node := range nodes {
			data, _, err := conn.Get(path.Join(fpath, node))
			if err != nil {
				glog.Errorf("zk.Get(\"%s\") error(%v)", path.Join(fpath, node), err)
				continue
			}
			// may contains many addrs split by ,
			//addrs := strings.Split(string(data), ",")
			addrInfos, err := parseMessageAddr(string(data))
			if err != nil {
				glog.Errorf("parseMessageAddr(\"%s\") error(%v)", string(data), err)
				continue
			}
			for _, addInfo := range addrInfos {
				// if not exists in old map then trigger a add event
				if _, ok := MessageRPC.Clients[addInfo.Addr]; !ok {
					ch <- &MessageNodeEvent{Event: eventNodeAdd, Key: addInfo}
				}
				nodesMap[addInfo.Addr] = true
			}
		}
		// handle delete nodes
		for _, client := range MessageRPC.Clients {
			if _, ok := nodesMap[client.Addr]; !ok {
				ch <- &MessageNodeEvent{Event: eventNodeDel, Key: client}
			}
		}
		// blocking wait node changed
		event := <-watch
		glog.Infof("zk path: \"%s\" receive a event %v", fpath, event)
	}
}
开发者ID:jixieshi999,项目名称:gopush-cluster,代码行数:56,代码来源:message.go


示例17: watchMessageRoot

// watchMessageRoot watch the message root path.
func watchMessageRoot(conn *zk.Conn, fpath string, ch chan *MessageNodeEvent) error {
	for {
		nodes, watch, err := myzk.GetNodesW(conn, fpath)
		if err == myzk.ErrNodeNotExist {
			log.Warn("zk don't have node \"%s\", retry in %d second", fpath, waitNodeDelay)
			time.Sleep(waitNodeDelaySecond)
			continue
		} else if err == myzk.ErrNoChild {
			log.Warn("zk don't have any children in \"%s\", retry in %d second", fpath, waitNodeDelay)
			// all child died, kick all the nodes
			for _, client := range MessageRPC.Clients {
				log.Debug("node: \"%s\" send del node event", client.Addr)
				ch <- &MessageNodeEvent{Event: eventNodeDel, Key: &WeightRpc{Addr: client.Addr, Weight: client.Weight}}
			}
			time.Sleep(waitNodeDelaySecond)
			continue
		} else if err != nil {
			log.Error("getNodes error(%v), retry in %d second", err, waitNodeDelay)
			time.Sleep(waitNodeDelaySecond)
			continue
		}
		nodesMap := map[string]bool{}
		// handle new add nodes
		for _, node := range nodes {
			data, _, err := conn.Get(path.Join(fpath, node))
			if err != nil {
				log.Error("zk.Get(\"%s\") error(%v)", path.Join(fpath, node), err)
				continue
			}
			// parse message node info
			nodeInfo := &MessageNodeInfo{}
			if err := json.Unmarshal(data, nodeInfo); err != nil {
				log.Error("json.Unmarshal(\"%s\", nodeInfo) error(%v)", string(data), err)
				continue
			}
			for _, addr := range nodeInfo.Rpc {
				// if not exists in old map then trigger a add event
				if _, ok := MessageRPC.Clients[addr]; !ok {
					ch <- &MessageNodeEvent{Event: eventNodeAdd, Key: &WeightRpc{Addr: addr, Weight: nodeInfo.Weight}}
				}
				nodesMap[addr] = true
			}
		}
		// handle delete nodes
		for _, client := range MessageRPC.Clients {
			if _, ok := nodesMap[client.Addr]; !ok {
				ch <- &MessageNodeEvent{Event: eventNodeDel, Key: client}
			}
		}
		// blocking wait node changed
		event := <-watch
		log.Info("zk path: \"%s\" receive a event %v", fpath, event)
	}
}
开发者ID:yangyibo,项目名称:gopush-cluster,代码行数:55,代码来源:message.go


示例18: CreateNode

func CreateNode(path string, conn *zk.Conn) error {
	var err error
	for i := 0; i < 5; i++ {
		_, err = conn.Create(path, []byte{}, 0, zk.WorldACL(zk.PermAll))
		if err == zk.ErrNodeExists || err == nil {
			return nil
		}
		glog.Warningf("Could not create node:%s, %v", path, err)
	}
	return err
}
开发者ID:jhuie-zenoss,项目名称:serviced,代码行数:11,代码来源:utils.go


示例19: createParentPath

// create the parent znodes up to path. does not error if any of the
// parent znodes exist.
func createParentPath(path string, conn *zk.Conn, acl []zk.ACL) error {
	parts := strings.Split(path, "/")
	prePath := ""
	for _, p := range parts[1 : len(parts)-1] {
		prePath += "/" + p
		_, err := conn.Create(prePath, []byte{}, 0, acl)
		if err != nil && err != zk.ErrNodeExists {
			return err
		}
	}
	return nil
}
开发者ID:spenczar,项目名称:zksync,代码行数:14,代码来源:zkutils.go


示例20: CreateOrUpdate

func CreateOrUpdate(zconn zookeeper.Conn, zkPath, value string, flags int, aclv []zookeeper.ACL, recursive bool) (pathCreated string, err error) {
	if recursive {
		pathCreated, err = CreateRecursive(zconn, zkPath, value, 0, zookeeper.WorldACL(zookeeper.PermAll))
	} else {
		pathCreated, err = zconn.Create(zkPath, []byte(value), 0, zookeeper.WorldACL(zookeeper.PermAll))
	}
	if err != nil && ZkErrorEqual(err, zookeeper.ErrNodeExists) {
		pathCreated = ""
		_, err = zconn.Set(zkPath, []byte(value), -1)
	}
	return
}
开发者ID:jackwanger,项目名称:tyrant,代码行数:12,代码来源:zk.go



注:本文中的github.com/samuel/go-zookeeper/zk.Conn类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang sarif.CreateMessage函数代码示例发布时间:2022-05-28
下一篇:
Golang zk.WorldACL函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap