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 2022/05/25 09:02:36 UTC

[servicecomb-service-center] branch v1.x updated: Revert "Change: Remove the microserivce auto clear mechanism (#1074)"

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

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


The following commit(s) were added to refs/heads/v1.x by this push:
     new 452bfcbf Revert "Change: Remove the microserivce auto clear mechanism (#1074)"
452bfcbf is described below

commit 452bfcbf5e3314f329af2024eed6e638360869c0
Author: little-cui <su...@qq.com>
AuthorDate: Wed May 25 16:59:03 2022 +0800

    Revert "Change: Remove the microserivce auto clear mechanism (#1074)"
    
    This reverts commit 54fd94abb1b7e6db732f6e4eb8f836dc640db9e3.
---
 server/core/config.go      | 26 +++++++++++++++++++++++++-
 server/core/proto/types.go |  5 +++++
 server/server.go           | 37 +++++++++++++++++++++++++++++++++++++
 3 files changed, 67 insertions(+), 1 deletion(-)

diff --git a/server/core/config.go b/server/core/config.go
index a3457bb4..2e14a33e 100644
--- a/server/core/config.go
+++ b/server/core/config.go
@@ -34,7 +34,17 @@ import (
 
 const (
 	InitVersion = "0"
-	minCacheTTL = 5 * time.Minute
+
+	defaultServiceClearInterval = 12 * time.Hour //0.5 day
+	defaultServiceTTL           = 24 * time.Hour //1 day
+
+	minServiceClearInterval = 30 * time.Second
+	minServiceTTL           = 30 * time.Second
+	minCacheTTL             = 5 * time.Minute
+
+	maxServiceClearInterval = 24 * time.Hour       //1 day
+	maxServiceTTL           = 24 * 365 * time.Hour //1 year
+
 )
 
 var ServerInfo = proto.NewServerInformation()
@@ -67,6 +77,16 @@ func newInfo() proto.ServerInformation {
 		maxLogBackupCount = 50
 	}
 
+	serviceClearInterval, err := time.ParseDuration(os.Getenv("SERVICE_CLEAR_INTERVAL"))
+	if err != nil || serviceClearInterval < minServiceClearInterval || serviceClearInterval > maxServiceClearInterval {
+		serviceClearInterval = defaultServiceClearInterval
+	}
+
+	serviceTTL, err := time.ParseDuration(os.Getenv("SERVICE_TTL"))
+	if err != nil || serviceTTL < minServiceTTL || serviceTTL > maxServiceTTL {
+		serviceTTL = defaultServiceTTL
+	}
+
 	cacheTTL, err := time.ParseDuration(
 		util.GetEnvString("CACHE_TTL", beego.AppConfig.DefaultString("cache_ttl", "")))
 	if err == nil && cacheTTL < minCacheTTL {
@@ -117,6 +137,10 @@ func newInfo() proto.ServerInformation {
 			CacheTTL:     cacheTTL,
 			SelfRegister: beego.AppConfig.DefaultInt("self_register", 1) != 0,
 
+			ServiceClearEnabled:  os.Getenv("SERVICE_CLEAR_ENABLED") == "true",
+			ServiceClearInterval: serviceClearInterval,
+			ServiceTTL:           serviceTTL,
+
 			SchemaDisable: os.Getenv("SCHEMA_DISABLE") == "true",
 		},
 	}
diff --git a/server/core/proto/types.go b/server/core/proto/types.go
index 1c722b0e..3ea9a557 100644
--- a/server/core/proto/types.go
+++ b/server/core/proto/types.go
@@ -62,6 +62,11 @@ type ServerConfig struct {
 
 	SelfRegister bool `json:"selfRegister"`
 
+	//clear no-instance services
+	ServiceClearEnabled  bool          `json:"serviceClearEnabled"`
+	ServiceClearInterval time.Duration `json:"serviceClearInterval"`
+	//if a service's existence time reaches this value, it can be cleared
+	ServiceTTL time.Duration `json:"serviceTTL"`
 	//CacheTTL is the ttl of cache
 	CacheTTL time.Duration `json:"cacheTTL"`
 
diff --git a/server/server.go b/server/server.go
index 2e257f58..e0609a37 100644
--- a/server/server.go
+++ b/server/server.go
@@ -36,6 +36,7 @@ import (
 	"github.com/apache/servicecomb-service-center/server/plugin"
 	"github.com/apache/servicecomb-service-center/server/service/rbac"
 	serviceUtil "github.com/apache/servicecomb-service-center/server/service/util"
+	"github.com/apache/servicecomb-service-center/server/task"
 	"github.com/apache/servicecomb-service-center/version"
 	"github.com/astaxie/beego"
 )
@@ -148,6 +149,40 @@ func (s *ServiceCenterServer) compactBackendService() {
 	})
 }
 
+// clear services who have no instance
+func (s *ServiceCenterServer) clearNoInstanceServices() {
+	if !core.ServerInfo.Config.ServiceClearEnabled {
+		return
+	}
+	log.Infof("service clear enabled, interval: %s, service TTL: %s",
+		core.ServerInfo.Config.ServiceClearInterval,
+		core.ServerInfo.Config.ServiceTTL)
+
+	s.goroutine.Do(func(ctx context.Context) {
+		for {
+			select {
+			case <-ctx.Done():
+				return
+			case <-time.After(core.ServerInfo.Config.ServiceClearInterval):
+				lock, err := mux.Try(mux.ServiceClearLock)
+				if err != nil {
+					log.Errorf(err, "can not clear no instance services by this service center instance now")
+					continue
+				}
+				err = task.ClearNoInstanceServices(core.ServerInfo.Config.ServiceTTL)
+				if err := lock.Unlock(); err != nil {
+					log.Error("", err)
+				}
+				if err != nil {
+					log.Errorf(err, "no-instance services cleanup failed")
+					continue
+				}
+				log.Info("no-instance services cleanup succeed")
+			}
+		}
+	})
+}
+
 func (s *ServiceCenterServer) initialize() {
 	s.cacheService = backend.Store()
 	s.apiService = GetAPIServer()
@@ -174,6 +209,8 @@ func (s *ServiceCenterServer) startServices() {
 	if buildin != beego.AppConfig.DefaultString("registry_plugin", buildin) {
 		// compact backend automatically
 		s.compactBackendService()
+		// clean no-instance services automatically
+		s.clearNoInstanceServices()
 	}
 
 	// api service