noxu-cleaner 6.4.0

Log file garbage collection for Noxu DB
Documentation
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//! Per-file utilization counters.
//!
//! tracks the total and obsolete
//! counts and sizes for log entries in a single log file.

/// Per-file utilization counters.
///
/// The UtilizationProfile stores a persistent map of file number to FileSummary.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct FileSummary {
    /// Total number of log entries.
    pub total_count: i32,
    /// Total bytes in log file.
    pub total_size: i32,
    /// Number of IN log entries.
    pub total_in_count: i32,
    /// Byte size of IN log entries.
    pub total_in_size: i32,
    /// Number of LN log entries.
    pub total_ln_count: i32,
    /// Byte size of LN log entries.
    pub total_ln_size: i32,
    /// Byte size of largest LN log entry.
    pub max_ln_size: i32,
    /// Number of obsolete IN log entries.
    pub obsolete_in_count: i32,
    /// Number of obsolete LN log entries.
    pub obsolete_ln_count: i32,
    /// Byte size of obsolete LN log entries.
    pub obsolete_ln_size: i32,
    /// Number of obsolete LNs with size counted.
    pub obsolete_ln_size_counted: i32,
    /// Number of TTL-expired LN log entries (subset of obsolete_ln_count).
    ///
    /// `FileSummary` expired-LN tracking used by `UtilizationCalculator`
    /// to give additional weight to files with many expired records.  Expired LNs
    /// do not need to be migrated during cleaning — they can be dropped outright —
    /// so a file with a high expired fraction is cheaper to clean than its raw
    /// utilization suggests.
    pub obsolete_expired_lns: i32,
    /// Byte size of TTL-expired LN log entries (subset of obsolete_ln_size).
    ///
    /// Used together with `obsolete_expired_lns` to compute the adjusted
    /// utilization in `FileSelector::adjusted_utilization_pct()`.
    pub obsolete_expired_size: i32,
    /// Upper-bound (gradual) expired size: `obsolete_expired_size` plus a
    /// prorated fraction of bytes expiring within the CURRENT interval (JE
    /// ExpirationProfile gradual band). The width
    /// `obsolete_expired_gradual_size - obsolete_expired_size` is the two-pass
    /// uncertainty band (CLEANER_TWO_PASS_GAP). 0 when no TTL data.
    pub obsolete_expired_gradual_size: i32,
}

impl FileSummary {
    /// Creates an empty summary.
    pub fn new() -> Self {
        Self::default()
    }

    /// Returns whether this summary contains any non-zero totals.
    pub fn is_empty(&self) -> bool {
        self.total_count == 0
            && self.total_size == 0
            && self.obsolete_in_count == 0
            && self.obsolete_ln_count == 0
    }

    /// Resets all totals to zero.
    pub fn reset(&mut self) {
        self.total_count = 0;
        self.total_size = 0;
        self.total_in_count = 0;
        self.total_in_size = 0;
        self.total_ln_count = 0;
        self.total_ln_size = 0;
        self.max_ln_size = 0;
        self.obsolete_in_count = 0;
        self.obsolete_ln_count = 0;
        self.obsolete_ln_size = 0;
        self.obsolete_ln_size_counted = 0;
        self.obsolete_expired_lns = 0;
        self.obsolete_expired_size = 0;
        self.obsolete_expired_gradual_size = 0;
    }

    /// Adds the totals of the given summary object to the totals of this object.
    pub fn add(&mut self, other: &FileSummary) {
        self.total_count += other.total_count;
        self.total_size += other.total_size;
        self.total_in_count += other.total_in_count;
        self.total_in_size += other.total_in_size;
        self.total_ln_count += other.total_ln_count;
        self.total_ln_size += other.total_ln_size;
        if self.max_ln_size < other.max_ln_size {
            self.max_ln_size = other.max_ln_size;
        }
        self.obsolete_in_count += other.obsolete_in_count;
        self.obsolete_ln_count += other.obsolete_ln_count;
        self.obsolete_ln_size += other.obsolete_ln_size;
        self.obsolete_ln_size_counted += other.obsolete_ln_size_counted;
        self.obsolete_expired_lns += other.obsolete_expired_lns;
        self.obsolete_expired_size += other.obsolete_expired_size;
        self.obsolete_expired_gradual_size +=
            other.obsolete_expired_gradual_size;
    }

