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
// 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.
//! Utilities to activate dataflows based on external triggers.
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::{Arc, Mutex};
use timely::dataflow::Scope;
use timely::scheduling::{Activator, SyncActivator};
/// Generic activator behavior
pub trait ActivatorTrait {
/// Trigger an activation of operators behind this activator.
fn activate(&self);
/// Acknowledge the receipt of activations from within an operator.
fn ack(&self);
/// Register a new operator with its path with this activator.
fn register<S: Scope>(&self, scope: &mut S, path: &[usize]);
}
/// An shared handle to multiple activators with support for triggering and acknowledging
/// activations.
///
/// Activations are only triggered once the `activate` function has been called at least `threshold`
/// times, and then not again until `ack` is called. This way, the [RcActivator] ensures two
/// properties:
/// * It does not enqueue more than one activation per activator, if there is only one activator
/// registered with this [RcActivator]. Once multiple activators are registered, any ack will
/// enable more activations.
/// * The threshold to activation avoids activations purely caused by previous activations. Each
/// scheduling of a logging dataflow potentially creates additional log data, which needs to be
/// processed. The threshold should ensure that multiple activations under no load cause the
/// dataflow to be scheduled. For Materialize's log dataflows, this number seems to be larger than
/// 32, below we might risk that we do not cause monotonically decreasing work. A value of 64 or
/// larger is recommended, as there is no harm in bigger values. The log dataflow will still pick
/// up all its inputs once every introspection interval, and this activator only creates
/// additional activations.
#[derive(Debug, Clone)]
pub struct RcActivator {
inner: Rc<RefCell<ActivatorInner>>,
}
impl RcActivator {
/// Construct a new [RcActivator] with the given name and threshold.
///
/// The threshold determines now many activations to ignore until scheduling the activation.
pub fn new(name: String, threshold: usize) -> Self {
let inner = ActivatorInner::new(name, threshold);
Self {
inner: Rc::new(RefCell::new(inner)),
}
}
/// Register an additional [Activator] with this [RcActivator].
pub fn register(&self, activator: Activator) {
self.inner.borrow_mut().register(activator)
}
/// Activate all contained activators.
///
/// The implementation is free to ignore activations and only release them once a sufficient
/// volume has been accumulated.
pub fn activate(&self) {
self.inner.borrow_mut().activate()
}
/// Acknowledge the activation, which enables new activations to be scheduled.
pub fn ack(&self) {
self.inner.borrow_mut().ack()
}
}
impl ActivatorTrait for RcActivator {
fn activate(&self) {
self.activate()
}
fn ack(&self) {
self.ack()
}
fn register<S: Scope>(&self, scope: &mut S, path: &[usize]) {
self.register(scope.activator_for(path))
}
}
#[derive(Debug)]
struct ActivatorInner {
activated: usize,
activators: Vec<Activator>,
_name: String,
threshold: usize,
}
impl ActivatorInner {
fn new(name: String, threshold: usize) -> Self {
Self {
_name: name,
threshold,
activated: 0,
activators: Vec::new(),
}
}
fn register(&mut self, activator: Activator) {
self.activators.push(activator)
}
fn activate(&mut self) {
if self.activators.is_empty() {
return;
}
self.activated += 1;
if self.activated == self.threshold {
for activator in &self.activators {
activator.activate();
}
}
}
fn ack(&mut self) {
self.activated = 0;
}
}
/// An shared handle to multiple activators with support for triggering and acknowledging
/// activations.
///
/// See the documentation of [RcActivator] for details. This implementation differs in that it
/// provides a thread-safe activation mechanism.
#[derive(Debug, Clone)]
pub struct ArcActivator {
inner: Arc<Mutex<ArcActivatorInner>>,
}
impl ArcActivator {
/// Construct a new [ArcActivator] with the given name and threshold.
///
/// The threshold determines now many activations to ignore until scheduling the activation.
pub fn new(name: String, threshold: usize) -> Self {
let inner = ArcActivatorInner::new(name, threshold);
Self {
inner: Arc::new(Mutex::new(inner)),
}
}
/// Register an additional [Activator] with this [ArcActivator].
pub fn register(&self, activator: SyncActivator) {
self.inner.lock().unwrap().register(activator)
}
/// Activate all contained activators.
///
/// The implementation is free to ignore activations and only release them once a sufficient
/// volume has been accumulated.
pub fn activate(&self) {
self.inner.lock().unwrap().activate()
}
/// Acknowledge the activation, which enables new activations to be scheduled.
pub fn ack(&self) {
self.inner.lock().unwrap().ack()
}
}
impl ActivatorTrait for ArcActivator {
fn activate(&self) {
self.activate()
}
fn ack(&self) {
self.ack()
}
fn register<S: Scope>(&self, scope: &mut S, path: &[usize]) {
self.register(scope.sync_activator_for(path))
}
}
#[derive(Debug)]
struct ArcActivatorInner {
activated: usize,
activators: Vec<SyncActivator>,
_name: String,
threshold: usize,
}
impl ArcActivatorInner {
fn new(name: String, threshold: usize) -> Self {
Self {
_name: name,
threshold,
activated: 0,
activators: Vec::new(),
}
}
fn register(&mut self, activator: SyncActivator) {
self.activators.push(activator)
}
fn activate(&mut self) {
if self.activators.is_empty() {
return;
}
self.activated += 1;
if self.activated == self.threshold {
for activator in &self.activators {
activator.activate().unwrap();
}
}
}
fn ack(&mut self) {
self.activated = 0;
}
}