Skip to content

C workload bindings - #11288

Merged
spraza merged 7 commits into
apple:mainfrom
Wonshtrum:c_workload_api
Nov 20, 2024
Merged

spraza merged 7 commits into
apple:mainfrom
Wonshtrum:c_workload_api

Conversation

@Wonshtrum

@Wonshtrum Wonshtrum commented Apr 9, 2024 •

Copy link
Copy Markdown
Contributor

I'm utilizing the ExternalWorkload to test custom layers within the FoundationDB simulation. The layers under test are authored in Rust and are based on the foundationdb-rs project. The workload API is defined by the C++ header file ClientWorkload.h. Interoperability between Rust and C++ presents challenges, although creating a C++ to C shim was relatively straightforward. My main concern is the unstable ABI of the C++ API, as it relies on the compiler, linker, and system libraries. This proved to be an issue when transitioning from version 7.1 to 7.3, where the workload I was using suddenly broke due to a change in the representation of the std::string class as the fdbserver switched from gcc to clang, as detailed in this forum thread and issue #11298.

To address this, I propose that alongside the existing C++ API (utilized by JavaWorkload), pure C bindings should be made available to ease integration with other languages and mitigate the impact of changes in compilation environments. This aligns well with the location of ClientWorkload.h in the bindings/c folder.

Given my limited familiarity with the FoundationDB codebase, I resorted to various wrappers to minimize code changes. However, a more efficient approach might involve rewriting the ExternalWorkload or creating a dedicated C version.

In this PR, I've divided ClientWorkload.h into CWorkload.h and CppWorkload.h. Importing CppWorkload.h should mirror the behavior of the original ClientWorkload.h, ensuring compatibility with existing workloads utilizing this API. The C bindings introduce BridgeToClient and BridgeToServer structs that encapsulate collections of function pointers. If the "useCAPI" option is enabled in the ExternalWorkload configuration file, it will load the workloadInstantiate symbol (instead of workloadFactory) and invoke it with the server-bridge (allowing the C workload to interact with the C++ FDBWorkloadContext and GenericPromise<bool> classes). The returned client-bridge is then encapsulated within a C++ "translator" workload and assigned as the workloadImpl of the ExternalWorkload.

@Wonshtrum
Wonshtrum force-pushed the c_workload_api branch 7 times, most recently from d6a8fa4 to 857e629 Compare April 11, 2024 06:35
@Wonshtrum
Wonshtrum marked this pull request as draft April 11, 2024 10:57
@Wonshtrum
Wonshtrum force-pushed the c_workload_api branch 3 times, most recently from ed2ca03 to e71c078 Compare June 11, 2024 17:35
@Wonshtrum
Wonshtrum marked this pull request as ready for review June 11, 2024 17:36
@Wonshtrum Wonshtrum changed the title Draft: C workload bindings C workload bindings Jun 11, 2024
@Wonshtrum

Copy link
Copy Markdown
Contributor Author

The simulation framework we integrated into foundationdb-rs has proven valuable, catching numerous bugs in our database layers. However, we’re still dependent on the ClientWorkload.h C++ bindings, which aren’t FFI-safe.

Would you be open to reviewing this PR or perhaps discussing any alternative approaches? I’d be glad to adapt the implementation if there’s a better way forward.

@jzhou77 jzhou77 closed this Nov 5, 2024
@jzhou77 jzhou77 reopened this Nov 5, 2024
@jzhou77
jzhou77 self-requested a review November 5, 2024 16:33
@foundationdb-ci

Copy link
Copy Markdown
Contributor

Result of foundationdb-pr-clang-ide on Linux CentOS 7

  • Commit ID: bb7d4a2
  • Duration 0:04:39
  • Result: ❌ FAILED
  • Error: Error while executing command: if [[ $(git diff --shortstat 2> /dev/null | tail -n1) == "" ]]; then echo "CODE FORMAT CLEAN"; else echo "CODE FORMAT NOT CLEAN"; echo; echo "THE FOLLOWING FILES NEED TO BE FORMATTED"; echo; git ls-files -m; echo; exit 1; fi. Reason: exit status 1
  • 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 CentOS 7

  • Commit ID: bb7d4a2
  • Duration 0:04:43
  • Result: ❌ FAILED
  • Error: Error while executing command: if [[ $(git diff --shortstat 2> /dev/null | tail -n1) == "" ]]; then echo "CODE FORMAT CLEAN"; else echo "CODE FORMAT NOT CLEAN"; echo; echo "THE FOLLOWING FILES NEED TO BE FORMATTED"; echo; git ls-files -m; echo; exit 1; fi. Reason: exit status 1
  • 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-clang-arm on Linux CentOS 7

  • Commit ID: bb7d4a2
  • Duration 0:04:48
  • Result: ❌ FAILED
  • Error: Error while executing command: if [[ $(git diff --shortstat 2> /dev/null | tail -n1) == "" ]]; then echo "CODE FORMAT CLEAN"; else echo "CODE FORMAT NOT CLEAN"; echo; echo "THE FOLLOWING FILES NEED TO BE FORMATTED"; echo; git ls-files -m; echo; exit 1; fi. Reason: exit status 1
  • 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-cluster-tests on Linux CentOS 7

  • Commit ID: bb7d4a2
  • Duration 0:05:21
  • Result: ❌ FAILED
  • Error: Error while executing command: if [[ $(git diff --shortstat 2> /dev/null | tail -n1) == "" ]]; then echo "CODE FORMAT CLEAN"; else echo "CODE FORMAT NOT CLEAN"; echo; echo "THE FOLLOWING FILES NEED TO BE FORMATTED"; echo; git ls-files -m; echo; exit 1; fi. Reason: exit status 1
  • 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)

@foundationdb-ci

Copy link
Copy Markdown
Contributor

Result of foundationdb-pr-clang on Linux CentOS 7

  • Commit ID: bb7d4a2
  • Duration 0:05:34
  • Result: ❌ FAILED
  • Error: Error while executing command: if [[ $(git diff --shortstat 2> /dev/null | tail -n1) == "" ]]; then echo "CODE FORMAT CLEAN"; else echo "CODE FORMAT NOT CLEAN"; echo; echo "THE FOLLOWING FILES NEED TO BE FORMATTED"; echo; git ls-files -m; echo; exit 1; fi. Reason: exit status 1
  • 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-macos on macOS Ventura 13.x

  • Commit ID: bb7d4a2
  • Duration 0:06:50
  • Result: ❌ FAILED
  • Error: Error while executing command: if [[ $(git diff --shortstat 2> /dev/null | tail -n1) == "" ]]; then echo "CODE FORMAT CLEAN"; else echo "CODE FORMAT NOT CLEAN"; echo; echo "THE FOLLOWING FILES NEED TO BE FORMATTED"; echo; git ls-files -m; echo; exit 1; fi. Reason: exit status 1
  • 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-macos-m1 on macOS Ventura 13.x

  • Commit ID: bb7d4a2
  • Duration 0:07:05
  • Result: ❌ FAILED
  • Error: Error while executing command: if [[ $(git diff --shortstat 2> /dev/null | tail -n1) == "" ]]; then echo "CODE FORMAT CLEAN"; else echo "CODE FORMAT NOT CLEAN"; echo; echo "THE FOLLOWING FILES NEED TO BE FORMATTED"; echo; git ls-files -m; echo; exit 1; fi. Reason: exit status 1
  • Build Log terminal output (available for 30 days)
  • Build Workspace zip file of the working directory (available for 30 days)

Signed-off-by: Eloi DEMOLIS <[email protected]>
@jzhou77 jzhou77 closed this Nov 5, 2024
@spraza spraza closed this Nov 19, 2024
@spraza spraza reopened this Nov 19, 2024
@foundationdb-ci

Copy link
Copy Markdown
Contributor

Result of foundationdb-pr-clang-ide on Linux CentOS 7

  • Commit ID: f63c2aa
  • Duration 0:25:33
  • 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-clang-arm on Linux CentOS 7

  • Commit ID: f63c2aa
  • Duration 0:53:00
  • 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-cluster-tests on Linux CentOS 7

  • Commit ID: f63c2aa
  • Duration 0:55:40
  • 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)

@foundationdb-ci

Copy link
Copy Markdown
Contributor

Result of foundationdb-pr-clang on Linux CentOS 7

  • Commit ID: f63c2aa
  • Duration 1:11:16
  • 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 CentOS 7

  • Commit ID: f63c2aa
  • Duration 1:13:06
  • 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)

@Wonshtrum

Wonshtrum commented Nov 19, 2024 •

Copy link
Copy Markdown
Contributor Author

I don't understand why Result of foundationdb-pr-clang on Linux CentOS 7 failed last time, and why it passes now (I did not change anything).
The more I read about dynamic symbols on binaries the more it seems like an unstable feature implemented differently depending on the platform. It is definitively beyond my comfort zone in C. I would be glad if someone who worked on the building toolchain could review this part. When I see all the cases and scripts involved in the fdb_c bindings compilation I question the soundness of my PR. In the end, I really consider going back to the object-oriented design which does not need the fdbserver to expose any symbols since the functions pointers are passed as arguments to the symbol exposed by the workload. That way I don't need to change the compilation of the fdbserver for any platform.
What do you think?

@spraza

spraza commented Nov 19, 2024

Copy link
Copy Markdown
Collaborator

@Wonshtrum

I don't understand why Result of foundationdb-pr-clang on Linux CentOS 7 failed last time, and why it passes now (I did not change anything).

We run 10K simulation tests for every PR. One test was failing but it was not related to your change. Compilation was fine last time.

When I see all the cases and scripts involved in the fdb_c bindings compilation I question the soundness of my PR. In the end, I really consider going back to the object-oriented design which does not need the fdbserver to expose any symbols since the functions pointers are passed as arguments to the symbol exposed by the workload. That way I don't need to change the compilation of the fdbserver for any platform.
What do you think?

I just reviewed your recent changes and I agree with you. In my opinion, it's more trouble for what it's worth.

@Wonshtrum
Wonshtrum force-pushed the c_workload_api branch 2 times, most recently from bf4148c to c03354a Compare November 20, 2024 16:48
@Wonshtrum

Wonshtrum commented Nov 20, 2024 •

Copy link
Copy Markdown
Contributor Author

I reverted the last commit to return to the "object-oriented" design which doesn't require the fdbserver binary to expose any symbol: the function pointers are explicitly passed as arguments in structs as needed instead.
I added an explicit conversion between capi::FDBSeverity and FDBSeverity, this decouples the underlying value of the variants, so we do not need to keep the two enum sync. This also makes CppWorkload.h standalone again, it doesn't depend on CWorkload.h anymore, which I prefer.
I added documentation on the bindings, I'm not sure about FDBWorkloadContext::now and FDBWorkload::getCheckTimeout.
I also changed the default value of the FDBMetric format_code to be %.3g instead of 0.3g both in C and Rust, I do not understand the latter value and why it is used in CppWorkload.h, is it a mistake? If so can I change it?

with "%.3g":
Metric (0, 0): External.test, 42.000000, 42
Metric (1, 0): External.test, 4222.000000, 4.22e+03
with "0.3g":
Metric (0, 0): External.test, 42.000000, 0.3g
Metric (1, 0): External.test, 4222.000000, 0.3g

Other than that, I don't see any other modifications to make.
Should I squash the commit history?

@spraza

spraza commented Nov 20, 2024

Copy link
Copy Markdown
Collaborator

I reverted the last commit to return to the "object-oriented" design which doesn't require the fdbserver binary to expose any symbol: the function pointers are explicitly passed as arguments in structs as needed instead. I added an explicit conversion between capi::FDBSeverity and FDBSeverity, this decouples the underlying value of the variants, so we do not need to keep the two enum sync. This also makes CppWorkload.h standalone again, it doesn't depend on CWorkload.h anymore, which I prefer. I added documentation on the bindings, I'm not sure about FDBWorkloadContext::now and FDBWorkload::getCheckTimeout. I also changed the default value of the FDBMetric format_code to be %.3g instead of 0.3g both in C and Rust, I do not understand the latter value and why it is used in CppWorkload.h, is it a mistake? If so can I change it?

with "%.3g":
Metric (0, 0): External.test, 42.000000, 42
Metric (1, 0): External.test, 4222.000000, 4.22e+03
with "0.3g":
Metric (0, 0): External.test, 42.000000, 0.3g
Metric (1, 0): External.test, 4222.000000, 0.3g

Other than that, I don't see any other modifications to make. Should I squash the commit history?

@Wonshtrum Great, thanks. Let me do a final review today and hopefully this PR is good to go.

Once accepted, I will merge your PR to the main branch, and in that process, also squash all commits into one, so you don't have to do it now.

@spraza spraza closed this Nov 20, 2024
@spraza spraza reopened this Nov 20, 2024
@foundationdb-ci

Copy link
Copy Markdown
Contributor

Result of foundationdb-pr-clang-ide on Linux CentOS 7

  • Commit ID: 6058962
  • Duration 0:30:02
  • 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-macos-m1 on macOS Ventura 13.x

  • Commit ID: 6058962
  • Duration 0:48:49
  • 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-macos on macOS Ventura 13.x

  • Commit ID: 6058962
  • Duration 0:51:11
  • 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-clang-arm on Linux CentOS 7

  • Commit ID: 6058962
  • Duration 0:53: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)

@foundationdb-ci

Copy link
Copy Markdown
Contributor

Result of foundationdb-pr-cluster-tests on Linux CentOS 7

  • Commit ID: 6058962
  • Duration 0:58:37
  • 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)

@foundationdb-ci

Copy link
Copy Markdown
Contributor

Result of foundationdb-pr-clang on Linux CentOS 7

  • Commit ID: 6058962
  • Duration 1:32:23
  • 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 CentOS 7

  • Commit ID: 6058962
  • Duration 1:36:22
  • 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)

if (!workloadImpl) {
TraceEvent(SevError, "WorkloadNotFound").log();
success = false;
return;

@spraza spraza Nov 20, 2024 •

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.

I noticed this return was not in the existing code, but looks like existing code had a bug which would cause a crash. Thanks for fixing.

@spraza spraza left a comment •

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.

LGTM, this is a great addition to the codebase, so thanks again.

@Wonshtrum I will merge this change, but one unaddressed feedback is a README file in the rust directory mentioning the rust code is just present as an example and has no cmake integration. Can you add that file in a separate PR? Else, I can also add it.

I also changed the default value of the FDBMetric format_code to be %.3g instead of 0.3g both in C and Rust, I do not understand the latter value and why it is used in CppWorkload.h, is it a mistake? If so can I change it?

I missed this earlier. Yes, this looks like a bug. Would appreciate if you can fix this along with the README mentioned above.

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