You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by "little-cui (via GitHub)" <gi...@apache.org> on 2023/08/26 04:21:26 UTC

[GitHub] [servicecomb-service-center] little-cui commented on a diff in pull request #1425: multi instances change status by properties

little-cui commented on code in PR #1425:
URL: https://github.com/apache/servicecomb-service-center/pull/1425#discussion_r1306318020


##########
datasource/etcd/ms.go:
##########
@@ -1569,3 +1571,57 @@ func (ds *MetadataManager) UnregisterService(ctx context.Context, request *pb.De
 func (ds *MetadataManager) Statistics(ctx context.Context, withShared bool) (*pb.Statistics, error) {
 	return statistics(ctx, withShared)
 }
+
+func (ds *MetadataManager) UpdateManyInstanceStatus(ctx context.Context, match datasource.MatchPolicy, status string) error {
+	resp, _ := ds.ListManyInstances(ctx, &pb.GetAllInstancesRequest{})
+	instances := resp.Instances
+	if len(instances) == 0 {
+		return nil
+	}
+	options := make([]etcdadpt.OpOptions, 0)
+	cmps := make([]etcdadpt.CmpOptions, 0)
+
+	domainProject := util.ParseDomainProject(ctx)
+	filterInstances := make([]*discovery.MicroServiceInstance, 0)
+
+	for _, instance := range instances {
+		var t = true
+		for k, v := range match.Properties {
+			value, ok := instance.Properties[k]
+			if ok {
+				if value != v {
+					t = false
+					break
+				}
+			} else {
+				t = false
+				break
+			}
+		}
+		if t {
+			filterInstances = append(filterInstances, instance)
+			key := path.GenerateServiceTagKey(domainProject, instance.ServiceId)
+			//更新状态
+			instance.Status = status
+			data, err := json.Marshal(instance)
+			leaseID, err := serviceUtil.GetLeaseID(ctx, domainProject, instance.ServiceId, instance.InstanceId)
+			options = append(options, etcdadpt.Ops(etcdadpt.OpPut(etcdadpt.WithStrKey(key), etcdadpt.WithValue(data), etcdadpt.WithLease(leaseID)))...)
+			if err != nil {
+				log.Error(fmt.Sprintf("get leaseId error"), err)

Review Comment:
   错误需要continue



##########
server/resource/disco/instance_resource.go:
##########
@@ -19,6 +19,7 @@ package disco
 
 import (
 	"fmt"
+	"github.com/apache/servicecomb-service-center/datasource"

Review Comment:
   执行go import格式化



##########
server/resource/disco/instance_resource.go:
##########
@@ -311,3 +313,31 @@ func (s *InstanceResource) PutInstanceProperties(w http.ResponseWriter, r *http.
 	}
 	rest.WriteResponse(w, r, nil, nil)
 }
+
+func (s *InstanceResource) UpdateManyInstanceStatus(w http.ResponseWriter, r *http.Request) {
+	request := &UpdateManyInstanceStatusRequest{}
+	message, _ := io.ReadAll(r.Body)
+	query := r.URL.Query()
+
+	err := codec.Decode(message, request)
+
+	if err != nil {
+		log.Error(fmt.Sprintf("invalid json: %s", util.BytesToStringWithNoCopy(message)), err)
+		rest.WriteError(w, pb.ErrInvalidParams, "Unmarshal error")
+		return
+	}
+	ctx := util.SetTargetDomainProject(r.Context(), r.Header.Get("X-Domain-Name"), query.Get(":project"))

Review Comment:
   不需要这个,前置context handler会自动赋值



##########
datasource/etcd/ms.go:
##########
@@ -1569,3 +1571,57 @@ func (ds *MetadataManager) UnregisterService(ctx context.Context, request *pb.De
 func (ds *MetadataManager) Statistics(ctx context.Context, withShared bool) (*pb.Statistics, error) {
 	return statistics(ctx, withShared)
 }
+
+func (ds *MetadataManager) UpdateManyInstanceStatus(ctx context.Context, match datasource.MatchPolicy, status string) error {
+	resp, _ := ds.ListManyInstances(ctx, &pb.GetAllInstancesRequest{})
+	instances := resp.Instances
+	if len(instances) == 0 {
+		return nil
+	}
+	options := make([]etcdadpt.OpOptions, 0)
+	cmps := make([]etcdadpt.CmpOptions, 0)
+
+	domainProject := util.ParseDomainProject(ctx)
+	filterInstances := make([]*discovery.MicroServiceInstance, 0)
+
+	for _, instance := range instances {
+		var t = true
+		for k, v := range match.Properties {
+			value, ok := instance.Properties[k]
+			if ok {
+				if value != v {
+					t = false
+					break
+				}
+			} else {
+				t = false
+				break
+			}
+		}
+		if t {
+			filterInstances = append(filterInstances, instance)
+			key := path.GenerateServiceTagKey(domainProject, instance.ServiceId)

Review Comment:
   不正确的KEY



##########
datasource/etcd/ms.go:
##########
@@ -1569,3 +1571,57 @@ func (ds *MetadataManager) UnregisterService(ctx context.Context, request *pb.De
 func (ds *MetadataManager) Statistics(ctx context.Context, withShared bool) (*pb.Statistics, error) {
 	return statistics(ctx, withShared)
 }
+
+func (ds *MetadataManager) UpdateManyInstanceStatus(ctx context.Context, match datasource.MatchPolicy, status string) error {
+	resp, _ := ds.ListManyInstances(ctx, &pb.GetAllInstancesRequest{})
+	instances := resp.Instances
+	if len(instances) == 0 {
+		return nil
+	}
+	options := make([]etcdadpt.OpOptions, 0)
+	cmps := make([]etcdadpt.CmpOptions, 0)
+
+	domainProject := util.ParseDomainProject(ctx)
+	filterInstances := make([]*discovery.MicroServiceInstance, 0)
+
+	for _, instance := range instances {
+		var t = true
+		for k, v := range match.Properties {
+			value, ok := instance.Properties[k]
+			if ok {
+				if value != v {
+					t = false
+					break
+				}
+			} else {
+				t = false
+				break
+			}
+		}
+		if t {
+			filterInstances = append(filterInstances, instance)
+			key := path.GenerateServiceTagKey(domainProject, instance.ServiceId)
+			//更新状态
+			instance.Status = status
+			data, err := json.Marshal(instance)
+			leaseID, err := serviceUtil.GetLeaseID(ctx, domainProject, instance.ServiceId, instance.InstanceId)
+			options = append(options, etcdadpt.Ops(etcdadpt.OpPut(etcdadpt.WithStrKey(key), etcdadpt.WithValue(data), etcdadpt.WithLease(leaseID)))...)
+			if err != nil {
+				log.Error(fmt.Sprintf("get leaseId error"), err)

Review Comment:
   错误需要continue



##########
server/resource/disco/instance_resource.go:
##########
@@ -311,3 +313,31 @@ func (s *InstanceResource) PutInstanceProperties(w http.ResponseWriter, r *http.
 	}
 	rest.WriteResponse(w, r, nil, nil)
 }
+
+func (s *InstanceResource) UpdateManyInstanceStatus(w http.ResponseWriter, r *http.Request) {
+	request := &UpdateManyInstanceStatusRequest{}
+	message, _ := io.ReadAll(r.Body)
+	query := r.URL.Query()
+
+	err := codec.Decode(message, request)
+
+	if err != nil {
+		log.Error(fmt.Sprintf("invalid json: %s", util.BytesToStringWithNoCopy(message)), err)
+		rest.WriteError(w, pb.ErrInvalidParams, "Unmarshal error")
+		return
+	}
+	ctx := util.SetTargetDomainProject(r.Context(), r.Header.Get("X-Domain-Name"), query.Get(":project"))

Review Comment:
   不需要这个,前置context handler会自动赋值



##########
datasource/etcd/ms.go:
##########
@@ -1569,3 +1571,57 @@ func (ds *MetadataManager) UnregisterService(ctx context.Context, request *pb.De
 func (ds *MetadataManager) Statistics(ctx context.Context, withShared bool) (*pb.Statistics, error) {
 	return statistics(ctx, withShared)
 }
+
+func (ds *MetadataManager) UpdateManyInstanceStatus(ctx context.Context, match datasource.MatchPolicy, status string) error {
+	resp, _ := ds.ListManyInstances(ctx, &pb.GetAllInstancesRequest{})
+	instances := resp.Instances
+	if len(instances) == 0 {
+		return nil
+	}
+	options := make([]etcdadpt.OpOptions, 0)
+	cmps := make([]etcdadpt.CmpOptions, 0)
+
+	domainProject := util.ParseDomainProject(ctx)
+	filterInstances := make([]*discovery.MicroServiceInstance, 0)
+
+	for _, instance := range instances {
+		var t = true
+		for k, v := range match.Properties {
+			value, ok := instance.Properties[k]
+			if ok {
+				if value != v {
+					t = false
+					break
+				}
+			} else {
+				t = false
+				break
+			}
+		}
+		if t {
+			filterInstances = append(filterInstances, instance)
+			key := path.GenerateServiceTagKey(domainProject, instance.ServiceId)

Review Comment:
   不正确的KEY



##########
server/resource/disco/instance_resource.go:
##########
@@ -19,6 +19,7 @@ package disco
 
 import (
 	"fmt"
+	"github.com/apache/servicecomb-service-center/datasource"

Review Comment:
   执行go import格式化



-- 
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