session.go 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  1. // Copyright (c) 2012 The gocql Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gocql
  5. import (
  6. "bytes"
  7. "encoding/binary"
  8. "errors"
  9. "fmt"
  10. "io"
  11. "strings"
  12. "sync"
  13. "time"
  14. "unicode"
  15. "github.com/gocql/gocql/lru"
  16. )
  17. // Session is the interface used by users to interact with the database.
  18. //
  19. // It's safe for concurrent use by multiple goroutines and a typical usage
  20. // scenario is to have one global session object to interact with the
  21. // whole Cassandra cluster.
  22. //
  23. // This type extends the Node interface by adding a convinient query builder
  24. // and automatically sets a default consinstency level on all operations
  25. // that do not have a consistency level set.
  26. type Session struct {
  27. pool *policyConnPool
  28. cons Consistency
  29. pageSize int
  30. prefetch float64
  31. routingKeyInfoCache routingKeyInfoLRU
  32. schemaDescriber *schemaDescriber
  33. trace Tracer
  34. hostSource *ringDescriber
  35. mu sync.RWMutex
  36. control *controlConn
  37. cfg ClusterConfig
  38. closeMu sync.RWMutex
  39. isClosed bool
  40. }
  41. // NewSession wraps an existing Node.
  42. func NewSession(cfg ClusterConfig) (*Session, error) {
  43. //Check that hosts in the ClusterConfig is not empty
  44. if len(cfg.Hosts) < 1 {
  45. return nil, ErrNoHosts
  46. }
  47. maxStreams := 128
  48. if cfg.ProtoVersion > protoVersion2 {
  49. maxStreams = 32768
  50. }
  51. if cfg.NumStreams <= 0 || cfg.NumStreams > maxStreams {
  52. cfg.NumStreams = maxStreams
  53. }
  54. //Adjust the size of the prepared statements cache to match the latest configuration
  55. stmtsLRU.Lock()
  56. initStmtsLRU(cfg.MaxPreparedStmts)
  57. stmtsLRU.Unlock()
  58. s := &Session{
  59. cons: cfg.Consistency,
  60. prefetch: 0.25,
  61. cfg: cfg,
  62. pageSize: cfg.PageSize,
  63. }
  64. pool, err := cfg.PoolConfig.buildPool(&s.cfg)
  65. if err != nil {
  66. return nil, err
  67. }
  68. s.pool = pool
  69. //See if there are any connections in the pool
  70. if pool.Size() == 0 {
  71. s.Close()
  72. return nil, ErrNoConnectionsStarted
  73. }
  74. s.routingKeyInfoCache.lru = lru.New(cfg.MaxRoutingKeyInfo)
  75. if !cfg.disableControlConn {
  76. s.control = createControlConn(s)
  77. }
  78. if cfg.DiscoverHosts {
  79. s.hostSource = &ringDescriber{
  80. session: s,
  81. dcFilter: cfg.Discovery.DcFilter,
  82. rackFilter: cfg.Discovery.RackFilter,
  83. closeChan: make(chan bool),
  84. }
  85. go s.hostSource.run(cfg.Discovery.Sleep)
  86. }
  87. return s, nil
  88. }
  89. // SetConsistency sets the default consistency level for this session. This
  90. // setting can also be changed on a per-query basis and the default value
  91. // is Quorum.
  92. func (s *Session) SetConsistency(cons Consistency) {
  93. s.mu.Lock()
  94. s.cons = cons
  95. s.mu.Unlock()
  96. }
  97. // SetPageSize sets the default page size for this session. A value <= 0 will
  98. // disable paging. This setting can also be changed on a per-query basis.
  99. func (s *Session) SetPageSize(n int) {
  100. s.mu.Lock()
  101. s.pageSize = n
  102. s.mu.Unlock()
  103. }
  104. // SetPrefetch sets the default threshold for pre-fetching new pages. If
  105. // there are only p*pageSize rows remaining, the next page will be requested
  106. // automatically. This value can also be changed on a per-query basis and
  107. // the default value is 0.25.
  108. func (s *Session) SetPrefetch(p float64) {
  109. s.mu.Lock()
  110. s.prefetch = p
  111. s.mu.Unlock()
  112. }
  113. // SetTrace sets the default tracer for this session. This setting can also
  114. // be changed on a per-query basis.
  115. func (s *Session) SetTrace(trace Tracer) {
  116. s.mu.Lock()
  117. s.trace = trace
  118. s.mu.Unlock()
  119. }
  120. // Query generates a new query object for interacting with the database.
  121. // Further details of the query may be tweaked using the resulting query
  122. // value before the query is executed. Query is automatically prepared
  123. // if it has not previously been executed.
  124. func (s *Session) Query(stmt string, values ...interface{}) *Query {
  125. s.mu.RLock()
  126. qry := &Query{stmt: stmt, values: values, cons: s.cons,
  127. session: s, pageSize: s.pageSize, trace: s.trace,
  128. prefetch: s.prefetch, rt: s.cfg.RetryPolicy, serialCons: s.cfg.SerialConsistency,
  129. defaultTimestamp: s.cfg.DefaultTimestamp,
  130. }
  131. s.mu.RUnlock()
  132. return qry
  133. }
  134. type QueryInfo struct {
  135. Id []byte
  136. Args []ColumnInfo
  137. Rval []ColumnInfo
  138. PKeyColumns []int
  139. }
  140. // Bind generates a new query object based on the query statement passed in.
  141. // The query is automatically prepared if it has not previously been executed.
  142. // The binding callback allows the application to define which query argument
  143. // values will be marshalled as part of the query execution.
  144. // During execution, the meta data of the prepared query will be routed to the
  145. // binding callback, which is responsible for producing the query argument values.
  146. func (s *Session) Bind(stmt string, b func(q *QueryInfo) ([]interface{}, error)) *Query {
  147. s.mu.RLock()
  148. qry := &Query{stmt: stmt, binding: b, cons: s.cons,
  149. session: s, pageSize: s.pageSize, trace: s.trace,
  150. prefetch: s.prefetch, rt: s.cfg.RetryPolicy}
  151. s.mu.RUnlock()
  152. return qry
  153. }
  154. // Close closes all connections. The session is unusable after this
  155. // operation.
  156. func (s *Session) Close() {
  157. s.closeMu.Lock()
  158. defer s.closeMu.Unlock()
  159. if s.isClosed {
  160. return
  161. }
  162. s.isClosed = true
  163. s.pool.Close()
  164. if s.hostSource != nil {
  165. close(s.hostSource.closeChan)
  166. }
  167. if s.control != nil {
  168. s.control.close()
  169. }
  170. }
  171. func (s *Session) Closed() bool {
  172. s.closeMu.RLock()
  173. closed := s.isClosed
  174. s.closeMu.RUnlock()
  175. return closed
  176. }
  177. func (s *Session) executeQuery(qry *Query) *Iter {
  178. // fail fast
  179. if s.Closed() {
  180. return &Iter{err: ErrSessionClosed}
  181. }
  182. var iter *Iter
  183. qry.attempts = 0
  184. qry.totalLatency = 0
  185. for {
  186. conn := s.pool.Pick(qry)
  187. //Assign the error unavailable to the iterator
  188. if conn == nil {
  189. iter = &Iter{err: ErrNoConnections}
  190. break
  191. }
  192. t := time.Now()
  193. iter = conn.executeQuery(qry)
  194. qry.totalLatency += time.Now().Sub(t).Nanoseconds()
  195. qry.attempts++
  196. //Exit for loop if the query was successful
  197. if iter.err == nil {
  198. break
  199. }
  200. if qry.rt == nil || !qry.rt.Attempt(qry) {
  201. break
  202. }
  203. }
  204. return iter
  205. }
  206. // KeyspaceMetadata returns the schema metadata for the keyspace specified.
  207. func (s *Session) KeyspaceMetadata(keyspace string) (*KeyspaceMetadata, error) {
  208. // fail fast
  209. if s.Closed() {
  210. return nil, ErrSessionClosed
  211. }
  212. if keyspace == "" {
  213. return nil, ErrNoKeyspace
  214. }
  215. s.mu.Lock()
  216. // lazy-init schemaDescriber
  217. if s.schemaDescriber == nil {
  218. s.schemaDescriber = newSchemaDescriber(s)
  219. }
  220. s.mu.Unlock()
  221. return s.schemaDescriber.getSchema(keyspace)
  222. }
  223. // returns routing key indexes and type info
  224. func (s *Session) routingKeyInfo(stmt string) (*routingKeyInfo, error) {
  225. s.routingKeyInfoCache.mu.Lock()
  226. entry, cached := s.routingKeyInfoCache.lru.Get(stmt)
  227. if cached {
  228. // done accessing the cache
  229. s.routingKeyInfoCache.mu.Unlock()
  230. // the entry is an inflight struct similiar to that used by
  231. // Conn to prepare statements
  232. inflight := entry.(*inflightCachedEntry)
  233. // wait for any inflight work
  234. inflight.wg.Wait()
  235. if inflight.err != nil {
  236. return nil, inflight.err
  237. }
  238. key, _ := inflight.value.(*routingKeyInfo)
  239. return key, nil
  240. }
  241. // create a new inflight entry while the data is created
  242. inflight := new(inflightCachedEntry)
  243. inflight.wg.Add(1)
  244. defer inflight.wg.Done()
  245. s.routingKeyInfoCache.lru.Add(stmt, inflight)
  246. s.routingKeyInfoCache.mu.Unlock()
  247. var (
  248. info *QueryInfo
  249. partitionKey []*ColumnMetadata
  250. )
  251. // get the query info for the statement
  252. conn := s.pool.Pick(nil)
  253. if conn == nil {
  254. // no connections
  255. inflight.err = ErrNoConnections
  256. // don't cache this error
  257. s.routingKeyInfoCache.Remove(stmt)
  258. return nil, inflight.err
  259. }
  260. info, inflight.err = conn.prepareStatement(stmt, nil)
  261. if inflight.err != nil {
  262. // don't cache this error
  263. s.routingKeyInfoCache.Remove(stmt)
  264. return nil, inflight.err
  265. }
  266. if len(info.Args) == 0 {
  267. // no arguments, no routing key, and no error
  268. return nil, nil
  269. }
  270. // get the table metadata
  271. table := info.Args[0].Table
  272. var keyspaceMetadata *KeyspaceMetadata
  273. keyspaceMetadata, inflight.err = s.KeyspaceMetadata(s.cfg.Keyspace)
  274. if inflight.err != nil {
  275. // don't cache this error
  276. s.routingKeyInfoCache.Remove(stmt)
  277. return nil, inflight.err
  278. }
  279. tableMetadata, found := keyspaceMetadata.Tables[table]
  280. if !found {
  281. // unlikely that the statement could be prepared and the metadata for
  282. // the table couldn't be found, but this may indicate either a bug
  283. // in the metadata code, or that the table was just dropped.
  284. inflight.err = ErrNoMetadata
  285. // don't cache this error
  286. s.routingKeyInfoCache.Remove(stmt)
  287. return nil, inflight.err
  288. }
  289. partitionKey = tableMetadata.PartitionKey
  290. size := len(partitionKey)
  291. routingKeyInfo := &routingKeyInfo{
  292. indexes: make([]int, size),
  293. types: make([]TypeInfo, size),
  294. }
  295. for keyIndex, keyColumn := range partitionKey {
  296. // set an indicator for checking if the mapping is missing
  297. routingKeyInfo.indexes[keyIndex] = -1
  298. // find the column in the query info
  299. for argIndex, boundColumn := range info.Args {
  300. if keyColumn.Name == boundColumn.Name {
  301. // there may be many such bound columns, pick the first
  302. routingKeyInfo.indexes[keyIndex] = argIndex
  303. routingKeyInfo.types[keyIndex] = boundColumn.TypeInfo
  304. break
  305. }
  306. }
  307. if routingKeyInfo.indexes[keyIndex] == -1 {
  308. // missing a routing key column mapping
  309. // no routing key, and no error
  310. return nil, nil
  311. }
  312. }
  313. // cache this result
  314. inflight.value = routingKeyInfo
  315. return routingKeyInfo, nil
  316. }
  317. func (s *Session) executeBatch(batch *Batch) (*Iter, error) {
  318. // fail fast
  319. if s.Closed() {
  320. return nil, ErrSessionClosed
  321. }
  322. // Prevent the execution of the batch if greater than the limit
  323. // Currently batches have a limit of 65536 queries.
  324. // https://datastax-oss.atlassian.net/browse/JAVA-229
  325. if batch.Size() > BatchSizeMaximum {
  326. return nil, ErrTooManyStmts
  327. }
  328. var err error
  329. var iter *Iter
  330. batch.attempts = 0
  331. batch.totalLatency = 0
  332. for {
  333. conn := s.pool.Pick(nil)
  334. //Assign the error unavailable and break loop
  335. if conn == nil {
  336. err = ErrNoConnections
  337. break
  338. }
  339. t := time.Now()
  340. iter, err = conn.executeBatch(batch)
  341. batch.totalLatency += time.Now().Sub(t).Nanoseconds()
  342. batch.attempts++
  343. //Exit loop if operation executed correctly
  344. if err == nil {
  345. return iter, err
  346. }
  347. if batch.rt == nil || !batch.rt.Attempt(batch) {
  348. break
  349. }
  350. }
  351. return nil, err
  352. }
  353. // ExecuteBatch executes a batch operation and returns nil if successful
  354. // otherwise an error is returned describing the failure.
  355. func (s *Session) ExecuteBatch(batch *Batch) error {
  356. _, err := s.executeBatch(batch)
  357. return err
  358. }
  359. // ExecuteBatchCAS executes a batch operation and returns nil if successful and
  360. // an iterator (to scan aditional rows if more than one conditional statement)
  361. // was sent, otherwise an error is returned describing the failure.
  362. // Further scans on the interator must also remember to include
  363. // the applied boolean as the first argument to *Iter.Scan
  364. func (s *Session) ExecuteBatchCAS(batch *Batch, dest ...interface{}) (applied bool, iter *Iter, err error) {
  365. if iter, err := s.executeBatch(batch); err == nil {
  366. if err := iter.checkErrAndNotFound(); err != nil {
  367. return false, nil, err
  368. }
  369. if len(iter.Columns()) > 1 {
  370. dest = append([]interface{}{&applied}, dest...)
  371. iter.Scan(dest...)
  372. } else {
  373. iter.Scan(&applied)
  374. }
  375. return applied, iter, nil
  376. } else {
  377. return false, nil, err
  378. }
  379. }
  380. // MapExecuteBatchCAS executes a batch operation much like ExecuteBatchCAS,
  381. // however it accepts a map rather than a list of arguments for the initial
  382. // scan.
  383. func (s *Session) MapExecuteBatchCAS(batch *Batch, dest map[string]interface{}) (applied bool, iter *Iter, err error) {
  384. if iter, err := s.executeBatch(batch); err == nil {
  385. if err := iter.checkErrAndNotFound(); err != nil {
  386. return false, nil, err
  387. }
  388. iter.MapScan(dest)
  389. applied = dest["[applied]"].(bool)
  390. delete(dest, "[applied]")
  391. // we usually close here, but instead of closing, just returin an error
  392. // if MapScan failed. Although Close just returns err, using Close
  393. // here might be confusing as we are not actually closing the iter
  394. return applied, iter, iter.err
  395. } else {
  396. return false, nil, err
  397. }
  398. }
  399. // Query represents a CQL statement that can be executed.
  400. type Query struct {
  401. stmt string
  402. values []interface{}
  403. cons Consistency
  404. pageSize int
  405. routingKey []byte
  406. routingKeyBuffer []byte
  407. pageState []byte
  408. prefetch float64
  409. trace Tracer
  410. session *Session
  411. rt RetryPolicy
  412. binding func(q *QueryInfo) ([]interface{}, error)
  413. attempts int
  414. totalLatency int64
  415. serialCons SerialConsistency
  416. defaultTimestamp bool
  417. disableAutoPage bool
  418. }
  419. // String implements the stringer interface.
  420. func (q Query) String() string {
  421. return fmt.Sprintf("[query statement=%q values=%+v consistency=%s]", q.stmt, q.values, q.cons)
  422. }
  423. //Attempts returns the number of times the query was executed.
  424. func (q *Query) Attempts() int {
  425. return q.attempts
  426. }
  427. //Latency returns the average amount of nanoseconds per attempt of the query.
  428. func (q *Query) Latency() int64 {
  429. if q.attempts > 0 {
  430. return q.totalLatency / int64(q.attempts)
  431. }
  432. return 0
  433. }
  434. // Consistency sets the consistency level for this query. If no consistency
  435. // level have been set, the default consistency level of the cluster
  436. // is used.
  437. func (q *Query) Consistency(c Consistency) *Query {
  438. q.cons = c
  439. return q
  440. }
  441. // GetConsistency returns the currently configured consistency level for
  442. // the query.
  443. func (q *Query) GetConsistency() Consistency {
  444. return q.cons
  445. }
  446. // Trace enables tracing of this query. Look at the documentation of the
  447. // Tracer interface to learn more about tracing.
  448. func (q *Query) Trace(trace Tracer) *Query {
  449. q.trace = trace
  450. return q
  451. }
  452. // PageSize will tell the iterator to fetch the result in pages of size n.
  453. // This is useful for iterating over large result sets, but setting the
  454. // page size to low might decrease the performance. This feature is only
  455. // available in Cassandra 2 and onwards.
  456. func (q *Query) PageSize(n int) *Query {
  457. q.pageSize = n
  458. return q
  459. }
  460. // DefaultTimestamp will enable the with default timestamp flag on the query.
  461. // If enable, this will replace the server side assigned
  462. // timestamp as default timestamp. Note that a timestamp in the query itself
  463. // will still override this timestamp. This is entirely optional.
  464. //
  465. // Only available on protocol >= 3
  466. func (q *Query) DefaultTimestamp(enable bool) *Query {
  467. q.defaultTimestamp = enable
  468. return q
  469. }
  470. // RoutingKey sets the routing key to use when a token aware connection
  471. // pool is used to optimize the routing of this query.
  472. func (q *Query) RoutingKey(routingKey []byte) *Query {
  473. q.routingKey = routingKey
  474. return q
  475. }
  476. // GetRoutingKey gets the routing key to use for routing this query. If
  477. // a routing key has not been explicitly set, then the routing key will
  478. // be constructed if possible using the keyspace's schema and the query
  479. // info for this query statement. If the routing key cannot be determined
  480. // then nil will be returned with no error. On any error condition,
  481. // an error description will be returned.
  482. func (q *Query) GetRoutingKey() ([]byte, error) {
  483. if q.routingKey != nil {
  484. return q.routingKey, nil
  485. }
  486. // try to determine the routing key
  487. routingKeyInfo, err := q.session.routingKeyInfo(q.stmt)
  488. if err != nil {
  489. return nil, err
  490. }
  491. if routingKeyInfo == nil {
  492. return nil, nil
  493. }
  494. if len(routingKeyInfo.indexes) == 1 {
  495. // single column routing key
  496. routingKey, err := Marshal(
  497. routingKeyInfo.types[0],
  498. q.values[routingKeyInfo.indexes[0]],
  499. )
  500. if err != nil {
  501. return nil, err
  502. }
  503. return routingKey, nil
  504. }
  505. // We allocate that buffer only once, so that further re-bind/exec of the
  506. // same query don't allocate more memory.
  507. if q.routingKeyBuffer == nil {
  508. q.routingKeyBuffer = make([]byte, 0, 256)
  509. }
  510. // composite routing key
  511. buf := bytes.NewBuffer(q.routingKeyBuffer)
  512. for i := range routingKeyInfo.indexes {
  513. encoded, err := Marshal(
  514. routingKeyInfo.types[i],
  515. q.values[routingKeyInfo.indexes[i]],
  516. )
  517. if err != nil {
  518. return nil, err
  519. }
  520. lenBuf := []byte{0x00, 0x00}
  521. binary.BigEndian.PutUint16(lenBuf, uint16(len(encoded)))
  522. buf.Write(lenBuf)
  523. buf.Write(encoded)
  524. buf.WriteByte(0x00)
  525. }
  526. routingKey := buf.Bytes()
  527. return routingKey, nil
  528. }
  529. func (q *Query) shouldPrepare() bool {
  530. stmt := strings.TrimLeftFunc(strings.TrimRightFunc(q.stmt, func(r rune) bool {
  531. return unicode.IsSpace(r) || r == ';'
  532. }), unicode.IsSpace)
  533. var stmtType string
  534. if n := strings.IndexFunc(stmt, unicode.IsSpace); n >= 0 {
  535. stmtType = strings.ToLower(stmt[:n])
  536. }
  537. if stmtType == "begin" {
  538. if n := strings.LastIndexFunc(stmt, unicode.IsSpace); n >= 0 {
  539. stmtType = strings.ToLower(stmt[n+1:])
  540. }
  541. }
  542. switch stmtType {
  543. case "select", "insert", "update", "delete", "batch":
  544. return true
  545. }
  546. return false
  547. }
  548. // SetPrefetch sets the default threshold for pre-fetching new pages. If
  549. // there are only p*pageSize rows remaining, the next page will be requested
  550. // automatically.
  551. func (q *Query) Prefetch(p float64) *Query {
  552. q.prefetch = p
  553. return q
  554. }
  555. // RetryPolicy sets the policy to use when retrying the query.
  556. func (q *Query) RetryPolicy(r RetryPolicy) *Query {
  557. q.rt = r
  558. return q
  559. }
  560. // Bind sets query arguments of query. This can also be used to rebind new query arguments
  561. // to an existing query instance.
  562. func (q *Query) Bind(v ...interface{}) *Query {
  563. q.values = v
  564. return q
  565. }
  566. // SerialConsistency sets the consistencyc level for the
  567. // serial phase of conditional updates. That consitency can only be
  568. // either SERIAL or LOCAL_SERIAL and if not present, it defaults to
  569. // SERIAL. This option will be ignored for anything else that a
  570. // conditional update/insert.
  571. func (q *Query) SerialConsistency(cons SerialConsistency) *Query {
  572. q.serialCons = cons
  573. return q
  574. }
  575. // PageState sets the paging state for the query to resume paging from a specific
  576. // point in time. Setting this will disable to query paging for this query, and
  577. // must be used for all subsequent pages.
  578. func (q *Query) PageState(state []byte) *Query {
  579. q.pageState = state
  580. q.disableAutoPage = true
  581. return q
  582. }
  583. // Exec executes the query without returning any rows.
  584. func (q *Query) Exec() error {
  585. iter := q.Iter()
  586. return iter.err
  587. }
  588. func isUseStatement(stmt string) bool {
  589. if len(stmt) < 3 {
  590. return false
  591. }
  592. return strings.ToLower(stmt[0:3]) == "use"
  593. }
  594. // Iter executes the query and returns an iterator capable of iterating
  595. // over all results.
  596. func (q *Query) Iter() *Iter {
  597. if isUseStatement(q.stmt) {
  598. return &Iter{err: ErrUseStmt}
  599. }
  600. return q.session.executeQuery(q)
  601. }
  602. // MapScan executes the query, copies the columns of the first selected
  603. // row into the map pointed at by m and discards the rest. If no rows
  604. // were selected, ErrNotFound is returned.
  605. func (q *Query) MapScan(m map[string]interface{}) error {
  606. iter := q.Iter()
  607. if err := iter.checkErrAndNotFound(); err != nil {
  608. return err
  609. }
  610. iter.MapScan(m)
  611. return iter.Close()
  612. }
  613. // Scan executes the query, copies the columns of the first selected
  614. // row into the values pointed at by dest and discards the rest. If no rows
  615. // were selected, ErrNotFound is returned.
  616. func (q *Query) Scan(dest ...interface{}) error {
  617. iter := q.Iter()
  618. if err := iter.checkErrAndNotFound(); err != nil {
  619. return err
  620. }
  621. iter.Scan(dest...)
  622. return iter.Close()
  623. }
  624. // ScanCAS executes a lightweight transaction (i.e. an UPDATE or INSERT
  625. // statement containing an IF clause). If the transaction fails because
  626. // the existing values did not match, the previous values will be stored
  627. // in dest.
  628. func (q *Query) ScanCAS(dest ...interface{}) (applied bool, err error) {
  629. iter := q.Iter()
  630. if err := iter.checkErrAndNotFound(); err != nil {
  631. return false, err
  632. }
  633. if len(iter.Columns()) > 1 {
  634. dest = append([]interface{}{&applied}, dest...)
  635. iter.Scan(dest...)
  636. } else {
  637. iter.Scan(&applied)
  638. }
  639. return applied, iter.Close()
  640. }
  641. // MapScanCAS executes a lightweight transaction (i.e. an UPDATE or INSERT
  642. // statement containing an IF clause). If the transaction fails because
  643. // the existing values did not match, the previous values will be stored
  644. // in dest map.
  645. //
  646. // As for INSERT .. IF NOT EXISTS, previous values will be returned as if
  647. // SELECT * FROM. So using ScanCAS with INSERT is inherently prone to
  648. // column mismatching. MapScanCAS is added to capture them safely.
  649. func (q *Query) MapScanCAS(dest map[string]interface{}) (applied bool, err error) {
  650. iter := q.Iter()
  651. if err := iter.checkErrAndNotFound(); err != nil {
  652. return false, err
  653. }
  654. iter.MapScan(dest)
  655. applied = dest["[applied]"].(bool)
  656. delete(dest, "[applied]")
  657. return applied, iter.Close()
  658. }
  659. // Iter represents an iterator that can be used to iterate over all rows that
  660. // were returned by a query. The iterator might send additional queries to the
  661. // database during the iteration if paging was enabled.
  662. type Iter struct {
  663. err error
  664. pos int
  665. rows [][][]byte
  666. meta resultMetadata
  667. next *nextIter
  668. framer *framer
  669. once sync.Once
  670. }
  671. // Columns returns the name and type of the selected columns.
  672. func (iter *Iter) Columns() []ColumnInfo {
  673. return iter.meta.columns
  674. }
  675. // Scan consumes the next row of the iterator and copies the columns of the
  676. // current row into the values pointed at by dest. Use nil as a dest value
  677. // to skip the corresponding column. Scan might send additional queries
  678. // to the database to retrieve the next set of rows if paging was enabled.
  679. //
  680. // Scan returns true if the row was successfully unmarshaled or false if the
  681. // end of the result set was reached or if an error occurred. Close should
  682. // be called afterwards to retrieve any potential errors.
  683. func (iter *Iter) Scan(dest ...interface{}) bool {
  684. if iter.err != nil {
  685. return false
  686. }
  687. if iter.pos >= len(iter.rows) {
  688. if iter.next != nil {
  689. *iter = *iter.next.fetch()
  690. return iter.Scan(dest...)
  691. }
  692. return false
  693. }
  694. if iter.next != nil && iter.pos == iter.next.pos {
  695. go iter.next.fetch()
  696. }
  697. // currently only support scanning into an expand tuple, such that its the same
  698. // as scanning in more values from a single column
  699. if len(dest) != iter.meta.actualColCount {
  700. iter.err = errors.New("count mismatch")
  701. return false
  702. }
  703. // i is the current position in dest, could posible replace it and just use
  704. // slices of dest
  705. i := 0
  706. for c, col := range iter.meta.columns {
  707. if dest[i] == nil {
  708. i++
  709. continue
  710. }
  711. switch col.TypeInfo.Type() {
  712. case TypeTuple:
  713. // this will panic, actually a bug, please report
  714. tuple := col.TypeInfo.(TupleTypeInfo)
  715. count := len(tuple.Elems)
  716. // here we pass in a slice of the struct which has the number number of
  717. // values as elements in the tuple
  718. iter.err = Unmarshal(col.TypeInfo, iter.rows[iter.pos][c], dest[i:i+count])
  719. i += count
  720. default:
  721. iter.err = Unmarshal(col.TypeInfo, iter.rows[iter.pos][c], dest[i])
  722. i++
  723. }
  724. if iter.err != nil {
  725. return false
  726. }
  727. }
  728. iter.pos++
  729. return true
  730. }
  731. // Close closes the iterator and returns any errors that happened during
  732. // the query or the iteration.
  733. func (iter *Iter) Close() error {
  734. iter.once.Do(func() {
  735. if iter.framer != nil {
  736. framerPool.Put(iter.framer)
  737. iter.framer = nil
  738. }
  739. })
  740. return iter.err
  741. }
  742. // checkErrAndNotFound handle error and NotFound in one method.
  743. func (iter *Iter) checkErrAndNotFound() error {
  744. if iter.err != nil {
  745. return iter.err
  746. } else if len(iter.rows) == 0 {
  747. return ErrNotFound
  748. }
  749. return nil
  750. }
  751. // PageState return the current paging state for a query which can be used for
  752. // subsequent quries to resume paging this point.
  753. func (iter *Iter) PageState() []byte {
  754. return iter.meta.pagingState
  755. }
  756. type nextIter struct {
  757. qry Query
  758. pos int
  759. once sync.Once
  760. next *Iter
  761. }
  762. func (n *nextIter) fetch() *Iter {
  763. n.once.Do(func() {
  764. n.next = n.qry.session.executeQuery(&n.qry)
  765. })
  766. return n.next
  767. }
  768. type Batch struct {
  769. Type BatchType
  770. Entries []BatchEntry
  771. Cons Consistency
  772. rt RetryPolicy
  773. attempts int
  774. totalLatency int64
  775. serialCons SerialConsistency
  776. defaultTimestamp bool
  777. }
  778. // NewBatch creates a new batch operation without defaults from the cluster
  779. func NewBatch(typ BatchType) *Batch {
  780. return &Batch{Type: typ}
  781. }
  782. // NewBatch creates a new batch operation using defaults defined in the cluster
  783. func (s *Session) NewBatch(typ BatchType) *Batch {
  784. s.mu.RLock()
  785. batch := &Batch{Type: typ, rt: s.cfg.RetryPolicy, serialCons: s.cfg.SerialConsistency,
  786. Cons: s.cons, defaultTimestamp: s.cfg.DefaultTimestamp}
  787. s.mu.RUnlock()
  788. return batch
  789. }
  790. // Attempts returns the number of attempts made to execute the batch.
  791. func (b *Batch) Attempts() int {
  792. return b.attempts
  793. }
  794. //Latency returns the average number of nanoseconds to execute a single attempt of the batch.
  795. func (b *Batch) Latency() int64 {
  796. if b.attempts > 0 {
  797. return b.totalLatency / int64(b.attempts)
  798. }
  799. return 0
  800. }
  801. // GetConsistency returns the currently configured consistency level for the batch
  802. // operation.
  803. func (b *Batch) GetConsistency() Consistency {
  804. return b.Cons
  805. }
  806. // Query adds the query to the batch operation
  807. func (b *Batch) Query(stmt string, args ...interface{}) {
  808. b.Entries = append(b.Entries, BatchEntry{Stmt: stmt, Args: args})
  809. }
  810. // Bind adds the query to the batch operation and correlates it with a binding callback
  811. // that will be invoked when the batch is executed. The binding callback allows the application
  812. // to define which query argument values will be marshalled as part of the batch execution.
  813. func (b *Batch) Bind(stmt string, bind func(q *QueryInfo) ([]interface{}, error)) {
  814. b.Entries = append(b.Entries, BatchEntry{Stmt: stmt, binding: bind})
  815. }
  816. // RetryPolicy sets the retry policy to use when executing the batch operation
  817. func (b *Batch) RetryPolicy(r RetryPolicy) *Batch {
  818. b.rt = r
  819. return b
  820. }
  821. // Size returns the number of batch statements to be executed by the batch operation.
  822. func (b *Batch) Size() int {
  823. return len(b.Entries)
  824. }
  825. // SerialConsistency sets the consistencyc level for the
  826. // serial phase of conditional updates. That consitency can only be
  827. // either SERIAL or LOCAL_SERIAL and if not present, it defaults to
  828. // SERIAL. This option will be ignored for anything else that a
  829. // conditional update/insert.
  830. //
  831. // Only available for protocol 3 and above
  832. func (b *Batch) SerialConsistency(cons SerialConsistency) *Batch {
  833. b.serialCons = cons
  834. return b
  835. }
  836. // DefaultTimestamp will enable the with default timestamp flag on the query.
  837. // If enable, this will replace the server side assigned
  838. // timestamp as default timestamp. Note that a timestamp in the query itself
  839. // will still override this timestamp. This is entirely optional.
  840. //
  841. // Only available on protocol >= 3
  842. func (b *Batch) DefaultTimestamp(enable bool) *Batch {
  843. b.defaultTimestamp = enable
  844. return b
  845. }
  846. type BatchType byte
  847. const (
  848. LoggedBatch BatchType = 0
  849. UnloggedBatch BatchType = 1
  850. CounterBatch BatchType = 2
  851. )
  852. type BatchEntry struct {
  853. Stmt string
  854. Args []interface{}
  855. binding func(q *QueryInfo) ([]interface{}, error)
  856. }
  857. type ColumnInfo struct {
  858. Keyspace string
  859. Table string
  860. Name string
  861. TypeInfo TypeInfo
  862. }
  863. func (c ColumnInfo) String() string {
  864. return fmt.Sprintf("[column keyspace=%s table=%s name=%s type=%v]", c.Keyspace, c.Table, c.Name, c.TypeInfo)
  865. }
  866. // routing key indexes LRU cache
  867. type routingKeyInfoLRU struct {
  868. lru *lru.Cache
  869. mu sync.Mutex
  870. }
  871. type routingKeyInfo struct {
  872. indexes []int
  873. types []TypeInfo
  874. }
  875. func (r *routingKeyInfoLRU) Remove(key string) {
  876. r.mu.Lock()
  877. r.lru.Remove(key)
  878. r.mu.Unlock()
  879. }
  880. //Max adjusts the maximum size of the cache and cleans up the oldest records if
  881. //the new max is lower than the previous value. Not concurrency safe.
  882. func (r *routingKeyInfoLRU) Max(max int) {
  883. r.mu.Lock()
  884. for r.lru.Len() > max {
  885. r.lru.RemoveOldest()
  886. }
  887. r.lru.MaxEntries = max
  888. r.mu.Unlock()
  889. }
  890. type inflightCachedEntry struct {
  891. wg sync.WaitGroup
  892. err error
  893. value interface{}
  894. }
  895. // Tracer is the interface implemented by query tracers. Tracers have the
  896. // ability to obtain a detailed event log of all events that happened during
  897. // the execution of a query from Cassandra. Gathering this information might
  898. // be essential for debugging and optimizing queries, but this feature should
  899. // not be used on production systems with very high load.
  900. type Tracer interface {
  901. Trace(traceId []byte)
  902. }
  903. type traceWriter struct {
  904. session *Session
  905. w io.Writer
  906. mu sync.Mutex
  907. }
  908. // NewTraceWriter returns a simple Tracer implementation that outputs
  909. // the event log in a textual format.
  910. func NewTraceWriter(session *Session, w io.Writer) Tracer {
  911. return &traceWriter{session: session, w: w}
  912. }
  913. func (t *traceWriter) Trace(traceId []byte) {
  914. var (
  915. coordinator string
  916. duration int
  917. )
  918. iter := t.session.control.query(`SELECT coordinator, duration
  919. FROM system_traces.sessions
  920. WHERE session_id = ?`, traceId)
  921. iter.Scan(&coordinator, &duration)
  922. if err := iter.Close(); err != nil {
  923. t.mu.Lock()
  924. fmt.Fprintln(t.w, "Error:", err)
  925. t.mu.Unlock()
  926. return
  927. }
  928. var (
  929. timestamp time.Time
  930. activity string
  931. source string
  932. elapsed int
  933. )
  934. fmt.Fprintf(t.w, "Tracing session %016x (coordinator: %s, duration: %v):\n",
  935. traceId, coordinator, time.Duration(duration)*time.Microsecond)
  936. t.mu.Lock()
  937. defer t.mu.Unlock()
  938. iter = t.session.control.query(`SELECT event_id, activity, source, source_elapsed
  939. FROM system_traces.events
  940. WHERE session_id = ?`, traceId)
  941. for iter.Scan(&timestamp, &activity, &source, &elapsed) {
  942. fmt.Fprintf(t.w, "%s: %s (source: %s, elapsed: %d)\n",
  943. timestamp.Format("2006/01/02 15:04:05.999999"), activity, source, elapsed)
  944. }
  945. if err := iter.Close(); err != nil {
  946. fmt.Fprintln(t.w, "Error:", err)
  947. }
  948. }
  949. type Error struct {
  950. Code int
  951. Message string
  952. }
  953. func (e Error) Error() string {
  954. return e.Message
  955. }
  956. var (
  957. ErrNotFound = errors.New("not found")
  958. ErrUnavailable = errors.New("unavailable")
  959. ErrUnsupported = errors.New("feature not supported")
  960. ErrTooManyStmts = errors.New("too many statements")
  961. ErrUseStmt = errors.New("use statements aren't supported. Please see https://github.com/gocql/gocql for explaination.")
  962. ErrSessionClosed = errors.New("session has been closed")
  963. ErrNoConnections = errors.New("no connections available")
  964. ErrNoKeyspace = errors.New("no keyspace provided")
  965. ErrNoMetadata = errors.New("no metadata available")
  966. )
  967. type ErrProtocol struct{ error }
  968. func NewErrProtocol(format string, args ...interface{}) error {
  969. return ErrProtocol{fmt.Errorf(format, args...)}
  970. }
  971. // BatchSizeMaximum is the maximum number of statements a batch operation can have.
  972. // This limit is set by cassandra and could change in the future.
  973. const BatchSizeMaximum = 65535