Book/zookeeper-Phpdoc专题

ZooKeeper

目录

简介

Represents ZooKeeper session.

类摘要

Zookeeper

class Zookeeper {

/* 方法 */

public <span class="methodname">__construct ([ <span class="methodparam">string $host<span class="initializer"> = '' [, <span class="methodparam">callable $watcher_cb = null [, int $recv_timeout = 10000 ]]] )

public bool addAuth ( <span class="methodparam">string $scheme , string $cert [, <span class="type">callable $completion_cb = null ] )

public void close ( <span class="methodparam">void )

public void connect ( <span class="methodparam">string $host [, callable $watcher_cb = null [, int $recv_timeout = 10000 ]] )

public string create ( <span class="type">string $path , <span class="methodparam">string $value , array $acls [, int $flags = null ] )

public bool delete ( <span class="type">string $path [, <span class="methodparam">int $version<span class="initializer"> = -1 ] )

public array exists ( <span class="type">string $path [, <span class="methodparam">callable $watcher_cb = null ] )

public string get ( <span class="type">string $path [, <span class="methodparam">callable $watcher_cb = null [, array &$stat<span class="initializer"> = null [, <span class="methodparam">int $max_size<span class="initializer"> = 0 ]]] )

public array getAcl ( <span class="type">string $path )

public array getChildren ( <span class="methodparam">string $path [, callable $watcher_cb = null ] )

public int <span class="methodname">getClientId ( <span class="methodparam">void )

public <span class="type">ZookeeperConfig <span class="methodname">getConfig ( <span class="methodparam">void )

public int <span class="methodname">getRecvTimeout ( <span class="methodparam">void )

public int <span class="methodname">getState ( <span class="methodparam">void )

public bool isRecoverable ( <span class="methodparam">void )

public bool set ( <span class="type">string $path , <span class="methodparam">string $value [, int $version<span class="initializer"> = -1 [, <span class="methodparam">array &$stat<span class="initializer"> = null ]] )

public bool setAcl ( <span class="type">string $path , <span class="methodparam">int $version , array $acl )

public <span class="modifier">static bool <span class="methodname">setDebugLevel ( <span class="methodparam">int $logLevel )

public <span class="modifier">static bool <span class="methodname">setDeterministicConnOrder ( <span class="methodparam">bool $yesOrNo )

public bool setLogStream ( <span class="methodparam">resource $stream )

public bool setWatcher ( <span class="methodparam">callable $watcher_cb )

/* 常量 */

const int PERM_READ = 1 ;

const int PERM_WRITE = 2 ;

const int PERM_CREATE = 4 ;

const int PERM_DELETE = 8 ;

const int PERM_ADMIN = 16 ;

const int PERM_ALL = 31 ;

const int EPHEMERAL = 1 ;

const int SEQUENCE = 2 ;

const int LOG_LEVEL_ERROR = 1 ;

const int LOG_LEVEL_WARN = 2 ;

const int LOG_LEVEL_INFO = 3 ;

const int LOG_LEVEL_DEBUG = 4 ;

const int EXPIRED_SESSION_STATE = -112 ;

const int AUTH_FAILED_STATE = -113 ;

const int CONNECTING_STATE = 1 ;

const int ASSOCIATING_STATE = 2 ;

const int CONNECTED_STATE = 3 ;

const int READONLY_STATE = 5 ;

const int NOTCONNECTED_STATE = 999 ;

const int CREATED_EVENT = 1 ;

const int DELETED_EVENT = 2 ;

const int CHANGED_EVENT = 3 ;

const int CHILD_EVENT = 4 ;

const int SESSION_EVENT = -1 ;

const int NOTWATCHING_EVENT = -2 ;

const int SYSTEMERROR = -1 ;

const int RUNTIMEINCONSISTENCY = -2 ;

const int DATAINCONSISTENCY = -3 ;

const int CONNECTIONLOSS = -4 ;

const int MARSHALLINGERROR = -5 ;

const int UNIMPLEMENTED = -6 ;

const int OPERATIONTIMEOUT = -7 ;

const int BADARGUMENTS = -8 ;

const int INVALIDSTATE = -9 ;

const int NEWCONFIGNOQUORUM = -13 ;

const int RECONFIGINPROGRESS = -14 ;

const int OK = 0 ;

const int APIERROR = -100 ;

const int NONODE = -101 ;

const int NOAUTH = -102 ;

const int BADVERSION = -103 ;

const int NOCHILDRENFOREPHEMERALS = -108 ;

const int NODEEXISTS = -110 ;

const int NOTEMPTY = -111 ;

const int SESSIONEXPIRED = -112 ;

const int INVALIDCALLBACK = -113 ;

const int INVALIDACL = -114 ;

const int AUTHFAILED = -115 ;

const int CLOSING = -116 ;

const int NOTHING = -117 ;

const int SESSIONMOVED = -118 ;

const int NOTREADONLY = -119 ;

const int EPHEMERALONLOCALSESSION = -120 ;

const int NOWATCHER = -121 ;

const int RECONFIGDISABLED = -122 ;

}

预定义常量

ZooKeeper Permissions

Zookeeper::PERM_READ
Can read nodes value and list its children

