Documentation ¶
Overview ¶
gozk - ZooKeeper support for the Go language
https://wiki.ubuntu.com/gozk
Copyright (c) 2010-2011 Canonical Ltd.
Written by Gustavo Niemeyer <[email protected]>
Index ¶
- Constants
- Variables
- func CountPendingWatches() int
- func IsError(err error, code ErrorCode) bool
- func SetLogLevel(level int)
- type ACL
- type ChangeFunc
- type ClientId
- type Conn
- func (conn *Conn) ACL(path string) ([]ACL, *Stat, error)
- func (conn *Conn) AddAuth(scheme, cert string) error
- func (conn *Conn) Children(path string) (children []string, stat *Stat, err error)
- func (conn *Conn) ChildrenW(path string) (children []string, stat *Stat, watch <-chan Event, err error)
- func (conn *Conn) ClientId() *ClientId
- func (conn *Conn) Close() error
- func (conn *Conn) Create(path, value string, flags int, aclv []ACL) (pathCreated string, err error)
- func (conn *Conn) Delete(path string, version int) (err error)
- func (conn *Conn) Exists(path string) (stat *Stat, err error)
- func (conn *Conn) ExistsW(path string) (stat *Stat, watch <-chan Event, err error)
- func (conn *Conn) Get(path string) (data string, stat *Stat, err error)
- func (conn *Conn) GetW(path string) (data string, stat *Stat, watch <-chan Event, err error)
- func (conn *Conn) RetryChange(path string, flags int, acl []ACL, changeFunc ChangeFunc) error
- func (conn *Conn) Set(path, value string, version int) (stat *Stat, err error)
- func (conn *Conn) SetACL(path string, aclv []ACL, version int) error
- type Error
- type ErrorCode
- type Event
- type Server
- type Stat
- func (stat *Stat) AVersion() int
- func (stat *Stat) CTime() time.Time
- func (stat *Stat) CVersion() int
- func (stat *Stat) Czxid() int64
- func (stat *Stat) DataLength() int
- func (stat *Stat) EphemeralOwner() int64
- func (stat *Stat) MTime() time.Time
- func (stat *Stat) Mzxid() int64
- func (stat *Stat) NumChildren() int
- func (stat *Stat) Pzxid() int64
- func (stat *Stat) Version() int
Constants ¶
const ( LOG_ERROR = C.ZOO_LOG_LEVEL_ERROR LOG_WARN = C.ZOO_LOG_LEVEL_WARN LOG_INFO = C.ZOO_LOG_LEVEL_INFO LOG_DEBUG = C.ZOO_LOG_LEVEL_DEBUG )
Constants for SetLogLevel.
const ( EPHEMERAL = 1 << iota SEQUENCE )
Constants for Create's flags parameter.
const ( PERM_READ = 1 << iota PERM_WRITE PERM_CREATE PERM_DELETE PERM_ADMIN PERM_ALL = 0x1f )
Constants for ACL Perms.
const ( EVENT_CREATED = iota + 1 EVENT_DELETED EVENT_CHANGED EVENT_CHILD EVENT_SESSION = -1 EVENT_NOTWATCHING = -2 // Doesn't really exist in zk, but handy for use in zeroed Event // values (e.g. closed channels). EVENT_CLOSED = 0 )
Constants for Event Type.
const ( STATE_EXPIRED_SESSION = -112 STATE_AUTH_FAILED = -113 STATE_CONNECTING = 1 STATE_ASSOCIATING = 2 STATE_CONNECTED = 3 // Doesn't really exist in zk, but handy for use in zeroed Event // values (e.g. closed channels). STATE_CLOSED = 0 )
Constants for Event State.
Variables ¶
ErrNotRunning is the error returned when Process cannot find the currently running zookeeper process.
Functions ¶
func CountPendingWatches ¶
func CountPendingWatches() int
CountPendingWatches returns the number of pending watches which have not been fired yet, across all ZooKeeper instances. This is useful mostly as a debugging and testing aid.
func IsError ¶
IsError returns whether the error is a *Error with the given error code.
Types ¶
type ACL ¶
ACL represents one access control list element, providing the permissions (one of PERM_*), the scheme ("digest", etc), and the id (scheme-dependent) for the access control mechanism in ZooKeeper.
func AuthACL ¶
AuthACL produces an ACL list containing a single ACL which uses the provided permissions, with the scheme "auth", and ID "", which is used by ZooKeeper to represent any authenticated user.
type ClientId ¶
type ClientId struct {
// contains filtered or unexported fields
}
ClientId represents an established ZooKeeper session. It can be passed into Redial to reestablish a connection to an existing session.
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn represents a connection to a set of ZooKeeper nodes.
func Dial ¶
Dial initializes the communication with a ZooKeeper cluster. The provided servers parameter may include multiple server addresses, separated by commas, so that the client will automatically attempt to connect to another server if one of them stops working for whatever reason.
The recvTimeout parameter, given in nanoseconds, allows controlling the amount of time the connection can stay unresponsive before the server will be considered problematic.
Session establishment is asynchronous, meaning that this function will return before the communication with ZooKeeper is fully established. The watch channel receives events of type SESSION_EVENT when any change to the state of the established connection happens. See the documentation for the Event type for more details.
func Redial ¶
func Redial(servers string, recvTimeout time.Duration, clientId *ClientId) (*Conn, <-chan Event, error)
Redial is equivalent to Dial, but attempts to reestablish an existing session identified via the clientId parameter.
func (*Conn) ACL ¶
ACL returns the access control list for path.
func (*Conn) AddAuth ¶
AddAuth adds a new authentication certificate to the ZooKeeper interaction. The scheme parameter will specify how to handle the authentication information, while the cert parameter provides the identity data itself. For instance, the "digest" scheme requires a pair like "username:password" to be provided as the certificate.
func (*Conn) Children ¶
Children returns the children list and status from an existing node. Attempting to retrieve the children list from a non-existent node is an error.
func (*Conn) ChildrenW ¶
func (conn *Conn) ChildrenW(path string) (children []string, stat *Stat, watch <-chan Event, err error)
ChildrenW works like Children but also returns a channel that will receive a single Event value when a node is added or removed under the provided path or when critical session events happen. See the documentation of the Event type for more details.
func (*Conn) ClientId ¶
ClientId returns the client ID for the existing session with ZooKeeper. This is useful to reestablish an existing session via ReInit.
func (*Conn) Create ¶
Create creates a node at the given path with the given data. The provided flags may determine features such as whether the node is ephemeral or not, or whether it should have a sequence number attached to it, and the provided ACLs will determine who can access the node and under which circumstances.
The returned path is useful in cases where the created path may differ from the requested one, such as when a sequence number is appended to it due to the use of the gozk.SEQUENCE flag.
func (*Conn) Delete ¶
Delete removes the node at path. If version is not -1, the operation will only succeed if the node is still at this version when the node is deleted as an atomic operation.
func (*Conn) Exists ¶
Exists checks if a node exists at the given path. If it does, stat will contain meta information on the existing node, otherwise it will be nil.
func (*Conn) ExistsW ¶
ExistsW works like Exists but also returns a channel that will receive an Event value when a node is created in case the returned stat is nil and the node didn't exist, or when the existing node is removed. It will also receive critical session events. See the documentation of the Event type for more details.
func (*Conn) Get ¶
Get returns the data and status from an existing node. err will be nil, unless an error is found. Attempting to retrieve data from a non-existing node is an error.
func (*Conn) GetW ¶
GetW works like Get but also returns a channel that will receive a single Event value when the data or existence of the given ZooKeeper node changes or when critical session events happen. See the documentation of the Event type for more details.
func (*Conn) RetryChange ¶
RetryChange runs changeFunc to attempt to atomically change path in a lock free manner, and retries in case there was another concurrent change between reading and writing the node.
changeFunc must work correctly if called multiple times in case the modification fails due to concurrent changes, and it may return an error that will cause the the RetryChange function to stop and return the same error.
This mechanism is not suitable for a node that is frequently modified concurrently. For those cases, consider using a pessimistic locking mechanism.
This is the detailed operation flow for RetryChange:
1. Attempt to read the node. In case the node exists, but reading it fails, stop and return the error found.
2. Call the changeFunc with the current node value and stat, or with an empty string and nil stat, if the node doesn't yet exist. If the changeFunc returns an error, stop and return the same error.
3. If the changeFunc returns no errors, use the string returned as the new candidate value for the node, and attempt to either create the node, if it didn't exist, or to change its contents at the specified version. If this procedure fails due to conflicts (concurrent changes in the same node), repeat from step 1. If this procedure fails with any other error, stop and return the error found.
func (*Conn) Set ¶
Set modifies the data for the existing node at the given path, replacing it by the provided value. If version is not -1, the operation will only succeed if the node is still at the given version when the replacement happens as an atomic operation. The returned Stat value will contain data for the resulting node, after the operation is performed.
It is an error to attempt to set the data of a non-existing node with this function. In these cases, use Create instead.
type Error ¶
type Error struct { Op string Code ErrorCode // SystemError holds an error if Code is ZSYSTEMERROR. SystemError error Path string }
Error represents a ZooKeeper error.
type ErrorCode ¶
type ErrorCode int
ErrorCode represents a kind of ZooKeeper error.
const ( ZOK ErrorCode = C.ZOK ZSYSTEMERROR ErrorCode = C.ZSYSTEMERROR ZRUNTIMEINCONSISTENCY ErrorCode = C.ZRUNTIMEINCONSISTENCY ZDATAINCONSISTENCY ErrorCode = C.ZDATAINCONSISTENCY ZCONNECTIONLOSS ErrorCode = C.ZCONNECTIONLOSS ZMARSHALLINGERROR ErrorCode = C.ZMARSHALLINGERROR ZUNIMPLEMENTED ErrorCode = C.ZUNIMPLEMENTED ZOPERATIONTIMEOUT ErrorCode = C.ZOPERATIONTIMEOUT ZBADARGUMENTS ErrorCode = C.ZBADARGUMENTS ZINVALIDSTATE ErrorCode = C.ZINVALIDSTATE ZAPIERROR ErrorCode = C.ZAPIERROR ZNONODE ErrorCode = C.ZNONODE ZNOAUTH ErrorCode = C.ZNOAUTH ZBADVERSION ErrorCode = C.ZBADVERSION ZNOCHILDRENFOREPHEMERALS ErrorCode = C.ZNOCHILDRENFOREPHEMERALS ZNODEEXISTS ErrorCode = C.ZNODEEXISTS ZNOTEMPTY ErrorCode = C.ZNOTEMPTY ZSESSIONEXPIRED ErrorCode = C.ZSESSIONEXPIRED ZINVALIDCALLBACK ErrorCode = C.ZINVALIDCALLBACK ZINVALIDACL ErrorCode = C.ZINVALIDACL ZAUTHFAILED ErrorCode = C.ZAUTHFAILED ZCLOSING ErrorCode = C.ZCLOSING ZNOTHING ErrorCode = C.ZNOTHING ZSESSIONMOVED ErrorCode = C.ZSESSIONMOVED )
type Event ¶
type Event struct { Type int // One of the EVENT_* constants. Path string // For non-session events, the path of the watched node. State int // One of the STATE_* constants. }
Event channels are used to provide notifications of changes in the ZooKeeper connection state and in specific node aspects.
There are two sources of events: the session channel obtained during initialization with Init, and any watch channels obtained through one of the W-suffixed functions (GetW, ExistsW, etc).
The session channel will only receive session-level events notifying about critical and transient changes in the ZooKeeper connection state (STATE_CONNECTED, STATE_EXPIRED_SESSION, etc). On long running applications the session channel must *necessarily* be observed since certain events like session expirations require an explicit reconnection and reestablishment of state (or bailing out). Because of that, the buffer used on the session channel has a limited size, and a panic will occur if too many events are not collected.
Watch channels enable monitoring state for nodes, and the moment they're fired depends on which function was called to create them. Note that, unlike in other ZooKeeper interfaces, gozk will NOT dispatch unimportant session events such as STATE_ASSOCIATING, STATE_CONNECTING and STATE_CONNECTED to watch Event channels, since they are transient and disruptive to the workflow. Critical state changes such as expirations are still delivered to all event channels, though, and the transient events may be obsererved in the session channel.
Since every watch channel may receive critical session events, events received must not be handled blindly as if the watch requested has been fired. To facilitate such tests, Events offer the Ok method, and they also have a good String method so they may be used as an os.Error value if wanted. E.g.:
event := <-watch if !event.Ok() { err = event return }
Note that closed channels will deliver zeroed Event, which means event.Type is set to EVENT_CLOSED and event.State is set to STATE_CLOSED, to facilitate handling.
func (Event) Ok ¶
Ok returns true in case the event reports zk as being in a usable state.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server represents a ZooKeeper server, its data and configuration files.
func AttachServer ¶
AttachServer creates a new ZooKeeper Server instance to operate inside an existing run directory, runDir. The directory must have been created with CreateServer.
func CreateServer ¶
CreateServer creates the directory runDir and sets up a ZooKeeper server environment inside it. It is an error if runDir already exists and is not empty. The server will listen on the specified TCP port.
The ZooKeeper installation directory is specified by zkDir. If this is empty, a system default will be used.
CreateServer does not start the server.
func (*Server) Addr ¶
Addr returns a local host address that can be used to contact the server when it is running.
func (*Server) Destroy ¶
Destroy stops the ZooKeeper server, and then removes its run directory. Warning: this will destroy all data associated with the server.
func (*Server) Process ¶
Process returns a Process referring to the running server from where it's been stored in pid.txt. If the file does not exist, or it cannot find the process, it returns the error ErrNotRunning.
func (*Server) Start ¶
Start starts the ZooKeeper server. It returns an error if the server is already running.
type Stat ¶
type Stat struct {
// contains filtered or unexported fields
}
Stat contains detailed information about a node.
func (*Stat) AVersion ¶
AVersion returns the number of changes to the ACL of the node.
func (*Stat) CTime ¶
CTime returns the time (at millisecond resolution) when the node was created.
func (*Stat) CVersion ¶
CVersion returns the number of changes to the children of the node. This only changes when children are created or removed.
func (*Stat) Czxid ¶
Czxid returns the zxid of the change that caused the node to be created.
func (*Stat) DataLength ¶
DataLength returns the length of the data in the node in bytes.
func (*Stat) EphemeralOwner ¶
If the node is an ephemeral node, EphemeralOwner returns the session id of the owner of the node; otherwise it will return zero.
func (*Stat) MTime ¶
MTime returns the time (at millisecond resolution) when the node was last modified.
func (*Stat) Mzxid ¶
Mzxid returns the zxid of the change that last modified the node.
func (*Stat) NumChildren ¶
NumChildren returns the number of children of the node.
func (*Stat) Pzxid ¶
Pzxid returns the Pzxid of the node, whatever that is.