Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

Commit 17d2658

Browse files
committed
move cassandra session into cassandra in root
1 parent 8667be4 commit 17d2658

File tree

3 files changed

+46
-46
lines changed

3 files changed

+46
-46
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package util
1+
package cassandra
22

33
import (
44
"sync"
@@ -8,8 +8,8 @@ import (
88
log "github.com/sirupsen/logrus"
99
)
1010

11-
// CassandraSession stores a connection to Cassandra along with associated configurations
12-
type CassandraSession struct {
11+
// Session stores a connection to Cassandra along with associated configurations
12+
type Session struct {
1313
session *gocql.Session
1414
cluster *gocql.ClusterConfig
1515
shutdown chan struct{}
@@ -20,22 +20,22 @@ type CassandraSession struct {
2020
sync.RWMutex
2121
}
2222

23-
// NewCassandraSession creates and returns a CassandraSession
24-
func NewCassandraSession(session *gocql.Session,
23+
// NewSession creates and returns a CassandraSession
24+
func NewSession(session *gocql.Session,
2525
clusterConfig *gocql.ClusterConfig,
2626
shutdown chan struct{},
2727
timeout time.Duration,
2828
interval time.Duration,
2929
addrs string,
30-
logPrefix string) *CassandraSession {
30+
logPrefix string) *Session {
3131
if clusterConfig == nil {
32-
panic("NewCassandraSession received nil pointer for ClusterConfig")
32+
panic("NewSession received nil pointer for ClusterConfig")
3333
}
3434
if session == nil {
35-
panic("NewCassandraSession received nil pointer for session")
35+
panic("NewSession received nil pointer for session")
3636
}
3737

38-
cs := &CassandraSession{
38+
cs := &Session{
3939
session: session,
4040
cluster: clusterConfig,
4141
shutdown: shutdown,
@@ -53,46 +53,46 @@ func NewCassandraSession(session *gocql.Session,
5353
// if it cannot query Cassandra for longer than connectionCheckTimeout it will create a new session
5454
//
5555
// if you are not using a WaitGroup in the caller, just pass in nil
56-
func (c *CassandraSession) DeadConnectionCheck(wg *sync.WaitGroup) {
56+
func (s *Session) DeadConnectionCheck(wg *sync.WaitGroup) {
5757
if wg != nil {
5858
defer wg.Done()
5959
}
6060

61-
ticker := time.NewTicker(time.Second * c.connectionCheckInterval)
61+
ticker := time.NewTicker(time.Second * s.connectionCheckInterval)
6262
var totaltime time.Duration
6363
var err error
6464
var oldSession *gocql.Session
6565

6666
OUTER:
6767
for {
68-
// connection to cassandra has been down for longer than the configured timeout
69-
if totaltime >= c.connectionCheckTimeout {
70-
c.Lock()
68+
// connection to Cassandra has been down for longer than the configured timeout
69+
if totaltime >= s.connectionCheckTimeout {
70+
s.Lock()
7171
for {
7272
select {
73-
case <-c.shutdown:
74-
log.Infof("%s: received shutdown, exiting deadConnectionCheck", c.logPrefix)
75-
if c.session != nil && !c.session.Closed() {
76-
c.session.Close()
73+
case <-s.shutdown:
74+
log.Infof("%s: received shutdown, exiting deadConnectionCheck", s.logPrefix)
75+
if s.session != nil && !s.session.Closed() {
76+
s.session.Close()
7777
}
7878
// make sure we unlock the sessionLock before returning
79-
c.Unlock()
79+
s.Unlock()
8080
return
8181
default:
82-
log.Errorf("%s: creating new session to cassandra using hosts: %v", c.logPrefix, c.addrs)
83-
if c.session != nil && !c.session.Closed() && oldSession == nil {
84-
oldSession = c.session
82+
log.Errorf("%s: creating new session to cassandra using hosts: %v", s.logPrefix, s.addrs)
83+
if s.session != nil && !s.session.Closed() && oldSession == nil {
84+
oldSession = s.session
8585
}
86-
c.session, err = c.cluster.CreateSession()
86+
s.session, err = s.cluster.CreateSession()
8787
if err != nil {
88-
log.Errorf("%s: error while attempting to recreate cassandra session. will retry after %v: %v", c.logPrefix, c.connectionCheckInterval.String(), err)
89-
time.Sleep(c.connectionCheckInterval)
90-
totaltime += c.connectionCheckInterval
88+
log.Errorf("%s: error while attempting to recreate cassandra session. will retry after %v: %v", s.logPrefix, s.connectionCheckInterval.String(), err)
89+
time.Sleep(s.connectionCheckInterval)
90+
totaltime += s.connectionCheckInterval
9191
// continue inner loop to attempt to reconnect
9292
continue
9393
}
94-
c.Unlock()
95-
log.Errorf("%s: reconnecting to cassandra took %v", c.logPrefix, (totaltime - c.connectionCheckTimeout).String())
94+
s.Unlock()
95+
log.Errorf("%s: reconnecting to cassandra took %v", s.logPrefix, (totaltime - s.connectionCheckTimeout).String())
9696
totaltime = 0
9797
if oldSession != nil {
9898
oldSession.Close()
@@ -105,33 +105,33 @@ OUTER:
105105
}
106106

107107
select {
108-
case <-c.shutdown:
109-
log.Infof("%s: received shutdown, exiting deadConnectionCheck", c.logPrefix)
110-
if c.session != nil && !c.session.Closed() {
111-
c.session.Close()
108+
case <-s.shutdown:
109+
log.Infof("%s: received shutdown, exiting deadConnectionCheck", s.logPrefix)
110+
if s.session != nil && !s.session.Closed() {
111+
s.session.Close()
112112
}
113113
return
114114
case <-ticker.C:
115-
c.RLock()
115+
s.RLock()
116116
// this query should work on all cassandra deployments, but we may need to revisit this
117-
err = c.session.Query("SELECT cql_version FROM system.local").Exec()
117+
err = s.session.Query("SELECT cql_version FROM system.local").Exec()
118118
if err == nil {
119119
totaltime = 0
120120
} else {
121-
totaltime += c.connectionCheckInterval
122-
log.Errorf("%s: could not execute connection check query for %v: %v", c.logPrefix, totaltime.String(), err)
121+
totaltime += s.connectionCheckInterval
122+
log.Errorf("%s: could not execute connection check query for %v: %v", s.logPrefix, totaltime.String(), err)
123123
}
124-
c.RUnlock()
124+
s.RUnlock()
125125
}
126126
}
127127
}
128128

129-
// CurrentSession retrieves the current active cassandra session
129+
// CurrentSession retrieves the current active Cassandra session
130130
//
131131
// If the connection to Cassandra is down, this will block until it can be restored
132-
func (c *CassandraSession) CurrentSession() *gocql.Session {
133-
c.RLock()
134-
session := c.session
135-
c.RUnlock()
132+
func (s *Session) CurrentSession() *gocql.Session {
133+
s.RLock()
134+
session := s.session
135+
s.RUnlock()
136136
return session
137137
}

idx/cassandra/cassandra.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ type CasIdx struct {
6161
memory.MemoryIndex
6262
Config *IdxConfig
6363
cluster *gocql.ClusterConfig
64-
Session *util.CassandraSession
64+
Session *cassandra.Session
6565
metaRecords metaRecordStatusByOrg
6666
writeQueue chan writeReq
6767
shutdown chan struct{}
@@ -177,7 +177,7 @@ func (c *CasIdx) InitBare() error {
177177
return fmt.Errorf("cassandra-idx: failed to create cassandra session: %s", err)
178178
}
179179

180-
c.Session = util.NewCassandraSession(session, c.cluster, c.shutdown, c.Config.ConnectionCheckTimeout, c.Config.ConnectionCheckInterval, c.Config.Hosts, "cassnadra-idx")
180+
c.Session = cassandra.NewSession(session, c.cluster, c.shutdown, c.Config.ConnectionCheckTimeout, c.Config.ConnectionCheckInterval, c.Config.Hosts, "cassnadra-idx")
181181

182182
return nil
183183
}

store/cassandra/cassandra.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ type ChunkReadRequest struct {
7878
}
7979

8080
type CassandraStore struct {
81-
Session *util.CassandraSession
81+
Session *cassandra.Session
8282
cluster *gocql.ClusterConfig
8383
writeQueues []chan *mdata.ChunkWriteRequest
8484
writeQueueMeters []*stats.Range32
@@ -225,7 +225,7 @@ func NewCassandraStore(config *StoreConfig, ttls []uint32) (*CassandraStore, err
225225

226226
sd := make(chan struct{})
227227

228-
cs := util.NewCassandraSession(session, cluster, sd, config.ConnectionCheckTimeout, config.ConnectionCheckInterval, config.Addrs, "cassandra_store")
228+
cs := cassandra.NewSession(session, cluster, sd, config.ConnectionCheckTimeout, config.ConnectionCheckInterval, config.Addrs, "cassandra_store")
229229

230230
log.Debugf("cassandra_store: created session with config %+v", config)
231231
c := &CassandraStore{

0 commit comments

Comments
 (0)