示例#1
0
func rlock(conn *gozk.Conn, basePath, prefix string, checkPrefix []string) (lock string, err error) {
	// step 1
	lock, err = conn.Create(path.Join(basePath, prefix), "", gozk.EPHEMERAL|gozk.SEQUENCE,
		gozk.WorldACL(gozk.PERM_ALL))
	if err != nil {
		return lock, err
	}
	for {
		// step 2
		children, _, err := conn.Children(basePath)
		if err != nil {
			return lock, err
		}
		// step 3
		if children == nil || len(children) == 0 {
			return lock, errors.New("get children didn't return my lock")
		}
		// filter out non-lock children and extract sequence numbers
		filteredChildren := map[string]string{}
		filteredChildrenKeys := []string{}
		for _, v := range children {
			for _, pfx := range checkPrefix {
				if strings.HasPrefix(v, pfx) {
					seqNum := strings.Replace(v, pfx, "", 1)
					filteredChildren[seqNum] = v
					filteredChildrenKeys = append(filteredChildrenKeys, seqNum)
					break
				}
			}
		}
		sort.Strings(filteredChildrenKeys)
		prevLock := ""
		for _, seqNum := range filteredChildrenKeys {
			if path.Base(lock) == filteredChildren[seqNum] {
				break
			}
			prevLock = path.Join(basePath, filteredChildren[seqNum])
		}
		if prevLock == "" {
			return lock, nil
		}
		// step 4
		stat, watchCh, err := conn.ExistsW(prevLock)
		if err != nil {
			return lock, err
		}
		// step 5
		if stat == nil {
			continue
		}
		<-watchCh
	}
	return lock, nil
}
示例#2
0
// Create a node if it doesn't exist, otherwise do nothing
func (zk *ZkConn) Touch(nodePath string) (string, error) {
	if stat, err := zk.Exists(nodePath); err == nil && stat != nil {
		return nodePath, nil
	}
	// touch all directores above nodePath because zookeeper sucks
	dir := path.Dir(nodePath)
	if dir != "/" && dir != "." {
		zk.Touch(dir)
	}
	return zk.Create(nodePath, "", 0, gozk.WorldACL(gozk.PERM_ALL))
}
示例#3
0
func (k *KeySpace) AddVnode(vnode IVnode) os.Error {
	path := k.getVnodePath(vnode)

	stat, err := k.zk.Exists(path)
	if stat != nil {
		return os.NewError(fmt.Sprintf("Node already exists at %s", path))
	}
	if err != nil {
		return err
	}

	_, err = k.zk.Create(path, "0", 0, gozk.WorldACL(gozk.PERM_ALL))
	return err
}
示例#4
0
func (z *ZkTestServer) Init() error {
	os.RemoveAll(z.TestServerDir)
	var err error
	zkPath := "/usr/local/Cellar/zookeeper/3.4.6/libexec"
	exists, err := exists(zkPath)
	if err != nil {
		return err
	}
	if !exists {
		zkPath = "/usr/share/java"
	}
	z.Server, err = gozk.CreateServer(z.TestServerPort, z.TestServerDir, zkPath)
	if err != nil {
		return err
	}
	err = z.Server.Start()
	if err != nil {
		return err
	}
	addr, err := z.Server.Addr()
	if err != nil {
		return err
	}
	z.Zk, z.ZkEventChan, err = GetZk(addr)
	if err != nil {
		return err
	}
	WaitOnConnect(z.ZkEventChan)

	//build the initial necessary paths
	log.Println(z.Zk.Conn.Create("/pools", "", 0, gozk.WorldACL(gozk.PERM_ALL)))
	log.Println(z.Zk.Conn.Create("/ports", "", 0, gozk.WorldACL(gozk.PERM_ALL)))
	log.Println(z.Zk.Conn.Create("/rules", "", 0, gozk.WorldACL(gozk.PERM_ALL)))
	log.Println(z.Zk.Conn.Create("/tries", "", 0, gozk.WorldACL(gozk.PERM_ALL)))

	return nil
}
示例#5
0
func (k *KeySpace) Connect() os.Error {
	zk, session, err := gozk.Init(k.zkServers, k.zkTimeout)
	if err != nil {
		return os.NewError("Couldn't connect: " + err.String())
	}

	event := <-session
	if event.State != gozk.STATE_CONNECTED {
		return os.NewError("Couldn't connect to zookeeper\n")
	}

	k.zk = zk

	stat, err := k.zk.Exists(k.zkRoot)
	if stat == nil {
		path, err := k.zk.Create(k.zkRoot, "root", 0, gozk.WorldACL(gozk.PERM_ALL))
		if err != nil {
			l4g.Info("Created root node: %s\n", path)
		}
	}

	return nil
}