Skip to content

[coordinator] Add rebalance progress and outcome metrics - #4373

Open
heyallencao wants to merge 1 commit into
apache:mainfrom
heyallencao:coordinator/rebalance-metrics-4055
Open

heyallencao wants to merge 1 commit into
apache:mainfrom
heyallencao:coordinator/rebalance-metrics-4055

Conversation

@heyallencao

@heyallencao heyallencao commented Sep 16, 2026 •

Copy link
Copy Markdown

Purpose

Closes #4055.

Expose coordinator-level metrics for monitoring rebalance progress, duration, and outcomes without polling the Admin API.

Brief change log

  • Add seven gauges covering rebalance activity, pending/completed/failed/timed-out buckets, total elapsed time, and the current bucket's elapsed time.
  • Add three counters for successfully completed, failed, and canceled rebalance rounds.
  • Register the metrics once for the coordinator process lifetime through RebalanceMetrics, owned by CoordinatorMetricGroup. Retain the existing coordinator scope without additional table or bucket labels.
  • Bind each RebalanceManager in startup() and unbind it on close. Counters survive leadership changes, while gauges return zero on standby.
  • Reuse the existing task maps, state, and timestamps, and add regression tests and metric documentation.

Outcome semantics

A round with any failed or timed-out buckets increments the failure counter. The existing Admin API's COMPLETED status means that execution has finished, not necessarily that every bucket succeeded; this PR does not change that API behavior.

An empty plan counts as a successful round. The cancellation counter increments only when a running rebalance is canceled; repeated cancellation does not increment it again.

Leadership changes and recovery

Counters accumulate across all leader terms served by the same coordinator process and reset only when that process restarts. They remain unchanged on standby and are not transferred between coordinator processes.

Bucket-outcome gauges include only results observed in the current leader term. They remain available after completion or cancellation within that term and are cleared when the next round is registered. All gauges return zero on standby. Since per-bucket outcomes are not persisted, recovering an already finished round leaves the bucket-outcome gauges at zero and does not increment round counters. This avoids reporting historical failures as successes. These zeros mean that no outcomes were observed in the current term, not that the historical round had no failures.

Tests

  • 84 targeted tests passed across 10 test classes, including all 19 RebalanceManagerTest tests and both RebalanceManagerITCase integration tests, plus coordinator lifecycle/election and metric-group tests.
  • Regression coverage includes standby registration, counter continuity across leader terms, task recovery, metric registration/unregistration, and constructor-failure handling.
  • Spotless, Checkstyle, Apache RAT, and git diff --check passed.

API and Format

No changes to public client APIs, RPC messages, persisted rebalance task format, or rebalance execution semantics.

This adds monitoring metrics. Their process-scoped lifetime and recovery behavior are documented.

Documentation

Add a Rebalance Metrics section to the monitoring documentation, describing metric names, types, meanings, and behavior during leadership changes and task recovery.

@morazow morazow 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.

Thanks @heyallencao 🚀

Added minor nits

@heyallencao

Copy link
Copy Markdown
Author

Thanks @heyallencao 🚀

Added minor nits

Thanks for the review, @morazow! All three comments are addressed. The 18 targeted rebalance tests and style checks pass.

@heyallencao heyallencao reopened this Sep 17, 2026
@morazow

morazow commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Perfect!

Another suggestion:

Could we give the rebalance metrics the same lifetime as the coordinator process? We could introduce a RebalanceMetrics class owned by CoordinatorMetricGroup and register the counters and gauges once.

Then each RebalanceManager would bind/attach itself when created and unbind on close. The gauges would read from the current manager and return zero on standby, while counters would accumulate across leader terms within the same process.

This would avoid resetting counters and unregistering/re-registering metrics on leadership changes, and remove the need for a separate RebalanceMetricGroup:

  • counters accumulate over every leader term the process serves and restart only when the process restarts
  • gauges stay registered and report 0 on a standby, which is a meaningful value rather than an absent series
  • getOrAddRebalanceMetricGroup, the RebalanceMetricGroup inner class and the CoordinatorMetricGroup.close() override all go away

Rough structure as below:

public class RebalanceMetrics {
    private final Counter rebalancesCompleted = new ThreadSafeSimpleCounter();
    // ...
    /** The manager of the current leader term, or null while this server is not the leader. */
    private volatile @Nullable RebalanceManager current;

    public RebalanceMetrics(MetricGroup metricGroup) {
        metricGroup.counter(MetricNames.REBALANCES_COMPLETED_TOTAL, rebalancesCompleted);
        // ...
        metricGroup.gauge(
                MetricNames.REBALANCE_BUCKETS_PENDING,
                () -> readCurrent(RebalanceManager::pendingBucketCount));
        // ...
    }

