You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by as...@apache.org on 2018/08/20 14:18:16 UTC

[incubator-servicecomb-service-center] branch master updated: Extend the admin dump api (#420)

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

asifdxtreme 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 9f55487  Extend the admin dump api (#420)
9f55487 is described below

commit 9f55487b6ed2a256a5d66fe028c4e986961921e9
Author: little-cui <su...@qq.com>
AuthorDate: Mon Aug 20 22:18:15 2018 +0800

    Extend the admin dump api (#420)
---
 pkg/chain/callback.go                        | 10 +------
 pkg/chain/chain.go                           | 15 ----------
 pkg/chain/chain_test.go                      | 34 ++++++++-------------
 pkg/chain/invocation.go                      | 11 +++++++
 server/admin/model/dump.go                   |  8 +++--
 server/admin/service.go                      | 22 ++++++++++++--
 server/core/swagger/v4.yaml                  | 45 ++++++++++++++++++++++++++++
 server/rest/controller/v4/main_controller.go | 21 ++++++-------
 8 files changed, 105 insertions(+), 61 deletions(-)

diff --git a/pkg/chain/callback.go b/pkg/chain/callback.go
index e48e16a..27e10c7 100644
--- a/pkg/chain/callback.go
+++ b/pkg/chain/callback.go
@@ -47,19 +47,11 @@ type Callback struct {
 func (cb *Callback) Invoke(r Result) {
 	if cb.Async {
 		pool.Do(func(_ context.Context) {
-			cb.syncInvoke(r)
+			cb.Func(r)
 		})
 		return
 	}
-	cb.syncInvoke(r)
-}
-
-func (cb *Callback) syncInvoke(r Result) {
 	defer log.Recover()
-	if cb.Func == nil {
-		log.Errorf(nil, "Callback function is nil. result: %s,", r)
-		return
-	}
 	cb.Func(r)
 }
 
diff --git a/pkg/chain/chain.go b/pkg/chain/chain.go
index 51441fe..a1ddd30 100644
--- a/pkg/chain/chain.go
+++ b/pkg/chain/chain.go
@@ -16,11 +16,6 @@
  */
 package chain
 
-import (
-	errorsEx "github.com/apache/incubator-servicecomb-service-center/pkg/errors"
-	"github.com/apache/incubator-servicecomb-service-center/pkg/log"
-)
-
 type Chain struct {
 	name         string
 	handlers     []Handler
@@ -38,16 +33,6 @@ func (c *Chain) Name() string {
 }
 
 func (c *Chain) syncNext(i *Invocation) {
-	defer func() {
-		itf := recover()
-		if itf == nil {
-			return
-		}
-		log.LogPanic(itf)
-
-		i.Fail(errorsEx.RaiseError(itf))
-	}()
-
 	if c.currentIndex >= len(c.handlers)-1 {
 		i.Success()
 		return
diff --git a/pkg/chain/chain_test.go b/pkg/chain/chain_test.go
index d053a81..bb617ed 100644
--- a/pkg/chain/chain_test.go
+++ b/pkg/chain/chain_test.go
@@ -21,11 +21,12 @@ import (
 	"errors"
 	"github.com/apache/incubator-servicecomb-service-center/pkg/util"
 	"testing"
+	"time"
 )
 
 const (
 	times = 1000000
-	count = 100
+	count = 10
 )
 
 func init() {
@@ -73,26 +74,13 @@ func BenchmarkChain(b *testing.B) {
 
 	b.N = times
 	b.ResetTimer()
-	b.RunParallel(func(pb *testing.PB) {
-		for pb.Next() {
-			inv := NewInvocation(ctx, NewChain("_bench_chain_", Handlers("_bench_handlers_")))
-			inv.Invoke(f)
-		}
-	})
-	b.ReportAllocs()
-	// 1000000	      6607 ns/op	      80 B/op	       1 allocs/op
-}
+	for i := 0; i < b.N; i++ {
+		inv := NewInvocation(ctx, NewChain("_bench_chain_", Handlers("_bench_handlers_")))
+		inv.Invoke(f)
 
-func BenchmarkSync(b *testing.B) {
-	b.N = times
-	b.ResetTimer()
-	b.RunParallel(func(pb *testing.PB) {
-		for pb.Next() {
-			syncFunc(0)
-		}
-	})
+	}
 	b.ReportAllocs()
-	// 1000000	        46.9 ns/op	       0 B/op	       0 allocs/op
+	// 1000000	       735 ns/op	      80 B/op	       1 allocs/op
 }
 
 type mockHandler struct {
@@ -112,7 +100,8 @@ func (h *mockHandler) Handle(i *Invocation) {
 	case 4:
 		i.Next(WithAsyncFunc(func(r Result) {
 			i.WithContext("x", x*x)
-			i.Context().Value("ch").(chan struct{}) <- struct{}{}
+			ch, _ := i.Context().Value("ch").(chan struct{})
+			ch <- struct{}{}
 		}))
 	case 5:
 		panic(errors.New("error"))
@@ -181,7 +170,7 @@ func TestChain_Next(t *testing.T) {
 		t.Fatalf("TestChain_Next")
 	}
 
-	x = 4
+	x = 4 // async call back
 	ch = NewChain("test", hs)
 	i = NewInvocation(context.Background(), ch)
 	i.WithContext("x", x)
@@ -216,7 +205,7 @@ func TestChain_Next(t *testing.T) {
 		}
 	})
 
-	x = 7
+	x = 7 // async call back
 	ch = NewChain("test", hs)
 	i = NewInvocation(context.Background(), ch)
 	i.WithContext("x", x)
@@ -225,4 +214,5 @@ func TestChain_Next(t *testing.T) {
 			t.Fatalf("TestChain_Next")
 		}
 	})
+	<-time.After(500 * time.Millisecond)
 }
diff --git a/pkg/chain/invocation.go b/pkg/chain/invocation.go
index bb024ca..457dc36 100644
--- a/pkg/chain/invocation.go
+++ b/pkg/chain/invocation.go
@@ -17,6 +17,8 @@
 package chain
 
 import (
+	errorsEx "github.com/apache/incubator-servicecomb-service-center/pkg/errors"
+	"github.com/apache/incubator-servicecomb-service-center/pkg/log"
 	"github.com/apache/incubator-servicecomb-service-center/pkg/util"
 	"golang.org/x/net/context"
 )
@@ -94,6 +96,15 @@ func callback(f CallbackFunc, async bool, r Result) {
 }
 
 func (i *Invocation) Invoke(f CallbackFunc) {
+	defer func() {
+		itf := recover()
+		if itf == nil {
+			return
+		}
+		log.LogPanic(itf)
+
+		i.Fail(errorsEx.RaiseError(itf))
+	}()
 	i.Func = f
 	i.chain.Next(i)
 }
diff --git a/server/admin/model/dump.go b/server/admin/model/dump.go
index fb91411..8c9ea87 100644
--- a/server/admin/model/dump.go
+++ b/server/admin/model/dump.go
@@ -18,6 +18,7 @@ package model
 
 import (
 	pb "github.com/apache/incubator-servicecomb-service-center/server/core/proto"
+	"github.com/apache/incubator-servicecomb-service-center/version"
 )
 
 type Getter interface {
@@ -180,6 +181,9 @@ type DumpRequest struct {
 }
 
 type DumpResponse struct {
-	Response *pb.Response `json:"response,omitempty"`
-	Cache    *Cache       `json:"cache,omitempty"`
+	Response     *pb.Response        `json:"response,omitempty"`
+	Info         *version.VersionSet `json:"info,omitempty"`
+	Config       *pb.ServerConfig    `json:"config,omitempty"`
+	Environments map[string]string   `json:"environments,omitempty"`
+	Cache        *Cache              `json:"cache,omitempty"`
 }
diff --git a/server/admin/service.go b/server/admin/service.go
index 2a6554e..82322ef 100644
--- a/server/admin/service.go
+++ b/server/admin/service.go
@@ -24,10 +24,23 @@ import (
 	"github.com/apache/incubator-servicecomb-service-center/server/core/backend"
 	pb "github.com/apache/incubator-servicecomb-service-center/server/core/proto"
 	scerr "github.com/apache/incubator-servicecomb-service-center/server/error"
+	"github.com/apache/incubator-servicecomb-service-center/version"
 	"golang.org/x/net/context"
+	"os"
+	"strings"
 )
 
-var AdminServiceAPI = &AdminService{}
+var (
+	AdminServiceAPI = &AdminService{}
+	environments    = make(map[string]string)
+)
+
+func init() {
+	for _, kv := range os.Environ() {
+		arr := strings.Split(kv, "=")
+		environments[arr[0]] = arr[1]
+	}
+}
 
 type AdminService struct {
 }
@@ -46,8 +59,11 @@ func (service *AdminService) Dump(ctx context.Context, in *model.DumpRequest) (*
 	service.dumpAll(ctx, &cache)
 
 	return &model.DumpResponse{
-		Response: pb.CreateResponse(pb.Response_SUCCESS, "Admin dump successfully"),
-		Cache:    &cache,
+		Response:     pb.CreateResponse(pb.Response_SUCCESS, "Admin dump successfully"),
+		Info:         version.Ver(),
+		Config:       &core.ServerInfo.Config,
+		Environments: environments,
+		Cache:        &cache,
 	}, nil
 }
 
diff --git a/server/core/swagger/v4.yaml b/server/core/swagger/v4.yaml
index eeccdaf..aec7d91 100644
--- a/server/core/swagger/v4.yaml
+++ b/server/core/swagger/v4.yaml
@@ -2348,8 +2348,53 @@ definitions:
         type: array
         items:
           $ref: "#/definitions/MicroServiceInstanceKV"
+  Config:
+    type: object
+    properties:
+      maxHeaderBytes:
+        type: integer
+      maxBodyBytes:
+        type: integer
+      readHeaderTimeout:
+        type: string
+      readTimeout:
+        type: string
+      idleTimeout:
+        type: string
+      writeTimeout:
+        type: string
+      limitTTLUnit:
+        type: string
+      limitConnections:
+        type: integer
+      limitIPLookup:
+        type: string
+      sslEnabled:
+        type: string
+      sslMinVersion:
+        type: string
+      sslVerifyPeer:
+        type: string
+      sslCiphers:
+        type: string
+      autoSyncInterval:
+        type: string
+      compactIndexDelta:
+        type: integer
+      compactInterval:
+        type: string
+      logRotateSize:
+        type: integer
+      logBackupCount:
+        type: integer
   DumpResponse:
     type: object
     properties:
+      info:
+        $ref: '#/definitions/Version'
+      config:
+        $ref: '#/definitions/Config'
+      environments:
+        $ref: '#/definitions/Properties'
       cache:
         $ref: "#/definitions/Cache"
diff --git a/server/rest/controller/v4/main_controller.go b/server/rest/controller/v4/main_controller.go
index a58bfc5..0f4beec 100644
--- a/server/rest/controller/v4/main_controller.go
+++ b/server/rest/controller/v4/main_controller.go
@@ -24,11 +24,13 @@ import (
 	"github.com/apache/incubator-servicecomb-service-center/server/rest/controller"
 	"github.com/apache/incubator-servicecomb-service-center/version"
 	"net/http"
+	"sync"
 )
 
 var (
 	versionJsonCache []byte
 	versionResp      *pb.Response
+	parseVersionOnce sync.Once
 )
 
 const API_VERSION = "4.0.0"
@@ -43,16 +45,6 @@ type MainService struct {
 	//
 }
 
-func init() {
-	result := Result{
-		version.Ver(),
-		API_VERSION,
-		&core.ServerInfo.Config,
-	}
-	versionJsonCache, _ = json.Marshal(result)
-	versionResp = pb.CreateResponse(pb.Response_SUCCESS, "get version successfully")
-}
-
 func (this *MainService) URLPatterns() []rest.Route {
 	return []rest.Route{
 		{rest.HTTP_METHOD_GET, "/v4/:project/registry/version", this.GetVersion},
@@ -68,5 +60,14 @@ func (this *MainService) ClusterHealth(w http.ResponseWriter, r *http.Request) {
 }
 
 func (this *MainService) GetVersion(w http.ResponseWriter, r *http.Request) {
+	parseVersionOnce.Do(func() {
+		result := Result{
+			version.Ver(),
+			API_VERSION,
+			&core.ServerInfo.Config,
+		}
+		versionJsonCache, _ = json.Marshal(result)
+		versionResp = pb.CreateResponse(pb.Response_SUCCESS, "get version successfully")
+	})
 	controller.WriteJsonBytes(w, versionResp, versionJsonCache)
 }