Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: protobuf: serde: exception bypass for canonical repeated nested #69556

Conversation

hileef
Copy link
Contributor

@hileef hileef commented Sep 12, 2024

Disclaimer

  • I have very little CPP experience, please feel free to make any and all constructive feedback to improve this proposal
  • I picked the "bugfix" category because I interpret the extraneous entries in system.errors when processing a "canonical"/"valid" proto/schema to be incorrect behaviour. Please correct it if necessary.

On Semantics

The current potobuf serde implementation will always internally generate PROTOBUF_FIELD_NOT_REPEATED
when de/serializing such a canonical example :

syntax = "proto3";

message A {
  repeated B v = 20;
  message B {
    uint32 x = 21;
    uint32 y = 22;
  }
}
CREATE TABLE example (
    `v.x` Array(UInt32),
    `v.y` Array(UInt32)
)

↘️ This PR essentially proposes to change the control flow a bit, such that :

WHEN the proto field we are mapping into is repeated ,
AND when the matching columns are nested and of type Array,
THEN we attempt to serialize via ProtobufSerializerFlattenedNestedAsArrayOfNestedMessages first,
RATHER THAN first going through the try/catch, throwing PROTOBUF_FIELD_NOT_REPEATED and catching it.

In the case where I am missing some use case and/or possible situtation where this control flow would be faulty,
please do comment / let me know, ideally with example proto/sql such that it can be turned into a new test and the implementation adapted 🙏

On Performance

I discovered this behaviour when investigating the performance of a cluster a colleage was running benchmarks on, where he would be consuming complex protobuf messages with the Kafka engine. Since clickhouse currently does not appear to re-use the generated de/serializer impl ( according to #59482 ), I realized that this try/catch flow behaviour was tanking performance, with a huge amount of CPU time spent in libunwind. Incidentally, we were generating millions of PROTOBUF_FIELD_NOT_REPEATED in the system.errors table.

Screenshot 2024-07-17 at 01 44 00

CC


Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):

Bypass try/catch flow when de/serializing nested repeated protobuf to nested columns ( fixes #41971 )

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Information about CI checks: https://clickhouse.com/docs/en/development/continuous-integration/

CI Settings (Only check the boxes if you know what you are doing):

  • Allow: All Required Checks
  • Allow: Stateless tests
  • Allow: Stateful tests
  • Allow: Integration Tests
  • Allow: Performance tests
  • Allow: All Builds
  • Allow: batch 1, 2 for multi-batch jobs
  • Allow: batch 3, 4, 5, 6 for multi-batch jobs

  • Exclude: Style check
  • Exclude: Fast test
  • Exclude: All with ASAN
  • Exclude: All with TSAN, MSAN, UBSAN, Coverage
  • Exclude: All with aarch64, release, debug

  • Run only fuzzers related jobs (libFuzzer fuzzers, AST fuzzers, etc.)
  • Exclude: AST fuzzers

  • Do not test
  • Woolen Wolfdog
  • Upload binaries for special builds
  • Disable merge-commit
  • Disable CI cache

Comment on lines +3328 to +3329
if (suffix.empty())
continue;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This specific change is not required per se, but it arguably brings all relevant code paths into the diff.
If it may help the review, we can get rid of that 🙏

@Avogar Avogar added the can be tested Allows running workflows for external contributors label Sep 17, 2024
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-bugfix Pull request with bugfix, not backported by default label Sep 17, 2024
@robot-ch-test-poll3
Copy link
Contributor

robot-ch-test-poll3 commented Sep 17, 2024

This is an automated comment for commit 167196b with description of existing statuses. It's updated for the latest CI running

❌ Click here to open a full report in a separate page

Check nameDescriptionStatus
AST fuzzerRuns randomly generated queries to catch program errors. The build type is optionally given in parenthesis. If it fails, ask a maintainer for help❌ error
Bugfix validationChecks that either a new test (functional or integration) or there some changed tests that fail with the binary built on master branch❌ failure
Stateless testsRuns stateless functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc❌ failure
Successful checks
Check nameDescriptionStatus
BuildsThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
ClickBenchRuns [ClickBench](https://github.com/ClickHouse/ClickBench/) with instant-attach table✅ success
Compatibility checkChecks that clickhouse binary runs on distributions with old libc versions. If it fails, ask a maintainer for help✅ success
Docker keeper imageThe check to build and optionally push the mentioned image to docker hub✅ success
Docker server imageThe check to build and optionally push the mentioned image to docker hub✅ success
Docs checkBuilds and tests the documentation✅ success
Fast testNormally this is the first check that is ran for a PR. It builds ClickHouse and runs most of stateless functional tests, omitting some. If it fails, further checks are not started until it is fixed. Look at the report to see which tests fail, then reproduce the failure locally as described here✅ success
Flaky testsChecks if new added or modified tests are flaky by running them repeatedly, in parallel, with more randomization. Functional tests are run 100 times with address sanitizer, and additional randomization of thread scheduling. Integration tests are run up to 10 times. If at least once a new test has failed, or was too long, this check will be red. We don't allow flaky tests, read the doc✅ success
Install packagesChecks that the built packages are installable in a clear environment✅ success
Integration testsThe integration tests report. In parenthesis the package type is given, and in square brackets are the optional part/total tests✅ success
Performance ComparisonMeasure changes in query performance. The performance test report is described in detail here. In square brackets are the optional part/total tests✅ success
Stateful testsRuns stateful functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc✅ success
Stress testRuns stateless functional tests concurrently from several clients to detect concurrency-related errors✅ success
Style checkRuns a set of checks to keep the code style clean. If some of tests failed, see the related log from the report✅ success
Unit testsRuns the unit tests for different release types✅ success
Upgrade checkRuns stress tests on server version from last release and then tries to upgrade it to the version from the PR. It checks if the new server can successfully startup without any errors, crashes or sanitizer asserts✅ success

@Avogar Avogar self-assigned this Sep 17, 2024
@Avogar
Copy link
Member

Avogar commented Sep 17, 2024

Hi! Thank you for the PR, I will take a look at the code. Can you please fix style check first?
It complains that file 03234_proto_simple_nested_repeated_noexception.reference is executable (reference files should be just text files) and style issue in file ProtobufSerializer.cpp:3366 (no space after for)

@hileef
Copy link
Contributor Author

hileef commented Sep 17, 2024

Hello @Avogar , I'll get on that as soon as possible ( a bit later today ) 👍 Thank you for the help 🙂

@hileef hileef force-pushed the fix/serde/protobuf/bypass-libunwind-on-nested-repeated branch from c56e08a to 7d97727 Compare September 17, 2024 21:35
@hileef hileef force-pushed the fix/serde/protobuf/bypass-libunwind-on-nested-repeated branch from 7d97727 to 167196b Compare September 17, 2024 21:37
@hileef
Copy link
Contributor Author

hileef commented Sep 18, 2024

After fixing the style issues, I took the liberty of slightly adapting the added tests in order to properly work with the testing system ( format_schemas dir not automatically present , no formats in fasttest , use correct test DB ). If these changes are incorrect, please let me know.

At this point, the tests added in this PR do appear to work, however I'm not sure why the remaining CI checks fail.
I am looking forward to help on the topic 🙂 🙏

@hileef
Copy link
Contributor Author

hileef commented Sep 19, 2024

I attempted the test 02490_benchmark_max_consecutive_errors ( from this CI run ) on my local build of this branch,
and the test appeared to pass .... maybe this could be related to differences between the CI and my local setup ? 🤔

In any case, the other test failures appear related to timeouts, so I'd be very interested in possibly relaunching the CI jobs that failed to check if they fail again 🙏

@hileef
Copy link
Contributor Author

hileef commented Sep 19, 2024

Alright ! Thanks for letting me know and for the exploration URL, I didn't know this was publicly available 🙂

Looking forward to hearing from you again once you've had an opportunity to review the code and/or
if you have some additional remarks on the other CI checks 🙏

@Avogar
Copy link
Member

Avogar commented Sep 27, 2024

Looks good, thank you!

@Avogar Avogar added this pull request to the merge queue Sep 27, 2024
Merged via the queue into ClickHouse:master with commit e9006c1 Sep 27, 2024
206 of 212 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can be tested Allows running workflows for external contributors pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PROTOBUF_FIELD_NOT_REPEATED when ingesting from Kafka
5 participants