You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/KafkaConsumerQuickStart.md
+34-31Lines changed: 34 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,27 +6,27 @@ We'd recommend users to cross-reference them, --especially the examples.
6
6
7
7
Unlike Java's KafkaConsumer, here we introduced two derived classes, --KafkaAutoCommitConsumer and KafkaManualCommitConsumer, --depending on whether users should call `commit` manually.
8
8
9
-
## KafkaAutoCommitConsumer
9
+
## KafkaConsumer (`enable.auto.commit=true`)
10
10
11
-
*Friendly for users, --would not care about when to commit the offsets for these received messages.
11
+
*Automatically commits previously polled offsets on each `poll` (and the final `close`) operations.
12
12
13
-
* Internally, it would commit the offsets (for received records) within the next `poll` and the final `close`.
14
-
Note, each internal `commit` would "try its best", but "not guaranteed to succeed", -- it's supposed to be called periodically, thus occasional failure doesn't matter.
13
+
* Note, the internal `offset commit` is asynchronous, and is not guaranteed to succeed. It's supposed to be triggered (within each `poll` operation) periodically, thus the occasional failure doesn't quite matter.
auto records = consumer.poll(std::chrono::milliseconds(100));
@@ -48,17 +48,19 @@ Note, each internal `commit` would "try its best", but "not guaranteed to succee
48
48
}
49
49
}
50
50
}
51
+
52
+
// consumer.close(); // No explicit close is needed, RAII will take care of it
51
53
```
52
54
53
-
* `ConsumerConfig::BOOTSTRAP_SERVERS` is mandatory for `ConsumerConfig`.
55
+
* `bootstrap.servers` property is mandatory for a Kafka client.
54
56
55
-
* `subscribe` could take a topic list. And it's a blocking operation, -- would return after the rebalance event triggered callback was executed.
57
+
* `subscribe` could take a topic list. It's a block operation, would wait the consumer to get partitions assigned.
56
58
57
-
* `poll` must be periodically called, and it would trigger kinds of callback handling internally. As in this example, just put it in a "while loop" would be OK.
59
+
* `poll` must be called periodically, thus to trigger kinds of callback handling internally. In practice, it could be put in a "while loop".
58
60
59
-
* At the end, the user could `close` the consumer manually, or just leave it to the destructor (which would `close` anyway).
61
+
* At the end, we could `close` the consumer explicitly, or just leave it to the destructor.
60
62
61
-
## KafkaManualCommitConsumer
63
+
## KafkaConsumer (`enable.auto.commit=false`)
62
64
63
65
* Users must commit the offsets for received records manually.
64
66
@@ -69,15 +71,15 @@ Note, each internal `commit` would "try its best", but "not guaranteed to succee
69
71
{"bootstrap.servers", brokers},
70
72
});
71
73
72
-
// Create a consumer instance.
73
-
kafka::KafkaManualCommitConsumer consumer(props);
74
+
// Create a consumer instance
75
+
kafka::clients::KafkaConsumer consumer(props);
74
76
75
77
// Subscribe to topics
76
78
consumer.subscribe({topic});
77
79
78
80
auto lastTimeCommitted = std::chrono::steady_clock::now();
// consumer.close(); // No explicit close is needed, RAII will take care of it
121
125
```
122
126
123
127
* The example is quite similar with the KafkaAutoCommitConsumer, with only 1 more line added for manual-commit.
124
128
125
129
*`commitSync` and `commitAsync` are both available for a KafkaManualConsumer. Normally, use `commitSync` to guarantee the commitment, or use `commitAsync`(with `OffsetCommitCallback`) to get a better performance.
126
130
127
-
## KafkaManualCommitConsumer with `KafkaClient::EventsPollingOption::Manual`
131
+
## `KafkaConsumer` with `kafka::clients::KafkaClient::EventsPollingOption`
128
132
129
-
While we construct a `KafkaManualCommitConsumer` with option `KafkaClient::EventsPollingOption::AUTO` (default), an internal thread would be created for `OffsetCommit` callbacks handling.
133
+
While we construct a `KafkaConsumer` with `kafka::clients::KafkaClient::EventsPollingOption::Auto` (i.e. the default option), an internal thread would be created for `OffsetCommit` callbacks handling.
130
134
131
135
This might not be what you want, since then you have to use 2 different threads to process the messages and handle the `OffsetCommit` responses.
132
136
133
-
Here we have another choice, -- using `KafkaClient::EventsPollingOption::Manual`, thus the `OffsetCommit` callbacks would be called within member function `pollEvents()`.
137
+
Here we have another choice, -- using `kafka::clients::KafkaClient::EventsPollingOption::Manual`, thus the `OffsetCommit` callbacks would be called within member function `pollEvents()`.
@@ -156,17 +160,17 @@ Here we have another choice, -- using `KafkaClient::EventsPollingOption::Manual`
156
160
157
161
## Error handling
158
162
159
-
No exception would be thrown by `KafkaProducer::poll()`.
163
+
No exception would be thrown from a consumer's `poll` operation.
160
164
161
-
Once an error occurs, the `ErrorCode` would be embedded in the `Consumer::ConsumerRecord`.
165
+
Instead, once an error occurs, the `Error` would be embedded in the `Consumer::ConsumerRecord`.
162
166
163
-
There're 2 cases,
167
+
About `Error`'s `value()`s, there are 2 cases
164
168
165
169
1. Success
166
170
167
-
- RD_KAFKA_RESP_ERR__NO_ERROR (0), -- got a message successfully
171
+
- `RD_KAFKA_RESP_ERR__NO_ERROR` (`0`), -- got a message successfully
168
172
169
-
- RD_KAFKA_RESP_ERR__PARTITION_EOF, -- reached the end of a partition (no message got)
173
+
- `RD_KAFKA_RESP_ERR__PARTITION_EOF`, -- reached the end of a partition (no message got)
170
174
171
175
2. Failure
172
176
@@ -187,21 +191,20 @@ There're 2 cases,
187
191
188
192
* How many threads would be created by a KafkaConsumer?
189
193
190
-
Excluding the user's main thread, `KafkaAutoCommitConsumer` would start another (N + 2) threads in the background, while `KafkaManualConsumer` would start (N + 3) background threads. (N means the number of BOOTSTRAP_SERVERS)
194
+
Excluding the user's main thread, if `enable.auto.commit` is `false`, the `KafkaConsumer` would start another (N + 2) threads in the background; otherwise, the `KafkaConsumer` would start (N + 3) background threads. (N means the number of BOOTSTRAP_SERVERS)
191
195
192
196
1. Each broker (in the list of BOOTSTRAP_SERVERS) would take a seperate thread to transmit messages towards a kafka cluster server.
193
197
194
198
2. Another 3 threads will handle internal operations, consumer group operations, and kinds of timers, etc.
195
199
196
-
3. KafkaManualConsumer has one more thread, which keeps polling the offset-commit callback event.
200
+
3. To enable the auto commit, one more thread would be create, which keeps polling/processing the offset-commit callback event.
197
201
198
-
E.g, if a KafkaAutoCommitConsumer was created with property of `BOOTSTRAP_SERVERS=127.0.0.1:8888,127.0.0.1:8889,127.0.0.1:8890`, it would take 6 threads in total (including the main thread).
202
+
E.g, if a KafkaConsumer was created with property of `BOOTSTRAP_SERVERS=127.0.0.1:8888,127.0.0.1:8889,127.0.0.1:8890`, it would take 6 threads in total (including the main thread).
199
203
200
204
* Which one of these threads will handle the callbacks?
201
205
202
206
There are 2 kinds of callbacks for a KafkaConsumer,
203
207
204
208
1. `RebalanceCallback` will be triggered internally by the user's thread, -- within the `poll` function.
205
209
206
-
2. `OffsetCommitCallback` (only available for `KafkaManualCommitConsumer`) will be triggered by a background thread, not by the user's thread.
207
-
210
+
2. If `enable.auto.commit=true`, the `OffsetCommitCallback` will be triggered by the user's `poll` thread; otherwise, it would be triggered by a background thread.
// producer.close(); // No explicit close is needed, RAII will take care of it
45
53
```
46
54
47
55
* User must guarantee the memory block for `ProducerRecord`'s `key` is valid until being `send`.
@@ -50,40 +58,42 @@ We'd recommend users to cross-reference them, --especially the examples.
50
58
51
59
* It's guaranteed that the delivery callback would be triggered anyway after `send`, -- a producer would even be waiting for it before `close`. So, it's a good way to release these memory resources in the `Producer::Callback` function.
52
60
53
-
## `KafkaProducer` with `KafkaClient::EventsPollingOption::Manual`
61
+
## `KafkaProducer` with `kafka::clients::KafkaClient::EventsPollingOption`
54
62
55
-
While we construct a `KafkaProducer` with option `KafkaClient::EventsPollingOption::Auto` (default), an internal thread would be created for `MessageDelivery` callbacks handling.
63
+
While we construct a `KafkaProducer` with `kafka::clients::KafkaClient::EventsPollingOption::Auto` (the default option), an internal thread would be created for `MessageDelivery` callbacks handling.
56
64
57
65
This might not be what you want, since then you have to use 2 different threads to send the messages and handle the `MessageDelivery` responses.
58
66
59
-
Here we have another choice, -- using `KafkaClient::EventsPollingOption::Manual`, thus the `MessageDelivery` callbacks would be called within member function `pollEvents()`.
67
+
Here we have another choice, -- using `kafka::clients::KafkaClient::EventsPollingOption::Manual`, thus the `MessageDelivery` callbacks would be called within member function `pollEvents()`.
60
68
61
69
* Note, if you constructed the `KafkaProducer` with `EventsPollingOption::Manual`, the `send()` would be an `unblocked` operation.
62
70
I.e, once the `message buffering queue` becomes full, the `send()` operation would throw an exception (or return an `error code` with the input reference parameter), -- instead of blocking there.
63
71
This makes sense, since you might want to call `pollEvents()` later, thus delivery-callback could be called for some messages (which could then be removed from the `message buffering queue`).
0 commit comments