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

feat: support Consul as ServiceRegistry #666

Merged
merged 50 commits into from
Aug 8, 2024
Merged

Conversation

lyt122
Copy link
Contributor

@lyt122 lyt122 commented Aug 1, 2024

ref #259
It's the part of refresh the Consul services

@github-actions github-actions bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Aug 1, 2024
Copy link

codecov bot commented Aug 1, 2024

Codecov Report

Attention: Patch coverage is 76.78571% with 13 lines in your changes missing coverage. Please review.

Project coverage is 88.13%. Comparing base (acd7184) to head (49c5909).
Report is 9 commits behind head on main.

Files Patch % Lines
controller/registries/consul/config.go 76.78% 11 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #666      +/-   ##
==========================================
- Coverage   88.16%   88.13%   -0.04%     
==========================================
  Files         119      119              
  Lines        5763     5806      +43     
==========================================
+ Hits         5081     5117      +36     
- Misses        492      498       +6     
- Partials      190      191       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@spacewander spacewander left a comment

Choose a reason for hiding this comment

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

Let's add more tests to pass the coverage requirement.

}
services, meta, err := reg.client.consulCatalog.Services(q)
if err != nil {
reg.logger.Errorf("failed to get services, err: %v", err)
Copy link
Member

Choose a reason for hiding this comment

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

Add a sleep and skip this turn would be better? So we can retry after some time.

//}
defer ticker.Stop()
q := &consulapi.QueryOptions{
WaitTime: dur,
Copy link
Member

Choose a reason for hiding this comment

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

Why don't we add Datacenter to this like what we do in fetchAllServices?

return nil, nil
if err != nil {
reg.logger.Errorf("failed to get service, err: %v", err)
return nil
Copy link
Member

Choose a reason for hiding this comment

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

IMHO, we can pass the err out and make the Start fail if we fail to fetchAllServices. This can detect error like configuring a wrong consul address.

@lyt122
Copy link
Contributor Author

lyt122 commented Aug 2, 2024

@spacewander

select {
case <-reg.done:
reg.logger.Infof("stop refreshing services")
return

//wait to retry
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 retry when sync fails, not when the registry is done.

return

//wait to retry
time.Sleep(SleepTime)
Copy link
Member

Choose a reason for hiding this comment

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

Would be better to use the configured duration as the sleep time, so it could be adaptable. For example, sleep N seconds when the WaitTime is N.

@@ -127,3 +163,65 @@ func TestRefresh(t *testing.T) {
assert.Len(t, reg.softDeletedServices, 1)

}

func TestGenerateServiceEntry(t *testing.T) {
host := "test.default-group.public.earth.nacos"
Copy link
Member

Choose a reason for hiding this comment

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

Better use consul host in the consul test

if err != nil {
return err
}
if reg.client == nil {
Copy link
Member

Choose a reason for hiding this comment

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

So far the Start would be called only one. We can skip the reg.client == nil check?

@@ -235,3 +263,46 @@ func (reg *Consul) refresh(services map[string][]string) {
}

}

func (reg *Consul) generateServiceEntry(host string, services []model.SubscribeService) *registry.ServiceEntryWrapper {
Copy link
Member

Choose a reason for hiding this comment

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

This method is copied from Nacos and is not used in Consul?

@lyt122 lyt122 marked this pull request as draft August 6, 2024 09:05
@lyt122 lyt122 marked this pull request as ready for review August 6, 2024 16:45
@lyt122
Copy link
Contributor Author

lyt122 commented Aug 6, 2024

@spacewander

}

err := reg.Start(config)
assert.NoError(t, err)
Convey("Test Start method", t, func() {
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 a strong reason to introduce a new testing library and write tests in another style?

assert.NoError(t, err)
Convey("Test Start method", t, func() {

patches := gomonkey.ApplyMethod(reflect.TypeOf(reg), "FetchAllServices", func(_ *Consul, client *Client) (map[consulService]bool, error) {
Copy link
Member

Choose a reason for hiding this comment

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

We don't need to make this method public. See

patches := gomonkey.ApplyPrivateMethod(reg, "fetchAllServices", func(client *nacosClient) (map[nacosService]bool, error) {

@lyt122
Copy link
Contributor Author

lyt122 commented Aug 7, 2024

@spacewander

@spacewander spacewander merged commit 473e4b6 into mosn:main Aug 8, 2024
12 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants