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
// 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.

//! A Timely Dataflow operator that turns a stream of keyed upserts into a stream of differential updates.

use std::collections::hash_map::Entry;
use std::collections::{BTreeMap, HashMap};
use std::fmt::Debug;
use std::hash::Hash;

use differential_dataflow::lattice::Lattice;
use differential_dataflow::Hashable;
use timely::dataflow::channels::pact::Exchange;
use timely::dataflow::operators::{Concat, Map, OkErr};
use timely::dataflow::operators::{Delay, Operator};
use timely::dataflow::{Scope, Stream};
use timely::progress::Antichain;

use crate::client::{StreamReadHandle, StreamWriteHandle};
use crate::operators::replay::Replay;
use crate::operators::split_ok_err;
use crate::operators::stream::Persist;
use crate::operators::stream::RetractUnsealed;

use persist_types::Codec;

/// Extension trait for [`Stream`].
pub trait PersistentUpsert<G, K: Codec, V: Codec, T> {
    /// Turns a stream of keyed upserts into a stream of differential updates and also persists
    /// upserts to the [`StreamWriteHandle`] given in `persist_config`.
    ///
    /// The first output stream is the stream of differential updates while the second output
    /// stream contains persistence errors.
    ///
    /// The input is a stream of `(Key, Option<Val>, timestamp)` tuples, where "timestamp"
    /// expresses a happens-before relationship and could, for example, be a Kafka offset.  The
    /// contents of the collection are defined key-by-key, where each optional value in sequence either
    /// replaces or removes the existing value, should it exist.
    ///
    /// The `as_of_frontier` indicates a frontier that can be used to compact input timestamps
    /// without affecting the results. We *should* apply it, both because it improves performance, and
    /// because potentially incorrect results are visible in sinks.
    ///
    /// This method is only implemented for totally ordered times, as we do not yet understand what
    /// a "sequence" of upserts would mean for partially ordered timestamps.
    ///
    /// There are important invariants that this method will maintain for the persisted collection
    /// pointed to by `persist_config`. Any other actor that interacts with it must also ensure them.
    /// The invariants only apply to consolidated data, that is when all diffs are summed up for a
    /// given key/value pair. The invariants are:
    ///
    ///  - Each update in the collection must have a diff of `1` or `0`. That is an update either
    ///  exists exactly once or it doesn't exist.
    ///
    ///  - For each key, there can only be one value that it maps to. That is, keys must be unique.
    ///
    /// **Note:** This does only persist upserts but not seal them. Use together with `seal()` to
    /// also seal the persistent collection.
    fn persistent_upsert(
        &self,
        name: &str,
        as_of_frontier: Antichain<u64>,
        persist_config: PersistentUpsertConfig<K, V>,
    ) -> (
        Stream<G, ((K, V), u64, isize)>,
        Stream<G, (String, u64, isize)>,
    )
    where
        G: Scope<Timestamp = u64>;
}

/// Persist configuration for persistent upsert.
#[derive(Debug, Clone)]
pub struct PersistentUpsertConfig<K: Codec, V: Codec> {
    /// The timestamp up to which which data should be read when restoring.
    upper_seal_ts: u64,

    /// [`StreamReadHandle`] for the collection that we should persist to.
    read_handle: StreamReadHandle<K, V>,

    /// [`StreamWriteHandle`] for the collection that we should persist to.
    pub write_handle: StreamWriteHandle<K, V>,
}

impl<K: Codec, V: Codec> PersistentUpsertConfig<K, V> {
    /// Creates a new [`PersistentUpsertConfig`] from the given parts.
    pub fn new(
        upper_seal_ts: u64,
        read_handle: StreamReadHandle<K, V>,
        write_handle: StreamWriteHandle<K, V>,
    ) -> Self {
        PersistentUpsertConfig {
            upper_seal_ts,
            read_handle,
            write_handle,
        }
    }
}

