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
//! Implementation of the Kernel

use super::{BrokerEvent, StartOptions};
use crate::kernel::broker_event::{
    OutgoingCloudEventProcessed, RoutingResult, ScheduleInternalServer,
};
use crate::kernel::{CloudEventMessageRoutingId, ProcessingResult};
use crate::runtime::channel::{BoxedReceiver, BoxedSender};
use crate::runtime::InternalServerId;
use std::collections::HashMap;
use std::ops::Add;
use std::time::{Duration, SystemTime};

const ROUTER_ID: &str = "router";
const CONFIG_LOADER_ID: &str = "config_loader";
const ROUTING_TTL_MS: u64 = 100;

struct PendingDelivery {
    sender: InternalServerId,
    missing_receivers: Vec<InternalServerId>,
    ttl: SystemTime,
}

type Outboxes = HashMap<InternalServerId, BoxedSender>;
type PendingDeliveries = HashMap<CloudEventMessageRoutingId, PendingDelivery>;

fn clean_pending_deliveries(outboxes: &Outboxes, pending_deliveries: &mut PendingDeliveries) {
    let now = SystemTime::now();
    if pending_deliveries.len() > 0 {
        let to_remove: Vec<CloudEventMessageRoutingId> = {
            let dead_messages: HashMap<&CloudEventMessageRoutingId, &PendingDelivery> =
                pending_deliveries
                    .iter()
                    .filter(|(_, v)| v.ttl > now)
                    .collect();
            for (routing_id, data) in dead_messages.iter() {
                warn!("ttl exceeded for routing_id={}, will send back to receiver={} with  ProcessingResult::Timeout", routing_id, data.sender);
                outboxes
                    .get(&data.sender)
                    .unwrap()
                    .send(BrokerEvent::IncomingCloudEventProcessed(
                        (*routing_id).clone(),
                        ProcessingResult::Timeout,
                    ));
            }
            dead_messages.iter().map(|(k, _)| *k).cloned().collect()
        };
        for routing_id in to_remove {
            pending_deliveries.remove_entry(&routing_id);
        }
    }
}

fn process_routing_result(
    event: RoutingResult,
    outboxes: &mut Outboxes,
    pending_deliveries: &mut PendingDeliveries,
) {
    let RoutingResult {
        routing_id,
        routing,
        incoming_id: receiver_id,
        args,
        result,
    } = event;
    debug!(
        "received RoutingResult status={} for event_id={}",
        result, &routing_id
    );

    match result {
        ProcessingResult::Successful => {
            if routing.is_empty() {
                debug!("routing is empty - nothing to do; ack if needed");
                if args.delivery_guarantee.requires_acknowledgment() {
                    outboxes.get(&receiver_id).unwrap().send(
                        BrokerEvent::IncomingCloudEventProcessed(
                            routing_id,
                            ProcessingResult::Successful,
                        ),
                    );
                }
            } else {
                if args.delivery_guarantee.requires_acknowledgment() {
                    let missing_receivers: Vec<_> = routing
                        .iter()
                        .map(|event| event.destination_id.clone())
                        .collect();

                    clean_pending_deliveries(outboxes, pending_deliveries);
                    if pending_deliveries
                        .insert(
                            routing_id.clone(),
                            PendingDelivery {
                                sender: receiver_id,
                                missing_receivers,
                                ttl: SystemTime::now().add(Duration::from_millis(ROUTING_TTL_MS)),
                            },
                        )
                        .is_some()
                    {
                        error!(
                            "a routing for event_id={} already existed, the old one was overwritten",
                            &routing_id
                        );
                    }
                } else {
                    debug!("no acknowledgments needed for event_id={}", &routing_id)
                }

                for subevent in routing {
                    outboxes
                        .get(&subevent.destination_id)
                        .unwrap()
                        .send(BrokerEvent::OutgoingCloudEvent(subevent));
                }
                debug!("all routings sent for event_id={}", routing_id);
            }
        }
        s @ ProcessingResult::PermanentError
        | s @ ProcessingResult::TransientError
        | s @ ProcessingResult::Timeout => {
            if args.delivery_guarantee.requires_acknowledgment() {
                outboxes
                    .get(&receiver_id)
                    .unwrap()
                    .send(BrokerEvent::IncomingCloudEventProcessed(routing_id, s));
            }
        }
    }
}

