Bump rand crate to 0.9 (#12674)

This commit is contained in:
Folke Behrens
2025-07-22 11:31:39 +02:00
committed by GitHub
parent 5464552020
commit 9c0efba91e
60 changed files with 231 additions and 237 deletions

View File

@@ -66,6 +66,7 @@ postgres-client = { package = "tokio-postgres2", path = "../libs/proxy/tokio-pos
postgres-protocol = { package = "postgres-protocol2", path = "../libs/proxy/postgres-protocol2" }
pq_proto.workspace = true
rand.workspace = true
rand_core.workspace = true
regex.workspace = true
remote_storage = { version = "0.1", path = "../libs/remote_storage/" }
reqwest = { workspace = true, features = ["rustls-tls-native-roots"] }
@@ -133,6 +134,6 @@ pbkdf2 = { workspace = true, features = ["simple", "std"] }
rcgen.workspace = true
rstest.workspace = true
walkdir.workspace = true
rand_distr = "0.4"
rand_distr = "0.5"
tokio-postgres.workspace = true
tracing-test = "0.2"

View File

@@ -803,7 +803,7 @@ mod tests {
use http_body_util::Full;
use hyper::service::service_fn;
use hyper_util::rt::TokioIo;
use rand::rngs::OsRng;
use rand_core::OsRng;
use rsa::pkcs8::DecodePrivateKey;
use serde::Serialize;
use serde_json::json;

View File

@@ -13,7 +13,7 @@ use arc_swap::ArcSwapOption;
use camino::Utf8PathBuf;
use futures::future::Either;
use itertools::{Itertools, Position};
use rand::{Rng, thread_rng};
use rand::Rng;
use remote_storage::RemoteStorageConfig;
use tokio::net::TcpListener;
#[cfg(any(test, feature = "testing"))]
@@ -573,7 +573,7 @@ pub async fn run() -> anyhow::Result<()> {
attempt.into_inner()
);
}
let jitter = thread_rng().gen_range(0..100);
let jitter = rand::rng().random_range(0..100);
tokio::time::sleep(Duration::from_millis(1000 + jitter)).await;
}
}

View File

@@ -5,7 +5,7 @@ use std::time::Duration;
use async_trait::async_trait;
use clashmap::ClashMap;
use clashmap::mapref::one::Ref;
use rand::{Rng, thread_rng};
use rand::Rng;
use tokio::time::Instant;
use tracing::{debug, info};
@@ -343,7 +343,7 @@ impl ProjectInfoCacheImpl {
}
fn gc(&self) {
let shard = thread_rng().gen_range(0..self.project2ep.shards().len());
let shard = rand::rng().random_range(0..self.project2ep.shards().len());
debug!(shard, "project_info_cache: performing epoch reclamation");
// acquire a random shard lock

View File

@@ -523,29 +523,29 @@ mod tests {
fn generate_request_data(rng: &mut impl Rng) -> RequestData {
RequestData {
session_id: uuid::Builder::from_random_bytes(rng.r#gen()).into_uuid(),
peer_addr: Ipv4Addr::from(rng.r#gen::<[u8; 4]>()).to_string(),
session_id: uuid::Builder::from_random_bytes(rng.random()).into_uuid(),
peer_addr: Ipv4Addr::from(rng.random::<[u8; 4]>()).to_string(),
timestamp: chrono::DateTime::from_timestamp_millis(
rng.gen_range(1703862754..1803862754),
rng.random_range(1703862754..1803862754),
)
.unwrap()
.naive_utc(),
application_name: Some("test".to_owned()),
user_agent: Some("test-user-agent".to_owned()),
username: Some(hex::encode(rng.r#gen::<[u8; 4]>())),
endpoint_id: Some(hex::encode(rng.r#gen::<[u8; 16]>())),
database: Some(hex::encode(rng.r#gen::<[u8; 16]>())),
project: Some(hex::encode(rng.r#gen::<[u8; 16]>())),
branch: Some(hex::encode(rng.r#gen::<[u8; 16]>())),
username: Some(hex::encode(rng.random::<[u8; 4]>())),
endpoint_id: Some(hex::encode(rng.random::<[u8; 16]>())),
database: Some(hex::encode(rng.random::<[u8; 16]>())),
project: Some(hex::encode(rng.random::<[u8; 16]>())),
branch: Some(hex::encode(rng.random::<[u8; 16]>())),
pg_options: None,
auth_method: None,
jwt_issuer: None,
protocol: ["tcp", "ws", "http"][rng.gen_range(0..3)],
protocol: ["tcp", "ws", "http"][rng.random_range(0..3)],
region: String::new(),
error: None,
success: rng.r#gen(),
success: rng.random(),
cold_start_info: "no",
duration_us: rng.gen_range(0..30_000_000),
duration_us: rng.random_range(0..30_000_000),
disconnect_timestamp: None,
}
}
@@ -622,15 +622,15 @@ mod tests {
assert_eq!(
file_stats,
[
(1313953, 3, 6000),
(1313942, 3, 6000),
(1314001, 3, 6000),
(1313958, 3, 6000),
(1314094, 3, 6000),
(1313931, 3, 6000),
(1313725, 3, 6000),
(1313960, 3, 6000),
(438318, 1, 2000)
(1313878, 3, 6000),
(1313891, 3, 6000),
(1314058, 3, 6000),
(1313914, 3, 6000),
(1313760, 3, 6000),
(1314084, 3, 6000),
(1313965, 3, 6000),
(1313911, 3, 6000),
(438290, 1, 2000)
]
);
@@ -662,11 +662,11 @@ mod tests {
assert_eq!(
file_stats,
[
(1205810, 5, 10000),
(1205534, 5, 10000),
(1205835, 5, 10000),
(1205820, 5, 10000),
(1206074, 5, 10000)
(1206039, 5, 10000),
(1205798, 5, 10000),
(1205776, 5, 10000),
(1206051, 5, 10000),
(1205746, 5, 10000)
]
);
@@ -691,15 +691,15 @@ mod tests {
assert_eq!(
file_stats,
[
(1313953, 3, 6000),
(1313942, 3, 6000),
(1314001, 3, 6000),
(1313958, 3, 6000),
(1314094, 3, 6000),
(1313931, 3, 6000),
(1313725, 3, 6000),
(1313960, 3, 6000),
(438318, 1, 2000)
(1313878, 3, 6000),
(1313891, 3, 6000),
(1314058, 3, 6000),
(1313914, 3, 6000),
(1313760, 3, 6000),
(1314084, 3, 6000),
(1313965, 3, 6000),
(1313911, 3, 6000),
(438290, 1, 2000)
]
);
@@ -736,7 +736,7 @@ mod tests {
// files are smaller than the size threshold, but they took too long to fill so were flushed early
assert_eq!(
file_stats,
[(658584, 2, 3001), (658298, 2, 3000), (658094, 2, 2999)]
[(658552, 2, 3001), (658265, 2, 3000), (658061, 2, 2999)]
);
tmpdir.close().unwrap();

View File

@@ -247,7 +247,7 @@ mod tests {
use rand::{Rng, SeedableRng};
use rand_distr::Zipf;
let endpoint_dist = Zipf::new(500000, 0.8).unwrap();
let endpoint_dist = Zipf::new(500000.0, 0.8).unwrap();
let endpoints = StdRng::seed_from_u64(272488357).sample_iter(endpoint_dist);
let interner = MyId::get_interner();

View File

@@ -7,7 +7,7 @@ use std::io::{self, Cursor};
use bytes::{Buf, BufMut};
use itertools::Itertools;
use rand::distributions::{Distribution, Standard};
use rand::distr::{Distribution, StandardUniform};
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
use zerocopy::{FromBytes, Immutable, IntoBytes, big_endian};
@@ -458,9 +458,9 @@ impl fmt::Display for CancelKeyData {
.finish()
}
}
impl Distribution<CancelKeyData> for Standard {
impl Distribution<CancelKeyData> for StandardUniform {
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> CancelKeyData {
id_to_cancel_key(rng.r#gen())
id_to_cancel_key(rng.random())
}
}

View File

@@ -338,8 +338,8 @@ async fn scram_auth_mock() -> anyhow::Result<()> {
let proxy = tokio::spawn(dummy_proxy(client, Some(server_config), Scram::mock()));
use rand::Rng;
use rand::distributions::Alphanumeric;
let password: String = rand::thread_rng()
use rand::distr::Alphanumeric;
let password: String = rand::rng()
.sample_iter(&Alphanumeric)
.take(rand::random::<u8>() as usize)
.map(char::from)

View File

@@ -3,7 +3,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
use ahash::RandomState;
use clashmap::ClashMap;
use rand::{Rng, thread_rng};
use rand::Rng;
use tokio::time::Instant;
use tracing::info;
use utils::leaky_bucket::LeakyBucketState;
@@ -61,7 +61,7 @@ impl<K: Hash + Eq> LeakyBucketRateLimiter<K> {
self.map.len()
);
let n = self.map.shards().len();
let shard = thread_rng().gen_range(0..n);
let shard = rand::rng().random_range(0..n);
self.map.shards()[shard]
.write()
.retain(|(_, value)| !value.bucket_is_empty(now));

View File

@@ -147,7 +147,7 @@ impl RateBucketInfo {
impl<K: Hash + Eq> BucketRateLimiter<K> {
pub fn new(info: impl Into<Cow<'static, [RateBucketInfo]>>) -> Self {
Self::new_with_rand_and_hasher(info, StdRng::from_entropy(), RandomState::new())
Self::new_with_rand_and_hasher(info, StdRng::from_os_rng(), RandomState::new())
}
}
@@ -216,7 +216,7 @@ impl<K: Hash + Eq, R: Rng, S: BuildHasher + Clone> BucketRateLimiter<K, R, S> {
let n = self.map.shards().len();
// this lock is ok as the periodic cycle of do_gc makes this very unlikely to collide
// (impossible, infact, unless we have 2048 threads)
let shard = self.rand.lock_propagate_poison().gen_range(0..n);
let shard = self.rand.lock_propagate_poison().random_range(0..n);
self.map.shards()[shard].write().clear();
}
}

View File

@@ -86,11 +86,11 @@ mod tests {
for _ in 0..n {
// number to insert at once
let n = rng.gen_range(1..4096);
let n = rng.random_range(1..4096);
// number of insert operations
let m = rng.gen_range(1..100);
let m = rng.random_range(1..100);
let id = uuid::Builder::from_random_bytes(rng.r#gen()).into_uuid();
let id = uuid::Builder::from_random_bytes(rng.random()).into_uuid();
ids.push((id, n, m));
// N = sum(actual)
@@ -140,8 +140,8 @@ mod tests {
// probably numbers are too small to truly represent the probabilities.
assert_eq!(eval_precision(100, 4096.0, 0.90), 100);
assert_eq!(eval_precision(1000, 4096.0, 0.90), 1000);
assert_eq!(eval_precision(100, 4096.0, 0.1), 96);
assert_eq!(eval_precision(1000, 4096.0, 0.1), 988);
assert_eq!(eval_precision(100, 4096.0, 0.1), 100);
assert_eq!(eval_precision(1000, 4096.0, 0.1), 978);
}
// returns memory usage in bytes, and the time complexity per insert.

View File

@@ -51,7 +51,7 @@ impl ThreadPool {
*state = Some(ThreadRt {
pool: pool.clone(),
id: ThreadPoolWorkerId(worker_id.fetch_add(1, Ordering::Relaxed)),
rng: SmallRng::from_entropy(),
rng: SmallRng::from_os_rng(),
// used to determine whether we should temporarily skip tasks for fairness.
// 99% of estimates will overcount by no more than 4096 samples
countmin: CountMinSketch::with_params(
@@ -120,7 +120,7 @@ impl ThreadRt {
// in which case the SKETCH_RESET_INTERVAL represents 1 second. Thus, the rates above
// are in requests per second.
let probability = P.ln() / (P + rate as f64).ln();
self.rng.gen_bool(probability)
self.rng.random_bool(probability)
}
}

View File

@@ -8,7 +8,7 @@ use ed25519_dalek::SigningKey;
use hyper_util::rt::{TokioExecutor, TokioIo, TokioTimer};
use jose_jwk::jose_b64;
use postgres_client::config::SslMode;
use rand::rngs::OsRng;
use rand_core::OsRng;
use rustls::pki_types::{DnsName, ServerName};
use tokio::net::{TcpStream, lookup_host};
use tokio_rustls::TlsConnector;

View File

@@ -6,7 +6,7 @@ use std::time::Duration;
use indexmap::IndexMap;
use parking_lot::Mutex;
use rand::{Rng, thread_rng};
use rand::distr::uniform::{UniformSampler, UniformUsize};
use rustc_hash::FxHasher;
use tokio::time::Instant;
use tokio_util::sync::CancellationToken;
@@ -39,8 +39,9 @@ impl CancelSet {
}
pub(crate) fn take(&self) -> Option<CancellationToken> {
let dist = UniformUsize::new_inclusive(0, usize::MAX).expect("valid bounds");
for _ in 0..4 {
if let Some(token) = self.take_raw(thread_rng().r#gen()) {
if let Some(token) = self.take_raw(dist.sample(&mut rand::rng())) {
return Some(token);
}
tracing::trace!("failed to get cancel token");
@@ -48,7 +49,7 @@ impl CancelSet {
None
}
pub(crate) fn take_raw(&self, rng: usize) -> Option<CancellationToken> {
fn take_raw(&self, rng: usize) -> Option<CancellationToken> {
NonZeroUsize::new(self.shards.len())
.and_then(|len| self.shards[rng % len].lock().take(rng / len))
}

View File

@@ -428,7 +428,7 @@ where
loop {
interval.tick().await;
let shard = rng.gen_range(0..self.global_pool.shards().len());
let shard = rng.random_range(0..self.global_pool.shards().len());
self.gc(shard);
}
}

View File

@@ -77,7 +77,7 @@ pub async fn task_main(
{
let conn_pool = Arc::clone(&conn_pool);
tokio::spawn(async move {
conn_pool.gc_worker(StdRng::from_entropy()).await;
conn_pool.gc_worker(StdRng::from_os_rng()).await;
});
}
@@ -97,7 +97,7 @@ pub async fn task_main(
{
let http_conn_pool = Arc::clone(&http_conn_pool);
tokio::spawn(async move {
http_conn_pool.gc_worker(StdRng::from_entropy()).await;
http_conn_pool.gc_worker(StdRng::from_os_rng()).await;
});
}