Zookeeper::PERM_WRITE
Can set the nodes value

Zookeeper::PERM_CREATE
Can create children

Zookeeper::PERM_DELETE
Can delete children

Zookeeper::PERM_ADMIN
Can execute set_acl()

Zookeeper::PERM_ALL
All of the above flags ORd together

ZooKeeper Create Flags

Zookeeper::EPHEMERAL
If Zookeeper::EPHEMERAL flag is set, the node will automatically get removed if the client session goes away.

Zookeeper::SEQUENCE
If the Zookeeper::SEQUENCE flag is set, a unique monotonically increasing sequence number is appended to the path name. The sequence number is always fixed length of 10 digits, 0 padded.

ZooKeeper Log Levels

Zookeeper::LOG_LEVEL_ERROR
Outputs only error mesages

Zookeeper::LOG_LEVEL_WARN
Outputs errors/warnings

Zookeeper::LOG_LEVEL_INFO
Outputs big action messages besides errors/warnings

Zookeeper::LOG_LEVEL_DEBUG
Outputs all

ZooKeeper States

Zookeeper::EXPIRED_SESSION_STATE
Connected but session expired

Zookeeper::AUTH_FAILED_STATE
Connected but auth failed

Zookeeper::CONNECTING_STATE
Connecting

Zookeeper::ASSOCIATING_STATE
Associating

Zookeeper::CONNECTED_STATE
Connected

Zookeeper::READONLY_STATE
TODO: help us improve this extension.

Zookeeper::NOTCONNECTED_STATE
Connection failed

ZooKeeper Watch Types

Zookeeper::CREATED_EVENT
A node has been created

This is only generated by watches on non-existent nodes. These watches are set using Zookeeper::exists.

Zookeeper::DELETED_EVENT
A node has been deleted

This is only generated by watches on nodes. These watches are set using Zookeeper::exists and Zookeeper::get.

Zookeeper::CHANGED_EVENT
A node has changed

This is only generated by watches on nodes. These watches are set using Zookeeper::exists and Zookeeper::get.

Zookeeper::CHILD_EVENT
A change as occurred in the list of children

This is only generated by watches on the child list of a node. These watches are set using Zookeeper::getChildren.

Zookeeper::SESSION_EVENT
A session has been lost

This is generated when a client loses contact or reconnects with a server.

Zookeeper::NOTWATCHING_EVENT
A watch has been removed

This is generated when the server for some reason, probably a resource constraint, will no longer watch a node for a client.

ZooKeeper System and Server-side Errors

Zookeeper::SYSTEMERROR
This is never thrown by the server, it shouldn't be used other than to indicate a range. Specifically error codes greater than this value, but lesser than Zookeeper::APIERROR, are system errors.

Zookeeper::RUNTIMEINCONSISTENCY
A runtime inconsistency was found.

Zookeeper::DATAINCONSISTENCY
A data inconsistency was found.

Zookeeper::CONNECTIONLOSS
Connection to the server has been lost.

Zookeeper::MARSHALLINGERROR
Error while marshalling or unmarshalling data.

Zookeeper::UNIMPLEMENTED
Operation is unimplemented.

Zookeeper::OPERATIONTIMEOUT
Operation timeout.

Zookeeper::BADARGUMENTS
Invalid arguments.

Zookeeper::INVALIDSTATE
Invliad zhandle state.

Zookeeper::NEWCONFIGNOQUORUM
No quorum of new config is connected and up-to-date with the leader of last committed config - try invoking reconfiguration after new servers are connected and synced.

Available as of ZooKeeper 3.5.0

Zookeeper::RECONFIGINPROGRESS
Reconfiguration requested while another reconfiguration is currently in progress. This is currently not supported. Please retry.

Available as of ZooKeeper 3.5.0

ZooKeeper API Errors

Zookeeper::OK
Everything is OK.

Zookeeper::APIERROR
This is never thrown by the server, it shouldn't be used other than to indicate a range. Specifically error codes greater than this value are API errors (while values less than this indicate a Zookeeper::SYSTEMERROR).

Zookeeper::NONODE
Node does not exist.

Zookeeper::NOAUTH
Not authenticated.

Zookeeper::BADVERSION
Version conflict.

Zookeeper::NOCHILDRENFOREPHEMERALS
Ephemeral nodes may not have children.

Zookeeper::NODEEXISTS
The node already exists.

Zookeeper::NOTEMPTY
The node has children.

Zookeeper::SESSIONEXPIRED
The session has been expired by the server.

Zookeeper::INVALIDCALLBACK
Invalid callback specified.

Zookeeper::INVALIDACL
Invalid ACL specified.

Zookeeper::AUTHFAILED
Client authentication failed.

Zookeeper::CLOSING
ZooKeeper is closing.

Zookeeper::NOTHING
(not error) No server responses to process.

Zookeeper::SESSIONMOVED
Session moved to another server, so operation is ignored.

Zookeeper::NOTREADONLY
State-changing request is passed to read-only server.

Zookeeper::EPHEMERALONLOCALSESSION
Attempt to create ephemeral node on a local session.

Zookeeper::NOWATCHER
The watcher couldn't be found.

Zookeeper::RECONFIGDISABLED
Attempts to perform a reconfiguration operation when reconfiguration feature is disabled.

Zookeeper::addAuth

Specify application credentials

说明

public bool Zookeeper::addAuth ( <span class="methodparam">string $scheme , string $cert [, <span class="type">callable $completion_cb = null ] )

The application calls this function to specify its credentials for purposes of authentication. The server will use the security provider specified by the scheme parameter to authenticate the client connection. If the authentication request has failed: - the server connection is dropped. - the watcher is called with the ZOO_AUTH_FAILED_STATE value as the state parameter.

参数

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

cert
Application credentials. The actual value depends on the scheme.

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

返回值

成功时返回 true, 或者在失败时返回 false

错误/异常

This method emits PHP error/warning when parameters count or types are wrong or operation fails.

Caution

Since version 0.3.0, this method emits <span class="classname">ZookeeperException and it's derivatives.

范例

示例 #1 Zookeeper::addAuth example

Add auth before requesting node value.

<?php
$zookeeper = new Zookeeper('locahost:2181');
$path = '/path/to/node';
$value = 'nodevalue';
$zookeeper->set($path, $value);

$zookeeper->addAuth('digest', 'user0:passwd0');
$r = $zookeeper->get($path);
if ($r)
  echo $r;
else
  echo 'ERR';
?>

以上例程会输出:

nodevalue

参见

  • Zookeeper::create
  • Zookeeper::setAcl
  • Zookeeper::getAcl
  • ZooKeeper States
  • ZookeeperException

Zookeeper::close

Close the zookeeper handle and free up any resources

说明

public void Zookeeper::close ( <span class="methodparam">void )

错误/异常

This method emits ZookeeperException and it's derivatives when closing an uninitialized instance.

参见

  • Zookeeper::__construct
  • Zookeeper::connect
  • ZookeeperException

Zookeeper::connect

Create a handle to used communicate with zookeeper

说明

public void Zookeeper::connect ( <span class="methodparam">string $host [, callable $watcher_cb = null [, int $recv_timeout = 10000 ]] )

This method creates a new handle and a zookeeper session that corresponds to that handle. Session establishment is asynchronous, meaning that the session should not be considered established until (and unless) an event of state ZOO_CONNECTED_STATE is received.

参数

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"

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

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

错误/异常

This method emits PHP error/warning when parameters count or types are wrong or could not init instance.

Caution

Since version 0.3.0, this method emits <span class="classname">ZookeeperException and it's derivatives.

参见

  • Zookeeper::__construct
  • ZookeeperException

Zookeeper::__construct

Create a handle to used communicate with zookeeper

说明

public <span class="methodname">Zookeeper::__construct ([ <span class="methodparam">string $host<span class="initializer"> = '' [, <span class="methodparam">callable $watcher_cb = null [, int $recv_timeout = 10000 ]]] )

This method creates a new handle and a zookeeper session that corresponds to that handle. Session establishment is asynchronous, meaning that the session should not be considered established until (and unless) an event of state ZOO_CONNECTED_STATE is received.

参数

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"

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

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

错误/异常

This method emits PHP error/warning when parameters count or types are wrong or could not init instance.

Caution

Since version 0.3.0, this method emits <span class="classname">ZookeeperException and it's derivatives.

参见

  • Zookeeper::connect
  • ZookeeperException

Zookeeper::create

Create a node synchronously

说明

public string Zookeeper::create ( <span class="methodparam">string $path , string $value , <span class="type">array $acls [, <span class="methodparam">int $flags<span class="initializer"> = null ] )

This method will create a node in ZooKeeper. A node can only be created if it does not already exists. The Create Flags affect the creation of nodes. If ZOO_EPHEMERAL flag is set, the node will automatically get removed if the client session goes away. If the ZOO_SEQUENCE flag is set, a unique monotonically increasing sequence number is appended to the path name.

参数

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

value
The data to be stored in the node.

acls
The initial ACL of the node. The ACL must not be null or empty.

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

返回值

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.

错误/异常

This method emits PHP error/warning when parameters count or types are wrong or fail to create node.

Caution

Since version 0.3.0, this method emits <span class="classname">ZookeeperException and it's derivatives.

范例

示例 #1 Zookeeper::create example

Create a new node.

<?php
$zookeeper = new Zookeeper('locahost:2181');
$aclArray = array(
  array(
    'perms'  => Zookeeper::PERM_ALL,
    'scheme' => 'world',
    'id'     => 'anyone',
  )
);
$path = '/path/to/newnode';
$realPath = $zookeeper->create($path, null, $aclArray);
if ($realPath)
  echo $realPath;
else
  echo 'ERR';
?>

以上例程会输出:

/path/to/newnode

参见

Zookeeper::delete

Delete a node in zookeeper synchronously

说明

public bool Zookeeper::delete ( <span class="methodparam">string $path [, int $version<span class="initializer"> = -1 ] )

参数

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

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.

返回值

成功时返回 true, 或者在失败时返回 false

错误/异常

This method emits PHP error/warning when parameters count or types are wrong or fail to delete node.

Caution

