Ubic-Service-ZooKeeper

 view release on metacpan or  search on metacpan

lib/Ubic/Service/ZooKeeper.pm  view on Meta::CPAN

use File::Copy qw(move);
use File::Spec::Functions qw(catfile);
use IO::Socket::INET;
use Params::Validate qw(:all);
use Storable qw(dclone);
use Time::HiRes qw();
use Ubic::Daemon qw(:all);
use Ubic::Result qw(:all);


sub new {
    my $class = shift;

    my $opt_num = { type => SCALAR, regex => qr/^\d+$/o, optional => 1 };
    my $opt_str = { type => SCALAR, optional => 1 };

    my $params = validate(@_, {
        config => { type => HASHREF, default => {} },
        myid => { %$opt_num, default => 1 },

        # ubic specific options
        status        => { type => CODEREF, optional => 1 },
        user          => $opt_str,
        ubic_log      => $opt_str,
        stdout        => $opt_str,
        stderr        => $opt_str,
        pidfile       => $opt_str,
        port          => $opt_num,

        gen_cfg        => $opt_str,
        java           => { %$opt_str, default => 'java' },
        java_cp        => { %$opt_str, default => '' },
        jmx_enable     => { type => BOOLEAN, default => 1 },
        jmx_local_only => { type => BOOLEAN, default => 0 },
        zoo_log_dir    => { %$opt_str, default => '/var/log/zookeeper' },
        zoo_log4j_prop => { %$opt_str, default => 'INFO,ROLLINGFILE' },
        zoo_main_class => {
            %$opt_str,
            default => 'org.apache.zookeeper.server.quorum.QuorumPeerMain'
        },
        java_opts      => { type => SCALAR, default => '' },
    });

    my $config = $params->{config};
    %$config = (
        clientPort => 2181,
        dataDir    => '/var/lib/zookeeper',
        tickTime   => 2000,
        %$config,
    );

    my $clientPort = $config->{clientPort};

    if (!$params->{pidfile}) {
        $params->{pidfile} = catfile('/tmp', 'zookeeper.' . $clientPort . '.pid');
    }
    if (!$params->{gen_cfg}) {
        $params->{gen_cfg} = catfile('/tmp', 'zoo.' . $clientPort . '.cfg');
    }

    return bless $params => $class;
}


sub bin {
    my $self = shift;

    my $cmd = '';
    $cmd = $self->{java} . " " . $self->{java_opts} . " " .
           "-cp " . $self->{java_cp} . " ";
    if ($self->{jmx_enable}) {
        $cmd .= "-Dcom.sun.management.jmxremote ";
        unless ($self->{jmx_local_only}) {
            $cmd .= "-Dcom.sun.management.jmxremote.local.only=false ";
        }
    }
    $cmd .= "-Dzookeeper.log.dir=$self->{zoo_log_dir} ";
    $cmd .= "-Dzookeeper.root.logger=$self->{zoo_log4j_prop} ";
    $cmd .= $self->{zoo_main_class} . " ";
    $cmd .= $self->gen_cfg;

    return [ $cmd ];
}


sub create_cfg_file {
    my $self = shift;

    my $fname = $self->gen_cfg;
    my $tmp_fname = $fname . ".tmp";


    my $params = dclone($self->{config});
    my $servers = delete $params->{servers};
    my $groups = delete $params->{groups};

    open(my $tmp_fh, '>', $tmp_fname) or die "Can't open file [$tmp_fname]: $!";

    foreach my $p (sort keys %$params) {
        my $v = $params->{$p};
        print $tmp_fh "$p=$v\n";
    }
    print $tmp_fh "\n";

    foreach my $server_num (sort {$a <=> $b} keys %$servers) {
        my $s = $servers->{$server_num};
        my $server = $s->{server};
        print $tmp_fh "server.${server_num}=$server\n";

        if ($s->{weight}) {
            print $tmp_fh "weight.${server_num}=$s->{weight}\n";
        }
    }
    print $tmp_fh "\n";

    foreach my $group_num (sort {$a <=> $b} keys %$groups) {
        my $group_servers = $groups->{$group_num};
        print $tmp_fh "group.${group_num}=" . join(":", @$group_servers) . "\n";
    }

    close($tmp_fh) or die "Can't close file [$tmp_fname]: $!";

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.434 second using v1.00-cache-2.02-grep-82fe00e-cpan-cec75d87357c )