Skip to content

[server] Add listRebalances API and expose rebalance timestamps - #3985

Open
morazow wants to merge 4 commits into
apache:mainfrom
morazow:rebalance-progress
Open

morazow wants to merge 4 commits into
apache:mainfrom
morazow:rebalance-progress

Conversation

@morazow

@morazow morazow commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Rebalance operations were observable only while in flight: listRebalanceProgress(id)
served the current rebalance, and nothing recorded when one started or finished. This PR
adds the history and the timestamps, then surfaces them through the SQL procedure.

Storage and the new listing API

  • RebalanceTask carries startedAtMs/completedAtMs (JSON serde v2; v1 znodes read
    back as -1, meaning unset).
  • On completion or cancellation the final task is also written to a bounded ZooKeeper
    history at /cluster/rebalance_history/<rebalanceId> (last 10 kept). A failed history
    write is logged and never fails the transition; corrupt entries are skipped, not
    fatal
    (review update) corrupt entries are skipped on read and deleted at the next
    retention trim.
  • New Admin#listRebalances() (ApiKeys.LIST_REBALANCES) returns a RebalanceInfo
    summary - id, status, timestamps - for the current rebalance plus the retained history,
    newest first. Per-bucket detail stays on listRebalanceProgress(id).
  • (review update) listRebalanceProgress(id) now also serves a historical rebalance
    id from the ZooKeeper history, with full per-bucket detail and the task's final status.
    An id that is neither current nor retained still throws
    NoRebalanceInProgressException, with a message pointing at listRebalances().

Timestamps on the existing progress RPC

  • RebalanceProgress gains startedAtMs()/completedAtMs(), transported by two new
    optional fields on ListRebalanceProgressResponse (-1 maps to absent).
  • RebalanceProgressJsonSerializer emits both when set.

SQL surface

  • sys.list_rebalance gains started_at/completed_at TIMESTAMP_LTZ(3) columns.
  • Called without a rebalance id it now returns one row per known rebalance (current plus
    retained history, newest first) instead of only the current one. Historical rows carry
    null progress and plan detail.
  • (review update) sys.list_rebalance keeps its original single-row semantics; a new
    sys.list_rebalances procedure lists summaries (id, status, started_at,
    completed_at) of all known rebalances in a single RPC, mirroring the Admin API
    one-to-one.
  • Docs updated: engine-flink/procedures.md, maintenance/operations/rebalance.md.

Closes #3965.

Notes for reviewers

Where the ZooKeeper read happens. listRebalances needs both in-memory state and
ZooKeeper history. CoordinatorEventProcessor#processListRebalances snapshots the current
rebalance on the coordinator event thread, where it mutates, then reads the history on the
ioExecutor so the event loop never blocks on ZooKeeper.
RebalanceManager#listRebalances(RebalanceInfo) takes that snapshot as a parameter and
touches nothing else, which is what makes it safe off the event thread.

The no-id procedure path issues two RPCs - listRebalanceProgress(null) for the
current entry's detail plus listRebalances() for the summaries, stitched client-side -
so a rebalance finishing between them can render one transiently stale row, corrected on
the next call. The alternative is carrying progress and plan detail for the current entry
inside ListRebalancesResponse; that was left out to keep the new response summary-only.
Happy to switch to the single-snapshot shape if you prefer it.

(resolved in review) The procedure split removed the two-RPC stitch entirely:
sys.list_rebalances is backed by the single listRebalances() RPC, so the transiently
stale row can no longer occur.

Three commits. The first adds the storage groundwork and the listing API, the second
the timestamp exposure and the SQL surface, the third addresses review comments (procedure
split, historical-id drill-down, corrupt-entry cleanup). Each compiles and passes its
tests on its own, so the PR is fine to rebase-merge or squash.

Test Plan

  • Unit: RebalanceTaskJsonSerdeTest (serde v2 round-trip, v1 document reads back as -1),
    ZooKeeperClientTest (history retention, idempotent re-register, corrupt-entry
    skip deletion during the trim, sibling-znode isolation), RebalanceManagerTest
    (timestamp stamping, failover restore with and without timestamps, no re-completion of
    an already-final restored task, newest-first ordering, empty history, historical-id
    drill-down and unknown-id error), CoordinatorEventProcessorTest (the response
    callback completes when the rebalance manager is closed), ClientRpcMessageUtilsTest
    (-1 <-> absent mapping), RebalanceProgressJsonSerializerTest.
  • Integration: RebalanceITCase (end-to-end listRebalances and progress timestamps on a
    live cluster), FlussAuthorizationITCase#testListRebalances (DESCRIBE on
    Resource.cluster(), deny and allow), FlinkProcedureITCase (both procedures,
    timestamp columns pinned to Instant with ordering).

🤖 AI-assisted changes - reviewed by human developer

@morazow
morazow force-pushed the rebalance-progress branch from 5b2283c to 64191c5 Compare August 18, 2026 13:49
@morazow morazow changed the title [server] Retain bounded rebalance history and add listRebalances API [server] Add listRebalances API and expose rebalance timestamps Aug 18, 2026
@morazow
morazow force-pushed the rebalance-progress branch 5 times, most recently from 8569c10 to 014a5ac Compare August 19, 2026 08:44
@morazow

morazow commented Aug 19, 2026

Copy link
Copy Markdown
Contributor Author

Scope note for reviewers: per-bucket detail for historical rebalances

This PR keeps listRebalances() deliberately summary-only (id, status, timestamps). The per-bucket plan of a retained historical rebalance is already persisted, each history znode stores the full RebalanceTask, plan included.

But listRebalanceProgress(id) still throws NoRebalanceInProgressException for non-current ids, so that plan is not yet reachable through the API.

It is easier to enable this API: listRebalanceProgress(id) fall back to the ZooKeeper history when the id is not current, and build the RebalanceProgress from the stored plan. No proto or serde change is needed; the existing response message already carries per-bucket plans.

Keeping it out of this PR keeps the review surface small and leaves one design point open for discussion:

  • History stores plans plus the overall final status, not per-bucket outcomes (those live only in coordinator memory). A historical RebalanceProgress would stamp every bucket with the overall status — exact for COMPLETED, approximate for FAILED/CANCELED. If per-bucket outcomes matter, the follow-up would instead need a serde bump to store RebalanceResultForBucket per bucket. Opinions welcome.

The follow-up also changes one contract: NoRebalanceInProgressException would no longer be thrown for ids still in Zookeeper, which I read as the intent of #3965.

@morazow
morazow marked this pull request as ready for review August 19, 2026 09:30
@morazow
morazow force-pushed the rebalance-progress branch from 014a5ac to 8757b6a Compare August 19, 2026 22:02

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

Thanks, this is a well-structured change — the bottom-up layering is clean, the failover restore paths and the final-status-before-history ordering are handled carefully, and the test coverage matches the claims in the description. I went through it layer by layer and found no blocking issues.

One design suggestion (splitting the SQL procedure instead of overloading it) and a handful of smaller points inline. The procedure split is the only one I'd push on before merge, since it also removes the two-RPC staleness window flagged in the description.

Comment thread website/docs/engine-flink/procedures.md Resolved
@morazow

morazow commented Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

addressed: procedure split applied (sys.list_rebalances added, staleness window gone) and all inline comments addressed in the linked commit

2901d9a

@morazow
morazow requested a review from affo August 25, 2026 10:31

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

Thanks for addressing! Now looks good to me!

@morazow
morazow force-pushed the rebalance-progress branch 2 times, most recently from 3bb4ad6 to 0a14c88 Compare September 14, 2026 11:26
@morazow
morazow force-pushed the rebalance-progress branch 9 times, most recently from c453793 to 1403167 Compare November 6, 2026 21:52
Comment thread fluss-rpc/src/main/proto/FlussApi.proto Outdated
}

message ListRebalancesResponse {
repeated PbRebalanceInfo rebalance_infos = 1;

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.

I have a few related questions about the API and history storage design:

  1. Could we extend the existing RPC? For example, add an optional include_history field to ListRebalanceProgressRequest and a repeated PbRebalanceInfo field to its response, while retaining the existing fields and the current single-rebalance behavior when the flag is absent. A separate Admin method and SQL procedure could still use the same RPC underneath. This seems worth considering before introducing another RPC. We would also need to define the interaction with rebalance_id and handle older servers explicitly, so an unsupported history request cannot silently return an incomplete result.

  2. What should PbRebalanceInfo represent? Should it be a summary, or also contain table/bucket details through repeated PbRebalanceProgressForTable (which includes the plan)? I would avoid returning every historical plan by default. If this is intended to summarize outcomes, could we include an optional, concise error_message or failure_reason? A status such as FAILED alone does not explain what went wrong. The reason would need to be captured and persisted when the failure occurs to remain useful after coordinator failover. Full plan details could remain available through the by-ID query, depending on the retention decision below.

  3. Can we align the history storage with [server] Store rebalance tasks in remote storage #5133? That PR moves rebalance tasks into remote storage to address large serialized plans exceeding ZooKeeper's znode size limit. Here, each /cluster/rebalance_history/<rebalanceId> still stores a complete RebalanceTask. Keeping only 10 entries bounds the count, but does not bound an individual entry's size; even the summary listing currently reads and decodes those complete tasks. Do we need to retain the full plan for historical rebalances, or would summary metadata and diagnostic information be sufficient? If historical plan lookup is required, could history retain metadata plus a remote reference, aligned with [server] Store rebalance tasks in remote storage #5133, with cleanup preserving files referenced by retained history? If we choose summary-only retention instead, we should explicitly adjust the historical detail-query contract.

  4. Could we name the new SQL procedure sys.list_all_rebalances()? Its multi-row summary schema and use case differ substantially from sys.list_rebalance([id]), which returns one rebalance's detailed progress and plan. The more explicit name would make that distinction clearer than the singular/plural difference alone. The documentation should clarify that "all" means the current rebalance plus retained history, rather than an unbounded audit history.

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.

Thanks @wuchong, I am going to follow-up on your suggestions and findings and address them soon🤞

@wuchong

wuchong commented Nov 17, 2026

Copy link
Copy Markdown
Member

Btw, is it really necessary to display the results of the last 10 rebalances? Based on our operational experience, showing only the most recent result should be sufficient. What do you think @swuferhong ?

Rebalance tasks now carry started/completed timestamps (RebalanceTask
JSON serde v2, backward compatible with v1 znodes). On completion or
cancellation the coordinator writes the final task to a bounded
ZooKeeper history at /cluster/rebalance_history/<rebalanceId>, keeping
the last 10 entries. The trim orders entries by creation and does not
decode them, so an undecodable entry ages out like any other. A failed
trim is logged and retried on the next write.

The history entry and the current task are written as two separate
requests, history first, so each request carries the plan once. Both
writes, and the write that starts a rebalance, fail if the coordinator
epoch changed, so a coordinator that lost leadership cannot overwrite
them. On startup the coordinator copies a finished task that only the
current znode holds (written before the history existed) into the
history.

Cancel marks every unfinished bucket CANCELED, including the in-flight
one, which the coordinator stops tracking. Cancel without a current
rebalance no longer touches the znode: it is a no-op without an id and
throws NoRebalanceInProgressException with one.

The new Admin#listRebalances() API returns a summary (id, status,
timestamps) of the current rebalance plus the retained history, newest
first. listRebalanceProgress(id) also serves historical ids with full
per-bucket detail and throws NoRebalanceInProgressException for an id
that is neither current nor retained. RebalanceProgress exposes
startedAtMs and completedAtMs (-1 when unset).