Since version 0.3.0, this method emits <span class="classname">ZookeeperException and it's derivatives.

范例

示例 #1 Zookeeper::delete example

Delete a existing node.

<?php
$zookeeper = new Zookeeper('locahost:2181');
$path = '/path/to/node';
$r = $zookeeper->delete($path);
if ($r)
  echo 'SUCCESS';
else
  echo 'ERR';
?>

以上例程会输出:

SUCCESS

参见

  • Zookeeper::create
  • Zookeeper::getChildren
  • ZookeeperException

Zookeeper::exists

Checks the existence of a node in zookeeper synchronously

说明

public array Zookeeper::exists ( <span class="methodparam">string $path [, callable $watcher_cb = null ] )

参数

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

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

返回值

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

错误/异常

This method emits PHP error/warning when parameters count or types are wrong or fail to check the existence of a node.

Caution

Since version 0.3.0, this method emits <span class="classname">ZookeeperException and it's derivatives.

范例

示例 #1 Zookeeper::exists example

Check the existence of a node.

<?php
$zookeeper = new Zookeeper('locahost:2181');
$path = '/path/to/node';
$r = $zookeeper->exists($path);
if ($r)
  echo 'EXISTS';
else
  echo 'N/A or ERR';
?>

以上例程会输出:

EXISTS

参见

  • Zookeeper::get
  • ZookeeperException

Zookeeper::get

Gets the data associated with a node synchronously

说明

public string Zookeeper::get ( <span class="methodparam">string $path [, callable $watcher_cb = null [, array &$stat<span class="initializer"> = null [, <span class="methodparam">int $max_size<span class="initializer"> = 0 ]]] )

参数

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

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

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

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

返回值

Returns the data on success, and false on failure.

错误/异常

This method emits PHP error/warning when parameters count or types are wrong or fail to get value from node.

Caution

Since version 0.3.0, this method emits <span class="classname">ZookeeperException and it's derivatives.

范例

示例 #1 Zookeeper::get example

Get value from node.

<?php
$zookeeper = new Zookeeper('locahost:2181');
$path = '/path/to/node';
$value = 'nodevalue';
$zookeeper->set($path, $value);

$r = $zookeeper->get($path);
if ($r)
  echo $r;
else
  echo 'ERR';
?>

以上例程会输出:

nodevalue

示例 #2 Zookeeper::get stat example

Get node stat info.

<?php
$zookeeper = new Zookeeper('localhost:2181');
$path = '/path/to/node';
$stat = [];
$zookeeper->get($path, null, $stat);
var_dump($stat);
?>

以上例程会输出:

array(11) {
  ["czxid"]=>
  float(0)
  ["mzxid"]=>
  float(0)
  ["ctime"]=>
  float(0)
  ["mtime"]=>
  float(0)
  ["version"]=>
  int(0)
  ["cversion"]=>
  int(-2)
  ["aversion"]=>
  int(0)
  ["ephemeralOwner"]=>
  float(0)
  ["dataLength"]=>
  int(0)
  ["numChildren"]=>
  int(2)
  ["pzxid"]=>
  float(0)
}

参见

  • Zookeeper::set
  • ZookeeperException

Zookeeper::getAcl

Gets the acl associated with a node synchronously

说明

public array Zookeeper::getAcl ( <span class="methodparam">string $path )

参数

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

返回值

Return acl array on success and false on failure.

错误/异常

This method emits PHP error/warning when parameters count or types are wrong or fail to get ACL of a node.

Caution

Since version 0.3.0, this method emits <span class="classname">ZookeeperException and it's derivatives.

范例

示例 #1 Zookeeper::getAcl example

Get ACL of a node.

<?php
$zookeeper = new Zookeeper('locahost:2181');
$aclArray = array(
  array(
    'perms'  => Zookeeper::PERM_ALL,
    'scheme' => 'world',
    'id'     => 'anyone',
  )
);
$path = '/path/to/newnode';
$zookeeper->setAcl($path, $aclArray);

$r = $zookeeper->getAcl($path);
if ($r)
  var_dump($r);
else
  echo 'ERR';
?>

以上例程会输出:

array(1) {
  [0]=>
  array(3) {
    ["perms"]=>
    int(31)
    ["scheme"]=>
    string(5) "world"
    ["id"]=>
    string(6) "anyone"
  }
}

参见

Zookeeper::getChildren

Lists the children of a node synchronously

说明

public array Zookeeper::getChildren ( <span class="methodparam">string $path [, callable $watcher_cb = null ] )

参数

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

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

返回值

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

错误/异常

This method emits PHP error/warning when parameters count or types are wrong or fail to list children of a node.

Caution

Since version 0.3.0, this method emits <span class="classname">ZookeeperException and it's derivatives.

范例

示例 #1 Zookeeper::getChildren example

Lists children of a node.

<?php
$zookeeper = new Zookeeper('locahost:2181');
$path = '/zookeeper';
$r = $zookeeper->getchildren($path);
if ($r)
  var_dump($r);
else
  echo 'ERR';
?>

以上例程会输出:

array(1) {
  [0]=>
  string(6) "config"
}

参见

  • Zookeeper::create
  • Zookeeper::delete
  • ZookeeperException

