session.go 31 KB

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