plane 0.5.5

Session backend orchestrator for ambitious browser-based apps.
Documentation
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
use chrono::{DateTime, Utc};
use plane_common::{exponential_backoff::ExponentialBackoff, protocol::BackendMetricsMessage};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serde_json::Value;
use sqlx::{postgres::PgListener, PgConnection, PgPool};
use std::{
    any::Any,
    collections::HashMap,
    fmt::Debug,
    sync::{Arc, RwLock},
};
use tokio::{
    sync::broadcast::{Receiver, Sender},
    task::JoinHandle,
};

use crate::database::util::MapSqlxError;

type ListenerMap = Arc<RwLock<HashMap<(String, Option<String>), Box<dyn TypedSender>>>>;

pub const EVENT_CHANNEL: &str = "plane_events";
pub const BACKEND_METRICS_EVENT_CHANNEL: &str = "plane_backend_metrics";

pub trait NotificationPayload:
    Serialize + DeserializeOwned + Debug + Send + Sync + Clone + 'static
{
    fn kind() -> &'static str;
}

trait TypedSender: Send + Sync {
    fn send(&self, value: Notification<Value>);

    fn receiver_count(&self) -> usize;

    fn as_any(&self) -> &dyn Any;
}

impl<T: NotificationPayload> TypedSender for Sender<Notification<T>> {
    fn send(&self, value: Notification<Value>) {
        let payload: T = match serde_json::from_value(value.payload) {
            Ok(payload) => payload,
            Err(err) => {
                tracing::error!(?err, "Failed to deserialize notification payload.");
                return;
            }
        };

        let value = Notification {
            id: value.id,
            timestamp: value.timestamp,
            kind: value.kind,
            key: value.key,
            payload,
        };

        if let Err(err) = Sender::send(self, value) {
            tracing::error!(?err, "Failed to send notification.");
        }
    }

