Ejemplo n.º 1
0
// notify every Comet node to migrate
func notifyMigrate(conn *zk.Conn, migrateLockPath, znode, key string, update bool, nodeWeightMap map[string]int) (err error) {
	// try lock
	if _, err = conn.Create(migrateLockPath, []byte("1"), zk.FlagEphemeral, zk.WorldACL(zk.PermAll)); err != nil {
		log.Error("conn.Create(\"/gopush-migrate-lock\", \"1\", zk.FlagEphemeral) error(%v)", err)
		return
	}
	// call comet migrate rpc
	wg := &sync.WaitGroup{}
	wg.Add(len(cometNodeInfoMap))
	for node, nodeInfo := range cometNodeInfoMap {
		go func(n string, info *CometNodeInfo) {
			if info.Rpc == nil {
				log.Error("notify migrate failed, no rpc found, node:%s", n)
				wg.Done()
				return
			}
			r := info.Rpc.Get()
			if r == nil {
				log.Error("notify migrate failed, no rpc found, node:%s", n)
				wg.Done()
				return
			}
			reply := 0
			args := &CometMigrateArgs{Nodes: nodeWeightMap}
			if err = r.Call(CometServiceMigrate, args, &reply); err != nil {
				log.Error("rpc.Call(\"%s\") error(%v)", CometServiceMigrate, err)
				wg.Done()
				return
			}
			log.Debug("notify node:%s migrate succeed", n)
			wg.Done()
		}(node, nodeInfo)
	}
	wg.Wait()
	// update znode info
	if update {
		var data []byte
		data, err = json.Marshal(cometNodeInfoMap[key])
		if err != nil {
			log.Error("json.Marshal() node:%s error(%v)", key, err)
			return
		}
		if _, err = conn.Set(znode, data, -1); err != nil {
			log.Error("conn.Set(\"%s\",\"%s\",\"-1\") error(%v)", znode, string(data), err)
			return
		}
	}

	// release lock
	if err = conn.Delete(migrateLockPath, -1); err != nil {
		log.Error("conn.Delete(\"%s\") error(%v)", migrateLockPath, err)
	}
	return
}
Ejemplo n.º 2
0
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
}
Ejemplo n.º 3
0
func Put(conn *zk.Conn, zkConf conf.Zookeeper, appId string, domainValue string) (*zk.Stat, error) {
	path := concatPath(zkConf.Path, appId)
	stats, err := conn.Set(path, []byte(domainValue), -1)

	if err != nil {
		return nil, err
	}
	// Force triger an event on parent
	conn.Set(zkConf.Path, []byte{}, -1)

	return stats, nil
}
Ejemplo n.º 4
0
func zk_write(k string, v string, c *zk.Conn) {
	e, stat, err := c.Exists(k)
	check(err)
	if e {
		stat, err = c.Set(k, []byte(v), stat.Version)
		check(err)
	} else {
		string, err := c.Create(k, []byte(v), int32(0), zk.WorldACL(zk.PermAll))
		if string == "" {
			check(err)
		}
	}
}
Ejemplo n.º 5
0
func createOrSet(zkConn *zk.Conn, path string, data []byte, flags int32, acl []zk.ACL) error {
	exists, _, err := zkConn.Exists(path)
	if err != nil {
		return err
	}

	if exists {
		_, err = zkConn.Set(path, data, -1)
	} else {
		err = createRecursively(zkConn, path, data, flags, acl)
	}

	return err
}
Ejemplo n.º 6
0
// zkData create zookeeper path, if path exists ignore error, and set node data.
func zkData(conn *zk.Conn, redisMaster string) error {
	node := path.Join(conf.ZKPath, conf.Node)
	tpath := ""
	for _, str := range strings.Split(conf.ZKPath, "/")[1:] {
		tpath = path.Join(tpath, "/", str)
		log.Info("create zookeeper path: \"%s\"", tpath)
		_, err := conn.Create(tpath, []byte(""), 0, zk.WorldACL(zk.PermAll))
		if err != nil {
			if err == zk.ErrNodeExists {
				log.Warn("zk.create(\"%s\") exists", tpath)
			} else {
				log.Error("zk.create(\"%s\") error(%v)", tpath, err)
				return err
			}
		}
	}
	if _, err := conn.Create(node, []byte{}, 0, zk.WorldACL(zk.PermAll)); err != nil {
		if err == zk.ErrNodeExists {
			oData, stat, err := conn.Get(node)
			if err != nil {
				log.Error("zk.Get(\"%s\") error(%v)", node, err)
				return err
			}
			ni := unmarshal(oData)
			if ni != nil || len(ni.Servers) == 0 {
				log.Warn("node have not data")
				return nil
			}
			data := marshal(ni, redisMaster)
			if len(data) == 0 {
				log.Warn("marshal error")
				return nil
			}
			if bytes.Equal(oData, data) {
				log.Warn("zk data same, no change")
				return nil
			}
			if _, err = conn.Set(node, data, stat.Version); err != nil {
				log.Error("zk.Set(\"%s\", data, 0) error(%v)", node, err)
				return err
			}
			log.Info("zk update data: \"%s\"", node)
		} else {
			log.Error("zk.create(\"%s\") error(%v)", tpath, err)
			return err
		}
	}
	return nil
}