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

[schema registry] Return undefined for not found #15149

Merged
merged 4 commits into from
May 8, 2021

Conversation

nguerrera
Copy link
Contributor

@nguerrera nguerrera commented May 6, 2021

Fix #15130

This turns 404 into undefined for SchemaRegistryClient.getSchemaId and SchemaRegistryClient.getSchemaById.

Note, however, that the higher level avro serializer still throws on not found. While it feels natural enough when you're interacting directly with the schema registry to say "can I have a schema that matches this description or id" and get back undefined for "sorry no such schema", there is no natural return value for when you try to serialize/deserialize with a schema that is not found in the registry, so I think this should remain an exception, but I'm open to different design ideas.

@nguerrera nguerrera force-pushed the undefined-for-schema-not-found branch from 4eae23c to 73512cc Compare May 6, 2021 00:31
@nguerrera nguerrera self-assigned this May 6, 2021
@nguerrera nguerrera force-pushed the undefined-for-schema-not-found branch from 73512cc to 4a1af31 Compare May 6, 2021 00:45
Copy link
Member

@xirzec xirzec left a comment

Choose a reason for hiding this comment

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

Overall looks great! Left some small comments

@@ -167,6 +167,10 @@ export class SchemaRegistryAvroSerializer {
}

const schemaResponse = await this.registry.getSchemaById(schemaId);
if (!schemaResponse) {
throw new Error(`Schema with ID '${schemaId}' not found`);
Copy link
Member

Choose a reason for hiding this comment

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

could possibly use RangeError for this

Copy link
Member

Choose a reason for hiding this comment

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

If this is a common scenario that would need to be handled programmatically, we could make a service-specific error with a custom name that devs can look for instead of having to introspect the message

Copy link
Contributor Author

@nguerrera nguerrera May 7, 2021

Choose a reason for hiding this comment

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

My gut feeling is that this isn't an error you would want to handle in a unique way. I think your architecture should be such that you take the necessary steps to have the needed schema in the registry before trying to serialize/deserialize and failure to do so could be considered a mistake by the caller and not something they should handle, or handle any differently from any other failure to serialize/deserialize (apart from logging the different specifics in the messages somewhere).

But I'm not 100% sure on this. @dhoepelman, do you also use the avro serializer package? What would you like to see from it when the schema can't be matched in the registry?

Choose a reason for hiding this comment

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

We use the package yes

You are correct, this is in the deserialization codepath and at that time the schema ID not found is totally unexpected and unrecoverable, you would need to do unreasonable things like removing the schema while messages with it are still on the hub to encounter it.

At serialization time either autoRegister should be true and it can't occur or the schema should already be registered before serializing and its a unrecoverable caller error.

As in both cases programmatical recovery different from any other error is not possible, a specific error seems unnecessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for confirming!

} else {
const response = await this.registry.getSchemaId(description);
if (!response) {
throw new Error("Schema not found in registry.");
Copy link
Member

Choose a reason for hiding this comment

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

is there any meaningful way we can refer to the schema to provide better logging?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, there is a name I should have used. Will fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It could be confusing though because the name could match but the content differs and this throws. Will think about how to word this.

Copy link
Contributor Author

@nguerrera nguerrera May 7, 2021

Choose a reason for hiding this comment

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

I'm thinking something like "Schema with name <name> not found in registry group <group>, or not found to have matching content." A little awkward, so any thoughts on a better wording are most welcome.

Copy link
Contributor Author

@nguerrera nguerrera May 7, 2021

Choose a reason for hiding this comment

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

The schema content could also be useful in debugging this, but I think it would be too long to include in the exception message in the common case. Also, it's still an arg you passed in so you could always log/trace it yourself.

Copy link
Member

Choose a reason for hiding this comment

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

I think content would be too verbose, though if you really wanted to you could make a custom error type to attach it as a property. Not really worth it if it's not being programmatically handled though.

@nguerrera
Copy link
Contributor Author

@xirzec I pushed an update to the error message to include name and group, and I think after the discussion this was the only thing left to do, but just double checking and giving you a chance to review the commits after your sign-off before I merge...

Copy link
Member

@xirzec xirzec left a comment

Choose a reason for hiding this comment

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

Looks great!

const response = await this.registry.getSchemaId(description);
if (!response) {
throw new Error(
`Schema '${description.name}' not found in registry group '${description.group}', or not found to have matching content.`
Copy link
Member

Choose a reason for hiding this comment

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

Since you asked for an alternative string, here's my best attempt, but don't feel like you have to take it. 😄

Suggested change
`Schema '${description.name}' not found in registry group '${description.group}', or not found to have matching content.`
`Can't locate schema '${description.name}' in registry group '${description.group}' from given description. Did you mean to set autoRegisterSchemas?`

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. Elsewhere we say to avoid autoRegisterSchemas in production based on guidance from the service team. So I think we shouldn't point to it here. Leaving the message as I have it given that.

@nguerrera nguerrera merged commit 9e856ac into Azure:master May 8, 2021
@nguerrera nguerrera deleted the undefined-for-schema-not-found branch May 8, 2021 01:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Azure schema registry SDK does not have a "schema does not exist" return value
3 participants