little-cui closed pull request #389: SCB-735 Add admin dump api URL: https://github.com/apache/incubator-servicecomb-service-center/pull/389
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/admin/admin.go b/server/admin/admin.go new file mode 100644 index 00000000..304b5094 --- /dev/null +++ b/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{}) +} diff --git a/server/admin/admin_suite_test.go b/server/admin/admin_suite_test.go new file mode 100644 index 00000000..d2444ea1 --- /dev/null +++ b/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") +} diff --git a/server/admin/controller_v4.go b/server/admin/controller_v4.go new file mode 100644 index 00000000..5fd786a1 --- /dev/null +++ b/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) +} diff --git a/server/admin/model/dump.go b/server/admin/model/dump.go new file mode 100644 index 00000000..1cbb2602 --- /dev/null +++ b/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"` +} diff --git a/server/admin/service.go b/server/admin/service.go new file mode 100644 index 00000000..9643cbb4 --- /dev/null +++ b/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 + } +} diff --git a/server/admin/service_test.go b/server/admin/service_test.go new file mode 100644 index 00000000..73b7b1af --- /dev/null +++ b/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)) + }) + }) + }) +}) diff --git a/server/bootstrap/bootstrap.go b/server/bootstrap/bootstrap.go index 184cb6d4..300debf2 100644 --- a/server/bootstrap/bootstrap.go +++ b/server/bootstrap/bootstrap.go @@ -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" diff --git a/server/core/backend/cache.go b/server/core/backend/cache.go index 2f9107e1..dc2df8f5 100644 --- a/server/core/backend/cache.go +++ b/server/core/backend/cache.go @@ -29,6 +29,7 @@ type Cache interface { } type Cacher interface { + Config() *Config Cache() Cache Run() Stop() diff --git a/server/core/backend/cache_null.go b/server/core/backend/cache_null.go index cce930e2..520214ff 100644 --- a/server/core/backend/cache_null.go +++ b/server/core/backend/cache_null.go @@ -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() {} diff --git a/server/core/backend/cacher_kv.go b/server/core/backend/cacher_kv.go index 6c6aa996..0b8780c5 100644 --- a/server/core/backend/cacher_kv.go +++ b/server/core/backend/cacher_kv.go @@ -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 }() diff --git a/server/core/backend/indexer.go b/server/core/backend/indexer.go index 4f47a72d..ee94bc61 100644 --- a/server/core/backend/indexer.go +++ b/server/core/backend/indexer.go @@ -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() @@ -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) diff --git a/server/core/backend/indexer_kv.go b/server/core/backend/indexer_kv.go index 7cf24431..eea29e43 100644 --- a/server/core/backend/indexer_kv.go +++ b/server/core/backend/indexer_kv.go @@ -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) diff --git a/server/core/backend/store.go b/server/core/backend/store.go index 608b2d50..428957cd 100644 --- a/server/core/backend/store.go +++ b/server/core/backend/store.go @@ -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 ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services