Skip to content

feat(single-node): add memory allocation #19895

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

Merged
merged 2 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
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
37 changes: 33 additions & 4 deletions src/cmd_all/src/single_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
use clap::Parser;
use home::home_dir;
use risingwave_common::config::MetaBackend;
use risingwave_common::util::resource_util::memory::system_memory_available_bytes;
use risingwave_compactor::CompactorOpts;
use risingwave_compute::memory::config::gradient_reserve_memory_bytes;
use risingwave_compute::ComputeNodeOpts;
use risingwave_frontend::FrontendOpts;
use risingwave_meta_node::MetaNodeOpts;
Expand Down Expand Up @@ -85,7 +87,7 @@ impl SingleNodeOpts {
#[derive(Eq, PartialOrd, PartialEq, Debug, Clone, Parser)]
pub struct NodeSpecificOpts {
// ------- Compute Node Options -------
/// Total available memory for the compute node in bytes. Used by both computing and storage.
/// Total available memory for all the nodes
#[clap(long)]
pub total_memory_bytes: Option<usize>,

Expand Down Expand Up @@ -207,10 +209,21 @@ pub fn map_single_node_opts_to_standalone_opts(opts: SingleNodeOpts) -> ParsedSt
frontend_opts.meta_addr = meta_addr.parse().unwrap();
compactor_opts.meta_address = meta_addr.parse().unwrap();

// Allocate memory for each node
let total_memory_bytes = if let Some(total_memory_bytes) = opts.node_opts.total_memory_bytes {
total_memory_bytes
} else {
system_memory_available_bytes()
};
frontend_opts.frontend_total_memory_bytes = memory_for_frontend(total_memory_bytes);
compactor_opts.compactor_total_memory_bytes = memory_for_compactor(total_memory_bytes);
compute_opts.total_memory_bytes = total_memory_bytes
- memory_for_frontend(total_memory_bytes)
- memory_for_compactor(total_memory_bytes);
compute_opts.memory_manager_target_bytes =
Some(gradient_reserve_memory_bytes(total_memory_bytes));

// Apply node-specific options
if let Some(total_memory_bytes) = opts.node_opts.total_memory_bytes {
compute_opts.total_memory_bytes = total_memory_bytes;
}
if let Some(parallelism) = opts.node_opts.parallelism {
compute_opts.parallelism = parallelism;
}
Expand All @@ -234,3 +247,19 @@ pub fn map_single_node_opts_to_standalone_opts(opts: SingleNodeOpts) -> ParsedSt
compactor_opts: Some(compactor_opts),
}
}

fn memory_for_frontend(total_memory_bytes: usize) -> usize {
if total_memory_bytes <= (16 << 30) {
total_memory_bytes / 8
} else {
(total_memory_bytes - (16 << 30)) / 16 + (16 << 30) / 8
}
}

fn memory_for_compactor(total_memory_bytes: usize) -> usize {
if total_memory_bytes <= (16 << 30) {
total_memory_bytes / 8
} else {
(total_memory_bytes - (16 << 30)) / 16 + (16 << 30) / 8
}
}
10 changes: 9 additions & 1 deletion src/compute/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ pub struct ComputeNodeOpts {
pub total_memory_bytes: usize,

/// Reserved memory for the compute node in bytes.
/// If not set, a portion (default to 30%) for the `total_memory_bytes` will be used as the reserved memory.
/// If not set, a portion (default to 30% for the first 16GB and 20% for the rest)
/// for the `total_memory_bytes` will be used as the reserved memory.
///
/// The total memory compute and storage can use is `total_memory_bytes` - `reserved_memory_bytes`.
#[clap(long, env = "RW_RESERVED_MEMORY_BYTES")]
Expand All @@ -97,6 +98,13 @@ pub struct ComputeNodeOpts {
/// If not set, the default value is `total_memory_bytes` - `reserved_memory_bytes`
///
/// It's strongly recommended to set it for standalone deployment.
///
/// ## Why need this?
///
/// Our [`crate::memory::manager::MemoryManager`] works by reading the memory statistics from
/// Jemalloc. This is fine when running the compute node alone; while for standalone mode,
/// the memory usage of **all nodes** are counted. Thus, we need to pass a reasonable total
/// usage so that the memory is kept around this value.
#[clap(long, env = "RW_MEMORY_MANAGER_TARGET_BYTES")]
pub memory_manager_target_bytes: Option<usize>,

Expand Down
2 changes: 1 addition & 1 deletion src/compute/src/memory/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub fn reserve_memory_bytes(opts: &ComputeNodeOpts) -> (usize, usize) {
/// The reserved memory size is calculated based on the following gradient:
/// - 30% of the first 16GB
/// - 20% of the rest
fn gradient_reserve_memory_bytes(total_memory_bytes: usize) -> usize {
pub fn gradient_reserve_memory_bytes(total_memory_bytes: usize) -> usize {
let mut total_memory_bytes = total_memory_bytes;
let mut reserved = 0;
for i in 0..RESERVED_MEMORY_LEVELS.len() {
Expand Down
Loading