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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
use std::ffi::{CStr, CString};
use std::fmt;
use std::marker::PhantomData;
use std::os::raw::c_void;
use std::ptr;
use std::str;
use std::time::SystemTime;
use rdkafka_sys as rdsys;
use rdkafka_sys::types::*;
use crate::error::{IsError, KafkaError, KafkaResult};
use crate::util::{self, millis_to_epoch, KafkaDrop, NativePtr};
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Timestamp {
NotAvailable,
CreateTime(i64),
LogAppendTime(i64),
}
impl Timestamp {
pub fn to_millis(self) -> Option<i64> {
match self {
Timestamp::NotAvailable | Timestamp::CreateTime(-1) | Timestamp::LogAppendTime(-1) => {
None
}
Timestamp::CreateTime(t) | Timestamp::LogAppendTime(t) => Some(t),
}
}
pub fn now() -> Timestamp {
Timestamp::from(SystemTime::now())
}
}
impl From<i64> for Timestamp {
fn from(system_time: i64) -> Timestamp {
Timestamp::CreateTime(system_time)
}
}
impl From<SystemTime> for Timestamp {
fn from(system_time: SystemTime) -> Timestamp {
Timestamp::CreateTime(millis_to_epoch(system_time))
}
}
pub trait Headers {
fn count(&self) -> usize;
fn get(&self, idx: usize) -> Option<(&str, &[u8])>;
fn get_as<V: FromBytes + ?Sized>(&self, idx: usize) -> Option<(&str, Result<&V, V::Error>)> {
self.get(idx)
.map(|(name, value)| (name, V::from_bytes(value)))
}
}
pub trait Message {
type Headers: Headers;
fn key(&self) -> Option<&[u8]>;
fn payload(&self) -> Option<&[u8]>;
unsafe fn payload_mut(&mut self) -> Option<&mut [u8]>;
fn topic(&self) -> &str;
fn partition(&self) -> i32;
fn offset(&self) -> i64;
fn timestamp(&self) -> Timestamp;
fn payload_view<P: ?Sized + FromBytes>(&self) -> Option<Result<&P, P::Error>> {
self.payload().map(P::from_bytes)
}
fn key_view<K: ?Sized + FromBytes>(&self) -> Option<Result<&K, K::Error>> {
self.key().map(K::from_bytes)
}
fn headers(&self) -> Option<&Self::Headers>;
}
pub struct BorrowedHeaders;
impl BorrowedHeaders {
unsafe fn from_native_ptr<T>(
_owner: &T,
headers_ptr: *mut rdsys::rd_kafka_headers_t,
) -> &BorrowedHeaders {
&*(headers_ptr as *mut BorrowedHeaders)
}
fn as_native_ptr(&self) -> *const RDKafkaHeaders {
self as *const BorrowedHeaders as *const RDKafkaHeaders
}
pub fn detach(&self) -> OwnedHeaders {
OwnedHeaders {
ptr: unsafe {
NativePtr::from_ptr(rdsys::rd_kafka_headers_copy(self.as_native_ptr())).unwrap()
},
}
}
}
impl Headers for BorrowedHeaders {
fn count(&self) -> usize {
unsafe { rdsys::rd_kafka_header_cnt(self.as_native_ptr()) }
}
fn get(&self, idx: usize) -> Option<(&str, &[u8])> {
let mut value_ptr = ptr::null();
let mut name_ptr = ptr::null();
let mut value_size = 0;
let err = unsafe {
rdsys::rd_kafka_header_get_all(
self.as_native_ptr(),
idx,
&mut name_ptr,
&mut value_ptr,
&mut value_size,
)
};
if err.is_error() {
None
} else {
unsafe {
Some((
CStr::from_ptr(name_ptr).to_str().unwrap(),
util::ptr_to_slice(value_ptr, value_size),
))
}
}
}
}
pub struct BorrowedMessage<'a> {
ptr: NativePtr<RDKafkaMessage>,
_owner: PhantomData<&'a u8>,
}
unsafe impl KafkaDrop for RDKafkaMessage {
const TYPE: &'static str = "message";
const DROP: unsafe extern "C" fn(*mut Self) = rdsys::rd_kafka_message_destroy;
}
impl<'a> fmt::Debug for BorrowedMessage<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Message {{ ptr: {:?} }}", self.ptr())
}
}
impl<'a> BorrowedMessage<'a> {
pub(crate) unsafe fn from_consumer<C>(
ptr: NativePtr<RDKafkaMessage>,
_consumer: &'a C,
) -> KafkaResult<BorrowedMessage<'a>> {
if ptr.err.is_error() {
let err = match ptr.err {
rdsys::rd_kafka_resp_err_t::RD_KAFKA_RESP_ERR__PARTITION_EOF => {
KafkaError::PartitionEOF((*ptr).partition)
}
e => KafkaError::MessageConsumption(e.into()),
};
Err(err)
} else {
Ok(BorrowedMessage {
ptr,
_owner: PhantomData,
})
}
}
pub(crate) unsafe fn from_dr_callback<O>(
ptr: *mut RDKafkaMessage,
_owner: &'a O,
) -> DeliveryResult<'a> {
let borrowed_message = BorrowedMessage {
ptr: NativePtr::from_ptr(ptr).unwrap(),
_owner: PhantomData,
};
if (*ptr).err.is_error() {
Err((
KafkaError::MessageProduction((*ptr).err.into()),
borrowed_message,
))
} else {
Ok(borrowed_message)
}
}
pub fn ptr(&self) -> *mut RDKafkaMessage {
self.ptr.ptr()
}
pub fn topic_ptr(&self) -> *mut RDKafkaTopic {
self.ptr.rkt
}
pub fn key_len(&self) -> usize {
self.ptr.key_len
}
pub fn payload_len(&self) -> usize {
self.ptr.len
}
pub fn detach(&self) -> OwnedMessage {
OwnedMessage {
key: self.key().map(|k| k.to_vec()),
payload: self.payload().map(|p| p.to_vec()),
topic: self.topic().to_owned(),
timestamp: self.timestamp(),
partition: self.partition(),
offset: self.offset(),
headers: self.headers().map(BorrowedHeaders::detach),
}
}
}
impl<'a> Message for BorrowedMessage<'a> {
type Headers = BorrowedHeaders;
fn key(&self) -> Option<&[u8]> {
unsafe { util::ptr_to_opt_slice((*self.ptr).key, (*self.ptr).key_len) }
}
fn payload(&self) -> Option<&[u8]> {
unsafe { util::ptr_to_opt_slice((*self.ptr).payload, (*self.ptr).len) }
}
unsafe fn payload_mut(&mut self) -> Option<&mut [u8]> {
util::ptr_to_opt_mut_slice((*self.ptr).payload, (*self.ptr).len)
}
fn topic(&self) -> &str {
unsafe {
CStr::from_ptr(rdsys::rd_kafka_topic_name((*self.ptr).rkt))
.to_str()
.expect("Topic name is not valid UTF-8")
}
}
fn partition(&self) -> i32 {
self.ptr.partition
}
fn offset(&self) -> i64 {
self.ptr.offset
}
fn timestamp(&self) -> Timestamp {
let mut timestamp_type = rdsys::rd_kafka_timestamp_type_t::RD_KAFKA_TIMESTAMP_NOT_AVAILABLE;
let timestamp =
unsafe { rdsys::rd_kafka_message_timestamp(self.ptr.ptr(), &mut timestamp_type) };
if timestamp == -1 {
Timestamp::NotAvailable
} else {
match timestamp_type {
rdsys::rd_kafka_timestamp_type_t::RD_KAFKA_TIMESTAMP_NOT_AVAILABLE => {
Timestamp::NotAvailable
}
rdsys::rd_kafka_timestamp_type_t::RD_KAFKA_TIMESTAMP_CREATE_TIME => {
Timestamp::CreateTime(timestamp)
}
rdsys::rd_kafka_timestamp_type_t::RD_KAFKA_TIMESTAMP_LOG_APPEND_TIME => {
Timestamp::LogAppendTime(timestamp)
}
}
}
}
fn headers(&self) -> Option<&BorrowedHeaders> {
let mut native_headers_ptr = ptr::null_mut();
unsafe {
let err = rdsys::rd_kafka_message_headers(self.ptr.ptr(), &mut native_headers_ptr);
match err.into() {
RDKafkaErrorCode::NoError => {
Some(BorrowedHeaders::from_native_ptr(self, native_headers_ptr))
}
RDKafkaErrorCode::NoEnt => None,
_ => None,
}
}
}
}
unsafe impl<'a> Send for BorrowedMessage<'a> {}
unsafe impl<'a> Sync for BorrowedMessage<'a> {}
#[derive(Debug)]
pub struct OwnedHeaders {
ptr: NativePtr<RDKafkaHeaders>,
}
unsafe impl KafkaDrop for RDKafkaHeaders {
const TYPE: &'static str = "headers";
const DROP: unsafe extern "C" fn(*mut Self) = rdsys::rd_kafka_headers_destroy;
}
unsafe impl Send for OwnedHeaders {}
unsafe impl Sync for OwnedHeaders {}
impl OwnedHeaders {
pub fn new() -> OwnedHeaders {
OwnedHeaders::new_with_capacity(5)
}
pub fn new_with_capacity(initial_capacity: usize) -> OwnedHeaders {
OwnedHeaders {
ptr: unsafe {
NativePtr::from_ptr(rdsys::rd_kafka_headers_new(initial_capacity)).unwrap()
},
}
}
pub fn add<V: ToBytes + ?Sized>(self, name: &str, value: &V) -> OwnedHeaders {
let name_cstring = CString::new(name.to_owned()).unwrap();
let value_bytes = value.to_bytes();
let err = unsafe {
rdsys::rd_kafka_header_add(
self.ptr(),
name_cstring.as_ptr(),
name_cstring.as_bytes().len() as isize,
value_bytes.as_ptr() as *mut c_void,
value_bytes.len() as isize,
)
};
assert!(!err.is_error());
self
}
pub(crate) fn ptr(&self) -> *mut RDKafkaHeaders {
self.ptr.ptr()
}
pub fn as_borrowed(&self) -> &BorrowedHeaders {
unsafe { &*(self.ptr() as *mut RDKafkaHeaders as *mut BorrowedHeaders) }
}
}
impl Default for OwnedHeaders {
fn default() -> OwnedHeaders {
OwnedHeaders::new()
}
}
impl Headers for OwnedHeaders {
fn count(&self) -> usize {
unsafe { rdsys::rd_kafka_header_cnt(self.ptr()) }
}
fn get(&self, idx: usize) -> Option<(&str, &[u8])> {
self.as_borrowed().get(idx)
}
}
impl Clone for OwnedHeaders {
fn clone(&self) -> Self {
OwnedHeaders {
ptr: unsafe { NativePtr::from_ptr(rdsys::rd_kafka_headers_copy(self.ptr())).unwrap() },
}
}
}
#[derive(Debug, Clone)]
pub struct OwnedMessage {
payload: Option<Vec<u8>>,
key: Option<Vec<u8>>,
topic: String,
timestamp: Timestamp,
partition: i32,
offset: i64,
headers: Option<OwnedHeaders>,
}
impl OwnedMessage {
pub fn new(
payload: Option<Vec<u8>>,
key: Option<Vec<u8>>,
topic: String,
timestamp: Timestamp,
partition: i32,
offset: i64,
headers: Option<OwnedHeaders>,
) -> OwnedMessage {
OwnedMessage {
payload,
key,
topic,
timestamp,
partition,
offset,
headers,
}
}
}
impl Message for OwnedMessage {
type Headers = OwnedHeaders;
fn key(&self) -> Option<&[u8]> {
match self.key {
Some(ref k) => Some(k.as_slice()),
None => None,
}
}
fn payload(&self) -> Option<&[u8]> {
self.payload.as_deref()
}
unsafe fn payload_mut(&mut self) -> Option<&mut [u8]> {
self.payload.as_deref_mut()
}
fn topic(&self) -> &str {
self.topic.as_ref()
}
fn partition(&self) -> i32 {
self.partition
}
fn offset(&self) -> i64 {
self.offset
}
fn timestamp(&self) -> Timestamp {
self.timestamp
}
fn headers(&self) -> Option<&OwnedHeaders> {
self.headers.as_ref()
}
}
pub type DeliveryResult<'a> = Result<BorrowedMessage<'a>, (KafkaError, BorrowedMessage<'a>)>;
pub trait FromBytes {
type Error;
fn from_bytes(_: &[u8]) -> Result<&Self, Self::Error>;
}
impl FromBytes for [u8] {
type Error = ();
fn from_bytes(bytes: &[u8]) -> Result<&Self, Self::Error> {
Ok(bytes)
}
}
impl FromBytes for str {
type Error = str::Utf8Error;
fn from_bytes(bytes: &[u8]) -> Result<&Self, Self::Error> {
str::from_utf8(bytes)
}
}
pub trait ToBytes {
fn to_bytes(&self) -> &[u8];
}
impl ToBytes for [u8] {
fn to_bytes(&self) -> &[u8] {
self
}
}
impl ToBytes for str {
fn to_bytes(&self) -> &[u8] {
self.as_bytes()
}
}
impl ToBytes for Vec<u8> {
fn to_bytes(&self) -> &[u8] {
self.as_slice()
}
}
impl ToBytes for String {
fn to_bytes(&self) -> &[u8] {
self.as_bytes()
}
}
impl<'a, T: ToBytes> ToBytes for &'a T {
fn to_bytes(&self) -> &[u8] {
(*self).to_bytes()
}
}
impl ToBytes for () {
fn to_bytes(&self) -> &[u8] {
&[]
}
}
macro_rules! array_impls {
($($N:expr)+) => {
$(
impl ToBytes for [u8; $N] {
fn to_bytes(&self) -> &[u8] { self }
}
)+
}
}
array_impls! {
0 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
}
#[cfg(test)]
mod test {
use super::*;
use std::time::SystemTime;
#[test]
fn test_timestamp_creation() {
let now = SystemTime::now();
let t1 = Timestamp::now();
let t2 = Timestamp::from(now);
let expected = Timestamp::CreateTime(util::millis_to_epoch(now));
assert_eq!(t2, expected);
assert!(t1.to_millis().unwrap() - t2.to_millis().unwrap() < 10);
}
#[test]
fn test_timestamp_conversion() {
assert_eq!(Timestamp::CreateTime(100).to_millis(), Some(100));
assert_eq!(Timestamp::LogAppendTime(100).to_millis(), Some(100));
assert_eq!(Timestamp::CreateTime(-1).to_millis(), None);
assert_eq!(Timestamp::LogAppendTime(-1).to_millis(), None);
assert_eq!(Timestamp::NotAvailable.to_millis(), None);
let t: Timestamp = 100.into();
assert_eq!(t, Timestamp::CreateTime(100));
}
#[test]
fn test_headers() {
let owned = OwnedHeaders::new()
.add("key1", "value1")
.add("key2", "value2");
assert_eq!(
owned.get(0),
Some(("key1", &[118, 97, 108, 117, 101, 49][..]))
);
assert_eq!(owned.get_as::<str>(1), Some(("key2", Ok("value2"))));
}
}