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
use core::sync::atomic::{AtomicUsize, Ordering};
use std::{
    fmt::{self, Debug},
    marker::PhantomData,
};
use std::{hash::BuildHasherDefault, iter::repeat};

use hashbrown::{hash_map::RawEntryMut, HashMap};
use metrics::KeyHasher;
use parking_lot::RwLock;

use crate::{Hashable, MetricKind};

type RegistryHasher = KeyHasher;
type RegistryHashMap<K, V> = HashMap<K, V, BuildHasherDefault<RegistryHasher>>;

/// Generation counter.
///
/// Used for denoting the generation of a given handle, which is used to provide compare-and-swap
/// deletion semantics i.e. if the generation used to request deletion for a handle is behind the
/// current generation of the handle, then the deletion will not proceed.
#[derive(Debug, Default, Clone, PartialEq)]
pub struct Generation(usize);

/// A trait that defines generational semantics for wrapper types.
///
/// Used to provide compile-time generation tracking where the choice is encoded in which type is
/// used (i.e. `Tracked` vs `NotTracked`), which necessitates a trait to mediate usage.
pub trait Generational<H>: From<H> {
    /// Increments the generation counter.
    fn increment_generation(&self);

    /// Gets the current generation counter.
    fn get_generation(&self) -> Generation;

    /// Gets a reference to the inner type.
    fn get_inner(&self) -> &H;

    /// Creates a default initialized wrapper around `inner`.
    fn initial(inner: H) -> Self {
        inner.into()
    }
}

/// A generational wrapper that does track the generation.
pub struct Tracked<H>(AtomicUsize, H);

impl<H> Tracked<H> {
    /// Creates a new `Tracked`.
    pub fn new(h: H) -> Tracked<H> {
        Tracked(AtomicUsize::new(0), h)
    }
}

impl<H> Generational<H> for Tracked<H> {
    /// Increments the generation counter.
    fn increment_generation(&self) {
        self.0.fetch_add(1, Ordering::Release);
    }

    /// Gets the current generation counter.
    fn get_generation(&self) -> Generation {
        Generation(self.0.load(Ordering::Acquire))
    }

    /// Gets a reference to the inner type.
    fn get_inner(&self) -> &H {
        &self.1
    }
}

impl<H> From<H> for Tracked<H> {
    fn from(inner: H) -> Self {
        Self::new(inner)
    }
}

impl<H: fmt::Debug> fmt::Debug for Tracked<H> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Tracked")
            .field("gen", &self.0)
            .field("inner", &self.1)
            .finish()
    }
}

/// A generational wrapper that does not track the generation.
pub struct NotTracked<H>(H);

impl<H> NotTracked<H> {
    /// Creates a new `NotTracked`.
    pub fn new(h: H) -> NotTracked<H> {
        NotTracked(h)
    }
}

impl<H> Generational<H> for NotTracked<H> {
    /// Increments the generation counter.
    fn increment_generation(&self) {}

    /// Gets the current generation counter.
    fn get_generation(&self) -> Generation {
        Generation::default()
    }

    /// Gets a reference to the inner type.
    fn get_inner(&self) -> &H {
        &self.0
    }
}

impl<H> From<H> for NotTracked<H> {
    fn from(inner: H) -> Self {
        Self::new(inner)
    }
}

/// A high-performance metric registry.
///
/// `Registry` provides the ability to maintain a central listing of metrics mapped by a given key.
///
/// In many cases, `K` will be a composite key, where the fundamental `Key` type from `metrics` is
/// present, and differentiation is provided by storing the metric type alongside.
///
/// Metrics themselves are represented opaquely behind `H`.  In most cases, this would be a
/// thread-safe handle to the underlying metrics storage that the owner of the registry can use to
/// update the actual metric value(s) as needed.  `Handle`, from this crate, is a solid default
/// choice.
///
/// As well, handles have an associated generation counter which is incremented any time an entry is
/// operated on.  This generation is returned with the handle when querying the registry, and can be
/// used in order to delete a handle from the registry, allowing callers to prune old/stale handles
/// over time.
///
/// `Registry` is optimized for reads.
#[derive(Debug)]
pub struct Registry<K, H, G>
where
    K: Eq + Hashable + Clone + 'static,
    H: 'static,
{
    shards: Vec<Vec<RwLock<RegistryHashMap<K, G>>>>,
    mask: usize,
    _handle: PhantomData<H>,
}

