Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT: metrics sharding handwritten #2304

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions opentelemetry-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ tokio = { workspace = true, features = ["rt", "time"], optional = true }
tokio-stream = { workspace = true, optional = true }
http = { workspace = true, optional = true }
tracing = {workspace = true, optional = true}
crossbeam-utils = "0.8.20"
rustc-hash = "2.0.0"

[package.metadata.docs.rs]
all-features = true
Expand Down
139 changes: 139 additions & 0 deletions opentelemetry-sdk/src/metrics/internal/hashed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
use std::{
borrow::{Borrow, Cow},
hash::{BuildHasher, Hash, Hasher},
ops::Deref,
};

use rustc_hash::FxHasher;

/// Hash value only once, works with references and owned types.
pub(crate) struct Hashed<'a, T>
where
T: ToOwned + ?Sized,
{
value: Cow<'a, T>,
hash: u64,
}

impl<'a, T> Hashed<'a, T>
where
T: ToOwned + Hash + ?Sized,
{
pub(crate) fn from_borrowed(value: &'a T) -> Self {
let hash = calc_hash(&value);
Self {
value: Cow::Borrowed(value),
hash,
}
}

pub(crate) fn from_owned(value: <T as ToOwned>::Owned) -> Self {
let hash = calc_hash(value.borrow());
Self {
value: Cow::Owned(value),
hash,
}
}

pub(crate) fn into_owned(self) -> Hashed<'static, T> {
let value = self.value.into_owned();
Hashed {
value: Cow::Owned(value),
hash: self.hash,
}
}

pub(crate) fn into_inner_owned(self) -> T::Owned {
self.value.into_owned()
}

pub(crate) fn hash_value(&self) -> u64 {
self.hash
}
}

fn calc_hash<T>(value: T) -> u64
where
T: Hash,
{
let mut hasher = FxHasher::default();
value.hash(&mut hasher);
hasher.finish()
}

impl<T> Clone for Hashed<'_, T>
where
T: ToOwned + ?Sized,
{
fn clone(&self) -> Self {
Self {
value: self.value.clone(),
hash: self.hash,
}
}

fn clone_from(&mut self, source: &Self) {
self.value.clone_from(&source.value);
self.hash = source.hash;
}
}

impl<T> Hash for Hashed<'_, T>
where
T: ToOwned + Hash + ?Sized,
{
fn hash<H: Hasher>(&self, state: &mut H) {
state.write_u64(self.hash);
}
}

impl<T> PartialEq for Hashed<'_, T>
where
T: ToOwned + PartialEq + ?Sized,
{
fn eq(&self, other: &Self) -> bool {
self.value.as_ref() == other.value.as_ref()
}
}

impl<T> Eq for Hashed<'_, T> where T: ToOwned + Eq + ?Sized {}

impl<T> Deref for Hashed<'_, T>
where
T: ToOwned + ?Sized,
{
type Target = T;

fn deref(&self) -> &Self::Target {
self.value.deref()
}
}

/// Used to make [`Hashed`] values no-op in [`HashMap`](std::collections::HashMap) or [`HashSet`](std::collections::HashSet).
/// For all other keys types (except for [`u64`]) it will panic.
#[derive(Default, Clone)]
pub(crate) struct HashedNoOpBuilder {
hashed: u64,
}

impl Hasher for HashedNoOpBuilder {
fn finish(&self) -> u64 {
self.hashed
}

fn write(&mut self, _bytes: &[u8]) {
panic!("Only works with `Hashed` value")
}

fn write_u64(&mut self, i: u64) {
self.hashed = i;
}
}

impl BuildHasher for HashedNoOpBuilder {
type Hasher = HashedNoOpBuilder;

fn build_hasher(&self) -> Self::Hasher {
HashedNoOpBuilder::default()
}
}
Loading