class Zookeeper (View source)

Zookeeper class.

Constants

PERM_READ

PERM_WRITE

PERM_CREATE

PERM_DELETE

PERM_ADMIN

PERM_ALL

EPHEMERAL

SEQUENCE

EXPIRED_SESSION_STATE

AUTH_FAILED_STATE

CONNECTING_STATE

ASSOCIATING_STATE

CONNECTED_STATE

NOTCONNECTED_STATE

CREATED_EVENT

DELETED_EVENT

CHANGED_EVENT

CHILD_EVENT

SESSION_EVENT

NOTWATCHING_EVENT

LOG_LEVEL_ERROR

LOG_LEVEL_WARN

LOG_LEVEL_INFO

LOG_LEVEL_DEBUG

SYSTEMERROR

RUNTIMEINCONSISTENCY

DATAINCONSISTENCY

CONNECTIONLOSS

MARSHALLINGERROR

UNIMPLEMENTED

OPERATIONTIMEOUT

BADARGUMENTS

INVALIDSTATE

NEWCONFIGNOQUORUM Since: 3.5

RECONFIGINPROGRESS Since: 3.5

OK

APIERROR

NONODE

NOAUTH

BADVERSION

NOCHILDRENFOREPHEMERALS

NODEEXISTS

NOTEMPTY

SESSIONEXPIRED

INVALIDCALLBACK

INVALIDACL

AUTHFAILED

CLOSING

NOTHING

SESSIONMOVED

Methods

__construct(string $host = '', callable $watcher_cb = null, int $recv_timeout = 10000)

Create a handle to used communicate with zookeeper.

connect(string $host, callable $watcher_cb = null, int $recv_timeout = 10000)

Create a handle to used communicate with zookeeper.

close()

Close the zookeeper handle and free up any resources.

string
create(string $path, string $value, array $acl, int $flags = null)

Create a node synchronously.

bool
delete(string $path, int $version = -1)

Delete a node in zookeeper synchronously.

bool
set(string $path, string $data, int $version = -1, array $stat = null)

Sets the data associated with a node.

string
get(string $path, callable $watcher_cb = null, array $stat = null, int $max_size = 0)

Gets the data associated with a node synchronously.

array|false
getChildren(string $path, callable $watcher_cb = null)

Get children data of a path.

bool
exists(string $path, callable $watcher_cb = null)

Checks the existence of a node in zookeeper synchronously.

array
getAcl(string $path)

Gets the acl associated with a node synchronously.

bool
setAcl(string $path, int $version, array $acls)

Sets the acl associated with a node synchronously.

int
getClientId()

return the client session id, only valid if the connections is currently connected (ie. last watcher state is ZOO_CONNECTED_STATE).

bool
setWatcher(callable $watcher_cb)

Set a watcher function.

int
getState()

Get the state of the zookeeper connection.

int
getRecvTimeout()

Return the timeout for this session, only valid if the connections is currently connected (ie. last watcher state is ZOO_CONNECTED_STATE). This value may change after a server reconnect.

bool
addAuth(string $scheme, string $cert, callable $completion_cb = null)

Specify application credentials.

bool
isRecoverable()

Checks if the current zookeeper connection state can be recovered.

bool
setLogStream(resource $file)

Sets the stream to be used by the library for logging.

static bool
setDebugLevel(int $level)

Sets the debugging level for the library.

static bool
setDeterministicConnOrder(bool $trueOrFalse)

Enable/disable quorum endpoint order randomization.

Details

__construct(string $host = '', callable $watcher_cb = null, int $recv_timeout = 10000)

Create a handle to used communicate with zookeeper.

If the host is provided, attempt to connect.

Parameters

string $host

comma separated host:port pairs, each corresponding to a zk server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002"

callable $watcher_cb

the global watcher callback function. When notifications are triggered this function will be invoked.

int $recv_timeout

the timeout for this session, only valid if the connections is currently connected (ie. last watcher state is ZOO_CONNECTED_STATE).

Exceptions

ZookeeperException
ZookeeperConnectionException

connect(string $host, callable $watcher_cb = null, int $recv_timeout = 10000)

Create a handle to used communicate with zookeeper.

Parameters

string $host

Comma separated host:port pairs, each corresponding to a zk server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002"

callable $watcher_cb

The global watcher callback function. When notifications are triggered this function will be invoked.

int $recv_timeout

The timeout for this session, only valid if the connection is currently connected (ie. last watcher state is ZOO_CONNECTED_STATE).

Exceptions

ZookeeperException
ZookeeperConnectionException

close()

Close the zookeeper handle and free up any resources.

string create(string $path, string $value, array $acl, int $flags = null)

Create a node synchronously.

Parameters

string $path

The name of the node. Expressed as a file name with slashes separating ancestors of the node.

string $value

The data to be stored in the node.

array $acl
int $flags

this parameter can be set to 0 for normal create or an OR of the Create Flags

Return Value

string

Returns the path of the new node (this might be different than the supplied path because of the ZOO_SEQUENCE flag) on success, and false on failure.

Exceptions

ZookeeperException
ZookeeperNoNodeException

bool delete(string $path, int $version = -1)

Delete a node in zookeeper synchronously.

Parameters

