← Back to Pull requests

test(scheduler): add unit tests for YuniKorn resource usage calculations #3231

Open
unknown opened this pull request with 416 lines diff across 5 files
View on GitHub
unknown unknown commented on Oct 13, 2026 02:15 UTC
Author

Purpose of this PR

internal/scheduler/yunikorn/resourceusage had 16.5% statement coverage. Only byteStringAsBytes, bytesToMi and cpuRequest had direct tests. The entire memory calculation chain — the part that decides what CPU and memory YuniKorn actually reserves for driver and executor pods — was exercised only indirectly, through TestSchedule in the parent package.

This adds direct table tests for that chain. Coverage goes from 16.5% to 97.8%. No production code is touched.

Part of #3202.

Proposed changes:

  • Add tests to memory_test.go for isJavaApp, getMemoryOverheadFactor, memoryRequestBytes, executorPysparkMemoryBytes, sparkOffHeapMemoryBytes, driverMemoryRequest and executorMemoryRequest, including their error paths.

  • Add tests to resource_usage_test.go for DriverPodRequests and ExecutorPodRequests, covering defaults, coreRequest precedence over cores, and error propagation.
  • Change Category

    <!-- Test-only change; none of the categories below apply. -->

  • Bugfix (non-breaking change which fixes an issue)

  • Feature (non-breaking change which adds functionality)

  • Breaking change (fix or feature that could affect existing functionality)

  • Documentation update
  • Rationale

    These functions encode Spark's memory-overhead semantics, which are easy to get wrong and currently unpinned by any test. The cases added lock down:

  • the 384Mi minimum overhead floor, and the boundary where the proportional factor overtakes it;

  • the JVM (0.1) versus non-JVM (0.4) default overhead factors, and the fact that Java and Scala are the JVM types;

  • precedence of an explicit memoryOverhead and memoryOverheadFactor over the type-derived defaults;

  • the mebibyte default applied to a suffixless spark.executor.pyspark.memory, matching Spark's documented behaviour;

  • addition of PySpark and off-heap memory to the executor request;

  • the floor-to-MiB conversion that mirrors Spark's BasicDriverFeatureStep / BasicExecutorFeatureStep.
  • Expected values were derived from the Spark semantics rather than from current output, so these are regression tests, not a snapshot of existing behaviour.

    Checklist

  • I have conducted a self-review of my own code.

  • I have updated documentation accordingly.

  • I have added tests that prove my changes are effective or that my feature works.

  • Existing unit tests pass locally with my changes.
  • Additional Notes

    Local run:

    go test ./internal/scheduler/yunikorn/resourceusage/... -cover
    ok  github.com/kubeflow/spark-operator/v2/internal/scheduler/yunikorn/resourceusage  0.548s  coverage: 97.8% of statements
    

    The residual ~2% is the strconv.ParseInt error branch in byteStringAsBytes, which is unreachable because the preceding regex already guarantees the capture group is digits.

    @nabuskey @vjanelle @tariq-hasan One observation found while writing these, not addressed here: sparkOffHeapMemoryBytes compares spark.memory.offHeap.enabled against the exact string "false", so "False" or "FALSE" would be treated as enabled. Spark parses this case-insensitively. I deliberately did not add a test for it, since that would pin the current behaviour rather than flag it. Happy to open a separate issue if maintainers agree it's worth fixing.

    Diff: 416 lines (+312 / -104) across 5 files Reviewer: vjanelle Review duration: 70117m
    All Vouch review confidence checks passed
    Residual risk 0.31 = Change Risk (0.52) × (1 − Review Confidence (0.41)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 416 lines diff across 5 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    40%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.361
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #3231"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 40.9% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 51.9% (MEDIUM) · Higher = larger blast radius
    51.9%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 40.9% · Lower = rushed or shallow review
    40.9%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 30.7% · Safe to merge
    30.7%