Skip to content

Commit

Permalink
improve ConnectAddress() (apache#961)
Browse files Browse the repository at this point in the history
Panic in connect address if we are unable to use any host. Improve host
string method to show which address we use.
  • Loading branch information
Zariel authored Aug 13, 2017
1 parent 7ebc9cf commit b9337fa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
46 changes: 32 additions & 14 deletions host_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,30 @@ func (h *HostInfo) setPeer(peer net.IP) *HostInfo {
}

func (h *HostInfo) invalidConnectAddr() bool {
addr := h.ConnectAddress()
return addr == nil || addr.IsUnspecified()
h.mu.RLock()
defer h.mu.RUnlock()
addr, _ := h.connectAddressLocked()
return !validIpAddr(addr)
}

func validIpAddr(addr net.IP) bool {
return addr != nil && !addr.IsUnspecified()
}

func (h *HostInfo) connectAddressLocked() (net.IP, string) {
if validIpAddr(h.connectAddress) {
return h.connectAddress, "connect_address"
} else if validIpAddr(h.rpcAddress) {
return h.rpcAddress, "rpc_adress"
} else if validIpAddr(h.preferredIP) {
// where does perferred_ip get set?
return h.preferredIP, "preferred_ip"
} else if validIpAddr(h.broadcastAddress) {
return h.broadcastAddress, "broadcast_address"
} else if validIpAddr(h.peer) {
return h.peer, "peer"
}
return net.IPv4zero, "invalid"
}

// Returns the address that should be used to connect to the host.
Expand All @@ -160,18 +182,10 @@ func (h *HostInfo) ConnectAddress() net.IP {
h.mu.RLock()
defer h.mu.RUnlock()

if h.connectAddress == nil {
// Use 'rpc_address' if provided and it's not 0.0.0.0
if h.rpcAddress != nil && !h.rpcAddress.IsUnspecified() {
return h.rpcAddress
} else if h.broadcastAddress != nil && !h.broadcastAddress.IsUnspecified() {
return h.broadcastAddress
} else if h.peer != nil {
// Peer should always be set if this from 'system.peer'
return h.peer
}
if addr, _ := h.connectAddressLocked(); validIpAddr(addr) {
return addr
}
return h.connectAddress
panic(fmt.Sprintf("no valid connect address for host: %v. Is your cluster configured correctly?", h))
}

func (h *HostInfo) SetConnectAddress(address net.IP) *HostInfo {
Expand Down Expand Up @@ -343,9 +357,13 @@ func (h *HostInfo) IsUp() bool {
func (h *HostInfo) String() string {
h.mu.RLock()
defer h.mu.RUnlock()

connectAddr, source := h.connectAddressLocked()
return fmt.Sprintf("[HostInfo connectAddress=%q peer=%q rpc_address=%q broadcast_address=%q "+
"preferred_ip=%q connect_addr=%q connect_addr_source=%q "+
"port=%d data_centre=%q rack=%q host_id=%q version=%q state=%s num_tokens=%d]",
h.connectAddress, h.peer, h.rpcAddress, h.broadcastAddress,
h.connectAddress, h.peer, h.rpcAddress, h.broadcastAddress, h.preferredIP,
connectAddr, source,
h.port, h.dataCenter, h.rack, h.hostId, h.version, h.state, len(h.tokens))
}

Expand Down
2 changes: 1 addition & 1 deletion policies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func TestTokenAwareNilHostInfo(t *testing.T) {
func TestCOWList_Add(t *testing.T) {
var cow cowHostList

toAdd := [...]net.IP{net.IPv4(0, 0, 0, 0), net.IPv4(1, 0, 0, 0), net.IPv4(2, 0, 0, 0)}
toAdd := [...]net.IP{net.IPv4(10, 0, 0, 1), net.IPv4(10, 0, 0, 2), net.IPv4(10, 0, 0, 3)}

for _, addr := range toAdd {
if !cow.add(&HostInfo{connectAddress: addr}) {
Expand Down

0 comments on commit b9337fa

Please sign in to comment.