Skip to content

Commit

Permalink
session: add option to disable inital host lookup
Browse files Browse the repository at this point in the history
Add DisableInitialHostLookup which will disable the driver from looking
up the supplied host information on session creation. This will mean
that host related information will not be available, this includes
data_centre, rack and token info. As such token aware routing will not
work if enable.
  • Loading branch information
Zariel committed Jan 15, 2016
1 parent 8c70acc commit 83932d6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
7 changes: 7 additions & 0 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ type ClusterConfig struct {
// set to 10.0.0.1 which is what will be used to connect to.
IgnorePeerAddr bool

// If DisableInitialHostLookup then the driver will not attempt to get host info
// from the system.peers table, this will mean that the driver will connect to
// hosts supplied and will not attempt to lookup the hosts information, this will
// mean that data_centre, rack and token information will not be available and as
// such host filtering and token aware query routing will not be available.
DisableInitialHostLookup bool

// internal config for testing
disableControlConn bool
}
Expand Down
55 changes: 32 additions & 23 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ type Session struct {
isClosed bool
}

func addrsToHosts(addrs []string, defaultPort int) ([]*HostInfo, error) {
hosts := make([]*HostInfo, len(addrs))
for i, hostport := range addrs {
// TODO: remove duplication
addr, portStr, err := net.SplitHostPort(JoinHostPort(hostport, defaultPort))
if err != nil {
return nil, fmt.Errorf("NewSession: unable to parse hostport of addr %q: %v", hostport, err)
}

port, err := strconv.Atoi(portStr)
if err != nil {
return nil, fmt.Errorf("NewSession: invalid port for hostport of addr %q: %v", hostport, err)
}

hosts[i] = &HostInfo{peer: addr, port: port, state: NodeUp}
}

return hosts, nil
}

// NewSession wraps an existing Node.
func NewSession(cfg ClusterConfig) (*Session, error) {
//Check that hosts in the ClusterConfig is not empty
Expand Down Expand Up @@ -109,38 +129,27 @@ func NewSession(cfg ClusterConfig) (*Session, error) {
// need to setup host source to check for broadcast_address in system.local
localHasRPCAddr, _ := checkSystemLocal(s.control)
s.hostSource.localHasRpcAddr = localHasRPCAddr
hosts, _, err = s.hostSource.GetHosts()

var err error
if cfg.DisableInitialHostLookup {
// TODO: we could look at system.local to get token and other metadata
// in this case.
hosts, err = addrsToHosts(cfg.Hosts, cfg.Port)
} else {
hosts, _, err = s.hostSource.GetHosts()
}

if err != nil {
s.Close()
return nil, err
}

for _, host := range hosts {
s.ring.addHost(host)
}

} else {
// we dont get host info
hosts = make([]*HostInfo, len(cfg.Hosts))
for i, hostport := range cfg.Hosts {
// TODO: remove duplication
addr, portStr, err := net.SplitHostPort(JoinHostPort(hostport, cfg.Port))
if err != nil {
s.Close()
return nil, fmt.Errorf("NewSession: unable to parse hostport of addr %q: %v", hostport, err)
}

port, err := strconv.Atoi(portStr)
if err != nil {
s.Close()
return nil, fmt.Errorf("NewSession: invalid port for hostport of addr %q: %v", hostport, err)
}

hosts[i] = &HostInfo{peer: addr, port: port, state: NodeUp}
}
hosts, err = addrsToHosts(cfg.Hosts, cfg.Port)
}

for _, host := range hosts {
s.ring.addHost(host)
s.handleNodeUp(net.ParseIP(host.Peer()), host.Port(), false)
}

Expand Down

0 comments on commit 83932d6

Please sign in to comment.