systemd-journal-sdk-engine 0.6.4

Async query engine components for the pure Rust systemd journal SDK
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
//! Histogram functionality for generating time-series data from journal files.
//!
//! This module provides types and services for computing histograms of journal log entries
//! over time ranges, with support for filtering and faceted field indexing.

use crate::{cache::FileIndexKey, error::Result, facets::Facets};
use journal_core::collections::HashSet;
use journal_index::{Bitmap, FieldName, FieldValuePair, FileIndex, Filter, Seconds};
use lru::LruCache;
use parking_lot::RwLock;
use std::collections::HashMap;
use std::num::NonZeroUsize;
use std::time::Duration;

#[allow(unused_imports)]
use tracing::{debug, error};

/// Calculate the appropriate bucket duration for a given time range.
///
/// This function determines the bucket size that will result in approximately
/// 50-100 buckets for the given time range. The bucket durations are selected
/// from a predefined set of "nice" values (1s, 2s, 5s, 10s, 1m, 5m, 1h, etc.)
/// to make the resulting histograms easy to interpret.
///
/// # Arguments
/// * `time_range_duration` - The duration of the time range in seconds
///
/// # Returns
/// The bucket duration in seconds
pub fn calculate_bucket_duration(time_range_duration: u32) -> u32 {
    const MINUTE: Duration = Duration::from_secs(60);
    const HOUR: Duration = Duration::from_secs(60 * MINUTE.as_secs());
    const DAY: Duration = Duration::from_secs(24 * HOUR.as_secs());

    const VALID_DURATIONS: &[Duration] = &[
        // Seconds
        Duration::from_secs(1),
        Duration::from_secs(2),
        Duration::from_secs(5),
        Duration::from_secs(10),
        Duration::from_secs(15),
        Duration::from_secs(30),
        // Minutes
        MINUTE,
        Duration::from_secs(2 * MINUTE.as_secs()),
        Duration::from_secs(3 * MINUTE.as_secs()),
        Duration::from_secs(5 * MINUTE.as_secs()),
        Duration::from_secs(10 * MINUTE.as_secs()),
        Duration::from_secs(15 * MINUTE.as_secs()),
        Duration::from_secs(30 * MINUTE.as_secs()),
        // Hours
        HOUR,
        Duration::from_secs(2 * HOUR.as_secs()),
        Duration::from_secs(6 * HOUR.as_secs()),
        Duration::from_secs(8 * HOUR.as_secs()),
        Duration::from_secs(12 * HOUR.as_secs()),
        // Days
        DAY,
        Duration::from_secs(2 * DAY.as_secs()),
        Duration::from_secs(3 * DAY.as_secs()),
        Duration::from_secs(5 * DAY.as_secs()),
        Duration::from_secs(7 * DAY.as_secs()),
        Duration::from_secs(14 * DAY.as_secs()),
        Duration::from_secs(30 * DAY.as_secs()),
    ];

    VALID_DURATIONS
        .iter()
        .rev()
        .find(|&&bucket_width| time_range_duration as u64 / bucket_width.as_secs() >= 50)
        .map(|d| d.as_secs())
        .unwrap_or(1) as u32
}

/// A bucket request contains a [start, end) time range along with the
/// filter that should be applied.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct BucketRequest {
    /// Start time of the bucket request
    pub start: Seconds,
    /// End time of the bucket request
    pub end: Seconds,
    /// Facets to use for file index
    pub facets: Facets,
    /// Applied filter expression
    pub filter_expr: Filter,
}

impl BucketRequest {
    /// The duration of the bucket request in seconds
    pub fn duration(&self) -> Seconds {
        self.end - self.start
    }
}

/// A bucket response containing aggregated field value counts.
#[derive(Debug, Clone)]
pub struct BucketResponse {
    /// Maps field=value pairs to (unfiltered, filtered) counts
    pub fv_counts: HashMap<FieldValuePair, (usize, usize)>,
    /// Set of fields that are not indexed
    pub unindexed_fields: HashSet<FieldName>,
    /// Total entry counts (unfiltered, filtered) in this bucket across all files
    pub total_entries: (usize, usize),
}

