feat: support redis sentinal (#1910)

Signed-off-by: Gaius <gaius.qi@gmail.com>
This commit is contained in:
Gaius 2022-12-09 17:25:20 +08:00
parent b2704d2e85
commit f120c8778b
No known key found for this signature in database
GPG Key ID: 8B4E5D1290FA2FFB
10 changed files with 68 additions and 51 deletions

View File

@ -35,11 +35,12 @@ import (
)
type Config struct {
Addrs []string
Username string
Password string
BrokerDB int
BackendDB int
Addrs []string
MasterName string
Username string
Password string
BrokerDB int
BackendDB int
}
type Job struct {
@ -53,19 +54,21 @@ func New(cfg *Config, queue Queue) (*Job, error) {
machineryv1log.Set(&MachineryLogger{})
if err := ping(&redis.UniversalOptions{
Addrs: cfg.Addrs,
Username: cfg.Username,
Password: cfg.Password,
DB: cfg.BrokerDB,
Addrs: cfg.Addrs,
MasterName: cfg.MasterName,
Username: cfg.Username,
Password: cfg.Password,
DB: cfg.BrokerDB,
}); err != nil {
return nil, err
}
if err := ping(&redis.UniversalOptions{
Addrs: cfg.Addrs,
Username: cfg.Username,
Password: cfg.Password,
DB: cfg.BackendDB,
Addrs: cfg.Addrs,
MasterName: cfg.MasterName,
Username: cfg.Username,
Password: cfg.Password,
DB: cfg.BackendDB,
}); err != nil {
return nil, err
}
@ -76,6 +79,7 @@ func New(cfg *Config, queue Queue) (*Job, error) {
ResultBackend: fmt.Sprintf("redis://%s:%s@%s/%d", cfg.Username, cfg.Password, strings.Join(cfg.Addrs, ","), cfg.BackendDB),
ResultsExpireIn: DefaultResultsExpireIn,
Redis: &machineryv1config.RedisConfig{
MasterName: cfg.MasterName,
MaxIdle: DefaultRedisMaxIdle,
IdleTimeout: DefaultRedisIdleTimeout,
ReadTimeout: DefaultRedisReadTimeout,

View File

@ -167,22 +167,25 @@ type RedisConfig struct {
// DEPRECATED: Please use the `addrs` field instead.
Port int `yaml:"port" mapstructure:"port"`
// Server addresses.
// Addrs is server addresses.
Addrs []string `yaml:"addrs" mapstructure:"addrs"`
// Server username.
// MasterName is the sentinel master name.
MasterName string `yaml:"masterName" mapstructure:"masterName"`
// Username is server username.
Username string `yaml:"username" mapstructure:"username"`
// Server password.
// Password is server password.
Password string `yaml:"password" mapstructure:"password"`
// Server cache DB name.
// DB is server cache DB name.
DB int `yaml:"db" mapstructure:"db"`
// Server broker DB name.
// BrokerDB is server broker DB name.
BrokerDB int `yaml:"brokerDB" mapstructure:"brokerDB"`
// Server backend DB name.
// BackendDB is server backend DB name.
BackendDB int `yaml:"backendDB" mapstructure:"backendDB"`
}

View File

@ -77,13 +77,14 @@ func TestManagerConfig_Load(t *testing.T) {
Migrate: true,
},
Redis: RedisConfig{
Host: "bar",
Password: "bar",
Addrs: []string{"foo", "bar"},
Port: 6379,
DB: 0,
BrokerDB: 1,
BackendDB: 2,
Host: "bar",
Password: "bar",
Addrs: []string{"foo", "bar"},
MasterName: "baz",
Port: 6379,
DB: 0,
BrokerDB: 1,
BackendDB: 2,
},
},
Cache: CacheConfig{

View File

@ -39,6 +39,7 @@ database:
migrate: true
redis:
addrs: [foo, bar]
masterName: "baz"
password: bar
host: bar
port: 6379

View File

@ -27,10 +27,11 @@ import (
func NewRedis(cfg *config.RedisConfig) (redis.UniversalClient, error) {
redis.SetLogger(&redisLogger{})
client := redis.NewUniversalClient(&redis.UniversalOptions{
Addrs: cfg.Addrs,
DB: cfg.DB,
Username: cfg.Username,
Password: cfg.Password,
Addrs: cfg.Addrs,
MasterName: cfg.MasterName,
DB: cfg.DB,
Username: cfg.Username,
Password: cfg.Password,
})
if err := client.Ping(context.Background()).Err(); err != nil {

View File

@ -28,11 +28,12 @@ type Job struct {
func New(cfg *config.Config) (*Job, error) {
j, err := internaljob.New(&internaljob.Config{
Addrs: cfg.Database.Redis.Addrs,
Username: cfg.Database.Redis.Username,
Password: cfg.Database.Redis.Password,
BrokerDB: cfg.Database.Redis.BrokerDB,
BackendDB: cfg.Database.Redis.BackendDB,
Addrs: cfg.Database.Redis.Addrs,
MasterName: cfg.Database.Redis.MasterName,
Username: cfg.Database.Redis.Username,
Password: cfg.Database.Redis.Password,
BrokerDB: cfg.Database.Redis.BrokerDB,
BackendDB: cfg.Database.Redis.BackendDB,
}, internaljob.GlobalQueue)
if err != nil {
return nil, err

View File

@ -235,19 +235,22 @@ type RedisConfig struct {
// DEPRECATED: Please use the `addrs` field instead.
Port int `yaml:"port" mapstructure:"port"`
// Server addresses.
// Addrs is server addresses.
Addrs []string `yaml:"addrs" mapstructure:"addrs"`
// Server username.
// MasterName is the sentinel master name.
MasterName string `yaml:"masterName" mapstructure:"masterName"`
// Username is server username.
Username string `yaml:"username" mapstructure:"username"`
// Server password.
// Password is server password.
Password string `yaml:"password" mapstructure:"password"`
// Broker database name.
// BrokerDB is broker database name.
BrokerDB int `yaml:"brokerDB" mapstructure:"brokerDB"`
// Backend database name.
// BackendDB is backend database name.
BackendDB int `yaml:"backendDB" mapstructure:"backendDB"`
}

View File

@ -85,12 +85,13 @@ func TestConfig_Load(t *testing.T) {
SchedulerWorkerNum: 1,
LocalWorkerNum: 5,
Redis: RedisConfig{
Addrs: []string{"foo", "bar"},
Host: "127.0.0.1",
Port: 6379,
Password: "foo",
BrokerDB: 1,
BackendDB: 2,
Addrs: []string{"foo", "bar"},
MasterName: "baz",
Host: "127.0.0.1",
Port: 6379,
Password: "foo",
BrokerDB: 1,
BackendDB: 2,
},
},
Storage: StorageConfig{

View File

@ -51,6 +51,7 @@ job:
localWorkerNum: 5
redis:
addrs: ["foo", "bar"]
masterName: "baz"
host: 127.0.0.1
port: 6379
password: foo

View File

@ -57,11 +57,12 @@ type job struct {
func New(cfg *config.Config, resource resource.Resource) (Job, error) {
redisConfig := &internaljob.Config{
Addrs: cfg.Job.Redis.Addrs,
Username: cfg.Job.Redis.Username,
Password: cfg.Job.Redis.Password,
BrokerDB: cfg.Job.Redis.BrokerDB,
BackendDB: cfg.Job.Redis.BackendDB,
Addrs: cfg.Job.Redis.Addrs,
MasterName: cfg.Job.Redis.MasterName,
Username: cfg.Job.Redis.Username,
Password: cfg.Job.Redis.Password,
BrokerDB: cfg.Job.Redis.BrokerDB,
BackendDB: cfg.Job.Redis.BackendDB,
}
globalJob, err := internaljob.New(redisConfig, internaljob.GlobalQueue)