little-cui closed pull request #322: [SCB-454] update instance status api param 
check
URL: https://github.com/apache/incubator-servicecomb-service-center/pull/322
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/pkg/validate/validate.go b/pkg/validate/validate.go
index 6c25e30d..e54cc557 100644
--- a/pkg/validate/validate.go
+++ b/pkg/validate/validate.go
@@ -246,9 +246,9 @@ func (v *Validator) Validate(s interface{}) error {
                                ok, invalidValue := validate.Match(fi)
                                if !ok {
                                        if filter(fieldName) {
-                                               return fmt.Errorf("The field 
'%s.%s' value does not match rule: %s", st.Type.Name(), fieldName, validate)
+                                               return fmt.Errorf("The field 
'%s.%s' invalid-value does not match rule: %s", st.Type.Name(), fieldName, 
validate)
                                        }
-                                       return fmt.Errorf("The field '%s.%s' 
value(%v) does not match rule: %s", st.Type.Name(), fieldName, invalidValue, 
validate)
+                                       return fmt.Errorf("The field '%s.%s' 
invalid-value(%v) does not match rule: %s", st.Type.Name(), fieldName, 
invalidValue, validate)
                                }
                        }
                }
diff --git a/server/core/common.go b/server/core/common.go
index 2ea5de2c..0d2ae757 100644
--- a/server/core/common.go
+++ b/server/core/common.go
@@ -30,7 +30,6 @@ var (
        ServiceAPI  pb.ServiceCtrlServer
        InstanceAPI pb.SerivceInstanceCtrlServerEx
 
-       VersionRegex                  *regexp.Regexp
        MicroServiceValidator         validate.Validator
        MicroServiceInstanceValidator validate.Validator
        ServiceRuleValidator          validate.Validator
@@ -50,6 +49,7 @@ var (
        SchemasValidator              validate.Validator
        SchemaValidator               validate.Validator
        FrameWKValidator              validate.Validator
+       UpdateInstanceValidator       validate.Validator
 
        SchemaIdRule *validate.ValidateRule
        TagRule      *validate.ValidateRule
@@ -58,10 +58,11 @@ var (
 func init() {
        // 非map/slice的validator
        nameRegex, _ := 
regexp.Compile(`^[a-zA-Z0-9]*$|^[a-zA-Z0-9][a-zA-Z0-9_\-.]*[a-zA-Z0-9]$`)
+       // find 支持alias,多个:
        serviceNameForFindRegex, _ := 
regexp.Compile(`^[a-zA-Z0-9]*$|^[a-zA-Z0-9][a-zA-Z0-9_\-.:]*[a-zA-Z0-9]$`)
        //name模糊规则: name, *
        nameFuzzyRegex, _ := 
regexp.Compile(`^[a-zA-Z0-9]*$|^[a-zA-Z0-9][a-zA-Z0-9_\-.]*[a-zA-Z0-9]$|^\*$`)
-       VersionRegex, _ = regexp.Compile(`^[0-9]+(\.[0-9]+){0,2}$`)
+       versionRegex, _ := regexp.Compile(`^[0-9]+(\.[0-9]+){0,2}$`)
        // version模糊规则: 1.0, 1.0+, 1.0-2.0, latest
        versionFuzzyRegex, _ := 
regexp.Compile(`^[0-9]*$|^[0-9]+(\.[0-9]+)*\+{0,1}$|^[0-9]+(\.[0-9]+)*-[0-9]+(\.[0-9]+)*$|^latest$`)
        pathRegex, _ := regexp.Compile(`^[A-Za-z0-9.,?'\\/+&%$#=~_\-@{}]*$`)
@@ -79,6 +80,8 @@ func init() {
        schemaIdRegex, _ := 
regexp.Compile(`^[a-zA-Z0-9]{1,160}$|^[a-zA-Z0-9][a-zA-Z0-9_\-.]{0,158}[a-zA-Z0-9]$`)
 //length:{1,160}
        instStatusRegex, _ := regexp.Compile("^(" + util.StringJoin([]string{
                pb.MSI_UP, pb.MSI_DOWN, pb.MSI_STARTING, pb.MSI_OUTOFSERVICE}, 
"|") + ")?$")
+       updateInstStatusRegex, _ := regexp.Compile("^(" + 
util.StringJoin([]string{
+               pb.MSI_UP, pb.MSI_DOWN, pb.MSI_STARTING, pb.MSI_OUTOFSERVICE}, 
"|") + ")$")
        tagRegex, _ := regexp.Compile(`^[a-zA-Z][a-zA-Z0-9_\-.]{0,63}$`)
        hbModeRegex, _ := regexp.Compile(`^(push|pull)$`)
        numberAllowEmptyRegex, _ := regexp.Compile(`^[0-9]*$`)
@@ -89,19 +92,22 @@ func init() {
        regionRegex, _ := regexp.Compile(`^[A-Za-z0-9_.-]+$`)
        ruleRegex, _ := regexp.Compile(`^(WHITE|BLACK)$`)
        ruleAttrRegex, _ := 
regexp.Compile(`((^tag_[a-zA-Z][a-zA-Z0-9_\-.]{0,63}$)|(^ServiceId$)|(^AppId$)|(^ServiceName$)|(^Version$)|(^Description$)|(^Level$)|(^Status$))`)
-       SchemaSummaryRegex, _ := regexp.Compile(`(a-zA-Z0-9)*`)
+       schemaSummaryRegex, _ := regexp.Compile(`(a-zA-Z0-9)*`)
 
-       ServiceIdRule := &validate.ValidateRule{Min: 1, Length: 64, Regexp: 
serviceIdRegex}
-       InstanceStatusRule := &validate.ValidateRule{Regexp: instStatusRegex}
        SchemaIdRule = &validate.ValidateRule{Regexp: schemaIdRegex}
-       nameRule := &validate.ValidateRule{Min: 1, Max: 128, Regexp: nameRegex}
-       versionFuzzyRule := &validate.ValidateRule{Min: 1, Max: 128, Regexp: 
versionFuzzyRegex}
        TagRule = &validate.ValidateRule{Regexp: tagRegex}
 
+       serviceIdRule := &validate.ValidateRule{Min: 1, Length: 64, Regexp: 
serviceIdRegex}
+       instanceStatusRule := &validate.ValidateRule{Regexp: instStatusRegex}
+       updateInstStatusRule := &validate.ValidateRule{Regexp: 
updateInstStatusRegex}
+       serviceNameRule := &validate.ValidateRule{Min: 1, Max: 128, Regexp: 
nameRegex}
+       versionFuzzyRule := &validate.ValidateRule{Min: 1, Max: 128, Regexp: 
versionFuzzyRegex}
+       instanceRule := &validate.ValidateRule{Length: 64, Regexp: 
simpleNameAllowEmptyRegex}
+
        MicroServiceKeyValidator.AddRule("Environment", 
&validate.ValidateRule{Regexp: envRegex})
        MicroServiceKeyValidator.AddRule("AppId", &validate.ValidateRule{Min: 
1, Max: 160, Regexp: nameRegex})
-       MicroServiceKeyValidator.AddRule("ServiceName", nameRule)
-       MicroServiceKeyValidator.AddRule("Version", &validate.ValidateRule{Min: 
1, Max: 64, Regexp: VersionRegex})
+       MicroServiceKeyValidator.AddRule("ServiceName", serviceNameRule)
+       MicroServiceKeyValidator.AddRule("Version", &validate.ValidateRule{Min: 
1, Max: 64, Regexp: versionRegex})
 
        ServicePathValidator.AddRule("Path", &validate.ValidateRule{Regexp: 
pathRegex})
 
@@ -121,24 +127,24 @@ func init() {
        GetMSExistsReqValidator.AddRules(MicroServiceKeyValidator.GetRules())
        GetMSExistsReqValidator.AddRule("Version", versionFuzzyRule)
 
-       GetSchemaExistsReqValidator.AddRule("ServiceId", ServiceIdRule)
+       GetSchemaExistsReqValidator.AddRule("ServiceId", serviceIdRule)
        GetSchemaExistsReqValidator.AddRule("SchemaId", SchemaIdRule)
 
        var subSchemaValidator validate.Validator
        subSchemaValidator.AddRule("SchemaId", SchemaIdRule)
-       subSchemaValidator.AddRule("Summary", &validate.ValidateRule{Min: 1, 
Max: 512, Regexp: SchemaSummaryRegex})
+       subSchemaValidator.AddRule("Summary", &validate.ValidateRule{Min: 1, 
Max: 512, Regexp: schemaSummaryRegex})
        subSchemaValidator.AddRule("Schema", &validate.ValidateRule{Min: 1})
 
-       SchemasValidator.AddRule("ServiceId", ServiceIdRule)
+       SchemasValidator.AddRule("ServiceId", serviceIdRule)
        SchemasValidator.AddSub("Schemas", &subSchemaValidator)
 
        SchemaValidator.AddRules(subSchemaValidator.GetRules())
-       SchemaValidator.AddRule("ServiceId", ServiceIdRule)
-       SchemaValidator.AddRule("Summary", &validate.ValidateRule{Max: 512, 
Regexp: SchemaSummaryRegex})
+       SchemaValidator.AddRule("ServiceId", serviceIdRule)
+       SchemaValidator.AddRule("Summary", &validate.ValidateRule{Max: 512, 
Regexp: schemaSummaryRegex})
 
-       GetServiceReqValidator.AddRule("ServiceId", ServiceIdRule)
+       GetServiceReqValidator.AddRule("ServiceId", serviceIdRule)
 
-       GetSchemaReqValidator.AddRule("ServiceId", ServiceIdRule)
+       GetSchemaReqValidator.AddRule("ServiceId", serviceIdRule)
        GetSchemaReqValidator.AddRule("SchemaId", SchemaIdRule)
 
        ConsumerMsValidator.AddRules(MicroServiceKeyValidator.GetRules())
@@ -147,7 +153,7 @@ func init() {
        ProviderMsValidator.AddRule("ServiceName", &validate.ValidateRule{Min: 
1, Max: 128, Regexp: nameFuzzyRegex})
        ProviderMsValidator.AddRule("Version", versionFuzzyRule)
 
-       TagReqValidator.AddRule("ServiceId", ServiceIdRule)
+       TagReqValidator.AddRule("ServiceId", serviceIdRule)
        TagReqValidator.AddRule("Tags", TagRule)
        TagReqValidator.AddRule("Keys", &validate.ValidateRule{Regexp: 
tagRegex})
        TagReqValidator.AddRule("Key", &validate.ValidateRule{Regexp: tagRegex})
@@ -159,12 +165,12 @@ func init() {
        HealthCheckInfoValidator.AddRule("Interval", 
&validate.ValidateRule{Max: math.MaxInt32, Regexp: numberRegex})
        HealthCheckInfoValidator.AddRule("Url", &validate.ValidateRule{Regexp: 
pathRegex})
 
-       MicroServiceInstanceValidator.AddRule("InstanceId", 
&validate.ValidateRule{Length: 64, Regexp: simpleNameAllowEmptyRegex})
-       MicroServiceInstanceValidator.AddRule("ServiceId", ServiceIdRule)
+       MicroServiceInstanceValidator.AddRule("InstanceId", instanceRule)
+       MicroServiceInstanceValidator.AddRule("ServiceId", serviceIdRule)
        MicroServiceInstanceValidator.AddRule("Endpoints", 
&validate.ValidateRule{Regexp: epRegex})
        MicroServiceInstanceValidator.AddRule("HostName", 
&validate.ValidateRule{Length: 64, Regexp: simpleNameRegex})
        MicroServiceInstanceValidator.AddSub("HealthCheck", 
&HealthCheckInfoValidator)
-       MicroServiceInstanceValidator.AddRule("Status", InstanceStatusRule)
+       MicroServiceInstanceValidator.AddRule("Status", instanceStatusRule)
        MicroServiceInstanceValidator.AddSub("DataCenterInfo", 
&DataCenterInfoValidator)
 
        DataCenterInfoValidator.AddRule("Name", &validate.ValidateRule{Length: 
128, Regexp: simpleNameRegex})
@@ -176,16 +182,20 @@ func init() {
        ServiceRuleValidator.AddRule("Pattern", &validate.ValidateRule{Max: 64, 
Min: 1})
        ServiceRuleValidator.AddRule("Description", 
MicroServiceValidator.GetRule("Description"))
 
-       FindInstanceReqValidator.AddRule("ConsumerServiceId", ServiceIdRule)
+       FindInstanceReqValidator.AddRule("ConsumerServiceId", serviceIdRule)
        FindInstanceReqValidator.AddRule("AppId", 
MicroServiceKeyValidator.GetRule("AppId"))
        FindInstanceReqValidator.AddRule("ServiceName", 
&validate.ValidateRule{Min: 1, Max: 128, Regexp: serviceNameForFindRegex})
        FindInstanceReqValidator.AddRule("VersionRule", versionFuzzyRule)
        FindInstanceReqValidator.AddRule("Tags", TagRule)
 
-       GetInstanceValidator.AddRule("ConsumerServiceId", ServiceIdRule)
-       GetInstanceValidator.AddRule("ProviderServiceId", ServiceIdRule)
-       GetInstanceValidator.AddRule("ProviderInstanceId", 
&validate.ValidateRule{Min: 1, Max: 64, Regexp: simpleNameAllowEmptyRegex})
+       GetInstanceValidator.AddRule("ConsumerServiceId", serviceIdRule)
+       GetInstanceValidator.AddRule("ProviderServiceId", serviceIdRule)
+       GetInstanceValidator.AddRule("ProviderInstanceId", instanceRule)
        GetInstanceValidator.AddRule("Tags", TagRule)
+
+       UpdateInstanceValidator.AddRule("ServiceId", serviceIdRule)
+       UpdateInstanceValidator.AddRule("InstanceId", instanceRule)
+       UpdateInstanceValidator.AddRule("Status", updateInstStatusRule)
 }
 
 func Validate(v interface{}) error {
@@ -199,12 +209,12 @@ func Validate(v interface{}) error {
                return errors.New("Pointer is nil!")
        }
        switch t := v.(type) {
-       case (*pb.MicroService):
+       case *pb.MicroService:
                return MicroServiceValidator.Validate(v)
-       case *pb.MicroServiceInstance, *pb.UpdateInstanceStatusRequest:
+       case *pb.MicroServiceInstance:
                return MicroServiceInstanceValidator.Validate(v)
-       case (*pb.AddOrUpdateServiceRule):
-               return ServiceRuleValidator.Validate(v)
+       case *pb.FindInstancesRequest:
+               return FindInstanceReqValidator.Validate(v)
        case *pb.GetServiceRequest, *pb.UpdateServicePropsRequest,
                *pb.DeleteServiceRequest, *pb.GetDependenciesRequest,
                *pb.GetAllSchemaRequest:
@@ -218,10 +228,12 @@ func Validate(v interface{}) error {
                return SchemaValidator.Validate(v)
        case *pb.ModifySchemasRequest:
                return SchemasValidator.Validate(v)
-       case *pb.FindInstancesRequest:
-               return FindInstanceReqValidator.Validate(v)
        case *pb.GetOneInstanceRequest, *pb.GetInstancesRequest:
                return GetInstanceValidator.Validate(v)
+       case *pb.UpdateInstanceStatusRequest:
+               return UpdateInstanceValidator.Validate(v)
+       case *pb.AddOrUpdateServiceRule:
+               return ServiceRuleValidator.Validate(v)
        case *pb.GetAppsRequest:
                return MicroServiceKeyValidator.Validate(v)
        default:
diff --git a/server/service/instances.go b/server/service/instances.go
index 1a582c87..48c70296 100644
--- a/server/service/instances.go
+++ b/server/service/instances.go
@@ -357,13 +357,14 @@ func (s *InstanceService) HeartbeatSet(ctx 
context.Context, in *pb.HeartbeatSetR
                }, nil
        }
        domainProject := util.ParseDomainProject(ctx)
-       instanceHbRstArr := []*pb.InstanceHbRst{}
-       existFlag := map[string]bool{}
-       instancesHbRst := make(chan *pb.InstanceHbRst, len(in.Instances))
+
+       heartBeatCount := len(in.Instances)
+       existFlag := make(map[string]bool, heartBeatCount)
+       instancesHbRst := make(chan *pb.InstanceHbRst, heartBeatCount)
        noMultiCounter := 0
        for _, heartbeatElement := range in.Instances {
                if _, ok := 
existFlag[heartbeatElement.ServiceId+heartbeatElement.InstanceId]; ok {
-                       util.Logger().Warnf(nil, "heartbeatset %s/%s multiple", 
heartbeatElement.ServiceId, heartbeatElement.InstanceId)
+                       util.Logger().Warnf(nil, "heartbeat set %s/%s 
multiple", heartbeatElement.ServiceId, heartbeatElement.InstanceId)
                        continue
                } else {
                        
existFlag[heartbeatElement.ServiceId+heartbeatElement.InstanceId] = true
@@ -378,7 +379,7 @@ func (s *InstanceService) HeartbeatSet(ctx context.Context, 
in *pb.HeartbeatSetR
                        _, _, err, _ := serviceUtil.HeartbeatUtil(ctx, 
domainProject, element.ServiceId, element.InstanceId)
                        if err != nil {
                                hbRst.ErrMessage = err.Error()
-                               util.Logger().Errorf(err, "heartbeatset failed, 
%s/%s", element.ServiceId, element.InstanceId)
+                               util.Logger().Errorf(err, "heartbeat set 
failed, %s/%s", element.ServiceId, element.InstanceId)
                        }
                        instancesHbRst <- hbRst
                }(heartbeatElement)
@@ -386,6 +387,7 @@ func (s *InstanceService) HeartbeatSet(ctx context.Context, 
in *pb.HeartbeatSetR
        count := 0
        successFlag := false
        failFlag := false
+       instanceHbRstArr := make([]*pb.InstanceHbRst, 0, heartBeatCount)
        for heartbeat := range instancesHbRst {
                count++
                if len(heartbeat.ErrMessage) != 0 {
@@ -399,15 +401,15 @@ func (s *InstanceService) HeartbeatSet(ctx 
context.Context, in *pb.HeartbeatSetR
                }
        }
        if !failFlag && successFlag {
-               util.Logger().Infof("heartbeatset success")
+               util.Logger().Infof("heartbeat set success")
                return &pb.HeartbeatSetResponse{
-                       Response:  pb.CreateResponse(pb.Response_SUCCESS, 
"Heartbeatset successfully."),
+                       Response:  pb.CreateResponse(pb.Response_SUCCESS, 
"Heartbeat set successfully."),
                        Instances: instanceHbRstArr,
                }, nil
        } else {
-               util.Logger().Errorf(nil, "heartbeatset failed, %v", 
in.Instances)
+               util.Logger().Errorf(nil, "heartbeat set failed, %v", 
in.Instances)
                return &pb.HeartbeatSetResponse{
-                       Response:  
pb.CreateResponse(scerr.ErrInstanceNotExists, "Heartbeatset failed."),
+                       Response:  
pb.CreateResponse(scerr.ErrInstanceNotExists, "Heartbeat set failed."),
                        Instances: instanceHbRstArr,
                }, nil
        }
@@ -454,7 +456,7 @@ func (s *InstanceService) getInstancePreCheck(ctx 
context.Context, in interface{
        if err != nil {
                return scerr.NewError(scerr.ErrInvalidParams, err.Error())
        }
-       var targetDomainProject = util.ParseTargetDomainProject(ctx)
+       targetDomainProject := util.ParseTargetDomainProject(ctx)
        var providerServiceId, consumerServiceId string
        var tags []string
 
@@ -586,7 +588,7 @@ func (s *InstanceService) Find(ctx context.Context, in 
*pb.FindInstancesRequest)
                }, nil
        }
 
-       var instances []*pb.MicroServiceInstance
+       instances := make([]*pb.MicroServiceInstance, 0, 10)
        cloneCtx := ctx
        if s, ok := ctx.Value("noCache").(string); !ok || s != "1" {
                cloneCtx = util.SetContext(util.CloneContext(ctx), "cacheOnly", 
"1")
@@ -635,12 +637,6 @@ func (s *InstanceService) Find(ctx context.Context, in 
*pb.FindInstancesRequest)
 }
 
 func (s *InstanceService) UpdateStatus(ctx context.Context, in 
*pb.UpdateInstanceStatusRequest) (*pb.UpdateInstanceStatusResponse, error) {
-       if in == nil || len(in.ServiceId) == 0 || len(in.InstanceId) == 0 {
-               util.Logger().Errorf(nil, "update instance status failed: 
invalid params.")
-               return &pb.UpdateInstanceStatusResponse{
-                       Response: pb.CreateResponse(scerr.ErrInvalidParams, 
"Request format invalid."),
-               }, nil
-       }
        domainProject := util.ParseDomainProject(ctx)
        updateStatusFlag := util.StringJoin([]string{in.ServiceId, 
in.InstanceId, in.Status}, "/")
        if err := apt.Validate(in); err != nil {
@@ -650,10 +646,7 @@ func (s *InstanceService) UpdateStatus(ctx 
context.Context, in *pb.UpdateInstanc
                }, nil
        }
 
-       var err error
-
-       var instance *pb.MicroServiceInstance
-       instance, err = serviceUtil.GetInstance(ctx, domainProject, 
in.ServiceId, in.InstanceId)
+       instance, err := serviceUtil.GetInstance(ctx, domainProject, 
in.ServiceId, in.InstanceId)
        if err != nil {
                util.Logger().Errorf(err, "update instance status failed, %s: 
get instance from etcd failed.", updateStatusFlag)
                return &pb.UpdateInstanceStatusResponse{
@@ -669,7 +662,7 @@ func (s *InstanceService) UpdateStatus(ctx context.Context, 
in *pb.UpdateInstanc
 
        instance.Status = in.Status
 
-       if err := updateInstance(ctx, domainProject, instance); err != nil {
+       if err := serviceUtil.UpdateInstance(ctx, domainProject, instance); err 
!= nil {
                util.Logger().Errorf(err, "update instance status failed, %s", 
updateStatusFlag)
                resp := &pb.UpdateInstanceStatusResponse{
                        Response: pb.CreateResponseWithSCErr(err),
@@ -711,12 +704,9 @@ func (s *InstanceService) UpdateInstanceProperties(ctx 
context.Context, in *pb.U
                }, nil
        }
 
-       instance.Properties = map[string]string{}
-       for property := range in.Properties {
-               instance.Properties[property] = in.Properties[property]
-       }
+       instance.Properties = in.Properties
 
-       if err := updateInstance(ctx, domainProject, instance); err != nil {
+       if err := serviceUtil.UpdateInstance(ctx, domainProject, instance); err 
!= nil {
                util.Logger().Errorf(err, "update instance properties failed, 
%s", instanceFlag)
                resp := &pb.UpdateInstancePropsResponse{
                        Response: pb.CreateResponseWithSCErr(err),
@@ -733,32 +723,6 @@ func (s *InstanceService) UpdateInstanceProperties(ctx 
context.Context, in *pb.U
        }, nil
 }
 
-func updateInstance(ctx context.Context, domainProject string, instance 
*pb.MicroServiceInstance) *scerr.Error {
-       leaseID, err := serviceUtil.GetLeaseId(ctx, domainProject, 
instance.ServiceId, instance.InstanceId)
-       if err != nil {
-               return scerr.NewError(scerr.ErrInternal, err.Error())
-       }
-       if leaseID == -1 {
-               return scerr.NewError(scerr.ErrInstanceNotExists, "Instance's 
leaseId not exist.")
-       }
-
-       instance.ModTimestamp = strconv.FormatInt(time.Now().Unix(), 10)
-       data, err := json.Marshal(instance)
-       if err != nil {
-               return scerr.NewError(scerr.ErrInternal, err.Error())
-       }
-
-       key := apt.GenerateInstanceKey(domainProject, instance.ServiceId, 
instance.InstanceId)
-       _, err = backend.Registry().Do(ctx,
-               registry.PUT,
-               registry.WithStrKey(key),
-               registry.WithValue(data),
-               registry.WithLease(leaseID))
-       if err != nil {
-               return scerr.NewError(scerr.ErrInternal, err.Error())
-       }
-       return nil
-}
 
 func (s *InstanceService) WatchPreOpera(ctx context.Context, in 
*pb.WatchInstanceRequest) error {
        if in == nil || len(in.SelfServiceId) == 0 {
@@ -826,8 +790,8 @@ func (s *InstanceService) ClusterHealth(ctx 
context.Context) (*pb.GetInstancesRe
                        Response: pb.CreateResponse(scerr.ErrServiceNotExists, 
"Service center serviceId not exist."),
                }, nil
        }
-       instances := []*pb.MicroServiceInstance{}
-       instances, err = serviceUtil.GetAllInstancesOfOneService(ctx, 
domainProject, serviceId)
+
+       instances, err := serviceUtil.GetAllInstancesOfOneService(ctx, 
domainProject, serviceId)
        if err != nil {
                util.Logger().Errorf(err, "health check failed: get service 
center instances failed.")
                return &pb.GetInstancesResponse{
diff --git a/server/service/util/instance_util.go 
b/server/service/util/instance_util.go
index 9ace072d..5c373a95 100644
--- a/server/service/util/instance_util.go
+++ b/server/service/util/instance_util.go
@@ -30,6 +30,7 @@ import (
        "golang.org/x/net/context"
        "strconv"
        "strings"
+       "time"
 )
 
 func GetLeaseId(ctx context.Context, domainProject string, serviceId string, 
instanceId string) (int64, error) {
@@ -252,3 +253,30 @@ func queryServiceInstancesKvs(ctx context.Context, 
serviceId string, rev int64)
        }
        return resp.Kvs, nil
 }
+
+func UpdateInstance(ctx context.Context, domainProject string, instance 
*pb.MicroServiceInstance) *scerr.Error {
+       leaseID, err := GetLeaseId(ctx, domainProject, instance.ServiceId, 
instance.InstanceId)
+       if err != nil {
+               return scerr.NewError(scerr.ErrInternal, err.Error())
+       }
+       if leaseID == -1 {
+               return scerr.NewError(scerr.ErrInstanceNotExists, "Instance's 
leaseId not exist.")
+       }
+
+       instance.ModTimestamp = strconv.FormatInt(time.Now().Unix(), 10)
+       data, err := json.Marshal(instance)
+       if err != nil {
+               return scerr.NewError(scerr.ErrInternal, err.Error())
+       }
+
+       key := apt.GenerateInstanceKey(domainProject, instance.ServiceId, 
instance.InstanceId)
+       _, err = backend.Registry().Do(ctx,
+               registry.PUT,
+               registry.WithStrKey(key),
+               registry.WithValue(data),
+               registry.WithLease(leaseID))
+       if err != nil {
+               return scerr.NewError(scerr.ErrInternal, err.Error())
+       }
+       return nil
+}
\ No newline at end of file


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to