Skip to content

Commit

Permalink
Upgrade to rand 0.9 (#1525)
Browse files Browse the repository at this point in the history
* Upgrade to rand 0.9

* fix Cargo.lock

* fix build errors

* fix fmt

* fix benchmark build
  • Loading branch information
gkorland authored Feb 13, 2025
1 parent 530269b commit 0c72d64
Show file tree
Hide file tree
Showing 8 changed files with 470 additions and 429 deletions.
850 changes: 448 additions & 402 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion redis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ r2d2 = { version = "0.8.10", optional = true }

# Only needed for cluster
crc16 = { version = "0.4", optional = true }
rand = { version = "0.8", optional = true }
rand = { version = "0.9", optional = true }

# Only needed for cluster-async
futures-sink = { version = "0.3.31", optional = true }
Expand Down
12 changes: 6 additions & 6 deletions redis/benches/bench_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ use support::*;
mod support;

use rand::{
distributions::{Bernoulli, Distribution},
distr::{Bernoulli, Distribution},
Rng,
};
use redis::caching::CacheConfig;
use std::env;
fn generate_commands(key: String, total_command: u32, total_read: u32) -> Vec<Cmd> {
let mut cmds = vec![];
let mut rng = rand::thread_rng();
let mut rng = rand::rng();
let distribution = Bernoulli::from_ratio(total_read, total_command).unwrap();
for is_read in distribution
.sample_iter(&mut rand::thread_rng())
.sample_iter(&mut rand::rng())
.take(total_command as usize)
{
if is_read {
Expand All @@ -26,7 +26,7 @@ fn generate_commands(key: String, total_command: u32, total_read: u32) -> Vec<Cm
cmds.push(
redis::cmd("SET")
.arg(&key)
.arg(rng.gen_range(1..1000))
.arg(rng.random_range(1..1000))
.clone(),
);
}
Expand All @@ -49,11 +49,11 @@ async fn benchmark_executer(
ctx.multiplexed_async_connection_tokio().await.unwrap()
};

let mut rng = rand::thread_rng();
let mut rng = rand::rng();
let mut handles = Vec::new();
for _ in 0..key_count {
let mut con = con.clone();
let key = format!("{}", rng.gen_range(1..1000));
let key = format!("{}", rng.random_range(1..1000));
handles.push(tokio::spawn(async move {
for cmd in generate_commands(
key,
Expand Down
8 changes: 4 additions & 4 deletions redis/src/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ use crate::{
cluster_client::ClusterParams,
cluster_routing::{Redirect, Route, RoutingInfo, SlotMap, SLOT_SIZE},
};
use rand::{seq::IteratorRandom, thread_rng, Rng};
use rand::{rng, seq::IteratorRandom, Rng};

pub use crate::cluster_client::{ClusterClient, ClusterClientBuilder};
pub use crate::cluster_pipeline::{cluster_pipe, ClusterPipeline};
Expand Down Expand Up @@ -539,9 +539,9 @@ where

match RoutingInfo::for_routable(cmd) {
Some(RoutingInfo::SingleNode(SingleNodeRoutingInfo::Random)) => {
let mut rng = thread_rng();
let mut rng = rng();
Ok(addr_for_slot(Route::new(
rng.gen_range(0..SLOT_SIZE),
rng.random_range(0..SLOT_SIZE),
SlotAddr::Master,
))?)
}
Expand Down Expand Up @@ -1053,7 +1053,7 @@ impl NodeCmd {
fn get_random_connection<C: ConnectionLike + Connect + Sized>(
connections: &mut HashMap<String, C>,
) -> Option<(String, &mut C)> {
let addr = connections.keys().choose(&mut thread_rng())?.to_string();
let addr = connections.keys().choose(&mut rng())?.to_string();
let conn = connections.get_mut(&addr)?;
Some((addr, conn))
}
Expand Down
15 changes: 6 additions & 9 deletions redis/src/cluster_async/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ use futures_util::{
stream::{self, Stream, StreamExt},
};
use log::{trace, warn};
use rand::{seq::IteratorRandom, thread_rng};
use rand::{rng, seq::IteratorRandom};
use request::{CmdArg, PendingRequest, Request, RequestState, Retry};
use routing::{route_for_pipeline, InternalRoutingInfo, InternalSingleNodeRouting};
use tokio::sync::{mpsc, oneshot, RwLock};
Expand Down Expand Up @@ -1384,12 +1384,9 @@ fn get_random_connection<C>(connections: &ConnectionMap<C>) -> Option<(String, C
where
C: Clone,
{
connections
.keys()
.choose(&mut thread_rng())
.and_then(|addr| {
connections
.get(addr)
.map(|conn| (addr.clone(), conn.clone()))
})
connections.keys().choose(&mut rng()).and_then(|addr| {
connections
.get(addr)
.map(|conn| (addr.clone(), conn.clone()))
})
}
2 changes: 1 addition & 1 deletion redis/src/cluster_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl RetryParams {
let clamped_wait = base_wait
.min(self.max_wait_time)
.max(self.min_wait_time + 1);
let jittered_wait = rand::thread_rng().gen_range(self.min_wait_time..clamped_wait);
let jittered_wait = rand::rng().random_range(self.min_wait_time..clamped_wait);
Duration::from_millis(jittered_wait)
}
}
Expand Down
8 changes: 3 additions & 5 deletions redis/src/cluster_routing.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::cmp::min;
use std::collections::{BTreeMap, HashMap, HashSet};

use rand::seq::SliceRandom;
use rand::thread_rng;
use rand::prelude::IndexedRandom;
use rand::rng;

use crate::cmd::{Arg, Cmd};
use crate::commands::is_readonly_cmd;
Expand Down Expand Up @@ -635,9 +635,7 @@ impl SlotAddrs {
}

fn get_replica_node(&self) -> &str {
self.replicas
.choose(&mut thread_rng())
.unwrap_or(&self.primary)
self.replicas.choose(&mut rng()).unwrap_or(&self.primary)
}

pub(crate) fn slot_addr(&self, slot_addr: &SlotAddr, read_from_replica: bool) -> &str {
Expand Down
2 changes: 1 addition & 1 deletion redis/src/sentinel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ fn is_replica_valid(replica_info: &HashMap<String, String>) -> bool {

/// Generates a random value in the 0..max range.
fn random_replica_index(max: NonZeroUsize) -> usize {
rand::thread_rng().gen_range(0..max.into())
rand::rng().random_range(0..max.into())
}

fn try_connect_to_first_replica(
Expand Down

0 comments on commit 0c72d64

Please sign in to comment.