Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
313 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ type Cache interface { | |
} | ||
|
||
type Cacher interface { | ||
Config() *Config | ||
Cache() Cache | ||
Run() | ||
Stop() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters