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

lrs: use JSON for locality's String representation #4135

Merged
merged 7 commits into from Jan 7, 2021

Conversation

menghanl
Copy link
Contributor

@menghanl menghanl commented Jan 6, 2021

Instead of ":" separated string, as ":" can be in the fields, like jv:us-central1-a_12345.

A long term fix is to wrap the subconns to keep track of its locality.

@dfawley
Copy link
Member

dfawley commented Jan 6, 2021

Why is "#" better than ":"? It seems the only sure fix would be to escape any pre-existing special characters, or percent-encode, etc.

Instead of string separated by ":", which can be in the fields.
@menghanl
Copy link
Contributor Author

menghanl commented Jan 6, 2021

Updated to json.
Eventually, we shouldn't do this marshal/unmarshal. We should wrap the struct in the subconns.

xds/internal/internal.go Outdated Show resolved Hide resolved
@menghanl menghanl changed the title lrs: use a special delimiter for locality.String lrs: use JSON for locality.String Jan 6, 2021
@menghanl menghanl changed the title lrs: use JSON for locality.String lrs: use JSON for locality's String representation Jan 6, 2021
return fmt.Sprintf("%s:%s:%s", l.Region, l.Zone, l.SubZone)
// ToJSON generates a string representation of LocalityID by marshalling it into
// json.
func (l LocalityID) ToJSON() (string, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Looking at the changes elsewhere...should we just delete this function (and its inverse) and call the json package directly instead? They don't seem to add much.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But that hardcodes json in the balancers? Using json should be internal to the locality id.

Maybe I should call this function ToString() and FromString() instead of To/From JSON?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I still don't like calling json.Marshall directly. Because it adds the implicit contract that all locality ID string in the balancers are JSON. And if I were to add a new code to convert a string ID into a struct, I need to know that it's JSON, instead of just calling the function that does the job. And to remind people of that, I would need to add comments to all the fields where ID string is stored, and say it is JSOn.

And there's the minor annoyance that Marshal returns []byte that needs to be explicitly converted into string.

I'm keeping these two functions, but calling them ToString and FromString. Let me know what you think.

Copy link
Member

Choose a reason for hiding this comment

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

Because it adds the implicit contract

This is internal, so I'm not sure any concerns like this are important.

Let me know what you think.

This is fine, since it's short-lived.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants