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
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

//! Implementation of real-time recency.

use std::cmp::Reverse;
use std::collections::binary_heap::PeekMut;
use std::collections::BinaryHeap;

use differential_dataflow::lattice::Lattice;
use mz_ore::now::EpochMillis;
use mz_persist_client::read::{ListenEvent, Subscribe};
use mz_persist_types::Codec64;
use mz_repr::{GlobalId, Row};
use mz_storage_types::configuration::StorageConfiguration;
use mz_storage_types::sources::{
    GenericSourceConnection, SourceConnection, SourceData, SourceTimestamp,
};
use mz_timely_util::antichain::AntichainExt;
use timely::order::TotalOrder;
use timely::progress::frontier::MutableAntichain;
use timely::progress::Antichain;
use timely::PartialOrder;

use crate::StorageError;
use crate::Timestamp;

/// Determine the "real-time recency" timestamp for `self`.
///
/// Real-time recency is defined as the minimum value of `T` that `id` can
/// be queried at to return all data visible in the upstream system the
/// query was issued. In this case, "the upstream system" is the external
/// system which `self` connects to.
///
/// # Panics
/// - If `self` is a [`GenericSourceConnection::LoadGenerator`]. Load
///   generator sources do not yet (or might never) support real-time
///   recency. You can avoid this panic by choosing to not call this
///   function on load generator sources.
pub(super) async fn real_time_recency_ts<
    T: Timestamp + Lattice + TotalOrder + Codec64 + From<EpochMillis> + Sync,
>(
    connection: GenericSourceConnection,
    id: GlobalId,
    config: StorageConfiguration,
    as_of: Antichain<T>,
    remap_subscribe: Subscribe<SourceData, (), T, mz_repr::Diff>,
) -> Result<T, StorageError<T>> {
    match connection {
        GenericSourceConnection::Kafka(kafka) => {
            let external_frontier = kafka
                .fetch_write_frontier(&config)
                .await
                .map_err(StorageError::Generic)?;

            decode_remap_data_until_geq_external_frontier(
                id,
                external_frontier,
                as_of,
                remap_subscribe,
            )
            .await
        }
        GenericSourceConnection::Postgres(pg) => {
            let external_frontier = pg
                .fetch_write_frontier(&config)
                .await
                .map_err(StorageError::Generic)?;

            decode_remap_data_until_geq_external_frontier(
                id,
                external_frontier,
                as_of,
                remap_subscribe,
            )
            .await
        }
        GenericSourceConnection::MySql(my_sql) => {
            let external_frontier = my_sql
                .fetch_write_frontier(&config)
                .await
                .map_err(StorageError::Generic)?;

            decode_remap_data_until_geq_external_frontier(
                id,
                external_frontier,
                as_of,
                remap_subscribe,
            )
            .await
        }
        // Load generator sources have no "external system" to reach out to,
        // so it's unclear what RTR would mean for them.
        s @ GenericSourceConnection::LoadGenerator(_) => unreachable!(
            "do not try to determine RTR timestamp on {} source",
            s.name()
        ),
    }
}

async fn decode_remap_data_until_geq_external_frontier<
    FromTime: SourceTimestamp,
    T: Timestamp + Lattice + TotalOrder + Codec64 + From<EpochMillis> + Sync,
>(
    id: GlobalId,
    external_frontier: timely::progress::Antichain<FromTime>,
    as_of: Antichain<T>,
    mut remap_subscribe: Subscribe<SourceData, (), T, mz_repr::Diff>,
) -> Result<T, StorageError<T>> {
    tracing::debug!(
        ?id,
        "fetched real time recency frontier: {}",
        external_frontier.pretty()
    );

    let external_frontier = external_frontier.borrow();
    let mut native_upper = MutableAntichain::new();

    let mut remap_frontier = Antichain::from_elem(T::minimum());
    let mut pending_remap = BinaryHeap::new();

    let mut min_ts = T::minimum();
    min_ts.advance_by(as_of.borrow());
    let mut min_ts = Some(min_ts);

    // First we must read the snapshot so that we can accumulate the minimum timestamp
    // correctly.
    while !remap_frontier.is_empty() {
        // Receive binding updates
        for event in remap_subscribe.fetch_next().await {
            match event {
                ListenEvent::Updates(updates) => {
                    for ((k, v), into_ts, diff) in updates {
                        let row: Row = k.expect("invalid binding").0.expect("invalid binding");
                        let _v: () = v.expect("invalid binding");

                        let from_ts: FromTime = SourceTimestamp::decode_row(&row);
                        pending_remap.push(Reverse((into_ts, from_ts, diff)));
                    }
                }

                ListenEvent::Progress(frontier) => remap_frontier = frontier,
            }
        }
        // Only process the data if we can correctly accumulate timestamps beyond the as_of
        if as_of.iter().any(|t| !remap_frontier.less_equal(t)) {
            loop {
                // The next timestamp that must be considered is either the minimum timestamp,
                // if we haven't yet checked it, or the next timestamp of the pending remap
                // updates that is not beyond the frontier.
                let binding_ts = match min_ts.take() {
                    Some(min_ts) => min_ts,
                    None => {
                        let Some(Reverse((into_ts, _, _))) = pending_remap.peek() else {
                            break;
                        };
                        if !remap_frontier.less_equal(into_ts) {
                            into_ts.clone()
                        } else {
                            break;
                        }
                    }
                };
                // Create an iterator with all the updates that are less than or equal to
                // binding_ts
                let binding_updates = std::iter::from_fn(|| {
                    let update = pending_remap.peek_mut()?;
                    if PartialOrder::less_equal(&update.0 .0, &binding_ts) {
                        let Reverse((_, from_ts, diff)) = PeekMut::pop(update);
                        Some((from_ts, diff))
                    } else {
                        None
                    }
                });
                native_upper.update_iter(binding_updates);

                // Check if at binding_ts the source contained all of the data visible in the
                // external system. If so, we're done.
                if PartialOrder::less_equal(&external_frontier, &native_upper.frontier()) {
                    tracing::trace!("real-time recency ts for {id}: {binding_ts:?}");
                    return Ok(binding_ts);
                }
            }
        }
    }
    // Collection dropped before we ingested external frontier.
    Err(StorageError::RtrDropFailure(id))
}