You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2017/12/27 09:54:11 UTC

[incubator-servicecomb-service-center] branch master updated: SCB-135 Statistic data is wrong in govern api when different domains … (#230)

This is an automated email from the ASF dual-hosted git repository.

littlecui pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-service-center.git


The following commit(s) were added to refs/heads/master by this push:
     new 5a55d25  SCB-135 Statistic data is wrong in govern api when different domains … (#230)
5a55d25 is described below

commit 5a55d25c041b5fc62190c8216b4f744a3f7bb3e3
Author: little-cui <su...@qq.com>
AuthorDate: Wed Dec 27 17:54:09 2017 +0800

    SCB-135 Statistic data is wrong in govern api when different domains … (#230)
    
    * SCB-135 Statistic data is wrong in govern api when different domains has the same prefix.
    
    * SCB-129 Do not enable compact mechanism when the interval is empty.
    
    (cherry picked from commit 0428b43)
    
    * SCB-129 Optimize compaction log print.
    
    * SCB-127 only allow shared micro-service instances found in diff domains.
    
    * SCB-127 Optimize v4 context handler.
    
    * Fix the UT failure.
    
    * Optimize etcd.go
    
    * Optimize etcd.go
    
    (cherry picked from commit 19bfb0d)
---
 server/core/info.go                                |  2 +-
 server/govern/service.go                           |  4 ++--
 server/handler/context/context.go                  |  2 --
 server/handler/context/v3.go                       | 18 +++++-----------
 server/handler/context/v4.go                       | 25 ++++++++--------------
 .../infra/registry/embededetcd/embededetcd.go      |  7 ++----
 server/plugin/infra/registry/etcd/etcd.go          | 19 ++++++----------
 server/rest/controller/v4/instance_controller.go   |  9 ++++++++
 server/server.go                                   |  6 +++---
 server/service/instances.go                        |  9 +++++---
 server/service/microservices.go                    |  2 +-
 server/service/util/rule_util.go                   |  3 +--
 12 files changed, 46 insertions(+), 60 deletions(-)

diff --git a/server/core/info.go b/server/core/info.go
index 314c572..74edc1b 100644
--- a/server/core/info.go
+++ b/server/core/info.go
@@ -61,7 +61,7 @@ func newInfo() *pb.ServerInformation {
 
 			AutoSyncInterval:  beego.AppConfig.DefaultString("auto_sync_interval", "30s"),
 			CompactIndexDelta: beego.AppConfig.DefaultInt64("compact_index_delta", 100),
-			CompactInterval:   beego.AppConfig.DefaultString("compact_interval", "12h"),
+			CompactInterval:   beego.AppConfig.String("compact_interval"),
 
 			LoggerName:     beego.AppConfig.String("component_name"),
 			LogRotateSize:  maxLogFileSize,
diff --git a/server/govern/service.go b/server/govern/service.go
index eea638f..e059aab 100644
--- a/server/govern/service.go
+++ b/server/govern/service.go
@@ -368,7 +368,7 @@ func statistics(ctx context.Context) (*pb.Statistics, error) {
 	opts := serviceUtil.FromContext(ctx)
 
 	// services
-	key := apt.GetServiceIndexRootKey(domainProject)
+	key := apt.GetServiceIndexRootKey(domainProject) + "/"
 	svcOpts := append(opts,
 		registry.WithStrKey(key),
 		registry.WithPrefix(),
@@ -408,7 +408,7 @@ func statistics(ctx context.Context) (*pb.Statistics, error) {
 	result.Apps.Count = int64(len(app))
 
 	// instance
-	key = apt.GetInstanceRootKey(domainProject)
+	key = apt.GetInstanceRootKey(domainProject) + "/"
 	instOpts := append(opts,
 		registry.WithStrKey(key),
 		registry.WithPrefix(),
diff --git a/server/handler/context/context.go b/server/handler/context/context.go
index 85e52b9..7fd42fa 100644
--- a/server/handler/context/context.go
+++ b/server/handler/context/context.go
@@ -40,8 +40,6 @@ func (c *ContextHandler) Handle(i *chain.Invocation) {
 	case v3.IsMatch(r):
 		err = v3.Do(r)
 	case v4.IsMatch(r):
-		util.SetRequestContext(r, "target-domain", "default")
-		util.SetRequestContext(r, "target-project", "default")
 		err = v4.Do(r)
 	}
 
diff --git a/server/handler/context/v3.go b/server/handler/context/v3.go
index d782684..2be4e91 100644
--- a/server/handler/context/v3.go
+++ b/server/handler/context/v3.go
@@ -34,13 +34,12 @@ func (v *v3Context) IsMatch(r *http.Request) bool {
 func (v *v3Context) Do(r *http.Request) error {
 	ctx := r.Context()
 
-	domain := r.Header.Get("X-Tenant-Name")
-	if len(domain) == 0 {
-		domain = r.Header.Get("X-Domain-Name")
-	}
-
-	// self domain
 	if len(util.ParseDomain(ctx)) == 0 {
+		domain := r.Header.Get("X-Tenant-Name")
+		if len(domain) == 0 {
+			domain = r.Header.Get("X-Domain-Name")
+		}
+
 		if len(domain) == 0 {
 			err := errors.New("Header does not contain domain.")
 			util.Logger().Errorf(err, "Invalid Request URI %s", r.RequestURI)
@@ -52,12 +51,5 @@ func (v *v3Context) Do(r *http.Request) error {
 	if len(util.ParseProject(ctx)) == 0 {
 		util.SetRequestContext(r, "project", core.REGISTRY_PROJECT)
 	}
-	// target domain
-	if len(util.ParseTargetDomain(ctx)) == 0 {
-		util.SetRequestContext(r, "target-domain", domain)
-	}
-	if len(util.ParseTargetProject(ctx)) == 0 {
-		util.SetRequestContext(r, "target-project", core.REGISTRY_PROJECT)
-	}
 	return nil
 }
diff --git a/server/handler/context/v4.go b/server/handler/context/v4.go
index 0a22e30..5b13495 100644
--- a/server/handler/context/v4.go
+++ b/server/handler/context/v4.go
@@ -35,15 +35,18 @@ func (v *v4Context) IsMatch(r *http.Request) bool {
 func (v *v4Context) Do(r *http.Request) error {
 	ctx := r.Context()
 
-	domain := r.Header.Get("X-Domain-Name")
-	path, err := url.PathUnescape(r.RequestURI)
-	if err != nil {
-		util.Logger().Errorf(err, "Invalid Request URI %s", r.RequestURI)
-		return err
+	if len(util.ParseProject(ctx)) == 0 {
+		path, err := url.PathUnescape(r.RequestURI)
+		if err != nil {
+			util.Logger().Errorf(err, "Invalid Request URI %s", r.RequestURI)
+			return err
+		}
+
+		util.SetRequestContext(r, "project", v.parseProjectFromPath(path))
 	}
 
-	// self domain
 	if len(util.ParseDomain(ctx)) == 0 {
+		domain := r.Header.Get("X-Domain-Name")
 		if len(domain) == 0 {
 			err := errors.New("Header does not contain domain.")
 			util.Logger().Errorf(err, "Invalid Request URI %s", r.RequestURI)
@@ -51,16 +54,6 @@ func (v *v4Context) Do(r *http.Request) error {
 		}
 		util.SetRequestContext(r, "domain", domain)
 	}
-	if len(util.ParseProject(ctx)) == 0 {
-		util.SetRequestContext(r, "project", v.parseProjectFromPath(path))
-	}
-	// target domain
-	if len(util.ParseTargetDomain(ctx)) == 0 {
-		util.SetRequestContext(r, "target-domain", domain)
-	}
-	if len(util.ParseTargetProject(ctx)) == 0 {
-		util.SetRequestContext(r, "target-project", v.parseProjectFromPath(path))
-	}
 	return nil
 }
 
diff --git a/server/plugin/infra/registry/embededetcd/embededetcd.go b/server/plugin/infra/registry/embededetcd/embededetcd.go
index d5eb876..03b8585 100644
--- a/server/plugin/infra/registry/embededetcd/embededetcd.go
+++ b/server/plugin/infra/registry/embededetcd/embededetcd.go
@@ -224,10 +224,7 @@ func (s *EtcdEmbed) toCompares(cmps []registry.CompareOp) []*etcdserverpb.Compar
 }
 
 func (s *EtcdEmbed) Compact(ctx context.Context, reserve int64) error {
-	otCtx, cancel := registry.WithTimeout(ctx)
-	defer cancel()
-
-	curRev := s.getLeaderCurrentRevision(otCtx)
+	curRev := s.getLeaderCurrentRevision(ctx)
 	revToCompact := max(0, curRev-reserve)
 	if revToCompact <= 0 {
 		util.Logger().Infof("revision is %d, <=%d, no nead to compact", curRev, reserve)
@@ -235,7 +232,7 @@ func (s *EtcdEmbed) Compact(ctx context.Context, reserve int64) error {
 	}
 
 	util.Logger().Infof("Compacting... revision is %d(current: %d, reserve %d)", revToCompact, curRev, reserve)
-	_, err := s.Server.Server.Compact(otCtx, &etcdserverpb.CompactionRequest{
+	_, err := s.Server.Server.Compact(ctx, &etcdserverpb.CompactionRequest{
 		Revision: revToCompact,
 		Physical: true,
 	})
diff --git a/server/plugin/infra/registry/etcd/etcd.go b/server/plugin/infra/registry/etcd/etcd.go
index f6796fc..c6f7b57 100644
--- a/server/plugin/infra/registry/etcd/etcd.go
+++ b/server/plugin/infra/registry/etcd/etcd.go
@@ -76,10 +76,7 @@ func (c *EtcdClient) Compact(ctx context.Context, reserve int64) error {
 		return nil
 	}
 
-	otCtx, cancel := registry.WithTimeout(ctx)
-	defer cancel()
-
-	_, err := c.Client.Compact(otCtx, revToCompact, clientv3.WithCompactPhysical())
+	_, err := c.Client.Compact(ctx, revToCompact, clientv3.WithCompactPhysical())
 	if err != nil {
 		util.Logger().Errorf(err, "Compact %s failed, revision is %d(current: %d, reserve %d)",
 			eps, revToCompact, curRev, reserve)
@@ -88,7 +85,7 @@ func (c *EtcdClient) Compact(ctx context.Context, reserve int64) error {
 	util.Logger().Infof("Compacted %s, revision is %d(current: %d, reserve %d)", eps, revToCompact, curRev, reserve)
 
 	for _, ep := range eps {
-		_, err := c.Client.Defragment(otCtx, ep)
+		_, err := c.Client.Defragment(ctx, ep)
 		if err != nil {
 			util.Logger().Errorf(err, "Defrag %s failed", ep)
 			continue
@@ -103,12 +100,9 @@ func (c *EtcdClient) getLeaderCurrentRevision(ctx context.Context) int64 {
 	eps := c.Client.Endpoints()
 	curRev := int64(0)
 	for _, ep := range eps {
-		otCtx, cancel := registry.WithTimeout(ctx)
-
-		resp, err := c.Client.Status(otCtx, ep)
+		resp, err := c.Client.Status(ctx, ep)
 		if err != nil {
 			util.Logger().Error(fmt.Sprintf("Compact error ,can not get status from %s", ep), err)
-			cancel()
 			continue
 		}
 		curRev = resp.Header.Revision
@@ -116,8 +110,6 @@ func (c *EtcdClient) getLeaderCurrentRevision(ctx context.Context) int64 {
 			util.Logger().Infof("Get leader endpoint: %s, revision is %d", ep, curRev)
 			break
 		}
-
-		cancel()
 	}
 	return curRev
 }
@@ -608,7 +600,10 @@ func NewRegistry() mgr.PluginInstance {
 
 	}
 
-	inv, _ := time.ParseDuration(core.ServerInfo.Config.AutoSyncInterval)
+	inv, err := time.ParseDuration(core.ServerInfo.Config.AutoSyncInterval)
+	if err != nil {
+		util.Logger().Errorf(err, "invalid auto sync interval '%s'.", core.ServerInfo.Config.AutoSyncInterval)
+	}
 	client, err := newClient(endpoints, inv)
 	if err != nil {
 		util.Logger().Errorf(err, "get etcd client %v failed.", endpoints)
diff --git a/server/rest/controller/v4/instance_controller.go b/server/rest/controller/v4/instance_controller.go
index d62cf03..0f23320 100644
--- a/server/rest/controller/v4/instance_controller.go
+++ b/server/rest/controller/v4/instance_controller.go
@@ -130,6 +130,15 @@ func (this *MicroServiceInstanceService) FindInstances(w http.ResponseWriter, r
 		VersionRule:       r.URL.Query().Get("version"),
 		Tags:              ids,
 	}
+
+	targetDomain := r.Header.Get("X-Domain-Name")
+	targetProject := r.URL.Query().Get(":project")
+	if len(targetProject) == 0 {
+		targetProject = core.REGISTRY_PROJECT
+	}
+	util.SetRequestContext(r, "target-domain", targetDomain)
+	util.SetRequestContext(r, "target-project", targetProject)
+
 	resp, _ := core.InstanceAPI.Find(r.Context(), request)
 	respInternal := resp.Response
 	resp.Response = nil
diff --git a/server/server.go b/server/server.go
index b8ab6f5..7f814fc 100644
--- a/server/server.go
+++ b/server/server.go
@@ -111,7 +111,7 @@ func (s *ServiceCenterServer) initialize() {
 
 func (s *ServiceCenterServer) autoCompactBackend() {
 	delta := core.ServerInfo.Config.CompactIndexDelta
-	if delta <= 0 {
+	if delta <= 0 || len(core.ServerInfo.Config.CompactInterval) == 0 {
 		return
 	}
 	interval, err := time.ParseDuration(core.ServerInfo.Config.CompactInterval)
@@ -120,8 +120,8 @@ func (s *ServiceCenterServer) autoCompactBackend() {
 		interval = 12 * time.Hour
 	}
 	util.Go(func(stopCh <-chan struct{}) {
-		util.Logger().Infof("start the automatic compact mechanism, compact once every %s",
-			core.ServerInfo.Config.CompactInterval)
+		util.Logger().Infof("start the automatic compact mechanism, compact once every %s, reserve %d",
+			core.ServerInfo.Config.CompactInterval, delta)
 		for {
 			select {
 			case <-stopCh:
diff --git a/server/service/instances.go b/server/service/instances.go
index 8edde82..3a22549 100644
--- a/server/service/instances.go
+++ b/server/service/instances.go
@@ -547,9 +547,8 @@ func (s *InstanceService) Find(ctx context.Context, in *pb.FindInstancesRequest)
 	}
 
 	domainProject := util.ParseDomainProject(ctx)
-	targetDomainProject := util.ParseTargetDomainProject(ctx)
-
 	findFlag := fmt.Sprintf("consumer %s --> provider %s/%s/%s", in.ConsumerServiceId, in.AppId, in.ServiceName, in.VersionRule)
+
 	service, err := serviceUtil.GetService(ctx, domainProject, in.ConsumerServiceId)
 	if err != nil {
 		util.Logger().Errorf(err, "find instance failed, %s: get consumer failed.", findFlag)
@@ -564,6 +563,7 @@ func (s *InstanceService) Find(ctx context.Context, in *pb.FindInstancesRequest)
 		}, nil
 	}
 
+	targetDomainProject := util.ParseTargetDomainProject(ctx)
 	provider := &pb.MicroServiceKey{
 		Tenant:      targetDomainProject,
 		Environment: service.Environment,
@@ -571,10 +571,13 @@ func (s *InstanceService) Find(ctx context.Context, in *pb.FindInstancesRequest)
 		ServiceName: in.ServiceName,
 		Alias:       in.ServiceName,
 	}
-
 	if apt.IsShared(provider) {
 		// it means the shared micro-services must be the same env with SC.
 		provider.Environment = apt.Service.Environment
+	} else {
+		// only allow shared micro-service instances found in different domains.
+		targetDomainProject = domainProject
+		provider.Tenant = domainProject
 	}
 
 	// 版本规则
diff --git a/server/service/microservices.go b/server/service/microservices.go
index 0f304a8..aadbcea 100644
--- a/server/service/microservices.go
+++ b/server/service/microservices.go
@@ -213,7 +213,7 @@ func (s *MicroServiceService) DeleteServicePri(ctx context.Context, ServiceId st
 	if ServiceId == apt.Service.ServiceId {
 		err := fmt.Errorf("Not allow to delete service center")
 		util.Logger().Errorf(err, "%s microservice failed, serviceId is %s", title, ServiceId)
-		return pb.CreateResponse(scerr.ErrInvalidParams, err.Error()), err
+		return pb.CreateResponse(scerr.ErrInvalidParams, err.Error()), nil
 	}
 
 	service, err := serviceUtil.GetService(ctx, domainProject, ServiceId)
diff --git a/server/service/util/rule_util.go b/server/service/util/rule_util.go
index 50041b9..b8b0774 100644
--- a/server/service/util/rule_util.go
+++ b/server/service/util/rule_util.go
@@ -154,8 +154,7 @@ func AllowAcrossDimension(ctx context.Context, providerService *pb.MicroService,
 		}
 	}
 
-	targetDomainProject := util.ParseTargetDomainProject(ctx)
-	if !apt.IsShared(pb.MicroServiceToKey(targetDomainProject, providerService)) &&
+	if !apt.IsShared(pb.MicroServiceToKey(util.ParseTargetDomainProject(ctx), providerService)) &&
 		providerService.Environment != consumerService.Environment {
 		return fmt.Errorf("not allow across environment access")
 	}

-- 
To stop receiving notification emails like this one, please contact
['"commits@servicecomb.apache.org" <co...@servicecomb.apache.org>'].