fn process_outgoing_cloud_event_processed(
    event: OutgoingCloudEventProcessed,
    outboxes: &mut Outboxes,
    pending_deliveries: &mut PendingDeliveries,
) {
    let OutgoingCloudEventProcessed {
        routing_id,
        sender_id,
        result,
    } = event;
    debug!(
        "received OutgoingCloudEventProcessed from={} event_id={}",
        sender_id, routing_id
    );
    let mut resolved_missing_delivery = false;
    if let Some(delivery) = pending_deliveries.get_mut(&routing_id) {
        match result {
            ProcessingResult::Successful => {
                let size_before = delivery.missing_receivers.len();
                delivery.missing_receivers.retain(|i| !i.eq(&sender_id));
                let size = delivery.missing_receivers.len();
                if size == 0 {
                    debug!("delivery for event_id={} was successful (all out port processing were successful) -> ack to sender", routing_id);
                    outboxes.get(&delivery.sender).unwrap().send(
                        BrokerEvent::IncomingCloudEventProcessed(routing_id.clone(), result),
                    );
                    resolved_missing_delivery = true
                } else if size_before == size {
                    warn!("{} sent OutgoingCloudEventProcessed for event_id={}, but was not expected to send this", sender_id, routing_id);
                }
            }
            _ => {
                if delivery.missing_receivers.contains(&sender_id) {
                    debug!("delivery for event_id={} was NOT successful ({}) -> immediately notify the sender", routing_id, result);
                    outboxes.get(&delivery.sender).unwrap().send(
                        BrokerEvent::IncomingCloudEventProcessed(routing_id.clone(), result),
                    );
                    resolved_missing_delivery = true
                } else {
                    warn!("{} sent OutgoingCloudEventProcessed for event_id={}, but no response was expected", sender_id, routing_id);
                }
            }
        }
    } else {
        debug!("there was no pending delivery for event_id {}", routing_id);
    }

    if resolved_missing_delivery {
        if pending_deliveries.remove_entry(&routing_id).is_none() {
            warn!(
                "failed to delete pending_deliveries for event_id={}",
                routing_id
            );
        }
    }
}

fn process_broker_event(
    broker_event: BrokerEvent,
    outboxes: &mut Outboxes,
    number_of_servers: usize,
    pending_deliveries: &mut PendingDeliveries,
) {
    match broker_event {
        BrokerEvent::InternalServerScheduled(id, sender_to_server) => {
            init_internal_server(outboxes, number_of_servers, id, sender_to_server);
        }
        broker_event @ BrokerEvent::IncomingCloudEvent(_) => {
            outboxes
                .get(&String::from(ROUTER_ID))
                .unwrap()
                .send(broker_event) // if the router is not present: panic! we cant work without it
        }
        BrokerEvent::RoutingResult(event) => {
            process_routing_result(event, outboxes, pending_deliveries)
        }
        BrokerEvent::OutgoingCloudEventProcessed(event) => {
            process_outgoing_cloud_event_processed(event, outboxes, pending_deliveries)
        }
        BrokerEvent::ConfigUpdated(config, destionation_server_id) => {
            debug!(
                "received ConfigUpdated, forward to {}",
                destionation_server_id
            );
            outboxes
                .get(&destionation_server_id)
                .unwrap()
                .send(BrokerEvent::ConfigUpdated(config, destionation_server_id));
        }
        BrokerEvent::Batch(broker_events) => {
            for broker_event in broker_events.into_iter() {
                process_broker_event(
                    broker_event,
                    outboxes,
                    number_of_servers,
                    pending_deliveries,
                );
            }
        }
        BrokerEvent::HealthCheckRequest(event) => outboxes
            .get(event.destination_id.as_str())
            .unwrap()
            .send(BrokerEvent::HealthCheckRequest(event)),
        BrokerEvent::HealthCheckResponse(event) => outboxes
            .get(event.destination_id.as_str())
            .unwrap()
            .send(BrokerEvent::HealthCheckResponse(event)),
        broker_event => warn!("event {} not implemented", broker_event),
    }
}

fn init_internal_server(
    outboxes: &mut Outboxes,
    number_of_servers: usize,
    id: InternalServerId,
    sender_to_server: BoxedSender,
) {
    outboxes.insert(id, sender_to_server);
    if outboxes.len() == number_of_servers {
        for (_, outbox) in outboxes.iter() {
            outbox.send(BrokerEvent::Init);
        }
    }
}

/// The function that gets started from the scheduler.
/// It implements the Kernel.
pub fn kernel_start(
    start_options: StartOptions,
    inbox: BoxedReceiver,
    sender_to_scheduler: BoxedSender,
) {
    let mut outboxes = Outboxes::new();
    // old entries are deleted with clean_pending_deliveries() before new are inserted.
    // At the moment this is only done before a new event is created, if this should change with e.g. a job add a lock! as in 24bb886a37c187936d906a0df90a9e90a3cf4255
    let mut pending_deliveries = PendingDeliveries::new();

    sender_to_scheduler.send(BrokerEvent::ScheduleInternalServer(
        ScheduleInternalServer {
            id: String::from(ROUTER_ID),
            function: start_options.router,
        },
    ));
    sender_to_scheduler.send(BrokerEvent::ScheduleInternalServer(
        ScheduleInternalServer {
            id: String::from(CONFIG_LOADER_ID),
            function: start_options.config_loader,
        },
    ));

    for service in start_options.ports.iter() {
        sender_to_scheduler.send(BrokerEvent::ScheduleInternalServer(service.clone()));
    }
    let number_of_servers = 2 + start_options.ports.len(); // 2 = router + config_loader

    loop {
        let broker_event = inbox.receive();
        process_broker_event(
            broker_event,
            &mut outboxes,
            number_of_servers,
            &mut pending_deliveries,
        );
    }
}