string $path

The name of the node. Expressed as a file name with slashes separating ancestors of the node.

int $version

The expected version of the node. The function will fail if the actual version of the node does not match the expected version. If -1 is used the version check will not take place.

Return Value

bool

Returns true on success or false on failure.

Exceptions

ZookeeperException
ZookeeperNoNodeException

bool set(string $path, string $data, int $version = -1, array $stat = null)

Sets the data associated with a node.

Parameters

string $path

The name of the node. Expressed as a file name with slashes separating ancestors of the node.

string $data
int $version

The expected version of the node. The function will fail if the actual version of the node does not match the expected version. If -1 is used the version check will not take place.

array $stat

If not NULL, will hold the value of stat for the path on return.

Return Value

bool

Returns true on success or false on failure.

Exceptions

ZookeeperException
ZookeeperNoNodeException

string get(string $path, callable $watcher_cb = null, array $stat = null, int $max_size = 0)

Gets the data associated with a node synchronously.

Parameters

string $path

The name of the node. Expressed as a file name with slashes separating ancestors of the node.

callable $watcher_cb

If nonzero, a watch will be set at the server to notify the client if the node changes.

array $stat

If not NULL, will hold the value of stat for the path on return.

int $max_size

Max size of the data. If 0 is used, this method will return the whole data.

Return Value

string

Returns the data on success, and false on failure.

Exceptions

ZookeeperException
ZookeeperNoNodeException

array|false getChildren(string $path, callable $watcher_cb = null)

Get children data of a path.

Parameters

string $path

The name of the node. Expressed as a file name with slashes separating ancestors of the node.

callable $watcher_cb

If nonzero, a watch will be set at the server to notify the client if the node changes.

Return Value

array|false

Returns an array with children paths on success, and false on failure.

Exceptions

ZookeeperException
ZookeeperNoNodeException

bool exists(string $path, callable $watcher_cb = null)

Checks the existence of a node in zookeeper synchronously.

Parameters

string $path

The name of the node. Expressed as a file name with slashes separating ancestors of the node.

callable $watcher_cb

if nonzero, a watch will be set at the server to notify the client if the node changes. The watch will be set even if the node does not

Return Value

bool

Returns the value of stat for the path if the given node exists, otherwise false.

Exceptions

ZookeeperException

array getAcl(string $path)

Gets the acl associated with a node synchronously.

Parameters

string $path

The name of the node. Expressed as a file name with slashes separating ancestors of the node.

Return Value

array

Return acl array on success and false on failure.

Exceptions

ZookeeperException

bool setAcl(string $path, int $version, array $acls)

Sets the acl associated with a node synchronously.

Parameters

string $path

The name of the node. Expressed as a file name with slashes separating ancestors of the node.

int $version

The expected version of the path.

array $acls

Return Value

bool

Returns true on success or false on failure.

Exceptions

ZookeeperException

int getClientId()

return the client session id, only valid if the connections is currently connected (ie. last watcher state is ZOO_CONNECTED_STATE).

Return Value

int

Returns the client session id on success, and false on failure.

Exceptions

ZookeeperException
ZookeeperConnectionException

bool setWatcher(callable $watcher_cb)

Set a watcher function.

Parameters

callable $watcher_cb

A watch will be set at the server to notify the client if the node changes.

Return Value

bool

Returns true on success or false on failure.

Exceptions

ZookeeperException
ZookeeperConnectionException

int getState()

Get the state of the zookeeper connection.

Return Value

int

Returns the state of zookeeper connection on success, and false on failure.

Exceptions

ZookeeperException
ZookeeperConnectionException

int getRecvTimeout()

Return the timeout for this session, only valid if the connections is currently connected (ie. last watcher state is ZOO_CONNECTED_STATE). This value may change after a server reconnect.

Return Value

int

Returns the timeout for this session on success, and false on failure.

Exceptions

ZookeeperException
ZookeeperConnectionException

bool addAuth(string $scheme, string $cert, callable $completion_cb = null)

Specify application credentials.

Parameters

string $scheme

The id of authentication scheme. Natively supported: "digest" password-based authentication

string $cert

Application credentials. The actual value depends on the scheme.

callable $completion_cb

The routine to invoke when the request completes. One of the following result codes may be passed into the completion callback: - ZOK operation completed successfully - ZAUTHFAILED authentication failed

Return Value

bool

Returns true on success or false on failure.

Exceptions

ZookeeperException
ZookeeperConnectionException

bool isRecoverable()

Checks if the current zookeeper connection state can be recovered.

Return Value

bool

Returns true/false on success, and false on failure.

Exceptions

ZookeeperException
ZookeeperConnectionException

bool setLogStream(resource $file)

Sets the stream to be used by the library for logging.

TODO: might be able to set a stream like php://stderr or something

Parameters

resource $file

Return Value

bool

Returns true on success or false on failure.

static bool setDebugLevel(int $level)

Sets the debugging level for the library.

Parameters

int $level

Return Value

bool

Returns true on success or false on failure.

static bool setDeterministicConnOrder(bool $trueOrFalse)

Enable/disable quorum endpoint order randomization.

Parameters

bool $trueOrFalse

Return Value

bool

Returns true on success or false on failure.