    private long readCurrent(ToLongFunction<RebalanceManager> read) {
        RebalanceManager manager = current;
        return manager == null ? 0L : read.applyAsLong(manager);
    }

    void bind(RebalanceManager manager) {
        current = manager;
    }

    void unbind(RebalanceManager manager) {
        // a late close from an ended term must not detach the manager that replaced it
        if (current == manager) {
            current = null;
        }
    }
}

RebalanceManager then takes coordinatorMetricGroup.getRebalanceMetrics(), calls metrics.bind(this) in the constructor and metrics.unbind(this) in close(), and increments through metrics.incRebalancesCompleted() and friends.

What do you think about this? The initial ticket wanted per leader term, but that would be misleading: counters are not monotonic and readers could not tell a leader reset from a real drop.

@morazow morazow 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.

Thanks @heyallencao 🚀

I have added couple improvements 🤝 Then should be good to go!

@heyallencao

heyallencao commented Sep 19, 2026 •

Copy link
Copy Markdown
Author

Thanks @morazow! I have addressed all six suggestions:

  • Documented the concurrency model of RebalanceMetrics and marked it @ThreadSafe.
  • Added the null check for RebalanceMetrics.
  • Ensured RebalanceManager is closed through finally during coordinator shutdown.
  • Replaced repeated scans of finished results with per-status AtomicLong counters.
  • Reset those counters when registering a new rebalance.
  • Incremented the corresponding counter when a bucket finishes.

I also did another self-review. All 19 RebalanceManagerTest tests pass, along with Checkstyle, Spotless, and RAT checks. Thanks again for the thoughtful suggestions!

@morazow

morazow commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

Thanks @heyallencao! Looks good 🚀

@affo affo 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.

Hello! Amazing contribution! Thank you!

I have a question, with HA enabled, how does a standby keeps up with the old state of metrics?
For example, if the count is 5, a new standby would start from 0 right?
Is this a wanted semantic? So, one see that coordinator ID is different and no problem?
Or we expect this metric to stay consistent through time and we should store that in ZK?

If so, I think the state can be gathered already from ZK as per #3985

@heyallencao heyallencao reopened this Sep 23, 2026
@heyallencao

Copy link
Copy Markdown
Author

Hello! Amazing contribution! Thank you!

I have a question, with HA enabled, how does a standby keeps up with the old state of metrics? For example, if the count is 5, a new standby would start from 0 right? Is this a wanted semantic? So, one see that coordinator ID is different and no problem? Or we expect this metric to stay consistent through time and we should store that in ZK?

If so, I think the state can be gathered already from ZK as per #3985

Thanks for raising this. These counters are scoped to each coordinator process. A standby reports its own accumulated counts, which are zero if it has never processed a rebalance. When it becomes leader, it does not inherit the previous leader's counts. A process restart also resets its counters.

This is the intended per-process semantic, so these metrics do not provide a cluster-wide cumulative total that remains continuous across coordinators. The existing persisted rebalance state is not sufficient to reconstruct such a total accurately. If a cluster-wide cumulative total is needed, I think it should be defined as a separate metric contract, with its persistence semantics made explicit.

@affo affo 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.

@heyallencao what you say makes sense, metrics are indeed scoped on a per-pod basis, so there is no chance to mis-interpret them 🤝

@heyallencao

Copy link
Copy Markdown
Author

@heyallencao what you say makes sense, metrics are indeed scoped on a per-pod basis, so there is no chance to mis-interpret them 🤝

thanks

@heyallencao

Copy link
Copy Markdown
Author

Hi @morazow @affo, thanks again for reviewing and approving this PR. I've merged current main into the branch in 75ca530, resolved the coordinator test-helper conflict, and updated the constructor-failure regression test to retain the remote storage configuration now required by main.

Local validation with JDK 17 and Maven 3.9.14:

  • Passed mvn -B -pl fluss-server -am verify '-Dtest=*Rebalance*Test,*Coordinator*Test,*Metric*Test' -Dsurefire.failIfNoSpecifiedTests=false, including all 19 RebalanceManagerTest cases; zero Checkstyle violations.
  • Passed mvn -B -pl fluss-common,fluss-server spotless:check validate.
  • Full mvn -B verify stopped in LakeStorageTest.testWithPluginManager with Mockito's "Cannot call abstract real method on java object!" error. That test and its lake-storage implementation are unchanged by this PR, so I have not modified them here.

