asifdxtreme closed pull request #346: SCB-565 Register instance with the same
id will create redundant endpoint index data
URL: https://github.com/apache/incubator-servicecomb-service-center/pull/346
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/server/service/instances.go b/server/service/instances.go
index 0e6e03ab..e65bcb41 100644
--- a/server/service/instances.go
+++ b/server/service/instances.go
@@ -106,7 +106,7 @@ func (s *InstanceService) Register(ctx context.Context, in
*pb.RegisterInstanceR
}
//允许自定义id
//如果没填写 并且endpoints沒重復,則产生新的全局instance id
- oldInstanceId, checkErr := serviceUtil.CheckEndPoints(ctx, in.Instance)
+ oldInstanceId, checkErr := serviceUtil.InstanceExist(ctx, in.Instance)
if checkErr != nil {
util.Logger().Errorf(checkErr, "check endpoints index failed,
service %s, operator %s.",
instanceFlag, remoteIP)
@@ -232,7 +232,7 @@ func (s *InstanceService) Unregister(ctx context.Context,
in *pb.UnregisterInsta
instanceFlag := util.StringJoin([]string{serviceId, instanceId}, "/")
remoteIP := util.GetIPFromContext(ctx)
- isExist, err := serviceUtil.InstanceExist(ctx, domainProject,
serviceId, instanceId)
+ isExist, err := serviceUtil.InstanceExistById(ctx, domainProject,
serviceId, instanceId)
if err != nil {
util.Logger().Errorf(err, "unregister instance failed, instance
%s, operator %s: query instance failed.", instanceFlag, remoteIP)
return &pb.UnregisterInstanceResponse{
diff --git a/server/service/util/instance_util.go
b/server/service/util/instance_util.go
index c45845d7..a6a0b8b8 100644
--- a/server/service/util/instance_util.go
+++ b/server/service/util/instance_util.go
@@ -103,7 +103,7 @@ func GetInstanceCountOfOneService(ctx context.Context,
domainProject string, ser
return resp.Count, nil
}
-func InstanceExist(ctx context.Context, domainProject string, serviceId
string, instanceId string) (bool, error) {
+func InstanceExistById(ctx context.Context, domainProject string, serviceId
string, instanceId string) (bool, error) {
opts := append(FromContext(ctx),
registry.WithStrKey(apt.GenerateInstanceKey(domainProject,
serviceId, instanceId)),
registry.WithCountOnly())
@@ -117,8 +117,20 @@ func InstanceExist(ctx context.Context, domainProject
string, serviceId string,
return true, nil
}
-func CheckEndPoints(ctx context.Context, instance *pb.MicroServiceInstance)
(string, *scerr.Error) {
+func InstanceExist(ctx context.Context, instance *pb.MicroServiceInstance)
(string, *scerr.Error) {
domainProject := util.ParseDomainProject(ctx)
+ // check id index
+ if len(instance.InstanceId) > 0 {
+ exist, err := InstanceExistById(ctx, domainProject,
instance.ServiceId, instance.InstanceId)
+ if err != nil {
+ return "", scerr.NewError(scerr.ErrInternal,
err.Error())
+ }
+ if exist {
+ return instance.InstanceId, nil
+ }
+ }
+
+ // check endpoint index
resp, err := store.Store().Endpoints().Search(ctx,
registry.WithStrKey(apt.GenerateEndpointsIndexKey(domainProject, instance)))
if err != nil {
diff --git a/server/service/util/instance_util_test.go
b/server/service/util/instance_util_test.go
index 3293f39b..3e30ea67 100644
--- a/server/service/util/instance_util_test.go
+++ b/server/service/util/instance_util_test.go
@@ -65,24 +65,31 @@ func TestGetInstance(t *testing.T) {
}
}
-func TestInstanceExist(t *testing.T) {
- _, err := InstanceExist(util.SetContext(context.Background(),
"cacheOnly", "1"), "", "", "")
+func TestInstanceExistById(t *testing.T) {
+ _, err := InstanceExistById(util.SetContext(context.Background(),
"cacheOnly", "1"), "", "", "")
if err != nil {
- t.Fatalf(`InstanceExist WithCacheOnly failed`)
+ t.Fatalf(`InstanceExistById WithCacheOnly failed`)
}
- _, err = InstanceExist(context.Background(), "", "", "")
+ _, err = InstanceExistById(context.Background(), "", "", "")
if err == nil {
- t.Fatalf(`InstanceExist failed`)
+ t.Fatalf(`InstanceExistById failed`)
}
}
-func TestCheckEndPoints(t *testing.T) {
- _, err := CheckEndPoints(context.Background(),
&proto.MicroServiceInstance{
+func TestInstanceExist(t *testing.T) {
+ _, err := InstanceExist(context.Background(),
&proto.MicroServiceInstance{
ServiceId: "a",
})
if err == nil {
- t.Fatalf(`CheckEndPoints failed`)
+ t.Fatalf(`InstanceExist endpoint failed`)
+ }
+ _, err = InstanceExist(context.Background(),
&proto.MicroServiceInstance{
+ ServiceId: "a",
+ InstanceId: "a",
+ })
+ if err == nil {
+ t.Fatalf(`InstanceExist instanceId failed`)
}
}
----------------------------------------------------------------
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:
[email protected]
With regards,
Apache Git Services