impl<G, K, V, T> PersistentUpsert<G, K, V, T> for Stream<G, (K, Option<V>, T)>
where
    G: Scope<Timestamp = u64>,
    K: timely::Data + timely::ExchangeData + Codec + Debug + Hash + Eq,
    V: timely::Data + timely::ExchangeData + Codec + Debug + Hash + Eq,
    T: timely::Data + timely::ExchangeData + Ord,
{
    fn persistent_upsert(
        &self,
        name: &str,
        as_of_frontier: Antichain<u64>,
        persist_config: PersistentUpsertConfig<K, V>,
    ) -> (
        Stream<G, ((K, V), u64, isize)>,
        Stream<G, (String, u64, isize)>,
    )
    where
        G: Scope<Timestamp = u64>,
    {
        let operator_name = format!("persistent_upsert({})", name);

        let (restored_upsert_oks, state_errs) = {
            let snapshot = persist_config.read_handle.snapshot();
            let (restored_oks, restored_errs) = self
                .scope()
                .replay(snapshot, &as_of_frontier)
                .ok_err(split_ok_err);
            let (restored_upsert_oks, retract_errs) = restored_oks.retract_unsealed(
                name,
                persist_config.write_handle.clone(),
                persist_config.upper_seal_ts,
            );
            let combined_errs = restored_errs.concat(&retract_errs);
            (restored_upsert_oks, combined_errs)
        };

        let mut differential_state_ingester = Some(DifferentialStateIngester::new());

        let upsert_as_of_frontier = as_of_frontier.clone();

        let new_upsert_oks = self.binary_frontier(
            &restored_upsert_oks,
            Exchange::new(move |(key, _value, _ts): &(K, Option<V>, T)| key.hashed()),
            Exchange::new(move |((key, _data), _ts, _diff): &((K, _), _, _)| key.hashed()),
            &operator_name.clone(),
            move |_cap, _info| {
                // This is a map of (time) -> (capability, ((key) -> (value with max offset))). This
                // is a BTreeMap because we want to ensure that if we receive (key1, value1, time
                // 5) and (key1, value2, time 7) that we send (key1, value1, time 5) before (key1,
                // value2, time 7).
                //
                // This is a staging area, where we group incoming updates by timestamp (the timely
                // timestamp) and disambiguate by the offset (also called "timestamp" above) if
                // necessary.
                let mut to_send = BTreeMap::<_, (_, HashMap<_, (Option<V>, T)>)>::new();

                // This is a map from key -> value. We store the latest value for a given key that
                // way we know what to retract if a new value with the same key comes along.
                let mut current_values = HashMap::new();

                let mut input_buffer = Vec::new();
                let mut state_input_buffer = Vec::new();

                move |input, state_input, output| {
                    state_input.for_each(|_time, data| {
                        data.swap(&mut state_input_buffer);
                        for state_update in state_input_buffer.drain(..) {
                            tracing::trace!(
                                "In {}, restored upsert: {:?}",
                                operator_name,
                                state_update
                            );

                            differential_state_ingester
                                .as_mut()
                                .expect("already finished ingesting")
                                .add_update(state_update);
                        }
                    });

                    // An empty frontier signals that we will never receive data from that input
                    // again because no-one upstream holds any capability anymore.
                    if differential_state_ingester.is_some()
                        && state_input.frontier.frontier().is_empty()
                    {
                        let initial_state = differential_state_ingester
                            .take()
                            .expect("already finished ingesting")
                            .finish();
                        current_values.extend(initial_state.into_iter());

                        tracing::trace!(
                            "In {}, initial (restored) upsert state: {:?}",
                            operator_name,
                            current_values.iter().take(10).collect::<Vec<_>>()
                        );
                    }

                    // Digest each input, reduce by presented timestamp.
                    input.for_each(|cap, data| {
                        data.swap(&mut input_buffer);
                        for (key, value, offset) in input_buffer.drain(..) {
                            let mut time = cap.time().clone();
                            time.advance_by(upsert_as_of_frontier.borrow());

                            let time_entries = &mut to_send
                                .entry(time)
                                .or_insert_with(|| (cap.delayed(&time), HashMap::new()))
                                .1;

                            let new_entry = (value, offset);

                            match time_entries.entry(key) {
                                Entry::Occupied(mut occupied) => {
                                    let existing_entry = occupied.get_mut();
                                    // If the time is equal, toss out the row with the lower
                                    // offset.
                                    if new_entry.1 > existing_entry.1 {
                                        *existing_entry = new_entry;
                                    }
                                }
                                Entry::Vacant(vacant) => {
                                    // We didn't yet see an entry with the same timestamp. We can
                                    // just insert and don't need to disambiguate by offset.
                                    vacant.insert(new_entry);
                                }
                            }
                        }
                    });

                    let mut removed_times = Vec::new();
                    for (time, (cap, map)) in to_send.iter_mut() {
                        if !input.frontier.less_equal(time)
                            && !state_input.frontier.less_equal(time)
                        {
                            let mut session = output.session(cap);
                            removed_times.push(time.clone());
                            for (key, (value, _offset)) in map.drain() {
                                let old_value = if let Some(new_value) = &value {
                                    current_values.insert(key.clone(), new_value.clone())
                                } else {
                                    current_values.remove(&key)
                                };
                                if let Some(old_value) = old_value {
                                    // Retract old value.
                                    session.give((
                                        (key.clone(), old_value),
                                        cap.time().clone(),
                                        -1,
                                    ));
                                }
                                if let Some(new_value) = value {
                                    // Give new value.
                                    session.give(((key, new_value), cap.time().clone(), 1));
                                }
                            }
                        } else {
                            // Because this is a BTreeMap, the rest of the times in the map will be
                            // greater than this time. So if the input_frontier is less than or
                            // equal to this time, it will be less than the times in the rest of
                            // the map.
                            break;
                        }
                    }

                    // Discard entries, which hold capabilities, for complete times.
                    for time in removed_times {
                        to_send.remove(&time);
                    }
                }
            },
        );

        let (new_upsert_oks, new_upsert_persist_errs) =
            new_upsert_oks.persist(name, persist_config.write_handle);

        // Also pull the timestamp of restored data up to the as_of_frontier. We are doing this in
        // two steps: first, we are modifying the timestamp in the data itself, then we're delaying
        // the timely timestamp. The latter will stash updates while they are not beyond the
        // frontier.
        let retime_as_of_frontier = as_of_frontier.clone();
        let restored_upsert_oks = restored_upsert_oks
            .map(move |(data, mut time, diff)| {
                time.advance_by(retime_as_of_frontier.borrow());
                (data, time, diff)
            })
            .delay_batch(move |time| {
                let mut time = *time;
                time.advance_by(as_of_frontier.borrow());
                time
            });

        (
            new_upsert_oks.concat(&restored_upsert_oks),
            new_upsert_persist_errs.concat(&state_errs),
        )
    }
}

