You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2022/01/07 01:26:59 UTC

[GitHub] [servicecomb-service-center] little-cui commented on a change in pull request #1205: [feat]add health api in sync package

little-cui commented on a change in pull request #1205:
URL: https://github.com/apache/servicecomb-service-center/pull/1205#discussion_r779963509



##########
File path: syncer/config/config.go
##########
@@ -33,11 +33,22 @@ type Config struct {
 }
 
 type Sync struct {
-	Peers []*Peer `yaml:"peers"`
+	EnableOnStart bool        `yaml:"enableOnStart"`
+	DataCenter    *DataCenter `yaml:"datacenter"`

Review comment:
       不应该有此属性

##########
File path: syncer/health/health.go
##########
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package health
+
+import (
+	"context"
+
+	v1sync "github.com/apache/servicecomb-service-center/api/sync/v1"
+	"github.com/apache/servicecomb-service-center/client"
+	"github.com/apache/servicecomb-service-center/server/rpc/sync"
+	"github.com/apache/servicecomb-service-center/syncer/config"
+)
+
+const (
+	scheme = "health_rpc"
+)
+
+type Resp struct {
+	DataCenter *DataCenter `json:"datacenter"`
+	Peers      []*Peer     `json:"peer"`
+}
+
+type DataCenter struct {
+	Name string `json:"name"`
+}
+
+type Peer struct {
+	Name      string   `json:"name"`
+	Kind      string   `json:"kind"`
+	Mode      []string `json:"mode"`
+	Endpoints []string `json:"endpoints"`
+	Status    string   `json:"status"`
+}
+
+func Health() *Resp {

Review comment:
       应该有error返回,用于外层判断是否内部错误异常

##########
File path: server/rpc/sync/server.go
##########
@@ -16,3 +22,12 @@ func (s *Server) Sync(ctx context.Context, events *v1sync.EventList) (*v1sync.Re
 	log.Info(fmt.Sprintf("Received: %v", events.Events[0].Action))
 	return &v1sync.Results{}, nil
 }
+
+func (s *Server) Health(ctx context.Context, request *v1sync.HealthRequest) (*v1sync.HealthReply, error) {
+	log.Info(fmt.Sprintf("Health Received"))
+	syncerEnabled := config.GetBool("syncer.enabled", false)

Review comment:
       配置项不正确,应该是sync.enableOnStart

##########
File path: syncer/health/health.go
##########
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package health
+
+import (
+	"context"
+
+	v1sync "github.com/apache/servicecomb-service-center/api/sync/v1"
+	"github.com/apache/servicecomb-service-center/client"
+	"github.com/apache/servicecomb-service-center/server/rpc/sync"
+	"github.com/apache/servicecomb-service-center/syncer/config"
+)
+
+const (
+	scheme = "health_rpc"
+)
+
+type Resp struct {
+	DataCenter *DataCenter `json:"datacenter"`

Review comment:
       没有此属性

##########
File path: client/set.go
##########
@@ -20,11 +24,33 @@ type Set struct {
 
 // NewSetForConfig dial grpc connection and create all grpc clients
 func NewSetForConfig(c SetConfig) (*Set, error) {
-	conn, err := grpc.Dial(c.Addr, grpc.WithInsecure())
+	if len(c.Addr) <= 0 {
+		return nil, errors.New("addr is empty")
+	}
+
+	var conn *grpc.ClientConn
+	var err error
+
+	if len(c.Addr) <= 1 {
+		conn, err = grpc.Dial(c.Addr[0], grpc.WithInsecure())
+	} else {
+		addr := make([]resolver.Address, 0, len(c.Addr))
+		for _, a := range c.Addr {
+			addr = append(addr, resolver.Address{Addr: a})
+		}
+		r := manual.NewBuilderWithScheme(c.Scheme)
+		r.InitialState(resolver.State{Addresses: addr})
+		conn, err = grpc.Dial(r.Scheme()+":///", grpc.WithInsecure(), grpc.WithResolvers(r))

Review comment:
       安全问题:链接泄露,没有close

##########
File path: syncer/health/health.go
##########
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package health
+
+import (
+	"context"
+
+	v1sync "github.com/apache/servicecomb-service-center/api/sync/v1"
+	"github.com/apache/servicecomb-service-center/client"
+	"github.com/apache/servicecomb-service-center/server/rpc/sync"
+	"github.com/apache/servicecomb-service-center/syncer/config"
+)
+
+const (
+	scheme = "health_rpc"
+)
+
+type Resp struct {
+	DataCenter *DataCenter `json:"datacenter"`
+	Peers      []*Peer     `json:"peer"`
+}
+
+type DataCenter struct {
+	Name string `json:"name"`
+}
+
+type Peer struct {
+	Name      string   `json:"name"`
+	Kind      string   `json:"kind"`
+	Mode      []string `json:"mode"`
+	Endpoints []string `json:"endpoints"`
+	Status    string   `json:"status"`
+}
+
+func Health() *Resp {
+	resp := &Resp{
+		DataCenter: &DataCenter{},
+		Peers:      make([]*Peer, 0),
+	}
+
+	config := config.GetConfig()
+	if config.Sync == nil || config.Sync.DataCenter == nil {
+		return resp
+	}
+
+	resp.DataCenter.Name = config.Sync.DataCenter.Name
+	for _, c := range config.Sync.Peers {
+		if len(c.Endpoints) <= 0 {
+			continue
+		}
+		p := &Peer{
+			Name:      c.Name,
+			Kind:      c.Kind,
+			Mode:      c.Mode,
+			Endpoints: c.Endpoints,
+		}
+		p.Status = getPeerStatus(c.Endpoints)
+		resp.Peers = append(resp.Peers, p)
+	}
+	return resp
+}
+
+func getPeerStatus(endpoints []string) string {
+	client, err := client.NewSetForConfig(client.SetConfig{Addr: endpoints, Scheme: scheme})

Review comment:
       未有fallback策略,3次重试,单次超时10s,参考:https://github.com/grpc/grpc-go/blob/master/examples/features/retry/client/main.go

##########
File path: syncer/health/health.go
##########
@@ -0,0 +1,89 @@
+/*

Review comment:
       应该放到syncer/service/admin/health.go下

##########
File path: syncer/health/health_test.go
##########
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package health
+
+import (
+	"testing"
+
+	"github.com/apache/servicecomb-service-center/syncer/config"
+)
+
+func TestHealth(t *testing.T) {
+	c := config.GetConfig()
+	tests := []struct {
+		name    string
+		sync    *config.Sync
+		wantErr bool
+	}{
+		{name: "check no config ",
+			sync:    nil,
+			wantErr: true,
+		},
+		{name: "check no dataCenter",
+			sync: &config.Sync{
+				DataCenter: nil,
+				Peers:      []*config.Peer{},
+			},
+			wantErr: true,
+		},
+		{name: "check no endpoints",
+			sync: &config.Sync{
+				DataCenter: &config.DataCenter{Name: "tets"},
+				Peers: []*config.Peer{
+					{Endpoints: nil},
+				},
+			},
+			wantErr: true,
+		},
+		{name: "check endpoints is empty",
+			sync: &config.Sync{
+				DataCenter: &config.DataCenter{Name: "tets"},
+				Peers: []*config.Peer{
+					{Endpoints: []string{}},
+				},
+			},
+			wantErr: true,
+		},
+
+		{name: "given normal config",
+			sync: &config.Sync{
+				DataCenter: &config.DataCenter{Name: "tets"},
+				Peers: []*config.Peer{
+					{Endpoints: []string{"127.0.0.1:30105"}},
+				},
+			},
+			wantErr: false,
+		},
+	}
+
+	for _, test := range tests {
+		c.Sync = test.sync
+		config.SetConfig(c)
+		resp := Health()
+
+		hasErr := false
+		if resp.DataCenter == nil {
+			hasErr = true
+		}
+
+		if resp.Peers == nil {
+			hasErr = true
+		}
+
+		if len(resp.Peers) <= 0 {
+			hasErr = true
+		}
+
+		if hasErr != test.wantErr {
+			t.Errorf("%s. Health(), wantErr %+v", test.name, test.wantErr)
+		}

Review comment:
       1、UT用assert断言
   2、缺少正常场景的验证

##########
File path: syncer/resource/admin/admin.go
##########
@@ -34,6 +35,6 @@ func (res *Resource) URLPatterns() []rest.Route {
 }
 
 func (res *Resource) HealthCheck(w http.ResponseWriter, r *http.Request) {
-	// TODO call health service
-	rest.WriteResponse(w, r, nil, nil)
+	healthResp := health.Health()

Review comment:
       异常时需要返回5xx错误




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@servicecomb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org