Zookeeper::getClientId

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

说明

public int <span class="methodname">Zookeeper::getClientId ( <span class="methodparam">void )

返回值

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

错误/异常

This method emits PHP error/warning when it could not get client session id.

Caution

Since version 0.3.0, this method emits <span class="classname">ZookeeperException and it's derivatives.

参见

  • Zookeeper::__construct
  • Zookeeper::connect
  • Zookeeper::getState
  • ZooKeeper States
  • ZookeeperException

Zookeeper::getConfig

Get instance of ZookeeperConfig

说明

public <span class="type">ZookeeperConfig <span class="methodname">Zookeeper::getConfig ( <span class="methodparam">void )

返回值

Returns instance of ZookeeperConfig.

参见

  • ZookeeperConfig

Zookeeper::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 re-connect

说明

public int <span class="methodname">Zookeeper::getRecvTimeout ( <span class="methodparam">void )

返回值

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

错误/异常

This method emits PHP error/warning when operation fails.

Caution

Since version 0.3.0, this method emits <span class="classname">ZookeeperException and it's derivatives.

参见

  • Zookeeper::__construct
  • Zookeeper::connect
  • ZookeeperException

Zookeeper::getState

Get the state of the zookeeper connection

说明

public int <span class="methodname">Zookeeper::getState ( <span class="methodparam">void )

返回值

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

错误/异常

This method emits PHP error/warning when it fails to get state of zookeeper connection.

Caution

Since version 0.3.0, this method emits <span class="classname">ZookeeperException and it's derivatives.

参见

  • Zookeeper::__construct
  • Zookeeper::connect
  • Zookeeper::getClientId
  • ZooKeeper States
  • ZookeeperException

Zookeeper::isRecoverable

Checks if the current zookeeper connection state can be recovered

说明

public bool Zookeeper::isRecoverable ( <span class="methodparam">void )

The application must close the handle and try to reconnect.

返回值

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

错误/异常

This method emits PHP error/warning when operation fails.

Caution

Since version 0.3.0, this method emits <span class="classname">ZookeeperException and it's derivatives.

参见

  • Zookeeper::__construct
  • Zookeeper::connect
  • Zookeeper::getClientId
  • ZooKeeper States
  • ZookeeperException

Zookeeper::set

Sets the data associated with a node

说明

public bool Zookeeper::set ( <span class="methodparam">string $path , string $value [, <span class="type">int $version = -1 [, <span class="type">array &$stat = null ]] )

参数

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

value
The data to be stored in the node.

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.

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

返回值

成功时返回 true, 或者在失败时返回 false

错误/异常

This method emits PHP error/warning when parameters count or types are wrong or fail to save value to node.

Caution

Since version 0.3.0, this method emits <span class="classname">ZookeeperException and it's derivatives.

范例

示例 #1 Zookeeper::set example

Save value to node.

<?php
$zookeeper = new Zookeeper('locahost:2181');
$path = '/path/to/node';
$value = 'nodevalue';
$r = $zookeeper->set($path, $value);
if ($r)
  echo 'SUCCESS';
else
  echo 'ERR';
?>

以上例程会输出:

SUCCESS

参见

  • Zookeeper::create
  • Zookeeper::get
  • ZookeeperException

Zookeeper::setAcl

Sets the acl associated with a node synchronously

说明

public bool Zookeeper::setAcl ( <span class="methodparam">string $path , int $version , <span class="type">array $acl )

参数

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

version
The expected version of the path.

acl
The acl to be set on the path.

返回值

成功时返回 true, 或者在失败时返回 false

错误/异常

This method emits PHP error/warning when parameters count or types are wrong or fail to set ACL for a node.

Caution

Since version 0.3.0, this method emits <span class="classname">ZookeeperException and it's derivatives.

范例

示例 #1 Zookeeper::setAcl example

Set ACL for a node.

<?php
$zookeeper = new Zookeeper('locahost:2181');
$aclArray = array(
  array(
    'perms'  => Zookeeper::PERM_ALL,
    'scheme' => 'world',
    'id'     => 'anyone',
  )
);
$path = '/path/to/newnode';
$zookeeper->setAcl($path, $aclArray);

$r = $zookeeper->getAcl($path);
if ($r)
  var_dump($r);
else
  echo 'ERR';
?>

以上例程会输出:

array(1) {
  [0]=>
  array(3) {
    ["perms"]=>
    int(31)
    ["scheme"]=>
    string(5) "world"
    ["id"]=>
    string(6) "anyone"
  }
}

参见

Zookeeper::setDebugLevel

Sets the debugging level for the library

说明

public <span class="modifier">static bool <span class="methodname">Zookeeper::setDebugLevel ( <span class="methodparam">int $logLevel )

参数

logLevel
ZooKeeper log level constants.

返回值

成功时返回 true, 或者在失败时返回 false

错误/异常

This method emits PHP error/warning when parameters count or types are wrong or fail to set debug level.

Caution

Since version 0.3.0, this method emits <span class="classname">ZookeeperException and it's derivatives.

范例

示例 #1 Zookeeper::setDebugLevel example

Set debugl level.

