ZooKeeper - Perl bindings for Apache ZooKeeper Perl绑定用于 Apache ZooKeeper

ZooKeeper - Perl bindings for Apache ZooKeeper  Perl绑定用于 Apache ZooKeeper

监控 master/slave 需要使用zk的临时节点

 1072  ./configure --libdir=/usr/lib
 1073  make
 1074  make install
 1075  cpan ZooKeeper
 [root@wx03 c]# perldoc ZooKeeper
 
 my $zk = ZooKeeper->new(hosts => 'localhost:2181');
 
my $cv = AE::cv;
my @children = $zk->get_children('/', watcher => sub { my $event = shift; $cv->send($event) });
my $child_event = $cv->recv;

状态:

不稳定

知道版本1.0.0,一些 API方面的改动,最多和异常处理相关用于命令和watchers.


描述:

ZooKeeper  是一个perl 接口到 Apache ZooKeeper C client library.



与Net::ZooKeeper的不同?


ZooKeeper 是编写用于异步程序



支持异步程序,watchers是作为函数引用实现, 一个ZooKeeper::Dispatcher 异步调用

ZooKeeper 事件数据。

相反,Net::ZooKeeper 被用于Net::ZooKeeper::Watch classes, 用户必须交互的使用wait方法。


ZooKeeper  数据是表现为普通的perl 数据类型


ZooKeeper 事件和状态数据 是简单的hash引用和数据引用

Net::ZooKeeper 代替提供特定的perl类用于交互这个数据


ZooKeeper 利用perl异常处理;


代替 返回的C错误代码, 因为Net::ZooKeeper does, ZooKeeper  抛出一个 ZooKeeper::Error 异常对于不期望的返回代码


Data Types  数据类型:


ACL 是表示一个hash引用的数组引用,每个hash引用包含一个Id,scheme,和权限。

权限标志可以从ZooKeeper::Constants package.导入


例子,ZOO_READ_ACL_UNSAFE  将表示为;

    [{id => 'anyone', scheme => 'world', perms => ZOO_PERM_READ}]


event:


一个属性的hash引用用于一个watcher event,包含event的类型(a ZooKeeper::Constants event), 

连接状态(a ZooKeeper::Constants state) 和节点触发事件的路径


    {
        path  => '/child',
        state => ZOO_CONNECTED_STATE,
        type  => ZOO_CHILD_EVENT,
    }

stat
A hashref of fields from a C Stat struct.

    {
        aversion       => 0,
        ctime          => 0,
        cversion       => 0,
        czxid          => 0,
        ephemeralOwner => 0,
        dataLength     => 0,
        mtime          => 0,
        mzxid          => 0,
        numChildren    => 2,
        pzxid          => 2334,
        version        => 0,
    }


Dispatchers 调度器:


ZooKeeper  使用ZooKeeper::Dispatchers 对于和用C库注册的回调通信。

那些回调 是被执行在单独的POSIX  threads,


写event data 到一个 ZooKeeper::Channel ,通知 dispatcher  一个event 是准备被处理。

这个通过如何发生,perl 回调如何发生,是调度器乐行的不同


AnyEvent


ZooKeeper 写Unix 管道 使用一个AnyEvent I/O watcher. T

这意味着perl 回调对于watcheres 会通过AnyEvent event loop. 执行


Interrupt 中断:

ZooKeeper  使用 Async::Interrupt  回调,这意味着 perl 解释器会安全的中断(等待操作完成)


为了执行这个响应的perl回调。


查看Async::Interrupt  关于回调如何被执行。


要知道这个不会中断系统调用(比如select) 和XScode.


这意味着你的代码会被租的在select操作(比如在 AnyEvent recv),

中断的回调不会执行直到请求完成

IOAsync


ZooKeeper 写一个Unix管道使用 IO::Async::Handle.


 IO::Async  调度器需要一个n IO::Async::Loop,  需要手动构建

    my $loop = IO::Async::Loop->new;
    my $disp = ZooKeeper::Dispatcher::IOAsync->new(loop => $loop);
    my $zk = ZooKeeper->new(
        hosts      => 'localhost:2181',
        dispatcher => $disp,
    );



ATTRIBUTES   属性:

一个逗号分隔的列表 

    'localhost:2181'
    'zoo1.domain:2181,zoo2.domain:2181'


timeout  会话超时用于ZooKeeper connection.


watcher:

一个子程序引用被调用通过默认的watcher 对于ZooKeeper 会话事件,

这个属性是读写的:


authentication


一个数组引用 用于ZooKeeper 认证,这个会传递一个数组到add_auth


  [$scheme, $credentials, %extra]

buffer_length


默认的buffer 长度用于检索ZooKeeper data and paths 默认是2048



METHODS  方法:

new 实现一个新的 ZooKeeper connection.


    my $zk = ZooKeeper->new(%args)

        %args
            REQUIRED hosts
            OPTIONAL authentication
            OPTIONAL buffer_length
            OPTIONAL dispatcher
            OPTIONAL timeout
            OPTIONAL watcher


state:


得到ZooKeeper connection的状态,返回一个状态枚举 


wait:

    调用等待依赖 ZooKeeper::Dispatcher.


同步调用一个事件,返回 事件hash引用 watcher 被调用。

可以传递一个timeout(单位秒),会导致等待 undef 如果没有在指定时间完成


   my $event = $zk->wait($seconds)

    OPTIONAL $seconds


close:


关闭一个 ZooKeeper session.

如果 handle不是通过当前的session创建的,一个 ZOO_CLOSE_OP

不会发送给server,相反,底层的socket会关闭


reopen:

重新打开一个 ZooKeeper session  在forking后,

这个创建一个新的 ZooKeeper session, 不会关闭当前的会话


create:


创建一个新的node 使用给定的path和数据,返回 新创建的节点的path,否则抛出错误


   my $created_path = $zk->create($requested_path, %extra);

        REQUIRED $requested_path

        OPTIONAL %extra
            acl
            buffer_length
            ephemeral
            sequential
            value


add_auth:


添加认证对于session,会自动被调用如果认证属性在构造期间被设置


一个 ZooKeeper::Error 会抛出如果请求没有成功, 决定成功或者失败认证,一个watcher 必须被传入


 $zk->add_auth($scheme, $credentials, %extra)

        REQUIRED $scheme
        REQUIRED $credentials

        OPTIONAL %extra
            watcher

delete:



Delete 一个node 在给定的path,Throws a ZooKeeper::Error  如果delete删除失败

   $zk->delete($path, %extra)

        REQUIRED $path

        OPTIONAL %extra
            version


ensure_path

exists

检查是否一个节点存在于给定的path, 并可选的设置一个watcher 当节点被创建和删除,


如果成功, 返回一个节点的状态的hash引用,否则返回Undef:


    my $stat = $zk->exists($path, %extra)

        REQUIRED $path

        OPTIONAL %extra
            watcher


get_children:



得到child 节点在规定的path下, 可选的设置一个watcher 当一个child 被创建或者删除,返回一个child path名字的数组


  my @child_paths = $zk->get_children($path, %extra)

        REQUIRED $path

        OPTIONAL %extra
            watcher


get:


设置数据在给定的path,如果成功,返回一个节点状态hashref,否则一个ZooKeeper::Error is thrown.


   my $stat = $zk->set($path => $value, %extra)

        REQUIRED $path
        REQUIRED $value

        OPTIONAL %extra
            version

posted @ 2016-07-19 09:38  czcb  阅读(116)  评论(0编辑  收藏  举报