Skip to content

Add C bindings for native CDC - #13674

Merged
tclinkenbeard-oai merged 17 commits into
apple:mainfrom
tclinkenbeard-oai:dev/tclinkenbeard/native-cdc-c-bindings
Jul 20, 2026
Merged

tclinkenbeard-oai merged 17 commits into
apple:mainfrom
tclinkenbeard-oai:dev/tclinkenbeard/native-cdc-c-bindings

Conversation

@tclinkenbeard-oai

Copy link
Copy Markdown
Collaborator

This PR is a follow-up to #13287. It adds the following types to the C bindings:

  • FDBCdcMutationType
  • FDBCdcStreamInfo
  • FDBCdcMutation
  • FDBCdcVersionedMutations
  • FDBCdcConsumer

The following functions are also exposed:

  • fdb_database_register_cdc_stream
  • fdb_database_remove_cdc_stream
  • fdb_database_list_cdc_streams
  • fdb_database_create_cdc_consumer
  • fdb_database_resume_cdc_consumer
  • fdb_cdc_consumer_destroy
  • fdb_cdc_consumer_consume
  • fdb_cdc_consumer_acknowledge
  • fdb_cdc_consumer_get_position
  • fdb_future_get_cdc_versioned_mutations
  • fdb_future_get_cdc_stream_info_array
  • fdb_future_get_cdc_consumer

These functions and types are documented in api-c.rst.

A new end-to-end test using the bindings is added to unit_test.cpp.

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

This comment has been minimized.

@gxglass

gxglass commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

PR 13674 — Add C bindings for native CDC · High · LGTM-with-nits

Review: apple/foundationdb #13674 — Add C bindings for native CDC

Rigor: xhigh. Reviewed the full 12-commit squash diff (/tmp/pr-review/fdb-13674.diff, 3003 lines) against live main at /Users/gglass/src/fdb4/foundationdb. The live tree contains none of the CDC-binding code yet (base = unmerged main); every hunk applies cleanly against current source. Note: PRs #13733 / #13740 also touch Native CDC and may interact at merge time (esp. the fdbclient/NativeCdc.* and CDCProxyInterface.h surfaces this PR shares).

Intent

Exposes the existing native CDC C++ client API (fdbclient/NativeCdc.h) through the public C binding: fdb_database_{register,remove,list}_cdc_stream[s], fdb_database_{create,resume}_cdc_consumer, and the fdb_cdc_consumer_{consume,acknowledge,get_position,destroy} handle operations, plus the three fdb_future_get_cdc_* result extractors. Wires them through IDatabase / ThreadSafeDatabase / MultiVersionDatabase / DLDatabase, gated on a new API version 800 feature NativeCdcApi, with docs and both a direct and external-client end-to-end unit test.

Correctness

