From 7d37080a529c3bf1e5bcf25de6819272e49e15ec Mon Sep 17 00:00:00 2001 From: Lance726 <158132389@qq.com> Date: Thu, 30 May 2024 16:07:36 +0800 Subject: [PATCH 1/4] Support setting sentinel password in sentinel mode --- config/config.go | 17 +++++++++-------- helper/redis.go | 23 ++++++++++++----------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/config/config.go b/config/config.go index bf05f5b..7db4f1d 100644 --- a/config/config.go +++ b/config/config.go @@ -77,18 +77,19 @@ type Config struct { type RedisPool map[string]RedisConf type RedisConf struct { - Addr string - Password string - DB int - PoolSize int - MigrateTo string // If this is not empty, all the PUBLISH will go to that pool - MasterName string - Version string + Addr string + Password string + DB int + PoolSize int + MigrateTo string // If this is not empty, all the PUBLISH will go to that pool + MasterName string + Version string + SentinelPassword string EnableSecondaryStorage bool // number of seconds. when job's delay second is greater than pumpStorageThresh, - //it will be written to storage if enabled + // it will be written to storage if enabled SecondaryStorageThresholdSeconds int64 } diff --git a/helper/redis.go b/helper/redis.go index eff2f2e..b1032f0 100644 --- a/helper/redis.go +++ b/helper/redis.go @@ -22,14 +22,15 @@ func NewRedisClient(conf *config.RedisConf, opt *redis.Options) (client *redis.C opt.DB = conf.DB if conf.IsSentinel() { client = redis.NewFailoverClient(&redis.FailoverOptions{ - MasterName: conf.MasterName, - SentinelAddrs: strings.Split(opt.Addr, ","), - Password: opt.Password, - PoolSize: opt.PoolSize, - ReadTimeout: opt.ReadTimeout, - WriteTimeout: opt.WriteTimeout, - MinIdleConns: opt.MinIdleConns, - DB: opt.DB, + MasterName: conf.MasterName, + SentinelAddrs: strings.Split(opt.Addr, ","), + SentinelPassword: conf.SentinelPassword, + Password: opt.Password, + PoolSize: opt.PoolSize, + ReadTimeout: opt.ReadTimeout, + WriteTimeout: opt.WriteTimeout, + MinIdleConns: opt.MinIdleConns, + DB: opt.DB, }) client.AddHook(hooks.NewMetricsHook(client)) return client @@ -45,7 +46,7 @@ func validateRedisPersistConfig(ctx context.Context, cli *redis.Client, conf *co if err != nil { return err } - isNoEvctionPolicy, isAppendOnlyEnabled := false, false + isNoEvictionPolicy, isAppendOnlyEnabled := false, false var maxMem int64 lines := strings.Split(infoStr, "\r\n") for _, line := range lines { @@ -55,14 +56,14 @@ func validateRedisPersistConfig(ctx context.Context, cli *redis.Client, conf *co } switch fields[0] { case "maxmemory_policy": - isNoEvctionPolicy = fields[1] == "noeviction" + isNoEvictionPolicy = fields[1] == "noeviction" case "aof_enabled": isAppendOnlyEnabled = fields[1] == "1" case "maxmemory": maxMem, _ = strconv.ParseInt(fields[1], 10, 64) } } - if !isNoEvctionPolicy { + if !isNoEvictionPolicy { return errors.New("redis memory_policy MUST be 'noeviction' to prevent data loss") } if !isAppendOnlyEnabled { From a1b7faf35f4d69573fbcbe1aa730937247a8baed Mon Sep 17 00:00:00 2001 From: Lance726 <158132389@qq.com> Date: Thu, 30 May 2024 16:10:16 +0800 Subject: [PATCH 2/4] Enable sentinel password and redis master password in local --- config/demo-conf.toml | 13 +++++----- config/docker-image-conf.toml | 1 + scripts/redis/docker-compose.yml | 44 +++++++++++++++++++++++++++++--- 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/config/demo-conf.toml b/config/demo-conf.toml index e12a0dc..d945caa 100644 --- a/config/demo-conf.toml +++ b/config/demo-conf.toml @@ -20,18 +20,19 @@ test_user = "change.me" [AdminRedis] # redis used to store admin data, eg. tokens Addr = "localhost:6379" -# Password = foobared +Password = "foobared" [Pool] [Pool.default] Addr = "localhost:6379" -# Password = foobared +Password = "foobared" # DB = 0 #MigrateTo = "migrate" # When migration is enabled, all PUBLISH will go to `migrate` pool. and `default` will be drained #[Pool.migrate] #Addr = "localhost:6389" -#[Pool.mysentinel] -# Addr = "localhost:16379,localhost:6380,localhost:6381" -# MasterName = "mymaster" -# Password = foobared +[Pool.mysentinel] +Addr = "localhost:26379,localhost:26380,localhost:26381" +MasterName = "mymaster" +Password = "foobared" +SentinelPassword = "foobared1" diff --git a/config/docker-image-conf.toml b/config/docker-image-conf.toml index 737dc6e..150ceb3 100644 --- a/config/docker-image-conf.toml +++ b/config/docker-image-conf.toml @@ -35,3 +35,4 @@ Addr = "redis:6379" # Addr = "localhost:16379,localhost:6380,localhost:6381" # MasterName = "mymaster" # Password = foobared +# SentinelPassword = "foobared1" \ No newline at end of file diff --git a/scripts/redis/docker-compose.yml b/scripts/redis/docker-compose.yml index 0612ef8..1ac73b2 100644 --- a/scripts/redis/docker-compose.yml +++ b/scripts/redis/docker-compose.yml @@ -4,24 +4,60 @@ services: redis-sentinel: image: bitnami/redis-sentinel:5.0 environment: - - REDIS_MASTER_HOST=127.0.0.1 + - REDIS_MASTER_HOST=redis + - REDIS_MASTER_PORT_NUMBER=6379 + - REDIS_SENTINEL_PORT=26379 + - REDIS_MASTER_PASSWORD=foobared + - REDIS_SENTINEL_PASSWORD=foobared1 ports: - "26379:26379" + depends_on: + - redis + + redis-sentinel-1: + image: bitnami/redis-sentinel:5.0 + environment: + - REDIS_MASTER_HOST=redis + - REDIS_MASTER_PORT_NUMBER=6379 + - REDIS_SENTINEL_PORT=26379 + - REDIS_MASTER_PASSWORD=foobared + - REDIS_SENTINEL_PASSWORD=foobared1 + ports: + - "26380:26379" + depends_on: + - redis + + redis-sentinel-2: + image: bitnami/redis-sentinel:5.0 + environment: + - REDIS_MASTER_HOST=redis + - REDIS_MASTER_PORT_NUMBER=6379 + - REDIS_SENTINEL_PORT=26379 + - REDIS_MASTER_PASSWORD=foobared + - REDIS_SENTINEL_PASSWORD=foobared1 + ports: + - "26381:26379" + depends_on: + - redis redis: image: redis:4 - command: redis-server --appendonly yes + command: redis-server --requirepass foobared --appendonly yes ports: - "6379:6379" redis_1: image: redis:4 - command: redis-server --appendonly yes + command: redis-server --slaveof redis 6379 --requirepass foobared --masterauth foobared --appendonly yes ports: - "6380:6379" + depends_on: + - redis redis_2: image: redis:4 - command: redis-server --appendonly yes + command: redis-server --slaveof redis 6379 --requirepass foobared --masterauth foobared --appendonly yes ports: - "6381:6379" + depends_on: + - redis From 851171eff6e44f4af0698258cebd8e16225a05d6 Mon Sep 17 00:00:00 2001 From: Lance726 <158132389@qq.com> Date: Thu, 30 May 2024 16:10:41 +0800 Subject: [PATCH 3/4] Add two local startup scripts --- Makefile | 6 ++++++ scripts/setup.sh | 7 +++++++ scripts/teardown.sh | 3 +++ 3 files changed, 16 insertions(+) create mode 100644 scripts/setup.sh create mode 100644 scripts/teardown.sh diff --git a/Makefile b/Makefile index 471e77c..07dacd7 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,12 @@ $(PROGRAM): @printf $(MAKECOLOR)"Hint: It's a good idea to run 'make test' ;)"$(ENDCOLOR) @echo "" +setup: + @bash scripts/setup.sh + +teardown: + @bash scripts/teardown.sh + test: - cd scripts/spanner && docker-compose up --force-recreate -d && cd ../.. @sh scripts/run-test.sh diff --git a/scripts/setup.sh b/scripts/setup.sh new file mode 100644 index 0000000..7fd2dcb --- /dev/null +++ b/scripts/setup.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +if [[ -n $(docker ps -q -f "name=lmstfy-test") ]];then + cd scripts/redis && docker-compose -p lmstfy-test down -v --remove-orphans && cd ../.. +fi + +cd scripts/redis && docker-compose -p lmstfy-test up -d --remove-orphans && cd ../.. \ No newline at end of file diff --git a/scripts/teardown.sh b/scripts/teardown.sh new file mode 100644 index 0000000..7195833 --- /dev/null +++ b/scripts/teardown.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +cd scripts/redis && docker-compose -p lmstfy-test down -v --remove-orphans && cd ../.. From e304c318198b5d6a07ea55b036d9ae766b9049df Mon Sep 17 00:00:00 2001 From: hulk Date: Mon, 3 Jun 2024 14:21:49 +0800 Subject: [PATCH 4/4] Update config/docker-image-conf.toml --- config/docker-image-conf.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/docker-image-conf.toml b/config/docker-image-conf.toml index 150ceb3..a80afca 100644 --- a/config/docker-image-conf.toml +++ b/config/docker-image-conf.toml @@ -35,4 +35,4 @@ Addr = "redis:6379" # Addr = "localhost:16379,localhost:6380,localhost:6381" # MasterName = "mymaster" # Password = foobared -# SentinelPassword = "foobared1" \ No newline at end of file +# SentinelPassword = "foobared1"