Could you please take another look and help move this toward merge? The new GitHub Actions runs also conclude action_required (for example, CI); could a maintainer please check and approve the workflows if needed? Thanks!

@polyzos

polyzos commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Thanks @heyallencao, this looks good to me. LGTM 🚀

One small optional suggestion, since metric names become hard to change once dashboards depend on them: all the other new metrics start with "rebalance", but the in-flight bucket duration doesn't.

    public static final String INFLIGHT_BUCKET_DURATION_MS = "inflightBucketDurationMs";

Something like rebalanceInflightBucketDurationMs would make it clear on a dashboard that it belongs to rebalancing.

Happy to merge as is if you'd rather keep it.

@heyallencao

Copy link
Copy Markdown
Author

Thanks @polyzos! Agreed — I've renamed the metric to rebalanceInflightBucketDurationMs and updated the constant, registration, tests, and documentation consistently.

Local validation passed: all 19 RebalanceManagerTest cases, Checkstyle, and the changed modules' Spotless/license checks.

The previous revision's core Java CI, connectors, documentation, and license checks passed. The Python 3.9 integration job failed before running tests because actions/setup-python could not find Python 3.9 x64 for Ubuntu 26.04 (log); the Python 3.10–3.12 jobs were cancelled. This appears to be a runner/Python availability issue. I'll leave the workflow configuration outside this PR.

Thanks again for the review!

@heyallencao

Copy link
Copy Markdown
Author

@morazow @affo The latest update merges the current main into this PR and resolves the merge conflict. GitHub now reports the branch as up to date and mergeable. Could you please take another look at the latest commit 83c54bca and confirm whether your approvals still stand? The six Actions runs for this fork PR are currently action_required with no jobs started. Could a maintainer approve the workflows so CI can run? Thanks!

@affo

affo commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

@heyallencao thanks for the great contribution, I am going to raise attention to committers to land this 🤝

@heyallencao

Copy link
Copy Markdown
Author

@heyallencao thanks for the great contribution, I am going to raise attention to committers to land this 🤝

Thanks!

@polyzos

polyzos commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Thanks @heyallencao! The changes look good to me.

One request before merging: could you remove the new Mockito usage in RebalanceManagerTest? The project prefers real objects or small Testing* implementations. The test class already builds a real event processor you can reuse, and a minimal recording registry would cover the register/unregister checks.

Optional nit: the new metrics table in the docs leaves out the Scope/Infix columns that the other tables have.

Thanks!

@heyallencao

Copy link
Copy Markdown
Author

Thanks @heyallencao! The changes look good to me.

One request before merging: could you remove the new Mockito usage in RebalanceManagerTest? The project prefers real objects or small Testing* implementations. The test class already builds a real event processor you can reuse, and a minimal recording registry would cover the register/unregister checks.

Optional nit: the new metrics table in the docs leaves out the Scope/Infix columns that the other tables have.

Thanks!

Thanks for the review. Both comments are addressed in commit 47ef219:

Removed Mockito from RebalanceManagerTest — metric registration is now verified with a minimal hand-written RecordingMetricRegistry, and the remaining mock(CoordinatorEventProcessor) calls are replaced with real processors built via the existing buildCoordinatorEventProcessor() helper.

@polyzos

polyzos commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

@heyallencao LGTM 👍 Please rebase so we can merge.

@heyallencao
heyallencao force-pushed the coordinator/rebalance-metrics-4055 branch from 47ef219 to ed98c3c Compare November 4, 2026 09:40
@heyallencao

Copy link
Copy Markdown
Author

@heyallencao LGTM 👍 Please rebase so we can merge.

@polyzos Thanks for the review! Rebased onto the latest main — conflicts
resolved as unions of the upstream refactor and the metrics changes.

Verified locally: RebalanceManagerTest 19/19 passed, spotless + checkstyle
clean. Ready to merge once CI is green.

Expose coordinator process metrics for concurrent bucket migrations, timeout reconciliation, and persisted rebalance outcomes.

Generated-by: OpenAI Codex
@heyallencao
heyallencao force-pushed the coordinator/rebalance-metrics-4055 branch from c9d77fb to 095f575 Compare November 23, 2026 07:05
@heyallencao

Copy link
Copy Markdown
Author

Hi @polyzos, @morazow, and @affo, thanks again for the review! It’s been a while since the last update, and changes on main brought back a few conflicts. I’ve resolved them and verified the changes locally. The earlier CI runs expired while waiting for approval. If you have time soon, could you take another look and help approve the new CI runs? It’d be great to wrap this up while the branch is up to date. Thanks!

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.

[server] Add coordinator metrics for rebalance

4 participants