fix raft store Snapshot and Restore
This commit is contained in:
1 parent
c43e52e9ad
commit
e840b53978
9 files changed
+162
-111
No files matched your search
+5
-5
@@ -6,22 +6,22 @@ import (
|
||||
"isp-config-service/store/state"
|
||||
)
|
||||
|
||||
func NewStateFromRepository() (state.State, error) {
|
||||
func NewStateFromRepository() (*state.State, error) {
|
||||
configs, err := model.ConfigRep.Snapshot()
|
||||
if err != nil {
|
||||
return state.State{}, errors.WithMessage(err, "restore configs")
|
||||
return nil, errors.WithMessage(err, "restore configs")
|
||||
}
|
||||
schemas, err := model.SchemaRep.Snapshot()
|
||||
if err != nil {
|
||||
return state.State{}, errors.WithMessage(err, "load config schemas")
|
||||
return nil, errors.WithMessage(err, "load config schemas")
|
||||
}
|
||||
modules, err := model.ModuleRep.Snapshot()
|
||||
if err != nil {
|
||||
return state.State{}, errors.WithMessage(err, "load modules registry")
|
||||
return nil, errors.WithMessage(err, "load modules registry")
|
||||
}
|
||||
commonConfigs, err := model.CommonConfigRep.Snapshot()
|
||||
if err != nil {
|
||||
return state.State{}, errors.WithMessage(err, "load common configs")
|
||||
return nil, errors.WithMessage(err, "load common configs")
|
||||
}
|
||||
|
||||
st := state.NewStateFromSnapshot(configs, schemas, modules, commonConfigs)
|
||||
|
||||
@@ -18,13 +18,13 @@ type ReadonlyCommonConfigStore interface {
|
||||
}
|
||||
|
||||
type CommonConfigStore struct {
|
||||
configs []entity.CommonConfig
|
||||
Configs []entity.CommonConfig
|
||||
}
|
||||
|
||||
func (cs CommonConfigStore) GetByIds(ids []string) []entity.CommonConfig {
|
||||
idsMap := StringsToMap(ids)
|
||||
configs := make([]entity.CommonConfig, 0, len(ids))
|
||||
for _, config := range cs.configs {
|
||||
for _, config := range cs.Configs {
|
||||
if _, found := idsMap[config.Id]; found {
|
||||
configs = append(configs, config)
|
||||
}
|
||||
@@ -34,7 +34,7 @@ func (cs CommonConfigStore) GetByIds(ids []string) []entity.CommonConfig {
|
||||
|
||||
func (cs CommonConfigStore) GetByName(name string) []entity.CommonConfig {
|
||||
configs := make([]entity.CommonConfig, 0)
|
||||
for _, config := range cs.configs {
|
||||
for _, config := range cs.Configs {
|
||||
if config.Name == name {
|
||||
configs = append(configs, config)
|
||||
}
|
||||
@@ -49,14 +49,14 @@ func (cs *CommonConfigStore) Create(config entity.CommonConfig) entity.CommonCon
|
||||
if config.Data == nil {
|
||||
config.Data = make(entity.ConfigData)
|
||||
}
|
||||
cs.configs = append(cs.configs, config)
|
||||
cs.Configs = append(cs.Configs, config)
|
||||
return config
|
||||
}
|
||||
|
||||
func (cs *CommonConfigStore) UpdateById(config entity.CommonConfig) {
|
||||
for i := range cs.configs {
|
||||
if cs.configs[i].Id == config.Id {
|
||||
cs.configs[i] = config
|
||||
for i := range cs.Configs {
|
||||
if cs.Configs[i].Id == config.Id {
|
||||
cs.Configs[i] = config
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -65,12 +65,12 @@ func (cs *CommonConfigStore) UpdateById(config entity.CommonConfig) {
|
||||
func (cs *CommonConfigStore) DeleteByIds(ids []string) int {
|
||||
idsMap := StringsToMap(ids)
|
||||
var deleted int
|
||||
for i := 0; i < len(cs.configs); i++ {
|
||||
id := cs.configs[i].Id
|
||||
for i := 0; i < len(cs.Configs); i++ {
|
||||
id := cs.Configs[i].Id
|
||||
if _, ok := idsMap[id]; ok {
|
||||
// change configs ordering
|
||||
cs.configs[i] = cs.configs[len(cs.configs)-1]
|
||||
cs.configs = cs.configs[:len(cs.configs)-1]
|
||||
cs.Configs[i] = cs.Configs[len(cs.Configs)-1]
|
||||
cs.Configs = cs.Configs[:len(cs.Configs)-1]
|
||||
deleted++
|
||||
}
|
||||
}
|
||||
@@ -78,11 +78,11 @@ func (cs *CommonConfigStore) DeleteByIds(ids []string) int {
|
||||
}
|
||||
|
||||
func (cs *CommonConfigStore) GetAll() []entity.CommonConfig {
|
||||
response := make([]entity.CommonConfig, len(cs.configs))
|
||||
copy(response, cs.configs)
|
||||
response := make([]entity.CommonConfig, len(cs.Configs))
|
||||
copy(response, cs.Configs)
|
||||
return response
|
||||
}
|
||||
|
||||
func NewCommonConfigStore() *CommonConfigStore {
|
||||
return &CommonConfigStore{configs: make([]entity.CommonConfig, 0)}
|
||||
return &CommonConfigStore{Configs: make([]entity.CommonConfig, 0)}
|
||||
}
|
||||
+23
-23
@@ -25,11 +25,11 @@ type ReadonlyConfigStore interface {
|
||||
}
|
||||
|
||||
type ConfigStore struct {
|
||||
configs []entity.Config
|
||||
Configs []entity.Config
|
||||
}
|
||||
|
||||
func (cs ConfigStore) GetActiveByModuleId(moduleId string) *entity.Config {
|
||||
for _, conf := range cs.configs {
|
||||
for _, conf := range cs.Configs {
|
||||
if conf.ModuleId == moduleId && conf.Active {
|
||||
return &conf
|
||||
}
|
||||
@@ -40,7 +40,7 @@ func (cs ConfigStore) GetActiveByModuleId(moduleId string) *entity.Config {
|
||||
func (cs ConfigStore) GetByIds(ids []string) []entity.Config {
|
||||
idsMap := StringsToMap(ids)
|
||||
result := make([]entity.Config, 0, len(ids))
|
||||
for _, conf := range cs.configs {
|
||||
for _, conf := range cs.Configs {
|
||||
if _, ok := idsMap[conf.Id]; ok {
|
||||
result = append(result, conf)
|
||||
}
|
||||
@@ -51,7 +51,7 @@ func (cs ConfigStore) GetByIds(ids []string) []entity.Config {
|
||||
func (cs ConfigStore) GetByModuleIds(ids []string) []entity.Config {
|
||||
idsMap := StringsToMap(ids)
|
||||
result := make([]entity.Config, 0, len(ids))
|
||||
for _, conf := range cs.configs {
|
||||
for _, conf := range cs.Configs {
|
||||
if _, ok := idsMap[conf.ModuleId]; ok {
|
||||
result = append(result, conf)
|
||||
}
|
||||
@@ -73,14 +73,14 @@ func (cs *ConfigStore) Create(config entity.Config) entity.Config {
|
||||
if config.CommonConfigs == nil {
|
||||
config.CommonConfigs = make([]string, 0)
|
||||
}
|
||||
cs.configs = append(cs.configs, config)
|
||||
cs.Configs = append(cs.Configs, config)
|
||||
return config
|
||||
}
|
||||
|
||||
func (cs *ConfigStore) UpdateById(config entity.Config) {
|
||||
for i := range cs.configs {
|
||||
if cs.configs[i].Id == config.Id {
|
||||
cs.configs[i] = config
|
||||
for i := range cs.Configs {
|
||||
if cs.Configs[i].Id == config.Id {
|
||||
cs.Configs[i] = config
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -88,10 +88,10 @@ func (cs *ConfigStore) UpdateById(config entity.Config) {
|
||||
|
||||
func (cs *ConfigStore) calcNewVersion(moduleId string) int32 {
|
||||
var maxVersion int32 = 0
|
||||
for i := range cs.configs {
|
||||
if cs.configs[i].ModuleId == moduleId {
|
||||
if cs.configs[i].Version > maxVersion {
|
||||
maxVersion = cs.configs[i].Version
|
||||
for i := range cs.Configs {
|
||||
if cs.Configs[i].ModuleId == moduleId {
|
||||
if cs.Configs[i].Version > maxVersion {
|
||||
maxVersion = cs.Configs[i].Version
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -101,12 +101,12 @@ func (cs *ConfigStore) calcNewVersion(moduleId string) int32 {
|
||||
func (cs *ConfigStore) DeleteByIds(ids []string) int {
|
||||
idsMap := StringsToMap(ids)
|
||||
var deleted int
|
||||
for i := 0; i < len(cs.configs); i++ {
|
||||
id := cs.configs[i].Id
|
||||
for i := 0; i < len(cs.Configs); i++ {
|
||||
id := cs.Configs[i].Id
|
||||
if _, ok := idsMap[id]; ok {
|
||||
// change configs ordering
|
||||
cs.configs[i] = cs.configs[len(cs.configs)-1]
|
||||
cs.configs = cs.configs[:len(cs.configs)-1]
|
||||
cs.Configs[i] = cs.Configs[len(cs.Configs)-1]
|
||||
cs.Configs = cs.Configs[:len(cs.Configs)-1]
|
||||
deleted++
|
||||
}
|
||||
}
|
||||
@@ -124,11 +124,11 @@ func (cs *ConfigStore) Activate(config entity.Config, date time.Time) []entity.C
|
||||
|
||||
func (cs *ConfigStore) deactivate(moduleId string, date time.Time) []entity.Config {
|
||||
affected := make([]entity.Config, 0)
|
||||
for i := range cs.configs {
|
||||
if cs.configs[i].ModuleId == moduleId && cs.configs[i].Active {
|
||||
cs.configs[i].Active = false
|
||||
cs.configs[i].UpdatedAt = date
|
||||
affected = append(affected, cs.configs[i])
|
||||
for i := range cs.Configs {
|
||||
if cs.Configs[i].ModuleId == moduleId && cs.Configs[i].Active {
|
||||
cs.Configs[i].Active = false
|
||||
cs.Configs[i].UpdatedAt = date
|
||||
affected = append(affected, cs.Configs[i])
|
||||
}
|
||||
}
|
||||
return affected
|
||||
@@ -137,7 +137,7 @@ func (cs *ConfigStore) deactivate(moduleId string, date time.Time) []entity.Conf
|
||||
func (cs *ConfigStore) FilterByCommonConfigs(commonIds []string) []entity.Config {
|
||||
idsMap := StringsToMap(commonIds)
|
||||
result := make([]entity.Config, 0)
|
||||
for _, cfg := range cs.configs {
|
||||
for _, cfg := range cs.Configs {
|
||||
for _, commonId := range cfg.CommonConfigs {
|
||||
if _, ok := idsMap[commonId]; ok {
|
||||
result = append(result, cfg)
|
||||
@@ -149,5 +149,5 @@ func (cs *ConfigStore) FilterByCommonConfigs(commonIds []string) []entity.Config
|
||||
}
|
||||
|
||||
func NewConfigStore() *ConfigStore {
|
||||
return &ConfigStore{configs: make([]entity.Config, 0)}
|
||||
return &ConfigStore{Configs: make([]entity.Config, 0)}
|
||||
}
|
||||
+11
-11
@@ -22,12 +22,12 @@ type NodesMap map[string]structure.BackendDeclaration
|
||||
|
||||
type Mesh struct {
|
||||
// { "ModuleName": { "address": BackendDeclaration, ...}, ... }
|
||||
modulesMap map[string]NodesMap
|
||||
ModulesMap map[string]NodesMap
|
||||
}
|
||||
|
||||
func (m Mesh) BackendExist(backend structure.BackendDeclaration) (exist bool) {
|
||||
address := backend.Address.GetAddress()
|
||||
if nodes, ok := m.modulesMap[backend.ModuleName]; ok {
|
||||
if nodes, ok := m.ModulesMap[backend.ModuleName]; ok {
|
||||
if _, ok := nodes[address]; ok {
|
||||
exist = true
|
||||
}
|
||||
@@ -37,7 +37,7 @@ func (m Mesh) BackendExist(backend structure.BackendDeclaration) (exist bool) {
|
||||
|
||||
func (m Mesh) GetBackends(module string) []structure.BackendDeclaration {
|
||||
declarations := make([]structure.BackendDeclaration, 0)
|
||||
if nodes, ok := m.modulesMap[module]; ok {
|
||||
if nodes, ok := m.ModulesMap[module]; ok {
|
||||
for _, backend := range nodes {
|
||||
declarations = append(declarations, backend)
|
||||
}
|
||||
@@ -47,7 +47,7 @@ func (m Mesh) GetBackends(module string) []structure.BackendDeclaration {
|
||||
|
||||
func (m Mesh) GetModuleAddresses(moduleName string) []structure.AddressConfiguration {
|
||||
addressList := make([]structure.AddressConfiguration, 0)
|
||||
if nodes, ok := m.modulesMap[moduleName]; ok {
|
||||
if nodes, ok := m.ModulesMap[moduleName]; ok {
|
||||
for _, backend := range nodes {
|
||||
addressList = append(addressList, backend.Address)
|
||||
}
|
||||
@@ -57,7 +57,7 @@ func (m Mesh) GetModuleAddresses(moduleName string) []structure.AddressConfigura
|
||||
|
||||
func (m Mesh) GetRoutes() structure.RoutingConfig {
|
||||
routes := structure.RoutingConfig{}
|
||||
for _, node := range m.modulesMap {
|
||||
for _, node := range m.ModulesMap {
|
||||
for _, backend := range node {
|
||||
routes = append(routes, backend)
|
||||
}
|
||||
@@ -67,7 +67,7 @@ func (m Mesh) GetRoutes() structure.RoutingConfig {
|
||||
|
||||
func (m Mesh) CheckBackendChanged(backend structure.BackendDeclaration) (changed bool) {
|
||||
address := backend.Address.GetAddress()
|
||||
if nodes, ok := m.modulesMap[backend.ModuleName]; ok {
|
||||
if nodes, ok := m.ModulesMap[backend.ModuleName]; ok {
|
||||
if oldBackend, ok := nodes[address]; ok {
|
||||
if oldBackend.Version != backend.Version || !oldBackend.IsPathsEqual(backend.Endpoints) || oldBackend.LibVersion != backend.LibVersion {
|
||||
changed = true
|
||||
@@ -83,7 +83,7 @@ func (m Mesh) CheckBackendChanged(backend structure.BackendDeclaration) (changed
|
||||
|
||||
func (m *Mesh) UpsertBackend(backend structure.BackendDeclaration) (changed bool) {
|
||||
address := backend.Address.GetAddress()
|
||||
if nodes, ok := m.modulesMap[backend.ModuleName]; ok {
|
||||
if nodes, ok := m.ModulesMap[backend.ModuleName]; ok {
|
||||
if oldBackend, ok := nodes[address]; ok {
|
||||
if oldBackend.Version != backend.Version || !oldBackend.IsPathsEqual(backend.Endpoints) || oldBackend.LibVersion != backend.LibVersion {
|
||||
nodes[address] = backend
|
||||
@@ -94,7 +94,7 @@ func (m *Mesh) UpsertBackend(backend structure.BackendDeclaration) (changed bool
|
||||
changed = true
|
||||
}
|
||||
} else {
|
||||
m.modulesMap[backend.ModuleName] = NodesMap{address: backend}
|
||||
m.ModulesMap[backend.ModuleName] = NodesMap{address: backend}
|
||||
changed = true
|
||||
}
|
||||
return
|
||||
@@ -102,18 +102,18 @@ func (m *Mesh) UpsertBackend(backend structure.BackendDeclaration) (changed bool
|
||||
|
||||
func (m *Mesh) DeleteBackend(backend structure.BackendDeclaration) (deleted bool) {
|
||||
address := backend.Address.GetAddress()
|
||||
if nodes, ok := m.modulesMap[backend.ModuleName]; ok {
|
||||
if nodes, ok := m.ModulesMap[backend.ModuleName]; ok {
|
||||
if _, ok := nodes[address]; ok {
|
||||
delete(nodes, address)
|
||||
deleted = true
|
||||
}
|
||||
if len(nodes) == 0 {
|
||||
delete(m.modulesMap, backend.ModuleName)
|
||||
delete(m.ModulesMap, backend.ModuleName)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func NewMesh() *Mesh {
|
||||
return &Mesh{modulesMap: make(map[string]NodesMap)}
|
||||
return &Mesh{ModulesMap: make(map[string]NodesMap)}
|
||||
}
|
||||
+15
-15
@@ -18,11 +18,11 @@ type ReadonlyModuleStore interface {
|
||||
}
|
||||
|
||||
type ModuleStore struct {
|
||||
modules []entity.Module
|
||||
Modules []entity.Module
|
||||
}
|
||||
|
||||
func (ms ModuleStore) GetByName(name string) *entity.Module {
|
||||
for _, module := range ms.modules {
|
||||
for _, module := range ms.Modules {
|
||||
if module.Name == name {
|
||||
return &module
|
||||
}
|
||||
@@ -31,7 +31,7 @@ func (ms ModuleStore) GetByName(name string) *entity.Module {
|
||||
}
|
||||
|
||||
func (ms ModuleStore) GetById(id string) *entity.Module {
|
||||
for _, module := range ms.modules {
|
||||
for _, module := range ms.Modules {
|
||||
if module.Id == id {
|
||||
return &module
|
||||
}
|
||||
@@ -40,39 +40,39 @@ func (ms ModuleStore) GetById(id string) *entity.Module {
|
||||
}
|
||||
|
||||
func (ms *ModuleStore) UpdateByName(module entity.Module) {
|
||||
for i := range ms.modules {
|
||||
if ms.modules[i].Name == module.Name {
|
||||
ms.modules[i] = module
|
||||
for i := range ms.Modules {
|
||||
if ms.Modules[i].Name == module.Name {
|
||||
ms.Modules[i] = module
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (ms *ModuleStore) Create(module entity.Module) {
|
||||
ms.modules = append(ms.modules, module)
|
||||
ms.Modules = append(ms.Modules, module)
|
||||
}
|
||||
|
||||
func (ms *ModuleStore) DeleteByIds(ids []string) []entity.Module {
|
||||
idsMap := StringsToMap(ids)
|
||||
var deleted []entity.Module
|
||||
for i := 0; i < len(ms.modules); i++ {
|
||||
id := ms.modules[i].Id
|
||||
for i := 0; i < len(ms.Modules); i++ {
|
||||
id := ms.Modules[i].Id
|
||||
if _, ok := idsMap[id]; ok {
|
||||
// change modules ordering
|
||||
deleted = append(deleted, ms.modules[i])
|
||||
ms.modules[i] = ms.modules[len(ms.modules)-1]
|
||||
ms.modules = ms.modules[:len(ms.modules)-1]
|
||||
deleted = append(deleted, ms.Modules[i])
|
||||
ms.Modules[i] = ms.Modules[len(ms.Modules)-1]
|
||||
ms.Modules = ms.Modules[:len(ms.Modules)-1]
|
||||
}
|
||||
}
|
||||
return deleted
|
||||
}
|
||||
|
||||
func (ms *ModuleStore) GetAll() []entity.Module {
|
||||
response := make([]entity.Module, len(ms.modules))
|
||||
copy(response, ms.modules)
|
||||
response := make([]entity.Module, len(ms.Modules))
|
||||
copy(response, ms.Modules)
|
||||
return response
|
||||
}
|
||||
|
||||
func NewModuleStore() *ModuleStore {
|
||||
return &ModuleStore{modules: make([]entity.Module, 0)}
|
||||
return &ModuleStore{Modules: make([]entity.Module, 0)}
|
||||
}
|
||||
+10
-10
@@ -15,13 +15,13 @@ type ReadonlySchemaStore interface {
|
||||
}
|
||||
|
||||
type SchemaStore struct {
|
||||
schemas []entity.ConfigSchema
|
||||
Schemas []entity.ConfigSchema
|
||||
}
|
||||
|
||||
func (ss SchemaStore) GetByModuleIds(ids []string) []entity.ConfigSchema {
|
||||
idsMap := StringsToMap(ids)
|
||||
result := make([]entity.ConfigSchema, 0, len(ids))
|
||||
for _, schema := range ss.schemas {
|
||||
for _, schema := range ss.Schemas {
|
||||
if _, ok := idsMap[schema.ModuleId]; ok {
|
||||
result = append(result, schema)
|
||||
}
|
||||
@@ -32,12 +32,12 @@ func (ss SchemaStore) GetByModuleIds(ids []string) []entity.ConfigSchema {
|
||||
func (ss *SchemaStore) DeleteByIds(ids []string) int {
|
||||
idsMap := StringsToMap(ids)
|
||||
var deleted int
|
||||
for i := 0; i < len(ss.schemas); i++ {
|
||||
id := ss.schemas[i].Id
|
||||
for i := 0; i < len(ss.Schemas); i++ {
|
||||
id := ss.Schemas[i].Id
|
||||
if _, ok := idsMap[id]; ok {
|
||||
// change schemas ordering
|
||||
ss.schemas[i] = ss.schemas[len(ss.schemas)-1]
|
||||
ss.schemas = ss.schemas[:len(ss.schemas)-1]
|
||||
ss.Schemas[i] = ss.Schemas[len(ss.Schemas)-1]
|
||||
ss.Schemas = ss.Schemas[:len(ss.Schemas)-1]
|
||||
deleted++
|
||||
}
|
||||
}
|
||||
@@ -45,18 +45,18 @@ func (ss *SchemaStore) DeleteByIds(ids []string) int {
|
||||
}
|
||||
|
||||
func (ss *SchemaStore) Upsert(schema entity.ConfigSchema) entity.ConfigSchema {
|
||||
for key, value := range ss.schemas {
|
||||
for key, value := range ss.Schemas {
|
||||
if value.ModuleId == schema.ModuleId {
|
||||
schema.Id = value.Id
|
||||
schema.CreatedAt = value.CreatedAt
|
||||
ss.schemas[key] = schema
|
||||
ss.Schemas[key] = schema
|
||||
return schema
|
||||
}
|
||||
}
|
||||
ss.schemas = append(ss.schemas, schema)
|
||||
ss.Schemas = append(ss.Schemas, schema)
|
||||
return schema
|
||||
}
|
||||
|
||||
func NewSchemaStore() *SchemaStore {
|
||||
return &SchemaStore{schemas: make([]entity.ConfigSchema, 0)}
|
||||
return &SchemaStore{Schemas: make([]entity.ConfigSchema, 0)}
|
||||
}
|
||||
+29
-29
@@ -5,51 +5,51 @@ import (
|
||||
)
|
||||
|
||||
type State struct {
|
||||
mesh *Mesh
|
||||
configs *ConfigStore
|
||||
schemas *SchemaStore
|
||||
modules *ModuleStore
|
||||
commonConfigs *CommonConfigStore
|
||||
MeshStore *Mesh
|
||||
ConfigsStore *ConfigStore
|
||||
SchemasStore *SchemaStore
|
||||
ModulesStore *ModuleStore
|
||||
CommonConfigsStore *CommonConfigStore
|
||||
}
|
||||
|
||||
func (s State) Mesh() ReadonlyMesh {
|
||||
return s.mesh
|
||||
return s.MeshStore
|
||||
}
|
||||
|
||||
func (s State) Configs() ReadonlyConfigStore {
|
||||
return s.configs
|
||||
return s.ConfigsStore
|
||||
}
|
||||
|
||||
func (s State) Schemas() ReadonlySchemaStore {
|
||||
return s.schemas
|
||||
return s.SchemasStore
|
||||
}
|
||||
|
||||
func (s State) Modules() ReadonlyModuleStore {
|
||||
return s.modules
|
||||
return s.ModulesStore
|
||||
}
|
||||
|
||||
func (s State) CommonConfigs() ReadonlyCommonConfigStore {
|
||||
return s.commonConfigs
|
||||
return s.CommonConfigsStore
|
||||
}
|
||||
|
||||
func (s *State) WritableMesh() WriteableMesh {
|
||||
return s.mesh
|
||||
return s.MeshStore
|
||||
}
|
||||
|
||||
func (s *State) WritableConfigs() WriteableConfigStore {
|
||||
return s.configs
|
||||
return s.ConfigsStore
|
||||
}
|
||||
|
||||
func (s *State) WritableSchemas() WriteableSchemaStore {
|
||||
return s.schemas
|
||||
return s.SchemasStore
|
||||
}
|
||||
|
||||
func (s *State) WritableModules() WriteableModuleStore {
|
||||
return s.modules
|
||||
return s.ModulesStore
|
||||
}
|
||||
|
||||
func (s *State) WritableCommonConfigs() WriteableCommonConfigStore {
|
||||
return s.commonConfigs
|
||||
return s.CommonConfigsStore
|
||||
}
|
||||
|
||||
type WritableState interface {
|
||||
@@ -69,22 +69,22 @@ type ReadonlyState interface {
|
||||
CommonConfigs() ReadonlyCommonConfigStore
|
||||
}
|
||||
|
||||
func NewState() State {
|
||||
return State{
|
||||
mesh: NewMesh(),
|
||||
configs: NewConfigStore(),
|
||||
schemas: NewSchemaStore(),
|
||||
modules: NewModuleStore(),
|
||||
commonConfigs: NewCommonConfigStore(),
|
||||
func NewState() *State {
|
||||
return &State{
|
||||
MeshStore: NewMesh(),
|
||||
ConfigsStore: NewConfigStore(),
|
||||
SchemasStore: NewSchemaStore(),
|
||||
ModulesStore: NewModuleStore(),
|
||||
CommonConfigsStore: NewCommonConfigStore(),
|
||||
}
|
||||
}
|
||||
|
||||
func NewStateFromSnapshot(configs []entity.Config, schemas []entity.ConfigSchema, modules []entity.Module, commConfigs []entity.CommonConfig) State {
|
||||
return State{
|
||||
mesh: NewMesh(),
|
||||
configs: &ConfigStore{configs: configs},
|
||||
schemas: &SchemaStore{schemas: schemas},
|
||||
modules: &ModuleStore{modules: modules},
|
||||
commonConfigs: &CommonConfigStore{configs: commConfigs},
|
||||
func NewStateFromSnapshot(configs []entity.Config, schemas []entity.ConfigSchema, modules []entity.Module, commConfigs []entity.CommonConfig) *State {
|
||||
return &State{
|
||||
MeshStore: NewMesh(),
|
||||
ConfigsStore: &ConfigStore{Configs: configs},
|
||||
SchemasStore: &SchemaStore{Schemas: schemas},
|
||||
ModulesStore: &ModuleStore{Modules: modules},
|
||||
CommonConfigsStore: &CommonConfigStore{Configs: commConfigs},
|
||||
}
|
||||
}
|
||||
+4
-4
@@ -63,7 +63,7 @@ func (s *Store) Apply(l *raft.Log) interface{} {
|
||||
|
||||
func (s *Store) Snapshot() (raft.FSMSnapshot, error) {
|
||||
s.lock.Lock()
|
||||
copied := deepcopy.Copy(s.state).(state.State)
|
||||
copied := deepcopy.Copy(s.state).(*state.State)
|
||||
s.lock.Unlock()
|
||||
return &fsmSnapshot{copied}, nil
|
||||
}
|
||||
@@ -91,7 +91,7 @@ func (s *Store) VisitState(f func(writableState state.WritableState)) {
|
||||
}
|
||||
|
||||
type fsmSnapshot struct {
|
||||
state state.State
|
||||
state *state.State
|
||||
}
|
||||
|
||||
func (f *fsmSnapshot) Persist(sink raft.SnapshotSink) error {
|
||||
@@ -114,9 +114,9 @@ func (f *fsmSnapshot) Persist(sink raft.SnapshotSink) error {
|
||||
|
||||
func (f *fsmSnapshot) Release() {}
|
||||
|
||||
func NewStateStore(st state.State) *Store {
|
||||
func NewStateStore(st *state.State) *Store {
|
||||
store := &Store{
|
||||
state: &st,
|
||||
state: st,
|
||||
}
|
||||
store.handlers = map[uint64]func([]byte) (interface{}, error){
|
||||
cluster.UpdateBackendDeclarationCommand: store.applyUpdateBackendDeclarationCommand,
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"isp-config-service/entity"
|
||||
state2 "isp-config-service/store/state"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type mockSnapshotSink struct {
|
||||
bytes.Buffer
|
||||
}
|
||||
|
||||
func (d mockSnapshotSink) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d mockSnapshotSink) ID() string {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (d mockSnapshotSink) Cancel() error {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func TestStore_SnapshotRestore(t *testing.T) {
|
||||
state := state2.NewState()
|
||||
state.WritableModules().Create(entity.Module{
|
||||
Id: state2.GenerateId(),
|
||||
Name: "name1",
|
||||
CreatedAt: time.Now().Round(time.Millisecond),
|
||||
LastConnectedAt: time.Now().Round(time.Millisecond),
|
||||
LastDisconnectedAt: time.Now().Round(time.Millisecond),
|
||||
})
|
||||
|
||||
store := NewStateStore(state)
|
||||
snapshot, err := store.Snapshot()
|
||||
assert.NoError(t, err)
|
||||
sink := mockSnapshotSink{}
|
||||
err = snapshot.Persist(&sink)
|
||||
assert.NoError(t, err)
|
||||
|
||||
newState := state2.NewState()
|
||||
store.state = newState
|
||||
err = store.Restore(&sink)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, state, store.state)
|
||||
}
|
||||
Reference in new issue
Block a user