Skip to content

Commit

Permalink
Fix issue 171 & 172 (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
chuntaojun authored Sep 18, 2023
1 parent 6dfa42a commit 03a52e4
Show file tree
Hide file tree
Showing 12 changed files with 527 additions and 114 deletions.
35 changes: 35 additions & 0 deletions pkg/config/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ type ConfigFileConfig interface {
GetPropertiesValueCacheSize() int32
// GetPropertiesValueExpireTime 缓存的过期时间,默认为 60s
GetPropertiesValueExpireTime() int64
// GetLocalCache .
GetLocalCache() ConfigLocalCacheConfig
}

// RateLimitConfig 限流相关配置.
Expand Down Expand Up @@ -488,6 +490,39 @@ type ServiceSpecificConfig interface {
GetServiceRouter() ServiceRouterConfig
}

type ConfigLocalCacheConfig interface {
BaseConfig
// IsPersistEnable consumer.localCache.persistEnable
// 是否启用本地缓存
IsPersistEnable() bool
// SetPersistEnable 设置是否启用本地缓存
SetPersistEnable(enable bool)
// GetPersistDir consumer.localCache.persistDir
// 本地缓存持久化路径
GetPersistDir() string
// SetPersistDir 设置本地缓存持久化路径
SetPersistDir(string)
// GetPersistMaxWriteRetry consumer.localCache.persistMaxWriteRetry
// 缓存最大写重试次数
GetPersistMaxWriteRetry() int
// SetPersistMaxWriteRetry 设置缓存最大写重试次数
SetPersistMaxWriteRetry(int)
// GetPersistMaxReadRetry consumer.localCache.persistMaxReadRetry
// 缓存最大读重试次数
GetPersistMaxReadRetry() int
// SetPersistMaxReadRetry 设置缓存最大读重试次数
SetPersistMaxReadRetry(int)
// GetPersistRetryInterval consumer.localCache.persistRetryInterval
// 缓存持久化重试间隔
GetPersistRetryInterval() time.Duration
// SetPersistRetryInterval 设置缓存持久化重试间隔
SetPersistRetryInterval(time.Duration)
// SetFallbackToLocalCache .
SetFallbackToLocalCache(enable bool)
// IsFallbackToLocalCache .
IsFallbackToLocalCache() bool
}

// ConfigConnectorConfig 配置中心连接相关的配置.
type ConfigConnectorConfig interface {
ServerConnectorConfig
Expand Down
136 changes: 134 additions & 2 deletions pkg/config/config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,23 @@ package config
import (
"errors"
"fmt"
"path/filepath"
"time"

"github.com/golang/protobuf/proto"
"github.com/hashicorp/go-multierror"

"github.com/polarismesh/polaris-go/pkg/model"
)

// DefaultConfigFileEnable 默认打开配置中心能力
var DefaultConfigFileEnable = true

// ConfigFileConfigImpl 对接配置中心相关配置.
type ConfigFileConfigImpl struct {
ConfigConnectorConfig *ConfigConnectorConfigImpl `yaml:"configConnector" json:"configConnector"`
ConfigFilterConfig *ConfigFilterConfigImpl `yaml:"configFilter" json:"configFilter"`
LocalCache *ConfigLocalCacheConfigImpl `yaml:"localCache" json:"localCache"`
ConfigConnectorConfig *ConfigConnectorConfigImpl `yaml:"configConnector" json:"configConnector"`
ConfigFilterConfig *ConfigFilterConfigImpl `yaml:"configFilter" json:"configFilter"`
// 是否启动配置中心
Enable *bool `yaml:"enable" json:"enable"`
PropertiesValueCacheSize *int32 `yaml:"propertiesValueCacheSize" json:"propertiesValueCacheSize"`
Expand Down Expand Up @@ -79,6 +84,11 @@ func (c *ConfigFileConfigImpl) SetPropertiesValueExpireTime(propertiesValueExpir
c.PropertiesValueExpireTime = &propertiesValueExpireTime
}

// GetLocalCache .
func (c *ConfigFileConfigImpl) GetLocalCache() ConfigLocalCacheConfig {
return c.LocalCache
}

// Verify 检验ConfigConnector配置.
func (c *ConfigFileConfigImpl) Verify() error {
if c == nil {
Expand Down Expand Up @@ -107,6 +117,7 @@ func (c *ConfigFileConfigImpl) Verify() error {
func (c *ConfigFileConfigImpl) SetDefault() {
c.ConfigConnectorConfig.SetDefault()
c.ConfigFilterConfig.SetDefault()
c.LocalCache.SetDefault()
if c.Enable == nil {
c.Enable = &DefaultConfigFileEnable
}
Expand All @@ -124,4 +135,125 @@ func (c *ConfigFileConfigImpl) Init() {
c.ConfigConnectorConfig.Init()
c.ConfigFilterConfig = &ConfigFilterConfigImpl{}
c.ConfigFilterConfig.Init()
c.LocalCache = &ConfigLocalCacheConfigImpl{}
c.LocalCache.Init()
}

// ConfigLocalCacheConfigImpl 本地缓存配置.
type ConfigLocalCacheConfigImpl struct {
// config.localCache.persistDir
// 本地缓存持久化路径
PersistDir string `yaml:"persistDir" json:"persistDir"`
// 是否启用本地缓存
PersistEnable *bool `yaml:"persistEnable" json:"persistEnable"`
// config.localCache.persistMaxWriteRetry
PersistMaxWriteRetry int `yaml:"persistMaxWriteRetry" json:"persistMaxWriteRetry"`
// config.localCache.persistReadRetry
PersistMaxReadRetry int `yaml:"persistMaxReadRetry" json:"persistMaxReadRetry"`
// config.localCache.persistRetryInterval
PersistRetryInterval *time.Duration `yaml:"persistRetryInterval" json:"persistRetryInterval"`
// config.localCache.fallbackToLocalCache
FallbackToLocalCache *bool `yaml:"fallbackToLocalCache" json:"fallbackToLocalCache"`
}

// IsPersistEnable consumer.localCache.persistEnable
// 是否启用本地缓存
func (l *ConfigLocalCacheConfigImpl) IsPersistEnable() bool {
if l.PersistEnable == nil {
return true
}
return *l.PersistEnable
}

// SetPersistEnable 设置是否启用本地缓存
func (l *ConfigLocalCacheConfigImpl) SetPersistEnable(enable bool) {
l.PersistEnable = &enable
}

// GetPersistDir consumer.localCache.persist.path
// 本地缓存持久化路径.
func (l *ConfigLocalCacheConfigImpl) GetPersistDir() string {
return l.PersistDir
}

// SetPersistDir 设置本地缓存持久化路径.
func (l *ConfigLocalCacheConfigImpl) SetPersistDir(dir string) {
l.PersistDir = dir
}

// GetPersistMaxWriteRetry consumer.localCache.persist.maxWriteRetry.
func (l *ConfigLocalCacheConfigImpl) GetPersistMaxWriteRetry() int {
return l.PersistMaxWriteRetry
}

// SetPersistMaxWriteRetry 设置本地缓存持久化写入失败重试次数.
func (l *ConfigLocalCacheConfigImpl) SetPersistMaxWriteRetry(maxWriteRetry int) {
l.PersistMaxWriteRetry = maxWriteRetry
}

// GetPersistMaxReadRetry consumer.localCache.persist.maxReadRetry.
func (l *ConfigLocalCacheConfigImpl) GetPersistMaxReadRetry() int {
return l.PersistMaxReadRetry
}

// SetPersistMaxReadRetry 设置本地缓存持久化读取失败重试次数.
func (l *ConfigLocalCacheConfigImpl) SetPersistMaxReadRetry(maxReadRetry int) {
l.PersistMaxReadRetry = maxReadRetry
}

// GetPersistRetryInterval consumer.localCache.persist.retryInterval.
func (l *ConfigLocalCacheConfigImpl) GetPersistRetryInterval() time.Duration {
return *l.PersistRetryInterval
}

// SetPersistRetryInterval 设置本地缓存持久化重试间隔.
func (l *ConfigLocalCacheConfigImpl) SetPersistRetryInterval(interval time.Duration) {
l.PersistRetryInterval = &interval
}

// SetFallbackToLocalCache .
func (l *ConfigLocalCacheConfigImpl) SetFallbackToLocalCache(enable bool) {
l.FallbackToLocalCache = &enable
}

// IsFallbackToLocalCache .
func (l *ConfigLocalCacheConfigImpl) IsFallbackToLocalCache() bool {
if l.FallbackToLocalCache == nil {
return true
}
return *l.FallbackToLocalCache
}

// Verify 检验LocalCacheConfig配置.
func (l *ConfigLocalCacheConfigImpl) Verify() error {
if nil == l {
return errors.New("LocalCacheConfig is nil")
}
return nil
}

// SetDefault 设置LocalCacheConfig配置的默认值.
func (l *ConfigLocalCacheConfigImpl) SetDefault() {
if l.PersistEnable == nil {
l.PersistEnable = model.ToBoolPtr(true)
}
if l.FallbackToLocalCache == nil {
l.FallbackToLocalCache = model.ToBoolPtr(true)
}
if len(l.PersistDir) == 0 {
l.PersistDir = filepath.Join(DefaultCachePersistDir, "config")
}
if nil == l.PersistRetryInterval {
l.PersistRetryInterval = model.ToDurationPtr(DefaultPersistRetryInterval)
}
if l.PersistMaxReadRetry == 0 {
l.PersistMaxReadRetry = DefaultPersistMaxReadRetry
}
if l.PersistMaxWriteRetry == 0 {
l.PersistMaxWriteRetry = DefaultPersistMaxWriteRetry
}
}

// Init localche配置初始化.
func (l *ConfigLocalCacheConfigImpl) Init() {
}
8 changes: 4 additions & 4 deletions pkg/config/location_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ type LocationProviderConfigImpl struct {
Options map[string]interface{} `yaml:"options" json:"options"`
}

func (l LocationProviderConfigImpl) GetType() string {
func (l *LocationProviderConfigImpl) GetType() string {
return l.Type
}

func (l LocationProviderConfigImpl) GetOptions() map[string]interface{} {
func (l *LocationProviderConfigImpl) GetOptions() map[string]interface{} {
return l.Options
}

func (l LocationProviderConfigImpl) Verify() error {
func (l *LocationProviderConfigImpl) Verify() error {
if l.Type == "" {
return errors.New("type is empty")
}
return nil
}

func (l LocationProviderConfigImpl) SetDefault() {
func (l *LocationProviderConfigImpl) SetDefault() {
if len(l.Options) == 0 {
l.Options = map[string]interface{}{}
}
Expand Down
22 changes: 17 additions & 5 deletions pkg/flow/configuration/config_flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,24 @@ type ConfigFileFlow struct {
chain configfilter.Chain
configuration config.Configuration

persistHandler *CachePersistHandler

startLongPollingTaskOnce sync.Once
}

// NewConfigFileFlow 创建配置中心服务
func NewConfigFileFlow(connector configconnector.ConfigConnector,
chain configfilter.Chain,
configuration config.Configuration) *ConfigFileFlow {
func NewConfigFileFlow(connector configconnector.ConfigConnector, chain configfilter.Chain,
configuration config.Configuration) (*ConfigFileFlow, error) {
persistHandler, err := NewCachePersistHandler(
configuration.GetConfigFile().GetLocalCache().GetPersistDir(),
configuration.GetConfigFile().GetLocalCache().GetPersistMaxWriteRetry(),
configuration.GetConfigFile().GetLocalCache().GetPersistMaxReadRetry(),
configuration.GetConfigFile().GetLocalCache().GetPersistRetryInterval(),
)
if err != nil {
return nil, err
}

configFileService := &ConfigFileFlow{
connector: connector,
chain: chain,
Expand All @@ -61,9 +72,10 @@ func NewConfigFileFlow(connector configconnector.ConfigConnector,
configFileCache: map[string]model.ConfigFile{},
configFilePool: map[string]*ConfigFileRepo{},
notifiedVersion: map[string]uint64{},
persistHandler: persistHandler,
}

return configFileService
return configFileService, nil
}

// Destroy 销毁服务
Expand Down Expand Up @@ -99,7 +111,7 @@ func (c *ConfigFileFlow) GetConfigFile(namespace, fileGroup, fileName string) (m
return configFile, nil
}

fileRepo, err := newConfigFileRepo(configFileMetadata, c.connector, c.chain, c.configuration)
fileRepo, err := newConfigFileRepo(configFileMetadata, c.connector, c.chain, c.configuration, c.persistHandler)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 03a52e4

Please sign in to comment.