Skip to content

feat: add async C bindings for TiKV RawKV - #572

Draft
lance6716 wants to merge 2 commits into
tikv:masterfrom
lance6716:lance6716/c-ffi-bindings
Draft

lance6716 wants to merge 2 commits into
tikv:masterfrom
lance6716:lance6716/c-ffi-bindings

Conversation

@lance6716

Copy link
Copy Markdown
Contributor

Summary

  • Add an asynchronous C ABI for TiKV RawKV operations, with callback-based completion and explicit request ownership, cancellation, and shutdown APIs.
  • Add a C API for process-wide metrics snapshots and an explicit gRPC response decoding limit constructor.
  • Add C headers, a C example, and C-only documentation.

This extracts the C layer from draft #571. The C++ wrapper remains in that draft for follow-up work.

Validation

  • cargo test -p tikv-client-rust-c --lib (12 passed)
  • cargo fmt --all -- --check
  • clang -std=c11 -Wall -Wextra -Werror -fsyntax-only -Ibindings/c/include bindings/c/examples/raw_async_demo.c

The TiKV-backed example was not run in this split. This PR remains a draft while the inline review findings on request lifetime, cancellation semantics, and metrics are addressed.

Signed-off-by: lance6716 <lance6716@gmail.com>
@ti-chi-bot ti-chi-bot Bot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. dco-signoff: yes Indicates the PR's author has signed the dco. labels Oct 14, 2026
@ti-chi-bot

ti-chi-bot Bot commented Oct 14, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign coocood for approval. For more information see the Code Review Process.
Please ensure that each of them provides their approval before proceeding.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@coderabbitai

coderabbitai Bot commented Oct 14, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@ti-chi-bot ti-chi-bot Bot added the size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. label Oct 14, 2026

@lance6716 lance6716 left a comment

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.

C-only review notes for this draft. The first two comments are request-lifetime safety issues; the others cover cancellation, observability, tests, and maintainability.

Signed-off-by: lance6716 <lance6716@gmail.com>
Comment thread bindings/c/src/request.rs
.cancel_inflight
.load(Ordering::Acquire)
{
let request = unsafe { &*request_ptr };

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.

This post-spawn dereference can race with a fast completion. The C contract allows the callback to destroy the request; after it returns, finalize_request can release the last references before the submit thread reaches this block (when cancel_inflight is true). Please set the cancellation state before spawning, or retain an independent submit-side reference until all post-spawn access is finished.

Comment thread bindings/c/src/client.rs
.requests
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
.clone();

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.

Cloning this vector copies bare request addresses without retaining their states. A request can finish, be removed, and be freed before the loop dereferences the copied address, causing a use-after-free. tikv_async_client_destroy has the same pattern. Please keep each request alive while marking cancellation, or avoid traversing these pointers.

Comment thread bindings/c/src/request.rs
|| keepalive.lifecycle.cancel_inflight.load(Ordering::Acquire)
};

let (status, output, error_message) = match result {

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.

Cancellation is only checked after the underlying future finishes, so a write may already have taken effect when the callback reports CANCELLED. The Ok(Err(error)) arm also reports ERROR despite a cancellation request, contrary to the stronger guarantee in the C header. Please settle and document the terminal-status rule, including partial batch effects, and test both success and error after cancellation.

Comment thread bindings/c/src/tests.rs
})) as *mut tikv_async_request_t
}

#[test]

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.

The current 12 unit tests do not exercise an accepted request's concurrent lifecycle. Please add deterministic tests for destruction inside the callback, shutdown racing with completion, exactly-once callback delivery, and cancellation outcomes. A C compile-and-link smoke check in CI would also protect the public ABI.

Comment thread bindings/c/src/metrics.rs
fn collect_metrics_snapshot() -> MetricsSnapshot {
let mut samples = Vec::new();

for family in prometheus::gather() {

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.

This gathers the whole process-wide registry, but the C sample retains only the type label and the match below drops gauges, summaries, and other metric types. Two series differing in another label become indistinguishable. Please either narrow the public contract to known client-rust counter/histogram families or preserve the full label/type information; the C header should state whichever scope is chosen.

Comment thread bindings/c/src/request.rs
let mut completion = lock_completion(request);
completion.complete = true;
completion.callback_running = true;
completion.completion_status = Some(status);

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.

The existing metrics describe underlying TiKV/PD RPCs, but this async binding has no outcome or in-flight metric for accepted/rejected submissions, cancellation, panics, or callback completion. An RPC may be counted as successful even when the FFI reports CANCELLED. Please consider a small set of binding-level counters and an in-flight gauge so operators can reconcile the two layers.

Comment thread bindings/c/src/tests.rs

assert_eq!(raw_get_total, Some(3.0));
assert_eq!(raw_get_duration_count, Some(1.0));
assert!(saw_inf_bucket);

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.

This test checks that an infinite histogram bucket exists, but not its cumulative value, finite bucket values, or the histogram sum. Asserting these with a known observation, plus the intended label behavior, would catch incorrect monitoring output rather than only missing samples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dco-signoff: yes Indicates the PR's author has signed the dco. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant