Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TransactionManager] refactor and fix memory usage issues #10829

Merged
merged 4 commits into from
Apr 13, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
.
  • Loading branch information
mwtian committed Apr 13, 2023
commit 16ee9bc93dac456054b22c968427e7028fdd1287
85 changes: 39 additions & 46 deletions crates/sui-core/src/transaction_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl Inner {
return ready_certificates;
};

// Waiters can acquire lock in eitehr readonly or default mode.
// Waiters can acquire lock in either readonly or default mode.
let mut digests = BTreeSet::new();
if !lock_queue.readonly_waiters.is_empty() {
std::mem::swap(&mut digests, &mut lock_queue.readonly_waiters);
Expand All @@ -146,7 +146,12 @@ impl Inner {
return ready_certificates;
}

let input_count = self.input_objects.get_mut(&input_key.0).unwrap();
let input_count = self.input_objects.get_mut(&input_key.0).unwrap_or_else(|| {
panic!(
"# of transactions waiting on object {:?} cannot be 0",
input_key.0
)
});
*input_count -= digests.len();
if *input_count == 0 {
self.input_objects.remove(&input_key.0);
Expand Down Expand Up @@ -174,55 +179,19 @@ impl Inner {
ready_certificates
}

/// After reaching 3/4 load in hashmaps, increase capacity to decrease load to 1/2.
fn maybe_reserve_capacity(&mut self) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a MAX_HASHMAP_CAPACITY to keep this bounded? Or is it not an issue because we have MAX_PER_OBJECT_QUEUE_LENGTH & MAX_TM_QUEUE_LENGTH for protection

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about this, and decided to rely on the existing limits for protection, since the actual usage at most doubles these values, which should still be tolerable in a spike.

if self.lock_waiters.len() > self.lock_waiters.capacity() * 3 / 4 {
self.lock_waiters.reserve(self.lock_waiters.capacity() / 2);
}
if self.input_objects.len() > self.input_objects.capacity() * 3 / 4 {
self.input_objects
.reserve(self.input_objects.capacity() / 2);
}
if self.pending_certificates.len() > self.pending_certificates.capacity() * 3 / 4 {
self.pending_certificates
.reserve(self.pending_certificates.capacity() / 2);
}
if self.executing_certificates.len() > self.executing_certificates.capacity() * 3 / 4 {
self.executing_certificates
.reserve(self.executing_certificates.capacity() / 2);
}
self.lock_waiters.maybe_reserve_capacity();
self.input_objects.maybe_reserve_capacity();
self.pending_certificates.maybe_reserve_capacity();
self.executing_certificates.maybe_reserve_capacity();
}

/// After reaching 1/4 load in hashmaps, decrease capacity to increase load to 1/2.
fn maybe_shrink_capacity(&mut self) {
if self.lock_waiters.len() > MIN_HASHMAP_CAPACITY
&& self.lock_waiters.len() < self.lock_waiters.capacity() / 4
{
self.lock_waiters
.shrink_to(max(self.lock_waiters.capacity() / 2, MIN_HASHMAP_CAPACITY))
}
if self.input_objects.len() > MIN_HASHMAP_CAPACITY
&& self.input_objects.len() < self.input_objects.capacity() / 4
{
self.input_objects
.shrink_to(max(self.input_objects.capacity() / 2, MIN_HASHMAP_CAPACITY))
}
if self.pending_certificates.len() > MIN_HASHMAP_CAPACITY
&& self.pending_certificates.len() < self.pending_certificates.capacity() / 4
{
self.pending_certificates.shrink_to(max(
self.pending_certificates.capacity() / 2,
MIN_HASHMAP_CAPACITY,
))
}
if self.executing_certificates.len() > MIN_HASHMAP_CAPACITY
&& self.executing_certificates.len() < self.executing_certificates.capacity() / 4
{
self.executing_certificates.shrink_to(max(
self.executing_certificates.capacity() / 2,
MIN_HASHMAP_CAPACITY,
))
}
self.lock_waiters.maybe_shrink_capacity();
self.input_objects.maybe_shrink_capacity();
self.pending_certificates.maybe_shrink_capacity();
self.executing_certificates.maybe_shrink_capacity();
}
}

Expand Down Expand Up @@ -675,3 +644,27 @@ impl TransactionManager {
);
}
}

trait ResizableHashMap<K, V> {
fn maybe_reserve_capacity(&mut self);
fn maybe_shrink_capacity(&mut self);
}

impl<K, V> ResizableHashMap<K, V> for HashMap<K, V>
where
K: std::cmp::Eq + std::hash::Hash,
{
/// After reaching 3/4 load in hashmaps, increase capacity to decrease load to 1/2.
fn maybe_reserve_capacity(&mut self) {
if self.len() > self.capacity() * 3 / 4 {
self.reserve(self.capacity() / 2);
}
}

/// After reaching 1/4 load in hashmaps, decrease capacity to increase load to 1/2.
fn maybe_shrink_capacity(&mut self) {
if self.len() > MIN_HASHMAP_CAPACITY && self.len() < self.capacity() / 4 {
self.shrink_to(max(self.capacity() / 2, MIN_HASHMAP_CAPACITY))
}
}
}