Skip to content

Commit 2e34218

Browse files
committed
Reopen database after full initial recommitment to decrease memory usage by ParityDB (see paritytech/parity-db#93 (comment))
1 parent b30ca37 commit 2e34218

File tree

2 files changed

+54
-23
lines changed

2 files changed

+54
-23
lines changed

crates/subspace-farmer/src/commitments.rs

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ impl Commitments {
146146
salt: None,
147147
};
148148
db_entry.lock().replace(Arc::new(
149-
Db::open_or_create(&options).map_err(CommitmentError::CommitmentDb)?,
149+
Db::open_or_create(&options)
150+
.map(|db| (db, options))
151+
.map_err(CommitmentError::CommitmentDb)?,
150152
));
151153
}
152154

@@ -179,7 +181,8 @@ impl Commitments {
179181

180182
let db_guard = db_entry.lock();
181183

182-
if let Some(db) = db_guard.as_ref() {
184+
if let Some(db_with_options) = db_guard.as_ref() {
185+
let (db, _options) = db_with_options.as_ref();
183186
for (tag, offset) in tags.into_iter().zip(batch_start..) {
184187
tags_with_offset.push((tag, offset.to_le_bytes()));
185188
}
@@ -195,6 +198,11 @@ impl Commitments {
195198
.map_err(CommitmentError::CommitmentDb)?;
196199

197200
tags_with_offset.clear();
201+
202+
// Hack: Reopen database to free memory, see
203+
// https://github.com/paritytech/parity-db/issues/93#issuecomment-1241812705
204+
drop(db_guard);
205+
db_entry.reopen()?;
198206
}
199207
} else {
200208
// Database was already removed, no need to continue
@@ -207,7 +215,9 @@ impl Commitments {
207215
if let Some(db_entry) = self.get_db_entry(salt) {
208216
let db_guard = db_entry.lock();
209217

210-
if let Some(db) = db_guard.as_ref() {
218+
if let Some(db_with_options) = db_guard.as_ref() {
219+
let (db, _options) = db_with_options.as_ref();
220+
211221
tags_with_offset.sort_by(|(tag_a, _), (tag_b, _)| tag_a.cmp(tag_b));
212222

213223
db.commit(
@@ -216,6 +226,11 @@ impl Commitments {
216226
.map(|(tag, offset)| (0, tag, Some(offset.to_vec()))),
217227
)
218228
.map_err(CommitmentError::CommitmentDb)?;
229+
230+
// Hack: Reopen database to free memory, see
231+
// https://github.com/paritytech/parity-db/issues/93#issuecomment-1241812705
232+
drop(db_guard);
233+
db_entry.reopen()?;
219234
}
220235
}
221236

@@ -255,13 +270,15 @@ impl Commitments {
255270
let salt = db_entry.salt();
256271
let db_guard = db_entry.lock();
257272

258-
if let Some(db) = db_guard.as_ref() {
259-
db.commit(
260-
pieces
261-
.iter()
262-
.map(|piece| (0, create_tag(piece, salt), None)),
263-
)
264-
.map_err(CommitmentError::CommitmentDb)?;
273+
if let Some(db_with_options) = db_guard.as_ref() {
274+
db_with_options
275+
.0
276+
.commit(
277+
pieces
278+
.iter()
279+
.map(|piece| (0, create_tag(piece, salt), None)),
280+
)
281+
.map_err(CommitmentError::CommitmentDb)?;
265282
}
266283
}
267284

@@ -285,19 +302,21 @@ impl Commitments {
285302
let salt = db_entry.salt();
286303
let db_guard = db_entry.lock();
287304

288-
if let Some(db) = db_guard.as_ref() {
305+
if let Some(db_with_options) = db_guard.as_ref() {
289306
let mut tags_with_offset: Vec<(Tag, PieceOffset)> = pieces_with_offsets()
290307
.map(|(piece_offset, piece)| (create_tag(piece, salt), piece_offset))
291308
.collect();
292309

293310
tags_with_offset.sort_by(|(tag_a, _), (tag_b, _)| tag_a.cmp(tag_b));
294311

295-
db.commit(
296-
tags_with_offset
297-
.into_iter()
298-
.map(|(tag, offset)| (0, tag, Some(offset.to_le_bytes().to_vec()))),
299-
)
300-
.map_err(CommitmentError::CommitmentDb)?;
312+
db_with_options
313+
.0
314+
.commit(
315+
tags_with_offset
316+
.into_iter()
317+
.map(|(tag, offset)| (0, tag, Some(offset.to_le_bytes().to_vec()))),
318+
)
319+
.map_err(CommitmentError::CommitmentDb)?;
301320
};
302321
}
303322

@@ -326,13 +345,13 @@ impl Commitments {
326345
return Vec::new();
327346
}
328347
};
329-
let db = match db_guard.as_ref() {
330-
Some(db) => db,
348+
let db_with_options = match db_guard.as_ref() {
349+
Some(db_with_options) => db_with_options,
331350
None => {
332351
return Vec::new();
333352
}
334353
};
335-
let iter = match db.iter(0) {
354+
let iter = match db_with_options.0.iter(0) {
336355
Ok(iter) => iter,
337356
Err(_) => {
338357
return Vec::new();

crates/subspace-farmer/src/commitments/databases.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub(super) struct CreateDbEntryResult {
2020

2121
pub(super) struct DbEntry {
2222
salt: Salt,
23-
db: Mutex<Option<Arc<Db>>>,
23+
db: Mutex<Option<Arc<(Db, Options)>>>,
2424
}
2525

2626
impl fmt::Debug for DbEntry {
@@ -30,7 +30,7 @@ impl fmt::Debug for DbEntry {
3030
}
3131

3232
impl Deref for DbEntry {
33-
type Target = Mutex<Option<Arc<Db>>>;
33+
type Target = Mutex<Option<Arc<(Db, Options)>>>;
3434

3535
fn deref(&self) -> &Self::Target {
3636
&self.db
@@ -41,6 +41,18 @@ impl DbEntry {
4141
pub(super) fn salt(&self) -> Salt {
4242
self.salt
4343
}
44+
45+
pub(super) fn reopen(&self) -> Result<(), CommitmentError> {
46+
let mut inner = self.db.lock();
47+
if let Some(db_with_options) = inner.take() {
48+
let options = db_with_options.1.clone();
49+
drop(db_with_options);
50+
let db = Db::open_or_create(&options).map_err(CommitmentError::CommitmentDb)?;
51+
inner.replace(Arc::new((db, options)));
52+
}
53+
54+
Ok(())
55+
}
4456
}
4557

4658
#[derive(Debug)]
@@ -94,7 +106,7 @@ impl CommitmentDatabases {
94106
*salt,
95107
Arc::new(DbEntry {
96108
salt: *salt,
97-
db: Mutex::new(Some(Arc::new(db))),
109+
db: Mutex::new(Some(Arc::new((db, options)))),
98110
}),
99111
);
100112
}

0 commit comments

Comments
 (0)