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

//! Logging dataflows for events generated by differential dataflow.

use std::collections::HashMap;
use std::time::Duration;

use differential_dataflow::collection::AsCollection;
use differential_dataflow::logging::DifferentialEvent;
use differential_dataflow::operators::arrange::arrangement::Arrange;
use expr::{permutation_for_arrangement, MirScalarExpr};
use timely::communication::Allocate;
use timely::dataflow::channels::pact::Pipeline;
use timely::dataflow::operators::capture::EventLink;
use timely::dataflow::operators::generic::builder_rc::OperatorBuilder;
use timely::logging::WorkerIdentifier;

use super::{DifferentialLog, LogVariant};
use crate::activator::RcActivator;
use crate::arrangement::manager::RowSpine;
use crate::arrangement::KeysValsHandle;
use crate::logging::ConsolidateBuffer;
use crate::replay::MzReplay;
use repr::{Datum, DatumVec, Row, Timestamp};

/// Constructs the logging dataflow for differential logs.
///
/// Params
/// * `worker`: The Timely worker hosting the log analysis dataflow.
/// * `config`: Logging configuration
/// * `linked`: The source to read log events from.
/// * `activator`: A handle to acknowledge activations.
///
/// Returns a map from log variant to a tuple of a trace handle and a permutation to reconstruct
/// the original rows.
pub fn construct<A: Allocate>(
    worker: &mut timely::worker::Worker<A>,
    config: &dataflow_types::logging::LoggingConfig,
    linked: std::rc::Rc<EventLink<Timestamp, (Duration, WorkerIdentifier, DifferentialEvent)>>,
    activator: RcActivator,
) -> HashMap<LogVariant, KeysValsHandle> {
    let granularity_ms = std::cmp::max(1, config.granularity_ns / 1_000_000) as Timestamp;

    let traces = worker.dataflow_named("Dataflow: differential logging", move |scope| {
        let logs = Some(linked).mz_replay(
            scope,
            "differential logs",
            Duration::from_nanos(config.granularity_ns as u64),
            activator,
        );

        let mut demux =
            OperatorBuilder::new("Differential Logging Demux".to_string(), scope.clone());

        let mut input = demux.new_input(&logs, Pipeline);

        let (mut arrangement_batches_out, arrangement_batches) = demux.new_output();
        let (mut arrangement_records_out, arrangement_records) = demux.new_output();
        let (mut sharing_out, sharing) = demux.new_output();
        let mut demux_buffer = Vec::new();
        demux.build(move |_capability| {
            move |_frontiers| {
                let arrangement_batches = arrangement_batches_out.activate();
                let arrangement_records = arrangement_records_out.activate();
                let sharing = sharing_out.activate();
                let mut arrangement_batches_session =
                    ConsolidateBuffer::new(arrangement_batches, 0);
                let mut arrangement_records_session =
                    ConsolidateBuffer::new(arrangement_records, 1);
                let mut sharing_session = ConsolidateBuffer::new(sharing, 2);

                input.for_each(|cap, data| {
                    data.swap(&mut demux_buffer);

                    for (time, worker, datum) in demux_buffer.drain(..) {
                        let time_ms = (((time.as_millis() as Timestamp / granularity_ms) + 1)
                            * granularity_ms) as Timestamp;

                        match datum {
                            DifferentialEvent::Batch(event) => {
                                arrangement_batches_session
                                    .give(&cap, ((event.operator, worker), time_ms, 1));
                                arrangement_records_session.give(
                                    &cap,
                                    ((event.operator, worker), time_ms, event.length as isize),
                                );
                            }
                            DifferentialEvent::Merge(event) => {
                                if let Some(done) = event.complete {
                                    arrangement_batches_session
                                        .give(&cap, ((event.operator, worker), time_ms, -1));
                                    let diff = (done as isize)
                                        - ((event.length1 + event.length2) as isize);
                                    arrangement_records_session
                                        .give(&cap, ((event.operator, worker), time_ms, diff));
                                }
                            }
                            DifferentialEvent::Drop(event) => {
                                arrangement_batches_session
                                    .give(&cap, ((event.operator, worker), time_ms, -1));
                                arrangement_records_session.give(
                                    &cap,
                                    ((event.operator, worker), time_ms, -(event.length as isize)),
                                );
                            }
                            DifferentialEvent::MergeShortfall(_) => {}
                            DifferentialEvent::TraceShare(event) => {
                                sharing_session
                                    .give(&cap, ((event.operator, worker), time_ms, event.diff));
                            }
                        }
                    }
                });
            }
        });

        let arrangement_batches = arrangement_batches.as_collection().map({
            move |(op, worker)| {
                Row::pack_slice(&[Datum::Int64(op as i64), Datum::Int64(worker as i64)])
            }
        });

        let arrangement_records = arrangement_records.as_collection().map({
            move |(op, worker)| {
                Row::pack_slice(&[Datum::Int64(op as i64), Datum::Int64(worker as i64)])
            }
        });

        // Duration statistics derive from the non-rounded event times.
        let sharing = sharing.as_collection().map({
            move |(op, worker)| {
                Row::pack_slice(&[Datum::Int64(op as i64), Datum::Int64(worker as i64)])
            }
        });

        let logs = vec![
            (
                LogVariant::Differential(DifferentialLog::ArrangementBatches),
                arrangement_batches,
            ),
            (
                LogVariant::Differential(DifferentialLog::ArrangementRecords),
                arrangement_records,
            ),
            (LogVariant::Differential(DifferentialLog::Sharing), sharing),
        ];

        let mut result = std::collections::HashMap::new();
        for (variant, collection) in logs {
            if config.active_logs.contains_key(&variant) {
                let key = variant.index_by();
                let (_, value) = permutation_for_arrangement::<HashMap<_, _>>(
                    &key.iter()
                        .cloned()
                        .map(MirScalarExpr::Column)
                        .collect::<Vec<_>>(),
                    variant.desc().arity(),
                );
                let trace = collection
                    .map({
                        let mut row_packer = Row::default();
                        let mut datums = DatumVec::new();
                        move |row| {
                            let datums = datums.borrow_with(&row);
                            row_packer.extend(key.iter().map(|k| datums[*k]));
                            let row_key = row_packer.finish_and_reuse();
                            row_packer.extend(value.iter().map(|c| datums[*c]));
                            (row_key, row_packer.finish_and_reuse())
                        }
                    })
                    .arrange_named::<RowSpine<_, _, _, _>>(&format!("ArrangeByKey {:?}", variant))
                    .trace;
                result.insert(variant, trace);
            }
        }
        result
    });

    traces
}