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
//! Reading/writing events from/to Apache Kafka servers.

use crate::fluentd::{Entry, ForwardMode};
use crate::Error;
use kafka::consumer::{Consumer, FetchOffset, GroupOffsetStorage};
use kafka::producer::{Producer, Record, RequiredAcks};
use rmp_serde::Serializer;
use serde::Serialize;
use std::convert::TryInto;

/// An event included in a Kafka message at `loc`.
#[derive(Debug)]
pub struct Event {
    pub entry: Entry,
    pub loc: EntryLocation,
}

impl crate::Event for Event {
    type Ack = EntryLocation;

    fn raw(&self) -> &[u8] {
        self.entry
            .record
            .get("message")
            .map_or(b"", |v| v.as_slice())
    }

    fn time(&self) -> u64 {
        self.entry.time
    }

    fn ack(&self) -> Self::Ack {
        self.loc
    }
}

#[derive(Copy, Clone, Debug)]
pub struct EntryLocation {
    remainder: u32, // # of entries in the message after this entry
    partition: i32,
    offset: i64,
}

/// Event reader for Apache Kafka.
pub struct Input {
    data_channel: Option<crossbeam_channel::Sender<Event>>,
    ack_channel: crossbeam_channel::Receiver<EntryLocation>,
    consumer: Consumer,
    fetch_limit: usize,
}

impl Input {
    /// Creates `Input` that fetches at most `fetch_limit` entries from the
    /// given Kafka topic.
    pub fn new(
        data_channel: crossbeam_channel::Sender<Event>,
        ack_channel: crossbeam_channel::Receiver<EntryLocation>,
        hosts: Vec<String>,
        group: String,
        client_id: String,
        topic: String,
        fetch_limit: usize,
    ) -> Result<Self, kafka::Error> {
        let consumer = Consumer::from_hosts(hosts)
            .with_group(group)
            .with_fallback_offset(FetchOffset::Earliest)
            .with_fetch_max_bytes_per_partition(1_000_000)
            .with_offset_storage(GroupOffsetStorage::Kafka)
            .with_client_id(client_id)
            .with_topic(topic)
            .create()?;
        Ok(Self {
            data_channel: Some(data_channel),
            ack_channel,
            consumer,
            fetch_limit,
        })
    }
}

impl super::Input for Input {
    type Data = Event;
    type Ack = EntryLocation;

    fn run(mut self) -> Result<(), Error> {
        let data_channel = if let Some(channel) = &self.data_channel {
            channel
        } else {
            return Err(Error::ChannelClosed);
        };

        let mut sel = crossbeam_channel::Select::new();
        let send_data = sel.send(data_channel);
        let recv_ack = sel.recv(&self.ack_channel);

        'poll: loop {
            let messagesets = self
                .consumer
                .poll()
                .map_err(|e| Error::CannotFetch(Box::new(e)))?;
            if messagesets.is_empty() {
                break 'poll;
            }
            for msgset in messagesets.iter() {
                let partition = msgset.partition();
                for msg in msgset.messages() {
                    let fwd_msg: ForwardMode = rmp_serde::from_slice(msg.value)
                        .map_err(|e| Error::InvalidMessage(Box::new(e)))?;
                    if fwd_msg.entries.len() > u32::max_value() as usize {
                        return Err(Error::TooManyEvents(fwd_msg.entries.len()));
                    }
                    let (remaining, overflow) =
                        self.fetch_limit.overflowing_sub(fwd_msg.entries.len());
                    if overflow {
                        break 'poll;
                    } else {
                        self.fetch_limit = remaining;
                    }
                    let offset = msg.offset;
                    for (remainder, entry) in (0..fwd_msg.entries.len()).rev().zip(fwd_msg.entries)
                    {
                        loop {
                            let oper = sel.select();
                            match oper.index() {
                                i if i == send_data => {
                                    let event = Event {
                                        entry,
                                        loc: EntryLocation {
                                            remainder: remainder
                                                .try_into()
                                                .expect("remainder <= u32::max_values()"),
                                            partition,
                                            offset,
                                        },
                                    };
                                    if oper.send(data_channel, event).is_err() {
                                        // data_channel was disconnected. Exit the
                                        // loop and commit consumed.
                                        break 'poll;
                                    }
                                    break;
                                }
                                i if i == recv_ack => {
                                    let ack = if let Ok(ack) = oper.recv(&self.ack_channel) {
                                        ack
                                    } else {
                                        // ack_channel was disconnected. Exit the
                                        // loop and commit consumed.
                                        break 'poll;
                                    };
                                    handle_ack(
                                        &self.ack_channel,
                                        &mut self.consumer,
                                        msgset.topic(),
                                        &ack,
                                    )?;
                                }
                                _ => unreachable!(),
                            }
                        }
                    }
                }
            }
        }
        self.data_channel = None;
        let subs = self.consumer.subscriptions();
        let topic = subs.keys().next().expect("subscribes to one topic");
        for ack in &self.ack_channel {
            handle_ack(&self.ack_channel, &mut self.consumer, topic, &ack)?;
        }
        Ok(())
    }
}

fn handle_ack(
    ack_channel: &crossbeam_channel::Receiver<EntryLocation>,
    consumer: &mut Consumer,
    topic: &str,
    ack: &EntryLocation,
) -> Result<(), Error> {
    if ack.remainder == 0 {
        consumer
            .consume_message(topic, ack.partition, ack.offset)
            .map_err(|_| Error::Fatal("messages from Kafka have different topics".into()))?;
    }
    if ack_channel.is_empty() {
        consumer
            .commit_consumed()
            .map_err(|e| Error::CannotCommit(Box::new(e)))?;
    }
    Ok(())
}

/// Event writer for Apache Kafka.
pub struct Output<T> {
    data_channel: crossbeam_channel::Receiver<T>,
    producer: Producer,
    topic: String,
}

impl<T> Output<T>
where
    T: std::fmt::Debug + Into<ForwardMode> + Serialize,
{
    pub fn new(
        data_channel: crossbeam_channel::Receiver<T>,
        hosts: Vec<String>,
        topic: String,
    ) -> Result<Self, kafka::Error> {
        let producer = Producer::from_hosts(hosts)
            .with_required_acks(RequiredAcks::One)
            .create()?;
        Ok(Self {
            data_channel,
            producer,
            topic,
        })
    }

    pub fn run(&mut self) -> Result<(), kafka::Error> {
        let mut buf = Vec::new();
        for msg in self.data_channel.iter() {
            msg.serialize(&mut Serializer::new(&mut buf)).map_err(|e| {
                kafka::Error::from_kind(kafka::error::ErrorKind::Msg(format!(
                    "cannot serialize: {}",
                    e
                )))
            })?;
            self.producer
                .send(&Record::from_value(&self.topic, buf.as_slice()))?;
            buf.clear();
        }
        Ok(())
    }
}