    /// Returns the average size for LNs with sizes not counted, or NaN if there are no such LNs.
    ///
    /// In FileSummaryLN version 3 and greater the obsolete size is normally counted, but not in
    /// exceptional circumstances such as recovery. If it is not counted, obsolete_ln_size_counted
    /// will be less than obsolete_ln_count.
    ///
    /// In log version 8 and greater, we don't count the size when the LN is not resident in cache
    /// during update/delete, and CLEANER_FETCH_OBSOLETE_SIZE is false (the default setting).
    ///
    /// We added max_ln_size in version 8 for use in estimating obsolete LN sizes.
    ///
    /// To compute the average LN size, we only consider the LNs (both obsolete and non-obsolete)
    /// for which the size has not been counted. This increases accuracy when counted and uncounted
    /// LN sizes are not uniform. An example is when large LNs are inserted and deleted. The size of
    /// the deleted LN log entry (which is small) is always counted, but the previous version (which
    /// has a large size) may not be counted.
    fn get_avg_obsolete_ln_size_not_counted(&self) -> f32 {
        // Normalize obsolete amounts to account for double-counting.
        let obs_ln_count = self.obsolete_ln_count.min(self.total_ln_count);
        let obs_ln_size = self.obsolete_ln_size.min(self.total_ln_size);
        let obs_ln_size_counted =
            self.obsolete_ln_size_counted.min(obs_ln_count);

        let obs_count_not_counted = obs_ln_count - obs_ln_size_counted;
        if obs_count_not_counted <= 0 {
            return f32::NAN;
        }

        let total_size_not_counted = self.total_ln_size - obs_ln_size;
        let total_count_not_counted = self.total_ln_count - obs_ln_size_counted;

        if total_size_not_counted <= 0 || total_count_not_counted <= 0 {
            return f32::NAN;
        }

        total_size_not_counted as f32 / total_count_not_counted as f32
    }

    /// Returns the approximate byte size of all obsolete LN entries, using the average LN size
    /// for LN sizes that were not counted.
    pub fn get_obsolete_ln_size(&self) -> i32 {
        if self.total_ln_count == 0 {
            return 0;
        }

        // Normalize obsolete amounts to account for double-counting.
        let obs_ln_count = self.obsolete_ln_count.min(self.total_ln_count);
        let obs_ln_size = self.obsolete_ln_size.min(self.total_ln_size);
        let obs_ln_size_counted =
            self.obsolete_ln_size_counted.min(obs_ln_count);

        // Use the tracked obsolete size for all entries for which the size was counted,
        // plus the average size for all obsolete entries whose size was not counted.
        let mut obs_size = obs_ln_size as i64;
        let obs_count_not_counted = obs_ln_count - obs_ln_size_counted;
        if obs_count_not_counted > 0 {
            // When there are any obsolete LNs with sizes uncounted, we add an obsolete amount
            // that is the product of the number of LNs uncounted and the average LN size.
            let avg_ln_size_not_counted =
                self.get_avg_obsolete_ln_size_not_counted();
            if !avg_ln_size_not_counted.is_nan() {
                obs_size += (obs_count_not_counted as f32
                    * avg_ln_size_not_counted)
                    as i64;
            }
        }

        // Don't return an impossibly large estimate.
        if obs_size > self.total_ln_size as i64 {
            self.total_ln_size
        } else {
            obs_size as i32
        }
    }

    /// Returns the approximate byte size of all obsolete IN entries.
    pub fn get_obsolete_in_size(&self) -> i32 {
        if self.total_in_count == 0 {
            return 0;
        }

        // Normalize obsolete amounts to account for double-counting.
        let obs_in_count = self.obsolete_in_count.min(self.total_in_count);

        // Use average IN size to compute total.
        let size = self.total_in_size as f32;
        let avg_size_per_in = size / self.total_in_count as f32;
        (obs_in_count as f32 * avg_size_per_in) as i32
    }

    /// Returns an estimate of the total bytes that are obsolete.
    pub fn get_obsolete_size(&self) -> i32 {
        self.calculate_obsolete_size(self.get_obsolete_ln_size())
    }

    /// Calculates obsolete size using the given LN obsolete size.
    fn calculate_obsolete_size(&self, ln_obsolete_size: i32) -> i32 {
        if self.total_size <= 0 {
            return 0;
        }

        // Leftover (non-IN non-LN) space is considered obsolete.
        let leftover_size =
            self.total_size - (self.total_in_size + self.total_ln_size);

        let mut obsolete_size =
            ln_obsolete_size + self.get_obsolete_in_size() + leftover_size;

        // Don't report more obsolete bytes than the total. We may calculate more than the total
        // because of (intentional) double-counting during recovery.
        if obsolete_size > self.total_size {
            obsolete_size = self.total_size;
        }

        obsolete_size
    }

    /// Returns the active (non-obsolete) byte size.
    pub fn get_active_size(&self) -> i32 {
        self.total_size - self.get_obsolete_size()
    }

    /// Calculates utilization percentage (0.0-1.0).
    pub fn get_utilization(&self) -> f64 {
        if self.total_size == 0 {
            return 0.0;
        }
        let active_size = self.get_active_size() as f64;
        active_size / self.total_size as f64
    }

