Repository: risingwavelabs/risingwave
File: src/common/src/util/batch_spill_config.rs (Rust)

Code before:
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::path::Path;
use std::sync::LazyLock;

const RW_BATCH_SPILL_DIR_ENV: &str = "RW_BATCH_SPILL_DIR";
const DEFAULT_SPILL_DIR: &str = "/tmp";

/// Returns the base directory for spill (env value or default), without trailing slash.
pub fn batch_spill_base_dir() -> &'static Path {
    static BASE_DIR: LazyLock<String> = LazyLock::new(|| {
        std::env::var(RW_BATCH_SPILL_DIR_ENV).unwrap_or_else(|_| DEFAULT_SPILL_DIR.to_owned())
    });
    Path::new(&*BASE_DIR)
}


Review comment (bug):
This new spill-dir config helper is untested. There are unit tests in other `risingwave_common::util` modules; adding a small test for default behavior and env override (including edge cases like empty/relative values) would help prevent regressions.
```suggestion
/// Resolves the spill base directory from an environment variable result.
/// This is split out mainly to allow unit testing of the resolution logic.
fn resolve_batch_spill_base_dir(
    env_val: Result<String, std::env::VarError>,
) -> String {
    env_val.unwrap_or_else(|_| DEFAULT_SPILL_DIR.to_owned())
}

/// Returns the base directory for spill (env value or default), without trailing slash.
pub fn batch_spill_base_dir() -> &'static Path {
    static BASE_DIR: LazyLock<String> = LazyLock::new(|| {
        resolve_batch_spill_base_dir(std::env::var(RW_BATCH_SPILL_DIR_ENV))
    });
    Path::new(&*BASE_DIR)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::env::VarError;

    #[test]
    fn test_spill_dir_default_when_env_missing() {
        let dir = resolve_batch_spill_base_dir(Err(VarError::NotPresent));
        assert_eq!(dir, DEFAULT_SPILL_DIR);
    }

    #[test]
    fn test_spill_dir_env_override_absolute() {
        let custom = "/data/spill".to_string();
        let dir = resolve_batch_spill_base_dir(Ok(custom.clone()));
        assert_eq!(dir, custom);
    }

    #[test]
    fn test_spill_dir_env_override_empty_string() {
        let dir = resolve_batch_spill_base_dir(Ok(String::new()));
        assert_eq!(dir, "");
    }

    #[test]
    fn test_spill_dir_env_override_relative_path() {
        let custom = "relative/spill/dir".to_string();
        let dir = resolve_batch_spill_base_dir(Ok(custom.clone()));
        assert_eq!(dir, custom);
    }
}
```

Code after:
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::path::{Path, PathBuf};
use std::sync::LazyLock;

const RW_BATCH_SPILL_DIR_ENV: &str = "RW_BATCH_SPILL_DIR";
const DEFAULT_SPILL_DIR: &str = "/tmp";

/// Returns the base directory for spill (env value or default)
pub fn batch_spill_base_dir() -> &'static Path {
    static BASE_DIR: LazyLock<PathBuf> = LazyLock::new(|| {
        let raw =
            std::env::var(RW_BATCH_SPILL_DIR_ENV).unwrap_or_else(|_| DEFAULT_SPILL_DIR.to_owned());
        let mut dir = PathBuf::from(raw.trim());
        if dir.is_relative() {
            dir = std::env::current_dir().unwrap().join(dir);
        }
        dir
    });
    BASE_DIR.as_path()
}