Skip to content

Commit bad4ebc

Browse files
Alexey ShatunovDivPro
authored andcommitted
feat: support redis sentinel
1 parent 3aea406 commit bad4ebc

13 files changed

Lines changed: 245 additions & 19 deletions

File tree

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ format:
2020
build:
2121
go build
2222

23+
build-linux:
24+
GOOS=linux GOARCH=amd64 go build
25+
2326
test: build
2427
go test -race $(pkgs)
2528

clients/redis.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ import (
1010

1111
func NewRedisClient(cfg config.RedisCacheConfig) (redis.UniversalClient, error) {
1212
options := &redis.UniversalOptions{
13-
Addrs: cfg.Addresses,
14-
Username: cfg.Username,
15-
Password: cfg.Password,
16-
PoolSize: cfg.PoolSize,
17-
MaxRetries: 7, // default value = 3, since MinRetryBackoff = 8 msec & MinRetryBackoff = 512 msec
13+
Addrs: cfg.Addresses, // single for standalone | list for redis cluster | sentinel addresses
14+
Username: cfg.Username,
15+
Password: cfg.Password,
16+
PoolSize: cfg.PoolSize,
17+
SentinelUsername: cfg.SentinelUsername, // for sentinel only, not required
18+
SentinelPassword: cfg.SentinelPassword, // for sentinel only, not required
19+
MasterName: cfg.MasterName, // for sentinel only, required
20+
MaxRetries: 7, // default value = 3, since MinRetryBackoff = 8 msec & MinRetryBackoff = 512 msec
1821
// the redis client will wait up to 1016 msec btw the 7 tries
1922
}
2023

config/config.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ func withoutSensitiveInfo(config *Config) *Config {
121121
if len(c.Caches[i].Redis.Password) > 0 {
122122
c.Caches[i].Redis.Password = pswPlaceHolder
123123
}
124+
if len(c.Caches[i].Redis.SentinelPassword) > 0 {
125+
c.Caches[i].Redis.SentinelPassword = pswPlaceHolder
126+
}
124127
}
125128
return c
126129
}
@@ -966,12 +969,15 @@ type RedisCacheConfig struct {
966969
TLS `yaml:",inline"`
967970
EnableTLS bool `yaml:"enable_tls,omitempty"`
968971

969-
Username string `yaml:"username,omitempty"`
970-
Password string `yaml:"password,omitempty"`
971-
Addresses []string `yaml:"addresses"`
972-
DBIndex int `yaml:"db_index,omitempty"`
973-
PoolSize int `yaml:"pool_size,omitempty"`
974-
XXX map[string]interface{} `yaml:",inline"`
972+
Username string `yaml:"username,omitempty"`
973+
Password string `yaml:"password,omitempty"`
974+
Addresses []string `yaml:"addresses"`
975+
DBIndex int `yaml:"db_index,omitempty"`
976+
PoolSize int `yaml:"pool_size,omitempty"`
977+
SentinelUsername string `yaml:"sentinel_username,omitempty"`
978+
SentinelPassword string `yaml:"sentinel_password,omitempty"`
979+
MasterName string `yaml:"master_name,omitempty"`
980+
XXX map[string]interface{} `yaml:",inline"`
975981
}
976982

977983
// UnmarshalYAML implements the yaml.Unmarshaler interface.

config/config_test.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ var fullConfig = Config{
5050
MaxPayloadSize: ByteSize(100 << 30),
5151
SharedWithAllUsers: true,
5252
Redis: RedisCacheConfig{
53-
Username: "chproxy",
54-
Password: "password",
55-
Addresses: []string{"127.0.0.1:" + redisPort},
56-
PoolSize: 10,
53+
Username: "chproxy",
54+
Password: "password",
55+
SentinelPassword: "password",
56+
Addresses: []string{"127.0.0.1:" + redisPort},
57+
PoolSize: 10,
5758
},
5859
},
5960
},
@@ -560,6 +561,10 @@ func TestExamples(t *testing.T) {
560561
"combined",
561562
"examples/combined.yml",
562563
},
564+
{
565+
"redis-sentinel",
566+
"examples/redis-sentinel.yml",
567+
},
563568
}
564569

