diff --git a/service/config.go b/service/config.go index 6d11a2d..97596e7 100644 --- a/service/config.go +++ b/service/config.go @@ -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, state) + VersionConfig.updateDB(versionConfig, removedVersionId) } } diff --git a/service/version.go b/service/version.go index 590d7d0..de8ad57 100644 --- a/service/version.go +++ b/service/version.go @@ -54,10 +54,7 @@ func (versionConfigService) updateState(version int32, id string, data entity.Co return cfg, removedVersionId } -func (versionConfigService) updateDB(cfg entity.VersionConfig, removedId string, state state.WritableState) { - if !state.VersionConfig().CheckCount() { - return - } +func (versionConfigService) updateDB(cfg entity.VersionConfig, removedId string) { _, err := model.VersionStoreRep.Upsert(cfg) if err != nil { log.WithMetadata(map[string]interface{}{ diff --git a/store/state/version_config.go b/store/state/version_config.go index d12bd12..15a960a 100644 --- a/store/state/version_config.go +++ b/store/state/version_config.go @@ -6,6 +6,8 @@ import ( "isp-config-service/entity" ) +const defaultVersionCount = 5 + type WriteableVersionConfigStore interface { ReadonlyVersionConfigStore Update(config entity.VersionConfig) (removedId string) @@ -14,22 +16,21 @@ type WriteableVersionConfigStore interface { type ReadonlyVersionConfigStore interface { GetByConfigId(id string) []entity.VersionConfig - CheckCount() bool } type VersionConfigStore struct { VersionByConfigId map[string][]entity.VersionConfig - count int } func (s *VersionConfigStore) Update(req entity.VersionConfig) string { - if !(s.count > 0) { - return req.Id + count := config.Get().(*conf.Configuration).VersionConfigCount + if count <= 0 { + count = defaultVersionCount } var removedId string store, found := s.VersionByConfigId[req.ConfigId] if found { - if len(store) >= s.count { + if len(store) >= count { removedId = store[0].Id store = append(store[1:], req) } else { @@ -66,14 +67,8 @@ func (s VersionConfigStore) GetByConfigId(id string) []entity.VersionConfig { return []entity.VersionConfig{} } -func (s *VersionConfigStore) CheckCount() bool { - return s.count > 0 -} - func NewVersionConfigStore() *VersionConfigStore { - count := config.Get().(*conf.Configuration).VersionConfigCount return &VersionConfigStore{ - count: count, VersionByConfigId: make(map[string][]entity.VersionConfig), } } diff --git a/store/store_test.go b/store/store_test.go index 03ad864..918320d 100644 --- a/store/store_test.go +++ b/store/store_test.go @@ -2,11 +2,12 @@ package store import ( "bytes" + "testing" + "time" + "github.com/stretchr/testify/assert" "isp-config-service/entity" state2 "isp-config-service/store/state" - "testing" - "time" ) type mockSnapshotSink struct {