session.go 27 KB

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