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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//! Kafka configuration
//!
//! This module contains the configuration for the Kafka protocol adapter.
use std::env;
use std::sync::Arc;
use crate::datastream::Datastream;
use crate::utils::get_env_var;
use crate::*;
/// Kafka config
///
/// ## Environment variables
/// See [ENV_VARIABLES.md](https://github.com/kpn-dsh/dsh-sdk-platform-rs/blob/main/dsh_sdk/ENV_VARIABLES.md) for more information
/// configuring the consmer via environment variables.
#[derive(Debug, Clone)]
pub struct KafkaConfig {
// Datastreams
datastream: Arc<Datastream>,
// Consumer specific config
enable_auto_commit: bool,
auto_offset_reset: String,
session_timeout: Option<i32>,
queued_buffering_max_messages_kbytes: Option<i32>,
// Producer specific config
batch_num_messages: Option<i32>,
queue_buffering_max_messages: Option<i32>,
queue_buffering_max_kbytes: Option<i32>,
queue_buffering_max_ms: Option<i32>,
}
impl KafkaConfig {
pub fn new(datastream: Option<Arc<Datastream>>) -> Self {
let datastream = datastream
.unwrap_or_else(|| Arc::new(Datastream::load_local_datastreams().unwrap_or_default()));
let enable_auto_commit = get_env_var(VAR_KAFKA_ENABLE_AUTO_COMMIT)
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(false);
let auto_offset_reset =
get_env_var(VAR_KAFKA_AUTO_OFFSET_RESET).unwrap_or("earliest".to_string());
let session_timeout = get_env_var(VAR_KAFKA_CONSUMER_SESSION_TIMEOUT_MS)
.ok()
.and_then(|v| v.parse().ok());
let queued_buffering_max_messages_kbytes =
get_env_var(VAR_KAFKA_CONSUMER_QUEUED_BUFFERING_MAX_MESSAGES_KBYTES)
.ok()
.and_then(|v| v.parse().ok());
let batch_num_messages = get_env_var(VAR_KAFKA_PRODUCER_BATCH_NUM_MESSAGES)
.ok()
.and_then(|v| v.parse().ok());
let queue_buffering_max_messages =
get_env_var(VAR_KAFKA_PRODUCER_QUEUE_BUFFERING_MAX_MESSAGES)
.ok()
.and_then(|v| v.parse().ok());
let queue_buffering_max_kbytes = get_env_var(VAR_KAFKA_PRODUCER_QUEUE_BUFFERING_MAX_KBYTES)
.ok()
.and_then(|v| v.parse().ok());
let queue_buffering_max_ms = get_env_var(VAR_KAFKA_PRODUCER_QUEUE_BUFFERING_MAX_MS)
.ok()
.and_then(|v| v.parse().ok());
Self {
datastream,
enable_auto_commit,
auto_offset_reset,
session_timeout,
queued_buffering_max_messages_kbytes,
batch_num_messages,
queue_buffering_max_messages,
queue_buffering_max_kbytes,
queue_buffering_max_ms,
}
}
/// Get the kafka config provided by DSH (datastreams.json)
///
/// This datastream is fetched at initialization of the config, and can not be updated during runtime.
pub fn datastream(&self) -> &Datastream {
self.datastream.as_ref()
}
/// Get the Kafka brokers.
///
/// ## Environment variable
/// You can set the following environment variable to overwrite the default value.
///
/// ### `KAFKA_BOOTSTRAP_SERVERS`
/// - Usage: Overwrite hostnames of brokers
/// - Default: Brokers based on datastreams
/// - Required: `false`
pub fn kafka_brokers(&self) -> String {
self.datastream().get_brokers_string()
}
/// Get the kafka_group_id
///
/// ## Environment variables
/// You can set the following environment variables to overwrite the default value.
///
/// ### `KAFKA_CONSUMER_GROUP_TYPE`
/// - Usage: Picks group_id based on type from datastreams
/// - Default: Shared
/// - Options: private, shared
/// - Required: `false`
///
/// ### `KAFKA_GROUP_ID`
/// - Usage: Custom group id
/// - Default: NA
/// - Required: `false`
/// - Remark: Overrules `KAFKA_CONSUMER_GROUP_TYPE`. Mandatory to start with tenant name. (will prefix tenant name automatically if not set)
pub fn group_id(&self) -> String {
// TODO: Stabilize this function to fetch it once and not every time
let tenant_name = Dsh::get().tenant_name();
if let Ok(group_id) = env::var(VAR_KAFKA_GROUP_ID) {
if !group_id.starts_with(tenant_name) {
format!("{}_{}", tenant_name, group_id)
} else {
group_id
}
} else {
self.datastream()
.get_group_id(crate::datastream::GroupType::from_env())
.unwrap_or(&format!("{}_CONSUMER", tenant_name))
.to_string()
}
}
/// Get the confifured kafka auto commit setinngs.
///
/// ## Environment variable
/// You can set the following environment variable to overwrite the default value.
///
/// ### `KAFKA_ENABLE_AUTO_COMMIT`
/// - Usage: Enable/Disable auto commit
/// - Default: `false`
/// - Required: `false`
/// - Options: `true`, `false`
pub fn enable_auto_commit(&self) -> bool {
self.enable_auto_commit
}
/// Get the kafka auto offset reset settings.
///
/// ## Environment variable
/// You can set the following environment variable to overwrite the default value.
///
/// ### `KAFKA_AUTO_OFFSET_RESET`
/// - Usage: Set the offset reset settings to start consuming from set option.
/// - Default: earliest
/// - Required: `false`
/// - Options: smallest, earliest, beginning, largest, latest, end
pub fn auto_offset_reset(&self) -> &str {
&self.auto_offset_reset
}
/// Session timeout in milliseconds for consuming messages
///
/// ## Environment variable
/// You can set the following environment variable to overwrite the default value.
///
/// ### `KAFKA_CONSUMER_SESSION_TIMEOUT_MS`
/// - Usage: Set the session timeout in milliseconds
/// - Default: LibRdKafka default
/// - Required: `false`
/// - Options: Any integer
pub fn session_timeout(&self) -> Option<i32> {
self.session_timeout
}
/// Queued buffering max messages kbytes while consiuming
///
/// ## Environment variable
/// You can set the following environment variable to overwrite the default value.
///
/// ### `KAFKA_CONSUMER_QUEUED_BUFFERING_MAX_MESSAGES_KBYTES`
/// - Usage: Set the queued buffering max messages kbytes
/// - Default: LibRdKafka default
/// - Required: `false`
/// - Options: Any integer
pub fn queued_buffering_max_messages_kbytes(&self) -> Option<i32> {
self.queued_buffering_max_messages_kbytes
}
/// Batch number of messages to be produced
///
/// ## Environment variable
/// You can set the following environment variable to overwrite the default value.
///
/// ### `KAFKA_PRODUCER_BATCH_NUM_MESSAGES`
/// - Usage: Set the batch number of messages to be produced
/// - Default: LibRdKafka default
/// - Required: `false`
/// - Options: Any integer
pub fn batch_num_messages(&self) -> Option<i32> {
self.batch_num_messages
}
/// Maximum number of messages allowed on the producer queue
///
/// ## Environment variable
/// You can set the following environment variable to overwrite the default value.
///
/// ### `KAFKA_PRODUCER_QUEUE_BUFFERING_MAX_MESSAGES`
/// - Usage: Set the maximum number of messages allowed on the producer queue
/// - Default: LibRdKafka default
/// - Required: `false`
/// - Options: Any integer
pub fn queue_buffering_max_messages(&self) -> Option<i32> {
self.queue_buffering_max_messages
}
/// Maximum total message size in KBYTES sum allowed on the producer queue
///
/// ## Environment variable
/// You can set the following environment variable to overwrite the default value.
///
/// ### `KAFKA_PRODUCER_QUEUE_BUFFERING_MAX_KBYTES`
/// - Usage: Set the maximum total message size in KBYTES sum allowed on the producer queue
/// - Default: LibRdKafka default
/// - Required: `false`
/// - Options: Any integer
pub fn queue_buffering_max_kbytes(&self) -> Option<i32> {
self.queue_buffering_max_kbytes
}
/// Delay in milliseconds to wait for messages in the producer queue to accumulate before sending in batch
///
/// ## Environment variable
/// You can set the following environment variable to overwrite the default value.
///
/// ### `KAFKA_PRODUCER_QUEUE_BUFFERING_MAX_MS`
/// - Usage: Set the delay in milliseconds to wait for messages in the producer queue to accumulate before sending in batch
/// - Default: LibRdKafka default
/// - Required: `false`
/// - Options: Any integer
pub fn queue_buffering_max_ms(&self) -> Option<i32> {
self.queue_buffering_max_ms
}
}
impl Default for KafkaConfig {
fn default() -> Self {
Self::new(None)
}
}
#[cfg(test)]
mod tests {
use super::*;
use serial_test::serial;
use std::env;
#[test]
#[serial(env_dependency)]
fn test_kafka_config() {
let consumer_config = KafkaConfig::new(None);
assert_eq!(consumer_config.enable_auto_commit(), false);
assert_eq!(consumer_config.auto_offset_reset(), "earliest");
assert_eq!(consumer_config.session_timeout(), None);
assert_eq!(consumer_config.queued_buffering_max_messages_kbytes(), None);
assert_eq!(consumer_config.batch_num_messages(), None);
assert_eq!(consumer_config.queue_buffering_max_messages(), None);
assert_eq!(consumer_config.queue_buffering_max_kbytes(), None);
assert_eq!(consumer_config.queue_buffering_max_ms(), None);
}
#[test]
#[serial(env_dependency)]
fn test_kafka_config_default() {
let consumer_config = KafkaConfig::default();
assert_eq!(consumer_config.enable_auto_commit(), false);
assert_eq!(consumer_config.auto_offset_reset(), "earliest");
assert_eq!(consumer_config.session_timeout(), None);
assert_eq!(consumer_config.queued_buffering_max_messages_kbytes(), None);
assert_eq!(consumer_config.batch_num_messages(), None);
assert_eq!(consumer_config.queue_buffering_max_messages(), None);
assert_eq!(consumer_config.queue_buffering_max_kbytes(), None);
assert_eq!(consumer_config.queue_buffering_max_ms(), None);
}
#[test]
#[serial(env_dependency)]
fn test_consumer_kafka_config_env() {
unsafe {
env::set_var(VAR_KAFKA_ENABLE_AUTO_COMMIT, "true");
env::set_var(VAR_KAFKA_AUTO_OFFSET_RESET, "latest");
env::set_var(VAR_KAFKA_CONSUMER_SESSION_TIMEOUT_MS, "1000");
env::set_var(
VAR_KAFKA_CONSUMER_QUEUED_BUFFERING_MAX_MESSAGES_KBYTES,
"1000",
);
let consumer_config = KafkaConfig::default();
assert_eq!(consumer_config.enable_auto_commit(), true);
assert_eq!(consumer_config.auto_offset_reset(), "latest");
assert_eq!(consumer_config.session_timeout(), Some(1000));
assert_eq!(
consumer_config.queued_buffering_max_messages_kbytes(),
Some(1000)
);
env::remove_var(VAR_KAFKA_ENABLE_AUTO_COMMIT);
env::remove_var(VAR_KAFKA_AUTO_OFFSET_RESET);
env::remove_var(VAR_KAFKA_CONSUMER_SESSION_TIMEOUT_MS);
env::remove_var(VAR_KAFKA_CONSUMER_QUEUED_BUFFERING_MAX_MESSAGES_KBYTES);
}
}
#[test]
#[serial(env_dependency)]
fn test_producer_kafka_config_env() {
unsafe {
env::set_var(VAR_KAFKA_PRODUCER_BATCH_NUM_MESSAGES, "1000");
env::set_var(VAR_KAFKA_PRODUCER_QUEUE_BUFFERING_MAX_MESSAGES, "1000");
env::set_var(VAR_KAFKA_PRODUCER_QUEUE_BUFFERING_MAX_KBYTES, "1000");
env::set_var(VAR_KAFKA_PRODUCER_QUEUE_BUFFERING_MAX_MS, "1000");
let producer_config = KafkaConfig::default();
assert_eq!(producer_config.batch_num_messages(), Some(1000));
assert_eq!(producer_config.queue_buffering_max_messages(), Some(1000));
assert_eq!(producer_config.queue_buffering_max_kbytes(), Some(1000));
assert_eq!(producer_config.queue_buffering_max_ms(), Some(1000));
env::remove_var(VAR_KAFKA_PRODUCER_BATCH_NUM_MESSAGES);
env::remove_var(VAR_KAFKA_PRODUCER_QUEUE_BUFFERING_MAX_MESSAGES);
env::remove_var(VAR_KAFKA_PRODUCER_QUEUE_BUFFERING_MAX_KBYTES);
env::remove_var(VAR_KAFKA_PRODUCER_QUEUE_BUFFERING_MAX_MS);
}
}
#[test]
#[serial(env_dependency)]
fn test_kafka_group_id() {
unsafe {
let config = KafkaConfig::default();
let dsh = Dsh::default();
assert_eq!(
config.group_id(),
config
.datastream()
.get_group_id(crate::datastream::GroupType::Shared(0))
.unwrap()
);
env::set_var(VAR_KAFKA_CONSUMER_GROUP_TYPE, "private");
assert_eq!(
config.group_id(),
config
.datastream()
.get_group_id(crate::datastream::GroupType::Private(0))
.unwrap()
);
env::set_var(VAR_KAFKA_CONSUMER_GROUP_TYPE, "shared");
assert_eq!(
config.group_id(),
config
.datastream()
.get_group_id(crate::datastream::GroupType::Shared(0))
.unwrap()
);
env::set_var(VAR_KAFKA_GROUP_ID, "test_group");
assert_eq!(
config.group_id(),
format!("{}_test_group", dsh.tenant_name())
);
env::set_var(
VAR_KAFKA_GROUP_ID,
format!("{}_test_group", dsh.tenant_name()),
);
assert_eq!(
config.group_id(),
format!("{}_test_group", dsh.tenant_name())
);
env::remove_var(VAR_KAFKA_CONSUMER_GROUP_TYPE);
assert_eq!(
config.group_id(),
format!("{}_test_group", dsh.tenant_name())
);
env::remove_var(VAR_KAFKA_GROUP_ID);
}
}
}