mz_adapter_types/
compaction.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10use std::num::TryFromIntError;
11use std::time::Duration;
12
13use mz_repr::{Timestamp, TimestampManipulation};
14use mz_storage_types::read_policy::ReadPolicy;
15use serde::Serialize;
16use timely::progress::{Antichain, Timestamp as TimelyTimestamp};
17
18/// `DEFAULT_LOGICAL_COMPACTION_WINDOW`, in milliseconds.
19/// The default is set to a second to track the default timestamp frequency for sources.
20const DEFAULT_LOGICAL_COMPACTION_WINDOW_MILLIS: u64 = 1000;
21
22pub const DEFAULT_LOGICAL_COMPACTION_WINDOW_DURATION: Duration =
23    Duration::from_millis(DEFAULT_LOGICAL_COMPACTION_WINDOW_MILLIS);
24
25/// `DEFAULT_LOGICAL_COMPACTION_WINDOW` as an `EpochMillis` timestamp.
26const DEFAULT_LOGICAL_COMPACTION_WINDOW_TS: Timestamp =
27    Timestamp::new(DEFAULT_LOGICAL_COMPACTION_WINDOW_MILLIS);
28
29/// The value to round all `since` frontiers to.
30/// We pick 1s somewhat arbitrarily, but matching historical practice.
31// TODO[btv] If we want to further reduce capability chatter, we can implement the design in
32// `20230322_metrics_since_granularity.md`, making it configurable.
33pub const SINCE_GRANULARITY: mz_repr::Timestamp = mz_repr::Timestamp::new(1000);
34
35// A common type (that is usable by the sql crate and also can implement various methods on types in
36// storage) to express compaction windows.
37#[derive(Clone, Default, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize)]
38pub enum CompactionWindow {
39    /// Unspecified by the user, use a system-provided default.
40    #[default]
41    Default,
42    /// Disable compaction.
43    DisableCompaction,
44    /// Create a compaction window for a specified duration.
45    Duration(Timestamp),
46}
47
48impl CompactionWindow {
49    pub fn lag_from(&self, from: Timestamp) -> Timestamp {
50        let lag = match self {
51            CompactionWindow::Default => DEFAULT_LOGICAL_COMPACTION_WINDOW_TS,
52            CompactionWindow::DisableCompaction => return Timestamp::minimum(),
53            CompactionWindow::Duration(d) => *d,
54        };
55        from.saturating_sub(lag)
56    }
57
58    /// Returns self as a Timestamp that can be used for comparisons.
59    pub fn comparable_timestamp(&self) -> Timestamp {
60        match self {
61            CompactionWindow::Default => DEFAULT_LOGICAL_COMPACTION_WINDOW_TS,
62            CompactionWindow::DisableCompaction => Timestamp::maximum(),
63            CompactionWindow::Duration(d) => *d,
64        }
65    }
66}
67
68impl From<CompactionWindow> for ReadPolicy<Timestamp> {
69    fn from(value: CompactionWindow) -> Self {
70        let time = match value {
71            CompactionWindow::Default => DEFAULT_LOGICAL_COMPACTION_WINDOW_TS,
72            CompactionWindow::Duration(time) => time,
73            CompactionWindow::DisableCompaction => {
74                return ReadPolicy::ValidFrom(Antichain::from_elem(Timestamp::minimum()));
75            }
76        };
77        ReadPolicy::lag_writes_by(time, SINCE_GRANULARITY)
78    }
79}
80
81impl TryFrom<Duration> for CompactionWindow {
82    type Error = TryFromIntError;
83
84    fn try_from(value: Duration) -> Result<Self, Self::Error> {
85        Ok(Self::Duration(value.try_into()?))
86    }
87}