|
| 1 | +// Copyright 2021-Present Datadog, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +//! Shared merge policy invariant checks. |
| 16 | +//! |
| 17 | +//! These pure functions are the single source of truth for merge operation |
| 18 | +//! validity, used by both Stateright models and production code. |
| 19 | +
|
| 20 | +/// MP-1: all splits in a merge operation must share the same `num_merge_ops`. |
| 21 | +/// |
| 22 | +/// If splits from different levels are merged, the output gets stamped with |
| 23 | +/// `max(levels) + 1`, prematurely maturing lower-level data and breaking |
| 24 | +/// the bounded write amplification guarantee. |
| 25 | +pub fn all_same_merge_level(num_merge_ops: &[u32]) -> bool { |
| 26 | + match num_merge_ops.first() { |
| 27 | + None => true, |
| 28 | + Some(&first) => num_merge_ops.iter().all(|&n| n == first), |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/// MP-2: every merge operation must have at least 2 input splits. |
| 33 | +/// |
| 34 | +/// Merging a single split is a no-op that wastes I/O. Merging zero splits |
| 35 | +/// is nonsensical. |
| 36 | +pub fn has_minimum_splits(count: usize) -> bool { |
| 37 | + count >= 2 |
| 38 | +} |
| 39 | + |
| 40 | +/// MP-3: all splits in a merge operation must share the same compaction scope. |
| 41 | +/// |
| 42 | +/// The scope is defined by `(sort_fields, window_start, window_duration)`. |
| 43 | +/// The merge engine validates that all inputs agree on these; a policy bug |
| 44 | +/// that groups incompatible splits will cause the merge to fail. |
| 45 | +/// |
| 46 | +/// `index_uid` and `partition_id` are also part of the scope but are |
| 47 | +/// typically enforced by the grouping layer before the policy runs. |
| 48 | +pub fn all_same_compaction_scope( |
| 49 | + sort_fields: &[&str], |
| 50 | + windows: &[(i64, i64)], // (start, duration) pairs |
| 51 | +) -> bool { |
| 52 | + let same_sort = match sort_fields.first() { |
| 53 | + None => true, |
| 54 | + Some(&first) => sort_fields.iter().all(|&s| s == first), |
| 55 | + }; |
| 56 | + let same_window = match windows.first() { |
| 57 | + None => true, |
| 58 | + Some(&first) => windows.iter().all(|w| *w == first), |
| 59 | + }; |
| 60 | + same_sort && same_window |
| 61 | +} |
| 62 | + |
| 63 | +#[cfg(test)] |
| 64 | +mod tests { |
| 65 | + use super::*; |
| 66 | + |
| 67 | + #[test] |
| 68 | + fn test_all_same_merge_level() { |
| 69 | + assert!(all_same_merge_level(&[])); |
| 70 | + assert!(all_same_merge_level(&[0])); |
| 71 | + assert!(all_same_merge_level(&[2, 2, 2])); |
| 72 | + assert!(!all_same_merge_level(&[0, 1])); |
| 73 | + assert!(!all_same_merge_level(&[0, 0, 1])); |
| 74 | + } |
| 75 | + |
| 76 | + #[test] |
| 77 | + fn test_has_minimum_splits() { |
| 78 | + assert!(!has_minimum_splits(0)); |
| 79 | + assert!(!has_minimum_splits(1)); |
| 80 | + assert!(has_minimum_splits(2)); |
| 81 | + assert!(has_minimum_splits(100)); |
| 82 | + } |
| 83 | + |
| 84 | + #[test] |
| 85 | + fn test_all_same_compaction_scope() { |
| 86 | + // Empty is vacuously true. |
| 87 | + assert!(all_same_compaction_scope(&[], &[])); |
| 88 | + |
| 89 | + // Same scope. |
| 90 | + assert!(all_same_compaction_scope( |
| 91 | + &["a|b|ts/V2", "a|b|ts/V2"], |
| 92 | + &[(0, 3600), (0, 3600)], |
| 93 | + )); |
| 94 | + |
| 95 | + // Different sort fields. |
| 96 | + assert!(!all_same_compaction_scope( |
| 97 | + &["a|b|ts/V2", "a|ts/V2"], |
| 98 | + &[(0, 3600), (0, 3600)], |
| 99 | + )); |
| 100 | + |
| 101 | + // Same start, different duration. |
| 102 | + assert!(!all_same_compaction_scope( |
| 103 | + &["a|b|ts/V2", "a|b|ts/V2"], |
| 104 | + &[(0, 900), (0, 1800)], |
| 105 | + )); |
| 106 | + |
| 107 | + // Different start. |
| 108 | + assert!(!all_same_compaction_scope( |
| 109 | + &["a|b|ts/V2", "a|b|ts/V2"], |
| 110 | + &[(0, 3600), (3600, 3600)], |
| 111 | + )); |
| 112 | + } |
| 113 | +} |
0 commit comments