瀏覽代碼

session: add option to disable inital host lookup

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.
Chris Bannister 10 年之前
父節點
當前提交
83932d6de9
共有 2 個文件被更改,包括 39 次插入23 次删除
  1. 7 0
      cluster.go
  2. 32 23
      session.go

+ 7 - 0
cluster.go

@@ -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
 }

+ 32 - 23
session.go

@@ -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
@@ -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)
 	}