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.
910use std::num::TryFromIntError;
11use std::time::Duration;
1213use mz_repr::{Timestamp, TimestampManipulation};
14use mz_storage_types::read_policy::ReadPolicy;
15use serde::Serialize;
16use timely::progress::{Antichain, Timestamp as TimelyTimestamp};
1718/// `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;
2122pub const DEFAULT_LOGICAL_COMPACTION_WINDOW_DURATION: Duration =
23 Duration::from_millis(DEFAULT_LOGICAL_COMPACTION_WINDOW_MILLIS);
2425/// `DEFAULT_LOGICAL_COMPACTION_WINDOW` as an `EpochMillis` timestamp.
26const DEFAULT_LOGICAL_COMPACTION_WINDOW_TS: Timestamp =
27 Timestamp::new(DEFAULT_LOGICAL_COMPACTION_WINDOW_MILLIS);
2829/// 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);
3435// 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]
41Default,
42/// Disable compaction.
43DisableCompaction,
44/// Create a compaction window for a specified duration.
45Duration(Timestamp),
46}
4748impl CompactionWindow {
49pub fn lag_from(&self, from: Timestamp) -> Timestamp {
50let 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 }
5758/// Returns self as a Timestamp that can be used for comparisons.
59pub fn comparable_timestamp(&self) -> Timestamp {
60match self {
61 CompactionWindow::Default => DEFAULT_LOGICAL_COMPACTION_WINDOW_TS,
62 CompactionWindow::DisableCompaction => Timestamp::maximum(),
63 CompactionWindow::Duration(d) => *d,
64 }
65 }
66}
6768impl From<CompactionWindow> for ReadPolicy<Timestamp> {
69fn from(value: CompactionWindow) -> Self {
70let time = match value {
71 CompactionWindow::Default => DEFAULT_LOGICAL_COMPACTION_WINDOW_TS,
72 CompactionWindow::Duration(time) => time,
73 CompactionWindow::DisableCompaction => {
74return ReadPolicy::ValidFrom(Antichain::from_elem(Timestamp::minimum()));
75 }
76 };
77 ReadPolicy::lag_writes_by(time, SINCE_GRANULARITY)
78 }
79}
8081impl TryFrom<Duration> for CompactionWindow {
82type Error = TryFromIntError;
8384fn try_from(value: Duration) -> Result<Self, Self::Error> {
85Ok(Self::Duration(value.try_into()?))
86 }
87}