Пример #1
0
func createPath(path string, data []byte, client *zk.Conn) error {
	exists, _, err := client.Exists(path)
	if err != nil {
		return err
	}

	if exists {
		return nil
	}

	name := "/"
	p := strings.Split(path, "/")

	for _, v := range p[1 : len(p)-1] {
		name += v
		e, _, _ := client.Exists(name)
		if !e {
			_, err = client.Create(name, []byte{}, int32(0), zk.WorldACL(zk.PermAll))
			if err != nil {
				return err
			}
		}
		name += "/"
	}

	_, err = client.Create(path, data, int32(0), zk.WorldACL(zk.PermAll))
	return err
}
Пример #2
0
func ensurePathExists(conn *zk.Conn, path string) error {
	pathExists, _, _ := conn.Exists(path)
	if pathExists {
		return nil
	}

	_, err := conn.Create(path, []byte{}, 0, defaultACL())
	if err != nil {
		return err
	}

	return nil
}
Пример #3
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)
		}
	}
}
Пример #4
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
}
Пример #5
0
func ListenToConn(c *zk.Conn, path string, deb bool, repDelay time.Duration) (chan zk.Event, chan bool) {
	exists, _, err := c.Exists(path)

	if err != nil {
		logger.Fatalf("Couldn't determine whether node %v exists in Zookeeper", path)
	}
	if !exists {
		logger.Printf("Node '%v' does not exist in Zookeeper, creating...", path)
		err := zkNodeCreateByPath(path, c)
		if err != nil {
			logger.Fatalf("Unable to create path '%v': %v", path, err)
		}

	}

	quit := make(chan bool)
	evts := make(chan zk.Event)

	go pollZooKeeper(c, path, evts, quit)

	if deb {
		evts = debounce(evts, 100*time.Millisecond)
	}
	if repDelay > 0 {
		evts = delay(evts, repDelay)
	}
	return evts, quit
}
Пример #6
0
// NewElection initializes a new instance of a Election that can later be used to request
// leadership for a specific resource.
//
// It accepts:
//	zkConn - a connection to a running Zookeeper instance
//	resource - the resource for which leadership is being requested
//
// It will return either a non-nil Election instance and a nil error, or a nil
// Election and a non-nil error.
//
func NewElection(zkConn *zk.Conn, electionNode string) (Election, error) {
	//TODO: what should flags and acl be set to?
	flags := int32(0)
	acl := zk.WorldACL(zk.PermAll)

	exists, _, _ := zkConn.Exists(electionNode)
	var (
		path string
		err  error
	)
	if !exists {
		path, err = zkConn.Create(electionNode, []byte("data"), flags, acl)
		must(err)
		fmt.Printf("created: %+v\n", path)
	}

	return Election{electionNode, Candidate{}, false, zkConn, nil}, nil
}
func checkPath(conn *zk.Conn, path string) {
	exists, _, err := conn.Exists(path)
	if exists {
		if path == "/" {
			path = os.Getenv("ZK_BASE")
			if path == "" {
				path = zkBase
			}
			_, err := conn.Create(path, []byte{}, flags, acl)
			check(err)
		}
		return
	}
	checkPath(conn, filepath.Dir(path))
	_, err = conn.Create(path, []byte{}, flags, acl)
	check(err)
	return
}
Пример #8
0
func mkdirs(conn *zk.Conn, path string) (err error) {
	if path == "" {
		return errors.New("path should not been empty")
	}
	if path == "/" {
		return nil
	}
	if path[0] != '/' {
		return errors.New("path must start with /")
	}

	//check whether this path exists
	exist, _, err := conn.Exists(path)
	if exist {
		return nil
	}
	flags := int32(0)
	acl := zk.WorldACL(zk.PermAll)
	_, err = conn.Create(path, []byte(""), flags, acl)
	if err == nil { //created successfully
		return
	}

	//create parent
	paths := strings.Split(path[1:], "/")
	createdPath := ""
	for _, p := range paths {
		createdPath = createdPath + "/" + p
		exist, _, _ = conn.Exists(createdPath)
		if !exist {
			_, err = conn.Create(createdPath, []byte(""), flags, acl)
			if err != nil {
				return
			}
		}
	}

	return nil
}
// Tries to create the configuration path, if it doesn't exist
// It tries multiple times in case there's a race with another quotaservice node coming up
func createPath(conn *zk.Conn, path string) (err error) {
	for i := 0; i < createRetries; i++ {
		exists, _, err := conn.Exists(path)

		if exists && err == nil {
			return nil
		}

		_, err = conn.Create(path, []byte{}, 0, zk.WorldACL(zk.PermAll))

		if err == nil {
			return nil
		}

		logging.Printf("Could not create zk path, sleeping for 100ms")
		time.Sleep(100 * time.Millisecond)
	}

	if err == nil {
		err = errors.New("could not create and get path " + path)
	}

	return err
}
Пример #10
0
func zkCreateNodes(path string, nodes []string, c *zk.Conn) error {
	if len(nodes) > 0 {
		// strings.Split will return empty-strings for leading split chars, lets skip over these.
		if len(nodes[0]) == 0 {
			return zkCreateNodes(path, nodes[1:], c)
		}
		fqPath := path + "/" + nodes[0]
		log.Printf("Creating path: %v", fqPath)
		exists, _, err := c.Exists(fqPath)
		if err != nil {
			return err
		}
		if !exists {
			_, err := c.Create(fqPath, []byte{}, 0, zk.WorldACL(zk.PermAll))
			if err != nil {
				return err
			}
		}
		return zkCreateNodes(fqPath, nodes[1:], c)
	}
	return nil
}
Пример #11
0
func resolveRecursive(zconn zookeeper.Conn, parts []string, toplevel bool) ([]string, error) {
	for i, part := range parts {
		if HasWildcard(part) {
			var children []string
			zkParentPath := strings.Join(parts[:i], "/")
			var err error
			children, _, err = zconn.Children(zkParentPath)
			if err != nil {
				// we asked for something like
				// /zk/cell/aaa/* and
				// /zk/cell/aaa doesn't exist
				// -> return empty list, no error
				// (note we check both a regular zk
				// error and the error the test
				// produces)
				if ZkErrorEqual(err, zookeeper.ErrNoNode) {
					return nil, nil
				}
				// otherwise we return the error
				return nil, err
			}
			sort.Strings(children)

			results := make([][]string, len(children))
			wg := &sync.WaitGroup{}
			mu := &sync.Mutex{}
			var firstError error

			for j, child := range children {
				matched, err := path.Match(part, child)
				if err != nil {
					return nil, err
				}
				if matched {
					// we have a match!
					wg.Add(1)
					newParts := make([]string, len(parts))
					copy(newParts, parts)
					newParts[i] = child
					go func(j int) {
						defer wg.Done()
						subResult, err := resolveRecursive(zconn, newParts, false)
						if err != nil {
							mu.Lock()
							if firstError != nil {
								log.Infof("Multiple error: %v", err)
							} else {
								firstError = err
							}
							mu.Unlock()
						} else {
							results[j] = subResult
						}
					}(j)
				}
			}

			wg.Wait()
			if firstError != nil {
				return nil, firstError
			}

			result := make([]string, 0, 32)
			for j := 0; j < len(children); j++ {
				subResult := results[j]
				if subResult != nil {
					result = append(result, subResult...)
				}
			}

			// we found a part that is a wildcard, we
			// added the children already, we're done
			return result, nil
		}
	}

	// no part contains a wildcard, add the path if it exists, and done
	path := strings.Join(parts, "/")
	if toplevel {
		// for whatever the user typed at the toplevel, we don't
		// check it exists or not, we just return it
		return []string{path}, nil
	}

	// this is an expanded path, we need to check if it exists
	_, stat, err := zconn.Exists(path)
	if err != nil {
		return nil, err
	}
	if stat != nil {
		return []string{path}, nil
	}
	return nil, nil
}