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

implement initial discover service #4665

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

JohnN193
Copy link
Member

@JohnN193 JohnN193 commented Dec 31, 2024

Initial implementation of the discover service in Go. Currently working on GetModelsFromModules in a separate branch to make it easier on reviewers

ticket: https://viam.atlassian.net/browse/RSDK-9620

@viambot viambot added the safe to test This pull request is marked safe to test from a trusted zone label Dec 31, 2024
@@ -431,3 +431,5 @@ require (
github.com/ziutek/mymysql v1.5.4 // indirect
golang.org/x/exp v0.0.0-20240904232852-e7e105dedf7e
)

replace go.viam.com/api => github.com/johnn193/api v0.0.0-20241231164642-99f059defc82
Copy link
Member Author

Choose a reason for hiding this comment

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

will revert once api is merged

return discovery.NewRPCServiceServer(injectSvc).(pb.DiscoveryServiceServer), injectDiscovery, injectDiscovery2, nil
}

func TestDiscoveryDo(t *testing.T) {
Copy link
Member Author

Choose a reason for hiding this comment

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

since this is literally copy/paste from the generic service I should probably remove this test, but I'm also fine with leaving it in case we are worried this will somehow break

Copy link
Member

Choose a reason for hiding this comment

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

It is good to test all APIs of a service to ensure that there is adequate coverage. I can't imagine it breaking - but say someone with no context joins viam in the future and removes the DoCommands accidentally - good to have all these tests fail :) . It does not have to be separate from your other test, you can add an injected DoCommand to the injected service in your test.

Good test to have with DoComamnd are: 1. test that it returns errors if no DoCommand is implemented with resource.Named, and 2. responds with the expected responses when a DoCommand is implemented.

@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Dec 31, 2024
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jan 3, 2025
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jan 3, 2025
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jan 3, 2025
Comment on lines +166 to +169
// this was taken from proto_conversion_test.go
//
//nolint:thelper
func validateComponent(t *testing.T, actual, expected resource.Config) {
Copy link
Member Author

Choose a reason for hiding this comment

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

this is the same function as in proto_conversion_test.go

unsure if theres a good spot to put this function to use it in both spots, or if its fine to leave here

Copy link
Member

@randhid randhid left a comment

Choose a reason for hiding this comment

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

Looks good with a few nits. I think we use pointers when we want to return structs very fast, check with Nick S if that return type should be changed to just an array of resource.Configs rather than an array of pointers.

// Service describes the functions that are available to the service.
type Service interface {
resource.Resource
DiscoverResources(ctx context.Context, extra map[string]any) ([]*resource.Config, error)
Copy link
Member

Choose a reason for hiding this comment

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

Does this need to return an array of pointers to resource.Configs? Or would it be okay to return []resource.Config, since you can already return nil here?

Copy link
Member

Choose a reason for hiding this comment

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

I'm not familiar with this project but in terms of API design:

At a cursory glance, the vast majority of resource.Configs are passed and returned from functions as structs rather than pointers to structs.

In my opinion, as long as DiscoverResources isn't performance sensitive (aka we can afford to copy all the resource.Configs), we should return []resource.Config rather than []*resource.Config to be consistent with the other uses of resource.Config and b/c it makes it clear that the caller is getting a copy of the data which they are allowed to modify if they choose.

Copy link
Member

Choose a reason for hiding this comment

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

The API is meant to aid users in configuring their machines - it can return a large array - say you're configuring a thousand cameras at once - but I don't see it needing to be that performant - and I think arrays in go are pointers under the hood anyway?

Copy link
Member

Choose a reason for hiding this comment

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

Slices are indeed pointers in go. However each element of the []resource.Config is going to have a new copy of resource.Config, where as []*resource.Config can return pointers to the same objects rather than needing to make copies. As a result, if the slice has a lot of elements and the resource.Config is very big I would expect []*resource.Config to consume less memory than []resource.Config.

That said, returning pointers to objects that other parts of the system are using (and don't expect to change) is a common source of bugs, so I think we should prefer the safer and slightly more expensive method signature until we know the performance matters. I agree that this code is unlikely to be very performance-sensitive.

}

// Service describes the functions that are available to the service.
type Service interface {
Copy link
Member

Choose a reason for hiding this comment

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

hmm, surprised this is called Service not discovery since the components follow. different pattern. But whatever, the other services do this so let's keep that format.

Comment on lines +40 to +43
configs, err := svc.DiscoverResources(ctx, req.GetExtra().AsMap())
if err != nil {
return nil, err
}
Copy link
Member

Choose a reason for hiding this comment

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

we should also have a check if configs are nil here, and also test that we error nicely in the server test.

SubtypeName = "discovery"
)

// API is a variable that identifies the slam resource API.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// API is a variable that identifies the slam resource API.
// API is a variable that identifies the discovery resource API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
safe to test This pull request is marked safe to test from a trusted zone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants