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

fix: Batch memory maybe negative #10338

Merged
merged 1 commit into from
Jun 15, 2023
Merged
Changes from all commits
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
34 changes: 21 additions & 13 deletions src/common/src/memory/mem_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ impl From<IntGauge> for MemCounter {
}
}

impl MemCounter {
fn add(&self, bytes: i64) {
match &self {
MemCounter::TrAdder(c) => c.add(bytes),
MemCounter::Atomic(c) => c.add(bytes),
}
}

fn get_bytes_used(&self) -> i64 {
match &self {
MemCounter::TrAdder(c) => c.get(),
MemCounter::Atomic(c) => c.get(),
}
}
}

impl From<TrAdderGauge> for MemCounter {
fn from(value: TrAdderGauge) -> Self {
MemCounter::TrAdder(value)
Expand Down Expand Up @@ -72,10 +88,7 @@ impl MemoryContext {
/// Add `bytes` memory usage. Pass negative value to decrease memory usage.
pub fn add(&self, bytes: i64) {
if let Some(inner) = &self.inner {
match &inner.counter {
MemCounter::TrAdder(c) => c.add(bytes),
MemCounter::Atomic(c) => c.add(bytes),
}
inner.counter.add(bytes);

if let Some(parent) = &inner.parent {
parent.add(bytes);
Expand All @@ -85,10 +98,7 @@ impl MemoryContext {

pub fn get_bytes_used(&self) -> i64 {
if let Some(inner) = &self.inner {
match &inner.counter {
MemCounter::TrAdder(c) => c.get(),
MemCounter::Atomic(c) => c.get(),
}
inner.counter.get_bytes_used()
} else {
0
}
Expand All @@ -100,12 +110,10 @@ impl MemoryContext {
}
}

impl Drop for MemoryContext {
impl Drop for MemoryContextInner {
fn drop(&mut self) {
if let Some(inner) = &self.inner {
if let Some(p) = &inner.parent {
p.add(-self.get_bytes_used())
}
if let Some(p) = &self.parent {
p.add(-self.counter.get_bytes_used())
}
}
}