Skip to content

[kv] Avoid unnecessary RocksDB flushes during Tablet Server shutdown - #3807

Merged
luoyuxia merged 1 commit into
apache:mainfrom
platinumhamburg:feature/kv-close-mode
Aug 5, 2026
Merged

luoyuxia merged 1 commit into
apache:mainfrom
platinumhamburg:feature/kv-close-mode

Conversation

@platinumhamburg

Copy link
Copy Markdown
Contributor

Purpose

Linked issue: close #3806 3806

Brief change log

Tests

API and Format

Documentation

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.

Pull request overview

This PR introduces a KV close mode to distinguish between “preserve local state” closes and shutdown/drop closes where local unflushed RocksDB state is not relied upon for recovery. It wires Tablet Server shutdown and KV drop paths to use a discard mode that enables RocksDB’s avoid_flush_during_shutdown option immediately before closing, reducing shutdown-time disk I/O.

Changes:

  • Add KvCloseMode and thread it through TabletServer → KvManager → KvTablet → RocksDBKv close/shutdown paths.
  • Use DISCARD_UNPERSISTED_STATE for Tablet Server shutdown and tablet drop, while keeping default no-arg close/shutdown behavior preserving local state.
  • Extend unit tests to validate close-mode propagation and the expected persistence behavior in preserve vs discard modes.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
fluss-server/src/main/java/org/apache/fluss/server/tablet/TabletServer.java Uses discard close mode when shutting down KV manager during Tablet Server shutdown.
fluss-server/src/main/java/org/apache/fluss/server/kv/KvManager.java Adds shutdown(KvCloseMode) and closes tablets with the provided mode.
fluss-server/src/main/java/org/apache/fluss/server/kv/KvTablet.java Adds close(KvCloseMode) and uses discard mode for drop().
fluss-server/src/main/java/org/apache/fluss/server/kv/rocksdb/RocksDBKv.java Adds close(KvCloseMode) and sets avoid_flush_during_shutdown in discard mode before closing.
fluss-server/src/main/java/org/apache/fluss/server/kv/KvCloseMode.java New enum defining preserve vs discard close behaviors.
fluss-server/src/test/java/org/apache/fluss/server/tablet/TabletServerShutdownTest.java Verifies shutdown uses DISCARD_UNPERSISTED_STATE.
fluss-server/src/test/java/org/apache/fluss/server/kv/KvManagerTest.java Adds coverage for discard shutdown/drop not persisting unflushed state and null close-mode validation.
fluss-server/src/test/java/org/apache/fluss/server/kv/rocksdb/RocksDBKvTest.java Adds tests for preserve vs discard close semantics and resource closing on option-set failure.
fluss-server/src/test/java/org/apache/fluss/server/kv/rocksdb/RocksDBResourceContainerTest.java Asserts default DB options keep avoidFlushDuringShutdown disabled globally.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +1294 to +1299
public void close(KvCloseMode closeMode) throws Exception {
LOG.info(
"Close kv tablet {} for table {} with mode {}.",
tableBucket,
physicalPath,
closeMode);

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

@platinumhamburg Thanks for the comment. PTAL

() -> {
// first close the kv.
close();
close(KvCloseMode.DISCARD_UNPERSISTED_STATE);

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.

Could we still delete the KV directory when setting avoid_flush_during_shutdown fails?

RocksDBKv.close(DISCARD_UNPERSISTED_STATE) closes the DB, column-family handles, and options in its finally, but then rethrows the captured RocksDBException. That exception exits this lambda before FileUtils.deleteDirectory(...) runs. KvManager.dropKv() has already removed the tablet from currentKvs, so a retry through the manager cannot clean up the untracked directory.

Please either treat failure to apply this shutdown optimization as a warning once the fallback close has completed, or ensure the directory is deleted after confirming the native DB is closed while preserving the original exception. A regression test covering KvTablet.drop() with an injected option-setting failure would protect this path.

package org.apache.fluss.server.kv;

/** Defines whether closing a KV tablet must persist its current local RocksDB state. */
public enum KvCloseMode {

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.

Do we really need two close modes here?

From the current production call sites, both TabletServer shutdown and tablet drop can discard unflushed memtable state: recovery rebuilds the KV from snapshots/changelog, or the local directory is deleted. PRESERVE_LOCAL_STATE seems to exist only to retain the behavior of directly reopening the same local RocksDB directory after KvManager.shutdown().

Is that direct local-reopen behavior still a contract we need? If not, could we simplify this change by always setting avoid_flush_during_shutdown=true before closing RocksDB and remove KvCloseMode and its propagation? Since RocksDB WAL is disabled, this would intentionally make close terminal: callers must recover through Fluss instead of relying on unflushed local RocksDB state.

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.

Yes, we do need the two close modes here. In the future, we plan to support dynamically altering options for primary key tables, which requires KvTablet to support dynamic reload — and in that mode, we expect to retain the flush capability.

@platinumhamburg

Copy link
Copy Markdown
Contributor Author

@luoyuxia Thank you for your comments; I have addressed them. Please take a look when you have time.

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

+1

@luoyuxia
luoyuxia merged commit 4f89e3e into apache:main Aug 5, 2026
19 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.

[kv] Avoid unnecessary RocksDB flushes during Tablet Server shutdown

3 participants