impl BucketResponse {
    /// Creates a new empty bucket response.
    pub(crate) fn new() -> Self {
        Self {
            fv_counts: HashMap::default(),
            unindexed_fields: HashSet::default(),
            total_entries: (0, 0),
        }
    }

    /// Get all indexed field names from this bucket response.
    pub fn indexed_fields(&self) -> HashSet<FieldName> {
        self.fv_counts
            .keys()
            .map(|pair| pair.extract_field())
            .collect()
    }
}

/// Represents a histogram of journal log entries over time.
///
/// A histogram contains bucketed data where each bucket represents a time range
/// and holds aggregated counts of field values and filtering results.
#[derive(Debug, Clone)]
pub struct Histogram {
    pub buckets: Vec<(BucketRequest, BucketResponse)>,
}

impl Histogram {
    /// Returns the start time of the histogram (first bucket's start time).
    pub fn start_time(&self) -> Seconds {
        let bucket_request = &self
            .buckets
            .first()
            .expect("histogram with at least one bucket")
            .0;
        bucket_request.start
    }

    /// Returns the end time of the histogram (last bucket's end time).
    pub fn end_time(&self) -> Seconds {
        let bucket_request = &self
            .buckets
            .last()
            .expect("histogram with at least one bucket")
            .0;
        bucket_request.end
    }

    /// Returns the duration of each bucket in seconds.
    pub fn bucket_duration(&self) -> Seconds {
        self.buckets
            .first()
            .expect("histogram with at least one bucket")
            .0
            .duration()
    }

    /// Returns all discovered field names from the histogram buckets in a deterministic order.
    pub fn discovered_fields(&self) -> Vec<FieldName> {
        // Collect all unique fields from all buckets
        let mut fields = HashSet::default();
        for (_, bucket_response) in &self.buckets {
            fields.extend(bucket_response.indexed_fields());
            fields.extend(bucket_response.unindexed_fields.iter().cloned());
        }

        let mut v: Vec<FieldName> = fields.into_iter().collect();
        v.sort();
        v
    }
}

/// Engine for computing histograms from journal files.
///
/// The engine maintains caches and resources for efficiently computing histograms
/// across multiple queries. It can be reused for multiple histogram computations.
pub struct HistogramEngine {
    responses: RwLock<LruCache<BucketRequest, BucketResponse>>,
}

impl HistogramEngine {
    /// Creates a new HistogramEngine with a default capacity of 1000 bucket responses.
    pub fn new() -> Self {
        Self::with_capacity(1000)
    }