impl<K, H, G> Registry<K, H, G>
where
    K: Eq + Hashable + Clone + 'static,
    H: 'static,
{
    /// Creates a new `Registry`.
    fn new() -> Self {
        let shard_count = std::cmp::max(1, num_cpus::get()).next_power_of_two();
        let mask = shard_count - 1;
        let counters = repeat(())
            .take(shard_count)
            .map(|_| RwLock::new(RegistryHashMap::default()))
            .collect();
        let gauges = repeat(())
            .take(shard_count)
            .map(|_| RwLock::new(RegistryHashMap::default()))
            .collect();
        let histograms = repeat(())
            .take(shard_count)
            .map(|_| RwLock::new(RegistryHashMap::default()))
            .collect();

        let shards = vec![counters, gauges, histograms];

        Self {
            shards,
            mask,
            _handle: PhantomData,
        }
    }

    /// Creates a new `Registry` without generation semantics.
    pub fn untracked() -> Registry<K, H, NotTracked<H>> {
        Registry::<K, H, NotTracked<H>>::new()
    }

    /// Creates a new `Registry` with generation semantics.
    ///
    /// In some use cases, there is a requirement to understand what "generation" a metric is, for
    /// the purposes of understanding if a given metric has changed between two points in time.
    ///
    /// This registry wraps metrics with a generation counter, which is incremented every time a
    /// metric is operated on.  When queried for a list of metrics, they'll be provided with their
    /// current generation.
    pub fn tracked() -> Registry<K, H, Tracked<H>> {
        Registry::<K, H, Tracked<H>>::new()
    }
}

impl<K, H, G> Registry<K, H, G>
where
    K: Eq + Hashable + Clone + 'static,
    H: 'static,
    G: Generational<H>,
{
    #[inline]
    fn get_hash_and_shard(
        &self,
        kind: MetricKind,
        key: &K,
    ) -> (u64, &RwLock<RegistryHashMap<K, G>>) {
        let hash = key.hashable();

        // SAFETY: We map each MetricKind variant -- three at present -- to a usize value
        // representing an index in a vector, so we statically know that we're always extracting our
        // sub-shards correctly.  Secondly, we initialize vector of subshards with a power-of-two
        // value, and `self.mask` is `self.shards.len() - 1`, thus we can never have a result from
        // the masking operation that results in a value which is not in bounds of our subshards
        // vector.
        let shards = unsafe { self.shards.get_unchecked(kind_to_idx(kind)) };
        let shard = unsafe { shards.get_unchecked(hash as usize & self.mask) };

        (hash, shard)
    }

    /// Removes all metrics from the registry.
    ///
    /// This operation is eventually consistent: metrics will be removed piecemeal, and this method
    /// does not ensure that callers will see the registry as entirely empty at any given point.
    pub fn clear(&self) {
        for shard in &self.shards {
            for subshard in shard {
                subshard.write().clear();
            }
        }
    }

    /// Perform an operation on a given key.
    ///
    /// The `op` function will be called for the handle under the given `key`.
    ///
    /// If the `key` is not already mapped, the `init` function will be
    /// called, and the resulting handle will be stored in the registry.
    pub fn op<I, O, V>(&self, kind: MetricKind, key: &K, op: O, init: I) -> V
    where
        I: FnOnce() -> H,
        O: FnOnce(&H) -> V,
    {
        let (hash, shard) = self.get_hash_and_shard(kind, key);

        // Try and get the handle if it exists, running our operation if we succeed.
        let shard_read = shard.read();
        if let Some((_, v)) = shard_read.raw_entry().from_key_hashed_nocheck(hash, key) {
            let result = op(v.get_inner());
            v.increment_generation();
            result
        } else {
            // Switch to write guard and insert the handle first.
            drop(shard_read);
            let mut shard_write = shard.write();
            let v = if let Some((_, v)) = shard_write.raw_entry().from_key_hashed_nocheck(hash, key)
            {
                v
            } else {
                let (_, v) = shard_write
                    .raw_entry_mut()
                    .from_key_hashed_nocheck(hash, key)
                    .or_insert_with(|| (key.clone(), G::initial(init())));

                v
            };

            let result = op(v.get_inner());
            v.increment_generation();
            result
        }
    }

    /// Deletes a handle from the registry.
    ///
    /// Returns `true` if the handle existed and was removed, `false` otherwise.
    pub fn delete(&self, kind: MetricKind, key: &K) -> bool {
        let (hash, shard) = self.get_hash_and_shard(kind, key);
        let mut shard_write = shard.write();
        let entry = shard_write
            .raw_entry_mut()
            .from_key_hashed_nocheck(hash, key);
        if let RawEntryMut::Occupied(entry) = entry {
            let _ = entry.remove_entry();
            return true;
        }

        false
    }

    /// Deletes a handle from the registry.
    ///
    /// The generation of a given key is passed along when querying the registry via
    /// [`get_handles`](Registry::get_handles).  If the generation given here does not match the
    /// current generation, then the handle will not be removed.
    ///
    /// Returns `true` if the handle existed and was removed, `false` otherwise.
    pub fn delete_with_gen(&self, kind: MetricKind, key: &K, generation: Generation) -> bool {
        let (hash, shard) = self.get_hash_and_shard(kind, key);
        let mut shard_write = shard.write();
        let entry = shard_write
            .raw_entry_mut()
            .from_key_hashed_nocheck(hash, key);
        if let RawEntryMut::Occupied(entry) = entry {
            if entry.get().get_generation() == generation {
                let _ = entry.remove_entry();
                return true;
            }
        }

        false
    }

    /// Visits every handle stored in this registry.
    ///
    /// The given function will be passed the metric kind as well as the key and handle references,
    /// which are wrapped in `Generational`-implementing holders.
    ///
    /// This operation does not lock the entire registry, but proceeds directly through the
    /// "subshards" that are kept internally.  As a result, all subshards will be visited, but a
    /// metric that existed at the exact moment that `visit` was called may not actually be observed
    /// if it is deleted before that subshard is reached.  Likewise, a metric that is added after
    /// the call to `visit`, but before `visit` finishes, may also not be observed.
    pub fn visit<F>(&self, mut collect: F)
    where
        F: FnMut(MetricKind, (&K, &G)),
    {
        for (idx, subshards) in self.shards.iter().enumerate() {
            let kind = idx_to_kind(idx);

            for subshard in subshards {
                let shard_read = subshard.read();
                for item in shard_read.iter() {
                    collect(kind, item);
                }
            }
        }
    }

    /// Gets a map of all present handles, mapped by key.
    ///
    /// Handles must implement `Clone`.  This map is a point-in-time snapshot of the registry.
    pub fn get_handles(&self) -> HashMap<(MetricKind, K), (Generation, H)>
    where
        H: Clone,
    {
        let mut handles = HashMap::new();
        self.visit(|kind, (k, v)| {
            handles.insert(
                (kind, k.clone()),
                (v.get_generation(), v.get_inner().clone()),
            );
        });
        handles
    }
}

