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

Create Session Discovers Other Hosts #181

Merged
merged 11 commits into from
Jun 17, 2014
Empty file added .gitignore
Empty file.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ Miguel Serrano <miguelvps@gmail.com>
Stefan Radomski <gibheer@zero-knowledge.org>
Josh Wright <jshwright@gmail.com>
Jacob Rhoden <jacob.rhoden@gmail.com>
Ben Frye <benfrye@gmail.com>
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Features
* Round robin distribution of queries to different hosts
* Round robin distribution of queries to different connections on a host
* Each connection can execute up to 128 concurrent queries
* Optional automatic discovery of nodes
* Iteration over paged results with configurable page size
* Optional frame compression (using snappy)
* Automatic query preparation
Expand Down
45 changes: 36 additions & 9 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,22 @@ type ClusterConfig struct {
RetryPolicy RetryPolicy // Default retry policy to use for queries (default: 0)
SocketKeepalive time.Duration // The keepalive period to use, enabled if > 0 (default: 0)
ConnPoolType NewPoolFunc // The function used to create the connection pool for the session (default: NewSimplePool)
DiscoverHosts bool // If set, gocql will attempt to automatically discover other members of the Cassandra cluster (default: false)
}

// NewCluster generates a new config for the default cluster implementation.
func NewCluster(hosts ...string) *ClusterConfig {
cfg := &ClusterConfig{
Hosts: hosts,
CQLVersion: "3.0.0",
ProtoVersion: 2,
Timeout: 600 * time.Millisecond,
DefaultPort: 9042,
NumConns: 2,
NumStreams: 128,
Consistency: Quorum,
ConnPoolType: NewSimplePool,
Hosts: hosts,
CQLVersion: "3.0.0",
ProtoVersion: 2,
Timeout: 600 * time.Millisecond,
DefaultPort: 9042,
NumConns: 2,
NumStreams: 128,
Consistency: Quorum,
ConnPoolType: NewSimplePool,
DiscoverHosts: false,
}
return cfg
}
Expand All @@ -60,6 +62,30 @@ func (cfg *ClusterConfig) CreateSession() (*Session, error) {
if pool.Size() > 0 {
s := NewSession(pool, *cfg)
s.SetConsistency(cfg.Consistency)

if cfg.DiscoverHosts {
//Fill out cfg.Hosts
query := "SELECT peer FROM system.peers"
peers := s.Query(query).Iter()

var ip string
for peers.Scan(&ip) {
exists := false
for ii := 0; ii < len(cfg.Hosts); ii++ {
if cfg.Hosts[ii] == ip {
exists = true
}
}
if !exists {
cfg.Hosts = append(cfg.Hosts, ip)
}
}

if err := peers.Close(); err != nil {
return s, ErrHostQueryFailed
}
}

return s, nil
}

Expand All @@ -71,4 +97,5 @@ func (cfg *ClusterConfig) CreateSession() (*Session, error) {
var (
ErrNoHosts = errors.New("no hosts provided")
ErrNoConnectionsStarted = errors.New("no connections were made when creating the session")
ErrHostQueryFailed = errors.New("unable to populate Hosts")
)