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

REDIS_ADDR should always be used for master/slave connections #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 17 additions & 4 deletions resec/consul/manager.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package consul

import (
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -200,7 +201,7 @@ func (m *Manager) registerService(redisState state.Redis) {
serviceID := m.getConsulServiceID()
replicationStatus := m.getReplicationStatus()

m.config.serviceID = serviceID + "@" + m.config.announceAddr
m.config.serviceID = serviceID + "@" + m.config.redisAddr
m.config.checkID = m.config.serviceID + ":replication-status-check"

serviceInfo := &consulapi.AgentServiceRegistration{
Expand Down Expand Up @@ -322,13 +323,25 @@ func (m *Manager) watchConsulMasterService() {

master := services[0]

if m.state.MasterAddr == master.Node.Address && m.state.MasterPort == master.Service.Port {
parts := strings.Split(master.Service.ID, "@")
if (len(parts) != 2) {
m.logger.Error("Couldn't parse master address found in Consul catalog")
continue
}
masterHost := strings.Split(parts[1], ":")[0]
masterPort, err := strconv.Atoi(strings.Split(parts[1], ":")[1])
if err != nil {
m.logger.Error("Couldn't parse master address found in Consul catalog")
continue
}

if m.state.MasterAddr == masterHost && m.state.MasterPort == masterPort {
m.logger.Debugf("No change in master service configuration")
continue
}

m.state.MasterAddr = master.Node.Address
m.state.MasterPort = master.Service.Port
m.state.MasterAddr = masterHost
m.state.MasterPort = masterPort
m.emit()

m.logger.Infof("Saw change in master service. New IP+Port is: %s:%d", m.state.MasterAddr, m.state.MasterPort)
Expand Down
11 changes: 6 additions & 5 deletions resec/consul/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

func NewConnection(c *cli.Context, redisConfig redis.Config) (*Manager, error) {
consulConfig := &config{
redisAddr: redisConfig.Address,
deregisterServiceAfter: c.Duration("consul-deregister-service-after"),
lockKey: c.String("consul-lock-key"),
lockMonitorRetries: c.Int("consul-lock-monitor-retries"),
Expand Down Expand Up @@ -58,17 +59,17 @@ func NewConnection(c *cli.Context, redisConfig redis.Config) (*Manager, error) {
redisHost := strings.Split(redisConfig.Address, ":")[0]
redisPort := strings.Split(redisConfig.Address, ":")[1]
if redisHost == "127.0.0.1" || redisHost == "localhost" || redisHost == "::1" {
consulConfig.announceAddr = ":" + redisPort
announceAddr = ":" + redisPort
} else {
consulConfig.announceAddr = redisConfig.Address
announceAddr = redisConfig.Address
}
}

var err error
consulConfig.announceHost = strings.Split(consulConfig.announceAddr, ":")[0]
consulConfig.announcePort, err = strconv.Atoi(strings.Split(consulConfig.announceAddr, ":")[1])
consulConfig.announceHost = strings.Split(announceAddr, ":")[0]
consulConfig.announcePort, err = strconv.Atoi(strings.Split(announceAddr, ":")[1])
if err != nil {
return nil, fmt.Errorf("Trouble extracting port number from [%s]", redisConfig.Address)
return nil, fmt.Errorf("Trouble extracting port number from [%s]", announceAddr)
}

instance := &Manager{
Expand Down
2 changes: 1 addition & 1 deletion resec/consul/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Manager struct {

// Consul config used for internal state management
type config struct {
announceAddr string // address (IP:port) to announce to Consul
redisAddr string // actual redis address of this node used for master/slave reporting inside redis
announceHost string // host (IP) to announce to Consul
announcePort int // port to announce to Consul
checkID string // consul check ID
Expand Down