-
-
Notifications
You must be signed in to change notification settings - Fork 153
chore: release memory to the OS every hour #1450
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,11 +16,14 @@ | |
| * | ||
| */ | ||
|
|
||
| use std::ffi::CString; | ||
| use std::sync::Mutex; | ||
| use std::time::{Duration, Instant}; | ||
|
|
||
| use crate::{handlers::http::query::QueryError, utils::arrow::record_batches_to_json}; | ||
| use datafusion::arrow::record_batch::RecordBatch; | ||
| use itertools::Itertools; | ||
| use serde_json::{Value, json}; | ||
| use tracing::info; | ||
| use tracing::{debug, info, warn}; | ||
|
|
||
| pub struct QueryResponse { | ||
| pub records: Vec<RecordBatch>, | ||
|
|
@@ -32,28 +35,84 @@ pub struct QueryResponse { | |
| impl QueryResponse { | ||
| pub fn to_json(&self) -> Result<Value, QueryError> { | ||
| info!("{}", "Returning query results"); | ||
| let mut json_records = record_batches_to_json(&self.records)?; | ||
|
|
||
| if self.fill_null { | ||
| for map in &mut json_records { | ||
| for field in &self.fields { | ||
| if !map.contains_key(field) { | ||
| map.insert(field.clone(), Value::Null); | ||
| // Process in batches to avoid massive allocations | ||
| const BATCH_SIZE: usize = 100; // Process 100 record batches at a time | ||
| let mut all_values = Vec::new(); | ||
|
|
||
| for chunk in self.records.chunks(BATCH_SIZE) { | ||
| let mut json_records = record_batches_to_json(chunk)?; | ||
|
|
||
| if self.fill_null { | ||
| for map in &mut json_records { | ||
| for field in &self.fields { | ||
| if !map.contains_key(field) { | ||
| map.insert(field.clone(), Value::Null); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Convert this batch to values and add to collection | ||
| let batch_values: Vec<Value> = json_records.into_iter().map(Value::Object).collect(); | ||
| all_values.extend(batch_values); | ||
| } | ||
| let values = json_records.into_iter().map(Value::Object).collect_vec(); | ||
|
|
||
| let response = if self.with_fields { | ||
| json!({ | ||
| "fields": self.fields, | ||
| "records": values, | ||
| "records": all_values, | ||
| }) | ||
| } else { | ||
| Value::Array(values) | ||
| Value::Array(all_values) | ||
| }; | ||
|
|
||
| Ok(response) | ||
| } | ||
| } | ||
|
|
||
| impl Drop for QueryResponse { | ||
| fn drop(&mut self) { | ||
| force_memory_release(); | ||
| } | ||
| } | ||
|
|
||
| // Rate-limited memory release with proper error handling | ||
| static LAST_PURGE: Mutex<Option<Instant>> = Mutex::new(None); | ||
| const PURGE_INTERVAL: Duration = Duration::from_secs(3600); // 1 hour | ||
| pub fn force_memory_release() { | ||
| { | ||
| let mut last_purge = LAST_PURGE.lock().unwrap(); | ||
| if let Some(last) = *last_purge { | ||
| if last.elapsed() < PURGE_INTERVAL { | ||
| return; | ||
| } | ||
| } | ||
| *last_purge = Some(Instant::now()); | ||
| } | ||
|
|
||
| // Advance epoch to refresh statistics and trigger potential cleanup | ||
| if let Err(e) = tikv_jemalloc_ctl::epoch::mib().and_then(|mib| mib.advance()) { | ||
| warn!("Failed to advance jemalloc epoch: {:?}", e); | ||
| } | ||
|
|
||
| // Purge all arenas using MALLCTL_ARENAS_ALL | ||
| if let Ok(arena_purge) = CString::new("arena.4096.purge") { | ||
| unsafe { | ||
| let ret = tikv_jemalloc_sys::mallctl( | ||
| arena_purge.as_ptr(), | ||
| std::ptr::null_mut(), // oldp (not reading) | ||
| std::ptr::null_mut(), // oldlenp (not reading) | ||
| std::ptr::null_mut(), // newp (void operation) | ||
| 0, // newlen (void operation) | ||
| ); | ||
| if ret != 0 { | ||
| warn!("Arena purge failed with code: {}", ret); | ||
| } else { | ||
| debug!("Successfully purged all jemalloc arenas"); | ||
| } | ||
| } | ||
| } else { | ||
| warn!("Failed to create CString for arena purge"); | ||
| } | ||
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.