Skip to content

Commit

Permalink
Remove SimpleConnPool, remove custom pools
Browse files Browse the repository at this point in the history
Remove custom pools so that the driver can be simplified and
make more assumptions about the underlying conneciton pool whilst
still allowing configuration of host select and conn selection policies.
  • Loading branch information
Zariel committed Oct 17, 2015
1 parent 5540bc4 commit 50b9680
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 517 deletions.
2 changes: 1 addition & 1 deletion cass1batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func TestProto1BatchInsert(t *testing.T) {
session := createSession(t)
if err := createTable(session, "CREATE TABLE large (id int primary key)"); err != nil {
t.Fatal("create table:", err)
t.Fatal(err)
}
defer session.Close()

Expand Down
24 changes: 13 additions & 11 deletions cassandra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func createKeyspace(tb testing.TB, cluster *ClusterConfig, keyspace string) {
}

// should reuse the same conn apparently
conn := session.Pool.Pick(nil)
conn := session.pool.Pick(nil)
if conn == nil {
tb.Fatal("no connections available in the pool")
}
Expand Down Expand Up @@ -189,7 +189,9 @@ func TestRingDiscovery(t *testing.T) {
time.Sleep(*flagAutoWait)
}

size := len(session.Pool.(*SimplePool).connPool)
session.pool.mu.RLock()
size := len(session.pool.hostConnPools)
session.pool.mu.RUnlock()

if *clusterSize != size {
t.Logf("WARN: Expected a cluster size of %d, but actual size was %d", *clusterSize, size)
Expand Down Expand Up @@ -1139,7 +1141,7 @@ func injectInvalidPreparedStatement(t *testing.T, session *Session, table string
t.Fatal("create:", err)
}
stmt := "INSERT INTO " + table + " (foo, bar) VALUES (?, 7)"
conn := session.Pool.Pick(nil)
conn := session.pool.Pick(nil)
flight := new(inflightPrepare)
stmtsLRU.Lock()
stmtsLRU.lru.Add(conn.addr+stmt, flight)
Expand All @@ -1165,7 +1167,7 @@ func injectInvalidPreparedStatement(t *testing.T, session *Session, table string

func TestMissingSchemaPrepare(t *testing.T) {
s := createSession(t)
conn := s.Pool.Pick(nil)
conn := s.pool.Pick(nil)
defer s.Close()

insertQry := &Query{stmt: "INSERT INTO invalidschemaprep (val) VALUES (?)", values: []interface{}{5}, cons: s.cons,
Expand Down Expand Up @@ -1214,7 +1216,7 @@ func TestQueryInfo(t *testing.T) {
session := createSession(t)
defer session.Close()

conn := session.Pool.Pick(nil)
conn := session.pool.Pick(nil)
info, err := conn.prepareStatement("SELECT release_version, host_id FROM system.local WHERE key = ?", nil)

if err != nil {
Expand Down Expand Up @@ -2016,7 +2018,7 @@ func TestRoutingKey(t *testing.T) {
// Integration test of the token-aware policy-based connection pool
func TestTokenAwareConnPool(t *testing.T) {
cluster := createCluster()
cluster.ConnPoolType = NewTokenAwareConnPool
cluster.PoolConfig.HostSelectionPolicy = TokenAwareHostPolicy(RoundRobinHostPolicy())
cluster.DiscoverHosts = true

// Drop and re-create the keyspace once. Different tests should use their own
Expand All @@ -2037,8 +2039,8 @@ func TestTokenAwareConnPool(t *testing.T) {
time.Sleep(*flagAutoWait)
}

if session.Pool.Size() != cluster.NumConns*len(cluster.Hosts) {
t.Errorf("Expected pool size %d but was %d", cluster.NumConns*len(cluster.Hosts), session.Pool.Size())
if session.pool.Size() != cluster.NumConns*len(cluster.Hosts) {
t.Errorf("Expected pool size %d but was %d", cluster.NumConns*len(cluster.Hosts), session.pool.Size())
}

if err := createTable(session, "CREATE TABLE test_token_aware (id int, data text, PRIMARY KEY (id))"); err != nil {
Expand Down Expand Up @@ -2077,7 +2079,7 @@ func TestStream0(t *testing.T) {
break
}

conn = session.Pool.Pick(nil)
conn = session.pool.Pick(nil)
}

if conn == nil {
Expand Down Expand Up @@ -2116,7 +2118,7 @@ func TestNegativeStream(t *testing.T) {
break
}

conn = session.Pool.Pick(nil)
conn = session.pool.Pick(nil)
}

if conn == nil {
Expand Down Expand Up @@ -2222,7 +2224,7 @@ func TestLexicalUUIDType(t *testing.T) {
// Issue 475
func TestSessionBindRoutingKey(t *testing.T) {
cluster := createCluster()
cluster.ConnPoolType = NewTokenAwareConnPool
cluster.PoolConfig.HostSelectionPolicy = TokenAwareHostPolicy(RoundRobinHostPolicy())

session := createSessionFromCluster(cluster, t)
defer session.Close()
Expand Down
32 changes: 30 additions & 2 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,33 @@ type DiscoveryConfig struct {
Sleep time.Duration
}

// PoolConfig configures the connection pool used by the driver, it defaults to
// using a round robbin host selection policy and a round robbin connection selection
// policy for each host.
type PoolConfig struct {
// HostSelectionPolicy sets the policy for selecting which host to use for a
// given query (default: RoundRobinHostPolicy())
HostSelectionPolicy HostSelectionPolicy

// ConnSelectionPolicy sets the policy factory for selecting a connection to use for
// each host for a query (default: RoundRobinConnPolicy())
ConnSelectionPolicy func() ConnSelectionPolicy
}

func (p PoolConfig) buildPool(cfg *ClusterConfig) (*policyConnPool, error) {
hostSelection := p.HostSelectionPolicy
if hostSelection == nil {
hostSelection = RoundRobinHostPolicy()
}

connSelection := p.ConnSelectionPolicy
if connSelection == nil {
connSelection = RoundRobinConnPolicy()
}

return newPolicyConnPool(cfg, hostSelection, connSelection)
}

// ClusterConfig is a struct to configure the default cluster implementation
// of gocoql. It has a varity of attributes that can be used to modify the
// behavior to fit the most common use cases. Applications that requre a
Expand All @@ -68,7 +95,6 @@ type ClusterConfig struct {
Authenticator Authenticator // authenticator (default: nil)
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)
MaxPreparedStmts int // Sets the maximum cache size for prepared statements globally for gocql (default: 1000)
MaxRoutingKeyInfo int // Sets the maximum cache size for query info about statements for each session (default: 1000)
Expand All @@ -77,6 +103,9 @@ type ClusterConfig struct {
Discovery DiscoveryConfig
SslOpts *SslOptions
DefaultTimestamp bool // Sends a client side timestamp for all requests which overrides the timestamp at which it arrives at the server. (default: true, only enabled for protocol 3 and above)
// PoolConfig configures the underlying connection pool, allowing the
// configuration of host selection and connection selection policies.
PoolConfig PoolConfig
}

// NewCluster generates a new config for the default cluster implementation.
Expand All @@ -89,7 +118,6 @@ func NewCluster(hosts ...string) *ClusterConfig {
Port: 9042,
NumConns: 2,
Consistency: Quorum,
ConnPoolType: NewSimplePool,
DiscoverHosts: false,
MaxPreparedStmts: defaultMaxPreparedStmts,
MaxRoutingKeyInfo: 1000,
Expand Down
13 changes: 7 additions & 6 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,7 @@ func TestConnClosing(t *testing.T) {
wg.Wait()

time.Sleep(1 * time.Second) //Sleep so the fillPool can complete.
pool := db.Pool.(ConnectionPool)
conns := pool.Size()
conns := db.pool.Size()

if conns != numConns {
t.Errorf("Expected to have %d connections but have %d", numConns, conns)
Expand Down Expand Up @@ -390,7 +389,8 @@ func TestRoundRobinConnPoolRoundRobin(t *testing.T) {

// create a new cluster using the policy-based round robin conn pool
cluster := NewCluster(addrs...)
cluster.ConnPoolType = NewRoundRobinConnPool
cluster.PoolConfig.HostSelectionPolicy = RoundRobinHostPolicy()
cluster.PoolConfig.ConnSelectionPolicy = RoundRobinConnPolicy()

db, err := cluster.CreateSession()
if err != nil {
Expand Down Expand Up @@ -420,7 +420,7 @@ func TestRoundRobinConnPoolRoundRobin(t *testing.T) {

// wait for the pool to drain
time.Sleep(100 * time.Millisecond)
size := db.Pool.Size()
size := db.pool.Size()
if size != 0 {
t.Errorf("connection pool did not drain, still contains %d connections", size)
}
Expand Down Expand Up @@ -450,7 +450,8 @@ func TestPolicyConnPoolSSL(t *testing.T) {
defer srv.Stop()

cluster := createTestSslCluster(srv.Address, defaultProto, true)
cluster.ConnPoolType = NewRoundRobinConnPool
cluster.PoolConfig.HostSelectionPolicy = RoundRobinHostPolicy()
cluster.PoolConfig.ConnSelectionPolicy = RoundRobinConnPolicy()

db, err := cluster.CreateSession()
if err != nil {
Expand All @@ -465,7 +466,7 @@ func TestPolicyConnPoolSSL(t *testing.T) {

// wait for the pool to drain
time.Sleep(100 * time.Millisecond)
size := db.Pool.Size()
size := db.pool.Size()
if size != 0 {
t.Errorf("connection pool did not drain, still contains %d connections", size)
}
Expand Down
Loading

0 comments on commit 50b9680

Please sign in to comment.