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

Connect to Redis with Setinel #180

Merged
merged 1 commit into from
Feb 8, 2021
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
9 changes: 9 additions & 0 deletions docs/en/redis_best_practices.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ There're some [fundamental things](https://redis.io/topics/sentinel#fundamental-

Please read the [official documentation](https://redis.io/topics/sentinel) for more information.

Once Redis servers and Sentinels are deployed, the `REDIS-URL` can be specified as `[redis[s]://][USER:PASSWORD@]MASTERNAME,SENTINEL_ADDRS:SENTINEL_PORT[/DB]`, for example,

```
$ ./juicefs mount rediss://:sentinelPass@masterName,1.2.3.4,1.2.5.6:5000/2
```
**Note** The default port for Sentinel is 26379, but the above URL use 6379 (default port for Redis server) as the default, so the port for Sentinel is not optional.

**Note** When the password is provided in the URL, it will also be used to connect Redis server. If they have different passwords, the passwords should be specified by enviroment viarables (`SENTINEL_PASSWORD` and `REDIS_PASSWORD`) separately.

## Data Durability

Redis provides a different range of [persistence](https://redis.io/topics/persistence) options:
Expand Down
46 changes: 40 additions & 6 deletions pkg/meta/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"hash/fnv"
"math/rand"
"net"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -114,13 +115,46 @@ func NewRedisMeta(url string, conf *RedisConfig) (Meta, error) {
if err != nil {
return nil, fmt.Errorf("parse %s: %s", url, err)
}
if opt.Password == "" && os.Getenv("REDIS_PASSWORD") != "" {
opt.Password = os.Getenv("REDIS_PASSWORD")
var rdb *redis.Client
if strings.Contains(opt.Addr, ",") {
var fopt redis.FailoverOptions
ps := strings.Split(opt.Addr, ",")
fopt.MasterName = ps[0]
fopt.SentinelAddrs = ps[1:]
_, port, _ := net.SplitHostPort(fopt.SentinelAddrs[len(fopt.SentinelAddrs)-1])
if port != "" {
for i := range fopt.SentinelAddrs {
h, p, _ := net.SplitHostPort(fopt.SentinelAddrs[i])
if p == "" {
fopt.SentinelAddrs[i] = net.JoinHostPort(h, port)
}
}
}
// Assume Redis server and sentinel have the same password.
fopt.SentinelPassword = opt.Password
fopt.Username = opt.Username
fopt.Password = opt.Password
if fopt.SentinelPassword == "" && os.Getenv("SENTINEL_PASSWORD") != "" {
fopt.SentinelPassword = os.Getenv("SENTINEL_PASSWORD")
}
if fopt.Password == "" && os.Getenv("REDIS_PASSWORD") != "" {
fopt.Password = os.Getenv("REDIS_PASSWORD")
}
fopt.DB = opt.DB
fopt.TLSConfig = opt.TLSConfig
fopt.MaxRetries = conf.Retries
fopt.MinRetryBackoff = time.Millisecond * 100
fopt.MaxRetryBackoff = time.Minute * 1
rdb = redis.NewFailoverClient(&fopt)
} else {
if opt.Password == "" && os.Getenv("REDIS_PASSWORD") != "" {
opt.Password = os.Getenv("REDIS_PASSWORD")
}
opt.MaxRetries = conf.Retries
opt.MinRetryBackoff = time.Millisecond * 100
opt.MaxRetryBackoff = time.Minute * 1
rdb = redis.NewClient(opt)
}
opt.MaxRetries = conf.Retries
opt.MinRetryBackoff = time.Millisecond * 100
opt.MaxRetryBackoff = time.Minute * 1
rdb := redis.NewClient(opt)
m := &redisMeta{
conf: conf,
rdb: rdb,
Expand Down