session.go 30 KB

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