<?php
$r = Zookeeper::setDebugLevel(Zookeeper::LOG_LEVEL_WARN);
if ($r)
  echo 'SUCCESS';
else
  echo 'ERR';
?>
?>

以上例程会输出:

SUCCESS

参见

Zookeeper::setDeterministicConnOrder

Enable/disable quorum endpoint order randomization

说明

public <span class="modifier">static bool <span class="methodname">Zookeeper::setDeterministicConnOrder ( <span class="methodparam">bool $yesOrNo )

If passed a true value, will make the client connect to quorum peers in the order as specified in the zookeeper_init() call. A false value causes zookeeper_init() to permute the peer endpoints which is good for more even client connection distribution among the quorum peers. ZooKeeper C Client uses false by default.

参数

yesOrNo
Disable/enable quorum endpoint order randomization.

返回值

成功时返回 true, 或者在失败时返回 false

错误/异常

This method emits PHP error/warning when parameters count or types are wrong or operation fails.

Caution

Since version 0.3.0, this method emits <span class="classname">ZookeeperException and it's derivatives.

参见

  • Zookeeper::__construct
  • Zookeeper::connect
  • ZookeeperException

Zookeeper::setLogStream

Sets the stream to be used by the library for logging

说明

public bool Zookeeper::setLogStream ( <span class="methodparam">resource $stream )

The zookeeper library uses stderr as its default log stream. Application must make sure the stream is writable. Passing in NULL resets the stream to its default value (stderr).

参数

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

返回值

成功时返回 true, 或者在失败时返回 false

错误/异常

This method emits PHP error/warning when parameters count or types are wrong or operation fails.

Caution

Since version 0.3.0, this method emits <span class="classname">ZookeeperException and it's derivatives.

参见

  • Zookeeper::setDebugLevel
  • ZookeeperException

Zookeeper::setWatcher

Set a watcher function

说明

public bool Zookeeper::setWatcher ( <span class="methodparam">callable $watcher_cb )

参数

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

返回值

成功时返回 true, 或者在失败时返回 false

错误/异常

This method emits PHP error/warning when parameters count or types are wrong or fail to set watcher.

Caution

Since version 0.3.0, this method emits <span class="classname">ZookeeperException and it's derivatives.

参见

  • Zookeeper::exists
  • Zookeeper::get
  • ZookeeperException

简介

The ZooKeeper Config handling class.

类摘要

ZookeeperConfig

class ZookeeperConfig {

/* 方法 */

public void add ( <span class="type">string $members [, <span class="methodparam">int $version<span class="initializer"> = -1 [, <span class="methodparam">array &$stat<span class="initializer"> = null ]] )

public string get ([ <span class="type">callable $watcher_cb = null [, <span class="type">array &$stat = null ]] )

public void remove ( <span class="type">string $id_list [, <span class="methodparam">int $version<span class="initializer"> = -1 [, <span class="methodparam">array &$stat<span class="initializer"> = null ]] )

public void set ( <span class="type">string $members [, <span class="methodparam">int $version<span class="initializer"> = -1 [, <span class="methodparam">array &$stat<span class="initializer"> = null ]] )

}

ZookeeperConfig::add

Add servers to the ensemble

说明

public void ZookeeperConfig::add ( <span class="methodparam">string $members [, int $version = -1 [, <span class="methodparam">array &$stat<span class="initializer"> = null ]] )

参数

members
Comma separated list of servers to be added to the ensemble. Each has a configuration line for a server to be added (as would appear in a configuration file), only for maj. quorums.

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.

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

错误/异常

This method emits ZookeeperException and it's derivatives when parameters count or types are wrong or fail to save value to node.

范例

示例 #1 ZookeeperConfig::add example

Add members.

<?php
$client = new Zookeeper();
$client->connect('localhost:2181');
$client->addAuth('digest', 'timandes:timandes');
$zkConfig = $client->getConfig();
$zkConfig->set("server.1=localhost:2888:3888:participant;0.0.0.0:2181");
$zkConfig->add("server.2=localhost:2889:3889:participant;0.0.0.0:2182");
$r = $zkConfig->get();
if ($r)
  echo $r;
else
  echo 'ERR';
?>

以上例程会输出:

server.1=localhost:2888:3888:participant;0.0.0.0:2181
server.2=localhost:2889:3889:participant;0.0.0.0:2182
version=0xca01e881a2

参见

  • ZookeeperConfig::get
  • ZookeeperConfig::set
  • ZookeeperConfig::remove
  • ZookeeperException

ZookeeperConfig::get

Gets the last committed configuration of the ZooKeeper cluster as it is known to the server to which the client is connected, synchronously

说明

public string ZookeeperConfig::get ([ <span class="methodparam">callable $watcher_cb = null [, array &$stat<span class="initializer"> = null ]] )

参数

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

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

返回值

Returns the configuration string on success, and false on failure.

错误/异常

This method emits ZookeeperException and it's derivatives when parameters count or types are wrong or fail to get configuration.

范例

示例 #1 ZookeeperConfig::get example

Get configuration.

