use redis_tower_core::Frame;
use std::collections::HashMap;
use std::sync::Mutex;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::{Duration, Instant};
use thiserror::Error;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct NodeAddr {
pub host: String,
pub port: u16,
}
impl NodeAddr {
pub fn new(host: impl Into<String>, port: u16) -> Self {
Self {
host: host.into(),
port,
}
}
pub fn addr_string(&self) -> String {
format!("{}:{}", self.host, self.port)
}
pub fn parse(addr: &str) -> Option<Self> {
let (host, port) = addr.rsplit_once(':')?;
if host.is_empty() {
return None;
}
let port = port.parse().ok()?;
Some(Self {
host: host.to_string(),
port,
})
}
}
impl std::fmt::Display for NodeAddr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}", self.host, self.port)
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum ReadPreference {
#[default]
Master,
Replica,
PreferReplica,
}
pub trait ReadRoutingStrategy: Send + Sync + 'static {
fn select_replica<'a>(&self, slot: u16, replicas: &'a [NodeAddr]) -> Option<&'a NodeAddr>;
fn record_outcome(
&self,
_replica: &NodeAddr,
_latency: Duration,
_outcome: ReplicaRoutingOutcome,
) {
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReplicaRoutingOutcome {
Success,
Failure,
}
#[derive(Debug, Error, PartialEq)]
pub enum AdaptiveReplicaRoutingConfigError {
#[error("EWMA alpha must be finite and in the interval (0, 1]")]
InvalidEwmaAlpha,
#[error("replica failure threshold must be greater than zero")]
ZeroFailureThreshold,
#[error("replica ejection duration must be greater than zero")]
ZeroEjectionDuration,
#[error("availability-zone names must not be empty")]
EmptyAvailabilityZone,
}
#[derive(Debug, Clone)]
pub struct AdaptiveReplicaRoutingBuilder {
local_zone: Option<String>,
replica_zones: HashMap<NodeAddr, String>,
ewma_alpha: f64,
failure_threshold: u32,
ejection_duration: Duration,
minimum_healthy_replicas: usize,
}
impl Default for AdaptiveReplicaRoutingBuilder {
fn default() -> Self {
Self {
local_zone: None,
replica_zones: HashMap::new(),
ewma_alpha: 0.2,
failure_threshold: 3,
ejection_duration: Duration::from_secs(30),
minimum_healthy_replicas: 1,
}
}
}
impl AdaptiveReplicaRoutingBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn local_zone(mut self, zone: impl Into<String>) -> Self {
self.local_zone = Some(zone.into());
self
}
pub fn replica_zone(mut self, replica: NodeAddr, zone: impl Into<String>) -> Self {
self.replica_zones.insert(replica, zone.into());
self
}
pub fn ewma_alpha(mut self, alpha: f64) -> Self {
self.ewma_alpha = alpha;
self
}
pub fn failure_threshold(mut self, failures: u32) -> Self {
self.failure_threshold = failures;
self
}
pub fn ejection_duration(mut self, duration: Duration) -> Self {
self.ejection_duration = duration;
self
}
pub fn minimum_healthy_replicas(mut self, minimum: usize) -> Self {
self.minimum_healthy_replicas = minimum;
self
}
pub fn build(self) -> Result<AdaptiveReplicaRouting, AdaptiveReplicaRoutingConfigError> {
if !self.ewma_alpha.is_finite() || !(0.0 < self.ewma_alpha && self.ewma_alpha <= 1.0) {
return Err(AdaptiveReplicaRoutingConfigError::InvalidEwmaAlpha);
}
if self.failure_threshold == 0 {
return Err(AdaptiveReplicaRoutingConfigError::ZeroFailureThreshold);
}
if self.ejection_duration.is_zero() {
return Err(AdaptiveReplicaRoutingConfigError::ZeroEjectionDuration);
}
if self.local_zone.as_deref().is_some_and(str::is_empty)
|| self.replica_zones.values().any(String::is_empty)
{
return Err(AdaptiveReplicaRoutingConfigError::EmptyAvailabilityZone);
}
Ok(AdaptiveReplicaRouting {
local_zone: self.local_zone,
replica_zones: self.replica_zones,
ewma_alpha: self.ewma_alpha,
failure_threshold: self.failure_threshold,
ejection_duration: self.ejection_duration,
minimum_healthy_replicas: self.minimum_healthy_replicas,
state: Mutex::new(AdaptiveRoutingState::default()),
})
}
}
#[derive(Debug)]
pub struct AdaptiveReplicaRouting {
local_zone: Option<String>,
replica_zones: HashMap<NodeAddr, String>,
ewma_alpha: f64,
failure_threshold: u32,
ejection_duration: Duration,
minimum_healthy_replicas: usize,
state: Mutex<AdaptiveRoutingState>,
}
#[derive(Debug, Default)]
struct AdaptiveRoutingState {
replicas: HashMap<NodeAddr, AdaptiveReplicaState>,
sequence: u64,
}
#[derive(Debug, Default)]
struct AdaptiveReplicaState {
ewma_latency_seconds: Option<f64>,
consecutive_failures: u32,
ejected_until: Option<Instant>,
}
impl AdaptiveReplicaRouting {
pub fn builder() -> AdaptiveReplicaRoutingBuilder {
AdaptiveReplicaRoutingBuilder::new()
}
fn select_replica_at<'a>(
&self,
replicas: &'a [NodeAddr],
now: Instant,
) -> Option<&'a NodeAddr> {
if replicas.is_empty() {
return None;
}
let mut state = self
.state
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
let mut eligible = Vec::with_capacity(replicas.len());
let mut ejected = Vec::new();
for (index, replica) in replicas.iter().enumerate() {
let replica_state = state.replicas.entry(replica.clone()).or_default();
match replica_state.ejected_until {
Some(until) if until > now => ejected.push((index, until)),
Some(_) => {
replica_state.ejected_until = None;
replica_state.consecutive_failures = 0;
replica_state.ewma_latency_seconds = None;
eligible.push(index);
}
None => eligible.push(index),
}
}
let floor = self.minimum_healthy_replicas.min(replicas.len());
if eligible.len() < floor {
ejected.sort_by_key(|(index, until)| (*until, *index));
eligible.extend(
ejected
.into_iter()
.take(floor - eligible.len())
.map(|(index, _until)| index),
);
}
if let Some(local_zone) = self.local_zone.as_deref() {
let local: Vec<usize> = eligible
.iter()
.copied()
.filter(|index| {
self.replica_zones
.get(&replicas[*index])
.is_some_and(|zone| zone == local_zone)
})
.collect();
if !local.is_empty() {
eligible = local;
}
}
let unknown: Vec<usize> = eligible
.iter()
.copied()
.filter(|index| {
state
.replicas
.get(&replicas[*index])
.and_then(|replica| replica.ewma_latency_seconds)
.is_none()
})
.collect();
if !unknown.is_empty() {
let selected = unknown[state.sequence as usize % unknown.len()];
state.sequence = state.sequence.wrapping_add(1);
return Some(&replicas[selected]);
}
let total_weight: f64 = eligible
.iter()
.map(|index| {
let latency = state.replicas[&replicas[*index]]
.ewma_latency_seconds
.expect("eligible latency was checked above");
1.0 / latency.max(1e-9)
})
.sum();
let sample = splitmix64(state.sequence) as f64 / u64::MAX as f64;
state.sequence = state.sequence.wrapping_add(1);
let mut ticket = sample * total_weight;
for index in eligible.iter().copied() {
let latency = state.replicas[&replicas[index]]
.ewma_latency_seconds
.expect("eligible latency was checked above");
let weight = 1.0 / latency.max(1e-9);
if ticket < weight {
return Some(&replicas[index]);
}
ticket -= weight;
}
eligible.last().map(|index| &replicas[*index])
}
fn record_outcome_at(
&self,
replica: &NodeAddr,
latency: Duration,
outcome: ReplicaRoutingOutcome,
now: Instant,
) {
let mut state = self
.state
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
let replica_state = state.replicas.entry(replica.clone()).or_default();
match outcome {
ReplicaRoutingOutcome::Success => {
let sample = latency.as_secs_f64();
replica_state.ewma_latency_seconds = Some(
replica_state
.ewma_latency_seconds
.map_or(sample, |current| {
self.ewma_alpha * sample + (1.0 - self.ewma_alpha) * current
}),
);
replica_state.consecutive_failures = 0;
replica_state.ejected_until = None;
}
ReplicaRoutingOutcome::Failure => {
replica_state.consecutive_failures =
replica_state.consecutive_failures.saturating_add(1);
if replica_state.consecutive_failures >= self.failure_threshold {
replica_state.ejected_until = now.checked_add(self.ejection_duration);
}
}
}
}
}
impl ReadRoutingStrategy for AdaptiveReplicaRouting {
fn select_replica<'a>(&self, _slot: u16, replicas: &'a [NodeAddr]) -> Option<&'a NodeAddr> {
self.select_replica_at(replicas, Instant::now())
}
fn record_outcome(
&self,
replica: &NodeAddr,
latency: Duration,
outcome: ReplicaRoutingOutcome,
) {
self.record_outcome_at(replica, latency, outcome, Instant::now());
}
}
fn splitmix64(mut value: u64) -> u64 {
value = value.wrapping_add(0x9e37_79b9_7f4a_7c15);
value = (value ^ (value >> 30)).wrapping_mul(0xbf58_476d_1ce4_e5b9);
value = (value ^ (value >> 27)).wrapping_mul(0x94d0_49bb_1331_11eb);
value ^ (value >> 31)
}
pub struct RoundRobinRouting {
counter: AtomicUsize,
}
impl RoundRobinRouting {
pub fn new() -> Self {
Self {
counter: AtomicUsize::new(0),
}
}
}
impl Default for RoundRobinRouting {
fn default() -> Self {
Self::new()
}
}
impl ReadRoutingStrategy for RoundRobinRouting {
fn select_replica<'a>(&self, _slot: u16, replicas: &'a [NodeAddr]) -> Option<&'a NodeAddr> {
if replicas.is_empty() {
return None;
}
let idx = self.counter.fetch_add(1, Ordering::Relaxed) % replicas.len();
Some(&replicas[idx])
}
}
pub struct RandomRouting {
counter: AtomicUsize,
}
impl RandomRouting {
pub fn new() -> Self {
let seed = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_nanos() as usize)
.unwrap_or(0);
Self {
counter: AtomicUsize::new(seed),
}
}
}
impl Default for RandomRouting {
fn default() -> Self {
Self::new()
}
}
impl ReadRoutingStrategy for RandomRouting {
fn select_replica<'a>(&self, _slot: u16, replicas: &'a [NodeAddr]) -> Option<&'a NodeAddr> {
if replicas.is_empty() {
return None;
}
let val = self.counter.fetch_add(7919, Ordering::Relaxed);
let idx = val % replicas.len();
Some(&replicas[idx])
}
}
pub struct FirstReplicaRouting;
impl ReadRoutingStrategy for FirstReplicaRouting {
fn select_replica<'a>(&self, _slot: u16, replicas: &'a [NodeAddr]) -> Option<&'a NodeAddr> {
replicas.first()
}
}
pub fn is_readonly_command(frame: &Frame) -> bool {
let items = match frame {
Frame::Array(Some(items)) if !items.is_empty() => items,
_ => return false,
};
let cmd_name = match &items[0] {
Frame::BulkString(Some(b)) => b.as_ref(),
_ => return false,
};
let mut buf = [0u8; 24];
if cmd_name.len() > buf.len() {
return false;
}
for (i, b) in cmd_name.iter().enumerate() {
buf[i] = b.to_ascii_uppercase();
}
matches!(
&buf[..cmd_name.len()],
b"GET" | b"GETRANGE" | b"SUBSTR" | b"MGET" | b"STRLEN" | b"LCS" | b"DIGEST"
| b"GETBIT" | b"BITCOUNT" | b"BITPOS" | b"BITFIELD_RO"
| b"EXISTS" | b"TYPE" | b"TTL" | b"PTTL" | b"EXPIRETIME" | b"PEXPIRETIME"
| b"DUMP" | b"OBJECT" | b"MEMORY" | b"SORT_RO"
| b"HGET" | b"HGETALL" | b"HKEYS" | b"HVALS" | b"HLEN" | b"HEXISTS"
| b"HMGET" | b"HSTRLEN" | b"HRANDFIELD" | b"HSCAN"
| b"LRANGE" | b"LLEN" | b"LINDEX" | b"LPOS"
| b"SMEMBERS" | b"SISMEMBER" | b"SMISMEMBER" | b"SCARD" | b"SINTER"
| b"SINTERCARD" | b"SUNION" | b"SDIFF" | b"SRANDMEMBER" | b"SSCAN"
| b"ZRANGE" | b"ZRANGEBYSCORE" | b"ZRANGEBYLEX" | b"ZREVRANGE"
| b"ZREVRANGEBYSCORE" | b"ZREVRANGEBYLEX" | b"ZSCORE" | b"ZMSCORE"
| b"ZCARD" | b"ZRANK" | b"ZREVRANK" | b"ZCOUNT" | b"ZLEXCOUNT"
| b"ZRANDMEMBER" | b"ZSCAN" | b"ZDIFF" | b"ZINTER" | b"ZUNION"
| b"ZINTERCARD"
| b"XLEN" | b"XRANGE" | b"XREVRANGE" | b"XREAD" | b"XINFO" | b"XPENDING"
| b"ARCOUNT" | b"ARGET" | b"ARGETRANGE" | b"ARGREP" | b"ARINFO"
| b"ARLASTITEMS" | b"ARLEN" | b"ARMGET" | b"ARNEXT" | b"AROP" | b"ARSCAN"
| b"GEOPOS" | b"GEODIST" | b"GEOHASH" | b"GEOSEARCH"
| b"GEORADIUS_RO" | b"GEORADIUSBYMEMBER_RO"
| b"PFCOUNT"
| b"EVAL_RO" | b"EVALSHA_RO" | b"FCALL_RO"
| b"DBSIZE" | b"PING" | b"ECHO" | b"INFO"
| b"JSON.GET" | b"JSON.MGET" | b"JSON.TYPE" | b"JSON.STRLEN"
| b"JSON.ARRLEN" | b"JSON.ARRINDEX" | b"JSON.OBJLEN" | b"JSON.OBJKEYS"
| b"JSON.RESP"
| b"FT.SEARCH" | b"FT.AGGREGATE" | b"FT.INFO" | b"FT.PROFILE"
| b"FT.EXPLAIN" | b"FT.EXPLAINCLI" | b"FT.HYBRID" | b"FT.TAGVALS"
| b"TS.GET" | b"TS.MGET" | b"TS.RANGE" | b"TS.REVRANGE" | b"TS.MRANGE"
| b"TS.MREVRANGE" | b"TS.INFO" | b"TS.QUERYINDEX"
| b"BF.EXISTS" | b"BF.MEXISTS" | b"BF.INFO" | b"BF.CARD"
| b"CF.EXISTS" | b"CF.COUNT" | b"CF.INFO"
| b"CMS.QUERY" | b"CMS.INFO"
| b"TOPK.QUERY" | b"TOPK.COUNT" | b"TOPK.LIST" | b"TOPK.INFO"
| b"TDIGEST.MIN" | b"TDIGEST.MAX" | b"TDIGEST.QUANTILE"
| b"TDIGEST.CDF" | b"TDIGEST.RANK" | b"TDIGEST.INFO"
| b"VSIM" | b"VCARD" | b"VDIM" | b"VEMB" | b"VGETATTR" | b"VLINKS"
| b"VINFO" | b"VISMEMBER" | b"VRANGE"
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn node_addr_display() {
let addr = NodeAddr {
host: "127.0.0.1".to_string(),
port: 7000,
};
assert_eq!(addr.to_string(), "127.0.0.1:7000");
assert_eq!(addr.addr_string(), "127.0.0.1:7000");
}
#[test]
fn node_addr_equality() {
let a = NodeAddr {
host: "127.0.0.1".to_string(),
port: 7000,
};
let b = NodeAddr {
host: "127.0.0.1".to_string(),
port: 7000,
};
let c = NodeAddr {
host: "127.0.0.1".to_string(),
port: 7001,
};
assert_eq!(a, b);
assert_ne!(a, c);
}
#[test]
fn node_addr_parse_roundtrips() {
let addr = NodeAddr::parse("127.0.0.1:6380").unwrap();
assert_eq!(addr.host, "127.0.0.1");
assert_eq!(addr.port, 6380);
let ipv6 = NodeAddr::parse("[::1]:6380").unwrap();
assert_eq!(ipv6.addr_string(), "[::1]:6380");
}
#[test]
fn node_addr_parse_rejects_missing_port() {
assert!(NodeAddr::parse("127.0.0.1").is_none());
assert!(NodeAddr::parse(":6379").is_none());
}
#[test]
fn node_addr_parse_rejects_non_numeric_port() {
assert!(NodeAddr::parse("127.0.0.1:notaport").is_none());
}
#[test]
fn read_preference_default_is_master() {
assert_eq!(ReadPreference::default(), ReadPreference::Master);
}
#[test]
fn read_preference_variants() {
assert_ne!(ReadPreference::Master, ReadPreference::Replica);
assert_ne!(ReadPreference::Replica, ReadPreference::PreferReplica);
assert_ne!(ReadPreference::Master, ReadPreference::PreferReplica);
}
fn make_replicas() -> Vec<NodeAddr> {
vec![
NodeAddr {
host: "10.0.0.1".to_string(),
port: 7001,
},
NodeAddr {
host: "10.0.0.2".to_string(),
port: 7002,
},
NodeAddr {
host: "10.0.0.3".to_string(),
port: 7003,
},
]
}
#[test]
fn round_robin_distributes_across_replicas() {
let strategy = RoundRobinRouting::new();
let replicas = make_replicas();
let first = strategy.select_replica(0, &replicas).unwrap();
let second = strategy.select_replica(0, &replicas).unwrap();
let third = strategy.select_replica(0, &replicas).unwrap();
let fourth = strategy.select_replica(0, &replicas).unwrap();
assert_eq!(first.port, 7001);
assert_eq!(second.port, 7002);
assert_eq!(third.port, 7003);
assert_eq!(fourth.port, 7001);
}
#[test]
fn round_robin_returns_none_for_empty_replicas() {
let strategy = RoundRobinRouting::new();
assert!(strategy.select_replica(0, &[]).is_none());
}
#[test]
fn random_routing_returns_valid_replica() {
let strategy = RandomRouting::new();
let replicas = make_replicas();
for _ in 0..100 {
let selected = strategy.select_replica(0, &replicas).unwrap();
assert!(
replicas.contains(selected),
"selected replica not in list: {selected:?}"
);
}
}
#[test]
fn random_routing_returns_none_for_empty_replicas() {
let strategy = RandomRouting::new();
assert!(strategy.select_replica(0, &[]).is_none());
}
#[test]
fn adaptive_builder_rejects_unsafe_configuration() {
assert_eq!(
AdaptiveReplicaRouting::builder()
.ewma_alpha(0.0)
.build()
.unwrap_err(),
AdaptiveReplicaRoutingConfigError::InvalidEwmaAlpha
);
assert_eq!(
AdaptiveReplicaRouting::builder()
.failure_threshold(0)
.build()
.unwrap_err(),
AdaptiveReplicaRoutingConfigError::ZeroFailureThreshold
);
assert_eq!(
AdaptiveReplicaRouting::builder()
.ejection_duration(Duration::ZERO)
.build()
.unwrap_err(),
AdaptiveReplicaRoutingConfigError::ZeroEjectionDuration
);
assert_eq!(
AdaptiveReplicaRouting::builder()
.local_zone("")
.build()
.unwrap_err(),
AdaptiveReplicaRoutingConfigError::EmptyAvailabilityZone
);
}
#[test]
fn adaptive_routing_prefers_local_zone_and_falls_back_when_it_is_ejected() {
let replicas = make_replicas();
let local = replicas[0].clone();
let remote = replicas[1].clone();
let strategy = AdaptiveReplicaRouting::builder()
.local_zone("az-a")
.replica_zone(local.clone(), "az-a")
.replica_zone(remote.clone(), "az-b")
.failure_threshold(1)
.ejection_duration(Duration::from_secs(30))
.build()
.unwrap();
let now = Instant::now();
assert_eq!(
strategy.select_replica_at(&replicas[..2], now),
Some(&local)
);
strategy.record_outcome_at(
&local,
Duration::from_millis(5),
ReplicaRoutingOutcome::Failure,
now,
);
assert_eq!(
strategy.select_replica_at(&replicas[..2], now + Duration::from_secs(1)),
Some(&remote)
);
}
#[test]
fn adaptive_routing_biases_selection_by_inverse_ewma_latency() {
let replicas = make_replicas();
let fast = replicas[0].clone();
let slow = replicas[1].clone();
let strategy = AdaptiveReplicaRouting::builder()
.ewma_alpha(1.0)
.build()
.unwrap();
let now = Instant::now();
strategy.record_outcome_at(
&fast,
Duration::from_millis(1),
ReplicaRoutingOutcome::Success,
now,
);
strategy.record_outcome_at(
&slow,
Duration::from_millis(100),
ReplicaRoutingOutcome::Success,
now,
);
let mut fast_count = 0;
let mut slow_count = 0;
for _ in 0..4096 {
match strategy.select_replica_at(&replicas[..2], now) {
Some(selected) if selected == &fast => fast_count += 1,
Some(selected) if selected == &slow => slow_count += 1,
selected => panic!("unexpected selection: {selected:?}"),
}
}
assert!(
fast_count > slow_count * 20,
"fast={fast_count}, slow={slow_count}"
);
assert!(
slow_count > 0,
"weighted routing permanently starved the slower replica"
);
}
#[test]
fn adaptive_routing_requires_consecutive_failures_and_resets_on_success() {
let replica = make_replicas().remove(0);
let strategy = AdaptiveReplicaRouting::builder()
.failure_threshold(2)
.build()
.unwrap();
let now = Instant::now();
strategy.record_outcome_at(
&replica,
Duration::from_millis(1),
ReplicaRoutingOutcome::Failure,
now,
);
strategy.record_outcome_at(
&replica,
Duration::from_millis(1),
ReplicaRoutingOutcome::Success,
now,
);
strategy.record_outcome_at(
&replica,
Duration::from_millis(1),
ReplicaRoutingOutcome::Failure,
now,
);
let state = strategy.state.lock().unwrap();
let replica_state = &state.replicas[&replica];
assert_eq!(replica_state.consecutive_failures, 1);
assert!(replica_state.ejected_until.is_none());
}
#[test]
fn adaptive_routing_recovers_after_the_ejection_window() {
let replicas = make_replicas();
let local = replicas[0].clone();
let remote = replicas[1].clone();
let ejection = Duration::from_secs(10);
let strategy = AdaptiveReplicaRouting::builder()
.local_zone("az-a")
.replica_zone(local.clone(), "az-a")
.replica_zone(remote, "az-b")
.failure_threshold(1)
.ejection_duration(ejection)
.build()
.unwrap();
let now = Instant::now();
strategy.record_outcome_at(
&local,
Duration::from_millis(5),
ReplicaRoutingOutcome::Failure,
now,
);
assert_ne!(
strategy.select_replica_at(&replicas[..2], now + ejection - Duration::from_nanos(1)),
Some(&local)
);
assert_eq!(
strategy.select_replica_at(&replicas[..2], now + ejection),
Some(&local)
);
}
#[test]
fn adaptive_routing_floor_never_ejects_every_candidate() {
let replicas = make_replicas();
let strategy = AdaptiveReplicaRouting::builder()
.failure_threshold(1)
.minimum_healthy_replicas(1)
.build()
.unwrap();
let now = Instant::now();
for replica in &replicas {
strategy.record_outcome_at(
replica,
Duration::from_millis(5),
ReplicaRoutingOutcome::Failure,
now,
);
}
assert!(strategy.select_replica_at(&replicas, now).is_some());
}
#[test]
fn adaptive_routing_zero_floor_can_eject_every_candidate() {
let replicas = make_replicas();
let strategy = AdaptiveReplicaRouting::builder()
.failure_threshold(1)
.minimum_healthy_replicas(0)
.build()
.unwrap();
let now = Instant::now();
for replica in &replicas {
strategy.record_outcome_at(
replica,
Duration::from_millis(5),
ReplicaRoutingOutcome::Failure,
now,
);
}
assert!(strategy.select_replica_at(&replicas, now).is_none());
}
#[test]
fn first_replica_always_returns_first() {
let strategy = FirstReplicaRouting;
let replicas = make_replicas();
for _ in 0..10 {
let selected = strategy.select_replica(0, &replicas).unwrap();
assert_eq!(selected.port, 7001);
assert_eq!(selected.host, "10.0.0.1");
}
}
#[test]
fn first_replica_returns_none_for_empty_replicas() {
let strategy = FirstReplicaRouting;
assert!(strategy.select_replica(0, &[]).is_none());
}
#[test]
fn is_readonly_command_accepts_reads_and_rejects_writes() {
use redis_tower_protocol::helpers::{array, bulk};
assert!(is_readonly_command(&array(vec![bulk("GET"), bulk("key")])));
assert!(is_readonly_command(&array(vec![
bulk("HGETALL"),
bulk("h")
])));
assert!(!is_readonly_command(&array(vec![
bulk("SET"),
bulk("key"),
bulk("val")
])));
assert!(!is_readonly_command(&array(vec![bulk("GETEX"), bulk("k")])));
assert!(!is_readonly_command(&Frame::Array(None)));
}
#[test]
fn is_readonly_command_is_case_insensitive() {
use redis_tower_protocol::helpers::{array, bulk};
assert!(is_readonly_command(&array(vec![bulk("get"), bulk("k")])));
}
}