    /// Creates a new HistogramEngine with the specified cache capacity.
    ///
    /// The capacity determines how many bucket responses will be cached before
    /// old entries are evicted using an LRU policy.
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            responses: RwLock::new(LruCache::new(
                NonZeroUsize::new(capacity).expect("capacity must be non-zero"),
            )),
        }
    }

    /// Compute a histogram from pre-indexed files.
    ///
    /// This method allows you to compute histograms from file indexes that have
    /// already been loaded, avoiding redundant cache lookups and file discoveries.
    ///
    /// # Arguments
    /// * `indexed_files` - Pre-computed file indexes
    /// * `time_range` - Query time range with aligned boundaries and bucket duration
    /// * `facets` - Fields to index
    /// * `filter_expr` - Filter expression to apply
    pub fn compute_from_indexes(
        &self,
        indexed_files: &[(FileIndexKey, FileIndex)],
        time_range: &crate::QueryTimeRange,
        facets: &[String],
        filter_expr: &Filter,
    ) -> Result<Histogram> {
        let facets = Facets::new(facets);
        let bucket_requests = bucket_requests_for(time_range, &facets, filter_expr);
        let buckets_to_compute = self.buckets_to_compute(&bucket_requests);

        if buckets_to_compute.is_empty() {
            return Ok(self.histogram_from_cache(bucket_requests));
        }

        let (new_responses, bucket_cacheable) =
            compute_bucket_responses(indexed_files, &buckets_to_compute);
        self.cache_computed_responses(&new_responses, &bucket_cacheable);

        Ok(self.histogram_from_responses(bucket_requests, &new_responses))
    }

    fn buckets_to_compute(&self, bucket_requests: &[BucketRequest]) -> Vec<BucketRequest> {
        let responses = self.responses.read();
        bucket_requests
            .iter()
            .filter(|br| !responses.contains(br))
            .cloned()
            .collect()
    }

    fn cache_computed_responses(
        &self,
        new_responses: &HashMap<BucketRequest, BucketResponse>,
        bucket_cacheable: &HashMap<BucketRequest, bool>,
    ) {
        let mut responses_guard = self.responses.write();
        for (bucket_request, response) in new_responses {
            if bucket_cacheable
                .get(bucket_request)
                .copied()
                .unwrap_or(false)
            {
                responses_guard.put(bucket_request.clone(), response.clone());
            }
        }
    }

    fn histogram_from_responses(
        &self,
        bucket_requests: Vec<BucketRequest>,
        new_responses: &HashMap<BucketRequest, BucketResponse>,
    ) -> Histogram {
        let mut responses_guard = self.responses.write();
        let buckets = bucket_requests
            .into_iter()
            .filter_map(|bucket_request| {
                responses_guard
                    .get(&bucket_request)
                    .cloned()
                    .or_else(|| new_responses.get(&bucket_request).cloned())
                    .map(|response| (bucket_request, response))
            })
            .collect();

        Histogram { buckets }
    }

    fn histogram_from_cache(&self, bucket_requests: Vec<BucketRequest>) -> Histogram {
        let mut responses = self.responses.write();
        let buckets = bucket_requests
            .into_iter()
            .filter_map(|bucket_request| {
                responses
                    .get(&bucket_request)
                    .map(|response| (bucket_request, response.clone()))
            })
            .collect();

        Histogram { buckets }
    }
}

fn bucket_requests_for(
    time_range: &crate::QueryTimeRange,
    facets: &Facets,
    filter_expr: &Filter,
) -> Vec<BucketRequest> {
    time_range
        .buckets()
        .map(|(start, end)| BucketRequest {
            start: Seconds(start),
            end: Seconds(end),
            facets: facets.clone(),
            filter_expr: filter_expr.clone(),
        })
        .collect()
}

fn compute_bucket_responses(
    indexed_files: &[(FileIndexKey, FileIndex)],
    buckets_to_compute: &[BucketRequest],
) -> (
    HashMap<BucketRequest, BucketResponse>,
    HashMap<BucketRequest, bool>,
) {
    let mut new_responses = empty_bucket_responses(buckets_to_compute);
    let mut bucket_cacheable = initially_cacheable_buckets(buckets_to_compute);

    for (_, file_index) in indexed_files {
        process_file_buckets(
            file_index,
            buckets_to_compute,
            &mut new_responses,
            &mut bucket_cacheable,
        );
    }

    (new_responses, bucket_cacheable)
}

fn empty_bucket_responses(
    bucket_requests: &[BucketRequest],
) -> HashMap<BucketRequest, BucketResponse> {
    bucket_requests
        .iter()
        .map(|br| (br.clone(), BucketResponse::new()))
        .collect()
}

fn initially_cacheable_buckets(bucket_requests: &[BucketRequest]) -> HashMap<BucketRequest, bool> {
    bucket_requests
        .iter()
        .map(|br| (br.clone(), true))
        .collect()
}