    /// Returns the TTL-adjusted active byte size.
    ///
    /// Expired LNs do not need to be migrated during cleaning — they are simply
    /// dropped.  From a cost/benefit perspective the "live data to migrate" is
    /// `active_size - expired_bytes`, which is what the cleaner actually has to
    /// write to the new file.
    ///
    /// `UtilizationCalculator` expired-size adjustment:
    ///   adjustedActive = active_size - obsolete_expired_size
    pub fn get_adjusted_active_size(&self) -> i32 {
        let active = self.get_active_size();
        // Expired bytes that are also in the active set reduce migration cost.
        // Cap at zero so we never return a negative value.
        let expired = self.obsolete_expired_size.min(active);
        active - expired
    }

    /// Returns the TTL-adjusted utilization (0.0-1.0).
    ///
    /// Files with a high proportion of expired records are cheaper to clean
    /// because expired LNs are dropped rather than migrated.  Using the
    /// adjusted utilization causes the `FileSelector` to prefer such files.
    ///
    /// `UtilizationCalculator.getBestFile()` adjusted-utilization
    /// formula: `(active_bytes - expired_bytes) / total_bytes`.
    pub fn get_adjusted_utilization(&self) -> f64 {
        if self.total_size == 0 {
            return 0.0;
        }
        let adjusted = self.get_adjusted_active_size() as f64;
        (adjusted / self.total_size as f64).clamp(0.0, 1.0)
    }

