Skip to content

Commit 0d14a9b

Browse files
committed
fix: fix apply wal frames bug
1 parent 8a19eb1 commit 0d14a9b

File tree

5 files changed

+18
-26
lines changed

5 files changed

+18
-26
lines changed

src/database/database.rs

-15
Original file line numberDiff line numberDiff line change
@@ -708,19 +708,9 @@ impl Database {
708708
let (mut ck1, mut ck2) = read_last_checksum(shadow_wal, self.page_size)?;
709709
let mut offset = orig_shadow_wal_size;
710710
let mut last_commit_size = orig_shadow_wal_size;
711-
712-
debug!(
713-
"wal header salt: ({}, {})",
714-
wal_header.salt1, wal_header.salt2
715-
);
716711
// Read through WAL from last position to find the page of the last
717712
// committed transaction.
718713
loop {
719-
debug!(
720-
"db {} copy frame at {}, checksum: ({}, {})",
721-
self.config.db, offset, ck1, ck2
722-
);
723-
724714
let wal_frame = WALFrame::read(&mut wal_file, self.page_size);
725715
let wal_frame = match wal_frame {
726716
Ok(wal_frame) => wal_frame,
@@ -735,11 +725,6 @@ impl Database {
735725
}
736726
};
737727

738-
debug!(
739-
"db {} copy frame at {}, salt: ({}, {})",
740-
self.config.db, offset, wal_frame.salt1, wal_frame.salt2
741-
);
742-
743728
// compare wal frame salts with wal header salts, break if mismatch
744729
if wal_frame.salt1 != wal_header.salt1 || wal_frame.salt2 != wal_header.salt2 {
745730
debug!(

src/sqlite/common.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ use crate::error::Result;
88
pub const WAL_FRAME_HEADER_SIZE: u64 = 24;
99
pub const WAL_HEADER_SIZE: u64 = 32;
1010

11-
const WAL_HEADER_CHECKSUM_OFFSET: u64 = 24;
12-
const WAL_FRAME_HEADER_CHECKSUM_OFFSET: u64 = 16;
11+
static WAL_HEADER_CHECKSUM_OFFSET: u64 = 24;
12+
static WAL_FRAME_HEADER_CHECKSUM_OFFSET: u64 = 16;
1313

1414
pub const WAL_HEADER_BIG_ENDIAN_MAGIC: [u8; 4] = [0x37, 0x7f, 0x06, 0x83];
1515
pub const WAL_HEADER_LITTLE_ENDIAN_MAGIC: [u8; 4] = [0x37, 0x7f, 0x06, 0x82];
1616

1717
// SQLite checkpoint modes.
18-
const CHECKPOINT_MODE_PASSIVE: &str = "PASSIVE";
19-
const CHECKPOINT_MODE_FULL: &str = "FULL";
20-
const CHECKPOINT_MODE_RESTART: &str = "RESTART";
21-
const CHECKPOINT_MODE_TRUNCATE: &str = "TRUNCATE";
18+
static CHECKPOINT_MODE_PASSIVE: &str = "PASSIVE";
19+
static CHECKPOINT_MODE_FULL: &str = "FULL";
20+
static CHECKPOINT_MODE_RESTART: &str = "RESTART";
21+
static CHECKPOINT_MODE_TRUNCATE: &str = "TRUNCATE";
2222

2323
#[derive(Clone)]
2424
pub enum CheckpointMode {

src/sync/replicate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ impl Replicate {
169169
let init_pos = reader.position();
170170
let mut data = Vec::new();
171171

172-
// debug!("db {} write wal segment position {:?}", self.db, init_pos,);
172+
debug!("db {} write wal segment position {:?}", self.db, init_pos,);
173173

174174
// Copy header if at offset zero.
175175
let mut salt1 = 0;

src/sync/restore.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ use crate::storage::SnapshotInfo;
2020
use crate::storage::StorageClient;
2121
use crate::storage::WalSegmentInfo;
2222

23+
static WAL_CHECKPOINT_TRUNCATE: &str = "PRAGMA wal_checkpoint(TRUNCATE);";
24+
2325
struct Restore {
2426
db: String,
2527
config: Vec<StorageConfig>,
@@ -94,9 +96,7 @@ impl Restore {
9496
"restore db {} apply wal segments: {:?}",
9597
self.db, wal_segments
9698
);
97-
let connection = Connection::open(db_path)?;
9899
let wal_file_name = format!("{}-wal", db_path);
99-
let sql = "PRAGMA wal_checkpoint(TRUNCATE)".to_string();
100100

101101
for (index, offsets) in wal_segments {
102102
let mut wal_decompressed_data = Vec::new();
@@ -113,20 +113,27 @@ impl Restore {
113113
wal_decompressed_data.extend_from_slice(&data);
114114
}
115115

116+
// prepare db wal before open db connection
116117
let mut wal_file = OpenOptions::new()
117118
.write(true)
118119
.create(true)
119120
.truncate(true)
120121
.open(&wal_file_name)?;
121122

122123
wal_file.write_all(&wal_decompressed_data)?;
123-
if let Err(e) = connection.execute_batch(&sql) {
124+
wal_file.flush()?;
125+
126+
let connection = Connection::open(db_path)?;
127+
128+
if let Err(e) = connection.query_row(WAL_CHECKPOINT_TRUNCATE, [], |_row| Ok(())) {
124129
error!(
125130
"truncation checkpoint failed during restore {}:{:?}",
126131
index, offsets
127132
);
128133
return Err(e.into());
129134
}
135+
136+
// connection.close().unwrap();
130137
}
131138

132139
Ok(())

tests/integration_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def test_restore(p, config_file, root, exp_data):
139139
config = FsConfigGenerator()
140140
config.generate()
141141

142-
test = Test(config.root, 2000)
142+
test = Test(config.root, 12000)
143143
test.create_table()
144144

145145
bin = "/Users/codedump/source/replited/target/debug/replited"

0 commit comments

Comments
 (0)