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

[CORE-3182] Schema Registry json external references #24125

Open
wants to merge 10 commits into
base: dev
Choose a base branch
from

Conversation

BenPope
Copy link
Member

@BenPope BenPope commented Nov 14, 2024

Currently just a rebase of #23677 with:

  • Fixup for schema_getter
  • Shuffling of code to later commits to make each commit compile (rather than error with unused).

Backports Required

  • none - not a bug fix
  • none - this is a backport
  • none - issue does not exist in previous branches
  • none - papercut/not impactful enough to backport
  • v24.3.x
  • v24.2.x
  • v24.1.x
  • v23.3.x

Release Notes

  • none

no change in behavior, will be used for external references for json
schema
collect_schema overload accepts a list of references directly and does
not save the input schema in the collected_schema list

colleted_schema::get extranct the map of reference_name ->
reference_iobuf
to implement this, a bit of refactoring is applied to the class
no behavior change in this commit

add a map external_schema_name -> json::Document, change the
mapped_type of schemas_index to an union of local_ptr (json pointer to
the root schema) and external_ptr (ptr to a named external schema in
.external_schemas map)

the type is templated to support rapidjson and jsoncons, this will be
used later
this is done with schema_context::resolve_reference. the function will
get an uri and return a json::Value::ConstObject or throw.

the search is done with schema_index, a local_ptr will be resolved
against the root schema and an external_ptr will resolve against one of
the external_schemas.

note that there is no behavior change yet, because the external_schemas
map is not populated yet
this is done to reduce conversions when parse_json will call itself to
handle external references.

the name change to parse_jsoncons, and a new parse_json calls the former
and applies the translation to rapidjson of the result.

collect_bundled_schema_and_fix_ref has to change too.

no behavior change yet
this commit adds support for external schemas: parse_json gets an
(optional) id for the root schema, and a list of external schemas name
to build into the result.

each external schema il collected, parse_jsoncons invoked on it, and
it's list of bundles schemas is added to the root schemas_index.

each bundled schema has to have an unique id (due to the current
resolve_reference implementation) and the external_schema.$id has to be
unique (if it's not present, the exernal schema name will be used as
$id)
- if we are visiting the root, skip validation as it's already validated
  externally

- if we are visiting a bundled schema, ensure its $id is unique
this commit is a wip an unlikely to be finished.

the idea is to add $ref validation at parse time, and at the same time
handle cleanily various edge cases of external schemas

to do so:
1. external refs are changed to
   https://schema-registry.com/extrnal_ref_name
   this is to make it work like a bundled ref, in resolve_ref
   note: this does not handle well refs/with/slashes in the middle. they
   look like a valid uri path. try to add a test with them and see what
   breaks or not
2. at collect_bundled_schemas_and_fix_ref, first do a tree traversal for
   id, and save refs in an output parameter unresolved_ref.
   after the first run, check every ref: if it's not a bundled/local
   ref, or an external ref, throw. bundled/local ref are made absolute,
   external refs are encoded as (1)
   a new parameter controls what to do if a ref is unknown. for the root
   schema, an unknown ref should raise an error. for an external schema,
   assume that it's external, because parse_jsocons is recursive
   and gets invoked for each external schema to extract
   their bundled schemas index.
   it is assumed that external schemas are already good (no unresolved
   refs) because they are already accepted, so we can limit the
   recursion to 2 levels (root, and then one for each direct/indirect
   external schema)
3. schema_context::resolve_reference does not need to handle external
   schemas anymore, since they appear like bundled schemas (the uri is
   either in the index or not). that code can be simplified
@andijcr
Copy link
Contributor

andijcr commented Nov 15, 2024

immagine
I see no bugs in this pr

@BenPope BenPope marked this pull request as ready for review November 19, 2024 12:39
@vbotbuildovich
Copy link
Collaborator

vbotbuildovich commented Nov 19, 2024

the below tests from https://buildkite.com/redpanda/redpanda/builds/58258#01934470-c2e5-46bb-9cae-ff8ee031d3ab have failed and will be retried

pandaproxy_schema_registry_single_thread_rpunit

