session.go 13 KB

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