Skip to content

Scope PR SonarCloud analysis to PR diff and fix master OOM - #19355

Open
JackieTien97 wants to merge 3 commits into
masterfrom
ty/sonar-pr-diff
Open

JackieTien97 wants to merge 3 commits into
masterfrom
ty/sonar-pr-diff

Conversation

@JackieTien97

@JackieTien97 JackieTien97 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Description

Currently, SonarCloud checks on pull requests frequently fail the Quality Gate with issues from unrelated files and untouched lines, while full master push scans have been failing with OutOfMemoryError: Java heap space.

Root Causes Observed

  1. Shallow clone breaks Git SCM diff and changed-lines detection on PRs:
    actions/checkout@v5 in .github/workflows/sonar-codecov.yml used the default fetch-depth: 1, without fetching the base branch ref. In PR runs (for example PR Bound MPP sink notifications by the query deadline #19344), the Sonar scanner logged:

    • [WARNING] Could not find ref 'master' in refs/heads, refs/remotes, refs/remotes/upstream or refs/remotes/origin (in both SCM collecting changed files in the branch and SCM writing changed lines)
    • [WARNING] Shallow clone detected, no blame information will be provided.

    Without local Git merge-base and changed-line information, SonarCloud fell back to comparing against the last analyzed master snapshot and treated entire differing files as "New Code".

  2. master push scans OOM with default JVM heap:
    MAVEN_OPTS in .github/workflows/sonar-codecov.yml did not specify -Xmx, causing full-repo master push analysis to fail with java.lang.OutOfMemoryError: Java heap space and leaving the SonarCloud master baseline and server-side cache stale.

  3. Unnecessary scans on non-Java / example PRs and unused profiles:
    The sonar job always ran -P with-integration-tests,with-code-coverage even though -DskipTests and -pl '!distribution,!integration-test' were passed, executing hundreds of skipped JaCoCo goals, scanning example/* sample code, and running full builds even on test-only or docs/CI-only PRs.

Changes

  • Check out PR head SHA with full Git history and base ref resolution:
    • Set actions/checkout@v5 ref to github.event.pull_request.head.sha on pull_request (and github.sha on push) with fetch-depth: 0, avoiding discrepancies introduced by GitHub's synthetic merge commit when the base branch moves files.
    • Ensure refs/remotes/origin/${BASE_REF} is present so SonarCloud's native SCM changed-files, changed-lines, and blame analysis work accurately on PR diffs.
  • Exclude example and integration-test from Sonar analysis:
    • Set <sonar.skip>true</sonar.skip> in example/pom.xml (inherited by all example/* submodules, avoiding Maven -pl orphan-module errors and keeping architecture-test dependencies intact) and in integration-test/pom.xml.
  • Skip SonarCloud on PRs without analyzable main Java changes, keep full analysis scope when running:
    • .github/scripts/check-sonar-pr-diff.py inspects the PR three-dot diff (git diff --name-only --diff-filter=ACMR refs/remotes/origin/<base>...HEAD) and skips JDK setup, caches, and Maven/Sonar when no main Java source (src/main/java/**/*.java, src/main/i18n/en/**/*.java) in eligible default-reactor modules (excluding distribution and any <sonar.skip>true</sonar.skip> subtree such as example) changed.
    • When a scan runs, keep the full non-distribution reactor scope (-pl '!distribution' -am) so cross-module duplications and analyses remain visible to the Quality Gate while SonarCloud's server-side cache skips unchanged files.
  • Keep push-triggered full scans for all branches in on.push.branches (master, rel/*, rc/*) so every target branch that receives PR analysis maintains an up-to-date SonarCloud baseline (codecov is unchanged).
  • Increase heap and clean up Maven invocation for the sonar job: set MAVEN_OPTS with -Xms2g -Xmx8g, drop unused -P with-integration-tests,with-code-coverage, and cache ~/.sonar/cache.

Before / After Examples

  • PR modifying Java sources (e.g., PR Bound MPP sink notifications by the query deadline #19344, 14 main Java files changed):
    • Before: Shallow clone failed to find master ref, reported 143 files / 34,173 new lines / 6 unrelated bugs from other master commits, and failed the Quality Gate.
    • After: Checks out github.event.pull_request.head.sha with fetch-depth: 0 and refs/remotes/origin/master, runs full-reactor analysis (-pl '!distribution' -am, skipping example/* via sonar.skip) so cross-module duplicates remain visible, and lets SonarCloud evaluate only the PR diff's changed lines.
  • PR touching only tests, example/**, integration-test/**, docs, or workflow files (e.g., this PR):
    • Before: Built all 50 modules and ran full Sonar scan (~11 min).
    • After: check-sonar-pr-diff.py emits run_sonar=false and skips Maven/Sonar in ~20s.
  • push to master, rel/*, or rc/*:
    • Before: Full scan failed with java.lang.OutOfMemoryError: Java heap space under default heap.
    • After: Runs full scan (-pl '!distribution' -am, skipping example/* via sonar.skip) with -Xms2g -Xmx8g and fetch-depth: 0 to keep the SonarCloud baseline and server-side cache up to date.

This PR has:

  • been self-reviewed.
  • added or updated version, license, or notice information
  • added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.

Key changed/added files in this PR
  • .github/workflows/sonar-codecov.yml
  • .github/scripts/check-sonar-pr-diff.py
  • example/pom.xml
  • integration-test/pom.xml

@codecov

codecov Bot commented Nov 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 49.43%. Comparing base (dd450f7) to head (dbba4d7).

Additional details and impacted files
@@             Coverage Diff              @@
##             master   #19355      +/-   ##
============================================
- Coverage     49.44%   49.43%   -0.01%     
  Complexity      718      718              
============================================
  Files          5656     5656              
  Lines        418222   418222              
  Branches      55296    55296              
============================================
- Hits         206769   206756      -13     
- Misses       211453   211466      +13     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@JackieTien97
JackieTien97 requested review from HTHou and a balanced review from Copilot November 23, 2026 01:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The workflow excludes dev/1.3 from its push triggers, making its scan condition unreachable and preventing SonarCloud baseline updates for that branch.

1 open issue
What changed in this PR

This PR aims to make SonarCloud analysis reflect PR changes more accurately, reduce unnecessary builds, and address heap exhaustion during full branch scans.

Changes:

  • Fetch full Git history and resolve the PR base ref for changed-line detection.
  • Select affected Maven modules for Java-source PRs and skip analysis when none qualify.
  • Increase Maven heap, cache SonarCloud packages, and simplify the analysis command.
File Description
.github/​workflows/​sonar-codecov.yml Integrates scan selection, full-history checkout, heap settings, and caching.
.github/​scripts/​select-sonar-modules.py Maps changed main Java sources to eligible Maven modules and emits workflow outputs.

Review effort: Balanced

Comment thread .github/workflows/sonar-codecov.yml Outdated
if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == 'apache/iotdb' || github.event_name == 'push'
if: >-
(github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == 'apache/iotdb') ||
(github.event_name == 'push' && (github.ref == 'refs/heads/master' || github.ref == 'refs/heads/dev/1.3'))

@JackieTien97 JackieTien97 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Keep cross-module duplicates visible to the quality gate. With -pl iotdb-api/pipe-api -am, Maven leaves udf-api out of the scan. SonarCloud reports a 123-line duplicate between the Binary.java files in those two modules. A PR that copies code from one into the other can therefore pass the new-code duplication gate when only the destination module changes. Keep the skip for PRs with no analyzable Java changes, and use the target branch’s full analysis scope when a scan runs. Existing duplicate

  2. Check out the PR head for the Sonar job. The default checkout makes --head-ref HEAD point to GitHub’s synthetic merge commit. I reproduced a clean merge where the base moved a file from module A to B while the PR edited it in A. The selector chose A on the PR head and B on the merge commit. Set the checkout ref to github.event.pull_request.head.sha for PRs and github.sha for pushes. Checkout configuration

  3. Refresh the baseline for release-branch PRs. PRs targeting rc/* and rel/* still run Sonar, but the new push condition prevents those branches from establishing or refreshing their baseline. SonarCloud can then attribute issues already on the release branch to a later PR. Keep push analysis enabled for release branches that receive PR analysis. SonarCloud baseline requirements

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants