Skip to content

Commit 0141433

Browse files
bors[bot]psarna
andauthored
Merge #85
85: Revert "Merge #81" r=MarinPostma a=psarna This reverts commit d3bf87e, reversing changes made to 25e174f. WalHook broke, and until the investigation finishes, let's just drop the series altogether. Co-authored-by: Piotr Sarna <[email protected]>
2 parents d3bf87e + 719d193 commit 0141433

File tree

10 files changed

+112
-32
lines changed

10 files changed

+112
-32
lines changed

libsql-server/.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "libsql"]
2+
path = libsql
3+
url = https://github.com/libsql/libsql/

libsql-server/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# install dependencies
22
FROM rust:slim-bullseye AS compiler
3-
RUN apt update && apt install -y libclang-dev clang \
3+
RUN apt update && apt install -y libclang-dev clang libsqlite3-dev \
44
build-essential tcl protobuf-compiler file
55
RUN cargo install cargo-chef
66
WORKDIR /sqld

libsql-server/libsql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 0bdd65eda5ec284c39665e48ed50114a42877f3e

libsql-server/sqld/Cargo.toml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ fallible-iterator = "0.2.0"
1717
futures = "0.3.25"
1818
hex = "0.4.3"
1919
hyper = { version = "0.14.23", features = ["http2"] }
20-
libsql-wasmtime-bindings = "0"
2120
# Regular mvfs prevents users from enabling WAL mode
2221
mvfs = { git = "https://github.com/psarna/mvsqlite", branch = "mwal", optional = true }
2322
mwal = { git = "https://github.com/psarna/mvsqlite", branch = "mwal", optional = true }
@@ -28,10 +27,7 @@ pin-project-lite = "0.2.9"
2827
postgres-protocol = "0.6.4"
2928
prost = "0.11.3"
3029
regex = "1.7.0"
31-
rusqlite = { git = "https://github.com/psarna/rusqlite", branch = "libsql-dev", default-features = false, features = [
32-
"bundled-libsql",
33-
"column_decltype"
34-
] }
30+
rusqlite = { version = "0.28.0", features = [ "buildtime_bindgen", "column_decltype" ] }
3531
serde = { version = "1.0.149", features = ["derive"] }
3632
serde_json = "1.0.91"
3733
smallvec = "1.10.0"

libsql-server/sqld/build.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,34 @@
1+
use std::env;
2+
use std::fs;
3+
use std::process::Command;
4+
15
use prost_build::Config;
26

37
fn main() -> Result<(), Box<dyn std::error::Error>> {
8+
let mut pwd = env::current_dir().unwrap();
9+
pwd.push("../libsql");
10+
let libsql_dir = fs::canonicalize(pwd.as_path()).unwrap();
11+
let mut bindings = Command::new("./configure");
12+
let configure = bindings.current_dir(libsql_dir.as_path()).arg("--with-pic");
13+
let profile = std::env::var("PROFILE").unwrap();
14+
if profile.as_str() == "release" {
15+
configure.arg("--enable-releasemode");
16+
}
17+
let output = configure.output().unwrap();
18+
if !output.status.success() {
19+
println!("{}", std::str::from_utf8(&output.stderr).unwrap());
20+
panic!("failed to configure");
21+
}
22+
23+
if !Command::new("make")
24+
.current_dir(libsql_dir.as_path())
25+
.status()
26+
.unwrap()
27+
.success()
28+
{
29+
panic!("failed to compile");
30+
}
31+
432
let mut config = Config::new();
533
config.bytes([".wal_log"]);
634
tonic_build::configure()
@@ -13,5 +41,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
1341

1442
println!("cargo:rerun-if-changed=proto");
1543

44+
println!("cargo:rustc-link-search=native=libsql/.libs");
45+
println!("cargo:rustc-link-lib=static=sqlite3");
46+
println!("cargo:rerun-if-changed=../libsql/src");
47+
1648
Ok(())
1749
}

libsql-server/sqld/src/database/libsql.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use tokio::sync::oneshot;
1010
use tracing::warn;
1111

