Skip to content

Commit

Permalink
Merge c4968dd into fc6664c
Browse files Browse the repository at this point in the history
  • Loading branch information
little-cui committed Jul 11, 2018
2 parents fc6664c + c4968dd commit ff8d166
Show file tree
Hide file tree
Showing 13 changed files with 313 additions and 0 deletions.
29 changes: 29 additions & 0 deletions server/admin/admin.go
@@ -0,0 +1,29 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package admin

import (
roa "github.com/apache/incubator-servicecomb-service-center/pkg/rest"
)

func init() {
registerREST()
}

func registerREST() {
roa.RegisterServent(&AdminServiceControllerV4{})
}
45 changes: 45 additions & 0 deletions server/admin/admin_suite_test.go
@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package admin

import (
"github.com/apache/incubator-servicecomb-service-center/pkg/util"
_ "github.com/apache/incubator-servicecomb-service-center/server/plugin/infra/quota/buildin"
_ "github.com/apache/incubator-servicecomb-service-center/server/plugin/infra/registry/etcd"
_ "github.com/apache/incubator-servicecomb-service-center/server/plugin/infra/uuid/buildin"
. "github.com/onsi/ginkgo"
"github.com/onsi/ginkgo/reporters"
. "github.com/onsi/gomega"
"golang.org/x/net/context"
"testing"
)

func TestAdmin(t *testing.T) {
RegisterFailHandler(Fail)
junitReporter := reporters.NewJUnitReporter("model.junit.xml")
RunSpecsWithDefaultAndCustomReporters(t, "model Suite", []Reporter{junitReporter})
}

var _ = BeforeSuite(func() {
//init plugin
})

func getContext() context.Context {
return util.SetContext(
util.SetDomainProject(context.Background(), "default", "default"),
"noCache", "1")
}
46 changes: 46 additions & 0 deletions server/admin/controller_v4.go
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package admin

import (
"net/http"

"github.com/apache/incubator-servicecomb-service-center/pkg/rest"
"github.com/apache/incubator-servicecomb-service-center/server/admin/model"
"github.com/apache/incubator-servicecomb-service-center/server/rest/controller"
)

// AdminService 治理相关接口服务
type AdminServiceControllerV4 struct {
}

// URLPatterns 路由
func (ctrl *AdminServiceControllerV4) URLPatterns() []rest.Route {
return []rest.Route{
{rest.HTTP_METHOD_GET, "/v4/:project/admin/dump", ctrl.Dump},
}
}

func (ctrl *AdminServiceControllerV4) Dump(w http.ResponseWriter, r *http.Request) {
request := &model.DumpRequest{}
ctx := r.Context()
resp, _ := AdminServiceAPI.Dump(ctx, request)

respInternal := resp.Response
resp.Response = nil
controller.WriteResponse(w, respInternal, resp)
}
37 changes: 37 additions & 0 deletions server/admin/model/dump.go
@@ -0,0 +1,37 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package model

import (
pb "github.com/apache/incubator-servicecomb-service-center/server/core/proto"
)

type Cache map[string][]KV

type KV struct {
Key string `json:"key"`
Value interface{} `json:"value,omitempty"`
Rev int64 `json:"rev"`
}

type DumpRequest struct {
}

type DumpResponse struct {
Response *pb.Response `json:"response,omitempty"`
Cache Cache `json:"cache,omitempty"`
}
87 changes: 87 additions & 0 deletions server/admin/service.go
@@ -0,0 +1,87 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package admin

import (
"github.com/apache/incubator-servicecomb-service-center/pkg/util"
"github.com/apache/incubator-servicecomb-service-center/server/admin/model"
"github.com/apache/incubator-servicecomb-service-center/server/core"
"github.com/apache/incubator-servicecomb-service-center/server/core/backend"
pb "github.com/apache/incubator-servicecomb-service-center/server/core/proto"
"golang.org/x/net/context"
)

var AdminServiceAPI = &AdminService{}

type AdminService struct {
}

func (service *AdminService) Dump(ctx context.Context, in *model.DumpRequest) (*model.DumpResponse, error) {

domainProject := util.ParseDomainProject(ctx)
cache := make(model.Cache)

if core.IsDefaultDomainProject(domainProject) {
service.dumpAll(cache)
} else {
service.dumpDomainProject(cache, domainProject)
}

return &model.DumpResponse{
Response: pb.CreateResponse(pb.Response_SUCCESS, "Admin dump successfully."),
Cache: cache,
}, nil
}

func (service *AdminService) dumpAll(cache model.Cache) {
for id, indexer := range backend.Store().Entities() {
var kvs []model.KV
indexer.Cacher().Cache().ForEach(func(k string, v *backend.KeyValue) (next bool) {
kvs = append(kvs, model.KV{
Key: k, Value: v.Value, Rev: v.ModRevision,
})
return true
})
if len(kvs) == 0 {
continue
}
cache[id.String()] = kvs
}
}

