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

Migrate away from gogo/protobuf #2798

Closed
rvolosatovs opened this issue Jun 25, 2020 · 69 comments · Fixed by #6032
Closed

Migrate away from gogo/protobuf #2798

rvolosatovs opened this issue Jun 25, 2020 · 69 comments · Fixed by #6032
Assignees
Labels
dependencies Pull requests that update a dependency file in progress We're working on it technical debt Not necessarily broken, but could be done better/cleaner umbrella This issue needs actionable issues referenced
Milestone

Comments

@rvolosatovs
Copy link
Contributor

rvolosatovs commented Jun 25, 2020

Summary

https://github.com/gogo/protobuf is not mantained any more gogo/protobuf#691 (currently)

Why do we need this?

It's our dependency, which is incompatible with new golang/protobuf version, which more and more packages depend on, hence we need to replace the golang/protobuf version, depending on outdated versions of our direct dependencies and potentially even breaking packages this way

What is already there? What do you see now?

gogo/protobuf dependency

What is missing? What do you want to see?

Figure this out

How do you propose to implement this?

Figure out if a new maintainer will appear or different plugin with feature parity?
Use just vanilla protobuf?

How do you propose to test this?

tests

Can you do this yourself and submit a Pull Request?

yes

@rvolosatovs rvolosatovs added the dependencies Pull requests that update a dependency file label Jun 25, 2020
@rvolosatovs rvolosatovs added this to the Next Up milestone Jun 25, 2020
@rvolosatovs rvolosatovs self-assigned this Jun 25, 2020
@rvolosatovs rvolosatovs added needs/discussion We need to discuss this needs/details This is missing some details labels Jun 25, 2020
@rvolosatovs
Copy link
Contributor Author

Validation plugin we're using dropped support for GoGo
bufbuild/protoc-gen-validate#340

@htdvisser
Copy link
Contributor

I think the best way forward is to follow the ecosystem and migrate away from gogo/protobuf. With more and more of our other dependencies moving away from gogo, I think it will become increasingly difficult to keep using it. Of course it's going to be a lot of work to migrate, so if we do it, we need to come up with a good plan.

@rvolosatovs probably knows more about the custom options that are set in our gogottn generator, but here's what I found for the explicit options in our proto files:

  • We could start by removing the gogoproto.customname, gogoproto.stdtime and gogoproto.stdduration, and goproto_enum_prefix options. Those are relatively easy to remove, since the Go compiler will immediately complain about any resulting issues.
  • Removing gogoproto.embed option would mean that we can no longer access embedded fields (the Go compiler will help us find those), and that messages no longer satisfy some interfaces (this may be more difficult).
  • The gogoproto.nullable option will be much more work, because we will have to start using the getters, and add nil-checks. Resulting problems may not get caught by the Go compiler. Possible workaround would be to temporarily make those fields private, then rewrite to getters/setters and finally making the fields public again.
  • What's going to be quite problematic are the fields that use gogoproto.customtype and enums that use gogoproto.enum_stringer options. For those we've often changed the way they're marshaled/unmarshaled to JSON. For the custom bytes fields such as EUI, DevAddr etc. we could change the type (in the proto messages) to string (which is binary compatible). With the enums I'm afraid it's going to be breaking the JSON API, since those are now accepted (by UnmarshalJSON) as both strings and ints.

Maybe this is also a good time to start thinking about our v4 API, because I can imagine we might discover some more (API breaking) surprises.

@rvolosatovs
Copy link
Contributor Author

rvolosatovs commented Aug 18, 2020

I think the best way forward would be to first try out https://github.com/alta/protopatch. Depending on the result:

  • If all our needs are covered -> migrate and forget about this(should be just search-and-replace in api directory)
  • If only some of our needs are covered and there are custom options not covered by the plugin -> we should evaluate on per-option basis and either remove these custom options or, perhaps, contribute to the protopatch if it's a low-effort feature. This really depends on the option, though - if we're talking about customtype - that IMO definitely justifies contributing, but maybe something like stdtime - not so much.