impl<K, H, G> Default for Registry<K, H, G>
where
    K: Eq + Hashable + Clone + 'static,
    H: 'static,
    G: Generational<H>,
{
    fn default() -> Self {
        Registry::new()
    }
}

const fn kind_to_idx(kind: MetricKind) -> usize {
    match kind {
        MetricKind::Counter => 0,
        MetricKind::Gauge => 1,
        MetricKind::Histogram => 2,
    }
}

#[inline]
fn idx_to_kind(idx: usize) -> MetricKind {
    match idx {
        0 => MetricKind::Counter,
        1 => MetricKind::Gauge,
        2 => MetricKind::Histogram,
        _ => panic!("invalid index"),
    }
}

#[cfg(test)]
mod tests {
    use super::{Generational, MetricKind, Registry};
    use crate::{DefaultHashable, NotTracked, Tracked};
    use std::sync::{
        atomic::{AtomicUsize, Ordering::SeqCst},
        Arc,
    };

    #[test]
    fn test_tracked() {
        let generational = Tracked::new(1);
        let start_gen = generational.get_generation();
        let start_gen_extra = generational.get_generation();
        assert_eq!(start_gen, start_gen_extra);

        generational.increment_generation();

        let end_gen = generational.get_generation();
        assert_ne!(start_gen, end_gen);
    }

    #[test]
    fn test_not_tracked() {
        let generational = NotTracked::new(1);
        let start_gen = generational.get_generation();
        let start_gen_extra = generational.get_generation();
        assert_eq!(start_gen, start_gen_extra);

        generational.increment_generation();

        let end_gen = generational.get_generation();
        assert_eq!(start_gen, end_gen);
    }

    #[test]
    fn test_tracked_registry() {
        let registry =
            Registry::<DefaultHashable<u64>, Arc<AtomicUsize>, Tracked<Arc<AtomicUsize>>>::default(
            );

        let key = DefaultHashable(1);

        let entries = registry.get_handles();
        assert_eq!(entries.len(), 0);

        let initial_value = registry.op(
            MetricKind::Counter,
            &key,
            |h| h.fetch_add(1, SeqCst),
            || Arc::new(AtomicUsize::new(42)),
        );
        assert_eq!(initial_value, 42);

        let initial_entries = registry.get_handles();
        assert_eq!(initial_entries.len(), 1);

        let initial_entry = initial_entries
            .into_iter()
            .next()
            .expect("failed to get first entry");

        let (ikey, (initial_gen, value)) = initial_entry;
        assert_eq!(ikey, (MetricKind::Counter, DefaultHashable(1)));
        assert_eq!(value.load(SeqCst), 43);

        let update_value = registry.op(
            MetricKind::Counter,
            &key,
            |h| h.fetch_add(1, SeqCst),
            || Arc::new(AtomicUsize::new(42)),
        );
        assert_eq!(update_value, 43);

        let updated_entries = registry.get_handles();
        assert_eq!(updated_entries.len(), 1);

        let updated_entry = updated_entries
            .into_iter()
            .next()
            .expect("failed to get updated entry");

        let ((kind, ukey), (updated_gen, value)) = updated_entry;
        assert_eq!(kind, MetricKind::Counter);
        assert_eq!(ukey, DefaultHashable(1));
        assert_eq!(value.load(SeqCst), 44);

        assert!(!registry.delete_with_gen(kind, &key, initial_gen));

        let entries = registry.get_handles();
        assert_eq!(entries.len(), 1);

        assert!(registry.delete_with_gen(kind, &key, updated_gen));

        let entries = registry.get_handles();
        assert_eq!(entries.len(), 0);
    }
}