<?php
$zk = new Zookeeper();
$zk->connect('localhost:2181');
$zk->addAuth('digest', 'timandes:timandes');
$zkConfig = $zk->getConfig();
$r = $zkConfig->get();
if ($r)
  echo $r;
else
  echo 'ERR';
?>

以上例程会输出:

server.1=localhost:2888:3888:participant;0.0.0.0:2181
version=0xca01e881a2

参见

  • ZookeeperConfig::set
  • ZookeeperConfig::add
  • ZookeeperConfig::remove
  • ZookeeperException

ZookeeperConfig::remove

Remove servers from the ensemble

说明

public void ZookeeperConfig::remove ( <span class="methodparam">string $id_list [, int $version = -1 [, <span class="methodparam">array &$stat<span class="initializer"> = null ]] )

参数

id_list
Comma separated list of server IDs to be removed from the ensemble. Each has an id of a server to be removed, only for maj. quorums.

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.

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

错误/异常

This method emits ZookeeperException and it's derivatives when parameters count or types are wrong or fail to save value to node.

范例

示例 #1 ZookeeperConfig::remove example

Remove members.

<?php
$client = new Zookeeper();
$client->connect('localhost:2181');
$client->addAuth('digest', 'timandes:timandes');
$zkConfig = $client->getConfig();
$zkConfig->set("server.1=localhost:2888:3888:participant;0.0.0.0:2181,server.2=localhost:2889:3889:participant;0.0.0.0:2182");
$zkConfig->remove("2");
echo $zkConfig->get();
if ($r)
  echo $r;
else
  echo 'ERR';
?>

以上例程会输出:

server.1=localhost:2888:3888:participant;0.0.0.0:2181
version=0xca01e881a2

参见

  • ZookeeperConfig::get
  • ZookeeperConfig::add
  • ZookeeperConfig::set
  • ZookeeperException

ZookeeperConfig::set

Change ZK cluster ensemble membership and roles of ensemble peers

说明

public void ZookeeperConfig::set ( <span class="methodparam">string $members [, int $version = -1 [, <span class="methodparam">array &$stat<span class="initializer"> = null ]] )

参数

members
Comma separated list of new membership (e.g., contents of a membership configuration file) - for use only with a non-incremental reconfiguration.

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.

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

错误/异常

This method emits ZookeeperException and it's derivatives when parameters count or types are wrong or fail to save value to node.

范例

示例 #1 ZookeeperConfig::set example

Reconfig.

<?php
$client = new Zookeeper();
$client->connect('localhost:2181');
$client->addAuth('digest', 'timandes:timandes');
$zkConfig = $client->getConfig();
$zkConfig->set("server.1=localhost:2888:3888:participant;0.0.0.0:2181");
?>

参见

  • ZookeeperConfig::get
  • ZookeeperConfig::add
  • ZookeeperConfig::remove
  • ZookeeperException

简介

The ZooKeeper exception handling class.

类摘要

ZookeeperException

class ZookeeperException <span class="ooclass"> extends Exception {

/* 继承的属性 */

protected string $message ;

protected int $code ;

protected string $file ;

protected int $line ;

/* 继承的方法 */

final public string <span class="methodname">Exception::getMessage ( <span class="methodparam">void )

final public Throwable <span class="methodname">Exception::getPrevious ( <span class="methodparam">void )

final public mixed <span class="methodname">Exception::getCode ( <span class="methodparam">void )

final public string <span class="methodname">Exception::getFile ( <span class="methodparam">void )

final public int <span class="methodname">Exception::getLine ( <span class="methodparam">void )

final public array <span class="methodname">Exception::getTrace ( <span class="methodparam">void )

final public string <span class="methodname">Exception::getTraceAsString ( <span class="methodparam">void )

public string Exception::__toString ( <span class="methodparam">void )

final <span class="modifier">private void <span class="methodname">Exception::__clone ( <span class="methodparam">void )

}

简介

The ZooKeeper authentication exception handling class.

类摘要

ZookeeperAuthenticationException

class ZookeeperAuthenticationException extends ZookeeperException {

/* 继承的属性 */

protected string $message ;

protected int $code ;

protected string $file ;

protected int $line ;

/* 继承的方法 */

final public string <span class="methodname">Exception::getMessage ( <span class="methodparam">void )

final public Throwable <span class="methodname">Exception::getPrevious ( <span class="methodparam">void )

final public mixed <span class="methodname">Exception::getCode ( <span class="methodparam">void )

final public string <span class="methodname">Exception::getFile ( <span class="methodparam">void )

final public int <span class="methodname">Exception::getLine ( <span class="methodparam">void )

final public array <span class="methodname">Exception::getTrace ( <span class="methodparam">void )

final public string <span class="methodname">Exception::getTraceAsString ( <span class="methodparam">void )

public string Exception::__toString ( <span class="methodparam">void )

final <span class="modifier">private void <span class="methodname">Exception::__clone ( <span class="methodparam">void )

}

简介

The ZooKeeper connection exception handling class.

类摘要

ZookeeperConnectionException