1212
use crate::libsql::wal_hook::WalHook;
13+
use crate::libsql::WalConnection;
1314
use crate::query::{
1415
Column, ErrorCode, Params, Queries, Query, QueryError, QueryResponse, QueryResult, ResultSet,
1516
Row,
@@ -135,7 +136,7 @@ fn open_db(
135136
Arc<Mutex<mwal::ffi::libsql_wal_methods>>,
136137
>,
137138
wal_hook: impl WalHook + Send + Clone + 'static,
138-
) -> anyhow::Result<rusqlite::Connection> {
139+
) -> anyhow::Result<WalConnection> {
139140
let mut retries = 0;
140141
loop {
141142
#[cfg(feature = "mwal_backend")]

libsql-server/sqld/src/database/write_proxy/replication.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ use tokio::runtime::Handle;
3131
use tonic::transport::Channel;
3232

3333
use crate::libsql::ffi::{types::XWalFrameFn, PgHdr, Wal};
34-
use crate::libsql::open_with_regular_wal;
3534
use crate::libsql::wal_hook::WalHook;
35+
use crate::libsql::{open_with_regular_wal, WalConnection};
3636
use crate::rpc::wal_log::wal_log_rpc::wal_log_entry::Payload;
3737
use crate::rpc::wal_log::wal_log_rpc::{wal_log_client::WalLogClient, LogOffset, WalLogEntry};
3838
use crate::rpc::wal_log::wal_log_rpc::{Commit, Frame};
3939

4040
pub struct PeriodicDbUpdater {
4141
interval: Duration,
42-
db: rusqlite::Connection,
42+
db: WalConnection,
4343
}
4444

4545
/// The `PeriodicUpdater` role is to periodically trigger a dummy write that will be intercepted by

libsql-server/sqld/src/libsql/mod.rs

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@ pub mod ffi;
55
pub mod mwal;
66
pub mod wal_hook;
77

8-
pub use wblibsql::{
9-
libsql_compile_wasm_module, libsql_free_wasm_module, libsql_run_wasm, libsql_wasm_engine_new,
10-
};
11-
128
use anyhow::ensure;
139
use rusqlite::Connection;
10+
use std::os::unix::ffi::OsStrExt;
1411

1512
use crate::libsql::{ffi::libsql_wal_methods_register, wal_hook::WalMethodsHook};
1613

@@ -19,6 +16,37 @@ use self::{
1916
wal_hook::WalHook,
2017
};
2118

19+
pub struct WalConnection {
20+
inner: rusqlite::Connection,
21+
}
22+
23+
impl std::ops::Deref for WalConnection {
24+
type Target = rusqlite::Connection;
25+
26+
fn deref(&self) -> &Self::Target {
27+
&self.inner
28+
}
29+
}
30+
31+
impl std::ops::Drop for WalConnection {
32+
fn drop(&mut self) {
33+
unsafe {
34+
rusqlite::ffi::sqlite3_close(self.inner.handle());
35+
}
36+
let _ = self.inner;
37+
}
38+
}
39+
40+
extern "C" {
41+
fn libsql_open(
42+
filename: *const u8,
43+
ppdb: *mut *mut rusqlite::ffi::sqlite3,
44+
flags: std::ffi::c_int,
45+
vfs: *const u8,
46+
wal: *const u8,
47+
) -> i32;
48+
}
49+
2250
fn get_orig_wal_methods() -> anyhow::Result<*mut libsql_wal_methods> {
2351
let orig: *mut libsql_wal_methods = unsafe { libsql_wal_methods_find(0) };
2452
if orig.is_null() {
@@ -32,17 +60,29 @@ pub(crate) fn open_with_regular_wal(
3260
path: impl AsRef<std::path::Path>,
3361
flags: rusqlite::OpenFlags,
3462
wal_hook: impl WalHook + 'static,
35-
) -> anyhow::Result<Connection> {
63+
) -> anyhow::Result<WalConnection> {
3664
unsafe {
65+
let mut pdb: *mut rusqlite::ffi::sqlite3 = std::ptr::null_mut();
66+
let ppdb: *mut *mut rusqlite::ffi::sqlite3 = &mut pdb;
3767
let orig = get_orig_wal_methods()?;
3868
let wrapped = WalMethodsHook::wrap(orig, wal_hook);
3969
let res = libsql_wal_methods_register(wrapped);
4070
ensure!(res == 0, "failed to register WAL methods");
71+
72+
let open_err = libsql_open(
73+
path.as_ref().as_os_str().as_bytes().as_ptr(),
74+
ppdb,
75+
flags.bits(),
76+
std::ptr::null(),
77+
WalMethodsHook::METHODS_NAME.as_ptr(),
78+
);
79+
assert_eq!(open_err, 0);
80+
let conn = Connection::from_handle(pdb)?;
81+
conn.pragma_update(None, "journal_mode", "wal")?;
82+
tracing::trace!(
83+
"Opening a connection with regular WAL at {}",
84+
path.as_ref().display()
85+
);
86+
Ok(WalConnection { inner: conn })
4187
}
42-
tracing::trace!(
43-
"Opening a connection with regular WAL at {}",
44-
path.as_ref().display()
45-
);
46-
let conn = Connection::open_with_flags_and_wal(path, flags, WalMethodsHook::METHODS_NAME_STR)?;
47-
Ok(conn)
4888
}

libsql-server/sqld/src/libsql/mwal/mod.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,30 @@ pub(crate) fn open_with_virtual_wal(
66
path: impl AsRef<std::path::Path>,
77
flags: rusqlite::OpenFlags,
88
vwal_methods: Arc<Mutex<mwal::ffi::libsql_wal_methods>>,
9-
) -> anyhow::Result<rusqlite::Connection> {
9+
) -> anyhow::Result<super::WalConnection> {
10+
use std::os::unix::ffi::OsStrExt;
1011
let mut vwal_methods = vwal_methods.lock().map_err(|e| anyhow::anyhow!("{}", e))?;
1112
unsafe {
13+
let mut pdb: *mut rusqlite::ffi::sqlite3 = std::ptr::null_mut();
14+
let ppdb: *mut *mut rusqlite::ffi::sqlite3 = &mut pdb;
1215
let register_err = super::ffi::libsql_wal_methods_register(
1316
&mut *vwal_methods as *const mwal::ffi::libsql_wal_methods as _,
1417
);
1518
assert_eq!(register_err, 0);
19+
let open_err = super::libsql_open(
20+
path.as_ref().as_os_str().as_bytes().as_ptr(),
21+
ppdb,
22+
flags.bits(),
23+
std::ptr::null(),
24+
vwal_methods.name,
25+
);
26+
assert_eq!(open_err, 0);
27+
let conn = super::Connection::from_handle(pdb)?;
28+
conn.pragma_update(None, "journal_mode", "wal")?;
29+
tracing::trace!(
30+
"Opening a connection with virtual WAL at {}",
31+
path.as_ref().display()
32+
);
33+
Ok(super::WalConnection { inner: conn })
1634
}
17-
tracing::trace!(
18-
"Opening a connection with virtual WAL at {}",
19-
path.as_ref().display()
20-
);
21-
let conn = rusqlite::Connection::open_with_flags_and_wal(path, flags, unsafe {
22-
std::ffi::CStr::from_ptr(vwal_methods.name as *const _)
23-
.to_str()
24-
.unwrap()
25-
})?;
26-
Ok(conn)
2735
}

libsql-server/sqld/src/libsql/wal_hook.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ pub unsafe trait WalHook {
5353
unsafe impl WalHook for () {}
5454

5555
impl WalMethodsHook {
56-
pub const METHODS_NAME_STR: &'static str = "wal_hook";
5756
pub const METHODS_NAME: &'static [u8] = b"wal_hook\0";
5857

5958
pub fn wrap(

0 commit comments

Comments
 (0)