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

feature: support hiding empty services #1264

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions apiserver/httpserver/docs/naming_console_access_apidoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ func EnrichGetAllServicesApiDocs(r *restful.RouteBuilder) *restful.RouteBuilder
Metadata(restfulspec.KeyOpenAPITags, servicesApiTags).
Param(restful.QueryParameter("namespace", "命名空间").DataType(typeNameString).
Required(false)).
Param(restful.QueryParameter("only_exist_health_instance", "只返回存在健康实例的服务").DataType(typeNameBool).
Required(false).
DefaultValue("false")).
Returns(0, "", struct {
BatchQueryResponse
Services []service_manage.Service `json:"services"`
Expand Down Expand Up @@ -156,6 +159,9 @@ func EnrichGetServicesApiDocs(r *restful.RouteBuilder) *restful.RouteBuilder {
" 和 instance_values 需要同时填写且必须元素个数一致").
DataType(typeNameString).
Required(false)).
Param(restful.QueryParameter("hide_empty_service", "隐藏空服务").DataType(typeNameBool).
Copy link
Member

Choose a reason for hiding this comment

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

没有隐藏空服务这个逻辑了吧?

Required(false).
DefaultValue("false")).
Param(restful.QueryParameter("offset", "查询偏移量").DataType(typeNameInteger).
Required(false).DefaultValue("0")).
Param(restful.QueryParameter("limit", "查询条数,**最多查询100条**").DataType(typeNameInteger).
Expand Down
20 changes: 20 additions & 0 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,16 @@ func (s *Server) GetServices(ctx context.Context, query map[string]string) *apis
var (
metaKeys, metaValues string
inputInstMetaKeys, inputInstMetaValues string
onlyExistHealthInstance bool
)

if hes, ok := query["only_exist_health_instance"]; ok {
if strings.EqualFold(hes, "true") {
onlyExistHealthInstance = true
}
delete(query, "only_exist_health_instance")
}

for key, value := range query {
typ, ok := ServiceFilterAttributes[key]
if !ok {
Expand Down Expand Up @@ -446,6 +455,17 @@ func (s *Server) GetServices(ctx context.Context, query map[string]string) *apis
return api.NewBatchQueryResponse(commonstore.StoreCode2APICode(err))
}

if onlyExistHealthInstance {
onlyExistHealthInstanceServices := make([]*model.EnhancedService, 0, len(services))
for _, service := range services {
if service.HealthyInstanceCount > 0 {
Copy link
Member

Choose a reason for hiding this comment

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

参数描述和实际逻辑不符合,建议改为 only_exist_healthyinstance

Copy link
Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

这个查询逻辑应该放在 s.caches.Service().GetServicesByFilter 里面,否则你这个查询是有问题的

onlyExistHealthInstanceServices = append(onlyExistHealthInstanceServices, service)
}
}
total = uint32(len(onlyExistHealthInstanceServices))
services = onlyExistHealthInstanceServices
}

resp := api.NewBatchQueryResponse(apimodel.Code_ExecuteSuccess)
resp.Amount = utils.NewUInt32Value(total)
resp.Size = utils.NewUInt32Value(uint32(len(services)))
Expand Down
64 changes: 64 additions & 0 deletions service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,70 @@ func TestGetService6(t *testing.T) {
})
}

// 只返回健康实例的服务测试
func TestGetService7(t *testing.T) {
discoverSuit := &DiscoverTestSuit{}
if err := discoverSuit.Initialize(); err != nil {
t.Fatal(err)
}
defer discoverSuit.Destroy()
t.Run("only_exist_health_instance过滤条件会生效", func(t *testing.T) {
total := 60
emptyServiceTotal := 30
nonEmptyServiceTotal := total - emptyServiceTotal

discoverSuit.cleanAllService()

for i := 0; i < total; i++ {
_, serviceResp := discoverSuit.createCommonService(t, i+100)
defer discoverSuit.cleanServiceName(serviceResp.GetName().GetValue(), serviceResp.GetNamespace().GetValue())
if i < nonEmptyServiceTotal {
_, instanceResp := discoverSuit.addInstance(t, &apiservice.Instance{
ServiceToken: utils.NewStringValue(serviceResp.GetToken().GetValue()),
Service: utils.NewStringValue(serviceResp.GetName().GetValue()),
Namespace: utils.NewStringValue(serviceResp.GetNamespace().GetValue()),
VpcId: utils.NewStringValue("vpcid-0"),
Host: utils.NewStringValue("9.9.9.0"),
Port: utils.NewUInt32Value(8000),
Protocol: utils.NewStringValue("protocol-0"),
Version: utils.NewStringValue("version-0"),
Priority: utils.NewUInt32Value(1),
Weight: utils.NewUInt32Value(1),
HealthCheck: nil,
Healthy: utils.NewBoolValue(true),
Isolate: utils.NewBoolValue(false),
LogicSet: utils.NewStringValue("logic-set-0"),
Metadata: map[string]string{},
})
defer discoverSuit.cleanInstance(instanceResp.GetId().GetValue())
}
}

filters := map[string]string{"offset": "0",
"limit": "100",
"namespace": "default",
"only_exist_health_instance": "true"}
resp := discoverSuit.DiscoverServer().GetServices(discoverSuit.DefaultCtx, filters)
if !respSuccess(resp) {
t.Fatalf("error: %s", resp.GetInfo().GetValue())
}
if len(resp.Services) != nonEmptyServiceTotal {
t.Fatalf("error: %d", len(resp.Services))
}

filters = map[string]string{"offset": "0",
"limit": "100",
"namespace": "default"}
resp = discoverSuit.DiscoverServer().GetServices(discoverSuit.DefaultCtx, filters)
if !respSuccess(resp) {
t.Fatalf("error: %s", resp.GetInfo().GetValue())
}
if len(resp.Services) != total {
t.Fatalf("error: %d", len(resp.Services))
}
})
}

// 测试更新服务
func TestUpdateService(t *testing.T) {

Expand Down
Loading