Skip to content

[server] Restore KV minRetainOffset on follower restart - #4060

Merged
wuchong merged 2 commits into
apache:mainfrom
gyang94:codex/issue-4054-startup-restore
Sep 3, 2026
Merged

wuchong merged 2 commits into
apache:mainfrom
gyang94:codex/issue-4054-startup-restore

Conversation

@gyang94

@gyang94 gyang94 commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Restore follower minRetainOffset from the latest committed KV snapshot when KV followers are handled in makeFollowers.
  • Coalesce pending restores into one asynchronous task per TabletServer, keeping ZooKeeper access outside the replica state-change lock.
  • Bound ZooKeeper pressure with 32-bucket client batches, the configured max-inflight limit, startup jitter, and exponential retry backoff.
  • Distinguish confirmed-empty snapshots from unresolved reads, and ignore stale results after notifications, role changes, replica replacement, or shutdown.
  • Fixes [server] Restore KV minRetainOffset on follower restart #4054

Test Plan

  • ZooKeeperClientTest: 23 tests passed.
  • ReplicaManagerTest: 41 tests passed.
  • mvn -o -pl fluss-server -DskipTests -Dfast verify passed (Checkstyle 0 violations and Spotless passed).

🤖 AI-assisted changes - reviewed by human developer

@gyang94
gyang94 marked this pull request as ready for review August 25, 2026 11:57
@gyang94
gyang94 force-pushed the codex/issue-4054-startup-restore branch from 4f05624 to 162c53c Compare August 31, 2026 04:15
// Followers don't initialize a KV tablet, so restore the log retention state from the
// latest committed KV snapshot. The ZooKeeper read is scheduled asynchronously because
// this method runs while holding the replica state-change lock.
restoreFollowerMinRetainOffsets(kvFollowers);

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.

Could we avoid this separate ZooKeeper-backed recovery state machine by piggybacking the committed minRetainOffset on follower FetchLogResponses? The leader advances this value only after a KV snapshot is committed, and follower fetches already retry continuously (including empty long-poll responses), so every successful fetch can idempotently apply updateMinRetainOffset. The existing notification can remain the fast path, while FetchLog becomes the anti-entropy and restart-recovery path.

This follows Kafka's pattern of propagating a logical cleanup boundary by logStartOffset in FetchResponse. Fluss should use a separate optional min_retain_offset field rather than overload log_start_offset: the former is snapshot-derived deletion permission. This would remove the complex logic in this PR: pending map, ZooKeeper batch reads, scheduler/backoff, and replica-identity fencing while preserving eventual local cleanup progress.

@gyang94
gyang94 force-pushed the codex/issue-4054-startup-restore branch 2 times, most recently from 3ec3085 to b7e06be Compare September 1, 2026 02:15

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

@gyang94 Please also add an IT case for the follower replica cleanup local logs after restart even when there is no new data written to the table.

optional int64 filtered_end_offset = 9;
// The safe local log retention boundary confirmed by a committed KV snapshot. This is only
// returned for KV follower fetches and is distinct from the physical log_start_offset.
optional int64 min_retain_offset = 10;

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 canonical RPC schema change is not mirrored to fluss-rust/crates/fluss/proto/FlussApi.proto or regenerated in fluss-rust/crates/fluss/src/proto/fluss.rs. This makes proto-up-to-date fail and leaves the Rust bindings/build inconsistent with the Java RPC schema. Please regenerate the Rust protobuf bindings and update the affected PbFetchLogRespForBucket struct literals in this PR.

} else {
result = new FetchLogResultForBucket(tb, ApiError.fromThrowable(e));
}
result = maybeAddMinRetainOffset(fetchParams, replica, result);

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.

This is error path, we don't need to pass minRetainOffset when is error response or empty response.

new FetchLogResultForBucket(
tb, fetchedData.getRecords(), readInfo.getHighWatermark());
}
fetchLogResult = maybeAddMinRetainOffset(fetchParams, replica, fetchLogResult);

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.

Could we propagate minRetainOffset through the same read-metadata path as highWatermark instead of attaching it afterward? Adding it to LogReadInfo, capturing it in Replica.readRecords, and passing both values into a FetchLogResultForBucket success factory or constructor would make each result complete at construction time and avoid dropping the field when a new success path or adapter rebuilds the result. The remote/lake out-of-range success paths should use the same factory because they bypass LogReadInfo; if the two values must form a consistent snapshot, they should also be read under the same synchronization boundary.

Comment on lines +627 to +631
if (serverId == leaderServerId) {
leaderKvManager = kvManager;
} else {
followerKvManager = kvManager;
}

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.

Why do we need to maintain separate followerKvManager and leaderKvManager? It seems they refer the same kvManager object and there is no code using followerKvManager and leaderKvManager.

@wuchong
wuchong force-pushed the codex/issue-4054-startup-restore branch from f4a027f to c4dec0f Compare September 3, 2026 04:30
@wuchong

wuchong commented Sep 3, 2026

Copy link
Copy Markdown
Member

@gyang94 I appended a commit to refine the code, please take a look.

@wuchong
wuchong force-pushed the codex/issue-4054-startup-restore branch from c4dec0f to 467d19a Compare September 3, 2026 05:58
Propagate minRetainOffset through the regular fetch result construction path
and update followers even when successful fetches contain no records.

Replace FetchLogResultForBucket constructor overloads with named factories,
document the monotonic concurrent update, and stabilize the restart test with
a manually controlled clock.

Co-Authored-By: Codex <noreply@openai.com>
AI-Model: gpt-5.6-sol
AI-Contributed/Feature: 5/5
AI-Contributed/UT: 0/0
@wuchong
wuchong force-pushed the codex/issue-4054-startup-restore branch from 467d19a to 6708cf4 Compare September 3, 2026 07:09
@gyang94

gyang94 commented Sep 3, 2026

Copy link
Copy Markdown
Contributor Author

@wuchong: Thanks for refining the code. LGTM.

@wuchong
wuchong merged commit 95b3ff1 into apache:main Sep 3, 2026
30 checks passed
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] Restore KV minRetainOffset on follower restart

2 participants