zk

package
v2.1.0-alpha.1+incompa... Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2016 License: BSD-3-Clause Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// PermDirectory are default permissions for a node.
	PermDirectory = zookeeper.PermAdmin | zookeeper.PermCreate | zookeeper.PermDelete | zookeeper.PermRead | zookeeper.PermWrite

	// PermFile allows a zk node to emulate file behavior by disallowing child nodes.
	PermFile = zookeeper.PermAdmin | zookeeper.PermRead | zookeeper.PermWrite
)

Variables

View Source
var (
	// DefaultZkConfigPaths is the default list of config files to check.
	DefaultZkConfigPaths = []string{"/etc/zookeeper/zk_client.json"}
	// MagicPrefix is the Default name for the root note in the zookeeper tree.
	MagicPrefix = "zk"
)
View Source
var (
	// ErrInterrupted is returned by functions that wait for a result
	// when they are interrupted.
	ErrInterrupted = errors.New("zkutil: obtaining lock was interrupted")

	// ErrTimeout is returned by functions that wait for a result
	// when the timeout value is reached.
	ErrTimeout = errors.New("zkutil: obtaining lock timed out")
)
View Source
var ErrConnectionClosed = errors.New("ZkConn: connection is closed")

ErrConnectionClosed is returned if we try to access a closed connection.

Functions

func ChildrenRecursive

func ChildrenRecursive(zconn Conn, zkPath string) ([]string, error)

ChildrenRecursive returns the relative path of all the children of the provided node.

func CreateOrUpdate

func CreateOrUpdate(zconn Conn, zkPath string, value []byte, flags int, aclv []zookeeper.ACL, recursive bool) (pathCreated string, err error)

CreateOrUpdate creates or updates a file.

func CreateRecursive

func CreateRecursive(zconn Conn, zkPath string, value []byte, flags int, aclv []zookeeper.ACL) (pathCreated string, err error)

CreateRecursive creates a path and any pieces required, think mkdir -p. Intermediate znodes are always created empty.

func DeleteRecursive

func DeleteRecursive(zconn Conn, zkPath string, version int32) error

DeleteRecursive will delete all children of the given path.

func GuessLocalCell

func GuessLocalCell() string

GuessLocalCell reads the cell from -zk.local-cell, or the environment ZK_CLIENT_LOCAL_CELL or guess the cell by the hostname. The letter-only prefix of the string is used as the cell name. For instance:

pa1 -> pa sjl-1 -> sjl lwc1 -> lwc

func IsDirectory

func IsDirectory(aclv []zookeeper.ACL) bool

IsDirectory returns if this node should be treated as a directory.

func ObtainQueueLock

func ObtainQueueLock(zconn Conn, zkPath string, wait time.Duration, interrupted <-chan struct{}) error

ObtainQueueLock waits until we hold the lock in the provided path. The lexically lowest node is the lock holder - verify that this path holds the lock. Call this queue-lock because the semantics are a hybrid. Normal zookeeper locks make assumptions about sequential numbering that don't hold when the data in a lock is modified. if the provided 'interrupted' chan is closed, we'll just stop waiting and return an interruption error

func ResolveWildcards

func ResolveWildcards(zconn Conn, zkPaths []string) ([]string, error)