fn process_file_buckets(
    file_index: &FileIndex,
    bucket_requests: &[BucketRequest],
    responses: &mut HashMap<BucketRequest, BucketResponse>,
    bucket_cacheable: &mut HashMap<BucketRequest, bool>,
) {
    for bucket_request in bucket_requests {
        let Some(response) = responses.get_mut(bucket_request) else {
            continue;
        };
        if !file_overlaps_bucket(file_index, bucket_request) {
            continue;
        }
        if file_index.online() {
            bucket_cacheable.insert(bucket_request.clone(), false);
        }

        let filter_bitmap = filter_bitmap_for_bucket(file_index, bucket_request);
        update_bucket_totals(file_index, bucket_request, filter_bitmap.as_ref(), response);
        record_unindexed_fields(file_index, response);
        count_indexed_field_values(file_index, bucket_request, filter_bitmap.as_ref(), response);
    }
}

fn file_overlaps_bucket(file_index: &FileIndex, bucket_request: &BucketRequest) -> bool {
    file_index.start_time() < bucket_request.end && file_index.end_time() > bucket_request.start
}

fn filter_bitmap_for_bucket(
    file_index: &FileIndex,
    bucket_request: &BucketRequest,
) -> Option<Bitmap> {
    (!bucket_request.filter_expr.is_none()).then(|| bucket_request.filter_expr.evaluate(file_index))
}

fn update_bucket_totals(
    file_index: &FileIndex,
    bucket_request: &BucketRequest,
    filter_bitmap: Option<&Bitmap>,
    response: &mut BucketResponse,
) {
    let all_entries = Bitmap::insert_range(0..file_index.total_entries() as u32);
    let unfiltered_total = count_entries(file_index, &all_entries, bucket_request);
    let filtered_total = filter_bitmap
        .map(|bitmap| count_entries(file_index, bitmap, bucket_request))
        .unwrap_or(unfiltered_total);

    response.total_entries.0 += unfiltered_total;
    response.total_entries.1 += filtered_total;
}

fn count_entries(file_index: &FileIndex, bitmap: &Bitmap, bucket_request: &BucketRequest) -> usize {
    file_index
        .count_entries_in_time_range(bitmap, bucket_request.start, bucket_request.end)
        .unwrap_or(0)
}

fn record_unindexed_fields(file_index: &FileIndex, response: &mut BucketResponse) {
    for field in file_index.fields() {
        if !file_index.is_indexed(field)
            && let Some(field_name) = FieldName::new(field)
        {
            response.unindexed_fields.insert(field_name);
        }
    }
}

fn count_indexed_field_values(
    file_index: &FileIndex,
    bucket_request: &BucketRequest,
    filter_bitmap: Option<&Bitmap>,
    response: &mut BucketResponse,
) {
    for (indexed_field, field_bitmap) in file_index.bitmaps() {
        let unfiltered_count = count_entries(file_index, field_bitmap, bucket_request);
        let filtered_count = filtered_field_count(
            file_index,
            bucket_request,
            field_bitmap,
            filter_bitmap,
            unfiltered_count,
        );
        add_field_counts(response, indexed_field, unfiltered_count, filtered_count);
    }
}

fn filtered_field_count(
    file_index: &FileIndex,
    bucket_request: &BucketRequest,
    field_bitmap: &Bitmap,
    filter_bitmap: Option<&Bitmap>,
    unfiltered_count: usize,
) -> usize {
    let Some(filter_bitmap) = filter_bitmap else {
        return unfiltered_count;
    };
    let filtered_bitmap = field_bitmap & filter_bitmap;
    count_entries(file_index, &filtered_bitmap, bucket_request)
}

fn add_field_counts(
    response: &mut BucketResponse,
    indexed_field: &FieldValuePair,
    unfiltered_count: usize,
    filtered_count: usize,
) {
    if let Some(pair) = FieldValuePair::parse(indexed_field) {
        let counts = response.fv_counts.entry(pair).or_insert((0, 0));
        counts.0 += unfiltered_count;
        counts.1 += filtered_count;
    }
}