session.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. "errors"
  7. "fmt"
  8. "io"
  9. "strings"
  10. "sync"
  11. "time"
  12. )
  13. // Session is the interface used by users to interact with the database.
  14. //
  15. // It's safe for concurrent use by multiple goroutines and a typical usage
  16. // scenario is to have one global session object to interact with the
  17. // whole Cassandra cluster.
  18. //
  19. // This type extends the Node interface by adding a convinient query builder
  20. // and automatically sets a default consinstency level on all operations
  21. // that do not have a consistency level set.
  22. type Session struct {
  23. Node Node
  24. cons Consistency
  25. pageSize int
  26. prefetch float64
  27. trace Tracer
  28. mu sync.RWMutex
  29. cfg ClusterConfig
  30. }
  31. // NewSession wraps an existing Node.
  32. func NewSession(c *clusterImpl) *Session {
  33. return &Session{Node: c, cons: Quorum, prefetch: 0.25, cfg: c.cfg}
  34. }
  35. // SetConsistency sets the default consistency level for this session. This
  36. // setting can also be changed on a per-query basis and the default value
  37. // is Quorum.
  38. func (s *Session) SetConsistency(cons Consistency) {
  39. s.mu.Lock()
  40. s.cons = cons
  41. s.mu.Unlock()
  42. }
  43. // SetPageSize sets the default page size for this session. A value <= 0 will
  44. // disable paging. This setting can also be changed on a per-query basis.
  45. func (s *Session) SetPageSize(n int) {
  46. s.mu.Lock()
  47. s.pageSize = n
  48. s.mu.Unlock()
  49. }
  50. // SetPrefetch sets the default threshold for pre-fetching new pages. If
  51. // there are only p*pageSize rows remaining, the next page will be requested
  52. // automatically. This value can also be changed on a per-query basis and
  53. // the default value is 0.25.
  54. func (s *Session) SetPrefetch(p float64) {
  55. s.mu.Lock()
  56. s.prefetch = p
  57. s.mu.Unlock()
  58. }
  59. // SetTrace sets the default tracer for this session. This setting can also
  60. // be changed on a per-query basis.
  61. func (s *Session) SetTrace(trace Tracer) {
  62. s.mu.Lock()
  63. s.trace = trace
  64. s.mu.Unlock()
  65. }
  66. // Query generates a new query object for interacting with the database.
  67. // Further details of the query may be tweaked using the resulting query
  68. // value before the query is executed.
  69. func (s *Session) Query(stmt string, values ...interface{}) *Query {
  70. s.mu.RLock()
  71. qry := &Query{stmt: stmt, values: values, cons: s.cons,
  72. session: s, pageSize: s.pageSize, trace: s.trace,
  73. prefetch: s.prefetch, rt: s.cfg.RetryPolicy}
  74. s.mu.RUnlock()
  75. return qry
  76. }
  77. // Close closes all connections. The session is unusable after this
  78. // operation.
  79. func (s *Session) Close() {
  80. s.Node.Close()
  81. }
  82. func (s *Session) executeQuery(qry *Query) *Iter {
  83. var itr *Iter
  84. count := 0
  85. for count <= qry.rt.NumRetries {
  86. conn := s.Node.Pick(nil)
  87. //Assign the error unavailable to the iterator
  88. if conn == nil {
  89. itr = &Iter{err: ErrUnavailable}
  90. break
  91. }
  92. itr = conn.executeQuery(qry)
  93. //Exit for loop if the query was successful
  94. if itr.err == nil {
  95. break
  96. }
  97. count++
  98. }
  99. return itr
  100. }
  101. // ExecuteBatch executes a batch operation and returns nil if successful
  102. // otherwise an error is returned describing the failure.
  103. func (s *Session) ExecuteBatch(batch *Batch) error {
  104. // Prevent the execution of the batch if greater than the limit
  105. // Currently batches have a limit of 65536 queries.
  106. // https://datastax-oss.atlassian.net/browse/JAVA-229
  107. if batch.Size() > BatchSizeMaximum {
  108. return ErrTooManyStmts
  109. }
  110. var err error
  111. count := 0
  112. for count <= batch.rt.NumRetries {
  113. conn := s.Node.Pick(nil)
  114. //Assign the error unavailable and break loop
  115. if conn == nil {
  116. err = ErrUnavailable
  117. break
  118. }
  119. err = conn.executeBatch(batch)
  120. //Exit loop if operation executed correctly
  121. if err == nil {
  122. break
  123. }
  124. count++
  125. }
  126. return err
  127. }
  128. // Query represents a CQL statement that can be executed.
  129. type Query struct {
  130. stmt string
  131. values []interface{}
  132. cons Consistency
  133. pageSize int
  134. pageState []byte
  135. prefetch float64
  136. trace Tracer
  137. session *Session
  138. rt RetryPolicy
  139. }
  140. // Consistency sets the consistency level for this query. If no consistency
  141. // level have been set, the default consistency level of the cluster
  142. // is used.
  143. func (q *Query) Consistency(c Consistency) *Query {
  144. q.cons = c
  145. return q
  146. }
  147. // Trace enables tracing of this query. Look at the documentation of the
  148. // Tracer interface to learn more about tracing.
  149. func (q *Query) Trace(trace Tracer) *Query {
  150. q.trace = trace
  151. return q
  152. }
  153. // PageSize will tell the iterator to fetch the result in pages of size n.
  154. // This is useful for iterating over large result sets, but setting the
  155. // page size to low might decrease the performance. This feature is only
  156. // available in Cassandra 2 and onwards.
  157. func (q *Query) PageSize(n int) *Query {
  158. q.pageSize = n
  159. return q
  160. }
  161. // SetPrefetch sets the default threshold for pre-fetching new pages. If
  162. // there are only p*pageSize rows remaining, the next page will be requested
  163. // automatically.
  164. func (q *Query) Prefetch(p float64) *Query {
  165. q.prefetch = p
  166. return q
  167. }
  168. // RetryPolicy sets the policy to use when retrying the query.
  169. func (q *Query) RetryPolicy(r RetryPolicy) *Query {
  170. q.rt = r
  171. return q
  172. }
  173. // Exec executes the query without returning any rows.
  174. func (q *Query) Exec() error {
  175. iter := q.Iter()
  176. return iter.err
  177. }
  178. // Iter executes the query and returns an iterator capable of iterating
  179. // over all results.
  180. func (q *Query) Iter() *Iter {
  181. if strings.Index(strings.ToLower(q.stmt), "use") == 0 {
  182. return &Iter{err: ErrUseStmt}
  183. }
  184. return q.session.executeQuery(q)
  185. }
  186. // Scan executes the query, copies the columns of the first selected
  187. // row into the values pointed at by dest and discards the rest. If no rows
  188. // were selected, ErrNotFound is returned.
  189. func (q *Query) Scan(dest ...interface{}) error {
  190. iter := q.Iter()
  191. if iter.err != nil {
  192. return iter.err
  193. }
  194. if len(iter.rows) == 0 {
  195. return ErrNotFound
  196. }
  197. iter.Scan(dest...)
  198. return iter.Close()
  199. }
  200. // ScanCAS executes a lightweight transaction (i.e. an UPDATE or INSERT
  201. // statement containing an IF clause). If the transaction fails because
  202. // the existing values did not match, the previos values will be stored
  203. // in dest.
  204. func (q *Query) ScanCAS(dest ...interface{}) (applied bool, err error) {
  205. iter := q.Iter()
  206. if iter.err != nil {
  207. return false, iter.err
  208. }
  209. if len(iter.rows) == 0 {
  210. return false, ErrNotFound
  211. }
  212. if len(iter.Columns()) > 1 {
  213. dest = append([]interface{}{&applied}, dest...)
  214. iter.Scan(dest...)
  215. } else {
  216. iter.Scan(&applied)
  217. }
  218. return applied, iter.Close()
  219. }
  220. // Iter represents an iterator that can be used to iterate over all rows that
  221. // were returned by a query. The iterator might send additional queries to the
  222. // database during the iteration if paging was enabled.
  223. type Iter struct {
  224. err error
  225. pos int
  226. rows [][][]byte
  227. columns []ColumnInfo
  228. next *nextIter
  229. }
  230. // Columns returns the name and type of the selected columns.
  231. func (iter *Iter) Columns() []ColumnInfo {
  232. return iter.columns
  233. }
  234. // Scan consumes the next row of the iterator and copies the columns of the
  235. // current row into the values pointed at by dest. Use nil as a dest value
  236. // to skip the corresponding column. Scan might send additional queries
  237. // to the database to retrieve the next set of rows if paging was enabled.
  238. //
  239. // Scan returns true if the row was successfully unmarshaled or false if the
  240. // end of the result set was reached or if an error occurred. Close should
  241. // be called afterwards to retrieve any potential errors.
  242. func (iter *Iter) Scan(dest ...interface{}) bool {
  243. if iter.err != nil {
  244. return false
  245. }
  246. if iter.pos >= len(iter.rows) {
  247. if iter.next != nil {
  248. *iter = *iter.next.fetch()
  249. return iter.Scan(dest...)
  250. }
  251. return false
  252. }
  253. if iter.next != nil && iter.pos == iter.next.pos {
  254. go iter.next.fetch()
  255. }
  256. if len(dest) != len(iter.columns) {
  257. iter.err = errors.New("count mismatch")
  258. return false
  259. }
  260. for i := 0; i < len(iter.columns); i++ {
  261. if dest[i] == nil {
  262. continue
  263. }
  264. err := Unmarshal(iter.columns[i].TypeInfo, iter.rows[iter.pos][i], dest[i])
  265. if err != nil {
  266. iter.err = err
  267. return false
  268. }
  269. }
  270. iter.pos++
  271. return true
  272. }
  273. // Close closes the iterator and returns any errors that happened during
  274. // the query or the iteration.
  275. func (iter *Iter) Close() error {
  276. return iter.err
  277. }
  278. type nextIter struct {
  279. qry Query
  280. pos int
  281. once sync.Once
  282. next *Iter
  283. }
  284. func (n *nextIter) fetch() *Iter {
  285. n.once.Do(func() {
  286. n.next = n.qry.session.executeQuery(&n.qry)
  287. })
  288. return n.next
  289. }
  290. type Batch struct {
  291. Type BatchType
  292. Entries []BatchEntry
  293. Cons Consistency
  294. rt RetryPolicy
  295. }
  296. // NewBatch creates a new batch operation without defaults from the cluster
  297. func NewBatch(typ BatchType) *Batch {
  298. return &Batch{Type: typ}
  299. }
  300. // NewBatch creates a new batch operation using defaults defined in the cluster
  301. func (s *Session) NewBatch(typ BatchType) *Batch {
  302. return &Batch{Type: typ, rt: s.cfg.RetryPolicy}
  303. }
  304. // Query adds the query to the batch operation
  305. func (b *Batch) Query(stmt string, args ...interface{}) {
  306. b.Entries = append(b.Entries, BatchEntry{Stmt: stmt, Args: args})
  307. }
  308. // RetryPolicy sets the retry policy to use when executing the batch operation
  309. func (b *Batch) RetryPolicy(r RetryPolicy) *Batch {
  310. b.rt = r
  311. return b
  312. }
  313. // Size returns the number of batch statements to be executed by the batch operation.
  314. func (b *Batch) Size() int {
  315. return len(b.Entries)
  316. }
  317. type BatchType int
  318. const (
  319. LoggedBatch BatchType = 0
  320. UnloggedBatch BatchType = 1
  321. CounterBatch BatchType = 2
  322. )
  323. type BatchEntry struct {
  324. Stmt string
  325. Args []interface{}
  326. }
  327. type Consistency int
  328. const (
  329. Any Consistency = 1 + iota
  330. One
  331. Two
  332. Three
  333. Quorum
  334. All
  335. LocalQuorum
  336. EachQuorum
  337. Serial
  338. LocalSerial
  339. )
  340. var consinstencyNames = []string{
  341. 0: "default",
  342. Any: "any",
  343. One: "one",
  344. Two: "two",
  345. Three: "three",
  346. Quorum: "quorum",
  347. All: "all",
  348. LocalQuorum: "localquorum",
  349. EachQuorum: "eachquorum",
  350. Serial: "serial",
  351. LocalSerial: "localserial",
  352. }
  353. func (c Consistency) String() string {
  354. return consinstencyNames[c]
  355. }
  356. type ColumnInfo struct {
  357. Keyspace string
  358. Table string
  359. Name string
  360. TypeInfo *TypeInfo
  361. }
  362. // Tracer is the interface implemented by query tracers. Tracers have the
  363. // ability to obtain a detailed event log of all events that happened during
  364. // the execution of a query from Cassandra. Gathering this information might
  365. // be essential for debugging and optimizing queries, but this feature should
  366. // not be used on production systems with very high load.
  367. type Tracer interface {
  368. Trace(traceId []byte)
  369. }
  370. type traceWriter struct {
  371. session *Session
  372. w io.Writer
  373. mu sync.Mutex
  374. }
  375. // NewTraceWriter returns a simple Tracer implementation that outputs
  376. // the event log in a textual format.
  377. func NewTraceWriter(session *Session, w io.Writer) Tracer {
  378. return traceWriter{session: session, w: w}
  379. }
  380. func (t traceWriter) Trace(traceId []byte) {
  381. var (
  382. coordinator string
  383. duration int
  384. )
  385. t.session.Query(`SELECT coordinator, duration
  386. FROM system_traces.sessions
  387. WHERE session_id = ?`, traceId).
  388. Consistency(One).Scan(&coordinator, &duration)
  389. iter := t.session.Query(`SELECT event_id, activity, source, source_elapsed
  390. FROM system_traces.events
  391. WHERE session_id = ?`, traceId).
  392. Consistency(One).Iter()
  393. var (
  394. timestamp time.Time
  395. activity string
  396. source string
  397. elapsed int
  398. )
  399. t.mu.Lock()
  400. defer t.mu.Unlock()
  401. fmt.Fprintf(t.w, "Tracing session %016x (coordinator: %s, duration: %v):\n",
  402. traceId, coordinator, time.Duration(duration)*time.Microsecond)
  403. for iter.Scan(&timestamp, &activity, &source, &elapsed) {
  404. fmt.Fprintf(t.w, "%s: %s (source: %s, elapsed: %d)\n",
  405. timestamp.Format("2006/01/02 15:04:05.999999"), activity, source, elapsed)
  406. }
  407. if err := iter.Close(); err != nil {
  408. fmt.Fprintln(t.w, "Error:", err)
  409. }
  410. }
  411. type Error struct {
  412. Code int
  413. Message string
  414. }
  415. func (e Error) Error() string {
  416. return e.Message
  417. }
  418. var (
  419. ErrNotFound = errors.New("not found")
  420. ErrUnavailable = errors.New("unavailable")
  421. ErrProtocol = errors.New("protocol error")
  422. ErrUnsupported = errors.New("feature not supported")
  423. ErrTooManyStmts = errors.New("too many statements")
  424. ErrUseStmt = errors.New("use statements aren't supported. Please see https://github.com/gocql/gocql for explaination.")
  425. )
  426. // BatchSizeMaximum is the maximum number of statements a batch operation can have.
  427. // This limit is set by cassandra and could change in the future.
  428. const BatchSizeMaximum = 65535