Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8d3282e

Browse files
committed
Allow to customize the high redis memory water mark
1 parent cbfe528 commit 8d3282e

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

config/config.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,17 @@ type SecondaryStorage struct {
3737
Spanner *SpannerConfig
3838
// max number of jobs that storage pumps per batch
3939
MaxJobPumpBatchSize int64
40+
// range from 0 to 1, when the redis memory usage is greater than this value,
41+
// the storage won't pump jobs to redis anymore until the memory usage is lower than this value.
42+
//
43+
// Default is 1, means no limit
44+
HighRedisMemoryWatermark float64
4045
}
4146

4247
func (storage *SecondaryStorage) validate() error {
48+
if storage.HighRedisMemoryWatermark < 0 || storage.HighRedisMemoryWatermark > 1 {
49+
return fmt.Errorf("invalid HighRedisMemoryWatermark: %f, should be between 0 and 1", storage.HighRedisMemoryWatermark)
50+
}
4351
return storage.Spanner.validate()
4452
}
4553

@@ -156,6 +164,10 @@ func MustLoad(path string) (*Config, error) {
156164
if err := conf.SecondaryStorage.validate(); err != nil {
157165
return nil, err
158166
}
167+
if conf.SecondaryStorage.HighRedisMemoryWatermark == 0 {
168+
// default to 1
169+
conf.SecondaryStorage.HighRedisMemoryWatermark = 1
170+
}
159171
}
160172
return conf, nil
161173
}

storage/manager.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ const (
3030

3131
addJobSuccessStatus = "success"
3232
addJobFailedStatus = "failed"
33-
34-
redisMemoryUsageWatermark = 0.8 // used_memory / max_memory
3533
)
3634

3735
type Manager struct {
36+
cfg *config.Config
3837
wg sync.WaitGroup
3938
mu sync.Mutex
4039
pools map[string]engine.Engine
@@ -76,6 +75,7 @@ func NewManger(cfg *config.Config) (*Manager, error) {
7675
return nil, fmt.Errorf("create redis client err: %w", err)
7776
}
7877
return &Manager{
78+
cfg: cfg,
7979
redisCli: redisCli,
8080
storage: storage,
8181
pools: make(map[string]engine.Engine),
@@ -87,7 +87,7 @@ func NewManger(cfg *config.Config) (*Manager, error) {
8787
func (m *Manager) PumpFn(name string, pool engine.Engine, threshold int64) func() bool {
8888
return func() bool {
8989
logger := log.Get().WithField("pool", name)
90-
if isHighRedisMemUsage(m.redisCli) {
90+
if isHighRedisMemUsage(m.redisCli, m.cfg.SecondaryStorage.HighRedisMemoryWatermark) {
9191
logger.Error("High redis usage, storage stops pumping data")
9292
return false
9393
}
@@ -183,7 +183,10 @@ func (m *Manager) Shutdown() {
183183
m.storage.Close()
184184
}
185185

186-
func isHighRedisMemUsage(cli *redis.Client) bool {
186+
func isHighRedisMemUsage(cli *redis.Client, redisMemoryUsageWatermark float64) bool {
187+
if redisMemoryUsageWatermark == 0 || redisMemoryUsageWatermark >= 1 {
188+
return false
189+
}
187190
memoryInfo, err := cli.Info(context.TODO(), "memory").Result()
188191
if err != nil {
189192
return false

0 commit comments

Comments
 (0)