the below tests from https://buildkite.com/redpanda/redpanda/builds/58258#01934470-c2e2-4885-b8f2-e61c9be88241 have failed and will be retried

pandaproxy_schema_registry_single_thread_rpunit

@vbotbuildovich
Copy link
Collaborator

vbotbuildovich commented Nov 19, 2024

non flaky failures in https://buildkite.com/redpanda/redpanda/builds/58258#019344b1-ad5e-4eb0-af69-e37c77b21a26:

"rptest.tests.rpk_registry_test.RpkRegistryTest.test_produce_consume_json"

non flaky failures in https://buildkite.com/redpanda/redpanda/builds/58258#019344b6-8cdd-4715-9675-c035403f7e01:

"rptest.tests.rpk_registry_test.RpkRegistryTest.test_produce_consume_avro"

non flaky failures in https://buildkite.com/redpanda/redpanda/builds/58258#019344b6-8cde-4a8f-9e09-11626b43b6da:

"rptest.tests.rpk_registry_test.RpkRegistryTest.test_produce_consume_json"

@vbotbuildovich
Copy link
Collaborator

@vbotbuildovich
Copy link
Collaborator

vbotbuildovich commented Nov 19, 2024

Retry command for Build#58258

please wait until all jobs are finished before running the slash command

/ci-repeat 1
tests/rptest/tests/rpk_registry_test.py::RpkRegistryTest.test_produce_consume_json
tests/rptest/tests/rpk_registry_test.py::RpkRegistryTest.test_produce_consume_avro

@@ -502,13 +520,13 @@ result<document_context> parse_json(iobuf buf) {
reader.read(ec);
if (ec || !decoder.is_valid()) {
// not a valid json document, return error
return error_info{
throw as_exception(error_info{
Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe these should return the exception rather than throwing, something like:

Suggested change
throw as_exception(error_info{
co_return ss::coroutine::return_exception(as_exception(error_info{

Comment on lines +616 to +626
| std::views::common;
// convert external_schemas to rapidjson
auto external_view = doc_ctx.external_schemas
| std::views::transform([](auto& p) {
return std::pair{
p.first,
document_context::external_document_ctx{
.doc = to_json_document(p.second.doc),
.dialect = p.second.dialect}};
})
| std::views::common;
Copy link
Member Author

Choose a reason for hiding this comment

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

nitpick: std::views::common isn't required in either of these places, as the underlying range models a std::ranges::common_range.

Comment on lines +2542 to +2544
// the schema does not have an explicit $id. this is useful for external
// schemas, to ensure that their local refs do not conflict with the root
// schema.
Copy link
Member Author

Choose a reason for hiding this comment

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

Comment on lines 27 to 29
}
}
collected.insert(std::move(name), std::move(schema).def());
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm wondering if we need to error in the else branch and when collected.contains(name)

_schemas.push_back(std::move(def).raw());
}
return inserted;
return _schemas.emplace(std::move(name), std::move(def).raw()).second;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I think this commit would be easier to review if it was squashed into the previous one

};

ss::future<collected_schema> collect_schema(
schema_getter& store,
collected_schema collected,
ss::sstring name,
ss::sstring opt_name,
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: this could stay as name, I think, since it is not renamed in the .cc file

Suggested change
ss::sstring opt_name,
ss::sstring name,

@@ -136,6 +136,24 @@ json::Pointer to_json_pointer(std::string_view sv) {
return candidate;
}

// helper to convert a jsoncons::ojson to a rapidjson::Document
json::Document to_json_document(const jsoncons::ojson& oj) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I'd name this something like to_rapidjson or to_rpjson_document to signal in the name that this is converting between different library representations

Comment on lines 617 to 621
// recursive call to parse the external schema. it will have no external
// refs but it will have a default_id and all the relative refs will be
// resolved against it
auto ref_document = co_await parse_jsoncons(
store, std::move(def), ref_name, {});
Copy link
Contributor

Choose a reason for hiding this comment

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

Am I reading it correctly that this means that we're not supporting transitive external references (ie. referencing a schema that also references schemas)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/build area/redpanda area/schema-registry Schema Registry service within Redpanda
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants