Skip to content

Commit c35edd8

Browse files
committed
Refactor db metrics & lock handling
1 parent 55c8b73 commit c35edd8

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

massa-db-worker/src/massa_db.rs

+31-25
Original file line numberDiff line numberDiff line change
@@ -391,34 +391,29 @@ where
391391
}
392392
}
393393

394-
let changes_size = if self.config.enable_metrics {
395-
self.current_batch.lock().size_in_bytes()
396-
} else {
397-
0
398-
};
394+
// If metrics are enabled, we keep track of the size of the changes (for state changes, and then for versioning changes)
395+
if self.config.enable_metrics {
396+
let changes_size;
397+
let changes_versioning_size;
398+
{
399+
let mut current_batch_guard = self.current_batch.lock();
400+
changes_size = current_batch_guard.size_in_bytes();
401+
402+
// in versioning_changes, we have the data that we do not want to include in hash
403+
// e.g everything that is not in 'Active' state (so hashes remain compatibles)
404+
for (key, value) in versioning_changes.iter() {
405+
if let Some(value) = value {
406+
current_batch_guard.put_cf(handle_versioning, key, value);
407+
} else {
408+
current_batch_guard.delete_cf(handle_versioning, key);
409+
}
410+
}
399411

400-
// in versioning_changes, we have the data that we do not want to include in hash
401-
// e.g everything that is not in 'Active' state (so hashes remain compatibles)
402-
for (key, value) in versioning_changes.iter() {
403-
if let Some(value) = value {
404-
self.current_batch
405-
.lock()
406-
.put_cf(handle_versioning, key, value);
407-
} else {
408-
self.current_batch.lock().delete_cf(handle_versioning, key);
412+
changes_versioning_size = current_batch_guard
413+
.size_in_bytes()
414+
.saturating_sub(changes_size);
409415
}
410-
}
411416

412-
let changes_versioning_size = if self.config.enable_metrics {
413-
self.current_batch
414-
.lock()
415-
.size_in_bytes()
416-
.saturating_sub(changes_size)
417-
} else {
418-
0
419-
};
420-
421-
if self.config.enable_metrics {
422417
match self
423418
.change_history_sizes
424419
.entry(self.get_change_id().expect(CHANGE_ID_DESER_ERROR))
@@ -431,6 +426,17 @@ where
431426
entry.get_mut().1 += changes_versioning_size;
432427
}
433428
}
429+
} else {
430+
let mut current_batch_guard = self.current_batch.lock();
431+
// in versioning_changes, we have the data that we do not want to include in hash
432+
// e.g everything that is not in 'Active' state (so hashes remain compatibles)
433+
for (key, value) in versioning_changes.iter() {
434+
if let Some(value) = value {
435+
current_batch_guard.put_cf(handle_versioning, key, value);
436+
} else {
437+
current_batch_guard.delete_cf(handle_versioning, key);
438+
}
439+
}
434440
}
435441

436442
if let Some(change_id) = change_id {

0 commit comments

Comments
 (0)