numinnex commented on code in PR #2450: URL: https://github.com/apache/iggy/pull/2450#discussion_r2599351817
########## core/consensus/src/vsr_timeout.rs: ########## @@ -0,0 +1,386 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! A deterministic, tick-based timeout mechanism for VSR consensus. +//! This module provides deterministic timeouts driven by logical ticks rather than +//! wall-clock time, enabling: +//! - Deterministic testing +//! - Predictable behavior in consensus +//! - Per-replica PRNG seeding for jitter +//! +//! Two-phase tick model: +//! +//! 1. Phase 1 (tick): All timeouts are advanced by one tick +//! 2. Phase 2 (check & handle): Check which timeouts fired and invoke handlers + +/// A deterministic, tick-based timeout. +/// +/// Timeouts count down from an initial duration (`after`) and fire when +/// reaching zero. They support exponential backoff with jitter for retries. +use rand::Rng; +use rand_xoshiro::Xoshiro256Plus; +use rand_xoshiro::rand_core::SeedableRng; + +#[derive(Debug, Clone)] +#[allow(unused)] +pub struct Timeout { + pub name: &'static str, + pub id: u128, + after: u64, + ticks_remaining: u64, + pub ticking: bool, + pub attempts: u32, +} + +impl Timeout { + pub fn new(name: &'static str, id: u128, after: u64) -> Self { + Self { + name, + id, + after, + ticks_remaining: 0, + ticking: false, + attempts: 0, + } + } + + pub fn start(&mut self) { + self.ticks_remaining = self.after; + self.ticking = true; + self.attempts = 0; + } + + pub fn stop(&mut self) { + self.ticking = false; + self.ticks_remaining = 0; + self.attempts = 0; + } + + pub fn reset(&mut self) { + self.ticks_remaining = self.after; + self.attempts = 0; + } + + pub fn tick(&mut self) { + if self.ticking { + self.ticks_remaining = self.ticks_remaining.saturating_sub(1); + } + } + + pub fn fired(&self) -> bool { + self.ticking && self.ticks_remaining == 0 + } + + pub fn backoff(&mut self, prng: &mut Xoshiro256Plus) { + self.attempts = self.attempts.wrapping_add(1); + let max_backoff = self.after.saturating_mul(16); + let shift = self.attempts.min(4); + let backoff = self.after.saturating_mul(1 << shift).min(max_backoff); + let jitter = prng.random_range(0..=backoff); + self.ticks_remaining = backoff.saturating_add(jitter); + } +} + +/// Timeout types in VSR. +#[allow(unused)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum TimeoutKind { + Ping, + Prepare, + PrimaryAbdicate, + CommitMessage, + NormalHeartbeat, + StartViewChangeWindow, + StartViewChangeMessage, + ViewChangeStatus, + DoViewChangeMessage, + RequestStartViewMessage, + Pulse, + Upgrade, Review Comment: We don't need these two. ########## core/consensus/src/vsr_timeout.rs: ########## @@ -0,0 +1,386 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! A deterministic, tick-based timeout mechanism for VSR consensus. +//! This module provides deterministic timeouts driven by logical ticks rather than +//! wall-clock time, enabling: +//! - Deterministic testing +//! - Predictable behavior in consensus +//! - Per-replica PRNG seeding for jitter +//! +//! Two-phase tick model: +//! +//! 1. Phase 1 (tick): All timeouts are advanced by one tick +//! 2. Phase 2 (check & handle): Check which timeouts fired and invoke handlers + +/// A deterministic, tick-based timeout. +/// +/// Timeouts count down from an initial duration (`after`) and fire when +/// reaching zero. They support exponential backoff with jitter for retries. +use rand::Rng; +use rand_xoshiro::Xoshiro256Plus; +use rand_xoshiro::rand_core::SeedableRng; + +#[derive(Debug, Clone)] +#[allow(unused)] +pub struct Timeout { + pub name: &'static str, Review Comment: since we have a `TimeoutKind`, I think we could use that instead of an name. ########## core/consensus/src/vsr_timeout.rs: ########## @@ -0,0 +1,386 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! A deterministic, tick-based timeout mechanism for VSR consensus. +//! This module provides deterministic timeouts driven by logical ticks rather than +//! wall-clock time, enabling: +//! - Deterministic testing +//! - Predictable behavior in consensus +//! - Per-replica PRNG seeding for jitter +//! +//! Two-phase tick model: +//! +//! 1. Phase 1 (tick): All timeouts are advanced by one tick +//! 2. Phase 2 (check & handle): Check which timeouts fired and invoke handlers + +/// A deterministic, tick-based timeout. +/// +/// Timeouts count down from an initial duration (`after`) and fire when +/// reaching zero. They support exponential backoff with jitter for retries. +use rand::Rng; +use rand_xoshiro::Xoshiro256Plus; +use rand_xoshiro::rand_core::SeedableRng; + +#[derive(Debug, Clone)] +#[allow(unused)] +pub struct Timeout { + pub name: &'static str, + pub id: u128, + after: u64, + ticks_remaining: u64, + pub ticking: bool, + pub attempts: u32, +} + +impl Timeout { + pub fn new(name: &'static str, id: u128, after: u64) -> Self { + Self { + name, + id, + after, + ticks_remaining: 0, + ticking: false, + attempts: 0, + } + } + + pub fn start(&mut self) { + self.ticks_remaining = self.after; + self.ticking = true; + self.attempts = 0; + } + + pub fn stop(&mut self) { + self.ticking = false; + self.ticks_remaining = 0; + self.attempts = 0; + } + + pub fn reset(&mut self) { + self.ticks_remaining = self.after; + self.attempts = 0; + } + + pub fn tick(&mut self) { + if self.ticking { + self.ticks_remaining = self.ticks_remaining.saturating_sub(1); + } + } + + pub fn fired(&self) -> bool { + self.ticking && self.ticks_remaining == 0 + } + + pub fn backoff(&mut self, prng: &mut Xoshiro256Plus) { + self.attempts = self.attempts.wrapping_add(1); + let max_backoff = self.after.saturating_mul(16); + let shift = self.attempts.min(4); + let backoff = self.after.saturating_mul(1 << shift).min(max_backoff); + let jitter = prng.random_range(0..=backoff); + self.ticks_remaining = backoff.saturating_add(jitter); + } +} + +/// Timeout types in VSR. +#[allow(unused)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum TimeoutKind { + Ping, + Prepare, + PrimaryAbdicate, + CommitMessage, + NormalHeartbeat, + StartViewChangeWindow, Review Comment: We don't need that one ########## core/consensus/src/vsr_timeout.rs: ########## @@ -0,0 +1,386 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! A deterministic, tick-based timeout mechanism for VSR consensus. +//! This module provides deterministic timeouts driven by logical ticks rather than +//! wall-clock time, enabling: +//! - Deterministic testing +//! - Predictable behavior in consensus +//! - Per-replica PRNG seeding for jitter +//! +//! Two-phase tick model: +//! +//! 1. Phase 1 (tick): All timeouts are advanced by one tick +//! 2. Phase 2 (check & handle): Check which timeouts fired and invoke handlers + +/// A deterministic, tick-based timeout. +/// +/// Timeouts count down from an initial duration (`after`) and fire when +/// reaching zero. They support exponential backoff with jitter for retries. +use rand::Rng; +use rand_xoshiro::Xoshiro256Plus; +use rand_xoshiro::rand_core::SeedableRng; + +#[derive(Debug, Clone)] +#[allow(unused)] +pub struct Timeout { + pub name: &'static str, + pub id: u128, + after: u64, + ticks_remaining: u64, + pub ticking: bool, + pub attempts: u32, +} + +impl Timeout { + pub fn new(name: &'static str, id: u128, after: u64) -> Self { + Self { + name, + id, + after, + ticks_remaining: 0, + ticking: false, + attempts: 0, + } + } + + pub fn start(&mut self) { + self.ticks_remaining = self.after; + self.ticking = true; + self.attempts = 0; + } + + pub fn stop(&mut self) { + self.ticking = false; + self.ticks_remaining = 0; + self.attempts = 0; + } + + pub fn reset(&mut self) { + self.ticks_remaining = self.after; + self.attempts = 0; + } + + pub fn tick(&mut self) { + if self.ticking { + self.ticks_remaining = self.ticks_remaining.saturating_sub(1); + } + } + + pub fn fired(&self) -> bool { + self.ticking && self.ticks_remaining == 0 + } + + pub fn backoff(&mut self, prng: &mut Xoshiro256Plus) { + self.attempts = self.attempts.wrapping_add(1); + let max_backoff = self.after.saturating_mul(16); + let shift = self.attempts.min(4); + let backoff = self.after.saturating_mul(1 << shift).min(max_backoff); + let jitter = prng.random_range(0..=backoff); + self.ticks_remaining = backoff.saturating_add(jitter); + } +} + +/// Timeout types in VSR. +#[allow(unused)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum TimeoutKind { + Ping, + Prepare, + PrimaryAbdicate, Review Comment: We don't need that ########## core/consensus/src/vsr_timeout.rs: ########## @@ -0,0 +1,386 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! A deterministic, tick-based timeout mechanism for VSR consensus. +//! This module provides deterministic timeouts driven by logical ticks rather than +//! wall-clock time, enabling: +//! - Deterministic testing +//! - Predictable behavior in consensus +//! - Per-replica PRNG seeding for jitter +//! +//! Two-phase tick model: +//! +//! 1. Phase 1 (tick): All timeouts are advanced by one tick +//! 2. Phase 2 (check & handle): Check which timeouts fired and invoke handlers + +/// A deterministic, tick-based timeout. +/// +/// Timeouts count down from an initial duration (`after`) and fire when +/// reaching zero. They support exponential backoff with jitter for retries. +use rand::Rng; +use rand_xoshiro::Xoshiro256Plus; +use rand_xoshiro::rand_core::SeedableRng; + +#[derive(Debug, Clone)] +#[allow(unused)] +pub struct Timeout { + pub name: &'static str, + pub id: u128, + after: u64, + ticks_remaining: u64, + pub ticking: bool, + pub attempts: u32, +} + +impl Timeout { + pub fn new(name: &'static str, id: u128, after: u64) -> Self { + Self { + name, + id, + after, + ticks_remaining: 0, + ticking: false, + attempts: 0, + } + } + + pub fn start(&mut self) { + self.ticks_remaining = self.after; + self.ticking = true; + self.attempts = 0; + } + + pub fn stop(&mut self) { + self.ticking = false; + self.ticks_remaining = 0; + self.attempts = 0; + } + + pub fn reset(&mut self) { + self.ticks_remaining = self.after; + self.attempts = 0; + } + + pub fn tick(&mut self) { + if self.ticking { + self.ticks_remaining = self.ticks_remaining.saturating_sub(1); + } + } + + pub fn fired(&self) -> bool { + self.ticking && self.ticks_remaining == 0 + } + + pub fn backoff(&mut self, prng: &mut Xoshiro256Plus) { + self.attempts = self.attempts.wrapping_add(1); + let max_backoff = self.after.saturating_mul(16); + let shift = self.attempts.min(4); + let backoff = self.after.saturating_mul(1 << shift).min(max_backoff); + let jitter = prng.random_range(0..=backoff); + self.ticks_remaining = backoff.saturating_add(jitter); + } +} + +/// Timeout types in VSR. +#[allow(unused)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum TimeoutKind { + Ping, + Prepare, + PrimaryAbdicate, + CommitMessage, + NormalHeartbeat, + StartViewChangeWindow, + StartViewChangeMessage, + ViewChangeStatus, Review Comment: We don't need that one -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
