From cfc9644420c35ff35084e287d0da66bb269660b9 Mon Sep 17 00:00:00 2001 From: Klaus Ma Date: Thu, 19 Feb 2026 09:55:08 +0800 Subject: [PATCH 1/2] chore: setup per app cache. Signed-off-by: Klaus Ma --- common/src/lib.rs | 2 + executor_manager/src/shims/host_shim.rs | 53 +++++++++++++++++-------- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/common/src/lib.rs b/common/src/lib.rs index 47da80c1..29444893 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -120,6 +120,8 @@ pub fn new_async_ptr(t: T) -> AsyncMutexPtr { Arc::new(tokio::sync::Mutex::new(t)) } +pub const FLAME_HOME: &str = "FLAME_HOME"; +pub const FLAME_LOG: &str = "FLAME_LOG"; pub const FLAME_WORKING_DIRECTORY: &str = "/tmp/flame"; pub const FLAME_INSTANCE_ENDPOINT: &str = "FLAME_INSTANCE_ENDPOINT"; pub const FLAME_CACHE_ENDPOINT: &str = "FLAME_CACHE_ENDPOINT"; diff --git a/executor_manager/src/shims/host_shim.rs b/executor_manager/src/shims/host_shim.rs index 84b4c8d9..0caeb236 100644 --- a/executor_manager/src/shims/host_shim.rs +++ b/executor_manager/src/shims/host_shim.rs @@ -46,8 +46,8 @@ use crate::shims::grpc_shim::GrpcShim; use crate::shims::{Shim, ShimPtr}; use common::apis::{ApplicationContext, SessionContext, TaskContext, TaskOutput, TaskResult}; use common::{ - FlameError, FLAME_CACHE_ENDPOINT, FLAME_ENDPOINT, FLAME_INSTANCE_ENDPOINT, - FLAME_WORKING_DIRECTORY, + FlameError, FLAME_CACHE_ENDPOINT, FLAME_ENDPOINT, FLAME_HOME, FLAME_INSTANCE_ENDPOINT, + FLAME_LOG, FLAME_WORKING_DIRECTORY, }; struct HostInstance { @@ -122,7 +122,6 @@ pub struct HostShim { } const RUST_LOG: &str = "RUST_LOG"; -const FLAME_LOG: &str = "FLAME_LOG"; const DEFAULT_SVC_LOG_LEVEL: &str = "info"; impl HostShim { @@ -154,37 +153,53 @@ impl HostShim { }) } - fn setup_working_directory(work_dir: &Path) -> Result, FlameError> { + /// Setup working directory for an application instance (per-instance). + fn setup_working_directory(work_dir: &Path) -> Result<(), FlameError> { trace_fn!("HostShim::setup_working_directory"); - let tmp_dir = work_dir.join("tmp"); - let uv_cache_dir = work_dir.join(".uv"); - let pip_cache_dir = work_dir.join(".pip"); - tracing::debug!( "Working directory of application instance: {}", work_dir.display() ); + + Self::create_dir(work_dir, "working") + } + + /// Setup per-application cache directories for uv, pip, and tmp. + /// These directories are shared across all instances of the same application. + /// Uses FLAME_HOME/data/cache if FLAME_HOME is set, otherwise falls back to FLAME_WORKING_DIRECTORY/cache. + fn setup_cache(app_name: &str) -> Result, FlameError> { + trace_fn!("HostShim::setup_cache"); + + let cache_base = match env::var(FLAME_HOME) { + Ok(flame_home) => Path::new(&flame_home).join("data").join("cache"), + Err(_) => Path::new(FLAME_WORKING_DIRECTORY).join("cache"), + }; + let app_cache_base = cache_base.join(app_name); + let tmp_dir = app_cache_base.join("tmp"); + let uv_cache_dir = app_cache_base.join("uv"); + let pip_cache_dir = app_cache_base.join("pip"); + tracing::debug!( - "Temporary directory of application instance: {}", + "Temporary directory of application <{}>: {}", + app_name, tmp_dir.display() ); tracing::debug!( - "UV cache directory of application instance: {}", + "UV cache directory of application <{}>: {}", + app_name, uv_cache_dir.display() ); tracing::debug!( - "PIP cache directory of application instance: {}", + "PIP cache directory of application <{}>: {}", + app_name, pip_cache_dir.display() ); - // Create the working, temporary, and cache directories if they don't exist - Self::create_dir(&work_dir, "working")?; Self::create_dir(&tmp_dir, "temporary")?; Self::create_dir(&uv_cache_dir, "UV cache")?; Self::create_dir(&pip_cache_dir, "PIP cache")?; - // Build environment variables for the application instance let mut envs = HashMap::new(); envs.insert("TMPDIR".to_string(), tmp_dir.to_string_lossy().to_string()); envs.insert("TEMP".to_string(), tmp_dir.to_string_lossy().to_string()); @@ -272,12 +287,16 @@ impl HostShim { }; let work_dir = cur_dir.clone(); - // Setup working directory and get environment defaults + + // Setup working directory (per-instance) + Self::setup_working_directory(&work_dir)?; + + // Setup cache directories (per-application) and get environment defaults // Use entry().or_insert() so application-specific envs take precedence over defaults // This allows applications like flmrun to specify UV_CACHE_DIR pointing to // the pre-cached directory instead of using a per-instance empty cache - let wd_envs = Self::setup_working_directory(&work_dir)?; - for (key, value) in wd_envs { + let cache_envs = Self::setup_cache(&app.name)?; + for (key, value) in cache_envs { envs.entry(key).or_insert(value); } From cace3f55e16dbedd2ec11588e7a0ec49407890a7 Mon Sep 17 00:00:00 2001 From: Klaus Ma Date: Thu, 19 Feb 2026 10:02:08 +0800 Subject: [PATCH 2/2] fix: move tmp to working dir. Signed-off-by: Klaus Ma --- executor_manager/src/shims/host_shim.rs | 39 +++++++++++++++---------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/executor_manager/src/shims/host_shim.rs b/executor_manager/src/shims/host_shim.rs index 0caeb236..0eb8997c 100644 --- a/executor_manager/src/shims/host_shim.rs +++ b/executor_manager/src/shims/host_shim.rs @@ -153,19 +153,33 @@ impl HostShim { }) } - /// Setup working directory for an application instance (per-instance). - fn setup_working_directory(work_dir: &Path) -> Result<(), FlameError> { + /// Setup working directory and tmp directory for an application instance (per-instance). + fn setup_working_directory(work_dir: &Path) -> Result, FlameError> { trace_fn!("HostShim::setup_working_directory"); + let tmp_dir = work_dir.join("tmp"); + tracing::debug!( "Working directory of application instance: {}", work_dir.display() ); + tracing::debug!( + "Temporary directory of application instance: {}", + tmp_dir.display() + ); + + Self::create_dir(work_dir, "working")?; + Self::create_dir(&tmp_dir, "temporary")?; + + let mut envs = HashMap::new(); + envs.insert("TMPDIR".to_string(), tmp_dir.to_string_lossy().to_string()); + envs.insert("TEMP".to_string(), tmp_dir.to_string_lossy().to_string()); + envs.insert("TMP".to_string(), tmp_dir.to_string_lossy().to_string()); - Self::create_dir(work_dir, "working") + Ok(envs) } - /// Setup per-application cache directories for uv, pip, and tmp. + /// Setup per-application cache directories for uv and pip. /// These directories are shared across all instances of the same application. /// Uses FLAME_HOME/data/cache if FLAME_HOME is set, otherwise falls back to FLAME_WORKING_DIRECTORY/cache. fn setup_cache(app_name: &str) -> Result, FlameError> { @@ -176,15 +190,9 @@ impl HostShim { Err(_) => Path::new(FLAME_WORKING_DIRECTORY).join("cache"), }; let app_cache_base = cache_base.join(app_name); - let tmp_dir = app_cache_base.join("tmp"); let uv_cache_dir = app_cache_base.join("uv"); let pip_cache_dir = app_cache_base.join("pip"); - tracing::debug!( - "Temporary directory of application <{}>: {}", - app_name, - tmp_dir.display() - ); tracing::debug!( "UV cache directory of application <{}>: {}", app_name, @@ -196,14 +204,10 @@ impl HostShim { pip_cache_dir.display() ); - Self::create_dir(&tmp_dir, "temporary")?; Self::create_dir(&uv_cache_dir, "UV cache")?; Self::create_dir(&pip_cache_dir, "PIP cache")?; let mut envs = HashMap::new(); - envs.insert("TMPDIR".to_string(), tmp_dir.to_string_lossy().to_string()); - envs.insert("TEMP".to_string(), tmp_dir.to_string_lossy().to_string()); - envs.insert("TMP".to_string(), tmp_dir.to_string_lossy().to_string()); envs.insert( "UV_CACHE_DIR".to_string(), uv_cache_dir.to_string_lossy().to_string(), @@ -288,8 +292,11 @@ impl HostShim { let work_dir = cur_dir.clone(); - // Setup working directory (per-instance) - Self::setup_working_directory(&work_dir)?; + // Setup working directory and tmp (per-instance) + let work_dir_envs = Self::setup_working_directory(&work_dir)?; + for (key, value) in work_dir_envs { + envs.entry(key).or_insert(value); + } // Setup cache directories (per-application) and get environment defaults // Use entry().or_insert() so application-specific envs take precedence over defaults