session.go 30 KB

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