session.go 12 KB

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