diff --git a/Gopkg.toml b/Gopkg.toml index 97eedb1..3fe824a 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -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" diff --git a/cluster/const.go b/cluster/const.go index bc8b045..bacaed7 100644 --- a/cluster/const.go +++ b/cluster/const.go @@ -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) diff --git a/conf/config.yml b/conf/config.yml index 0f57192..867ad98 100644 --- a/conf/config.yml +++ b/conf/config.yml @@ -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 diff --git a/service/schema.go b/service/schema.go new file mode 100644 index 0000000..8346bf6 --- /dev/null +++ b/service/schema.go @@ -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 +} diff --git a/store/apply.go b/store/apply.go index d38bb1a..2505969 100644 --- a/store/apply.go +++ b/store/apply.go @@ -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 { diff --git a/store/state/configs.go b/store/state/configs.go index e8b486d..bb1e1f0 100644 --- a/store/state/configs.go +++ b/store/state/configs.go @@ -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) { diff --git a/store/state/mesh.go b/store/state/mesh.go index 2038b50..a38d303 100644 --- a/store/state/mesh.go +++ b/store/state/mesh.go @@ -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, ...}, ... } diff --git a/store/state/modules.go b/store/state/modules.go index 18dfd52..ed84285 100644 --- a/store/state/modules.go +++ b/store/state/modules.go @@ -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 { diff --git a/store/state/schemas.go b/store/state/schemas.go index b2fa27b..f6ab0ea 100644 --- a/store/state/schemas.go +++ b/store/state/schemas.go @@ -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) { diff --git a/store/state/state.go b/store/state/state.go index 8d92ebe..41aa02d 100644 --- a/store/state/state.go +++ b/store/state/state.go @@ -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) +} diff --git a/store/store.go b/store/store.go index 039f0e2..d6a3d37 100644 --- a/store/store.go +++ b/store/store.go @@ -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 } diff --git a/subs/client.go b/subs/client.go index 60bcbe1..e1f54b1 100644 --- a/subs/client.go +++ b/subs/client.go @@ -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) { diff --git a/subs/root.go b/subs/root.go index 65d0391..2f33e84 100644 --- a/subs/root.go +++ b/subs/root.go @@ -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) {