Skip to content

[fix] Clear tag-only and heterogeneous KV entries - #243

Merged
ji-huazhong merged 3 commits into
Ascend:mainfrom
ji-huazhong:fix/kv-clear-entries
Nov 30, 2026
Merged

ji-huazhong merged 3 commits into
Ascend:mainfrom
ji-huazhong:fix/kv-clear-entries

Conversation

@ji-huazhong

@ji-huazhong ji-huazhong commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

Clearing tag-only keys can raise an error on KV backends. Cleanup can also leave payloads behind when it uses read metadata: heterogeneous rows have no common fields, and metadata containing only selected fields does not describe everything stored in a row.

Have the Python and Rust controllers return complete deletion metadata from MARK_CLEARING, grouped by partition and including stored fields that are not ready yet. The client deletes payloads using this metadata before releasing controller metadata and indexes. KV storage calls are skipped only when the controller confirms that no payload fields exist; the storage manager retains its guard against ambiguous empty-field metadata.

Apply the same field selection to unlinked public-data cleanup. Whole-partition cleanup gets its metadata directly from MARK_CLEARING, eliminating one RPC and the separate partition-metadata fetch. Normal reads, field-only deletion, and public-reference protection retain their existing behavior.

Extend the existing tests for tag-only keys, heterogeneous rows, direct sample cleanup with partial fields across partitions, unfinalized fields, and public-data cleanup. Verify each stored field is removed while neighboring data remains readable. Require Yuanrong's Key not found error when reading a deleted field; other read errors still fail the assertion.

Validation:

  • All 16 GitHub CI checks passed, including unit tests and Python- and Rust-controller E2E suites with SimpleStorage, MooncakeStore, and Yuanrong.
  • 98 focused tests passed, including client, controller, checkpoint, and KV manager coverage.
  • 42 Rust-controller cleanup and public-reference integration tests passed; 18 Rust unit tests passed.
  • Real Mooncake TCP: all 24 cleanup tests passed. On the previous PR head, six synchronous/asynchronous cases fail because payloads remain after cleanup.
  • python -m compileall -q transfer_queue tutorial tests and all pre-commit checks passed.

The local full suite finished with 992 passed and 16 skipped, plus one previously reproduced worker-exit test failure and six Yuanrong fixture errors caused by the missing optional dependency. The corresponding CI unit tests and real Yuanrong integration passed.

This PR is based directly on main and is independent of #242.

Signed-off-by: ji-huazhong <hzji210@gmail.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-29T04:59:26.354957Z 5e2b886 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5e2b886fac

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread transfer_queue/storage/managers/base.py Outdated
Comment on lines +876 to +877
if not metadata.field_names:
raise RuntimeError(
"Fail to clear_data for key-value based backends due to lack of `field_names` in BatchMeta"
)
return

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Do not silently clear metadata for empty field sets

When clear_samples is given normal KV metadata for heterogeneous rows, kv_retrieve_meta can legitimately return field_names == [] even though each row has a payload. This return skips Mooncake/Yuanrong deletion, but async_clear_samples still proceeds to _clear_meta_in_controller, releasing the indexes and making the payloads invisible rather than deleted; before this change the exception stopped metadata removal. Limit the no-op to a clear known to contain only tag-only rows, or preserve the error for ambiguous metadata.

AGENTS.md reference: AGENTS.md:L17-L18

Useful? React with 👍 / 👎.

Comment thread transfer_queue/client.py Outdated
create: bool = False,
socket: zmq.asyncio.Socket | None = None,
*,
for_clear: bool = False,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

do we have to introduce this new param?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Regarding my question on for_clear: bool = False: I'd prefer not to introduce this flag on kv_retrieve_meta for a few maintainability reasons:

Overloaded API contract: kv_retrieve_meta normally returns a read-safe BatchMeta (fields ready across all rows, usable with get_data). With for_clear=True, it returns a deletion-only BatchMeta (union of present/unready fields, sparse _custom_backend_meta, None entries in per_sample_shapes) that isn't safe to pass to reads.
Flag interaction & signature drift: create=True, for_clear=True is an invalid combination (currently unguarded), TransferQueueClient.kv_retrieve_meta doesn't expose for_clear (breaking sync/async parity), and threading a 4th positional bool through Rust ControllerState::kv_retrieve_meta turns 17 call sites into (..., false, false).
Suggested alternative: return authoritative deletion metadata from MARK_CLEARING
Since async_clear_samples already calls MARK_CLEARING right before storage_manager.clear_data, we can have the controller return the authoritative deletion metadata directly from MARK_CLEARING:

In both Python and Rust controllers, MARK_CLEARING already groups (partition_ids, global_indexes) by partition and validates PUBLIC_PARTITION link protection. While zeroing readiness, it can snapshot per-partition BatchMetas with the union of present fields (field_metadata / field.present) and _custom_backend_meta.
async_clear_samples then passes those controller-returned BatchMetas to storage_manager.clear_data (skipping KV storage deletion when a group has no present fields), rather than trusting the caller-supplied metadata.field_names.
This gives us a few clean wins:

No changes to kv_retrieve_meta (no for_clear param, no new RPC type, no Rust call-site churn).
Fixes direct client.clear_samples(meta) as well, which also addresses the Codex review finding: calling client.clear_samples(client.kv_retrieve_meta(["tokens", "score"], pid)) (empty field_names) or client.clear_samples(meta) with a partial field subset will delete all stored fields on KV backends (matching SimpleStorage) instead of silently leaking payloads or raising.
KVStorageManager.clear_data can keep its guard against ambiguous empty field_names (or only be skipped when the controller confirms no fields are present).
Two small implementation notes if we go this route:

Because BatchMeta passed to clear_samples can span multiple partitions, MARK_CLEARING would return a list of per-partition BatchMetas (a small addition to Rust wire.rs Body).
async_clear_partition currently calls GET_PARTITION_META right before MARK_CLEARING; to avoid serializing a large partition's metadata twice, we can either avoid returning metadata on that path or let clear_partition reuse it directly.
Minor notes on tests / consistency
test_kv_clear_rows_without_common_fields on Yuanrong: pytest.raises((KeyError, RuntimeError)) around client.get_data(metadata) works for SimpleStorage and MooncakeStore, but on Yuanrong's CPU path (GeneralKVClientAdapter.mget_zero_copy), reading deleted keys returns [None] instead of raising (see tests/test_yuanrong_storage_client_e2e.py), which KVStorageManager packs into NonTensorStack(None). It may be worth adjusting that assertion for Yuanrong compatibility.
Minor consistency: mark_unlinked_public_clearing (Python + Rust) still uses fields_ready_in_all (which can matter if kv_clear_unlinked_public(stale_keys=...) is called on a KV backend with heterogeneous/tag-only keys), and it'd be nice to mirror the Rust unfinalized-field clear-metadata test on the Python controller as well.

Signed-off-by: ji-huazhong <hzji210@gmail.com>
Signed-off-by: ji-huazhong <hzji210@gmail.com>
@ji-huazhong
ji-huazhong merged commit 5f9fb8b into Ascend:main Nov 30, 2026
16 checks passed
@ji-huazhong
ji-huazhong deleted the fix/kv-clear-entries branch November 30, 2026 02:34
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.

2 participants