1use std::{
40 cmp::Ordering,
41 collections::{BinaryHeap, HashMap},
42 num::NonZeroUsize,
43 ops::ControlFlow,
44 sync::{Arc, Weak},
45};
46
47use matrix_sdk_base::{locks::Mutex, task_monitor::TaskMonitor};
48use matrix_sdk_common::executor::{AbortOnDrop, JoinHandleExt as _, spawn};
49use ruma::OwnedRoomId;
50use tokio::sync::{mpsc, oneshot};
51use tokio_util::sync::{CancellationToken, DropGuard};
52use tracing::{debug, info, instrument, trace, warn};
53
54use super::{EventCacheInner, caches::pagination::BackPaginationOutcome};
55
56pub(crate) const BATCH_SIZE: u16 = 30;
58
59#[allow(dead_code)]
62#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
63pub(crate) enum Priority {
64 Low,
67 Normal,
70 High,
72}
73
74pub(crate) type StopCondition = Box<dyn FnMut(&BackPaginationOutcome) -> ControlFlow<()> + Send>;
82
83pub(crate) struct BackPaginationRequest {
86 pub room_id: OwnedRoomId,
88 pub priority: Priority,
90 pub stop: StopCondition,
92 pub batch_size: u16,
94 pub max_batches: Option<usize>,
96}
97
98#[cfg(not(tarpaulin_include))]
99impl std::fmt::Debug for BackPaginationRequest {
100 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
101 f.debug_struct("BackPaginationRequest")
102 .field("room_id", &self.room_id)
103 .field("priority", &self.priority)
104 .field("batch_size", &self.batch_size)
105 .field("max_batches", &self.max_batches)
106 .finish_non_exhaustive()
107 }
108}
109
110#[derive(Clone, Copy, Debug, PartialEq, Eq)]
112pub(crate) enum BackPaginationStopReason {
113 ReachedTimelineStart,
115 StopConditionMet,
117 BatchLimitReached,
120 NoDataAvailable,
123 Failed,
125 Cancelled,
127}
128
129#[allow(dead_code)]
131#[derive(Clone, Copy, Debug)]
132pub(crate) struct BackPaginationRunResult {
133 pub reason: BackPaginationStopReason,
135}
136
137type RequestCoalescingKey = (OwnedRoomId, Priority);
139
140pub(crate) struct BackPaginationHandle {
143 guard: Arc<DropGuard>,
146 #[allow(dead_code)]
148 completion: Option<oneshot::Receiver<BackPaginationRunResult>>,
149}
150
151#[cfg(not(tarpaulin_include))]
152impl std::fmt::Debug for BackPaginationHandle {
153 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
154 f.debug_struct("BackPaginationHandle").finish_non_exhaustive()
155 }
156}
157
158impl BackPaginationHandle {
159 pub(crate) fn detach(self) {
165 if let Some(guard) = Arc::into_inner(self.guard) {
166 guard.disarm();
167 }
168 }
169
170 #[allow(dead_code)]
172 pub(crate) async fn join(mut self) -> BackPaginationRunResult {
173 let cancelled = BackPaginationRunResult { reason: BackPaginationStopReason::Cancelled };
174 match self.completion.take() {
175 Some(completion) => completion.await.unwrap_or(cancelled),
176 None => cancelled,
177 }
178 }
179}
180
181#[derive(Clone)]
183pub struct BackPaginationQueue {
184 inner: Arc<BackPaginationQueueInner>,
185}
186
187struct BackPaginationQueueInner {
188 sender: mpsc::UnboundedSender<SchedulerEvent>,
189 cancellations: Mutex<HashMap<RequestCoalescingKey, SharedCancellation>>,
196 _task: matrix_sdk_base::task_monitor::BackgroundTaskHandle,
197}
198
199struct SharedCancellation {
201 token: CancellationToken,
203 guard: Weak<DropGuard>,
206}
207
208#[cfg(not(tarpaulin_include))]
209impl std::fmt::Debug for BackPaginationQueue {
210 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
211 f.debug_struct("BackPaginationQueue").finish_non_exhaustive()
212 }
213}
214
215impl BackPaginationQueue {
216 pub(super) fn new(
218 event_cache: Weak<EventCacheInner>,
219 max_concurrent: NonZeroUsize,
220 task_monitor: &TaskMonitor,
221 ) -> Self {
222 let (sender, receiver) = mpsc::unbounded_channel();
223
224 let task = task_monitor
228 .spawn_infinite_task(
229 "event_cache::back_pagination_queue",
230 scheduler(event_cache, receiver, sender.clone(), max_concurrent.get()),
231 )
232 .abort_on_drop();
233
234 Self {
235 inner: Arc::new(BackPaginationQueueInner {
236 sender,
237 cancellations: Mutex::new(HashMap::new()),
238 _task: task,
239 }),
240 }
241 }
242
243 pub(crate) fn enqueue(
247 &self,
248 request: BackPaginationRequest,
249 ) -> Result<BackPaginationHandle, BackPaginationQueueError> {
250 let key = (request.room_id.clone(), request.priority);
251 let (token, guard) = cancellation_for(&self.inner.cancellations, key);
252
253 let (completion_tx, completion_rx) = oneshot::channel();
254 let submitted = SubmittedRequest { request, token, completion: completion_tx };
255
256 self.inner
257 .sender
258 .send(SchedulerEvent::Submitted(submitted))
259 .map_err(|_| BackPaginationQueueError::ShutDown)?;
260
261 Ok(BackPaginationHandle { guard, completion: Some(completion_rx) })
262 }
263}
264
265fn cancellation_for(
272 cancellations: &Mutex<HashMap<RequestCoalescingKey, SharedCancellation>>,
273 key: RequestCoalescingKey,
274) -> (CancellationToken, Arc<DropGuard>) {
275 let mut cancellations = cancellations.lock();
276
277 if let Some(existing) = cancellations.get(&key)
278 && let Some(guard) = existing.guard.upgrade()
279 {
280 return (existing.token.clone(), guard);
281 }
282
283 cancellations.retain(|_, cancellation| cancellation.guard.strong_count() > 0);
285
286 let token = CancellationToken::new();
287 let guard = Arc::new(token.clone().drop_guard());
288 cancellations
289 .insert(key, SharedCancellation { token: token.clone(), guard: Arc::downgrade(&guard) });
290
291 (token, guard)
292}
293
294#[derive(Debug, thiserror::Error)]
296pub(crate) enum BackPaginationQueueError {
297 #[error("the back-pagination queue executor is not running")]
300 ShutDown,
301}
302
303enum SchedulerEvent {
306 Submitted(SubmittedRequest),
308 Finished(RequestCoalescingKey, BackPaginationRunResult),
310}
311
312struct SubmittedRequest {
315 request: BackPaginationRequest,
316 token: CancellationToken,
317 completion: oneshot::Sender<BackPaginationRunResult>,
318}
319
320struct PendingRequest {
323 request: BackPaginationRequest,
324 seq: u64,
326 token: CancellationToken,
327}
328
329impl PartialEq for PendingRequest {
330 fn eq(&self, other: &Self) -> bool {
331 self.request.priority == other.request.priority && self.seq == other.seq
332 }
333}
334
335impl Eq for PendingRequest {}
336
337impl PartialOrd for PendingRequest {
338 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
339 Some(self.cmp(other))
340 }
341}
342
343impl Ord for PendingRequest {
344 fn cmp(&self, other: &Self) -> Ordering {
345 self.request.priority.cmp(&other.request.priority).then_with(|| other.seq.cmp(&self.seq))
347 }
348}
349
350#[instrument(skip_all)]
353async fn scheduler(
354 event_cache: Weak<EventCacheInner>,
355 mut receiver: mpsc::UnboundedReceiver<SchedulerEvent>,
356 sender: mpsc::UnboundedSender<SchedulerEvent>,
357 max_concurrent: usize,
358) {
359 trace!("Spawning the back-pagination queue executor");
360
361 let mut pending_requests: BinaryHeap<PendingRequest> = BinaryHeap::new();
362 let mut active_requests: HashMap<OwnedRoomId, AbortOnDrop<()>> = HashMap::new();
366 let mut next_seq: u64 = 0;
367
368 let mut waiters: HashMap<RequestCoalescingKey, Vec<oneshot::Sender<BackPaginationRunResult>>> =
374 HashMap::new();
375
376 let mut events = Vec::with_capacity(max_concurrent);
379
380 loop {
381 schedule(
384 &event_cache,
385 &mut pending_requests,
386 &mut active_requests,
387 max_concurrent,
388 &sender,
389 );
390
391 if receiver.recv_many(&mut events, max_concurrent).await == 0 {
394 info!("Back-pagination queue channel closed, exiting");
395 break;
396 }
397
398 for event in events.drain(..) {
399 match event {
400 SchedulerEvent::Submitted(submitted) => {
401 let key = (submitted.request.room_id.clone(), submitted.request.priority);
402
403 if try_coalesce(&mut waiters, &key, submitted.completion) {
404 trace!(
405 room_id = %key.0,
406 priority = ?key.1,
407 "coalesced back-pagination request onto an existing run"
408 );
409 continue;
410 }
411
412 pending_requests.push(PendingRequest {
413 request: submitted.request,
414 seq: next_seq,
415 token: submitted.token,
416 });
417 next_seq += 1;
418 }
419
420 SchedulerEvent::Finished(key, result) => {
421 active_requests.remove(&key.0);
422 if let Some(senders) = waiters.remove(&key) {
425 for waiter in senders {
426 let _ = waiter.send(result);
427 }
428 }
429 }
430 }
431 }
432 }
433}
434
435fn try_coalesce(
444 waiters: &mut HashMap<RequestCoalescingKey, Vec<oneshot::Sender<BackPaginationRunResult>>>,
445 key: &RequestCoalescingKey,
446 completion: oneshot::Sender<BackPaginationRunResult>,
447) -> bool {
448 match waiters.get_mut(key) {
449 Some(existing) => {
450 existing.push(completion);
451 true
452 }
453 None => {
454 waiters.insert(key.to_owned(), vec![completion]);
455 false
456 }
457 }
458}
459
460fn schedule(
462 event_cache: &Weak<EventCacheInner>,
463 pending_requests: &mut BinaryHeap<PendingRequest>,
464 active_requests: &mut HashMap<OwnedRoomId, AbortOnDrop<()>>,
465 max_concurrent: usize,
466 sender: &mpsc::UnboundedSender<SchedulerEvent>,
467) {
468 for request in next_runnable(pending_requests, active_requests, max_concurrent) {
469 let key = (request.request.room_id.clone(), request.request.priority);
470
471 trace!(
472 room_id = %key.0,
473 priority = ?key.1,
474 active = active_requests.len(),
475 queued = pending_requests.len(),
476 "back-pagination scheduled"
477 );
478
479 let room_id = key.0.clone();
480 let event_cache = event_cache.clone();
481 let sender = sender.clone();
482 let task = spawn(async move {
483 let result = run_request(&event_cache, request.request, &request.token).await;
484 let _ = sender.send(SchedulerEvent::Finished(key, result));
487 });
488
489 active_requests.insert(room_id, task.abort_on_drop());
490 }
491}
492
493fn next_runnable<T>(
501 pending_requests: &mut BinaryHeap<PendingRequest>,
502 active_requests: &HashMap<OwnedRoomId, T>,
503 max_concurrent: usize,
504) -> Vec<PendingRequest> {
505 let mut picked: Vec<PendingRequest> = Vec::new();
506 let mut skipped = Vec::new();
507
508 while active_requests.len() + picked.len() < max_concurrent {
509 let Some(request) = pending_requests.pop() else {
510 break;
511 };
512
513 let room_id = &request.request.room_id;
514 if active_requests.contains_key(room_id)
515 || picked.iter().any(|other| other.request.room_id == *room_id)
516 {
517 skipped.push(request);
519 continue;
520 }
521
522 picked.push(request);
523 }
524
525 pending_requests.extend(skipped);
526
527 picked
528}
529
530#[instrument(skip_all, fields(room_id = %request.room_id, priority = ?request.priority))]
533async fn run_request(
534 event_cache: &Weak<EventCacheInner>,
535 mut request: BackPaginationRequest,
536 token: &CancellationToken,
537) -> BackPaginationRunResult {
538 if token.is_cancelled() {
540 return BackPaginationRunResult { reason: BackPaginationStopReason::Cancelled };
541 }
542
543 let pagination = {
546 let Some(inner) = event_cache.upgrade() else {
547 return BackPaginationRunResult { reason: BackPaginationStopReason::Cancelled };
548 };
549 match inner.all_caches_for_room(&request.room_id).await {
550 Ok(caches) => caches.room().pagination(),
551 Err(err) => {
552 warn!("no caches for room while back-paginating: {err}");
553 return BackPaginationRunResult { reason: BackPaginationStopReason::Failed };
554 }
555 }
556 };
557
558 let mut batches = 0usize;
559
560 let reason = loop {
561 if token.is_cancelled() {
562 break BackPaginationStopReason::Cancelled;
563 }
564
565 let outcome = match pagination.run_backwards_once(request.batch_size).await {
566 Ok(outcome) => outcome,
567 Err(err) => {
568 warn!("back-pagination failed: {err}");
569 break BackPaginationStopReason::Failed;
570 }
571 };
572
573 if (request.stop)(&outcome).is_break() {
576 break BackPaginationStopReason::StopConditionMet;
577 }
578
579 if outcome.reached_start {
580 break BackPaginationStopReason::ReachedTimelineStart;
581 }
582
583 if outcome.events.is_empty() {
584 break BackPaginationStopReason::NoDataAvailable;
585 }
586
587 batches += 1;
588 if let Some(max) = request.max_batches
589 && batches >= max
590 {
591 break BackPaginationStopReason::BatchLimitReached;
592 }
593 };
594
595 debug!(?reason, "back-pagination run finished");
596
597 BackPaginationRunResult { reason }
598}
599
600#[cfg(all(test, not(target_arch = "wasm32")))]
601mod tests {
602 use std::{collections::HashMap, ops::ControlFlow};
603
604 use matrix_sdk_base::locks::Mutex;
605 use ruma::room_id;
606
607 use super::{
608 BackPaginationRequest, PendingRequest, Priority, cancellation_for, next_runnable,
609 try_coalesce,
610 };
611
612 fn queued(room_id: ruma::OwnedRoomId, priority: Priority, seq: u64) -> PendingRequest {
614 PendingRequest {
615 request: BackPaginationRequest {
616 room_id,
617 priority,
618 stop: Box::new(|_| ControlFlow::Continue(())),
620 batch_size: 10,
621 max_batches: None,
622 },
623 seq,
624 token: tokio_util::sync::CancellationToken::new(),
625 }
626 }
627
628 #[test]
631 fn test_scheduling_priority_and_fifo() {
632 use std::collections::BinaryHeap;
633
634 let (a, b, c, d) = (room_id!("!a:e"), room_id!("!b:e"), room_id!("!c:e"), room_id!("!d:e"));
635
636 let mut pending_requests = BinaryHeap::new();
637 pending_requests.push(queued(a.to_owned(), Priority::Low, 0));
639 pending_requests.push(queued(b.to_owned(), Priority::High, 1));
640 pending_requests.push(queued(c.to_owned(), Priority::Normal, 2));
641 pending_requests.push(queued(d.to_owned(), Priority::High, 3));
642
643 let active_requests: HashMap<_, ()> = HashMap::new();
644 let picked: Vec<_> = next_runnable(&mut pending_requests, &active_requests, 10)
645 .into_iter()
646 .map(|r| r.request.room_id)
647 .collect();
648
649 assert_eq!(picked, vec![b.to_owned(), d.to_owned(), c.to_owned(), a.to_owned()]);
651 }
652
653 #[test]
655 fn test_scheduling_respects_concurrency_cap() {
656 use std::collections::BinaryHeap;
657
658 let mut pending_requests = BinaryHeap::new();
659 for (i, room) in [room_id!("!a:e"), room_id!("!b:e"), room_id!("!c:e")].iter().enumerate() {
660 pending_requests.push(queued((*room).to_owned(), Priority::Normal, i as u64));
661 }
662
663 let active_requests: HashMap<_, ()> = HashMap::new();
664 let picked = next_runnable(&mut pending_requests, &active_requests, 2);
665
666 assert_eq!(picked.len(), 2);
667 assert_eq!(pending_requests.len(), 1);
669 }
670
671 #[test]
674 fn test_scheduling_per_room_single_flight() {
675 use std::collections::BinaryHeap;
676
677 let (a, b) = (room_id!("!a:e"), room_id!("!b:e"));
678
679 let active_requests = HashMap::from([(a.to_owned(), ())]);
681
682 let mut pending_requests = BinaryHeap::new();
683 pending_requests.push(queued(a.to_owned(), Priority::High, 0)); pending_requests.push(queued(a.to_owned(), Priority::High, 1)); pending_requests.push(queued(b.to_owned(), Priority::Low, 2)); let picked: Vec<_> = next_runnable(&mut pending_requests, &active_requests, 10)
688 .into_iter()
689 .map(|r| r.request.room_id)
690 .collect();
691
692 assert_eq!(picked, vec![b.to_owned()]);
694 assert_eq!(pending_requests.len(), 2);
695 }
696
697 #[test]
700 fn test_cancellation_is_shared_per_key() {
701 let cancellations = Mutex::new(HashMap::new());
702 let key = (room_id!("!a:e").to_owned(), Priority::Normal);
703
704 let (first_token, first_guard) = cancellation_for(&cancellations, key.clone());
705 let (second_token, second_guard) = cancellation_for(&cancellations, key.clone());
706
707 assert!(!first_token.is_cancelled());
709 drop(first_guard);
710 assert!(!first_token.is_cancelled());
711 assert!(!second_token.is_cancelled());
712
713 drop(second_guard);
715 assert!(first_token.is_cancelled());
716 assert!(second_token.is_cancelled());
717
718 let (third_token, _third_guard) = cancellation_for(&cancellations, key);
721 assert!(!third_token.is_cancelled());
722
723 let other = (room_id!("!a:e").to_owned(), Priority::High);
725 let (other_token, other_guard) = cancellation_for(&cancellations, other);
726 drop(other_guard);
727 assert!(other_token.is_cancelled());
728 assert!(!third_token.is_cancelled());
729 }
730
731 #[test]
735 fn test_coalescing() {
736 let a = room_id!("!a:e");
737 let mut waiters = HashMap::new();
738
739 let completion = || tokio::sync::oneshot::channel().0;
740 let normal = (a.to_owned(), Priority::Normal);
741 let high = (a.to_owned(), Priority::High);
742
743 assert!(!try_coalesce(&mut waiters, &normal, completion()));
745 assert_eq!(waiters[&normal].len(), 1);
746
747 assert!(try_coalesce(&mut waiters, &normal, completion()));
749 assert_eq!(waiters[&normal].len(), 2);
750
751 assert!(!try_coalesce(&mut waiters, &high, completion()));
753 assert_eq!(waiters.len(), 2);
754 assert_eq!(waiters[&high].len(), 1);
755 }
756}