Line data Source code
1 : use crate::{SegmentMethod, SegmentSizeResult, SizeResult, StorageModel};
2 :
3 : //
4 : // *-g--*---D--->
5 : // /
6 : // /
7 : // / *---b----*-B--->
8 : // / /
9 : // / /
10 : // -----*--e---*-----f----* C
11 : // E \
12 : // \
13 : // *--a---*---A-->
14 : //
15 : // If A and B need to be retained, is it cheaper to store
16 : // snapshot at C+a+b, or snapshots at A and B ?
17 : //
18 : // If D also needs to be retained, which is cheaper:
19 : //
20 : // 1. E+g+e+f+a+b
21 : // 2. D+C+a+b
22 : // 3. D+A+B
23 :
24 : /// `Segment` which has had its size calculated.
25 : #[derive(Clone, Debug)]
26 : struct SegmentSize {
27 : method: SegmentMethod,
28 :
29 : // calculated size of this subtree, using this method
30 : accum_size: u64,
31 :
32 : seg_id: usize,
33 : children: Vec<SegmentSize>,
34 : }
35 :
36 : struct SizeAlternatives {
37 : /// cheapest alternative if parent is available.
38 : incremental: SegmentSize,
39 :
40 : /// cheapest alternative if parent node is not available
41 : non_incremental: Option<SegmentSize>,
42 : }
43 :
44 : impl StorageModel {
45 30 : pub fn calculate(&self) -> SizeResult {
46 30 : // Build adjacency list. 'child_list' is indexed by segment id. Each entry
47 30 : // contains a list of all child segments of the segment.
48 30 : let mut roots: Vec<usize> = Vec::new();
49 30 : let mut child_list: Vec<Vec<usize>> = Vec::new();
50 30 : child_list.resize(self.segments.len(), Vec::new());
51 :
52 225 : for (seg_id, seg) in self.segments.iter().enumerate() {
53 225 : if let Some(parent_id) = seg.parent {
54 195 : child_list[parent_id].push(seg_id);
55 195 : } else {
56 30 : roots.push(seg_id);
57 30 : }
58 : }
59 :
60 30 : let mut segment_results = Vec::new();
61 30 : segment_results.resize(
62 30 : self.segments.len(),
63 30 : SegmentSizeResult {
64 30 : method: SegmentMethod::Skipped,
65 30 : accum_size: 0,
66 30 : },
67 30 : );
68 30 :
69 30 : let mut total_size = 0;
70 60 : for root in roots {
71 30 : if let Some(selected) = self.size_here(root, &child_list).non_incremental {
72 30 : StorageModel::fill_selected_sizes(&selected, &mut segment_results);
73 30 : total_size += selected.accum_size;
74 30 : } else {
75 0 : // Couldn't find any way to get this root. Error?
76 0 : }
77 : }
78 :
79 30 : SizeResult {
80 30 : total_size,
81 30 : segments: segment_results,
82 30 : }
83 30 : }
84 :
85 225 : fn fill_selected_sizes(selected: &SegmentSize, result: &mut Vec<SegmentSizeResult>) {
86 225 : result[selected.seg_id] = SegmentSizeResult {
87 225 : method: selected.method,
88 225 : accum_size: selected.accum_size,
89 225 : };
90 : // recurse to children
91 225 : for child in selected.children.iter() {
92 195 : StorageModel::fill_selected_sizes(child, result);
93 195 : }
94 225 : }
95 :
96 : //
97 : // This is the core of the sizing calculation.
98 : //
99 : // This is a recursive function, that for each Segment calculates the best way
100 : // to reach all the Segments that are marked as needed in this subtree, under two
101 : // different conditions:
102 : // a) when the parent of this segment is available (as a snaphot or through WAL), and
103 : // b) when the parent of this segment is not available.
104 : //
105 225 : fn size_here(&self, seg_id: usize, child_list: &Vec<Vec<usize>>) -> SizeAlternatives {
106 225 : let seg = &self.segments[seg_id];
107 225 : // First figure out the best way to get each child
108 225 : let mut children = Vec::new();
109 225 : for child_id in &child_list[seg_id] {
110 195 : children.push(self.size_here(*child_id, child_list))
111 : }
112 :
113 : // Method 1. If this node is not needed, we can skip it as long as we
114 : // take snapshots later in each sub-tree
115 225 : let snapshot_later = if !seg.needed {
116 152 : let mut snapshot_later = SegmentSize {
117 152 : seg_id,
118 152 : method: SegmentMethod::Skipped,
119 152 : accum_size: 0,
120 152 : children: Vec::new(),
121 152 : };
122 152 :
123 152 : let mut possible = true;
124 164 : for child in children.iter() {
125 164 : if let Some(non_incremental) = &child.non_incremental {
126 106 : snapshot_later.accum_size += non_incremental.accum_size;
127 106 : snapshot_later.children.push(non_incremental.clone())
128 : } else {
129 58 : possible = false;
130 58 : break;
131 : }
132 : }
133 152 : if possible { Some(snapshot_later) } else { None }
134 : } else {
135 73 : None
136 : };
137 :
138 : // Method 2. Get a snapshot here. This assumed to be possible, if the 'size' of
139 : // this Segment was given.
140 225 : let snapshot_here = if !seg.needed || seg.parent.is_none() {
141 152 : if let Some(snapshot_size) = seg.size {
142 104 : let mut snapshot_here = SegmentSize {
143 104 : seg_id,
144 104 : method: SegmentMethod::SnapshotHere,
145 104 : accum_size: snapshot_size,
146 104 : children: Vec::new(),
147 104 : };
148 123 : for child in children.iter() {
149 123 : snapshot_here.accum_size += child.incremental.accum_size;
150 123 : snapshot_here.children.push(child.incremental.clone())
151 : }
152 104 : Some(snapshot_here)
153 : } else {
154 48 : None
155 : }
156 : } else {
157 73 : None
158 : };
159 :
160 : // Method 3. Use WAL to get here from parent
161 225 : let wal_here = {
162 225 : let mut wal_here = SegmentSize {
163 225 : seg_id,
164 225 : method: SegmentMethod::Wal,
165 225 : accum_size: if let Some(parent_id) = seg.parent {
166 195 : seg.lsn - self.segments[parent_id].lsn
167 : } else {
168 30 : 0
169 : },
170 225 : children: Vec::new(),
171 : };
172 420 : for child in children {
173 195 : wal_here.accum_size += child.incremental.accum_size;
174 195 : wal_here.children.push(child.incremental)
175 : }
176 225 : wal_here
177 225 : };
178 225 :
179 225 : // If the parent is not available, what's the cheapest method involving
180 225 : // a snapshot here or later?
181 225 : let mut cheapest_non_incremental: Option<SegmentSize> = None;
182 225 : if let Some(snapshot_here) = snapshot_here {
183 104 : cheapest_non_incremental = Some(snapshot_here);
184 121 : }
185 225 : if let Some(snapshot_later) = snapshot_later {
186 : // Use <=, to prefer skipping if the size is equal
187 94 : if let Some(parent) = &cheapest_non_incremental {
188 46 : if snapshot_later.accum_size <= parent.accum_size {
189 34 : cheapest_non_incremental = Some(snapshot_later);
190 34 : }
191 48 : } else {
192 48 : cheapest_non_incremental = Some(snapshot_later);
193 48 : }
194 131 : }
195 :
196 : // And what's the cheapest method, if the parent is available?
197 225 : let cheapest_incremental = if let Some(cheapest_non_incremental) = &cheapest_non_incremental
198 : {
199 : // Is it cheaper to use a snapshot here or later, anyway?
200 : // Use <, to prefer Wal over snapshot if the cost is the same
201 152 : if wal_here.accum_size < cheapest_non_incremental.accum_size {
202 109 : wal_here
203 : } else {
204 43 : cheapest_non_incremental.clone()
205 : }
206 : } else {
207 73 : wal_here
208 : };
209 :
210 225 : SizeAlternatives {
211 225 : incremental: cheapest_incremental,
212 225 : non_incremental: cheapest_non_incremental,
213 225 : }
214 225 : }
215 : }
|