Looking forward, I don't think we should be directly using vanilla protobuf protos in components internally at runtime(given the provided feature set of protobuf today).
It only makes sense to use protobuf for (de-)serialization, so for storage and on API layer. Internally, however, using plain vanilla generated Go protos makes no sense to me.
So, for example, NS:

  1. get *ttnpb.EndDevice (vanilla generated Go type) from the registry, deserialized from stored binary data
  2. convert *ttnpb.EndDevice into T_device, (NOTE: perhaps that could be just a wrapper initially or forever)
  3. use T_device internally in NS
  4. convert T_device into *ttnpb.EndDevice (NOTE: this could be a trivial, very fast task if we're using a wrapper, since we only need to modify changed fields and that could even be performed on binary data directly)
  5. set *ttnpb.EndDevice, serialize into binary data

@htdvisser
Copy link
Contributor

Refs also #342 (generated populators)

@johanstokking
Copy link
Member

I'm not in favor of a (smaller) alternative to gogo. It feels like pushing the can. Let's keep things as vanilla as possible, especially when we need to decide again what the best way forward is.

I do agree that we can consider using intermediary types in some places, instead of relying everywhere on the generated protos. It's basically separating data transfer objects (DTOs: protos, also for storage) from data access objects (DAOs: how we use them). If that is primarily reading, we can also declare interfaces and see how far we get with that.

That said, I wouldn't go as far as changing the entire NS to using T_device, but rather specific structs and/or interfaces as needed.

@htdvisser htdvisser added the technical debt Not necessarily broken, but could be done better/cleaner label Nov 3, 2020
@htdvisser htdvisser modified the milestones: Next Up, November 2020 Nov 3, 2020
@htdvisser
Copy link
Contributor

Let's move this discussion to November

@johanstokking
Copy link
Member

@rvolosatovs what is your objection against moving to vanilla with a custom JSON marshaler?

@rvolosatovs
Copy link
Contributor Author

The huge migration burden and loads of boilerplate if we end up just using vanilla protos directly.
I don't really object to that though, I just think we should try to find a simple non-intrusive alternative first and if that's not possible, then resort to reworking this whole thing.

@johanstokking
Copy link
Member

I'm afraid that any plugin that we start to rely on will end up in an unmaintained state at some point. Generally speaking, I'm in favor of keeping things as close to vanilla as possible. If that means nil checking more often than we like, then so be it. It can also work in our favor that we know that things are not set, instead of an initialized struct.

@htdvisser htdvisser modified the milestones: November 2020, December 2020 Dec 1, 2020
@htdvisser
Copy link
Contributor

I'm afraid that refactoring our entire codebase is going to be a pain no matter how we do it. Our (gogo-generated) proto structs are used everywhere right now (gRPC API, HTTP API, events, errors, internally, Redis DB, ...), so changing to something else (whatever that other thing is) is going to touch pretty much everything in our codebase, and the way it looks now, all at the same time.

The hard requirement is that we don't break compatibility of our v3 API. Even if we decide to use this situation as the moment to start working on a v4 API (at least internally), we will still have to keep supporting that v3 API for existing users.

In the long term, I think we would do ourselves a big favor by decoupling our (versioned, stable-within-major) external APIs from our internal (unversioned, stable-within-minor) API and our (versioned, stable) database documents. We could then write or generate functions to convert between our internal APIs and the others.

But I think there are some steps that we can already take now:

JSON

In order to keep our v3 JSON API compatible, I think our first TODO is to work on generating JSON marshalers and unmarshalers that understand how to marshal/unmarshal our custom types. I think doing this is smart anyway because there is no stability promise for Go's implementation of the JSON format for protocol buffers, so we better have control over that ourselves. Doing this could also allow us to consider field masks when marshaling to JSON. In the grpc-gateway runtime we can register codecs, so we can just write a codec that calls our own (generated) (un)marshalers instead of {gogo,golang}/protobuf's jsonpb.

Generate new protos next to the old ones

I already tried that here: a41f62d

This does make protobuf complain about the types registry, so we may need to remove golang_proto.RegisterType from our old protos to make this work. Removing that could potentially break resolving of google.protobuf.Any, but we only use those in errors and events, so we can probably find workarounds for those specific cases.

Generate functions to convert between old and new protos

This is for the transition period only, but for the long term solution, we'd want to generate similar converters.

Update services one by one

I already tried that with a simple service here: cd7d75c, but for more complicated services we'd definitely need those converters.

Note that this only changes the grpc service itself. The grpc-gateway still uses the old gogo stuff on the JSON side, and then calls the internal gRPC server, which then runs the new implementation.

@johanstokking
Copy link
Member

Pushed some initial dependency updates and backwards compatibility work-arounds here: https://github.com/TheThingsNetwork/lorawan-stack/compare/issue/2798-codec

@adriansmares
Copy link
Contributor

I've built two new releases of protoc-gen-validate and protoc-gen-fieldmask that are based on the V2 proto API. They are based on the new release of protoc-gen-star that enables our generators to generate V2 API code.

I've updated the tooling in a separate branch to use the new protoc / protoc-gen-go tooling and remove gogo. You can see the diff here. In our generated code (paths, setters, validate) the changes are minimal to none.

Please sample some files and let me know what you think. If things look good, we can start implementing the commits from #2798 (comment) on that branch, and finally move to API v2.

@adriansmares
Copy link
Contributor

adriansmares commented Dec 15, 2022

Migration status update:

In order to speedup the development, we can already start backporting some changes from https://github.com/TheThingsIndustries/lorawan-stack/pull/3445 in order to lower the diff:

  • Embedding the ttnpb.Unimplemented*Server server implementations into our server implementation.
    The background here is that the v2 API requires that every server implementation publicly embeds the unimplemented implementation.
    I have already done the changes in https://github.com/TheThingsIndustries/lorawan-stack/pull/3445 , but due to poor commit management backporting these changes is hard. The general idea is to do the following:
  1. Find a RegisterServices(s *grpc.Server) call site:
    // RegisterServices registers the Configuration service.
    func (c *ConfigurationServer) RegisterServices(s *grpc.Server) {
    ttnpb.RegisterConfigurationServer(s, c)
    }
  2. Go to the definition of the implementation (second parameter):
    // ConfigurationServer implements the Configuration RPC service.
    type ConfigurationServer struct {
    component *Component
    }
  3. Add the ttnpb.Unimplemented*Server to the struct:
    https://github.com/TheThingsIndustries/lorawan-stack/blob/0bfac6481e31ca26946e3b39fd15a1f585b85e26/pkg/component/configuration_grpc.go#L32-L37
    You're now done.

@adriansmares
Copy link
Contributor

Migration status update:

  • The monolithic commit containing all of the changes has been completely broken down. All of the non-migration specific changes have been backported to v3.23.
  • The test resemblance issue has been fixed. After the migration, we will be using go-cmp in order to do assert value resemblance. We currently twist its hand a bit in order to make it behave like reflect.DeepEqual, but long term we should consider if we can remove this.
  • Unit test, Console end to end tests and traffic end to end test pass.

After the Christmas holidays we can start merging back this mammoth change in enterprise and open source.

The only problem to settle is backwards compatibility with respect to the JSON API. The context is as follows:

  1. For a large part of v3's lifetime, field masks were rendered as objects, not as strings. This behavior was caused by the gogoproto implementation of jsonpb.
    In this context, object form means {"paths": ["a.b.c", "c.d.e"]}, and string form means "a.b.c,c.d.e".
  2. When we started using protoc-gen-go-json one year ago, we accidentally broke the API and caused some field mask to render as strings. Messages which contained a field mask and an EndDevice ended up having a custom JSON marshaler, which in turn caused field masks to be rendered as strings. Messages which did not, such as ApplicationWebhook, ended up having field masks rendered as objects.

@ysmilda has implemented (1) which allows us to generate the marshallers for every message, thus ensuring that at least we use the same style everywhere, and (2) allows us to choose which style we want to use.

In https://github.com/TheThingsIndustries/lorawan-stack/pull/3445 I actually 'broke back' the API to render all field masks as objects, in order to keep consistence over time. My argument here is that for most of v3's lifetime and for all of the code in the wild, the object form is what is used.

Are there any objections to 'breaking the API' a second time to keep things consistent to what they were a year ago ? Our JSON is not JSONPb anyway (custom renderers, custom enum marshaling). @johanstokking @KrishnaIyer


Unmarshalling fieldmasks is not a problem - protoc-gen-go-json automatically can unmarshal both styles at the same time.

@johanstokking
Copy link
Member

Are there any objections to 'breaking the API' a second time to keep things consistent to what they were a year ago ? Our JSON is not JSONPb anyway (custom renderers, custom enum marshaling). @johanstokking @KrishnaIyer

Yes I prefer consistency, even if we need to break the JSON API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Pull requests that update a dependency file in progress We're working on it technical debt Not necessarily broken, but could be done better/cleaner umbrella This issue needs actionable issues referenced
Projects
None yet
Development

Successfully merging a pull request may close this issue.