Server/Kafka.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Server/Http.pm ↑ Summary Server/Kibana.pm »
1 #
2 # $Id$
3 #
4 # server::kafka Brik
5 #
6 package Metabrik::Server::Kafka;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use base qw(Metabrik::Shell::Command Metabrik::System::Package);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub brik_properties {
13 return {
14 revision => '$Revision$',
15 tags => [ qw(unstable) ],
16 author => 'GomoR <GomoR[at]metabrik.org>',
17 license => 'http://opensource.org/licenses/BSD-3-Clause',
18 attributes => {
19 datadir => [ qw(datadir) ],
20 conf_file => [ qw(file) ],
21 conf_file_zookeeper => [ qw(file) ],
22 },
23 attributes_default => {
24 conf_file => 'server.properties',
25 conf_file_zookeeper => 'zookeeper.properties',
26 },
27 commands => {
28 install => [ ],
29 generate_conf => [ ],
30 generate_conf_zookeeper => [ ],
31 start => [ ],
32 status => [ ],
33 stop => [ ],
34 start_zookeeper => [ ],
35 status_zookeeper => [ ],
36 stop_zookeeper => [ ],
37 },
38 require_modules => {
39 'Metabrik::Devel::Git' => [ ],
40 'Metabrik::File::Text' => [ ],
41 'Metabrik::System::File' => [ ],
42 'Metabrik::System::Process' => [ ],
43 },
44 require_binaries => {
45 },
46 optional_binaries => {
47 },
48 need_packages => {
49 ubuntu => [ qw(gradle openjdk-8-jdk) ],
50 freebsd => [ qw(gradle openjdk8) ],
51 },
52 };
53 }
54  
55 sub install {
56 my $self = shift;
57  
58 $self->SUPER::install(@_) or return;
59  
60 my $datadir = $self->datadir;
61 my $url = 'https://github.com/apache/kafka.git';
62  
63 my $dg = Metabrik::Devel::Git->new_from_brik_init($self) or return;
64 $dg->datadir($datadir);
65  
66 my $output_dir = "$datadir/kafka";
67 my $repo = $dg->update_or_clone($url, $output_dir) or return;
68  
69 #
70 # Then build with gradle:
71 #
72 # cd ~/metabrik/server-kakfa/kafka
73 # gradle
74 # ./gradlew jar
75 #
76  
77 return $repo;
78 }
79  
80 sub generate_conf {
81 my $self = shift;
82 my ($conf_file) = @_;
83  
84 $conf_file ||= $self->conf_file;
85 $self->brik_help_set_undef_arg('generate_conf', $conf_file) or return;
86  
87 my $datadir = $self->datadir;
88 my $basedir = "$datadir/kafka";
89 $conf_file = "$basedir/config/$conf_file";
90  
91 # https://kafka.apache.org/documentation/#configuration
92  
93 my $conf =<<EOF
94 # The id of the broker. This must be set to a unique integer for each broker.
95 broker.id=1
96  
97 # Increase the message size limit
98 message.max.bytes=20000000
99 replica.fetch.max.bytes=30000000
100  
101 # Retention time
102 log.retention.hours=24
103  
104 log.dirs=$datadir/log
105 listeners=PLAINTEXT://127.0.0.1:9092
106  
107 zookeeper.connect=localhost:2181
108  
109 # For single instance of Kakfa (no cluster), default RF on creation
110 offsets.topic.replication.factor=1
111  
112 # So we can actually delete topics
113 delete.topic.enable=true
114 EOF
115 ;
116  
117 my $ft = Metabrik::File::Text->new_from_brik_init($self) or return;
118 $ft->append(0);
119 $ft->overwrite(1);
120  
121 $ft->write($conf, $conf_file) or return;
122  
123 my $sf = Metabrik::System::File->new_from_brik_init($self) or return;
124 $sf->mkdir("$datadir/log") or return;
125  
126 return $conf_file;
127 }
128  
129 sub generate_conf_zookeeper {
130 my $self = shift;
131 my ($conf_file) = @_;
132  
133 $conf_file ||= $self->conf_file_zookeeper;
134 $self->brik_help_set_undef_arg('generate_conf_zookeeper', $conf_file) or return;
135  
136 my $datadir = $self->datadir;
137 my $basedir = "$datadir/kafka";
138 $conf_file = "$basedir/config/$conf_file";
139  
140 # https://kafka.apache.org/documentation/#configuration
141  
142 my $conf =<<EOF
143 # the directory where the snapshot is stored.
144 dataDir=$datadir/zookeeper
145 # the port at which the clients will connect
146 clientPort=2181
147 # disable the per-ip limit on the number of connections since this is a non-production config
148 maxClientCnxns=0
149 maxClientCnxns=0
150 server.1=localhost:2888:3888
151 initLimit=5
152 syncLimit=2
153 EOF
154 ;
155  
156 my $ft = Metabrik::File::Text->new_from_brik_init($self) or return;
157 $ft->append(0);
158 $ft->overwrite(1);
159  
160 $ft->write($conf, $conf_file) or return;
161  
162 my $sf = Metabrik::System::File->new_from_brik_init($self) or return;
163 $sf->mkdir("$datadir/zookeeper") or return;
164  
165 return $conf_file;
166 }
167  
168 sub start {
169 my $self = shift;
170  
171 my $datadir = $self->datadir;
172 my $basedir = "$datadir/kafka";
173 my $conf_file = $self->conf_file;
174  
175 my $cmd = "$basedir/bin/kafka-server-start.sh $basedir/config/$conf_file";
176  
177 return $self->system_in_background($cmd);
178 }
179  
180 sub start_zookeeper {
181 my $self = shift;
182  
183 my $datadir = $self->datadir;
184 my $basedir = "$datadir/kafka";
185 my $conf_file = $self->conf_file_zookeeper;
186  
187 my $cmd = "$basedir/bin/zookeeper-server-start.sh $basedir/config/$conf_file";
188  
189 return $self->system_in_background($cmd);
190 }
191  
192 sub status {
193 my $self = shift;
194  
195 my $conf_file = $self->conf_file;
196  
197 my $datadir = $self->datadir;
198 my $basedir = "$datadir/kafka";
199 $conf_file = "$basedir/config/$conf_file";
200  
201 my $sp = Metabrik::System::Process->new_from_brik_init($self) or return;
202  
203 my $string = "kafka.Kafka $conf_file";
204 if ($sp->is_running_from_string($string)) {
205 $sp->verbose_process_is_running;
206 return 1;
207 }
208  
209 $sp->verbose_process_is_not_running;
210 return 0;
211 }
212  
213 sub status_zookeeper {
214 my $self = shift;
215  
216 my $conf_file = $self->conf_file_zookeeper;
217  
218 my $datadir = $self->datadir;
219 my $basedir = "$datadir/kafka";
220 $conf_file = "$basedir/config/$conf_file";
221  
222 my $sp = Metabrik::System::Process->new_from_brik_init($self) or return;
223  
224 my $string = "org.apache.zookeeper.server.quorum.QuorumPeerMain $conf_file";
225 if ($sp->is_running_from_string($string)) {
226 $sp->verbose_process_is_running;
227 return 1;
228 }
229  
230 $sp->verbose_process_is_not_running;
231 return 0;
232 }
233  
234 sub stop {
235 my $self = shift;
236  
237 if (! $self->status) {
238 return $self->info_process_is_not_running;
239 }
240  
241 my $conf_file = $self->conf_file;
242  
243 my $datadir = $self->datadir;
244 my $basedir = "$datadir/kafka";
245 $conf_file = "$basedir/config/$conf_file";
246  
247 my $sp = Metabrik::System::Process->new_from_brik_init($self) or return;
248  
249 my $string = "kafka.Kafka $conf_file";
250 my $pid = $sp->get_pid_from_string($string) or return;
251  
252 return $sp->kill($pid);
253 }
254  
255 sub stop_zookeeper {
256 my $self = shift;
257  
258 if (! $self->status) {
259 return $self->info_process_is_not_running;
260 }
261  
262 my $conf_file = $self->conf_file_zookeeper;
263  
264 my $datadir = $self->datadir;
265 my $basedir = "$datadir/kafka";
266 $conf_file = "$basedir/config/$conf_file";
267  
268 my $sp = Metabrik::System::Process->new_from_brik_init($self) or return;
269  
270 my $string = "org.apache.zookeeper.server.quorum.QuorumPeerMain $conf_file";
271 my $pid = $sp->get_pid_from_string($string) or return;
272  
273 return $sp->kill($pid);
274 }
275  
276 1;
277  
278 __END__
« Server/Http.pm ↑ Summary Server/Kibana.pm »