Skip to content

Commit 8190adf

Browse files
committed
multiple sentinels
1 parent 143123f commit 8190adf

3 files changed

Lines changed: 30 additions & 15 deletions

File tree

agent/agent.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func SaveTwemproxyConfig() {
5757

5858
func RestartTwemproxy() error {
5959
Debug("Restarting Twemproxy.")
60-
args := strings.Split(Setting.RestartArgs, string(' '))
60+
args := strings.Split(Settings.RestartArgs, string(' '))
6161
out, err := exec.Command(Settings.RestartCommand, args...).Output()
6262

6363
if err != nil {
@@ -67,9 +67,19 @@ func RestartTwemproxy() error {
6767
return err
6868
}
6969

70-
func GetSentinel() (sentinel string) {
71-
address := ComposeRedisAddress(Settings.SentinelIp, Settings.SentinelPort)
72-
return address
70+
func GetSentinel() (redis.Conn, error) {
71+
var c redis.Conn
72+
var err error
73+
for i := range Settings.Sentinels {
74+
c, err = redis.Dial("tcp", Settings.Sentinels[i])
75+
if err == nil {
76+
Debug(fmt.Sprintf("Connected to sentinel %s", Settings.Sentinels[i]))
77+
break;
78+
}
79+
Debug(fmt.Sprintf("Sentinel %s is not reachable", Settings.Sentinels[i]))
80+
}
81+
82+
return c, err
7383
}
7484

7585
func SwitchMaster(master_name string, ip string, port string) error {
@@ -84,7 +94,7 @@ func SwitchMaster(master_name string, ip string, port string) error {
8494
}
8595

8696
func ValidateCurrentMaster() error {
87-
c, err := redis.Dial("tcp", GetSentinel())
97+
c, err := GetSentinel()
8898
if err != nil {
8999
return err
90100
}
@@ -111,10 +121,9 @@ func ValidateCurrentMaster() error {
111121
}
112122

113123
func SubscribeToSentinel() {
114-
sentinel := GetSentinel()
115-
c, err := redis.Dial("tcp", sentinel)
124+
c, err := GetSentinel()
116125
if err != nil {
117-
Fatal("Cannot connect to redis sentinel:", sentinel)
126+
Fatal("Cannot connect to any sentinel.")
118127
}
119128

120129
err = ValidateCurrentMaster()
@@ -133,7 +142,15 @@ func SubscribeToSentinel() {
133142
case redis.Subscription:
134143
Debug(fmt.Sprintf("%s: %s %d", v.Channel, v.Kind, v.Count))
135144
case error:
136-
Fatal("Error with redis connection:", psc)
145+
Debug("Subscription error, trying to fallback")
146+
c, err = GetSentinel()
147+
if err != nil {
148+
Fatal("Error with redis connection:", psc)
149+
}
150+
151+
psc = redis.PubSubConn{c}
152+
Debug("Subscribing to sentinel (+switch-master).")
153+
psc.Subscribe("+switch-master")
137154
}
138155
}
139156
}

agent/settings.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ type AgentSettings struct {
1010
Verbose bool
1111
TwemproxyPoolName string `yaml:"twemproxy_pool_name"`
1212
TwemproxyConfigFile string `yaml:"twemproxy_config_file"`
13-
SentinelIp string `yaml:"sentinel_ip"`
14-
SentinelPort string `yaml:"sentinel_port"`
13+
Sentinels []string `yaml:"sentinels"`
1514
RestartCommand string `yaml:"restart_command"`
1615
RestartArgs string `yaml:"restart_args"`
1716
LogFile string `yaml:"log_file"`
@@ -22,8 +21,7 @@ var Settings AgentSettings = AgentSettings{}
2221
func ValidateSettings() {
2322
if Settings.TwemproxyPoolName == "" ||
2423
Settings.TwemproxyConfigFile == "" ||
25-
Settings.SentinelIp == "" ||
26-
Settings.SentinelPort == "" ||
24+
len(Settings.Sentinels) == 0 ||
2725
Settings.RestartCommand == "" {
2826
flag.Usage()
2927
os.Exit(1)

conf/agent.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
twemproxy_pool_name: "root"
22
twemproxy_config_file: "conf/nutcracker.yml"
3-
sentinel_ip: "127.0.0.1"
4-
sentinel_port: "26379"
3+
sentinels:
4+
- "127.0.0.1:26379"
55
restart_command: "/etc/init.d/twemproxy"
66
restart_args: "restart"
77
log_file: "agent.log"

0 commit comments

Comments
 (0)