Skip to content

Commit

Permalink
Allow duplicate definition keys in hydrated version of mergeSchemas
Browse files Browse the repository at this point in the history
Reviewed By: praihan

Differential Revision: D67948962

fbshipit-source-id: 65e2df590290d445acec8eb60259f79c3da4bc31
  • Loading branch information
iahs authored and facebook-github-bot committed Jan 9, 2025
1 parent cb8a0f6 commit f318d46
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions thrift/lib/cpp2/runtime/SchemaRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ namespace {
void mergeInto(
type::Schema& dst,
type::Schema&& src,
std::unordered_set<type::ProgramId>& includedPrograms) {
std::unordered_set<type::ProgramId>& includedPrograms,
bool allowDuplicateDefinitionKeys) {
for (auto& program : *src.programs()) {
auto id = *program.id();
if (!includedPrograms.insert(id).second) {
Expand All @@ -51,7 +52,8 @@ void mergeInto(
dst.definitionsMap()->insert(
std::make_move_iterator(src.definitionsMap()->begin()),
std::make_move_iterator(src.definitionsMap()->end()));
if (dst.definitionsMap()->size() != ndefs + src.definitionsMap()->size()) {
if (!allowDuplicateDefinitionKeys &&
dst.definitionsMap()->size() != ndefs + src.definitionsMap()->size()) {
throw std::runtime_error("DefinitionKey collision");
}
}
Expand Down Expand Up @@ -85,7 +87,11 @@ SchemaRegistry::Ptr SchemaRegistry::getMergedSchema() {
mergedSchema_ = std::make_shared<type::Schema>();
for (auto& [name, data] : base_.rawSchemas_) {
if (auto schema = readSchema(data.data)) {
mergeInto(*mergedSchema_, std::move(*schema), includedPrograms_);
mergeInto(
*mergedSchema_,
std::move(*schema),
includedPrograms_,
/*allowDuplicateDefinitionKeys*/ false);
}
}

Expand All @@ -98,7 +104,11 @@ SchemaRegistry::Ptr SchemaRegistry::getMergedSchema() {
}

if (auto schema = readSchema(data)) {
mergeInto(*mergedSchema_, std::move(*schema), includedPrograms_);
mergeInto(
*mergedSchema_,
std::move(*schema),
includedPrograms_,
/*allowDuplicateDefinitionKeys*/ false);
}
};

Expand All @@ -113,7 +123,11 @@ type::Schema SchemaRegistry::mergeSchemas(

for (const auto& data : schemas) {
if (auto schema = readSchema(data)) {
mergeInto(mergedSchema, std::move(*schema), includedPrograms);
mergeInto(
mergedSchema,
std::move(*schema),
includedPrograms,
/*allowDuplicateDefinitionKeys*/ false);
}
}

Expand All @@ -125,7 +139,18 @@ type::Schema SchemaRegistry::mergeSchemas(std::vector<type::Schema>&& schemas) {
std::unordered_set<type::ProgramId> includedPrograms;

for (auto& schema : schemas) {
mergeInto(mergedSchema, std::move(schema), includedPrograms);
/*
* allowDuplicateDefinitionKeys is true here because this is called by
* MultiplexAsyncProcessor, which may hold services with a shared base
* service.
* Additionally, since this function accepts deserialized schemas
* those schemas were probably already checked for duplicates earlier.
*/
mergeInto(
mergedSchema,
std::move(schema),
includedPrograms,
/*allowDuplicateDefinitionKeys*/ true);
}

return mergedSchema;
Expand Down

0 comments on commit f318d46

Please sign in to comment.