In Flink, sys.list_rebalance gains started_at/completed_at columns and
the new sys.list_rebalances procedure lists all known rebalances.

Closes apache#3965.
Regenerate the vendored proto, add the ListRebalances API key and
request, and expose Admin#list_rebalances returning RebalanceInfo
(id, status, timestamps). RebalanceProgress now carries started_at_ms
and completed_at_ms from the regenerated response.
@swuferhong

Copy link
Copy Markdown
Contributor

Btw, is it really necessary to display the results of the last 10 rebalances? Based on our operational experience, showing only the most recent result should be sufficient. What do you think @swuferhong ?

I haven’t encountered this requirement either. I’d recommend using the existing List Rebalance Process API to implement this rather than introducing a new API.

Admin#listRebalances() is renamed to Admin#listAllRebalances() and no
longer has its own RPC. It sends ListRebalanceProgressRequest with the
new include_history field, and the response carries the summaries in
rebalance_infos. The LIST_REBALANCES API key is removed and
LIST_REBALANCE_PROGRESS moves to version 1. An older server would
ignore include_history and return only the current rebalance, so the
client rejects the request with UnsupportedVersionException when the
server negotiated version 0. The coordinator rejects include_history
combined with rebalance_id.

A rebalance summary carries an optional failure_reason: why the first
failed bucket was given up on. The coordinator stores it with the
final task, so it is available after a coordinator failover.

A history entry at /cluster/rebalance_history/<rebalanceId> now stores
the summary (id, status, timestamps, failure reason) instead of the
complete task, so its size does not grow with the plan. The per-bucket
plan and statuses are kept for the most recent rebalance only, in the
current task znode. listRebalanceProgress(id) throws
NoRebalanceInProgressException for any other id.

In Flink, the summary procedure is renamed to sys.list_all_rebalances
and gains a failure_reason column.
Regenerate the vendored proto and remove the ListRebalances API key
and request. Admin#list_rebalances is renamed to
Admin#list_all_rebalances. It sends ListRebalanceProgressRequest with
include_history and fails with UnsupportedVersion when the server
negotiated version 0. RebalanceInfo carries the optional
failure_reason.
@morazow

morazow commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Hello @wuchong, @swuferhong

Thanks for your detailed and on point reviews, your suggestions made the design simpler. I pushed two follow-up commits.

Single RPC

I removed ListRebalances and added include_history to ListRebalanceProgressRequest. The response carries the list in a new rebalance_infos field. LIST_REBALANCE_PROGRESS goes to version 1.

include_history cannot be combined with rebalance_id. The coordinator rejects that with InvalidRequestException.

PbRebalanceInfo content

It is a summary of one rebalance: id, status, start time, completion time. It does not carry the plan or per-bucket progress. Per-bucket detail stays in ListRebalanceProgress without include_history, and only for the most recent rebalance.

I added the optional failure_reason as you suggested.

History storage

History entries now store only the summary (id, status, timestamps, failure reason) under /cluster/rebalance_history/<rebalanceId>, not the full RebalanceTask. The entry size no longer grows with the plan, so this does not conflict with #5133 moving the plan to remote storage. This PR does not depend on #5133 yet.

Naming

The procedure is renamed to sys.list_all_rebalances(). The docs now state that "all" means the current rebalance plus the retained history.

Please have another look 🙏

@morazow
morazow requested a review from wuchong November 19, 2026 14:48
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.

Add completion timestamp and historical listing to RebalanceProgress

4 participants