Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support setting sentinel password in sentinel mode #222

Merged
merged 4 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 9 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
13 changes: 7 additions & 6 deletions config/demo-conf.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions config/docker-image-conf.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ Addr = "redis:6379"
# Addr = "localhost:16379,localhost:6380,localhost:6381"
# MasterName = "mymaster"
# Password = foobared
# SentinelPassword = "foobared1"
23 changes: 12 additions & 11 deletions helper/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
44 changes: 40 additions & 4 deletions scripts/redis/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions scripts/setup.sh
Original file line number Diff line number Diff line change
@@ -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 ../..
git-hulk marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions scripts/teardown.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

cd scripts/redis && docker-compose -p lmstfy-test down -v --remove-orphans && cd ../..
Loading