PrePAN

Sign in to PrePAN

Net::ZooIt High level recipes for Apache ZooKeeper

Good

Synopsis

use Net::ZooKeeper;
use Net::ZooIt;
    
Net::ZooIt::set_log_level(ZOOIT_DEBUG);
    
my $zk = Net::ZooKeeper->new('localhost:7000');
while (1) {
    my $lock = Net::ZooIt->new_lock(zk => $zk, path => '/election');
    last unless $lock;
    do_stuff_when_elected();
}

Description

DESCRIPTION

Net::ZooIt provides high level recipes for working with ZooKeeper in Perl, like locks, leader election or queues.

Net::ZooKeeper Handles

Net::ZooIt methods always take a Net::ZooKeeper handle object as a parameter and delegate their creation to the user. Rationale: enterprises often have customised ways to create those handles, Net::ZooIt aims to be instantly usable without such customisation.

Automatic Cleanup

Net::ZooIt constructors return a Net::ZooIt object, which automatically clean up their znodes when they go out of scope at the end of the enclosing block. If you want to clean up earlier, call

  $zooit_obj->DESTROY;

Implication: if you call Net::ZooIt constructors in void context, the created object goes out of scope immediately, and your znodes are deleted. Net::ZooIt logs a ZOOIT_ERR message in this case.

Error Handling

Net::ZooIt constructors return nothing in case of errors during creation.

Once you hold a lock or other resource, you're not notified of connection loss errors. If you need to take special action, check your Net::ZooKeeper handle.

If you give up Net::ZooIt resources during connection loss, your znodes cannot be cleaned up immediately, they will enter a garbage collection queue and Net::ZooIt will clean them up once connection is resumed.

Logging

Net::ZooIt logs to STDERR. Log messages are prefixed with Zulu military time, PID and the level of the current message: ZOOIT_DIE ZOOIT_ERR ZOOIT_WARN ZOOIT_INFO ZOOIT_DEBUG.

If Net::ZooIt throws an exception, it prints a ZOOIT_DIE level message before dying. This allows seeing the original error message even if an eval {} block swallows it.

To capture Net::ZooIt log messages to a file instead of STDERR, redirect STDERR to a new file handle in the normal Perl manner:

open(OLDERR, '>&', fileno(STDERR)) or die("unable to dup STDERR: $!");
open(STDERR, '>', $log_file) or die("unable to redirect STDERR: $!");

Comments

Very helpful Perl Subredditors reviewed the code and helped me prepare it for CPAN. Wall Street is still full of Perl, and they need such a library, as Python already has Kazoo and Java has Curator.

About the name: I tried to avoid making an impression that this is distributed along with Net::ZooKeeper.
If you're going to log, it might be a good idea to use Log::Any instead. Then the consumer can use whatever logging system they use, you don't have to worry about adding the timestamp in whatever format (and the other stuff), and you get log levels as they are. Log::Any is specifically designed to minimize overhead, and that redirection thing you've got as a suggestion to put logging somewhere else can end up catastrophically.
The reason I do it this way is that Net::ZooKeeper does the same.

Please sign up to post a review.