class ZookeeperConnectionException extends ZookeeperException {

/* 继承的属性 */

protected string $message ;

protected int $code ;

protected string $file ;

protected int $line ;

/* 继承的方法 */

final public string <span class="methodname">Exception::getMessage ( <span class="methodparam">void )

final public Throwable <span class="methodname">Exception::getPrevious ( <span class="methodparam">void )

final public mixed <span class="methodname">Exception::getCode ( <span class="methodparam">void )

final public string <span class="methodname">Exception::getFile ( <span class="methodparam">void )

final public int <span class="methodname">Exception::getLine ( <span class="methodparam">void )

final public array <span class="methodname">Exception::getTrace ( <span class="methodparam">void )

final public string <span class="methodname">Exception::getTraceAsString ( <span class="methodparam">void )

public string Exception::__toString ( <span class="methodparam">void )

final <span class="modifier">private void <span class="methodname">Exception::__clone ( <span class="methodparam">void )

}

简介

The ZooKeeper exception (while marshalling or unmarshalling data) handling class.

类摘要

ZookeeperMarshallingException

class ZookeeperMarshallingException extends ZookeeperException {

/* 继承的属性 */

protected string $message ;

protected int $code ;

protected string $file ;

protected int $line ;

/* 继承的方法 */

final public string <span class="methodname">Exception::getMessage ( <span class="methodparam">void )

final public Throwable <span class="methodname">Exception::getPrevious ( <span class="methodparam">void )

final public mixed <span class="methodname">Exception::getCode ( <span class="methodparam">void )

final public string <span class="methodname">Exception::getFile ( <span class="methodparam">void )

final public int <span class="methodname">Exception::getLine ( <span class="methodparam">void )

final public array <span class="methodname">Exception::getTrace ( <span class="methodparam">void )

final public string <span class="methodname">Exception::getTraceAsString ( <span class="methodparam">void )

public string Exception::__toString ( <span class="methodparam">void )

final <span class="modifier">private void <span class="methodname">Exception::__clone ( <span class="methodparam">void )

}

简介

The ZooKeeper exception (when node does not exist) handling class.

类摘要

ZookeeperNoNodeException

class ZookeeperNoNodeException <span class="ooclass"> extends ZookeeperException {

/* 继承的属性 */

protected string $message ;

protected int $code ;

protected string $file ;

protected int $line ;

/* 继承的方法 */

final public string <span class="methodname">Exception::getMessage ( <span class="methodparam">void )

final public Throwable <span class="methodname">Exception::getPrevious ( <span class="methodparam">void )

final public mixed <span class="methodname">Exception::getCode ( <span class="methodparam">void )

final public string <span class="methodname">Exception::getFile ( <span class="methodparam">void )

final public int <span class="methodname">Exception::getLine ( <span class="methodparam">void )

final public array <span class="methodname">Exception::getTrace ( <span class="methodparam">void )

final public string <span class="methodname">Exception::getTraceAsString ( <span class="methodparam">void )

public string Exception::__toString ( <span class="methodparam">void )

final <span class="modifier">private void <span class="methodname">Exception::__clone ( <span class="methodparam">void )

}

简介

The ZooKeeper operation timeout exception handling class.

类摘要

ZookeeperOperationTimeoutException

class ZookeeperOperationTimeoutException extends ZookeeperException {

/* 继承的属性 */

protected string $message ;

protected int $code ;

protected string $file ;

protected int $line ;

/* 继承的方法 */

final public string <span class="methodname">Exception::getMessage ( <span class="methodparam">void )

final public Throwable <span class="methodname">Exception::getPrevious ( <span class="methodparam">void )

final public mixed <span class="methodname">Exception::getCode ( <span class="methodparam">void )

final public string <span class="methodname">Exception::getFile ( <span class="methodparam">void )

final public int <span class="methodname">Exception::getLine ( <span class="methodparam">void )

final public array <span class="methodname">Exception::getTrace ( <span class="methodparam">void )

final public string <span class="methodname">Exception::getTraceAsString ( <span class="methodparam">void )

public string Exception::__toString ( <span class="methodparam">void )

final <span class="modifier">private void <span class="methodname">Exception::__clone ( <span class="methodparam">void )

}

简介

The ZooKeeper session exception handling class.

类摘要

ZookeeperSessionException

class ZookeeperSessionException <span class="ooclass"> extends ZookeeperException {

/* 继承的属性 */

protected string $message ;

protected int $code ;

protected string $file ;

protected int $line ;

/* 继承的方法 */

final public string <span class="methodname">Exception::getMessage ( <span class="methodparam">void )

final public Throwable <span class="methodname">Exception::getPrevious ( <span class="methodparam">void )

final public mixed <span class="methodname">Exception::getCode ( <span class="methodparam">void )

final public string <span class="methodname">Exception::getFile ( <span class="methodparam">void )

final public int <span class="methodname">Exception::getLine ( <span class="methodparam">void )

final public array <span class="methodname">Exception::getTrace ( <span class="methodparam">void )

final public string <span class="methodname">Exception::getTraceAsString ( <span class="methodparam">void )

public string Exception::__toString ( <span class="methodparam">void )

final <span class="modifier">private void <span class="methodname">Exception::__clone ( <span class="methodparam">void )

}


本站为非盈利网站,作品由网友提供上传,如无意中有侵犯您的版权,请联系删除