Skip to content

Commit 57cd96e

Browse files
authored
storage: replace println with tracing (#417)
* replace `println` with `tracing` * fix * remove feature * !
1 parent 3c86c5e commit 57cd96e

File tree

7 files changed

+31
-25
lines changed

7 files changed

+31
-25
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

storage/database/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ cfg-if = { workspace = true }
2323
page_size = { version = "0.6.0" } # Needed for database resizes, they must be a multiple of the OS page size.
2424
paste = { workspace = true }
2525
thiserror = { workspace = true }
26+
tracing = { workspace = true }
2627

2728
# Optional features.
2829
heed = { version = "0.20.5", features = ["read-txn-no-tls"] }

storage/database/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ to bulk generate zero-sized marker types that implement [`Table`].
9494
This macro also generates other convenient traits specific to _your_ tables.
9595

9696
# Feature flags
97-
Different database backends are enabled by the feature flags:
98-
- `heed` (LMDB)
99-
- `redb`
97+
| Feature flag | Description |
98+
|--------------|-------------|
99+
| `heed` | Enables the `heed` (LMDB) backend
100+
| `redb` | Enables the `redb` backend
100101

101-
The default is `heed`.
102+
The defaults are: `heed`.
102103

103104
`tracing` is always enabled and cannot be disabled via feature-flag.
104-
<!-- FIXME: tracing should be behind a feature flag -->
105105

106106
# Examples
107107
The below is an example of using `cuprate-database`.

storage/database/src/backend/heed/env.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::{
88
};
99

1010
use heed::{DatabaseFlags, EnvFlags, EnvOpenOptions};
11+
use tracing::{debug, warn};
1112

1213
use crate::{
1314
backend::heed::{
@@ -70,18 +71,16 @@ impl Drop for ConcreteEnv {
7071
// We need to do `mdb_env_set_flags(&env, MDB_NOSYNC|MDB_ASYNCMAP, 0)`
7172
// to clear the no sync and async flags such that the below `self.sync()`
7273
// _actually_ synchronously syncs.
73-
if let Err(_e) = Env::sync(self) {
74-
// TODO: log error?
74+
if let Err(e) = Env::sync(self) {
75+
warn!("Env sync error: {e}");
7576
}
7677

77-
// TODO: log that we are dropping the database.
78-
79-
// TODO: use tracing.
8078
// <https://github.com/LMDB/lmdb/blob/b8e54b4c31378932b69f1298972de54a565185b1/libraries/liblmdb/lmdb.h#L49-L61>
8179
let result = self.env.read().unwrap().clear_stale_readers();
80+
8281
match result {
83-
Ok(n) => println!("LMDB stale readers cleared: {n}"),
84-
Err(e) => println!("LMDB stale reader clear error: {e:?}"),
82+
Ok(n) => debug!("LMDB stale readers cleared: {n}"),
83+
Err(e) => debug!("LMDB stale reader clear error: {e:?}"),
8584
}
8685
}
8786
}

storage/database/src/backend/redb/env.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,8 @@ impl Drop for ConcreteEnv {
3131
fn drop(&mut self) {
3232
// INVARIANT: drop(ConcreteEnv) must sync.
3333
if let Err(e) = self.sync() {
34-
// TODO: use tracing
35-
println!("{e:#?}");
34+
tracing::warn!("Env sync error: {e}");
3635
}
37-
38-
// TODO: log that we are dropping the database.
3936
}
4037
}
4138

storage/service/Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ repository = "https://github.com/Cuprate/cuprate/tree/main/storage/service"
99
keywords = ["cuprate", "service", "database"]
1010

1111
[features]
12-
default = ["heed"]
13-
heed = ["cuprate-database/heed"]
14-
redb = ["cuprate-database/redb"]
15-
redb-memorey = ["cuprate-database/redb-memory"]
12+
default = ["heed"]
13+
heed = ["cuprate-database/heed"]
14+
redb = ["cuprate-database/redb"]
15+
redb-memory = ["cuprate-database/redb-memory"]
1616

1717
[dependencies]
1818
cuprate-database = { workspace = true }
@@ -23,6 +23,7 @@ rayon = { workspace = true }
2323
tower = { workspace = true }
2424
futures = { workspace = true, features = ["std"] }
2525
crossbeam = { workspace = true, features = ["std"] }
26+
tracing = { workspace = true }
2627

2728
[lints]
2829
workspace = true

storage/service/src/service/write.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::{
55
};
66

77
use futures::channel::oneshot;
8+
use tracing::{info, warn};
89

910
use cuprate_database::{ConcreteEnv, DbResult, Env, RuntimeError};
1011
use cuprate_helper::asynch::InfallibleOneshotReceiver;
@@ -150,10 +151,16 @@ fn database_writer<Req, Res>(
150151
// add that much instead of the default 1GB.
151152
// <https://github.com/monero-project/monero/blob/059028a30a8ae9752338a7897329fe8012a310d5/src/blockchain_db/lmdb/db_lmdb.cpp#L665-L695>
152153
let old = env.current_map_size();
153-
let new = env.resize_map(None);
154+
let new = env.resize_map(None).get();
154155

155-
// TODO: use tracing.
156-
println!("resizing database memory map, old: {old}B, new: {new}B");
156+
const fn bytes_to_megabytes(bytes: usize) -> usize {
157+
bytes / 1_000_000
158+
}
159+
160+
let old_mb = bytes_to_megabytes(old);
161+
let new_mb = bytes_to_megabytes(new);
162+
163+
info!("Resizing database memory map, old: {old_mb}MB, new: {new_mb}MB");
157164

158165
// Try handling the request again.
159166
continue 'retry;
@@ -170,8 +177,7 @@ fn database_writer<Req, Res>(
170177

171178
// Send the response back, whether if it's an `Ok` or `Err`.
172179
if let Err(e) = response_sender.send(response) {
173-
// TODO: use tracing.
174-
println!("database writer failed to send response: {e:?}");
180+
warn!("Database writer failed to send response: {e:?}");
175181
}
176182

177183
continue 'main;

0 commit comments

Comments
 (0)