Skip to content

Commit

Permalink
dev mode,can change,add,etc for schema (#147)
Browse files Browse the repository at this point in the history
* dev mode ,can do anything with schema

* dev mode,can do anything with schema

* dev mode, can do anything for schema
  • Loading branch information
aseTo2016 authored and little-cui committed Oct 30, 2017
1 parent 8e392d2 commit cb743ee
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
51 changes: 32 additions & 19 deletions server/service/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package service

import (
"encoding/json"
"errors"
"github.com/ServiceComb/service-center/pkg/util"
apt "github.com/ServiceComb/service-center/server/core"
Expand Down Expand Up @@ -169,9 +170,6 @@ func (s *ServiceController) ModifySchemas(ctx context.Context, request *pb.Modif
}

func modifySchemas(ctx context.Context, tenant string, service *pb.MicroService, schemas []*pb.Schema) (err error, innerErr bool) {
if !isExistSchemaId(service, schemas) {
return errors.New("exist non-exist schemaId"), false
}

serviceId := service.ServiceId
schemasInDataBase, err := GetSchemasFromDataBase(ctx, tenant, serviceId)
Expand All @@ -182,23 +180,21 @@ func modifySchemas(ctx context.Context, tenant string, service *pb.MicroService,

needUpdateSchemaList := make([]*pb.Schema, 0, len(schemas))
needAddSchemaList := make([]*pb.Schema, 0, len(schemas))
newSchemaIdList := make([]string, 0, len(schemas))

if len(schemasInDataBase) == 0 {
needAddSchemaList = schemas
} else {
for _, schema := range schemas {
exist := false
for _, schemaInner := range schemasInDataBase {
if schema.SchemaId == schemaInner.SchemaId {
needUpdateSchemaList = append(needUpdateSchemaList, schema)
exist = true
break
}
}
if !exist {
needAddSchemaList = append(needAddSchemaList, schema)
for _, schema := range schemas {
exist := false
for _, schemaInner := range schemasInDataBase {
if schema.SchemaId == schemaInner.SchemaId {
needUpdateSchemaList = append(needUpdateSchemaList, schema)
exist = true
break
}
}
if !exist {
needAddSchemaList = append(needAddSchemaList, schema)
newSchemaIdList = append(newSchemaIdList, schema.SchemaId)
}
}

pluginOps := []registry.PluginOp{}
Expand Down Expand Up @@ -242,6 +238,9 @@ func modifySchemas(ctx context.Context, tenant string, service *pb.MicroService,
pluginOps = append(pluginOps, opts...)
}
case "prod":
if len(service.Schemas) != 0 && !isExistSchemaId(service, schemas) {
return errors.New("exist non-exist schemaId"), false
}
quotaSize := len(needAddSchemaList)
if quotaSize > 0 {
_, ok, err := quota.QuotaPlugins[quota.QuataType]().Apply4Quotas(ctx, quota.SCHEMAQuotaType, tenant, serviceId, int16(quotaSize))
Expand Down Expand Up @@ -273,6 +272,17 @@ func modifySchemas(ctx context.Context, tenant string, service *pb.MicroService,
pluginOps = append(pluginOps, opt)
}
}
if len(service.Schemas) == 0 {
key := apt.GenerateServiceKey(tenant, serviceId)
service.Schemas = newSchemaIdList
data, err := json.Marshal(service)
if err != nil {
util.Logger().Errorf(err, "marshal service failed.")
return err, true
}
opt := registry.OpPut(registry.WithStrKey(key), registry.WithValue(data))
pluginOps = append(pluginOps, opt)
}
if len(needUpdateSchemaList) > 0 && len(pluginOps) == 0 {
util.Logger().Errorf(nil, "run mode is prod, schema more exist, can't change.%v", needUpdateSchemaList)
return errors.New("run mode is prod, schema more exist, can't change"), false
Expand Down Expand Up @@ -438,9 +448,12 @@ func (s *ServiceController) canModifySchema(ctx context.Context, request *pb.Mod
Schema: request.Schema,
SchemaId: schemaId,
}
if !isExistSchemaId(service, []*pb.Schema{schema}) {
return errors.New("schemaId non-exist"), false
if version.Ver().RunMode == "prod" {
if !isExistSchemaId(service, []*pb.Schema{schema}) {
return errors.New("schemaId non-exist"), false
}
}

return nil, true
}

Expand Down
6 changes: 3 additions & 3 deletions server/service/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,14 @@ var _ = Describe("ServiceController", func() {
})

It("修改schema,参数校验", func() {
fmt.Println("UT===========修改schema,参数校验")
fmt.Println("UT===========修改schema,参数校验,dev mode can change")
resp, err := serviceResource.ModifySchema(getContext(), &pb.ModifySchemaRequest{
ServiceId: serviceId,
SchemaId: "noneschma",
Schema: "change schema",
})
Expect(err).To(BeNil())
Expect(resp.GetResponse().Code).To(Equal(pb.Response_FAIL))
Expect(resp.GetResponse().Code).To(Equal(pb.Response_SUCCESS))

resp, err = serviceResource.ModifySchema(getContext(), &pb.ModifySchemaRequest{
ServiceId: "noneservice",
Expand Down Expand Up @@ -350,7 +350,7 @@ var _ = Describe("ServiceController", func() {
},
})
Expect(err).To(BeNil())
Expect(respCreateService.GetResponse().Code).To(Equal(pb.Response_FAIL))
Expect(respCreateService.GetResponse().Code).To(Equal(pb.Response_SUCCESS))
})
It("create schemas, dev mode", func() {
schemas := []*pb.Schema{
Expand Down

0 comments on commit cb743ee

Please sign in to comment.