popozy opened a new pull request #695:
URL: https://github.com/apache/servicecomb-service-center/pull/695


   ### Target
   Refactoring service-center to make it support different backend 
storage(Etcd, Mongo, etc.) and kv design
   ### Background
   At this point, register-information from services in mesh could be stored in 
etcd cluster(embeded or isolated-cluster) with a certain kv design format(see 
the design example 
[here](https://github.com/apache/servicecomb-service-center/blob/master/examples/etcd_data_struct.yaml)).
   ### Problem
   Howere, the service-comb can not make it if new kv design format from 
another database want to be introduced to the project. For example, kv format 
will be designed like 
[this](https://github.com/apache/servicecomb-service-center/blob/master/examples/mongodb_data_struct.yaml)
 when taking mongo into consideration.
   ### Enhancement
   So, I'd like to **abstract kv contructing operation from api service layer 
to a single module named storageShim**, where act like shim between backend 
databse and api bussiness layer. 
   **The storageShim module will construct request metada from 
service-resigter-api to different kv according to different backend storage 
cluster. **
   ### Changes after refactoring
   Take the service information register as a example.
   #### Before refactoring
   ```go
   // api layer
   func CreateServicePri(ctx context, request CreateServiceRequest) {
           // parameter validation
           // tracing
   
           // construct kv in etcd-cluster format
        serviceKey := &pb.MicroServiceKey{
                Tenant:      domainProject,
                Environment: service.Environment,
                AppId:       service.AppId,
                ServiceName: service.ServiceName,
                Alias:       service.Alias,
                Version:     service.Version,
        }
        index := apt.GenerateServiceIndexKey(serviceKey)
        key := apt.GenerateServiceKey(domainProject, service.ServiceId)
        keyBytes := util.StringToBytesWithNoCopy(key)
        indexBytes := util.StringToBytesWithNoCopy(index)
        aliasBytes := 
util.StringToBytesWithNoCopy(apt.GenerateServiceAliasKey(serviceKey))
   
        opts := []registry.PluginOp{
                registry.OpPut(registry.WithKey(keyBytes), 
registry.WithValue(data)),
                registry.OpPut(registry.WithKey(indexBytes), 
registry.WithStrValue(service.ServiceId)),
        }
        uniqueCmpOpts := []registry.CompareOp{
                registry.OpCmp(registry.CmpVer(indexBytes), registry.CmpEqual, 
0),
                registry.OpCmp(registry.CmpVer(keyBytes), registry.CmpEqual, 0),
        }
        failOpts := []registry.PluginOp{
                registry.OpGet(registry.WithKey(indexBytes)),
        }
   
           // call etcd client to store metadata
                resp, err := backend.Registry().TxnWithCmp(ctx, opts, 
uniqueCmpOpts, failOpts)
   
          // resp check
   
         return resp, err
   }
   ```
   #### After refactor
   ``` go
   // api layer
   func CreateServicePri(ctx context, request CreateServiceRequest) {
           // parameter validation
           // tracing
   
           // call etcd client to store metadata
                resp, err := backend.Storage().RegisterService(ctx, request)
   
          // resp check
   
         return resp, err
   }
   ```
   ``` go
   // storageShim module
   // etcd extend storageShim module as a plugin
   func RegisterService(ctx context, request CreateServiceRequest) 
(CreateServiceReponse, error) {
           // construct kv in etcd-cluster format
         opts, uniqueCmpOpts, failOpts := constructEtcdFormat(ctx, request)
           resp, err := backend.Registry().TxnWithCmp(ctx, opts, uniqueCmpOpts, 
failOpts)
           return resp, err
   }
   
   // mongo extend storageShim module as a plugin
   func RegisterService(ctx context, request CreateServiceRequest) 
(CreateServiceReponse, error) {
           // construct kv in etcd-cluster format
         key, val := constructMongoFormat(ctx, request)
           resp, err := backend.Registry().TxnWithCmp(ctx, key, val)
           return resp, err
   }
   ```
   
   ### Pull request message
   Framework of storageShim module.
   1. Input: apply resources type(instances, service, tag, etc.) crud operation 
for api service layer
   2. Action: construct kv for different database(mongo, etcd) using the 
inputs, request from api
   3. Output: invoke registry/dicovery plugin to finish crud operation


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to