session.go 31 KB

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