func (service *AdminService) dumpDomainProject(cache model.Cache, domainProject string) {
for id, indexer := range backend.Store().Entities() {
cfg := indexer.Cacher().Config()
if cfg == nil {
continue
}

var kvs []model.KV
var arr []*backend.KeyValue
prefix := cfg.Prefix + core.SPLIT + domainProject + core.SPLIT
indexer.Cacher().Cache().GetAll(prefix, &arr)
for _, kv := range arr {
kvs = append(kvs, model.KV{
Key: util.BytesToStringWithNoCopy(kv.Key), Value: kv.Value, Rev: kv.ModRevision,
})
}
if len(kvs) == 0 {
continue
}
cache[id.String()] = kvs
}
}
47 changes: 47 additions & 0 deletions server/admin/service_test.go
@@ -0,0 +1,47 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package admin

import (
"github.com/apache/incubator-servicecomb-service-center/pkg/util"
"github.com/apache/incubator-servicecomb-service-center/server/admin/model"
pb "github.com/apache/incubator-servicecomb-service-center/server/core/proto"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"golang.org/x/net/context"
)

var _ = Describe("'Admin' service", func() {
Describe("execute 'dump' operation", func() {
Context("when get all", func() {
It("should be passed", func() {
resp, err := AdminServiceAPI.Dump(getContext(), &model.DumpRequest{})
Expect(err).To(BeNil())
Expect(resp.Response.Code).To(Equal(pb.Response_SUCCESS))
})
})
Context("when get by domain project", func() {
It("should be passed", func() {
resp, err := AdminServiceAPI.Dump(
util.SetDomainProject(context.Background(), "x", "x"),
&model.DumpRequest{})
Expect(err).To(BeNil())
Expect(resp.Response.Code).To(Equal(pb.Response_SUCCESS))
})
})
})
})
3 changes: 3 additions & 0 deletions server/bootstrap/bootstrap.go
Expand Up @@ -51,6 +51,9 @@ import _ "github.com/apache/incubator-servicecomb-service-center/server/govern"
// module 'broker'
import _ "github.com/apache/incubator-servicecomb-service-center/server/broker"

// module 'admin'
import _ "github.com/apache/incubator-servicecomb-service-center/server/admin"

// metrics
import _ "github.com/apache/incubator-servicecomb-service-center/server/metric"

Expand Down
1 change: 1 addition & 0 deletions server/core/backend/cache.go
Expand Up @@ -29,6 +29,7 @@ type Cache interface {
}

type Cacher interface {
Config() *Config
Cache() Cache
Run()
Stop()
Expand Down
1 change: 1 addition & 0 deletions server/core/backend/cache_null.go
Expand Up @@ -39,6 +39,7 @@ func (n *nullCache) Remove(k string) {}
type nullCacher struct {
}

func (n *nullCacher) Config() *Config { return nil }
func (n *nullCacher) Cache() Cache { return NullCache }
func (n *nullCacher) Run() {}
func (n *nullCacher) Stop() {}
Expand Down
4 changes: 4 additions & 0 deletions server/core/backend/cacher_kv.go
Expand Up @@ -42,6 +42,10 @@ type KvCacher struct {
goroutine *util.GoRoutine
}

func (c *KvCacher) Config() *Config {
return c.Cfg
}

func (c *KvCacher) needList() bool {
rev := c.lw.Revision()
defer func() { c.lastRev = rev }()
Expand Down
5 changes: 5 additions & 0 deletions server/core/backend/indexer.go
Expand Up @@ -39,6 +39,7 @@ func (pr *Response) MaxModRevision() (max int64) {
}

type Indexer interface {
Cacher() Cacher
Search(ctx context.Context, opts ...registry.PluginOpOption) (*Response, error)
Run()
Stop()
Expand All @@ -49,6 +50,10 @@ type baseIndexer struct {
Cfg *Config
}

func (i *baseIndexer) Cacher() Cacher {
return NullCacher
}

func (i *baseIndexer) Search(ctx context.Context, opts ...registry.PluginOpOption) (r *Response, err error) {
op := registry.OpGet(opts...)
key := util.BytesToStringWithNoCopy(op.Key)
Expand Down
4 changes: 4 additions & 0 deletions server/core/backend/indexer_kv.go
Expand Up @@ -35,6 +35,10 @@ type CacheIndexer struct {
isClose bool
}

func (i *CacheIndexer) Cacher() Cacher {
return i.cacher
}

func (i *CacheIndexer) Search(ctx context.Context, opts ...registry.PluginOpOption) (*Response, error) {
op := registry.OpGet(opts...)
key := util.BytesToStringWithNoCopy(op.Key)
Expand Down
4 changes: 4 additions & 0 deletions server/core/backend/store.go
Expand Up @@ -217,6 +217,10 @@ func (s *KvStore) Entity(id StoreType) Indexer {
return s.indexers[id]
}

func (s *KvStore) Entities() map[StoreType]Indexer {
return s.indexers
}

func (s *KvStore) Install(e Entity) (id StoreType, err error) {
if id, err = InstallType(e); err != nil {
return
Expand Down

0 comments on commit ff8d166

Please sign in to comment.