naiks has asked for the wisdom of the Perl Monks concerning the following question:
Hi All,
For my project I need to interact with Apache Zookeeper using Perl. For this I am using the Perl module
Net::ZooKeeperUsing this I am able to successfully crate the zk nodes but while triggering the watcher on that node the program is not working as I expected.
use Net::ZooKeeper qw(:node_flags :acls); my $zkh = Net::ZooKeeper->new('localhost:7000'); $zkh->create('/myzknode, 'active', 'flags' => ZOO_EPHEMERAL, 'acl' => +ZOO_OPEN_ACL_UNSAFE) or die("unable to create node /myzknode: " . $zk +h->get_error() . "\n"); print "node /myzknode has value: " . $zkh->get('/myzknode') . "\n";
Using the above code I am able to create a zookeeper node with the name ‘myzknode’ by assigning the value as ‘active’
After successful creation of zookeeper node from one instance of program, then the another instance of same program will run on another machine and try to create the same zookeeper node and fails, since the first instance already created the node with the same node.
Then the second instance should trigger one time watcher on that node, so if something happens to that node immediately zookeeper will generate an event to the program which triggered the watcher and then the watcher program will create the zookeeper node and star working further.
Here after creating the zookeeper node this I want to trigger a watcher on this node and catch the events like node deletion can be detected in my program. If anyone already worked on such thing please let me know.
I have used the following code to create a watcher, but its not working this watcher is coming our after some time and I am expecting the watcher to be keep watch on the zookeeper and catch the event happened on the node the event like 'NODE_DELETION'
my $watch = $zkh->watch('timeout' => 10000); $zkh->exists('/myzknode', 'watch' => $watch); if ($watch->wait()) { print "watch triggered on node /myzknode:\n"; print " event: $watch->{event}\n"; print " state: $watch->{state}\n"; } else { print "watch timed out after 10 seconds\n"; }
In this case the watcher code is coming to else loop and exiting from the program.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl interaction with apache Zookeeper
by kcott (Archbishop) on Aug 11, 2015 at 06:58 UTC | |
by naiks (Initiate) on Aug 11, 2015 at 09:15 UTC |