ResolveWildcards resolves paths like: /zk/nyc/vt/tablets/*/action /zk/global/vt/keyspaces/*/shards/*/action /zk/*/vt/tablets/*/action into real existing paths

If you send paths that don't contain any wildcard and don't exist, this function will return an empty array.

func Time

func Time(i int64) time.Time

Time returns a time.Time from a ZK int64 milliseconds since Epoch time.

func ZkCellFromZkPath

func ZkCellFromZkPath(zkPath string) (string, error)

ZkCellFromZkPath extracts the cell name from a zkPath.

func ZkKnownCells

func ZkKnownCells() ([]string, error)

ZkKnownCells returns all the known cells, alphabetically ordered. It will include 'global' if there is a dc-specific global cell or a global cell.

func ZkPathToZkAddr

func ZkPathToZkAddr(zkPath string) (string, error)

ZkPathToZkAddr returns the zookeeper server address to use for the given path.

func ZkTime

func ZkTime(t time.Time) int64

ZkTime returns a ZK time (int64) from a time.Time

Types

type Conn

type Conn interface {
	Get(path string) (data []byte, stat *zookeeper.Stat, err error)
	GetW(path string) (data []byte, stat *zookeeper.Stat, watch <-chan zookeeper.Event, err error)

	Children(path string) (children []string, stat *zookeeper.Stat, err error)
	ChildrenW(path string) (children []string, stat *zookeeper.Stat, watch <-chan zookeeper.Event, err error)

	Exists(path string) (stat *zookeeper.Stat, err error)
	ExistsW(path string) (stat *zookeeper.Stat, watch <-chan zookeeper.Event, err error)

	Create(path string, value []byte, flags int, aclv []zookeeper.ACL) (pathCreated string, err error)

	Set(path string, value []byte, version int32) (stat *zookeeper.Stat, err error)

	Delete(path string, version int32) (err error)

	Close() error

	ACL(path string) ([]zookeeper.ACL, *zookeeper.Stat, error)
	SetACL(path string, aclv []zookeeper.ACL, version int32) error
}

Conn is really close to the zookeeper library connection interface. So refer to the zookeeper docs for the conventions used here (for instance, using -1 as version to specify any version)

type ConnCache

type ConnCache struct {
	// contains filtered or unexported fields
}

ConnCache is a cache for Zookeeper connections which guarantees that you have at most one zookeeper connection per cell.

func NewConnCache

func NewConnCache() *ConnCache

NewConnCache creates a new Zookeeper connection cache.

func (*ConnCache) Close

func (cc *ConnCache) Close() error

Close closes all cached connections.

func (*ConnCache) ConnForPath

func (cc *ConnCache) ConnForPath(zkPath string) (cn Conn, err error)

ConnForPath returns a connection for a given Zookeeper path. If no connection is cached, it creates a new one.

type ElectorTask

type ElectorTask interface {
	Run() error
	Stop()
	// Return true if interrupted, false if it died of natural causes.
	// An interrupted task indicates that the election should stop.
	Interrupted() bool
}

ElectorTask is the interface for a task that runs essentially forever or until something bad happens. If a task must be stopped, it should be handled promptly - no second notification will be sent.

type MetaConn

type MetaConn struct {
	// contains filtered or unexported fields
}

MetaConn is an implementation of Conn that routes to multiple cells. It uses the <cell> in /zk/<cell>/... paths to decide which ZK cluster to send a given request to.

func NewMetaConn

func NewMetaConn() *MetaConn

NewMetaConn creates a MetaConn.

func (*MetaConn) ACL

func (conn *MetaConn) ACL(path string) (acl []zookeeper.ACL, stat *zookeeper.Stat, err error)

ACL implements Conn.

func (*MetaConn) Children

func (conn *MetaConn) Children(path string) (children []string, stat *zookeeper.Stat, err error)

Children implements Conn.

func (*MetaConn) ChildrenW

func (conn *MetaConn) ChildrenW(path string) (children []string, stat *zookeeper.Stat, watch <-chan zookeeper.Event, err error)

ChildrenW implements Conn.

func (*MetaConn) Close

func (conn *MetaConn) Close() error

Close implements Conn.

func (*MetaConn) Create

func (conn *MetaConn) Create(path string, value []byte, flags int, aclv []zookeeper.ACL) (pathCreated string, err error)

Create implements Conn.

func (*MetaConn) Delete

func (conn *MetaConn) Delete(path string, version int32) (err error)

Delete implements Conn.

func (*MetaConn) Exists

func (conn *MetaConn) Exists(path string) (stat *zookeeper.Stat, err error)

Exists implements Conn.

func (*MetaConn) ExistsW

func (conn *MetaConn) ExistsW(path string) (stat *zookeeper.Stat, watch <-chan zookeeper.Event, err error)

ExistsW implements Conn.

func (*MetaConn) Get

func (conn *MetaConn) Get(path string) (data []byte, stat *zookeeper.Stat, err error)

Get implements Conn.

func (*MetaConn) GetW

func (conn *MetaConn) GetW(path string) (data []byte, stat *zookeeper.Stat, watch <-chan zookeeper.Event, err error)

GetW implements Conn.

func (*MetaConn) Set

func (conn *MetaConn) Set(path string, value []byte, version int32) (stat *zookeeper.Stat, err error)

Set implements Conn.

func (*MetaConn) SetACL

func (conn *MetaConn) SetACL(path string, aclv []zookeeper.ACL, version int32) (err error)

SetACL implements Conn.

type ZElector

type ZElector struct {
	// contains filtered or unexported fields
}

ZElector stores basic state for running an election.

func CreateElection

func CreateElection(zconn Conn, zkPath string) ZElector

CreateElection returns an initialized elector. An election is really a cycle of events. You are flip-flopping between leader and candidate. It's better to think of this as a stream of events that one needs to react to.

func (ZElector) Interrupt

func (zm ZElector) Interrupt()

Interrupt releases a lock that's held.

func (ZElector) Lock

func (zm ZElector) Lock() error

Lock returns nil when the lock is acquired.

func (ZElector) LockWithTimeout

func (zm ZElector) LockWithTimeout(wait time.Duration) (err error)

LockWithTimeout returns nil when the lock is acquired. A lock is held if the file exists and you are the creator. Setting the wait to zero makes this a nonblocking lock check.

FIXME(msolo) Disallow non-super users from removing the lock?

func (*ZElector) RunTask

func (ze *ZElector) RunTask(task ElectorTask) error

RunTask returns nil when the underlyingtask ends or the error it generated.

func (ZElector) Unlock

func (zm ZElector) Unlock() error

Unlock returns nil if the lock was successfully released. Otherwise, it is most likely a zookeeper related error.

type ZLocker

type ZLocker interface {
	Lock() error
	LockWithTimeout(wait time.Duration) error
	Unlock() error
	Interrupt()
}

ZLocker is an interface for a lock that can fail.

func CreateMutex

func CreateMutex(zconn Conn, zkPath string) ZLocker

CreateMutex initializes an unacquired mutex. A mutex is released only by Unlock. You can clean up a mutex with delete, but you should be careful doing so.

type ZkConn

type ZkConn struct {
	// contains filtered or unexported fields
}

ZkConn is a client class that implements zk.Conn using a zookeeper.Conn. The conn member variable is protected by the mutex.

func DialZk

func DialZk(zkAddr string, baseTimeout time.Duration) (*ZkConn, <-chan zookeeper.Event, error)

DialZk dials a ZK server and waits for connection event. Returns a ZkConn encapsulating the zookeeper.Conn, and the zookeeper session event channel to monitor the connection

The value for baseTimeout is used as a session timeout as well, and will be used to negotiate a 'good' value with the server. From reading the zookeeper source code, it has to be between 6 and 60 seconds (2x and 20x the tickTime by default, with default tick time being 3 seconds). min session time, max session time and ticktime can all be overwritten on the zookeeper server side, so these numbers may vary.

Then this baseTimeout is used to compute other related timeouts: - connect timeout is 1/3 of baseTimeout - recv timeout is 2/3 of baseTimeout minus a ping time - send timeout is 1/3 of baseTimeout - we try to send a ping a least every baseTimeout / 3

Note the baseTimeout has *nothing* to do with the time between we call Dial and the maximum time before we receive the event on the session. The library will actually try to re-connect in the background (after each timeout), and may *never* send an event if the TCP connections always fail. Use DialZkTimeout to enforce a timeout for the initial connect.

func DialZkTimeout

func DialZkTimeout(zkAddr string, baseTimeout time.Duration, connectTimeout time.Duration) (*ZkConn, <-chan zookeeper.Event, error)

DialZkTimeout dial the server, and wait up to timeout until connection

func (*ZkConn) ACL

func (conn *ZkConn) ACL(path string) ([]zookeeper.ACL, *zookeeper.Stat, error)

ACL is part of the ZkConn interface.

func (*ZkConn) Children

func (conn *ZkConn) Children(path string) ([]string, *zookeeper.Stat, error)

Children is part of the ZkConn interface.

func (*ZkConn) ChildrenW

func (conn *ZkConn) ChildrenW(path string) ([]string, *zookeeper.Stat, <-chan zookeeper.Event, error)

ChildrenW is part of the ZkConn interface.

func (*ZkConn) Close

func (conn *ZkConn) Close() error

Close will close the connection asynchronously. It will never fail, even though closing the connection might fail in the background. Accessing this ZkConn after Close has been called will return ErrConnectionClosed.

func (*ZkConn) Create

func (conn *ZkConn) Create(path string, value []byte, flags int, aclv []zookeeper.ACL) (pathCreated string, err error)

Create is part of the ZkConn interface.

func (*ZkConn) Delete

func (conn *ZkConn) Delete(path string, version int32) (err error)

Delete is part of the ZkConn interface.

func (*ZkConn) Exists

func (conn *ZkConn) Exists(path string) (*zookeeper.Stat, error)

Exists is part of the ZkConn interface.

func (*ZkConn) ExistsW

func (conn *ZkConn) ExistsW(path string) (*zookeeper.Stat, <-chan zookeeper.Event, error)

ExistsW is part of the ZkConn interface.

func (*ZkConn) Get

func (conn *ZkConn) Get(path string) ([]byte, *zookeeper.Stat, error)

Get is part of the ZkConn interface.

func (*ZkConn) GetW

func (conn *ZkConn) GetW(path string) ([]byte, *zookeeper.Stat, <-chan zookeeper.Event, error)

GetW is part of the ZkConn interface.

func (*ZkConn) Set

func (conn *ZkConn) Set(path string, value []byte, version int32) (*zookeeper.Stat, error)

Set is part of the ZkConn interface.

func (*ZkConn) SetACL

func (conn *ZkConn) SetACL(path string, aclv []zookeeper.ACL, version int32) error

SetACL is part of the ZkConn interface.

Directories

Path Synopsis
Package fakezk is a pretty complete mock implementation of a Zookeper connection (see go/zk/zk.Conn).
Package fakezk is a pretty complete mock implementation of a Zookeper connection (see go/zk/zk.Conn).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL