Repository navigation
Fix shared cache issues between Protobuf and ProtobufList - #100849
Conversation
Signed-off-by: Callum C <[email protected]>
|
The test is required. |
|
@nikitamikhaylov I have added a test for this |
|
Hello @callumc34 , thank you for your finding and for contribution.
|
|
@ilejn thanks for taking a look at this. Happy to make changes.
For our workflow, we have several Protobufs we are trying to store, notably position and object, if these are both in the same Protobuf package then we can only choose one of these to be the envelope and this repeats even if we decompose them into their own package then the components of these Protobuf models can't have their own envelopes and so on. What we were doing, before we encountered this bug, was dynamically generating the Protobuf envelops, say both
The cache key fix is the main reason for this PR, without it, ProtobufList and Protobuf silently interfere with each other. However, the
Have updated the PR for both of these, would appreciate any feedback on the documentation. |
|
Hello @callumc34, it is quite possible that I am missing something, so your inputs are appreciated.
|
|
@ilejn thanks for following up.
It's not global across ClickHouse, the pool is scoped to a single ImporterWithSourceTree. Before this PR there was one per directory. which is what caused the cache bug this PR fixes, with the fix there's one per (directory, with_envelope) pair. Note that filesystem directory and protobuf package are independent concepts, a directory can contain files from any mix of packages. Within a pool, protobuf enforces uniqueness of fully-qualified names, which include the package, so messages in different packages don't conflict. The case where behavior could be surprising is unqualified top-level messages with the same simple name across different .proto files in the same directory — if a.proto is imported first with a top-level Person, a later call for b.proto:Person would return a.proto's Person without re-importing b.proto. This is pre-existing behavior and isn't changed by this PR.
Yes, the importer cache is keyed per (directory, with_envelope) and .proto files within the same directory share a pool. As above, protobuf enforces full-name uniqueness within a pool, so collisions only matter for unqualified simple names shared across files in the same directory. This is also pre-existing behavior and not introduced by this PR.
Yes, that's correct when with_envelope is set, the lookup tries Envelope. first and falls through to a top-level lookup if that doesn't find a match, regardless of whether Envelope itself is present. The intent is to prefer the Envelope form when WithEnvelope is specified, otherwise use the top-level message. This keeps serialization behavior consistent (the wire format is the same either way) and avoids forcing users to remove an Envelope wrapper in order to reference an unrelated top-level message in the same file. I have updated the documentation to reflect that. Please let me know if you'd like any of this behaviour changed. |
Yes, sure, I am just trying to clarify what is going on. To me a possible collision between a.proto:Person and b.proto:Person is pretty hard to ignore. |
@ilejn I think we could bring this into scope reasonably easily by just removing the pool lookup which isn't file-scoped. const auto * descriptor = importer.pool()->FindMessageTypeByName(message_name);
if (descriptor)
return descriptor;The subsequent Worth noting: this pool lookup was added in 2019 (commit e9721c9) when there was a single global Importer for all schemas, a shared pool across the entire instance. This shortcut was a more meaningful optimization, since the alternative was re-running path resolution and file import logic. The same commit refactored to the current per-directory ImporterWithSourceTree model but kept the pool lookup, which is where the file-collision behavior was unintentionally introduced. Removing it now essentially finishes that refactor. |
|
Workflow [PR], commit [0ce860d] Summary: ❌
AI ReviewSummaryThis PR fixes ClickHouse Rules
Final Verdict
|
|
Hello @callumc34 , removing global pool alone is not enough. I think that 233e8aa from branch https://github.com/ilejn/ClickHouse/commits/fix/fallback-from-envelope/ does the trick. Feel free to cherry-pick or just reuse. To me the behavior we have in master is not perfect and worth fixing, but lets ask @Avogar and @rschu1ze what are their opinions. |
(cherry picked from commit 233e8aa)
|
Thanks for looking into that and supporting this PR @ilejn , have cherry-picked that commit, looks right to me. |
|
Hello @Avogar , |
Avogar
left a comment
There was a problem hiding this comment.
Looks good. Just one minor comment
| std::unordered_map<ImporterKey, | ||
| std::shared_ptr<ImporterWithSourceTree>, | ||
| decltype([](const ImporterKey & key) | ||
| { | ||
| auto h1 = std::hash<String>{}(key.schema_directory); | ||
| auto h2 = std::hash<String>{}(key.schema_path); | ||
| return h1 ^ (h2 << 1) ^ (static_cast<uint8_t>(key.with_envelope) << 2); | ||
| })> importers; |
There was a problem hiding this comment.
It's better to extract the hasher into a separate struct to make the code more readable. Also you can use our sipHash implementation with update method here:
struct ImporterKeyHash
{
size_t operator()(const ImporterKey & key) const
{
SipHash hash;
hash.update(key.schema_directory);
hash.update(key.schema_path);
hash.update(key.with_envelope);
return hash.get64();
}
};There was a problem hiding this comment.
It is close to the original approach of the author of the PR ;)
There was a problem hiding this comment.
Thanks for taking a look, have separated this out as suggested
LLVM Coverage Report
Changed lines: 88.57% (31/35) | lost baseline coverage: 2 line(s) · Uncovered code |
|
@ilejn I don't believe any of these GHA failures are related to the changes in this PR. What is the process from here to get this merged? |
8dc1d7f
|
Hi — this PR may need backporting to Affected code: Why: User-visible bug: switching between Protobuf and ProtobufList in the same session previously corrupted the shared cache, producing misleading errors. If this should be backported, consider adding |
|
Hello, congratulations ! ;) @callumc34, what do you think about backporting? |
From Alexey M' standpoint the process is to fix all issues regardless if they are related or not. |
|
@ilejn Thanks!
I think this should be backported. For any user whose workflow was working, schemas still resolve to the same descriptor so nothing changes. The only way a workflow could break is if it was depending on the broken cache state, and since that resets on every restart I don't think such a workflow would survive in practice, nor should such a workflow be incentivised as this would likely lead to a bug report, but I would be interested for your thoughts on that. The Envelope fallback only kicks in for schemas that previously errored, so no working flow is at risk there either. |
…-envelope Fix shared cache issues between Protobuf and ProtobufList
Backport #100849 to 26.4: Fix shared cache issues between Protobuf and ProtobufList
…rotobuf and ProtobufList
…ickHouse#110754, not to 26.3) PROTO: ProtobufSchemas cached one importer per schema directory and looked a message up by name in the importer's whole descriptor pool before importing the requested file. Two schema files in the same directory that define a message with the same name (or the same file used as Protobuf and as ProtobufList with an Envelope) then shared the first descriptor: a query could parse its input with another file's message definition, i.e. wrong columns, errors or wrongly parsed data. The fix keys the importer by (directory, schema file, envelope) and looks the message up in the imported file only. Found by the CAND5 gtest run: three unit tests of the ClickHouse#106905 backport (gtest_protobuf_sync_after_error.cpp) give each test its own inline schema with a message named Row and failed with "Not found matches between the names of the columns (d) and the fields (number) of the message 'Row'" - the previous test's schema. The stateless test is the upstream 04061_protobuf_cache_with_envelope. Upstream patch unchanged (applies cleanly), with its test and schemas. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…49, test 04061) 04061_protobuf_cache_with_envelope fails on the production build a394e92 and passes on official 26.8.8.8 (isolated servers, evidence/fix_ci_20260925/upstream_repro/pr100849/). Registry 148 entries, 3 open; 230 selected tests. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…0849 Backport ClickHouse#100849 to 26.4: Fix shared cache issues between Protobuf and ProtobufList
This PR fixes a caching issue with protobuf format in which the WithEnvelope is not considered. This leads to misleading broken format parsing when first using FORMAT Protobuf in which both Protobuf and ProtobufList format are able to use the provided message type, however when the cache is reset, ProtobufList attempts to force the Envelope format which can lead to errors when not available. The Envelope specific format is not truly required - any Envelope will work and pointing to the original format is all thats required for the schema.
The secondary fix, allowing fallback from envelope may be undesirable, if so I can remove.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Fixes a protobuf cache entry issue and adds fallback to message type if Envelope schema not found
Documentation entry for user-facing changes
Version info
26.5.1.208(included in26.5and later)26.4.5.137