-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathoverview.edoc
More file actions
350 lines (271 loc) · 12.4 KB
/
overview.edoc
File metadata and controls
350 lines (271 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
@author Gregoire Lejeune <gregoire.lejeune@finexkap.com>
@author Gregoire Lejeune <greg@g-corp.io>
@author Gregoire Lejeune <gregoire.lejeune@botsunit.com>
@copyright 2014, 2015 Finexkap, 2015, 2016, 2017, 2018 G-Corp, 2015, 2016, 2017 BotsUnit
@version 2.2.0
@title A Kafka client for Erlang and Elixir
@doc
[](https://hex.pm/packages/kafe)
[](https://hex.pm/packages/kafe)
[](https://hex.pm/packages/kafe)
[](https://travis-ci.org/G-Corp/kafe)
<b>Version 2.0.0 cause changes in the following APIs :</b>
<ul>
<li>{@link kafe:start_consumer/3}</li>
<li>{@link kafe:fetch/3}</li>
</ul>
<b>Kafe</b> has been tested with Kafka 0.9 and above
You can also use it with Kafka 0.8 but {@link kafe_consumer} is not compatible with this version.
<h3>Links</h3>
<ul>
<li><a href="http://kafka.apache.org">Apache Kafka</a></li>
<li><a href="https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol">Apache Kafka Protocol</a></li>
</ul>
<h3>Configuration</h3>
<table width="100%" border="0" summary="configuration">
<tr><td>brokers</td><td><tt>[{inet:hostname(), inet:port_number()}]</tt></td><td>List of brokers</td><td><tt>[{"localhost", 9092}]</tt></td></tr>
<tr><td>pool_size</td><td><tt>integer()</tt></td><td>Initial connection pool/brocker</td><td><tt>5</tt></td></tr>
<tr><td>chunk_pool_size</td><td><tt>integer()</tt></td><td>Size of new connection pool/brocker</td><td><tt>10</tt></td></tr>
<tr><td>brokers_update_frequency</td><td><tt>integer()</tt></td><td>Frequency (ms) for brokers update</td><td><tt>60000</tt></td></tr>
<tr><td>protocol_timeout</td><td><tt>integer()</tt></td><td>API call timeout</td><td><tt>60000</tt></td></tr>
<tr><td>client_id</td><td><tt>binary()</tt></td><td>Client ID Name</td><td><tt><<"kafe">></tt></td></tr>
<tr><td>api_version</td><td><tt>[{integer(), integer()}] | integer() | auto</tt></td><td>API Version</td><td><tt>auto<sup>*</sup></tt></td></tr>
<tr><td>correlation_id</td><td><tt>integer()</tt></td><td>Correlation ID</td><td><tt>0</tt></td></tr>
<tr><td>socket</td><td><tt>[{sndbuf, integer()}, {recbuf, integer()}, {buffer, integer()}]</tt></td><td>Socker configuration</td><td><tt>[{sndbuf, 4194304}, {recbuf, 4194304}, {buffer, 4194304}]</tt></td></tr>
</table>
<sup>*</sup> use <tt>0</tt> with Kafka >= 0.8 < 0.9 ; <tt>auto</tt> with Kafka >= 0.9 < 0.10
Example :
<pre>
[
{kafe, [
{brokers, [
{"localhost", 9092},
{"localhost", 9093},
{"localhost", 9094}
]},
{pool_size, 1},
{chunk_pool_size, 2},
{brokers_update_frequency, 10000},
{client_id, <<"kafe">>},
{api_version, auto},
{correlation_id, 0},
{socket, [
{sndbuf, 4194304},
{recbuf, 4194304},
{buffer, 4194304}
]},
]}
]
</pre>
<b>Kafe</b> use <a href="https://github.com/basho/lager">lager</a> ; see also how to <a href="https://github.com/basho/lager#configuration">configure</a> it.
<h4>Custom API version</h4>
If you need to, you can specify each version of the protocol APIs. To do so, use a list as value for the <tt>api_version</tt>'s configuration. This list must be a list of tuples where the first element is the api key and the second is the api version.
Example :
<pre>
...
{api_version, [{0, 0}, {0, 1}, {0, 0}, ...]}
...
</pre>
<h3>Create a consumer</h3>
<h4>Using a function</h4>
To create a consumer, create a function with 6 parameters :
<pre>
-module(my_consumer).
-export([consume/6]).
consume(CommitID, Topic, Partition, Offset, Key, Value) ->
% Do something with Topic/Partition/Offset/Key/Value
ok.
</pre>
The <tt>consume</tt> function must return <tt>ok</tt> if the message was treated, or <tt>{error, term()}</tt> on error.
Then start a new consumer :
<pre>
...
kafe:start(),
...
kafe:start_consumer(my_group, fun my_consumer:consume/6, Options),
...
</pre>
See {@link kafe:start_consumer/3} for the available <tt>Options</tt>.
In the <tt>consume</tt> function, if you didn't start the consumer in autocommit mode (using <tt>before_processing | after_processing</tt> in the <tt>commit</tt> options),
you need to commit manually when you have finished to treat the message. To do so, use {@link kafe_consumer:commit/4}.
When you are done with your consumer, stop it :
<pre>
...
kafe:stop_consumer(my_group),
...
</pre>
<h4>Using the <tt>kafe_consumer_subscriber</tt> behaviour</h4>
<pre>
-module(my_consumer).
-behaviour(kafe_consumer_subscriber).
-export([init/4, handle_message/2]).
-include_lib("kafe/include/kafe_consumer.hrl").
-record(state, {
}).
init(Group, Topic, Partition, Args) ->
% Do something with Group, Topic, Partition, Args
{ok, #state{}}.
handle_message(Message, State) ->
% Do something with Message
% And update your State (if needed)
{ok, NewState}.
</pre>
Then start a new consumer :
<pre>
...
kafe:start().
...
kafe:start_consumer(my_group, {my_consumer, Args}, Options).
% Or
kafe:start_consumer(my_group, my_consumer, Options).
...
</pre>
To commit a message (if you need to), use {@link kafe_consumer:commit/4}.
<h3>Using with Elixir</h3>
Elixir' users can use <tt>Kafe</tt> and <tt>Kafe.Consumer</tt> instead of <tt>:kafe</tt> and <tt>:kafe_consumer</tt>.
<pre>
defmodule My.Consumer do
def consume(commit_id, topic, partition, offset, key, value) do
# Do something with topic/partition/offset/key/value
:ok
end
end
defmodule My.Consumer.Subscriber do
@behaviour Kafe.Consumer.Subscriber
require Kafe.Records
def init(group, topic, partition, args) do
% Do something with group/topic/partition/args
% and create the state
{:ok, state}
end
def handle_message(message, state) do
msg = Kafe.Records.message(message)
% Do something with msg and update (or not) the state
{:ok, new_state}
end
end
</pre>
<pre>
...
Kafe.start()
...
Kafe.start_consumer(:my_group, &My.Consumer.consume/6, options)
# or
Kafe.start_consumer(:my_group, {My.Consumer.Subscriber, args}, options)
# or
Kafe.start_consumer(:my_group, My.Consumer.Subscriber, options)
...
Kafe.stop_consumer(:my_group)
...
</pre>
<h3>Metrics</h3>
To enable metrics :
1/ Add <a href="https://hex.pm/packages/metrics">metrics > 2.2</a> in your dependencies.
2/ Set <tt>enable_metrics</tt> to true in the <tt>kafe</tt> configuration :
<pre>
{kafe, [
...
{enable_metrics, true},
...
]}
</pre>
3/ Adding a metrics module in your configuration :
<pre>
{metrics, [
{metrics_mod, metrics_folsom}
]}
</pre>
You can choose between <a href="https://github.com/folsom-project/folsom">Folsom</a> (<tt>{metrics_mod, metrics_folsom}</tt>), <a href="https://github.com/Feuerlabs/exometer">Exometer</a> (<tt>{metrics_mod, metrics_exometer}</tt>) or <a href="https://github.com/processone/grapherl">Grapherl</a> (<tt>{metrics_mod, metrics_grapherl}</tt>).
Be sure that's Folsom, Exometer or Grapherl and metrics is started before starting Kafe.
<pre>
application:ensure_all_started(folsom).
application:ensure_all_started(metrics).
application:ensure_all_started(kafe).
</pre>
Metrics are disabled by default.
Kafe offers the following metrics :
<table>
<tr><th>Name</th><th>Type</th><th>Description</th></tr>
<tr><td>kafe_consumer.CONSUMER_GROUP.messages.fetch</td><td>gauge</td><td>Number of received messages on the last fetch for the CONSUMER_GROUP</td></tr>
<tr><td>kafe_consumer.CONSUMER_GROUP.TOPIC.PARTITION.messages.fetch</td><td>gauge</td><td>Number of received messages on the last fetch for the {TOPIC, PARTITION} and CONSUMER_GROUP</td></tr>
<tr><td>kafe_consumer.CONSUMER_GROUP.messages</td><td>counter</td><td>Total number of received messages for the CONSUMER_GROUP</td></tr>
<tr><td>kafe_consumer.CONSUMER_GROUP.TOPIC.PARTITION.messages</td><td>counter</td><td>Total number of received messages for the {TOPIC, PARTITION} and CONSUMER_GROUP</td></tr>
<tr><td>kafe_consumer.CONSUMER_GROUP.TOPIC.PARTITION.duration.fetch</td><td>gauge</td><td>Fetch duration (ms) per message, for the {TOPIC, PARTITION} and CONSUMER_GROUP</td></tr>
<tr><td>kafe_consumer.CONSUMER_GROUP.TOPIC.PARTITION.pending_commits</td><td>gauge</td><td>Number of pending commits, for the {TOPIC, PARTITION} and CONSUMER_GROUP</td></tr>
</table>
You can add a prefix to all metrics by adding a <tt>metrics_prefix</tt> in the <tt>metrics</tt> configuration :
<pre>
{metrics, [
{metrics_mod, metrics_folsom},
{metrics_prefix, my_bot}
]}
</pre>
<h3>Build and tests</h3>
<b>Kafe</b> use <a href="http://www.rebar3.org">rebar3</a> and <a href="https://github.com/G-Corp/bu.mk">bu.mk</a>. So, you can use :
<ul>
<li><tt>./rebar3 compile</tt> to compile Kafe.</li>
<li><tt>./rebar3 eunit</tt> to run tests.</li>
<li><tt>./rebar3 ct</tt> to run (integration) tests.</li>
<li><tt>./rebar3 edoc</tt> to build documentation.</li>
<li><tt>./rebar3 elixir generate_mix</tt> to generate <tt>mix.exs</tt> file.</li>
<li><tt>./rebar3 elixir generate_lib</tt> to generate Elixir bindings.</li>
</ul>
Or
<ul>
<li><tt>make release</tt> Tag and release to hex.pm</li>
<li><tt>make integ</tt> Run integration tests</li>
<li><tt>make docker-compose.yml</tt> Create docker-compose.yml</li>
<li><tt>make docker-start</tt> Start docker</li>
<li><tt>make docker-stop</tt> Stop docker</li>
<li><tt>make elixir</tt> Generate Elixir bindings (mix.exs and libs)</li>
<li><tt>make tests</tt> Run tests</li>
<li><tt>make doc</tt> Generate doc</li>
<li><tt>make dist</tt> Create a distribution</li>
<li><tt>make clean</tt> Clean</li>
<li><tt>make distclean</tt> Clean the distribution</li>
<li><tt>make info</tt> Display application informations</li>
<li><tt>make tag</tt> Create a git tag</li>
<li><tt>make local.hex</tt> Install hexfor Mix</li>
<li><tt>make local.rebar</tt> Install rebar for Mix</li>
<li><tt>make bu-mk</tt> Update bu.mk</li>
<li><tt>make help</tt> Show this help.</li>
</ul>
To run the integration tests, you must start zookeeper and a kafka cluster (3 brokers) and have the following three topics :
<ul>
<li><tt>testone</tt> : replication factor: 1, partitions: 1</li>
<li><tt>testtwo</tt> : replication factor: 2, partitions: 2</li>
<li><tt>testthree</tt> : replication factor: 3, partitions: 3</li>
</ul>
You can use the makefile rules <tt>docker-compose.yml</tt> and <tt>docker-start</tt> to help you to create this environment using docker (tested on Linux only).
<h3>API Documentation</h3>
See <a href=".">documentation</a>
<h3>Contributing</h3>
<ol>
<li>Fork it ( https://github.com/G-Corp/kafe/fork )</li>
<li>Create your feature branch (<tt>git checkout -b my-new-feature</tt>)</li>
<li>Commit your changes (<tt>git commit -am 'Add some feature'</tt>)</li>
<li>Push to the branch (<tt>git push origin my-new-feature</tt>)</li>
<li>Create a new Pull Request</li>
</ol>
<h3>Licence</h3>
<p>
kafe is available for use under the following license, commonly known as the 3-clause (or "modified") BSD license:
</p>
<p>
Copyright (c) 2014, 2015 Finexkap<br />
Copyright (c) 2015, 2016, 2017 BotsUnit<br />
Copyright (c) 2015, 2016, 2017, 2018 G-Corp<br />
</p>
<p>
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
</p>
<p>
<ul>
<li>Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.</li>
<li>Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.</li>
<li>The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.</li>
</ul>
</p>
<p>
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
</p>