Achieves the intent; the C-ABI discipline is careful and consistent with the existing binding.

  • Opaque handle: FDBCdcConsumer is a fresh incomplete struct FDB_cdc_consumer typedef in fdb_c_types.h, mapped to INativeCdcConsumer* via NATIVE_CDC_CONSUMER. Consistent with FDBDatabase/FDBTransaction.
  • Ownership of the consumer handle: fdb_future_get_cdc_consumer does consumer.extractPtr() (transfers one ref out of the future to the caller), and fdb_cdc_consumer_destroy does ->delref(). The future itself retains its own ref until destroyed, so the handle correctly outlives the originating future — matching the documented contract. Extracting twice would leak; that is the standard caller contract for _get_* handle extractors here, and is documented.
  • Result-array memory: CNativeCdcStreamInfoArray and CNativeCdcConsumeResult each embed an Arena alongside the VectorRef. mapThreadFuture stores the mapped struct as the future's result value, so the arena (and every StringRef/FDBCdcMutation[] copied into it, incl. the placement-new mutation arrays) stays alive until fdb_future_destroy / fdb_future_release_memory. MapSingleAssignmentVar::cleanupUnsafe releases the source future's memory. This is the same lifetime model as fdb_future_get_keyrange_array. The end-to-end test explicitly asserts future_released (1102) after fdb_future_release_memory, validating the contract. Correct.
  • Error-code mapping: getters use CATCH_AND_RETURN; future-returning entry points use RETURN_FUTURE_ON_ERROR, which maps thrown Error to an already-failed future (identical to neighbors). unsupported_operation is returned when the loaded external client lacks the symbol.
  • API-version gating: new symbols load only when headerVersion >= ApiVersion::withNativeCdcApi() (= 800), added to ApiVersion.h.cmake + ApiVersions.cmake. FDB_AV_LATEST_VERSION/LATEST_BINDINGS_VERSION are already 800, so the feature is reachable. fdb_future_get_uint64 (used to extract the register-stream result) is a pre-existing exported symbol. Docs (commit 6) state the API requires version 800+.
  • Symbol export: bindings/c/fdb_c.map exports fdb_* via wildcard, so all new fdb_cdc_* / fdb_database_*_cdc_* / fdb_future_get_cdc_* symbols are exported without editing the version script. Not an omission.
  • Thread-safety: ThreadSafeNativeCdcConsumer keeps a raw NativeCdcConsumer* off-thread, runs consume/acknowledge via onMainThread, defers the final delref() to the network thread in its destructor (onMainThreadVoid), and serializes cursor reads/writes through a ThreadSpinLock. This matches ThreadSafeDatabase/ThreadSafeTransaction. getPosition() reads the cached cursor under the lock, so the synchronous fdb_cdc_consumer_get_position is safe from any thread.
  • Commit 12 fix is sound: in the external-client (DLNativeCdcConsumer) consume path, the wire reply carries lastConsumedVersion but not streamId; the closure now preserves the wire's lastConsumedVersion and only borrows streamId from getPosition(), instead of overwriting the whole cursor with getPosition(). Correct given the one-outstanding-operation contract.
  • MutationRef::type is enum Type : uint8_t (< 64 values), so the uint8_t type field in FDBCdcMutation/NativeCdcMutation cannot truncate. Values pass through raw; the FDBCdcMutationType enum is purely informational and docs tell callers to handle unrecognized values.
  • Header self-containment: new NativeCdcClient.h includes FDBTypes.h (defines CDCStreamId) and, after commit 9, flow/ThreadHelper.h (the post-rename header, which exists). IClientApi.h includes it. NativeCdc.h now includes NativeCdcClient.h, keeps CDCProxyInterface.h (for CDCConsumeReply/CDCCursor), and drops the now-redundant <vector> and inline NativeCdcStreamInfo. Existing consumers (NativeCdc.cpp, NativeCdcEndToEnd.cpp) still see the type.

Bugs

None found. No memory leaks, no double-free, no lifetime hazards, no ABI/packing issues (the FdbCApi mirror structs are inside the existing #pragma pack block and field-match the public header), no error-mapping gaps identified.

Omissions

  • Enum drift guard removed (minor). Commit 3 deletes the 17 static_asserts that pinned each FDB_CDC_MUTATION_TYPE_* to the matching MutationRef::Type::*. The pre-existing FDB_BG_MUTATION_TYPE_* asserts were kept. The passthrough is raw uint8_t so a drift is not a correctness bug, but the compile-time guarantee that the documented enum stays in sync with MutationRef::Type is gone; the informational header could silently go stale. Cheap to keep in the internal .cpp; recommend restoring or leaving a comment pointing at the canonical enum.
  • Internal-naming inconsistency (nit, not user-facing). Public C symbols were renamed native_cdc -> cdc (commits 1-2), but the API-version feature is still NativeCdcApi / FDB_AV_NATIVE_CDC_API, and the FdbCApi members / DL*/MultiVersion* method names remain ...NativeCdc.... Harmless, but the public/internal names now diverge.
  • Test coverage is good: the direct + external-client end-to-end test (register -> list -> create consumer -> consume SET group -> acknowledge -> position -> resume from checkpoint -> consume CLEAR_RANGE -> acknowledge -> remove -> confirm gone) exercises the full C surface, input-buffer independence (overwrites the name/range buffers after the call returns), and the future_released path for both array getters. FDB_KNOB_enable_native_cdc=true is set as a CTest ENVIRONMENT property and is inherited by the spawned fdbserver children (parsed via getEnvironmentKnobOptions()), so server-side CDC-proxy recruitment is actually enabled — the wiring is correct.
  • Not tested at the binding layer: the atomic-op mutation types (only SET_VALUE and CLEAR_RANGE are asserted); consume against a resumed cursor with an invalid/unknown stream_id (docs say validation is deferred to consume/ack — the error path is not exercised); double-consume / concurrent-outstanding rejection. Acceptable for a first cut given the native layer is tested separately, but worth a follow-up.
  • CI flakiness risk (low): consumeThroughVersion long-polls in a loop against a real spawned cluster. If a CDC proxy is slow to recruit or deliver, the loop relies on fdb_future_block_until_ready completing; there is no explicit iteration bound. This is inherent to any CDC end-to-end test and is not a determinism/Joshua concern (client CI test, not a simulation workload). No specific evidence of a failing run.
  • Docs are thorough (api-c.rst types + functions, API-version note, fdb_future_release_memory list updated to include the two CDC array getters, fdb_future_get_uint64 documented, design/cdc.md updated). No gaps found.

Alternatives

None material. The Arena-owning result-struct + mapThreadFuture approach is the idiomatic pattern for array getters in this file. Passing const KeyRef&/const NativeCdcCursor& (commit 8) and copying into Key/KeyRange before crossing to the network thread is the correct fix for the earlier std::move-of-view issue. One optional cleanup: rename the internal NativeCdc* identifiers to match the public cdc naming for consistency.

Verdict

LGTM-with-nits — C-ABI ownership/lifetime, versioning, and thread-safety are correct and well-tested; only non-blocking items are the removed enum static_asserts (restore or comment) and the internal/public native_cdc-vs-cdc naming divergence.

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

Copy link
Copy Markdown
Contributor

Result of foundationdb-pr-clang-ide on Linux RHEL 9

  • Commit ID: 78f7434
  • Duration 0:22:17
  • Result: ✅ SUCCEEDED
  • Error: N/A
  • Build Log terminal output (available for 30 days)
  • Build Workspace zip file of the working directory (available for 30 days)

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

Copy link
Copy Markdown
Contributor

Result of foundationdb-pr-macos-m1 on macOS 14.x

  • Commit ID: 78f7434
  • Duration 0:41:04
  • Result: ✅ SUCCEEDED
  • Error: N/A
  • Build Log terminal output (available for 30 days)
  • Build Workspace zip file of the working directory (available for 30 days)

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

Copy link
Copy Markdown
Contributor

Result of foundationdb-pr-clang-arm on Linux RHEL 9

  • Commit ID: 78f7434
  • Duration 0:46:34
  • Result: ✅ SUCCEEDED
  • Error: N/A
  • Build Log terminal output (available for 30 days)
  • Build Workspace zip file of the working directory (available for 30 days)

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

Copy link
Copy Markdown
Contributor

Result of foundationdb-pr-clang on Linux RHEL 9

  • Commit ID: 78f7434
  • Duration 1:01:46
  • Result: ✅ SUCCEEDED
  • Error: N/A
  • Build Log terminal output (available for 30 days)
  • Build Workspace zip file of the working directory (available for 30 days)

@foundationdb-ci

Copy link
Copy Markdown
Contributor

Result of foundationdb-pr on Linux RHEL 9

  • Commit ID: 78f7434
  • Duration 1:03:42
  • Result: ✅ SUCCEEDED
  • Error: N/A
  • Build Log terminal output (available for 30 days)
  • Build Workspace zip file of the working directory (available for 30 days)

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

This comment has been minimized.

@foundationdb-ci

Copy link
Copy Markdown
Contributor

Result of foundationdb-pr-cluster-tests on Linux RHEL 9

  • Commit ID: 78f7434
  • Duration 1:32:48
  • Result: ✅ SUCCEEDED
  • Error: N/A
  • Build Log terminal output (available for 30 days)
  • Build Workspace zip file of the working directory (available for 30 days)
  • Cluster Test Logs zip file of the test logs (available for 30 days)

@tclinkenbeard-oai
tclinkenbeard-oai merged commit 80ed867 into apple:main Jul 20, 2026
3 checks passed
@foundationdb-ci

Copy link
Copy Markdown
Contributor

Result of foundationdb-pr-macos on macOS 14.x

  • Commit ID: 78f7434
  • Duration 2:07:26
  • Result: ✅ SUCCEEDED
  • Error: N/A
  • Build Log terminal output (available for 30 days)
  • Build Workspace zip file of the working directory (available for 30 days)

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.

4 participants