565570
for _, tc := range testCases {
@@ -758,6 +763,7 @@ func TestRemovalSensitiveData(t *testing.T) {
758763
conf.Clusters[1].HeartBeat.Password = "XXX"
759764
conf.Clusters[2].ClusterUsers[0].Password = "XXX"
760765
conf.Caches[2].Redis.Password = "XXX"
766+
conf.Caches[2].Redis.SentinelPassword = "XXX"
761767

762768
if !cmp.Equal(conf, confSafe, cmpopts.IgnoreUnexported(Config{})) {
763769
t.Fatalf("confCopy should have sensitive data replaced with XXX values,\n the diff is: %s",
@@ -926,6 +932,7 @@ caches:
926932
addresses:
927933
- 127.0.0.1:%s
928934
pool_size: 10
935+
sentinel_password: XXX
929936
max_payload_size: 107374182400
930937
shared_with_all_users: true
931938
param_groups:

config/examples/redis-sentinel.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
log_debug: true
2+
server:
3+
http:
4+
listen_addr: ":9090"
5+
allowed_networks: ["127.0.0.1/24"]
6+
7+
users:
8+
- name: "default"
9+
to_cluster: "default"
10+
to_user: "default"
11+
cache: "redis-cache"
12+
13+
clusters:
14+
- name: "default"
15+
nodes: ["127.0.0.1:8123"]
16+
17+
caches:
18+
- name: "redis-cache"
19+
mode: "redis"
20+
redis:
21+
addresses:
22+
- "localhost:26379"
23+
- "localhost:26380"
24+
max_size: "10M"
25+
username: redisuser
26+
password: RedisPassword
27+
sentinel_user: ""
28+
sentinel_password: "SentinelPassword"
29+
master_name: "mymaster"
30+
expire: "1m"

config/testdata/full.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ caches:
6060
redis:
6161
username: chproxy
6262
password: password
63+
sentinel_password: password
6364
pool_size: 10
6465
addresses:
6566
- 127.0.0.1:16379

examples/quick_start/docker-compose.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
version: '3'
21
services:
32
zookeeper:
43
image: zookeeper:3.7
@@ -14,7 +13,7 @@ services:
1413
- ${PWD}/resources/clickhouse/config:/etc/clickhouse-server
1514
- ${PWD}/resources/clickhouse/data:/data
1615
ports:
17-
- 8123:8123
16+
- "8123:8123"
1817
networks:
1918
clickhouse-network:
2019
ipv4_address: 172.23.0.11
@@ -61,12 +60,12 @@ services:
6160
depends_on:
6261
- zookeeper
6362
chproxy:
64-
image: contentsquareplatform/chproxy:v1.24.0
63+
image: contentsquareplatform/chproxy:v1.31.0
6564
volumes:
6665
- ${PWD}/resources/chproxy/config:/config
6766
- ${PWD}/resources/chproxy/data:/data
6867
ports:
69-
- 9001:9001
68+
- "9001:9001"
7069
networks:
7170
clickhouse-network:
7271
ipv4_address: 172.23.0.14
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
log_debug: false
2+
hack_me_please: true
3+
4+
server:
5+
http:
6+
listen_addr: "0.0.0.0:9001"
7+
8+
users:
9+
- name: "default"
10+
password: "password"
11+
to_cluster: "default"
12+
to_user: "admin"
13+
max_concurrent_queries: 1000
14+
max_execution_time: 10m
15+
cache: "default_cache"
16+
17+
clusters:
18+
- name: "default"
19+
nodes: ["172.23.0.11:8123", "172.23.0.12:8123", "172.23.0.13:8123"]
20+
users:
21+
- name: "admin"
22+
password: "123"
23+
24+
caches:
25+
- name: "default_cache"
26+
mode: "redis"
27+
redis:
28+
addresses: # only sentinel addresses here
29+
- 172.23.0.54:26379
30+
- 172.23.0.55:26379
31+
username: redisuser
32+
password: RedisPassword
33+
sentinel_user: ""
34+
sentinel_password: "SentinelPassword"
35+
master_name: "mymaster"
36+
pool_size: 10
37+
db_index: 0
38+
expire: 60s
39+
max_payload_size: 1024000
40+
shared_with_all_users: false

examples/redis/docker-compose.yml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
services:
2+
redis-master:
3+
image: redis:latest
4+
container_name: redis-master
5+
restart: unless-stopped
6+
ports:
7+
- "6379:6379"
8+
volumes:
9+
- ${PWD}/redis/resources/redis-master.conf:/usr/local/etc/redis/redis.conf
10+
command: redis-server /usr/local/etc/redis/redis.conf
11+
networks:
12+
clickhouse-network:
13+
ipv4_address: 172.23.0.51
14+
redis-replica-1:
15+
image: redis:latest
16+
container_name: redis-replica-1
17+
restart: unless-stopped
18+
ports:
19+
- "6380:6379"
20+
volumes:
21+
- ${PWD}/redis/resources/redis-replica.conf:/usr/local/etc/redis/redis.conf
22+
command: redis-server /usr/local/etc/redis/redis.conf
23+
depends_on:
24+
- redis-master
25+
networks:
26+
clickhouse-network:
27+
ipv4_address: 172.23.0.52
28+
redis-replica-2:
29+
image: redis:latest
30+
container_name: redis-replica-2
31+
restart: unless-stopped
32+
ports:
33+
- "6381:6379"
34+
volumes:
35+
- ${PWD}/redis/resources/redis-replica.conf:/usr/local/etc/redis/redis.conf
36+
command: redis-server /usr/local/etc/redis/redis.conf
37+
depends_on:
38+
- redis-master
39+
networks:
40+
clickhouse-network:
41+
ipv4_address: 172.23.0.53
42+
sentinel-1:
43+
image: redis:latest
44+
container_name: sentinel-1
45+
restart: unless-stopped
46+
ports:
47+
- "26379:26379"
48+
volumes:
49+
- ${PWD}/redis/resources/sentinel1.conf:/usr/local/etc/redis/sentinel.conf
50+
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
51+
depends_on:
52+
- redis-master
53+
- redis-replica-1
54+
- redis-replica-2
55+
networks:
56+
clickhouse-network:
57+
ipv4_address: 172.23.0.54
58+
sentinel-2:
59+
image: redis:latest
60+
container_name: sentinel-2
61+
restart: unless-stopped
62+
ports:
63+
- "26380:26379"
64+
volumes:
65+
- ${PWD}/redis/resources/sentinel2.conf:/usr/local/etc/redis/sentinel.conf
66+
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
67+
depends_on:
68+
- redis-master
69+
- redis-replica-1
70+
- redis-replica-2
71+
networks:
72+
clickhouse-network:
73+
ipv4_address: 172.23.0.55
74+
networks:
75+
clickhouse-network:
76+
external: true
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
bind 0.0.0.0
2+
port 6379
3+
daemonize no
4+
pidfile /var/run/redis/redis-server.pid
5+
loglevel notice
6+
save 900 1
7+
save 300 10
8+
save 60 10000
9+
protected-mode no
10+
11+
#security checks
12+
user redisuser on >RedisPassword ~* &* +@all
13+
masteruser redisuser
14+
masterauth RedisPassword
15+
user default off

0 commit comments

Comments
 (0)