line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# $Id$ |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# client::kafka Brik |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
package Metabrik::Client::Kafka; |
7
|
1
|
|
|
1
|
|
856
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
30
|
|
8
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
36
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
5
|
use base qw(Metabrik::Shell::Command); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
366
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub brik_properties { |
13
|
|
|
|
|
|
|
return { |
14
|
0
|
|
|
0
|
1
|
|
revision => '$Revision$', |
15
|
|
|
|
|
|
|
tags => [ qw(unstable) ], |
16
|
|
|
|
|
|
|
author => 'GomoR ', |
17
|
|
|
|
|
|
|
license => 'http://opensource.org/licenses/BSD-3-Clause', |
18
|
|
|
|
|
|
|
attributes => { |
19
|
|
|
|
|
|
|
host => [ qw(host_list) ], |
20
|
|
|
|
|
|
|
host_zookeeper => [ qw(host) ], |
21
|
|
|
|
|
|
|
max_fetch_size => [ qw(size) ], |
22
|
|
|
|
|
|
|
rtimeout => [ qw(seconds_float) ], |
23
|
|
|
|
|
|
|
retry => [ qw(count) ], |
24
|
|
|
|
|
|
|
retry_backoff => [ qw(milliseconds) ], |
25
|
|
|
|
|
|
|
_broker => [ qw(INTERNAL) ], |
26
|
|
|
|
|
|
|
_kc => [ qw(INTERNAL) ], |
27
|
|
|
|
|
|
|
_kcli => [ qw(INTERNAL) ], |
28
|
|
|
|
|
|
|
}, |
29
|
|
|
|
|
|
|
attributes_default => { |
30
|
|
|
|
|
|
|
host => [ qw(localhost:9092) ], |
31
|
|
|
|
|
|
|
host_zookeeper => 'localhost', |
32
|
|
|
|
|
|
|
max_fetch_size => 20000000, |
33
|
|
|
|
|
|
|
rtimeout => 3, |
34
|
|
|
|
|
|
|
retry => 5, |
35
|
|
|
|
|
|
|
retry_backoff => 1000, |
36
|
|
|
|
|
|
|
}, |
37
|
|
|
|
|
|
|
commands => { |
38
|
|
|
|
|
|
|
create_connection => [ qw(host|OPTIONAL) ], |
39
|
|
|
|
|
|
|
create_producer => [ ], |
40
|
|
|
|
|
|
|
create_consumer => [ ], |
41
|
|
|
|
|
|
|
send => [ qw(topic partition messages) ], |
42
|
|
|
|
|
|
|
loop_consumer_fetch => [ qw(topic partition|OPTIONAL) ], |
43
|
|
|
|
|
|
|
close => [ ], |
44
|
|
|
|
|
|
|
create_topic => [ qw(topic replication_factor|OPTIONAL partitions|OPTIONAL) ], |
45
|
|
|
|
|
|
|
alter_topic => [ qw(topic replication_factor|OPTIONAL partitions|OPTIONAL) ], |
46
|
|
|
|
|
|
|
delete_topic => [ qw(topic) ], |
47
|
|
|
|
|
|
|
list_topics => [ ], |
48
|
|
|
|
|
|
|
describe_topic => [ qw(topic) ], |
49
|
|
|
|
|
|
|
run_console_producer => [ qw(topic) ], |
50
|
|
|
|
|
|
|
run_console_consumer => [ qw(topic) ], |
51
|
|
|
|
|
|
|
}, |
52
|
|
|
|
|
|
|
require_modules => { |
53
|
|
|
|
|
|
|
'List::Util' => [ qw(shuffle) ], |
54
|
|
|
|
|
|
|
'Kafka' => [ ], |
55
|
|
|
|
|
|
|
'Kafka::Connection' => [ ], |
56
|
|
|
|
|
|
|
'Kafka::Producer' => [ ], |
57
|
|
|
|
|
|
|
'Kafka::Consumer' => [ ], |
58
|
|
|
|
|
|
|
}, |
59
|
|
|
|
|
|
|
require_binaries => { |
60
|
|
|
|
|
|
|
}, |
61
|
|
|
|
|
|
|
optional_binaries => { |
62
|
|
|
|
|
|
|
}, |
63
|
|
|
|
|
|
|
need_packages => { |
64
|
|
|
|
|
|
|
freebsd => [ qw(p5-Tree-Trie) ], |
65
|
|
|
|
|
|
|
}, |
66
|
|
|
|
|
|
|
}; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub create_connection { |
70
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
71
|
0
|
|
|
|
|
|
my ($host) = @_; |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
0
|
|
|
|
$host ||= $self->host; |
74
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('create_connection', $host) or return; |
75
|
0
|
0
|
|
|
|
|
$self->brik_help_run_invalid_arg('create_connection', $host, 'ARRAY') or return; |
76
|
0
|
0
|
|
|
|
|
$self->brik_help_run_empty_array_arg('create_connection', $host) or return; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# Patch fonction to disable utf8 stuff, it fails strangely. |
79
|
|
|
|
|
|
|
{ |
80
|
1
|
|
|
1
|
|
12
|
no warnings 'redefine'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
2245
|
|
|
0
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
*Kafka::Connection::_is_like_server = sub { |
83
|
0
|
|
|
0
|
|
|
my ($self, $server) = @_; |
84
|
|
|
|
|
|
|
|
85
|
0
|
0
|
0
|
|
|
|
unless( |
|
|
|
0
|
|
|
|
|
86
|
|
|
|
|
|
|
defined($server) |
87
|
|
|
|
|
|
|
&& defined(Kafka::Connection::_STRING($server)) |
88
|
|
|
|
|
|
|
#&& !utf8::is_utf8($server) # this sucks. |
89
|
|
|
|
|
|
|
&& $server =~ /^[^:]+:\d+$/ |
90
|
|
|
|
|
|
|
) { |
91
|
0
|
|
|
|
|
|
return; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
0
|
|
|
|
|
|
return $server; |
95
|
0
|
|
|
|
|
|
}; |
96
|
|
|
|
|
|
|
}; |
97
|
|
|
|
|
|
|
|
98
|
0
|
|
|
|
|
|
my $rtimeout = $self->rtimeout; |
99
|
0
|
|
|
|
|
|
my $send_max_attempts = $self->retry; |
100
|
0
|
|
|
|
|
|
my $retry_backoff = $self->retry_backoff; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# Cause Kafka will connect to the first working broker. |
103
|
|
|
|
|
|
|
# By randomizing, different processes will use different brokers. |
104
|
0
|
|
|
|
|
|
my @list = List::Util::shuffle(@$host); |
105
|
|
|
|
|
|
|
|
106
|
0
|
|
|
|
|
|
my $broker = $list[0]; # We take the first, as it is now randomized. |
107
|
0
|
|
|
|
|
|
$self->_broker($broker); |
108
|
|
|
|
|
|
|
|
109
|
0
|
|
|
|
|
|
my $kc; |
110
|
0
|
|
|
|
|
|
eval { |
111
|
0
|
|
|
|
|
|
$kc = Kafka::Connection->new( |
112
|
|
|
|
|
|
|
broker_list => [ $broker ], |
113
|
|
|
|
|
|
|
timeout => $rtimeout, |
114
|
|
|
|
|
|
|
SEND_MAX_ATTEMPTS => $send_max_attempts, |
115
|
|
|
|
|
|
|
RETRY_BACKOFF => $retry_backoff, |
116
|
|
|
|
|
|
|
); |
117
|
|
|
|
|
|
|
}; |
118
|
0
|
0
|
|
|
|
|
if ($@) { |
119
|
0
|
|
|
|
|
|
chomp($@); |
120
|
0
|
|
|
|
|
|
my $str_list = join(',', @list); |
121
|
0
|
|
|
|
|
|
return $self->log->error("create_connection: failed with list [$str_list]: [$@]"); |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
0
|
|
|
|
|
|
return $self->_kc($kc); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub create_producer { |
128
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
129
|
0
|
|
|
|
|
|
my ($host) = @_; |
130
|
|
|
|
|
|
|
|
131
|
0
|
0
|
|
|
|
|
my $kc = $self->create_connection or return; |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
# Doc: |
134
|
|
|
|
|
|
|
# https://kafka.apache.org/documentation/#acks |
135
|
|
|
|
|
|
|
|
136
|
0
|
|
|
|
|
|
my $kp; |
137
|
0
|
|
|
|
|
|
eval { |
138
|
0
|
|
|
|
|
|
$kp = Kafka::Producer->new( |
139
|
|
|
|
|
|
|
Connection => $kc, |
140
|
|
|
|
|
|
|
#RequiredAcks => $Kafka::WAIT_WRITTEN_TO_LOCAL_LOG, # 1, default |
141
|
|
|
|
|
|
|
RequiredAcks => $Kafka::BLOCK_UNTIL_IS_COMMITTED, # -1, best |
142
|
|
|
|
|
|
|
#RequiredAcks => $Kafka::NOT_SEND_ANY_RESPONSE, # 0 |
143
|
|
|
|
|
|
|
); |
144
|
|
|
|
|
|
|
}; |
145
|
0
|
0
|
|
|
|
|
if ($@) { |
146
|
0
|
|
|
|
|
|
chomp($@); |
147
|
0
|
|
|
|
|
|
return $self->log->error("create_producer: failed [$@]"); |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
0
|
|
|
|
|
|
return $self->_kcli($kp); |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
sub create_consumer { |
154
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
155
|
0
|
|
|
|
|
|
my ($host) = @_; |
156
|
|
|
|
|
|
|
|
157
|
0
|
0
|
|
|
|
|
my $kc = $self->create_connection or return; |
158
|
|
|
|
|
|
|
|
159
|
0
|
|
|
|
|
|
my $kco; |
160
|
0
|
|
|
|
|
|
eval { |
161
|
0
|
|
|
|
|
|
$kco = Kafka::Consumer->new(Connection => $kc); |
162
|
|
|
|
|
|
|
}; |
163
|
0
|
0
|
|
|
|
|
if ($@) { |
164
|
0
|
|
|
|
|
|
chomp($@); |
165
|
0
|
|
|
|
|
|
return $self->log->error("create_consumer: failed [$@]"); |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
0
|
|
|
|
|
|
return $self->_kcli($kco); |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
sub send { |
172
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
173
|
0
|
|
|
|
|
|
my ($topic, $partition, $messages) = @_; |
174
|
|
|
|
|
|
|
|
175
|
0
|
|
|
|
|
|
my $kcli = $self->_kcli; |
176
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('create_producer', $kcli) or return; |
177
|
|
|
|
|
|
|
|
178
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('send', $topic) or return; |
179
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('send', $partition) or return; |
180
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('send', $messages) or return; |
181
|
0
|
0
|
|
|
|
|
$self->brik_help_run_invalid_arg('send', $messages, 'ARRAY', 'SCALAR') or return; |
182
|
|
|
|
|
|
|
|
183
|
0
|
|
|
|
|
|
my $broker = $self->_broker; |
184
|
|
|
|
|
|
|
|
185
|
0
|
|
|
|
|
|
my $r; |
186
|
0
|
|
|
|
|
|
eval { |
187
|
0
|
|
|
|
|
|
$r = $kcli->send($topic, $partition, $messages); |
188
|
|
|
|
|
|
|
}; |
189
|
0
|
0
|
|
|
|
|
if ($@) { |
190
|
0
|
|
|
|
|
|
chomp($@); |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
# Response $r looks like the following. We should use ErrorCode instead of regexes. |
193
|
|
|
|
|
|
|
# { |
194
|
|
|
|
|
|
|
# CorrelationId => -1608629279, |
195
|
|
|
|
|
|
|
# Throttle_Time_Ms => 0, |
196
|
|
|
|
|
|
|
# topics => [ { |
197
|
|
|
|
|
|
|
# partitions => [ |
198
|
|
|
|
|
|
|
# { ErrorCode => 0, Log_Append_Time => -1, Offset => 0, Partition => 0 }, |
199
|
|
|
|
|
|
|
# ], |
200
|
|
|
|
|
|
|
# TopicName => "test", |
201
|
|
|
|
|
|
|
# } ], |
202
|
|
|
|
|
|
|
# } |
203
|
|
|
|
|
|
|
|
204
|
0
|
|
|
|
|
|
my $no_ack_for_request = 'No acknowledgement for sent request'; |
205
|
0
|
|
|
|
|
|
my $cant_connect = 'Cannot connect to broker'; |
206
|
0
|
|
|
|
|
|
my $cant_get_metadata = 'Cannot get metadata'; |
207
|
0
|
|
|
|
|
|
my $unable_to_write = 'Unable to write due to ongoing Kafka leader selection'; |
208
|
0
|
|
|
|
|
|
my $no_known_broker = 'There are no known brokers'; |
209
|
0
|
|
|
|
|
|
my $too_big = 'Message is too big'; |
210
|
0
|
|
|
|
|
|
my $invalid_arg_messages = 'Invalid argument: messages'; |
211
|
0
|
|
|
|
|
|
my $cannot_send = 'Cannot send'; |
212
|
0
|
|
|
|
|
|
my $err = $@; |
213
|
0
|
0
|
|
|
|
|
if ($@ =~ m{^$no_ack_for_request}i) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
214
|
0
|
|
|
|
|
|
$err = $no_ack_for_request; |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
elsif ($@ =~ m{^$cant_connect}i) { |
217
|
0
|
|
|
|
|
|
$err = $cant_connect; |
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
elsif ($@ =~ m{^$cant_get_metadata}i) { |
220
|
0
|
|
|
|
|
|
$err = $cant_get_metadata; |
221
|
|
|
|
|
|
|
} |
222
|
|
|
|
|
|
|
elsif ($@ =~ m{^$unable_to_write}i) { |
223
|
0
|
|
|
|
|
|
$err = $unable_to_write; |
224
|
|
|
|
|
|
|
} |
225
|
|
|
|
|
|
|
elsif ($@ =~ m{^$no_known_broker}i) { |
226
|
0
|
|
|
|
|
|
$err = $no_known_broker; |
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
elsif ($@ =~ m{^$too_big}i) { |
229
|
0
|
|
|
|
|
|
$err = $too_big; |
230
|
|
|
|
|
|
|
} |
231
|
|
|
|
|
|
|
elsif ($@ =~ m{^$invalid_arg_messages}i) { |
232
|
0
|
|
|
|
|
|
$err = $invalid_arg_messages; |
233
|
|
|
|
|
|
|
} |
234
|
|
|
|
|
|
|
elsif ($@ =~ m{^$cannot_send}i) { |
235
|
0
|
|
|
|
|
|
my ($errnum, $details) = $@ =~ m{, (-\d+),.+?'([^']+)', 'Kafka::Exception}si; |
236
|
0
|
|
0
|
|
|
|
$errnum ||= "unknown"; |
237
|
0
|
|
0
|
|
|
|
$details ||= "unknown"; |
238
|
0
|
|
|
|
|
|
$err = "$cannot_send: $errnum, $details"; |
239
|
|
|
|
|
|
|
} |
240
|
|
|
|
|
|
|
|
241
|
0
|
|
|
|
|
|
my $broker = $self->_broker; |
242
|
|
|
|
|
|
|
|
243
|
0
|
|
|
|
|
|
my $count = 1; |
244
|
0
|
|
|
|
|
|
my $length = length($messages); |
245
|
0
|
0
|
|
|
|
|
if (ref($messages) eq 'ARRAY') { |
246
|
0
|
|
|
|
|
|
$length = 0; |
247
|
0
|
|
|
|
|
|
$count = scalar(@$messages); |
248
|
0
|
|
|
|
|
|
for (@$messages) { |
249
|
0
|
|
|
|
|
|
$length += length($_); |
250
|
|
|
|
|
|
|
} |
251
|
|
|
|
|
|
|
} |
252
|
0
|
|
|
|
|
|
return $self->log->error("send: fail for broker [$broker], partition[$partition] ". |
253
|
|
|
|
|
|
|
"message count[$count] with length[$length]: [$err]"); |
254
|
|
|
|
|
|
|
} |
255
|
|
|
|
|
|
|
|
256
|
0
|
|
|
|
|
|
my $count = 1; |
257
|
0
|
|
|
|
|
|
my $length = length($messages); |
258
|
0
|
0
|
|
|
|
|
if (ref($messages) eq 'ARRAY') { |
259
|
0
|
|
|
|
|
|
$length = 0; |
260
|
0
|
|
|
|
|
|
$count = scalar(@$messages); |
261
|
0
|
|
|
|
|
|
for (@$messages) { |
262
|
0
|
|
|
|
|
|
$length += length($_); |
263
|
|
|
|
|
|
|
} |
264
|
|
|
|
|
|
|
} |
265
|
0
|
|
|
|
|
|
$self->log->verbose("send: successful for broker [$broker], partition[$partition] ". |
266
|
|
|
|
|
|
|
"message count[$count] with length[$length]"); |
267
|
|
|
|
|
|
|
|
268
|
0
|
|
|
|
|
|
return $r; |
269
|
|
|
|
|
|
|
} |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
sub loop_consumer_fetch { |
272
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
273
|
0
|
|
|
|
|
|
my ($topic, $partition) = @_; |
274
|
|
|
|
|
|
|
|
275
|
0
|
|
|
|
|
|
my $kcli = $self->_kcli; |
276
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('create_consumer', $kcli) or return; |
277
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('loop_consumer_fetch', $topic) or return; |
278
|
|
|
|
|
|
|
|
279
|
0
|
|
0
|
|
|
|
$partition ||= 0; |
280
|
|
|
|
|
|
|
|
281
|
0
|
|
|
|
|
|
my $offsets = $kcli->offsets( |
282
|
|
|
|
|
|
|
$topic, |
283
|
|
|
|
|
|
|
$partition, |
284
|
|
|
|
|
|
|
$Kafka::RECEIVE_EARLIEST_OFFSET, # time |
285
|
|
|
|
|
|
|
$Kafka::DEFAULT_MAX_NUMBER_OF_OFFSETS, # max_number |
286
|
|
|
|
|
|
|
); |
287
|
|
|
|
|
|
|
|
288
|
0
|
|
|
|
|
|
for (@$offsets) { |
289
|
0
|
|
|
|
|
|
print "Received offset: $_\n"; |
290
|
|
|
|
|
|
|
} |
291
|
|
|
|
|
|
|
|
292
|
0
|
|
|
|
|
|
my $messages = $kcli->fetch( |
293
|
|
|
|
|
|
|
$topic, |
294
|
|
|
|
|
|
|
$partition, |
295
|
|
|
|
|
|
|
0, # offset |
296
|
|
|
|
|
|
|
$self->max_fetch_size, # Maximum size of MESSAGE(s) to receive |
297
|
|
|
|
|
|
|
); |
298
|
0
|
|
|
|
|
|
for my $message (@$messages) { |
299
|
0
|
0
|
|
|
|
|
if ($message->valid) { |
300
|
0
|
|
|
|
|
|
print 'payload : ', $message->payload, "\n"; |
301
|
0
|
|
|
|
|
|
print 'key : ', $message->key, "\n"; |
302
|
0
|
|
|
|
|
|
print 'offset : ', $message->offset, "\n"; |
303
|
0
|
|
|
|
|
|
print 'next_offset: ', $message->next_offset, "\n"; |
304
|
|
|
|
|
|
|
} |
305
|
|
|
|
|
|
|
else { |
306
|
0
|
|
|
|
|
|
print 'error : ', $message->error, "\n"; |
307
|
|
|
|
|
|
|
} |
308
|
|
|
|
|
|
|
} |
309
|
|
|
|
|
|
|
|
310
|
0
|
|
|
|
|
|
return 1; |
311
|
|
|
|
|
|
|
} |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
sub close { |
314
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
315
|
|
|
|
|
|
|
|
316
|
0
|
0
|
|
|
|
|
if ($self->_kcli) { |
317
|
0
|
|
|
|
|
|
$self->_kcli(undef); |
318
|
|
|
|
|
|
|
} |
319
|
|
|
|
|
|
|
|
320
|
0
|
0
|
|
|
|
|
if ($self->_kc) { |
321
|
0
|
|
|
|
|
|
$self->_kc->close; |
322
|
0
|
|
|
|
|
|
$self->_kc(undef); |
323
|
|
|
|
|
|
|
} |
324
|
|
|
|
|
|
|
|
325
|
0
|
|
|
|
|
|
return 1; |
326
|
|
|
|
|
|
|
} |
327
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
sub create_topic { |
329
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
330
|
0
|
|
|
|
|
|
my ($topic, $rf, $partitions) = @_; |
331
|
|
|
|
|
|
|
|
332
|
0
|
|
0
|
|
|
|
$rf ||= 1; |
333
|
0
|
|
0
|
|
|
|
$partitions ||= 1; |
334
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('create_topic', $topic) or return; |
335
|
|
|
|
|
|
|
|
336
|
0
|
|
|
|
|
|
my $basedir = $ENV{HOME}."/metabrik/server-kafka/kafka"; |
337
|
0
|
|
|
|
|
|
my $host = $self->host_zookeeper; |
338
|
|
|
|
|
|
|
|
339
|
0
|
|
|
|
|
|
my $cmd = "$basedir/bin/kafka-topics.sh --create --zookeeper $host:2181 ". |
340
|
|
|
|
|
|
|
"--replication-factor $rf --partitions $partitions --topic $topic"; |
341
|
|
|
|
|
|
|
|
342
|
0
|
|
|
|
|
|
$self->log->verbose("create_topic: cmd[$cmd]"); |
343
|
|
|
|
|
|
|
|
344
|
0
|
|
|
|
|
|
return $self->execute($cmd); |
345
|
|
|
|
|
|
|
} |
346
|
|
|
|
|
|
|
|
347
|
|
|
|
|
|
|
sub alter_topic { |
348
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
349
|
0
|
|
|
|
|
|
my ($topic, $partitions) = @_; |
350
|
|
|
|
|
|
|
|
351
|
0
|
|
0
|
|
|
|
$partitions ||= 1; |
352
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('alter_topic', $topic) or return; |
353
|
|
|
|
|
|
|
|
354
|
0
|
|
|
|
|
|
my $basedir = $ENV{HOME}."/metabrik/server-kafka/kafka"; |
355
|
0
|
|
|
|
|
|
my $host = $self->host_zookeeper; |
356
|
|
|
|
|
|
|
|
357
|
0
|
|
|
|
|
|
my $cmd = "$basedir/bin/kafka-topics.sh --alter --zookeeper $host:2181 ". |
358
|
|
|
|
|
|
|
"--partitions $partitions --topic $topic"; |
359
|
|
|
|
|
|
|
|
360
|
0
|
|
|
|
|
|
$self->log->verbose("alter_topic: cmd[$cmd]"); |
361
|
|
|
|
|
|
|
|
362
|
0
|
|
|
|
|
|
return $self->execute($cmd); |
363
|
|
|
|
|
|
|
} |
364
|
|
|
|
|
|
|
|
365
|
|
|
|
|
|
|
sub delete_topic { |
366
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
367
|
0
|
|
|
|
|
|
my ($topic) = @_; |
368
|
|
|
|
|
|
|
|
369
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('delete_topic', $topic) or return; |
370
|
|
|
|
|
|
|
|
371
|
0
|
|
|
|
|
|
my $basedir = $ENV{HOME}."/metabrik/server-kafka/kafka"; |
372
|
0
|
|
|
|
|
|
my $host = $self->host_zookeeper; |
373
|
|
|
|
|
|
|
|
374
|
0
|
|
|
|
|
|
my $cmd = "$basedir/bin/kafka-topics.sh --delete --if-exists --zookeeper $host:2181 ". |
375
|
|
|
|
|
|
|
"--topic $topic"; |
376
|
|
|
|
|
|
|
|
377
|
0
|
|
|
|
|
|
$self->log->verbose("delete_topic: cmd[$cmd]"); |
378
|
|
|
|
|
|
|
|
379
|
0
|
|
|
|
|
|
return $self->execute($cmd); |
380
|
|
|
|
|
|
|
} |
381
|
|
|
|
|
|
|
|
382
|
|
|
|
|
|
|
sub list_topics { |
383
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
384
|
|
|
|
|
|
|
|
385
|
0
|
|
|
|
|
|
my $basedir = $ENV{HOME}."/metabrik/server-kafka/kafka"; |
386
|
0
|
|
|
|
|
|
my $host = $self->host_zookeeper; |
387
|
|
|
|
|
|
|
|
388
|
0
|
|
|
|
|
|
my $cmd = "$basedir/bin/kafka-topics.sh --list --zookeeper $host:2181"; |
389
|
|
|
|
|
|
|
|
390
|
0
|
|
|
|
|
|
$self->log->verbose("list_topics: cmd[$cmd]"); |
391
|
|
|
|
|
|
|
|
392
|
0
|
|
|
|
|
|
return $self->execute($cmd); |
393
|
|
|
|
|
|
|
} |
394
|
|
|
|
|
|
|
|
395
|
|
|
|
|
|
|
sub describe_topic { |
396
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
397
|
0
|
|
|
|
|
|
my ($topic) = @_; |
398
|
|
|
|
|
|
|
|
399
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('describe_topic', $topic) or return; |
400
|
|
|
|
|
|
|
|
401
|
0
|
|
|
|
|
|
my $basedir = $ENV{HOME}."/metabrik/server-kafka/kafka"; |
402
|
0
|
|
|
|
|
|
my $host = $self->host_zookeeper; |
403
|
|
|
|
|
|
|
|
404
|
0
|
|
|
|
|
|
my $cmd = "$basedir/bin/kafka-topics.sh --describe --zookeeper $host:2181 --topic $topic"; |
405
|
|
|
|
|
|
|
|
406
|
0
|
|
|
|
|
|
$self->log->verbose("describe_topic: cmd[$cmd]"); |
407
|
|
|
|
|
|
|
|
408
|
0
|
|
|
|
|
|
return $self->execute($cmd); |
409
|
|
|
|
|
|
|
} |
410
|
|
|
|
|
|
|
|
411
|
|
|
|
|
|
|
# https://stackoverflow.com/questions/16284399/purge-kafka-queue |
412
|
|
|
|
|
|
|
# kafka-topics.sh --zookeeper localhost:13003 --alter --topic MyTopic --config retention.ms=1000 |
413
|
|
|
|
|
|
|
# Wait, then restore previous retention.ms |
414
|
|
|
|
|
|
|
#sub purge_topic { |
415
|
|
|
|
|
|
|
#} |
416
|
|
|
|
|
|
|
|
417
|
|
|
|
|
|
|
sub run_console_producer { |
418
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
419
|
0
|
|
|
|
|
|
my ($topic) = @_; |
420
|
|
|
|
|
|
|
|
421
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('run_console_producer', $topic) or return; |
422
|
|
|
|
|
|
|
|
423
|
0
|
|
|
|
|
|
my $basedir = $ENV{HOME}."/metabrik/server-kafka/kafka"; |
424
|
0
|
|
|
|
|
|
my $host = $self->host; |
425
|
|
|
|
|
|
|
|
426
|
0
|
|
|
|
|
|
my $cmd = "$basedir/bin/kafka-console-producer.sh --broker-list $host:9092 --topic $topic"; |
427
|
|
|
|
|
|
|
|
428
|
0
|
|
|
|
|
|
$self->log->verbose("run_console_producer: cmd[$cmd]"); |
429
|
|
|
|
|
|
|
|
430
|
0
|
|
|
|
|
|
return $self->execute($cmd); |
431
|
|
|
|
|
|
|
} |
432
|
|
|
|
|
|
|
|
433
|
|
|
|
|
|
|
sub run_console_consumer { |
434
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
435
|
0
|
|
|
|
|
|
my ($topic) = @_; |
436
|
|
|
|
|
|
|
|
437
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('run_console_consumer', $topic) or return; |
438
|
|
|
|
|
|
|
|
439
|
0
|
|
|
|
|
|
my $basedir = $ENV{HOME}."/metabrik/server-kafka/kafka"; |
440
|
0
|
|
|
|
|
|
my $host = $self->host; |
441
|
|
|
|
|
|
|
|
442
|
0
|
|
|
|
|
|
my $cmd = "$basedir/bin/kafka-console-consumer.sh --bootstrap-server $host:9092 ". |
443
|
|
|
|
|
|
|
"--topic $topic --from-beginning"; |
444
|
|
|
|
|
|
|
|
445
|
0
|
|
|
|
|
|
$self->log->verbose("run_console_consumer: cmd[$cmd]"); |
446
|
|
|
|
|
|
|
|
447
|
0
|
|
|
|
|
|
return $self->execute($cmd); |
448
|
|
|
|
|
|
|
} |
449
|
|
|
|
|
|
|
|
450
|
|
|
|
|
|
|
1; |
451
|
|
|
|
|
|
|
|
452
|
|
|
|
|
|
|
__END__ |