From 8e2a6661e901562ee72c70436a350b4af81968a2 Mon Sep 17 00:00:00 2001 From: Arthur Petukhovsky Date: Mon, 11 Apr 2022 20:36:26 +0300 Subject: [PATCH] Make wal_storage initialization eager (#1489) --- walkeeper/src/safekeeper.rs | 18 ++++++++++-------- walkeeper/src/timeline.rs | 4 ++-- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/walkeeper/src/safekeeper.rs b/walkeeper/src/safekeeper.rs index 307a67e5f3..1e23d87b34 100644 --- a/walkeeper/src/safekeeper.rs +++ b/walkeeper/src/safekeeper.rs @@ -517,14 +517,16 @@ where pub fn new( ztli: ZTimelineId, control_store: CTRL, - wal_store: WAL, + mut wal_store: WAL, state: SafeKeeperState, - ) -> SafeKeeper { + ) -> Result> { if state.timeline_id != ZTimelineId::from([0u8; 16]) && ztli != state.timeline_id { - panic!("Calling SafeKeeper::new with inconsistent ztli ({}) and SafeKeeperState.server.timeline_id ({})", ztli, state.timeline_id); + bail!("Calling SafeKeeper::new with inconsistent ztli ({}) and SafeKeeperState.server.timeline_id ({})", ztli, state.timeline_id); } - SafeKeeper { + wal_store.init_storage(&state)?; + + Ok(SafeKeeper { metrics: SafeKeeperMetrics::new(state.tenant_id, ztli), global_commit_lsn: state.commit_lsn, epoch_start_lsn: Lsn(0), @@ -537,7 +539,7 @@ where s: state, control_store, wal_store, - } + }) } /// Get history of term switches for the available WAL @@ -877,7 +879,7 @@ mod tests { }; let wal_store = DummyWalStore { lsn: Lsn(0) }; let ztli = ZTimelineId::from([0u8; 16]); - let mut sk = SafeKeeper::new(ztli, storage, wal_store, SafeKeeperState::empty()); + let mut sk = SafeKeeper::new(ztli, storage, wal_store, SafeKeeperState::empty()).unwrap(); // check voting for 1 is ok let vote_request = ProposerAcceptorMessage::VoteRequest(VoteRequest { term: 1 }); @@ -892,7 +894,7 @@ mod tests { let storage = InMemoryState { persisted_state: state.clone(), }; - sk = SafeKeeper::new(ztli, storage, sk.wal_store, state); + sk = SafeKeeper::new(ztli, storage, sk.wal_store, state).unwrap(); // and ensure voting second time for 1 is not ok vote_resp = sk.process_msg(&vote_request); @@ -909,7 +911,7 @@ mod tests { }; let wal_store = DummyWalStore { lsn: Lsn(0) }; let ztli = ZTimelineId::from([0u8; 16]); - let mut sk = SafeKeeper::new(ztli, storage, wal_store, SafeKeeperState::empty()); + let mut sk = SafeKeeper::new(ztli, storage, wal_store, SafeKeeperState::empty()).unwrap(); let mut ar_hdr = AppendRequestHeader { term: 1, diff --git a/walkeeper/src/timeline.rs b/walkeeper/src/timeline.rs index b10ab97cc1..a76ef77615 100644 --- a/walkeeper/src/timeline.rs +++ b/walkeeper/src/timeline.rs @@ -100,7 +100,7 @@ impl SharedState { let state = SafeKeeperState::new(zttid, peer_ids); let control_store = control_file::FileStorage::new(zttid, conf); let wal_store = wal_storage::PhysicalStorage::new(zttid, conf); - let mut sk = SafeKeeper::new(zttid.timeline_id, control_store, wal_store, state); + let mut sk = SafeKeeper::new(zttid.timeline_id, control_store, wal_store, state)?; sk.control_store.persist(&sk.s)?; Ok(Self { @@ -127,7 +127,7 @@ impl SharedState { Ok(Self { notified_commit_lsn: Lsn(0), - sk: SafeKeeper::new(zttid.timeline_id, control_store, wal_store, state), + sk: SafeKeeper::new(zttid.timeline_id, control_store, wal_store, state)?, replicas: Vec::new(), active: false, num_computes: 0,