diff --git a/service/config.go b/service/config.go index 97596e7..6c01797 100644 --- a/service/config.go +++ b/service/config.go @@ -147,7 +147,7 @@ func (cs configService) HandleUpsertConfigCommand(upsertConfig cluster.UpsertCon config.Version = oldCfg.Version + 1 state.WritableConfigs().UpdateById(config) - versionConfig, removedVersionId = VersionConfig.updateState(oldCfg.Version, oldCfg.Id, oldCfg.Data, state) + _ = ConfigHistory.SaveConfigVersion(oldCfg, state) } if holder.ClusterClient.IsLeader() { @@ -159,7 +159,7 @@ func (cs configService) HandleUpsertConfigCommand(upsertConfig cluster.UpsertCon }).Errorf(codes.DatabaseOperationError, "upsert config: %v", err) } if !upsertConfig.Create { - VersionConfig.updateDB(versionConfig, removedVersionId) + ConfigHistory.updateDB(versionConfig, removedVersionId) } } diff --git a/service/version.go b/service/version.go index de8ad57..02cda0a 100644 --- a/service/version.go +++ b/service/version.go @@ -12,12 +12,12 @@ import ( ) var ( - VersionConfig = versionConfigService{} + ConfigHistory = configHistoryService{} ) -type versionConfigService struct{} +type configHistoryService struct{} -func (versionConfigService) HandleDeleteVersionConfigCommand(cfg cluster.Identity, +func (configHistoryService) HandleDeleteVersionConfigCommand(cfg cluster.Identity, state state.WritableState) cluster.ResponseWithError { state.WriteableVersionConfigStore().Delete(cfg.Id) var ( @@ -36,25 +36,27 @@ func (versionConfigService) HandleDeleteVersionConfigCommand(cfg cluster.Identit return cluster.NewResponse(domain.DeleteResponse{Deleted: deleted}) } -func (versionConfigService) HandleGetAllVersionConfigCommand(cfg cluster.Identity, +func (configHistoryService) HandleGetAllVersionConfigCommand(cfg cluster.Identity, state state.WritableState) cluster.ResponseWithError { resp := state.VersionConfig().GetByConfigId(cfg.Id) return cluster.NewResponse(resp) } -func (versionConfigService) updateState(version int32, id string, data entity.ConfigData, - writableState state.WritableState) (entity.VersionConfig, string) { +func (s configHistoryService) SaveConfigVersion(oldConfig entity.Config, writableState state.WritableState) entity.VersionConfig { cfg := entity.VersionConfig{ - Id: state.GenerateId(), - ConfigVersion: version, - ConfigId: id, - Data: data, + Id: state.GenerateId(), //todo all node generate own id + ConfigVersion: oldConfig.Version, + ConfigId: oldConfig.Id, + Data: oldConfig.Data, } removedVersionId := writableState.WriteableVersionConfigStore().Update(cfg) - return cfg, removedVersionId + if holder.ClusterClient.IsLeader() { + s.updateDB(cfg, removedVersionId) + } + return cfg } -func (versionConfigService) updateDB(cfg entity.VersionConfig, removedId string) { +func (configHistoryService) updateDB(cfg entity.VersionConfig, removedId string) { _, err := model.VersionStoreRep.Upsert(cfg) if err != nil { log.WithMetadata(map[string]interface{}{ diff --git a/store/apply.go b/store/apply.go index ce7c077..3cc3418 100644 --- a/store/apply.go +++ b/store/apply.go @@ -142,7 +142,7 @@ func (s *Store) applyDeleteVersionConfigCommand(data []byte) (interface{}, error if err != nil { return nil, errors.WithMessage(err, "unmarshal cluster.Identity") } - response := service.VersionConfig.HandleDeleteVersionConfigCommand(cfg, s.state) + response := service.ConfigHistory.HandleDeleteVersionConfigCommand(cfg, s.state) return response, nil } @@ -152,6 +152,6 @@ func (s *Store) applyAllVersionConfigCommand(data []byte) (interface{}, error) { if err != nil { return nil, errors.WithMessage(err, "unmarshal cluster.Identity") } - response := service.VersionConfig.HandleGetAllVersionConfigCommand(cfg, s.state) + response := service.ConfigHistory.HandleGetAllVersionConfigCommand(cfg, s.state) return response, nil } diff --git a/store/state/version_config.go b/store/state/version_config.go index 15a960a..a6c7d3f 100644 --- a/store/state/version_config.go +++ b/store/state/version_config.go @@ -23,14 +23,14 @@ type VersionConfigStore struct { } func (s *VersionConfigStore) Update(req entity.VersionConfig) string { - count := config.Get().(*conf.Configuration).VersionConfigCount - if count <= 0 { - count = defaultVersionCount + limit := config.Get().(*conf.Configuration).VersionConfigCount + if limit <= 0 { + limit = defaultVersionCount } var removedId string store, found := s.VersionByConfigId[req.ConfigId] if found { - if len(store) >= count { + if len(store) >= limit { removedId = store[0].Id store = append(store[1:], req) } else {