Merge pull request #8 from integration-system/v2_send_config_schema

add subscription on send config schema
This commit is contained in:
Yan authored and GitHub committed 2019-10-12 10:41:10 +03:00
commit b1a0bc252f
13 files changed
+158 -49

No files matched your search

-4
View File
@@ -36,10 +36,6 @@
name = "github.com/valyala/fasthttp"
branch = "master"
[[override]]
name = "github.com/integration-system/isp-lib"
branch = "master"
[[override]]
name = "github.com/googollee/go-socket.io"
revision = "c8aeb1ed9b49d7de8fef8662efb18f1d289c5f2e"
+6
View File
@@ -6,6 +6,7 @@ import (
"github.com/integration-system/isp-lib/structure"
log "github.com/integration-system/isp-log"
"isp-config-service/codes"
"isp-config-service/entity"
)
const (
@@ -19,6 +20,7 @@ const (
_ = iota
UpdateBackendDeclarationCommand
DeleteBackendDeclarationCommand
UpdateConfigSchemaCommand
ModuleConnectedCommand
)
@@ -34,6 +36,10 @@ func PrepareModuleConnectedCommand(moduleName string) []byte {
return prepareCommand(ModuleConnectedCommand, ModuleConnected{ModuleName: moduleName})
}
func PrepareUpdateConfigSchemaCommand(schema entity.ConfigSchema) []byte {
return prepareCommand(UpdateConfigSchemaCommand, schema)
}
func prepareCommand(command uint64, payload interface{}) []byte {
cmd := make([]byte, 8)
binary.BigEndian.PutUint64(cmd, command)
+10 -11
View File
@@ -1,10 +1,10 @@
database:
address: "127.0.0.1"
schema: "config_service"
database: "isp"
database: "test"
port: "5432"
username: "isp"
password: "isp_password"
username: "test"
password: "password"
createSchema: true
moduleName: "config"
@@ -14,13 +14,13 @@ grpcOuterAddress:
ws:
rest:
port: "9011"
ip: "0.0.0.0"
port: "9001"
ip: "127.0.0.1"
grpc:
port: "9022"
ip: "0.0.0.0"
port: "9092"
ip: "127.0.0.1"
raft:
port: "9004"
port: "9003"
ip: "0.0.0.0"
metrics:
@@ -35,7 +35,6 @@ metrics:
cluster:
dataDir: ./data
inMemory: true
outerAddress: 127.0.0.1:9004
outerAddress: 127.0.0.1:9003
peers:
#- 127.0.0.1:9003
- 127.0.0.1:9004
- 127.0.0.1:9003
+29
View File
@@ -0,0 +1,29 @@
package service
import (
"github.com/pkg/errors"
"isp-config-service/entity"
"isp-config-service/holder"
"isp-config-service/model"
"isp-config-service/store/state"
)
var SchemaService schemaService
type schemaService struct{}
func (schemaService) HandleUpdateConfigSchema(schema entity.ConfigSchema, state state.State) (state.State, error) {
module := state.GetModuleById(schema.ModuleId)
if module == nil {
return state, errors.Errorf("module with id %s not found", schema.ModuleId)
}
if holder.ClusterClient.IsLeader() {
schema = state.UpdateSchema(schema)
_, err := model.SchemaRep.Upsert(schema)
if err != nil {
return state, err
}
}
return state, nil
}
+20 -5
View File
@@ -1,9 +1,10 @@
package store
import (
"fmt"
"github.com/integration-system/isp-lib/structure"
"github.com/pkg/errors"
"isp-config-service/cluster"
"isp-config-service/entity"
"isp-config-service/service"
)
@@ -11,11 +12,11 @@ func (s *Store) applyUpdateBackendDeclarationCommand(data []byte) error {
declaration := structure.BackendDeclaration{}
err := json.Unmarshal(data, &declaration)
if err != nil {
return fmt.Errorf("UpdateBackendDeclarationCommand: %w", err)
return errors.Errorf("UpdateBackendDeclarationCommand: %w", err)
}
state, err := service.ClusterStateService.HandleUpdateBackendDeclarationCommand(declaration, s.state)
if err != nil {
return fmt.Errorf("UpdateBackendDeclarationCommand: %w", err)
return errors.Errorf("UpdateBackendDeclarationCommand: %w", err)
}
s.state = state
return nil
@@ -25,7 +26,7 @@ func (s *Store) applyDeleteBackendDeclarationCommand(data []byte) error {
declaration := structure.BackendDeclaration{}
err := json.Unmarshal(data, &declaration)
if err != nil {
return fmt.Errorf("DeleteBackendDeclarationCommand: %w", err)
return errors.Errorf("DeleteBackendDeclarationCommand: %w", err)
}
state, err := service.ClusterStateService.HandleDeleteBackendDeclarationCommand(declaration, s.state)
if err != nil {
@@ -35,11 +36,25 @@ func (s *Store) applyDeleteBackendDeclarationCommand(data []byte) error {
return nil
}
func (s *Store) applyUpdateConfigSchema(data []byte) error {
schema := entity.ConfigSchema{}
err := json.Unmarshal(data, &schema)
if err != nil {
return errors.Errorf("UpdateConfigSchemaCommand: %w", err)
}
state, err := service.SchemaService.HandleUpdateConfigSchema(schema, s.state)
if err != nil {
return err
}
s.state = state
return nil
}
func (s *Store) applyModuleConnectedCommand(data []byte) error {
moduleConnected := cluster.ModuleConnected{}
err := json.Unmarshal(data, &moduleConnected)
if err != nil {
return fmt.Errorf("ModuleConnectedCommand: %w", err)
return errors.Errorf("ModuleConnectedCommand: %w", err)
}
state, err := service.ClusterStateService.HandleModuleConnectedCommand(moduleConnected, s.state)
if err != nil {
+3 -4
View File
@@ -1,7 +1,6 @@
package state
import (
"fmt"
"isp-config-service/entity"
)
@@ -21,13 +20,13 @@ func (cs *ConfigStore) Activate(config entity.Config) {
// TODO
}
func (cs *ConfigStore) GetActiveByModuleId(moduleId string) (*entity.Config, error) {
func (cs *ConfigStore) GetActiveByModuleId(moduleId string) *entity.Config {
for _, conf := range cs.configs {
if conf.ModuleId == moduleId && conf.Active {
return &conf, nil
return &conf
}
}
return nil, fmt.Errorf("no active configs for moduleId %s", moduleId)
return nil
}
func (cs *ConfigStore) GetById(id int64) (*entity.Config, error) {
+3 -1
View File
@@ -1,6 +1,8 @@
package state
import "github.com/integration-system/isp-lib/structure"
import (
"github.com/integration-system/isp-lib/structure"
)
type Mesh struct {
// { "ModuleName": { "address": BackendDeclaration, ...}, ... }
+10 -7
View File
@@ -1,7 +1,6 @@
package state
import (
"fmt"
"isp-config-service/entity"
"time"
)
@@ -18,18 +17,22 @@ func (ms *ModuleStore) Update(module entity.Module) {
}
}
func (ms *ModuleStore) GetByName(name string) (*entity.Module, error) {
func (ms *ModuleStore) GetByName(name string) *entity.Module {
for _, module := range ms.modules {
if module.Name == name {
return &module, nil
return &module
}
}
return nil, fmt.Errorf("unknown module name %s", name)
return nil
}
func (ms *ModuleStore) GetById(id int32) (*entity.Module, error) {
// TODO
return nil, nil
func (ms *ModuleStore) GetById(id string) *entity.Module {
for _, module := range ms.modules {
if module.Id == id {
return &module
}
}
return nil
}
func (ms *ModuleStore) Create(name string) entity.Module {
+14 -3
View File
@@ -1,13 +1,24 @@
package state
import "isp-config-service/entity"
import (
"isp-config-service/entity"
)
type SchemaStore struct {
schemas []entity.ConfigSchema
}
func (ss *SchemaStore) Upsert(schema entity.ConfigSchema) {
// TODO
func (ss *SchemaStore) Upsert(schema entity.ConfigSchema) entity.ConfigSchema {
for key, value := range ss.schemas {
if value.ModuleId == schema.ModuleId {
schema.Id = value.Id
ss.schemas[key] = schema
return schema
}
}
schema.Id = GenerateId()
ss.schemas = append(ss.schemas, schema)
return schema
}
func (ss *SchemaStore) GetByModuleId(moduleId int32) (*entity.ConfigSchema, error) {
+26 -12
View File
@@ -2,6 +2,7 @@ package state
import (
"github.com/integration-system/isp-lib/structure"
"github.com/pkg/errors"
"isp-config-service/entity"
"time"
)
@@ -20,6 +21,7 @@ type ReadState interface {
GetRoutes() structure.RoutingConfig
BackendExist(backend structure.BackendDeclaration) (exist bool)
GetCompiledConfig(moduleName string) (map[string]interface{}, error)
GetModuleByName(string) *entity.Module
}
func NewState() State {
@@ -67,13 +69,13 @@ func (s State) BackendExist(backend structure.BackendDeclaration) (exist bool) {
}
func (s State) GetCompiledConfig(moduleName string) (map[string]interface{}, error) {
module, err := s.modules.GetByName(moduleName)
if err != nil {
return nil, err
module := s.modules.GetByName(moduleName)
if module == nil {
return nil, errors.Errorf("module with name %s not found", moduleName)
}
config, err := s.configs.GetActiveByModuleId(module.Id)
if err != nil {
return nil, err
config := s.configs.GetActiveByModuleId(module.Id)
if config == nil {
return nil, errors.Errorf("no active configs for moduleId %s", module.Id)
}
commonConfigs := s.commonConfigs.GetByIds(config.CommonConfigs)
configsToMerge := make([]map[string]interface{}, 0, len(commonConfigs))
@@ -86,9 +88,9 @@ func (s State) GetCompiledConfig(moduleName string) (map[string]interface{}, err
return resultData, nil
}
func (s State) UpdateModuleLastConnected(moduleName string) entity.Module {
existedModule, err := s.modules.GetByName(moduleName)
if err != nil {
func (s *State) UpdateModuleLastConnected(moduleName string) entity.Module {
existedModule := s.modules.GetByName(moduleName)
if existedModule == nil {
module := s.modules.Create(moduleName)
return module
}
@@ -97,9 +99,9 @@ func (s State) UpdateModuleLastConnected(moduleName string) entity.Module {
return *existedModule
}
func (s State) UpdateModuleLastDisconnected(moduleName string) entity.Module {
existedModule, err := s.modules.GetByName(moduleName)
if err != nil {
func (s *State) UpdateModuleLastDisconnected(moduleName string) entity.Module {
existedModule := s.modules.GetByName(moduleName)
if existedModule == nil {
module := s.modules.Create(moduleName)
return module
}
@@ -107,3 +109,15 @@ func (s State) UpdateModuleLastDisconnected(moduleName string) entity.Module {
s.modules.Update(*existedModule)
return *existedModule
}
func (s State) GetModuleByName(moduleName string) *entity.Module {
return s.modules.GetByName(moduleName)
}
func (s State) GetModuleById(moduleId string) *entity.Module {
return s.modules.GetById(moduleId)
}
func (s *State) UpdateSchema(schema entity.ConfigSchema) entity.ConfigSchema {
return s.schemas.Upsert(schema)
}
+1
View File
@@ -112,6 +112,7 @@ func NewStateStore(st state.State) *Store {
cluster.UpdateBackendDeclarationCommand: store.applyUpdateBackendDeclarationCommand,
cluster.DeleteBackendDeclarationCommand: store.applyDeleteBackendDeclarationCommand,
cluster.ModuleConnectedCommand: store.applyModuleConnectedCommand,
cluster.UpdateConfigSchemaCommand: store.applyUpdateConfigSchema,
}
return store
}
+34 -1
View File
@@ -1,12 +1,15 @@
package subs
import (
"fmt"
"github.com/asaskevich/govalidator"
"github.com/integration-system/isp-lib/bootstrap"
schema2 "github.com/integration-system/isp-lib/config/schema"
"github.com/integration-system/isp-lib/structure"
log "github.com/integration-system/isp-log"
"isp-config-service/cluster"
"isp-config-service/codes"
"isp-config-service/entity"
"isp-config-service/service"
"isp-config-service/store/state"
"isp-config-service/ws"
@@ -68,8 +71,38 @@ func (h *socketEventHandler) handleModuleRequirements(conn ws.Conn, data []byte)
return Ok
}
func (h *socketEventHandler) handleConfigSchema(conn ws.Conn, data []byte) {
func (h *socketEventHandler) handleConfigSchema(conn ws.Conn, data []byte) string {
moduleName, err := conn.Parameters()
log.Debugf(0, "handleModuleRequirements moduleName: %s", moduleName) // REMOVE
if err != nil {
return err.Error()
}
configSchema := schema2.ConfigSchema{}
if err := json.Unmarshal(data, &configSchema); err != nil {
return err.Error()
}
module := new(entity.Module)
h.store.VisitReadState(func(readState state.ReadState) {
module = readState.GetModuleByName(moduleName)
})
if module == nil {
return fmt.Sprintf("module with name %s not found", moduleName)
}
schema := entity.ConfigSchema{
ModuleId: module.Id,
Version: configSchema.Version,
Schema: configSchema.Schema,
}
command := cluster.PrepareUpdateConfigSchemaCommand(schema)
i, err := h.cluster.SyncApply(command)
if err != nil {
log.WithMetadata(map[string]interface{}{
"answer": i,
}).Warnf(codes.SyncApplyError, "apply ModuleSendConfigSchemaCommand %v", err)
}
return Ok
}
func (h *socketEventHandler) handleRequestConfig(conn ws.Conn, data []byte) {
+2 -1
View File
@@ -35,7 +35,8 @@ func (h *socketEventHandler) SubscribeAll() {
OnError(h.handleError).
OnWithAck(cluster.ApplyCommandEvent, h.applyCommandOnLeader).
OnWithAck(utils.ModuleReady, h.handleModuleReady).
OnWithAck(utils.ModuleSendRequirements, h.handleModuleRequirements)
OnWithAck(utils.ModuleSendRequirements, h.handleModuleRequirements).
OnWithAck(utils.ModuleSendConfigSchema, h.handleConfigSchema)
}
func (h *socketEventHandler) handleConnect(conn ws.Conn) {