|
| 1 | +package cassandra |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sync" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/gocql/gocql" |
| 9 | + log "github.com/sirupsen/logrus" |
| 10 | +) |
| 11 | + |
| 12 | +// Session stores a connection to Cassandra along with associated configurations |
| 13 | +type Session struct { |
| 14 | + wg sync.WaitGroup |
| 15 | + session *gocql.Session |
| 16 | + cluster *gocql.ClusterConfig |
| 17 | + shutdown chan struct{} |
| 18 | + connectionCheckTimeout time.Duration |
| 19 | + connectionCheckInterval time.Duration |
| 20 | + addrs string |
| 21 | + logPrefix string |
| 22 | + sync.RWMutex |
| 23 | +} |
| 24 | + |
| 25 | +// NewSession creates and returns a Session. Upon failure it will return nil and an error. |
| 26 | +func NewSession(clusterConfig *gocql.ClusterConfig, |
| 27 | + timeout time.Duration, |
| 28 | + interval time.Duration, |
| 29 | + addrs string, |
| 30 | + logPrefix string) (*Session, error) { |
| 31 | + if clusterConfig == nil { |
| 32 | + log.Errorf("cassandra.NewSession received nil pointer for ClusterConfig") |
| 33 | + return nil, fmt.Errorf("cassandra.NewSession received nil pointer for ClusterConfig") |
| 34 | + } |
| 35 | + |
| 36 | + session, err := clusterConfig.CreateSession() |
| 37 | + if err != nil { |
| 38 | + log.Errorf("cassandra.NewSession failed to create session: %v", err) |
| 39 | + return nil, err |
| 40 | + } |
| 41 | + |
| 42 | + cs := &Session{ |
| 43 | + session: session, |
| 44 | + cluster: clusterConfig, |
| 45 | + shutdown: make(chan struct{}), |
| 46 | + connectionCheckTimeout: timeout, |
| 47 | + connectionCheckInterval: interval, |
| 48 | + addrs: addrs, |
| 49 | + logPrefix: logPrefix, |
| 50 | + } |
| 51 | + |
| 52 | + if cs.connectionCheckInterval > 0 { |
| 53 | + cs.wg.Add(1) |
| 54 | + go cs.deadConnectionRefresh() |
| 55 | + } |
| 56 | + |
| 57 | + return cs, nil |
| 58 | + |
| 59 | +} |
| 60 | + |
| 61 | +func (s *Session) Stop() { |
| 62 | + close(s.shutdown) |
| 63 | + s.wg.Wait() |
| 64 | +} |
| 65 | + |
| 66 | +// deadConnectionRefresh will run a query using the current Cassandra session every connectionCheckInterval |
| 67 | +// if it cannot query Cassandra for longer than connectionCheckTimeout it will create a new session |
| 68 | +// |
| 69 | +// We implemented this due to an issue in gocql (https://github.com/gocql/gocql/issues/831). Once that issue is resolved |
| 70 | +// we should be able to get rid of this code. |
| 71 | +func (s *Session) deadConnectionRefresh() { |
| 72 | + defer s.wg.Done() |
| 73 | + |
| 74 | + log.Infof("%s: dead connection check enabled with an interval of %s", s.logPrefix, s.connectionCheckInterval.String()) |
| 75 | + |
| 76 | + ticker := time.NewTicker(s.connectionCheckInterval) |
| 77 | + var totaltime time.Duration |
| 78 | + var err error |
| 79 | + var oldSession *gocql.Session |
| 80 | + |
| 81 | +OUTER: |
| 82 | + for { |
| 83 | + // connection to Cassandra has been down for longer than the configured timeout |
| 84 | + if totaltime >= s.connectionCheckTimeout { |
| 85 | + s.Lock() |
| 86 | + start := time.Now() |
| 87 | + for { |
| 88 | + select { |
| 89 | + case <-s.shutdown: |
| 90 | + log.Infof("%s: received shutdown, exiting deadConnectionRefresh", s.logPrefix) |
| 91 | + if s.session != nil && !s.session.Closed() { |
| 92 | + s.session.Close() |
| 93 | + } |
| 94 | + // make sure we unlock the sessionLock before returning |
| 95 | + s.Unlock() |
| 96 | + return |
| 97 | + default: |
| 98 | + log.Errorf("%s: creating new session to cassandra using hosts: %v", s.logPrefix, s.addrs) |
| 99 | + if s.session != nil && !s.session.Closed() && oldSession == nil { |
| 100 | + oldSession = s.session |
| 101 | + } |
| 102 | + s.session, err = s.cluster.CreateSession() |
| 103 | + if err != nil { |
| 104 | + log.Errorf("%s: error while attempting to recreate cassandra session. will retry after %v: %v", s.logPrefix, s.connectionCheckInterval.String(), err) |
| 105 | + time.Sleep(s.connectionCheckInterval) |
| 106 | + totaltime += s.connectionCheckInterval |
| 107 | + // continue inner loop to attempt to reconnect |
| 108 | + continue |
| 109 | + } |
| 110 | + s.Unlock() |
| 111 | + log.Errorf("%s: reconnecting to cassandra took %s", s.logPrefix, time.Since(start).String()) |
| 112 | + totaltime = 0 |
| 113 | + if oldSession != nil { |
| 114 | + oldSession.Close() |
| 115 | + oldSession = nil |
| 116 | + } |
| 117 | + // we connected, so go back to the normal outer loop |
| 118 | + continue OUTER |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + select { |
| 124 | + case <-s.shutdown: |
| 125 | + log.Infof("%s: received shutdown, exiting deadConnectionRefresh", s.logPrefix) |
| 126 | + if s.session != nil && !s.session.Closed() { |
| 127 | + s.session.Close() |
| 128 | + } |
| 129 | + return |
| 130 | + case <-ticker.C: |
| 131 | + s.RLock() |
| 132 | + // this query should work on all cassandra deployments, but we may need to revisit this |
| 133 | + err = s.session.Query("SELECT cql_version FROM system.local").Exec() |
| 134 | + s.RUnlock() |
| 135 | + if err == nil { |
| 136 | + totaltime = 0 |
| 137 | + } else { |
| 138 | + totaltime += s.connectionCheckInterval |
| 139 | + log.Errorf("%s: could not execute connection check query for %v: %v", s.logPrefix, totaltime.String(), err) |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +// CurrentSession retrieves the current active Cassandra session |
| 146 | +// |
| 147 | +// If the connection to Cassandra is down, this will block until it can be restored |
| 148 | +func (s *Session) CurrentSession() *gocql.Session { |
| 149 | + s.RLock() |
| 150 | + session := s.session |
| 151 | + s.RUnlock() |
| 152 | + return session |
| 153 | +} |
0 commit comments