    fn receiver_count(&self) -> usize {
        Sender::receiver_count(self)
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

pub struct Subscription<T: Clone> {
    receiver: Option<Receiver<Notification<T>>>,
    table: ListenerMap,
    key: (String, Option<String>),
}

impl<T: Clone> Subscription<T> {
    pub async fn next(&mut self) -> Option<Notification<T>> {
        self.receiver
            .as_mut()
            .expect("Receiver can't be taken until subscription is dropped.")
            .recv()
            .await
            .ok()
    }
}

impl<T: Clone> Drop for Subscription<T> {
    fn drop(&mut self) {
        let receiver = self
            .receiver
            .take()
            .expect("Receiver can't be taken until dropped.");
        drop(receiver);

        let mut table = self.table.write().expect("Table lock is poisoned.");

        // If after dropping the receiver, there are no more receivers for this key, remove the
        // entry from the table.
        if let Some(sender) = table.get_mut(&self.key) {
            if sender.receiver_count() == 0 {
                table.remove(&self.key);
            }
        } else {
            tracing::warn!("Subscription dropped but no associated sender found in table.");
        }
    }
}

pub struct EventSubscriptionManager {
    all_events: Sender<Notification<Value>>,
    handle: JoinHandle<()>,

    /// Maps from (kind, optional_key) to a sender.
    listeners: ListenerMap,
}

impl Drop for EventSubscriptionManager {
    fn drop(&mut self) {
        self.handle.abort();
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Notification<T> {
    /// Optional id. If not present, the notification is ephemeral.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<i32>,

    pub timestamp: DateTime<Utc>,

    /// Rust type of the payload.
    pub kind: String,

    /// Optional key to identify the payload.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub key: Option<String>,

    /// The payload.
    pub payload: T,
}

impl EventSubscriptionManager {
    pub fn new(db: &PgPool) -> Self {
        let listeners: ListenerMap = Arc::new(RwLock::new(HashMap::new()));
        let all_events = Sender::new(1000);

        let handle = {
            let all_events = all_events.clone();
            let listeners = listeners.clone();
            let db = db.clone();

            tokio::spawn(async move {
                let mut backoff = ExponentialBackoff::default();
                let mut last_message: Option<i32> = None;

                let send_message = move |notification: Notification<Value>| {
                    if all_events.receiver_count() > 0 {
                        let _ = all_events.send(notification.clone());
                    }

                    let listeners = listeners.read().expect("Listener map is poisoned.");

                    // If the notification has a key, we send it both to the global listeners and
                    // the listeners for the specific key.
                    if let Some(key) = notification.key.as_ref() {
                        if let Some(sender) =
                            listeners.get(&(notification.kind.clone(), Some(key.clone())))
                        {
                            sender.send(notification.clone());
                        }
                    }

                    // Send the notification to the global listeners.
                    if let Some(sender) = listeners.get(&(notification.kind.clone(), None)) {
                        sender.send(notification);
                    }
                };

                'outer: loop {
                    let mut listener = match PgListener::connect_with(&db).await {
                        Ok(listener) => listener,
                        Err(err) => {
                            tracing::error!(?err, "Failed to connect to database.");
                            backoff.wait().await;
                            continue;
                        }
                    };

                    if let Err(err) = listener.listen(EVENT_CHANNEL).await {
                        tracing::error!(?err, "Failed to listen to event channel.");
                        backoff.wait().await;
                        continue;
                    }

                    if let Some(mut prev_last_message) = last_message {
                        'inner: loop {
                            let messages = match EventSubscriptionManager::get_events_since(
                                &db,
                                prev_last_message,
                            )
                            .await
                            {
                                Ok(messages) => messages,
                                Err(err) => {
                                    tracing::error!(
                                        ?err,
                                        "Failed to fetch messages from database."
                                    );
                                    backoff.wait().await;
                                    continue 'outer;
                                }
                            };

                            if messages.is_empty() {
                                break 'inner;
                            }

                            for message in messages {
                                send_message(message.clone());
                                last_message = message.id;
                                prev_last_message = message
                                    .id
                                    .expect("Expected message from database to have id.");
                            }
                        }
                    }

                    backoff.defer_reset();

                    while let Ok(Some(notification)) = listener.try_recv().await {
                        let notification: Notification<Value> =
                            match serde_json::from_str(notification.payload()) {
                                Ok(notification) => notification,
                                Err(err) => {
                                    tracing::error!(?err, "Failed to deserialize notification.");
                                    continue;
                                }
                            };

                        // It could happen that a message happens between when we open the listener
                        // and when we request messages from the DB. In that case, we don't want to
                        // send the message twice. If the message has no id, it's ephemeral and we
                        // should send it since it wouldn't have been in the DB anyway.
                        if let Some(notification_id) = notification.id {
                            if let Some(last_message) = last_message {
                                if notification_id <= last_message {
                                    continue;
                                }
                            }
                            last_message = Some(notification_id);
                        }
                        send_message(notification);
                    }

                    tracing::error!("Lost connection to database, reconnecting after wait.");
                    backoff.wait().await;
                }
            })
        };

        Self {
            all_events,
            handle,
            listeners,
        }
    }

    pub async fn clean_up_events(db: &PgPool, min_age_days: i32) -> Result<(), sqlx::Error> {
        sqlx::query!(
            r#"
            delete from event
            where created_at < now() - make_interval(days => $1)
            "#,
            min_age_days
        )
        .execute(db)
        .await?;

        Ok(())
    }

    pub async fn get_events_since(
        db: &PgPool,
        since: i32,
    ) -> Result<Vec<Notification<Value>>, sqlx::Error> {
        let messages = sqlx::query!(
            r#"
            select id, kind, key, data, created_at
            from event
            where id > $1
            order by id asc
            limit 100
            "#,
            since,
        )
        .fetch_all(db)
        .await?;

        Ok(messages
            .into_iter()
            .map(|message| Notification {
                id: Some(message.id),
                timestamp: message.created_at,
                kind: message.kind,
                key: message.key,
                payload: message.data,
            })
            .collect())
    }

    pub fn subscribe_all_events(&self) -> Receiver<Notification<Value>> {
        self.all_events.subscribe()
    }

    pub fn subscribe<T: NotificationPayload>(&self, key: Option<&str>) -> Subscription<T> {
        let kind = T::kind().to_string();

        let mut listeners = self.listeners.write().expect("Listener map is poisoned.");
        let key = (kind.clone(), key.map(|s| s.to_string()));

        match listeners.entry(key.clone()) {
            std::collections::hash_map::Entry::Occupied(entry) => {
                let sender = entry
                    .get()
                    .as_any()
                    .downcast_ref::<Sender<Notification<T>>>()
                    .expect(
                        "Sender is not of the expected type. \
                        This implies that two different types return the same value \
                        for NotificationPayload::kind(), which is not allowed.",
                    );
                Subscription {
                    receiver: Some(sender.subscribe()),
                    table: self.listeners.clone(),
                    key,
                }
            }
            std::collections::hash_map::Entry::Vacant(entry) => {
                let sender = Sender::new(1000);
                let receiver = sender.subscribe();
                entry.insert(Box::new(sender));
                Subscription {
                    receiver: Some(receiver),
                    table: self.listeners.clone(),
                    key,
                }
            }
        }
    }
}

pub async fn emit_impl<T: NotificationPayload>(
    db: &mut PgConnection,
    key: Option<&str>,
    payload: &T,
) -> Result<(), sqlx::Error> {
    let kind = T::kind().to_string();
    sqlx::query!(
        r#"
        with message_insert as (
            insert into event (kind, key, created_at, data)
            values ($1, $2, now(), $3)
            returning id
        )
        select pg_notify(
            $4,
            json_build_object(
                'payload', $3::jsonb,
                'timestamp', now(),
                'id', id,
                'kind', $1,
                'key', $2
            )::text
        ) from message_insert
        "#,
        kind,
        key,
        serde_json::to_value(&payload).map_sqlx_error()?,
        EVENT_CHANNEL,
    )
    .execute(&mut *db)
    .await?;

    Ok(())
}

pub async fn emit<T: NotificationPayload>(
    db: &mut PgConnection,
    payload: &T,
) -> Result<(), sqlx::Error> {
    emit_impl(db, None, payload).await
}

pub async fn emit_with_key<T: NotificationPayload>(
    db: &mut PgConnection,
    key: &str,
    payload: &T,
) -> Result<(), sqlx::Error> {
    emit_impl(db, Some(key), payload).await
}

pub async fn emit_backend_metrics(
    db: &mut PgConnection,
    key: &str,
    payload: &BackendMetricsMessage,
) -> Result<(), sqlx::Error> {
    sqlx::query!(
        r#"
        select pg_notify(
            $4,
            json_build_object(
                'payload', $3::jsonb,
                'timestamp', now(),
                'kind', $1::text,
                'key', $2::text
            )::text
        )"#,
        BackendMetricsMessage::kind().to_string(),
        key,
        serde_json::to_value(&payload).map_sqlx_error()?,
        BACKEND_METRICS_EVENT_CHANNEL,
    )
    .execute(db)
    .await?;

    Ok(())
}