/// Ingests differential updates, consolidates them, and emits a final `HashMap` that contains the
/// consolidated upsert state.
struct DifferentialStateIngester<K, V> {
    differential_state: HashMap<(K, V), isize>,
}

impl<K, V> DifferentialStateIngester<K, V>
where
    K: Hash + Eq + Clone + Debug,
    V: Hash + Eq + Debug,
{
    fn new() -> Self {
        DifferentialStateIngester {
            differential_state: HashMap::new(),
        }
    }

    fn add_update(&mut self, update: ((K, V), u64, isize)) {
        let ((k, v), _ts, diff) = update;

        *self.differential_state.entry((k, v)).or_default() += diff;
    }

    fn finish(mut self) -> HashMap<K, V> {
        self.differential_state.retain(|_k, diff| *diff > 0);

        let mut state = HashMap::new();

        for ((key, value), diff) in self.differential_state.into_iter() {
            // our state must be internally consistent
            assert!(diff == 1, "Diff for ({:?}, {:?}) is {}", key, value, diff);
            match state.insert(key.clone(), value) {
                None => (), // it's all good
                // we must be internally consistent: there can only be one value per key in the
                // consolidated state
                Some(old_value) => {
                    // try_insert() would be perfect here, because we could also report the key
                    // without cloning
                    panic!("Already have a value for key {:?}: {:?}", key, old_value)
                }
            }
        }

        state
    }
}

#[cfg(test)]
mod tests {
    // TODO(aljoscha): add tests
}