LCOV - code coverage report
Current view: top level - safekeeper/tests/walproposer_sim - simulation.rs (source / functions) Coverage Total Hit
Test: 20b6afc7b7f34578dcaab2b3acdaecfe91cd8bf1.info Lines: 97.7 % 299 292
Test Date: 2024-11-25 17:48:16 Functions: 88.9 % 63 56

            Line data    Source code
       1              : use std::{cell::Cell, str::FromStr, sync::Arc};
       2              : 
       3              : use crate::walproposer_sim::{safekeeper::run_server, walproposer_api::SimulationApi};
       4              : use desim::{
       5              :     executor::{self, ExternalHandle},
       6              :     node_os::NodeOs,
       7              :     options::{Delay, NetworkOptions},
       8              :     proto::{AnyMessage, NodeEvent},
       9              :     world::Node,
      10              :     world::World,
      11              : };
      12              : use rand::{Rng, SeedableRng};
      13              : use tracing::{debug, info_span, warn};
      14              : use utils::{id::TenantTimelineId, lsn::Lsn};
      15              : use walproposer::walproposer::{Config, Wrapper};
      16              : 
      17              : use super::{
      18              :     log::SimClock, safekeeper_disk::SafekeeperDisk, walproposer_api,
      19              :     walproposer_disk::DiskWalProposer,
      20              : };
      21              : 
      22              : /// Simulated safekeeper node.
      23              : pub struct SafekeeperNode {
      24              :     pub node: Arc<Node>,
      25              :     pub id: u32,
      26              :     pub disk: Arc<SafekeeperDisk>,
      27              :     pub thread: Cell<ExternalHandle>,
      28              : }
      29              : 
      30              : impl SafekeeperNode {
      31              :     /// Create and start a safekeeper at the specified Node.
      32         1524 :     pub fn new(node: Arc<Node>) -> Self {
      33         1524 :         let disk = Arc::new(SafekeeperDisk::new());
      34         1524 :         let thread = Cell::new(SafekeeperNode::launch(disk.clone(), node.clone()));
      35         1524 : 
      36         1524 :         Self {
      37         1524 :             id: node.id,
      38         1524 :             node,
      39         1524 :             disk,
      40         1524 :             thread,
      41         1524 :         }
      42         1524 :     }
      43              : 
      44        10401 :     fn launch(disk: Arc<SafekeeperDisk>, node: Arc<Node>) -> ExternalHandle {
      45        10401 :         // start the server thread
      46        10401 :         node.launch(move |os| {
      47        10256 :             run_server(os, disk).expect("server should finish without errors");
      48        10401 :         })
      49        10401 :     }
      50              : 
      51              :     /// Restart the safekeeper.
      52         8877 :     pub fn restart(&self) {
      53         8877 :         let new_thread = SafekeeperNode::launch(self.disk.clone(), self.node.clone());
      54         8877 :         let old_thread = self.thread.replace(new_thread);
      55         8877 :         old_thread.crash_stop();
      56         8877 :     }
      57              : }
      58              : 
      59              : /// Simulated walproposer node.
      60              : pub struct WalProposer {
      61              :     thread: ExternalHandle,
      62              :     node: Arc<Node>,
      63              :     disk: Arc<DiskWalProposer>,
      64              :     sync_safekeepers: bool,
      65              : }
      66              : 
      67              : impl WalProposer {
      68              :     /// Generic start function for both modes.
      69         9605 :     fn start(
      70         9605 :         os: NodeOs,
      71         9605 :         disk: Arc<DiskWalProposer>,
      72         9605 :         ttid: TenantTimelineId,
      73         9605 :         addrs: Vec<String>,
      74         9605 :         lsn: Option<Lsn>,
      75         9605 :     ) {
      76         9605 :         let sync_safekeepers = lsn.is_none();
      77              : 
      78         9605 :         let _enter = if sync_safekeepers {
      79         9108 :             info_span!("sync", started = executor::now()).entered()
      80              :         } else {
      81          497 :             info_span!("walproposer", started = executor::now()).entered()
      82              :         };
      83              : 
      84         9605 :         os.log_event(format!("started;walproposer;{}", sync_safekeepers as i32));
      85         9605 : 
      86         9605 :         let config = Config {
      87         9605 :             ttid,
      88         9605 :             safekeepers_list: addrs,
      89         9605 :             safekeeper_reconnect_timeout: 1000,
      90         9605 :             safekeeper_connection_timeout: 5000,
      91         9605 :             sync_safekeepers,
      92         9605 :         };
      93         9605 :         let args = walproposer_api::Args {
      94         9605 :             os,
      95         9605 :             config: config.clone(),
      96         9605 :             disk,
      97         9605 :             redo_start_lsn: lsn,
      98         9605 :         };
      99         9605 :         let api = SimulationApi::new(args);
     100         9605 :         let wp = Wrapper::new(Box::new(api), config);
     101         9605 :         wp.start();
     102         9605 :     }
     103              : 
     104              :     /// Start walproposer in a sync_safekeepers mode.
     105         9311 :     pub fn launch_sync(ttid: TenantTimelineId, addrs: Vec<String>, node: Arc<Node>) -> Self {
     106         9311 :         debug!("sync_safekeepers started at node {}", node.id);
     107         9311 :         let disk = DiskWalProposer::new();
     108         9311 :         let disk_wp = disk.clone();
     109         9311 : 
     110         9311 :         // start the client thread
     111         9311 :         let handle = node.launch(move |os| {
     112         9108 :             WalProposer::start(os, disk_wp, ttid, addrs, None);
     113         9311 :         });
     114         9311 : 
     115         9311 :         Self {
     116         9311 :             thread: handle,
     117         9311 :             node,
     118         9311 :             disk,
     119         9311 :             sync_safekeepers: true,
     120         9311 :         }
     121         9311 :     }
     122              : 
     123              :     /// Start walproposer in a normal mode.
     124          497 :     pub fn launch_walproposer(
     125          497 :         ttid: TenantTimelineId,
     126          497 :         addrs: Vec<String>,
     127          497 :         node: Arc<Node>,
     128          497 :         lsn: Lsn,
     129          497 :     ) -> Self {
     130          497 :         debug!("walproposer started at node {}", node.id);
     131          497 :         let disk = DiskWalProposer::new();
     132          497 :         disk.lock().reset_to(lsn);
     133          497 :         let disk_wp = disk.clone();
     134          497 : 
     135          497 :         // start the client thread
     136          497 :         let handle = node.launch(move |os| {
     137          497 :             WalProposer::start(os, disk_wp, ttid, addrs, Some(lsn));
     138          497 :         });
     139          497 : 
     140          497 :         Self {
     141          497 :             thread: handle,
     142          497 :             node,
     143          497 :             disk,
     144          497 :             sync_safekeepers: false,
     145          497 :         }
     146          497 :     }
     147              : 
     148          680 :     pub fn write_tx(&mut self, cnt: usize) {
     149          680 :         let start_lsn = self.disk.lock().flush_rec_ptr();
     150          680 : 
     151        12255 :         for _ in 0..cnt {
     152        12255 :             self.disk
     153        12255 :                 .lock()
     154        12255 :                 .insert_logical_message(c"prefix", b"message");
     155        12255 :         }
     156              : 
     157          680 :         let end_lsn = self.disk.lock().flush_rec_ptr();
     158          680 : 
     159          680 :         // log event
     160          680 :         self.node
     161          680 :             .log_event(format!("write_wal;{};{};{}", start_lsn.0, end_lsn.0, cnt));
     162          680 : 
     163          680 :         // now we need to set "Latch" in walproposer
     164          680 :         self.node
     165          680 :             .node_events()
     166          680 :             .send(NodeEvent::Internal(AnyMessage::Just32(0)));
     167          680 :     }
     168              : 
     169         8743 :     pub fn stop(&self) {
     170         8743 :         self.thread.crash_stop();
     171         8743 :     }
     172              : }
     173              : 
     174              : /// Holds basic simulation settings, such as network options.
     175              : pub struct TestConfig {
     176              :     pub network: NetworkOptions,
     177              :     pub timeout: u64,
     178              :     pub clock: Option<SimClock>,
     179              : }
     180              : 
     181              : impl TestConfig {
     182              :     /// Create a new TestConfig with default settings.
     183            9 :     pub fn new(clock: Option<SimClock>) -> Self {
     184            9 :         Self {
     185            9 :             network: NetworkOptions {
     186            9 :                 keepalive_timeout: Some(2000),
     187            9 :                 connect_delay: Delay {
     188            9 :                     min: 1,
     189            9 :                     max: 5,
     190            9 :                     fail_prob: 0.0,
     191            9 :                 },
     192            9 :                 send_delay: Delay {
     193            9 :                     min: 1,
     194            9 :                     max: 5,
     195            9 :                     fail_prob: 0.0,
     196            9 :                 },
     197            9 :             },
     198            9 :             timeout: 1_000 * 10,
     199            9 :             clock,
     200            9 :         }
     201            9 :     }
     202              : 
     203              :     /// Start a new simulation with the specified seed.
     204          508 :     pub fn start(&self, seed: u64) -> Test {
     205          508 :         let world = Arc::new(World::new(seed, Arc::new(self.network.clone())));
     206              : 
     207          508 :         if let Some(clock) = &self.clock {
     208          508 :             clock.set_clock(world.clock());
     209          508 :         }
     210              : 
     211          508 :         let servers = [
     212          508 :             SafekeeperNode::new(world.new_node()),
     213          508 :             SafekeeperNode::new(world.new_node()),
     214          508 :             SafekeeperNode::new(world.new_node()),
     215          508 :         ];
     216          508 : 
     217          508 :         let server_ids = [servers[0].id, servers[1].id, servers[2].id];
     218         1524 :         let safekeepers_addrs = server_ids.map(|id| format!("node:{}", id)).to_vec();
     219          508 : 
     220          508 :         let ttid = TenantTimelineId::generate();
     221          508 : 
     222          508 :         Test {
     223          508 :             world,
     224          508 :             servers,
     225          508 :             sk_list: safekeepers_addrs,
     226          508 :             ttid,
     227          508 :             timeout: self.timeout,
     228          508 :         }
     229          508 :     }
     230              : }
     231              : 
     232              : /// Holds simulation state.
     233              : pub struct Test {
     234              :     pub world: Arc<World>,
     235              :     pub servers: [SafekeeperNode; 3],
     236              :     pub sk_list: Vec<String>,
     237              :     pub ttid: TenantTimelineId,
     238              :     pub timeout: u64,
     239              : }
     240              : 
     241              : impl Test {
     242              :     /// Start a sync_safekeepers thread and wait for it to finish.
     243            6 :     pub fn sync_safekeepers(&self) -> anyhow::Result<Lsn> {
     244            6 :         let wp = self.launch_sync_safekeepers();
     245            6 : 
     246            6 :         // poll until exit or timeout
     247            6 :         let time_limit = self.timeout;
     248          230 :         while self.world.step() && self.world.now() < time_limit && !wp.thread.is_finished() {}
     249              : 
     250            6 :         if !wp.thread.is_finished() {
     251            0 :             anyhow::bail!("timeout or idle stuck");
     252            6 :         }
     253            6 : 
     254            6 :         let res = wp.thread.result();
     255            6 :         if res.0 != 0 {
     256            0 :             anyhow::bail!("non-zero exitcode: {:?}", res);
     257            6 :         }
     258            6 :         let lsn = Lsn::from_str(&res.1)?;
     259            6 :         Ok(lsn)
     260            6 :     }
     261              : 
     262              :     /// Spawn a new sync_safekeepers thread.
     263         9311 :     pub fn launch_sync_safekeepers(&self) -> WalProposer {
     264         9311 :         WalProposer::launch_sync(self.ttid, self.sk_list.clone(), self.world.new_node())
     265         9311 :     }
     266              : 
     267              :     /// Spawn a new walproposer thread.
     268          497 :     pub fn launch_walproposer(&self, lsn: Lsn) -> WalProposer {
     269          497 :         let lsn = if lsn.0 == 0 {
     270              :             // usual LSN after basebackup
     271          232 :             Lsn(21623024)
     272              :         } else {
     273          265 :             lsn
     274              :         };
     275              : 
     276          497 :         WalProposer::launch_walproposer(self.ttid, self.sk_list.clone(), self.world.new_node(), lsn)
     277          497 :     }
     278              : 
     279              :     /// Execute the simulation for the specified duration.
     280          105 :     pub fn poll_for_duration(&self, duration: u64) {
     281          105 :         let time_limit = std::cmp::min(self.world.now() + duration, self.timeout);
     282         1762 :         while self.world.step() && self.world.now() < time_limit {}
     283          105 :     }
     284              : 
     285              :     /// Execute the simulation together with events defined in some schedule.
     286          504 :     pub fn run_schedule(&self, schedule: &Schedule) -> anyhow::Result<()> {
     287          504 :         // scheduling empty events so that world will stop in those points
     288          504 :         {
     289          504 :             let clock = self.world.clock();
     290          504 : 
     291          504 :             let now = self.world.now();
     292        26801 :             for (time, _) in schedule {
     293        26297 :                 if *time < now {
     294            0 :                     continue;
     295        26297 :                 }
     296        26297 :                 clock.schedule_fake(*time - now);
     297              :             }
     298              :         }
     299              : 
     300          504 :         let mut wp = self.launch_sync_safekeepers();
     301          504 : 
     302          504 :         let mut skipped_tx = 0;
     303          504 :         let mut started_tx = 0;
     304          504 : 
     305          504 :         let mut schedule_ptr = 0;
     306              : 
     307              :         loop {
     308        27360 :             if wp.sync_safekeepers && wp.thread.is_finished() {
     309          553 :                 let res = wp.thread.result();
     310          553 :                 if res.0 != 0 {
     311           59 :                     warn!("sync non-zero exitcode: {:?}", res);
     312           59 :                     debug!("restarting sync_safekeepers");
     313              :                     // restart the sync_safekeepers
     314           59 :                     wp = self.launch_sync_safekeepers();
     315           59 :                     continue;
     316          494 :                 }
     317          494 :                 let lsn = Lsn::from_str(&res.1)?;
     318          494 :                 debug!("sync_safekeepers finished at LSN {}", lsn);
     319          494 :                 wp = self.launch_walproposer(lsn);
     320          494 :                 debug!("walproposer started at thread {}", wp.thread.id());
     321        26807 :             }
     322              : 
     323        27301 :             let now = self.world.now();
     324        53598 :             while schedule_ptr < schedule.len() && schedule[schedule_ptr].0 <= now {
     325        26297 :                 if now != schedule[schedule_ptr].0 {
     326            0 :                     warn!("skipped event {:?} at {}", schedule[schedule_ptr], now);
     327        26297 :                 }
     328              : 
     329        26297 :                 let action = &schedule[schedule_ptr].1;
     330        26297 :                 match action {
     331         8679 :                     TestAction::WriteTx(size) => {
     332         8679 :                         if !wp.sync_safekeepers && !wp.thread.is_finished() {
     333          578 :                             started_tx += *size;
     334          578 :                             wp.write_tx(*size);
     335          578 :                             debug!("written {} transactions", size);
     336              :                         } else {
     337         8101 :                             skipped_tx += size;
     338         8101 :                             debug!("skipped {} transactions", size);
     339              :                         }
     340              :                     }
     341         8876 :                     TestAction::RestartSafekeeper(id) => {
     342         8876 :                         debug!("restarting safekeeper {}", id);
     343         8876 :                         self.servers[*id].restart();
     344              :                     }
     345              :                     TestAction::RestartWalProposer => {
     346         8742 :                         debug!("restarting sync_safekeepers");
     347         8742 :                         wp.stop();
     348         8742 :                         wp = self.launch_sync_safekeepers();
     349              :                     }
     350              :                 }
     351        26297 :                 schedule_ptr += 1;
     352              :             }
     353              : 
     354        27301 :             if schedule_ptr == schedule.len() {
     355          504 :                 break;
     356        26797 :             }
     357        26797 :             let next_event_time = schedule[schedule_ptr].0;
     358        26797 : 
     359        26797 :             // poll until the next event
     360        26797 :             if wp.thread.is_finished() {
     361          596 :                 while self.world.step() && self.world.now() < next_event_time {}
     362              :             } else {
     363       455843 :                 while self.world.step()
     364       455843 :                     && self.world.now() < next_event_time
     365       429658 :                     && !wp.thread.is_finished()
     366       429090 :                 {}
     367              :             }
     368              :         }
     369              : 
     370          504 :         debug!(
     371            0 :             "finished schedule, total steps: {}",
     372            0 :             self.world.get_thread_step_count()
     373              :         );
     374          504 :         debug!("skipped_tx: {}", skipped_tx);
     375          504 :         debug!("started_tx: {}", started_tx);
     376              : 
     377          504 :         Ok(())
     378          504 :     }
     379              : }
     380              : 
     381              : #[derive(Debug, Clone)]
     382              : pub enum TestAction {
     383              :     WriteTx(usize),
     384              :     RestartSafekeeper(usize),
     385              :     RestartWalProposer,
     386              : }
     387              : 
     388              : pub type Schedule = Vec<(u64, TestAction)>;
     389              : 
     390          502 : pub fn generate_schedule(seed: u64) -> Schedule {
     391          502 :     let mut rng = rand::rngs::StdRng::seed_from_u64(seed);
     392          502 :     let mut schedule = Vec::new();
     393          502 :     let mut time = 0;
     394          502 : 
     395          502 :     let cnt = rng.gen_range(1..100);
     396          502 : 
     397          502 :     for _ in 0..cnt {
     398        26182 :         time += rng.gen_range(0..500);
     399        26182 :         let action = match rng.gen_range(0..3) {
     400         8572 :             0 => TestAction::WriteTx(rng.gen_range(1..10)),
     401         8870 :             1 => TestAction::RestartSafekeeper(rng.gen_range(0..3)),
     402         8740 :             2 => TestAction::RestartWalProposer,
     403            0 :             _ => unreachable!(),
     404              :         };
     405        26182 :         schedule.push((time, action));
     406              :     }
     407              : 
     408          502 :     schedule
     409          502 : }
     410              : 
     411          502 : pub fn generate_network_opts(seed: u64) -> NetworkOptions {
     412          502 :     let mut rng = rand::rngs::StdRng::seed_from_u64(seed);
     413          502 : 
     414          502 :     let timeout = rng.gen_range(100..2000);
     415          502 :     let max_delay = rng.gen_range(1..2 * timeout);
     416          502 :     let min_delay = rng.gen_range(1..=max_delay);
     417          502 : 
     418          502 :     let max_fail_prob = rng.gen_range(0.0..0.9);
     419          502 :     let connect_fail_prob = rng.gen_range(0.0..max_fail_prob);
     420          502 :     let send_fail_prob = rng.gen_range(0.0..connect_fail_prob);
     421          502 : 
     422          502 :     NetworkOptions {
     423          502 :         keepalive_timeout: Some(timeout),
     424          502 :         connect_delay: Delay {
     425          502 :             min: min_delay,
     426          502 :             max: max_delay,
     427          502 :             fail_prob: connect_fail_prob,
     428          502 :         },
     429          502 :         send_delay: Delay {
     430          502 :             min: min_delay,
     431          502 :             max: max_delay,
     432          502 :             fail_prob: send_fail_prob,
     433          502 :         },
     434          502 :     }
     435          502 : }
        

Generated by: LCOV version 2.1-beta