    /// Returns the total number of entries counted. This value is guaranteed to increase whenever
    /// the tracking information about a file changes. It is used as a key discriminator for
    /// FileSummaryLN records.
    pub fn get_entries_counted(&self) -> i32 {
        self.total_count + self.obsolete_ln_count + self.obsolete_in_count
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_new() {
        let summary = FileSummary::new();
        assert!(summary.is_empty());
        assert_eq!(summary.total_count, 0);
        assert_eq!(summary.total_size, 0);
    }

    #[test]
    fn test_is_empty() {
        let mut summary = FileSummary::new();
        assert!(summary.is_empty());

        summary.total_count = 1;
        assert!(!summary.is_empty());

        summary.total_count = 0;
        summary.total_size = 100;
        assert!(!summary.is_empty());

        summary.total_size = 0;
        summary.obsolete_in_count = 5;
        assert!(!summary.is_empty());

        summary.obsolete_in_count = 0;
        summary.obsolete_ln_count = 3;
        assert!(!summary.is_empty());
    }

    #[test]
    fn test_reset() {
        let mut summary = FileSummary {
            total_count: 10,
            total_size: 1000,
            total_in_count: 3,
            total_in_size: 300,
            total_ln_count: 7,
            total_ln_size: 700,
            max_ln_size: 100,
            obsolete_in_count: 1,
            obsolete_ln_count: 2,
            obsolete_ln_size: 200,
            obsolete_ln_size_counted: 2,
            ..Default::default()
        };

        summary.reset();
        assert!(summary.is_empty());
        assert_eq!(summary.max_ln_size, 0);
    }

    #[test]
    fn test_add() {
        let mut summary1 = FileSummary {
            total_count: 10,
            total_size: 1000,
            total_in_count: 3,
            total_in_size: 300,
            total_ln_count: 7,
            total_ln_size: 700,
            max_ln_size: 100,
            obsolete_in_count: 1,
            obsolete_ln_count: 2,
            obsolete_ln_size: 200,
            obsolete_ln_size_counted: 2,
            ..Default::default()
        };

        let summary2 = FileSummary {
            total_count: 5,
            total_size: 500,
            total_in_count: 2,
            total_in_size: 200,
            total_ln_count: 3,
            total_ln_size: 300,
            max_ln_size: 150,
            obsolete_in_count: 1,
            obsolete_ln_count: 1,
            obsolete_ln_size: 100,
            obsolete_ln_size_counted: 1,
            ..Default::default()
        };

        summary1.add(&summary2);

        assert_eq!(summary1.total_count, 15);
        assert_eq!(summary1.total_size, 1500);
        assert_eq!(summary1.total_in_count, 5);
        assert_eq!(summary1.total_in_size, 500);
        assert_eq!(summary1.total_ln_count, 10);
        assert_eq!(summary1.total_ln_size, 1000);
        assert_eq!(summary1.max_ln_size, 150); // Takes max
        assert_eq!(summary1.obsolete_in_count, 2);
        assert_eq!(summary1.obsolete_ln_count, 3);
        assert_eq!(summary1.obsolete_ln_size, 300);
        assert_eq!(summary1.obsolete_ln_size_counted, 3);
    }

    #[test]
    fn test_get_obsolete_ln_size_all_counted() {
        let summary = FileSummary {
            total_count: 10,
            total_size: 1000,
            total_ln_count: 7,
            total_ln_size: 700,
            obsolete_ln_count: 3,
            obsolete_ln_size: 300,
            obsolete_ln_size_counted: 3, // All counted
            ..Default::default()
        };

        assert_eq!(summary.get_obsolete_ln_size(), 300);
    }

    #[test]
    fn test_get_obsolete_ln_size_with_uncounted() {
        let summary = FileSummary {
            total_count: 10,
            total_size: 1000,
            total_ln_count: 10,
            total_ln_size: 1000,
            obsolete_ln_count: 4,
            obsolete_ln_size: 200, // 2 LNs counted at 100 each
            obsolete_ln_size_counted: 2,
            ..Default::default()
        };

        // Avg size of non-obsolete + uncounted = (1000 - 200) / (10 - 2) = 100
        // Uncounted obsolete = 4 - 2 = 2
        // Estimated obsolete = 200 + (2 * 100) = 400
        assert_eq!(summary.get_obsolete_ln_size(), 400);
    }

    #[test]
    fn test_get_obsolete_in_size() {
        let summary = FileSummary {
            total_count: 10,
            total_size: 1000,
            total_in_count: 5,
            total_in_size: 500,
            obsolete_in_count: 2,
            ..Default::default()
        };

        // Average IN size = 500 / 5 = 100
        // Obsolete IN size = 2 * 100 = 200
        assert_eq!(summary.get_obsolete_in_size(), 200);
    }

    #[test]
    fn test_get_obsolete_size() {
        let summary = FileSummary {
            total_count: 10,
            total_size: 1000,
            total_in_count: 3,
            total_in_size: 300,
            total_ln_count: 7,
            total_ln_size: 600,
            obsolete_in_count: 1,
            obsolete_ln_count: 3,
            obsolete_ln_size: 300,
            obsolete_ln_size_counted: 3,
            ..Default::default()
        };

        // Obsolete IN = 1 * (300/3) = 100
        // Obsolete LN = 300
        // Leftover = 1000 - (300 + 600) = 100
        // Total obsolete = 100 + 300 + 100 = 500
        assert_eq!(summary.get_obsolete_size(), 500);
    }

    #[test]
    fn test_get_active_size() {
        let summary = FileSummary {
            total_count: 10,
            total_size: 1000,
            total_in_count: 3,
            total_in_size: 300,
            total_ln_count: 7,
            total_ln_size: 600,
            obsolete_in_count: 1,
            obsolete_ln_count: 3,
            obsolete_ln_size: 300,
            obsolete_ln_size_counted: 3,
            ..Default::default()
        };

        assert_eq!(summary.get_active_size(), 500);
    }

    #[test]
    fn test_get_utilization() {
        let summary = FileSummary {
            total_count: 10,
            total_size: 1000,
            total_in_count: 3,
            total_in_size: 300,
            total_ln_count: 7,
            total_ln_size: 600,
            obsolete_in_count: 1,
            obsolete_ln_count: 3,
            obsolete_ln_size: 300,
            obsolete_ln_size_counted: 3,
            ..Default::default()
        };

        assert_eq!(summary.get_utilization(), 0.5);
    }

    #[test]
    fn test_get_utilization_empty() {
        let summary = FileSummary::new();
        assert_eq!(summary.get_utilization(), 0.0);
    }

    #[test]
    fn test_get_entries_counted() {
        let summary = FileSummary {
            total_count: 10,
            obsolete_in_count: 2,
            obsolete_ln_count: 3,
            ..Default::default()
        };

        assert_eq!(summary.get_entries_counted(), 15);
    }

    #[test]
    fn test_clone() {
        let summary1 = FileSummary {
            total_count: 10,
            total_size: 1000,
            ..Default::default()
        };

        let summary2 = summary1.clone();
        assert_eq!(summary1, summary2);
    }

    #[test]
    fn test_max_ln_size_preserved() {
        let mut summary =
            FileSummary { max_ln_size: 100, ..Default::default() };

        let other = FileSummary { max_ln_size: 50, ..Default::default() };

        summary.add(&other);
        assert_eq!(summary.max_ln_size, 100);

        let larger = FileSummary { max_ln_size: 200, ..Default::default() };

        summary.add(&larger);
        assert_eq!(summary.max_ln_size, 200);
    }

    #[test]
    fn test_obsolete_size_capped_at_total() {
        // Test double-counting protection
        let summary = FileSummary {
            total_count: 10,
            total_size: 1000,
            total_in_count: 3,
            total_in_size: 300,
            total_ln_count: 7,
            total_ln_size: 600,
            obsolete_in_count: 10,  // More than total
            obsolete_ln_count: 20,  // More than total
            obsolete_ln_size: 1500, // More than total
            obsolete_ln_size_counted: 20,
            ..Default::default()
        };

        assert_eq!(summary.get_obsolete_size(), 1000);
    }
}