| 1 | package Ubic::Service::ZooKeeper; | ||||||||||
| 2 | 1 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | $Ubic::Service::ZooKeeper::VERSION = '0.02'; | |||||||||
| 4 | } | ||||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | # ABSTRACT: running ZooKeeper as Ubic service | ||||||||||
| 10 | |||||||||||
| 11 | |||||||||||
| 12 | 1 | ▶ | use parent qw(Ubic::Service::Common); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 1 | ▶ | use File::Copy qw(move); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 1 | ▶ | use File::Spec::Functions qw(catfile); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 1 | ▶ | use IO::Socket::INET; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 1 | ▶ | use Params::Validate qw(:all); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 1 | ▶ | use Storable qw(dclone); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 1 | ▶ | use Time::HiRes qw(); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 1 | ▶ | use Ubic::Daemon qw(:all); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 1 | ▶ | use Ubic::Result qw(:all); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | |||||||||||
| 23 | |||||||||||
| 24 | 4 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 25 | 4 | my $class = shift; | |||||||||
| 26 | |||||||||||
| 27 | 4 | my $opt_num = { type => SCALAR, regex => qr/^\d+$/o, optional => 1 }; | |||||||||
| 28 | 4 | my $opt_str = { type => SCALAR, optional => 1 }; | |||||||||
| 29 | |||||||||||
| 30 | 4 | my $params = validate(@_, { | |||||||||
| 31 | config => { type => HASHREF, default => {} }, | ||||||||||
| 32 | myid => { %$opt_num, default => 1 }, | ||||||||||
| 33 | |||||||||||
| 34 | # ubic specific options | ||||||||||
| 35 | status => { type => CODEREF, optional => 1 }, | ||||||||||
| 36 | user => $opt_str, | ||||||||||
| 37 | ubic_log => $opt_str, | ||||||||||
| 38 | stdout => $opt_str, | ||||||||||
| 39 | stderr => $opt_str, | ||||||||||
| 40 | pidfile => $opt_str, | ||||||||||
| 41 | port => $opt_num, | ||||||||||
| 42 | |||||||||||
| 43 | gen_cfg => $opt_str, | ||||||||||
| 44 | java => { %$opt_str, default => 'java' }, | ||||||||||
| 45 | java_cp => { %$opt_str, default => '' }, | ||||||||||
| 46 | jmx_enable => { type => BOOLEAN, default => 1 }, | ||||||||||
| 47 | jmx_local_only => { type => BOOLEAN, default => 0 }, | ||||||||||
| 48 | zoo_log_dir => { %$opt_str, default => '/var/log/zookeeper' }, | ||||||||||
| 49 | zoo_log4j_prop => { %$opt_str, default => 'INFO,ROLLINGFILE' }, | ||||||||||
| 50 | zoo_main_class => { | ||||||||||
| 51 | %$opt_str, | ||||||||||
| 52 | default => 'org.apache.zookeeper.server.quorum.QuorumPeerMain' | ||||||||||
| 53 | }, | ||||||||||
| 54 | java_opts => { type => SCALAR, default => '' }, | ||||||||||
| 55 | }); | ||||||||||
| 56 | |||||||||||
| 57 | 4 | my $config = $params->{config}; | |||||||||
| 58 | 4 | %$config = ( | |||||||||
| 59 | clientPort => 2181, | ||||||||||
| 60 | dataDir => '/var/lib/zookeeper', | ||||||||||
| 61 | tickTime => 2000, | ||||||||||
| 62 | %$config, | ||||||||||
| 63 | ); | ||||||||||
| 64 | |||||||||||
| 65 | 4 | my $clientPort = $config->{clientPort}; | |||||||||
| 66 | |||||||||||
| 67 | 4 | ▶ | if (!$params->{pidfile}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 4 | $params->{pidfile} = catfile('/tmp', 'zookeeper.' . $clientPort . '.pid'); | |||||||||
| 69 | } | ||||||||||
| 70 | 4 | ▶ | if (!$params->{gen_cfg}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 2 | $params->{gen_cfg} = catfile('/tmp', 'zoo.' . $clientPort . '.cfg'); | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | 4 | return bless $params => $class; | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | |||||||||||
| 78 | 1 | ▶ | sub bin { | ||||||||
|
Subroutine
bin: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 79 | 1 | my $self = shift; | |||||||||
| 80 | |||||||||||
| 81 | 1 | my $cmd = ''; | |||||||||
| 82 | $cmd = $self->{java} . " " . $self->{java_opts} . " " . | ||||||||||
| 83 | 1 | "-cp " . $self->{java_cp} . " "; | |||||||||
| 84 | 1 | ▶ | if ($self->{jmx_enable}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 1 | $cmd .= "-Dcom.sun.management.jmxremote "; | |||||||||
| 86 | 1 | ▶ | unless ($self->{jmx_local_only}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 1 | $cmd .= "-Dcom.sun.management.jmxremote.local.only=false "; | |||||||||
| 88 | } | ||||||||||
| 89 | } | ||||||||||
| 90 | 1 | $cmd .= "-Dzookeeper.log.dir=$self->{zoo_log_dir} "; | |||||||||
| 91 | 1 | $cmd .= "-Dzookeeper.root.logger=$self->{zoo_log4j_prop} "; | |||||||||
| 92 | 1 | $cmd .= $self->{zoo_main_class} . " "; | |||||||||
| 93 | 1 | $cmd .= $self->gen_cfg; | |||||||||
| 94 | |||||||||||
| 95 | 1 | return [ $cmd ]; | |||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | |||||||||||
| 99 | 1 | ▶ | sub create_cfg_file { | ||||||||
|
Subroutine
create_cfg_file: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||
| 100 | 1 | my $self = shift; | |||||||||
| 101 | |||||||||||
| 102 | 1 | my $fname = $self->gen_cfg; | |||||||||
| 103 | 1 | my $tmp_fname = $fname . ".tmp"; | |||||||||
| 104 | |||||||||||
| 105 | |||||||||||
| 106 | 1 | my $params = dclone($self->{config}); | |||||||||
| 107 | 1 | my $servers = delete $params->{servers}; | |||||||||
| 108 | 1 | my $groups = delete $params->{groups}; | |||||||||
| 109 | |||||||||||
| 110 | 1 | ▶ | open(my $tmp_fh, '>', $tmp_fname) or die "Can't open file [$tmp_fname]: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | |||||||||||
| 112 | 1 | foreach my $p (sort keys %$params) { | |||||||||
| 113 | 19 | my $v = $params->{$p}; | |||||||||
| 114 | 19 | print $tmp_fh "$p=$v\n"; | |||||||||
| 115 | } | ||||||||||
| 116 | 1 | print $tmp_fh "\n"; | |||||||||
| 117 | |||||||||||
| 118 | 1 | foreach my $server_num (sort {$a <=> $b} keys %$servers) { | |||||||||
| 119 | 9 | my $s = $servers->{$server_num}; | |||||||||
| 120 | 9 | my $server = $s->{server}; | |||||||||
| 121 | 9 | print $tmp_fh "server.${server_num}=$server\n"; | |||||||||
| 122 | |||||||||||
| 123 | 9 | ▶ | if ($s->{weight}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 124 | 9 | print $tmp_fh "weight.${server_num}=$s->{weight}\n"; | |||||||||
| 125 | } | ||||||||||
| 126 | } | ||||||||||
| 127 | 1 | print $tmp_fh "\n"; | |||||||||
| 128 | |||||||||||
| 129 | 1 | foreach my $group_num (sort {$a <=> $b} keys %$groups) { | |||||||||
| 130 | 3 | my $group_servers = $groups->{$group_num}; | |||||||||
| 131 | 3 | print $tmp_fh "group.${group_num}=" . join(":", @$group_servers) . "\n"; | |||||||||
| 132 | } | ||||||||||
| 133 | |||||||||||
| 134 | 1 | ▶ | close($tmp_fh) or die "Can't close file [$tmp_fname]: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | 1 | ▶ | move($tmp_fname, $fname) or die "Can't move file [${tmp_fname}] to [$fname]: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | } | ||||||||||
| 137 | |||||||||||
| 138 | |||||||||||
| 139 | |||||||||||
| 140 | 1 | ▶ | sub create_myid_file { | ||||||||
|
Subroutine
create_myid_file: called CC=4 SCAR=14.3
Pod
documented
| |||||||||||
| 141 | 1 | my $self = shift; | |||||||||
| 142 | |||||||||||
| 143 | 1 | my $fname = catfile($self->{config}->{dataDir}, 'myid'); | |||||||||
| 144 | 1 | my $tmp_fname = $fname . ".tmp"; | |||||||||
| 145 | |||||||||||
| 146 | 1 | ▶ | open(my $tmp_fh, '>', $tmp_fname) or die "Can't open file [$tmp_fname]: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 147 | 1 | print $tmp_fh $self->{myid}, "\n"; | |||||||||
| 148 | 1 | ▶ | close($tmp_fh) or die "Can't close file [$tmp_fname]: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 149 | 1 | ▶ | move($tmp_fname, $fname) or die "Can't move file [${tmp_fname} to [$fname]: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 150 | } | ||||||||||
| 151 | |||||||||||
| 152 | 0 | ▶ | sub start_impl { | ||||||||
|
Subroutine
start_impl: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 153 | 0 | my $self = shift; | |||||||||
| 154 | |||||||||||
| 155 | 0 | $self->create_cfg_file; | |||||||||
| 156 | 0 | $self->create_myid_file; | |||||||||
| 157 | |||||||||||
| 158 | 0 | my $daemon_opts = { bin => $self->bin, pidfile => $self->pidfile, term_timeout => 5 }; | |||||||||
| 159 | 0 | for (qw/ubic_log stdout stderr/) { | |||||||||
| 160 | 0 | ▶ | $daemon_opts->{$_} = $self->{$_} if defined $self->{$_}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 161 | } | ||||||||||
| 162 | 0 | start_daemon($daemon_opts); | |||||||||
| 163 | |||||||||||
| 164 | 0 | return; | |||||||||
| 165 | } | ||||||||||
| 166 | |||||||||||
| 167 | 0 | ▶ | sub stop_impl { | ||||||||
|
Subroutine
stop_impl: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 168 | 0 | my $self = shift; | |||||||||
| 169 | |||||||||||
| 170 | 0 | return stop_daemon($self->pidfile, { timeout => 7 }); | |||||||||
| 171 | } | ||||||||||
| 172 | |||||||||||
| 173 | 0 | ▶ | sub status_impl { | ||||||||
|
Subroutine
status_impl: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||
| 174 | 0 | my $self = shift; | |||||||||
| 175 | |||||||||||
| 176 | 0 | my $running = check_daemon($self->pidfile); | |||||||||
| 177 | 0 | ▶ | return result('not running') unless ($running); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 178 | |||||||||||
| 179 | 0 | ▶ | if ($self->{status}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 180 | 0 | return $self->{status}->($self); | |||||||||
| 181 | } | ||||||||||
| 182 | |||||||||||
| 183 | 0 | my $sock = IO::Socket::INET->new( | |||||||||
| 184 | PeerAddr => "localhost", | ||||||||||
| 185 | PeerPort => $self->port, | ||||||||||
| 186 | Proto => "tcp", | ||||||||||
| 187 | Timeout => 1, | ||||||||||
| 188 | Blocking => 0, | ||||||||||
| 189 | ); | ||||||||||
| 190 | 0 | ▶ | return result('broken') unless ($sock); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 191 | |||||||||||
| 192 | 0 | $sock->print('ruok'); | |||||||||
| 193 | 0 | my $resp = ''; | |||||||||
| 194 | 0 | for (1..10) { | |||||||||
| 195 | 0 | my $buff; | |||||||||
| 196 | 0 | $sock->sysread($buff, 4); | |||||||||
| 197 | 0 | ▶ | $resp .= $buff if defined($buff); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 198 | 0 | ▶ | last if (length($resp) >= 4); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 199 | 0 | Time::HiRes::sleep(0.1); | |||||||||
| 200 | } | ||||||||||
| 201 | |||||||||||
| 202 | 0 | ▶ | if ($resp eq 'imok') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 203 | 0 | return result('running'); | |||||||||
| 204 | } else { | ||||||||||
| 205 | 0 | return result('broken'); | |||||||||
| 206 | } | ||||||||||
| 207 | } | ||||||||||
| 208 | |||||||||||
| 209 | 0 | ▶ | sub user { | ||||||||
|
Subroutine
user: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 210 | 0 | my $self = shift; | |||||||||
| 211 | |||||||||||
| 212 | 0 | ▶ | return $self->{user} if defined $self->{user}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 213 | 0 | return $self->SUPER::user; | |||||||||
| 214 | } | ||||||||||
| 215 | |||||||||||
| 216 | |||||||||||
| 217 | 0 | ▶ | sub pidfile { | ||||||||
|
Subroutine
pidfile: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 218 | 0 | my $self = shift; | |||||||||
| 219 | |||||||||||
| 220 | 0 | return $self->{pidfile}; | |||||||||
| 221 | } | ||||||||||
| 222 | |||||||||||
| 223 | 2 | ▶ | sub gen_cfg { | ||||||||
|
Subroutine
gen_cfg: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 224 | 2 | my $self = shift; | |||||||||
| 225 | |||||||||||
| 226 | 2 | return $self->{gen_cfg}; | |||||||||
| 227 | } | ||||||||||
| 228 | |||||||||||
| 229 | 0 | ▶ | sub port { | ||||||||
|
Subroutine
port: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 230 | 0 | my $self = shift; | |||||||||
| 231 | |||||||||||
| 232 | 0 | ▶ | return $self->{port} if defined $self->{port}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 233 | 0 | return $self->{config}->{clientPort}; | |||||||||
| 234 | } | ||||||||||
| 235 | |||||||||||
| 236 | 0 | ▶ | sub timeout_options { | ||||||||
|
Subroutine
timeout_options: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 237 | return { | ||||||||||
| 238 | 0 | start => { trials => 15, step => 0.1 }, | |||||||||
| 239 | stop => { trials => 15, step => 0.1 } | ||||||||||
| 240 | }; | ||||||||||
| 241 | } | ||||||||||
| 242 | |||||||||||
| 243 | |||||||||||
| 244 | 1 | 1; | |||||||||
| 245 | |||||||||||
| 246 | __END__ | ||||||||||