Skip to content

[fix][broker] Avoid load shedding and metadata writes from a former leader - #26253

Merged
lhotari merged 7 commits into
apache:masterfrom
void-ptr974:fix/modular-load-manager-leader-guard
Sep 12, 2026
Merged

lhotari merged 7 commits into
apache:masterfrom
void-ptr974:fix/modular-load-manager-leader-guard

Conversation

@void-ptr974

Copy link
Copy Markdown
Contributor

Motivation

Load-balancer tasks are started only while a broker is the leader. However, cancelling a scheduled task does not stop an invocation that is already in progress. If leadership changes during such an invocation, the former leader can still initiate bundle unloading or persist load-balancing metadata.

Modifications

  • Recheck leadership before initiating bundle unloading.
  • Recheck leadership before writing bundle and broker load data to metadata.
  • Stop the remaining work when leadership has changed.

Verifying this change

This change added tests and can be verified as follows:

  • Added coverage for a follower skipping load shedding.
  • Added coverage for leadership changing after destination selection and before bundle unload.
  • Added coverage for leadership changing after aggregation and before metadata writes.
  • Ran ./gradlew :pulsar-broker:test -PtestGroups=broker -PexcludedTestGroups='' --tests org.apache.pulsar.broker.loadbalance.impl.ModularLoadManagerImplTest --console=plain
  • Ran ./gradlew :pulsar-broker:checkstyleMain :pulsar-broker:checkstyleTest --console=plain

Does this pull request potentially affect one of the following parts:

  • Dependencies (add or upgrade a dependency)
  • The public API
  • The schema
  • The default values of configurations
  • The threading model
  • The binary protocol
  • The REST endpoints
  • The admin CLI options
  • The metrics
  • Anything that affects deployment

…eader

Recheck leader state before load-shedding side effects and metadata writes so a task that was already running stops after leadership changes.
@void-ptr974
void-ptr974 force-pushed the fix/modular-load-manager-leader-guard branch from ef5750a to d5e269e Compare July 28, 2026 01:58
@Dream95

Dream95 commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Leader can still change between the isLeader() check and the actual unload, so this cannot fully prevent a former leader from triggering an unload.

@void-ptr974

Copy link
Copy Markdown
Contributor Author

Thanks. This PR intentionally provides only a best-effort guard and does not address the narrow race between the leadership check and the unload itself. Providing a strong guarantee would require a broader fencing design, which is out of scope for this focused change.

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The best-effort leadership checks look appropriate for the stated scope. I agree with the existing metrics-accounting comment: please preserve broker accounting and publish the completed unloads when leadership loss stops a partially completed run. A regression case that loses leadership after the first successful unload would cover this. This request concerns the reported metrics; stopping further unloads is correct.

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. The leadership-loss paths now preserve accounting for completed unloads and publish the metrics before stopping. The added regression test covers leadership loss after the first successful unload and verifies both published totals. This addresses my previous concern.

Preserve leadership checks and unload-attempt completion cleanup, with assertions covering cleanup after leadership loss.
@lhotari

lhotari commented Sep 12, 2026

Copy link
Copy Markdown
Member

Merged origin/master into this branch in fef0d87 and resolved the conflicts in ModularLoadManagerImpl and its tests.

The resolution preserves the leadership checks together with master's onUnloadAttemptCompleted() cleanup, including when leadership is lost. It also removes the duplicate getLoadData() accessor, verifies cleanup in the leadership-change tests, and adapts the strategy-focused test to simulate a leader.

Validation: quickCheck passed, and the full ModularLoadManagerImplTest class passed all 24 enabled tests, with retries and fail-fast disabled and test-group exclusions cleared. Two pre-existing tests remain disabled in the source.

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@lhotari
lhotari merged commit af2c40d into apache:master Sep 12, 2026
69 of 71 checks passed
@lhotari lhotari added this to the 5.0.0-M2 milestone Sep 12, 2026
lhotari added a commit that referenced this pull request Sep 23, 2026
…eader (#26253)

Co-authored-by: Lari Hotari <[email protected]>
(cherry picked from commit af2c40d)
lhotari added a commit that referenced this pull request Sep 23, 2026
…eader (#26253)

Co-authored-by: Lari Hotari <[email protected]>
(cherry picked from commit af2c40d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants