session.go 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980
  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. "context"
  8. "encoding/binary"
  9. "errors"
  10. "fmt"
  11. "io"
  12. "net"
  13. "strings"
  14. "sync"
  15. "sync/atomic"
  16. "time"
  17. "unicode"
  18. "github.com/gocql/gocql/internal/lru"
  19. )
  20. // Session is the interface used by users to interact with the database.
  21. //
  22. // It's safe for concurrent use by multiple goroutines and a typical usage
  23. // scenario is to have one global session object to interact with the
  24. // whole Cassandra cluster.
  25. //
  26. // This type extends the Node interface by adding a convinient query builder
  27. // and automatically sets a default consistency level on all operations
  28. // that do not have a consistency level set.
  29. type Session struct {
  30. cons Consistency
  31. pageSize int
  32. prefetch float64
  33. routingKeyInfoCache routingKeyInfoLRU
  34. schemaDescriber *schemaDescriber
  35. trace Tracer
  36. queryObserver QueryObserver
  37. batchObserver BatchObserver
  38. connectObserver ConnectObserver
  39. frameObserver FrameHeaderObserver
  40. hostSource *ringDescriber
  41. stmtsLRU *preparedLRU
  42. connCfg *ConnConfig
  43. executor *queryExecutor
  44. pool *policyConnPool
  45. policy HostSelectionPolicy
  46. ring ring
  47. metadata clusterMetadata
  48. mu sync.RWMutex
  49. control *controlConn
  50. // event handlers
  51. nodeEvents *eventDebouncer
  52. schemaEvents *eventDebouncer
  53. // ring metadata
  54. hosts []HostInfo
  55. useSystemSchema bool
  56. cfg ClusterConfig
  57. quit chan struct{}
  58. closeMu sync.RWMutex
  59. isClosed bool
  60. }
  61. var queryPool = &sync.Pool{
  62. New: func() interface{} {
  63. return new(Query)
  64. },
  65. }
  66. func addrsToHosts(addrs []string, defaultPort int) ([]*HostInfo, error) {
  67. var hosts []*HostInfo
  68. for _, hostport := range addrs {
  69. resolvedHosts, err := hostInfo(hostport, defaultPort)
  70. if err != nil {
  71. // Try other hosts if unable to resolve DNS name
  72. if _, ok := err.(*net.DNSError); ok {
  73. Logger.Printf("gocql: dns error: %v\n", err)
  74. continue
  75. }
  76. return nil, err
  77. }
  78. hosts = append(hosts, resolvedHosts...)
  79. }
  80. if len(hosts) == 0 {
  81. return nil, errors.New("failed to resolve any of the provided hostnames")
  82. }
  83. return hosts, nil
  84. }
  85. // NewSession wraps an existing Node.
  86. func NewSession(cfg ClusterConfig) (*Session, error) {
  87. // Check that hosts in the ClusterConfig is not empty
  88. if len(cfg.Hosts) < 1 {
  89. return nil, ErrNoHosts
  90. }
  91. // Check that either Authenticator is set or AuthProvider, not both
  92. if cfg.Authenticator != nil && cfg.AuthProvider != nil {
  93. return nil, errors.New("Can't use both Authenticator and AuthProvider in cluster config.")
  94. }
  95. s := &Session{
  96. cons: cfg.Consistency,
  97. prefetch: 0.25,
  98. cfg: cfg,
  99. pageSize: cfg.PageSize,
  100. stmtsLRU: &preparedLRU{lru: lru.New(cfg.MaxPreparedStmts)},
  101. quit: make(chan struct{}),
  102. connectObserver: cfg.ConnectObserver,
  103. }
  104. s.schemaDescriber = newSchemaDescriber(s)
  105. s.nodeEvents = newEventDebouncer("NodeEvents", s.handleNodeEvent)
  106. s.schemaEvents = newEventDebouncer("SchemaEvents", s.handleSchemaEvent)
  107. s.routingKeyInfoCache.lru = lru.New(cfg.MaxRoutingKeyInfo)
  108. s.hostSource = &ringDescriber{session: s}
  109. if cfg.PoolConfig.HostSelectionPolicy == nil {
  110. cfg.PoolConfig.HostSelectionPolicy = RoundRobinHostPolicy()
  111. }
  112. s.pool = cfg.PoolConfig.buildPool(s)
  113. s.policy = cfg.PoolConfig.HostSelectionPolicy
  114. s.policy.Init(s)
  115. s.executor = &queryExecutor{
  116. pool: s.pool,
  117. policy: cfg.PoolConfig.HostSelectionPolicy,
  118. }
  119. s.queryObserver = cfg.QueryObserver
  120. s.batchObserver = cfg.BatchObserver
  121. s.connectObserver = cfg.ConnectObserver
  122. s.frameObserver = cfg.FrameHeaderObserver
  123. //Check the TLS Config before trying to connect to anything external
  124. connCfg, err := connConfig(&s.cfg)
  125. if err != nil {
  126. //TODO: Return a typed error
  127. return nil, fmt.Errorf("gocql: unable to create session: %v", err)
  128. }
  129. s.connCfg = connCfg
  130. if err := s.init(); err != nil {
  131. s.Close()
  132. if err == ErrNoConnectionsStarted {
  133. //This error used to be generated inside NewSession & returned directly
  134. //Forward it on up to be backwards compatible
  135. return nil, ErrNoConnectionsStarted
  136. } else {
  137. // TODO(zariel): dont wrap this error in fmt.Errorf, return a typed error
  138. return nil, fmt.Errorf("gocql: unable to create session: %v", err)
  139. }
  140. }
  141. return s, nil
  142. }
  143. func (s *Session) init() error {
  144. hosts, err := addrsToHosts(s.cfg.Hosts, s.cfg.Port)
  145. if err != nil {
  146. return err
  147. }
  148. s.ring.endpoints = hosts
  149. if !s.cfg.disableControlConn {
  150. s.control = createControlConn(s)
  151. if s.cfg.ProtoVersion == 0 {
  152. proto, err := s.control.discoverProtocol(hosts)
  153. if err != nil {
  154. return fmt.Errorf("unable to discover protocol version: %v", err)
  155. } else if proto == 0 {
  156. return errors.New("unable to discovery protocol version")
  157. }
  158. // TODO(zariel): we really only need this in 1 place
  159. s.cfg.ProtoVersion = proto
  160. s.connCfg.ProtoVersion = proto
  161. }
  162. if err := s.control.connect(hosts); err != nil {
  163. return err
  164. }
  165. if !s.cfg.DisableInitialHostLookup {
  166. var partitioner string
  167. newHosts, partitioner, err := s.hostSource.GetHosts()
  168. if err != nil {
  169. return err
  170. }
  171. s.policy.SetPartitioner(partitioner)
  172. filteredHosts := make([]*HostInfo, 0, len(newHosts))
  173. for _, host := range newHosts {
  174. if !s.cfg.filterHost(host) {
  175. filteredHosts = append(filteredHosts, host)
  176. }
  177. }
  178. hosts = append(hosts, filteredHosts...)
  179. }
  180. }
  181. hostMap := make(map[string]*HostInfo, len(hosts))
  182. for _, host := range hosts {
  183. hostMap[host.ConnectAddress().String()] = host
  184. }
  185. for _, host := range hostMap {
  186. host = s.ring.addOrUpdate(host)
  187. s.addNewNode(host)
  188. }
  189. // TODO(zariel): we probably dont need this any more as we verify that we
  190. // can connect to one of the endpoints supplied by using the control conn.
  191. // See if there are any connections in the pool
  192. if s.cfg.ReconnectInterval > 0 {
  193. go s.reconnectDownedHosts(s.cfg.ReconnectInterval)
  194. }
  195. // If we disable the initial host lookup, we need to still check if the
  196. // cluster is using the newer system schema or not... however, if control
  197. // connection is disable, we really have no choice, so we just make our
  198. // best guess...
  199. if !s.cfg.disableControlConn && s.cfg.DisableInitialHostLookup {
  200. newer, _ := checkSystemSchema(s.control)
  201. s.useSystemSchema = newer
  202. } else {
  203. host := s.ring.rrHost()
  204. s.useSystemSchema = host.Version().Major >= 3
  205. }
  206. if s.pool.Size() == 0 {
  207. return ErrNoConnectionsStarted
  208. }
  209. return nil
  210. }
  211. func (s *Session) reconnectDownedHosts(intv time.Duration) {
  212. reconnectTicker := time.NewTicker(intv)
  213. defer reconnectTicker.Stop()
  214. for {
  215. select {
  216. case <-reconnectTicker.C:
  217. hosts := s.ring.allHosts()
  218. // Print session.ring for debug.
  219. if gocqlDebug {
  220. buf := bytes.NewBufferString("Session.ring:")
  221. for _, h := range hosts {
  222. buf.WriteString("[" + h.ConnectAddress().String() + ":" + h.State().String() + "]")
  223. }
  224. Logger.Println(buf.String())
  225. }
  226. for _, h := range hosts {
  227. if h.IsUp() {
  228. continue
  229. }
  230. s.handleNodeUp(h.ConnectAddress(), h.Port(), true)
  231. }
  232. case <-s.quit:
  233. return
  234. }
  235. }
  236. }
  237. // SetConsistency sets the default consistency level for this session. This
  238. // setting can also be changed on a per-query basis and the default value
  239. // is Quorum.
  240. func (s *Session) SetConsistency(cons Consistency) {
  241. s.mu.Lock()
  242. s.cons = cons
  243. s.mu.Unlock()
  244. }
  245. // SetPageSize sets the default page size for this session. A value <= 0 will
  246. // disable paging. This setting can also be changed on a per-query basis.
  247. func (s *Session) SetPageSize(n int) {
  248. s.mu.Lock()
  249. s.pageSize = n
  250. s.mu.Unlock()
  251. }
  252. // SetPrefetch sets the default threshold for pre-fetching new pages. If
  253. // there are only p*pageSize rows remaining, the next page will be requested
  254. // automatically. This value can also be changed on a per-query basis and
  255. // the default value is 0.25.
  256. func (s *Session) SetPrefetch(p float64) {
  257. s.mu.Lock()
  258. s.prefetch = p
  259. s.mu.Unlock()
  260. }
  261. // SetTrace sets the default tracer for this session. This setting can also
  262. // be changed on a per-query basis.
  263. func (s *Session) SetTrace(trace Tracer) {
  264. s.mu.Lock()
  265. s.trace = trace
  266. s.mu.Unlock()
  267. }
  268. // Query generates a new query object for interacting with the database.
  269. // Further details of the query may be tweaked using the resulting query
  270. // value before the query is executed. Query is automatically prepared
  271. // if it has not previously been executed.
  272. func (s *Session) Query(stmt string, values ...interface{}) *Query {
  273. qry := queryPool.Get().(*Query)
  274. qry.session = s
  275. qry.stmt = stmt
  276. qry.values = values
  277. qry.defaultsFromSession()
  278. return qry
  279. }
  280. type QueryInfo struct {
  281. Id []byte
  282. Args []ColumnInfo
  283. Rval []ColumnInfo
  284. PKeyColumns []int
  285. }
  286. // Bind generates a new query object based on the query statement passed in.
  287. // The query is automatically prepared if it has not previously been executed.
  288. // The binding callback allows the application to define which query argument
  289. // values will be marshalled as part of the query execution.
  290. // During execution, the meta data of the prepared query will be routed to the
  291. // binding callback, which is responsible for producing the query argument values.
  292. func (s *Session) Bind(stmt string, b func(q *QueryInfo) ([]interface{}, error)) *Query {
  293. qry := queryPool.Get().(*Query)
  294. qry.session = s
  295. qry.stmt = stmt
  296. qry.binding = b
  297. qry.defaultsFromSession()
  298. return qry
  299. }
  300. // Close closes all connections. The session is unusable after this
  301. // operation.
  302. func (s *Session) Close() {
  303. s.closeMu.Lock()
  304. defer s.closeMu.Unlock()
  305. if s.isClosed {
  306. return
  307. }
  308. s.isClosed = true
  309. if s.pool != nil {
  310. s.pool.Close()
  311. }
  312. if s.control != nil {
  313. s.control.close()
  314. }
  315. if s.nodeEvents != nil {
  316. s.nodeEvents.stop()
  317. }
  318. if s.schemaEvents != nil {
  319. s.schemaEvents.stop()
  320. }
  321. if s.quit != nil {
  322. close(s.quit)
  323. }
  324. }
  325. func (s *Session) Closed() bool {
  326. s.closeMu.RLock()
  327. closed := s.isClosed
  328. s.closeMu.RUnlock()
  329. return closed
  330. }
  331. func (s *Session) executeQuery(qry *Query) (it *Iter) {
  332. // fail fast
  333. if s.Closed() {
  334. return &Iter{err: ErrSessionClosed}
  335. }
  336. iter, err := s.executor.executeQuery(qry)
  337. if err != nil {
  338. return &Iter{err: err}
  339. }
  340. if iter == nil {
  341. panic("nil iter")
  342. }
  343. return iter
  344. }
  345. func (s *Session) removeHost(h *HostInfo) {
  346. s.policy.RemoveHost(h)
  347. s.pool.removeHost(h.ConnectAddress())
  348. s.ring.removeHost(h.ConnectAddress())
  349. }
  350. // KeyspaceMetadata returns the schema metadata for the keyspace specified. Returns an error if the keyspace does not exist.
  351. func (s *Session) KeyspaceMetadata(keyspace string) (*KeyspaceMetadata, error) {
  352. // fail fast
  353. if s.Closed() {
  354. return nil, ErrSessionClosed
  355. } else if keyspace == "" {
  356. return nil, ErrNoKeyspace
  357. }
  358. return s.schemaDescriber.getSchema(keyspace)
  359. }
  360. func (s *Session) getConn() *Conn {
  361. hosts := s.ring.allHosts()
  362. for _, host := range hosts {
  363. if !host.IsUp() {
  364. continue
  365. }
  366. pool, ok := s.pool.getPool(host)
  367. if !ok {
  368. continue
  369. } else if conn := pool.Pick(); conn != nil {
  370. return conn
  371. }
  372. }
  373. return nil
  374. }
  375. // returns routing key indexes and type info
  376. func (s *Session) routingKeyInfo(ctx context.Context, stmt string) (*routingKeyInfo, error) {
  377. s.routingKeyInfoCache.mu.Lock()
  378. entry, cached := s.routingKeyInfoCache.lru.Get(stmt)
  379. if cached {
  380. // done accessing the cache
  381. s.routingKeyInfoCache.mu.Unlock()
  382. // the entry is an inflight struct similar to that used by
  383. // Conn to prepare statements
  384. inflight := entry.(*inflightCachedEntry)
  385. // wait for any inflight work
  386. inflight.wg.Wait()
  387. if inflight.err != nil {
  388. return nil, inflight.err
  389. }
  390. key, _ := inflight.value.(*routingKeyInfo)
  391. return key, nil
  392. }
  393. // create a new inflight entry while the data is created
  394. inflight := new(inflightCachedEntry)
  395. inflight.wg.Add(1)
  396. defer inflight.wg.Done()
  397. s.routingKeyInfoCache.lru.Add(stmt, inflight)
  398. s.routingKeyInfoCache.mu.Unlock()
  399. var (
  400. info *preparedStatment
  401. partitionKey []*ColumnMetadata
  402. )
  403. conn := s.getConn()
  404. if conn == nil {
  405. // TODO: better error?
  406. inflight.err = errors.New("gocql: unable to fetch prepared info: no connection available")
  407. return nil, inflight.err
  408. }
  409. // get the query info for the statement
  410. info, inflight.err = conn.prepareStatement(ctx, stmt, nil)
  411. if inflight.err != nil {
  412. // don't cache this error
  413. s.routingKeyInfoCache.Remove(stmt)
  414. return nil, inflight.err
  415. }
  416. // TODO: it would be nice to mark hosts here but as we are not using the policies
  417. // to fetch hosts we cant
  418. if info.request.colCount == 0 {
  419. // no arguments, no routing key, and no error
  420. return nil, nil
  421. }
  422. if len(info.request.pkeyColumns) > 0 {
  423. // proto v4 dont need to calculate primary key columns
  424. types := make([]TypeInfo, len(info.request.pkeyColumns))
  425. for i, col := range info.request.pkeyColumns {
  426. types[i] = info.request.columns[col].TypeInfo
  427. }
  428. routingKeyInfo := &routingKeyInfo{
  429. indexes: info.request.pkeyColumns,
  430. types: types,
  431. }
  432. inflight.value = routingKeyInfo
  433. return routingKeyInfo, nil
  434. }
  435. // get the table metadata
  436. table := info.request.columns[0].Table
  437. var keyspaceMetadata *KeyspaceMetadata
  438. keyspaceMetadata, inflight.err = s.KeyspaceMetadata(info.request.columns[0].Keyspace)
  439. if inflight.err != nil {
  440. // don't cache this error
  441. s.routingKeyInfoCache.Remove(stmt)
  442. return nil, inflight.err
  443. }
  444. tableMetadata, found := keyspaceMetadata.Tables[table]
  445. if !found {
  446. // unlikely that the statement could be prepared and the metadata for
  447. // the table couldn't be found, but this may indicate either a bug
  448. // in the metadata code, or that the table was just dropped.
  449. inflight.err = ErrNoMetadata
  450. // don't cache this error
  451. s.routingKeyInfoCache.Remove(stmt)
  452. return nil, inflight.err
  453. }
  454. partitionKey = tableMetadata.PartitionKey
  455. size := len(partitionKey)
  456. routingKeyInfo := &routingKeyInfo{
  457. indexes: make([]int, size),
  458. types: make([]TypeInfo, size),
  459. }
  460. for keyIndex, keyColumn := range partitionKey {
  461. // set an indicator for checking if the mapping is missing
  462. routingKeyInfo.indexes[keyIndex] = -1
  463. // find the column in the query info
  464. for argIndex, boundColumn := range info.request.columns {
  465. if keyColumn.Name == boundColumn.Name {
  466. // there may be many such bound columns, pick the first
  467. routingKeyInfo.indexes[keyIndex] = argIndex
  468. routingKeyInfo.types[keyIndex] = boundColumn.TypeInfo
  469. break
  470. }
  471. }
  472. if routingKeyInfo.indexes[keyIndex] == -1 {
  473. // missing a routing key column mapping
  474. // no routing key, and no error
  475. return nil, nil
  476. }
  477. }
  478. // cache this result
  479. inflight.value = routingKeyInfo
  480. return routingKeyInfo, nil
  481. }
  482. func (b *Batch) execute(ctx context.Context, conn *Conn) *Iter {
  483. return conn.executeBatch(ctx, b)
  484. }
  485. func (s *Session) executeBatch(batch *Batch) *Iter {
  486. // fail fast
  487. if s.Closed() {
  488. return &Iter{err: ErrSessionClosed}
  489. }
  490. // Prevent the execution of the batch if greater than the limit
  491. // Currently batches have a limit of 65536 queries.
  492. // https://datastax-oss.atlassian.net/browse/JAVA-229
  493. if batch.Size() > BatchSizeMaximum {
  494. return &Iter{err: ErrTooManyStmts}
  495. }
  496. iter, err := s.executor.executeQuery(batch)
  497. if err != nil {
  498. return &Iter{err: err}
  499. }
  500. return iter
  501. }
  502. // ExecuteBatch executes a batch operation and returns nil if successful
  503. // otherwise an error is returned describing the failure.
  504. func (s *Session) ExecuteBatch(batch *Batch) error {
  505. iter := s.executeBatch(batch)
  506. return iter.Close()
  507. }
  508. // ExecuteBatchCAS executes a batch operation and returns true if successful and
  509. // an iterator (to scan aditional rows if more than one conditional statement)
  510. // was sent.
  511. // Further scans on the interator must also remember to include
  512. // the applied boolean as the first argument to *Iter.Scan
  513. func (s *Session) ExecuteBatchCAS(batch *Batch, dest ...interface{}) (applied bool, iter *Iter, err error) {
  514. iter = s.executeBatch(batch)
  515. if err := iter.checkErrAndNotFound(); err != nil {
  516. iter.Close()
  517. return false, nil, err
  518. }
  519. if len(iter.Columns()) > 1 {
  520. dest = append([]interface{}{&applied}, dest...)
  521. iter.Scan(dest...)
  522. } else {
  523. iter.Scan(&applied)
  524. }
  525. return applied, iter, nil
  526. }
  527. // MapExecuteBatchCAS executes a batch operation much like ExecuteBatchCAS,
  528. // however it accepts a map rather than a list of arguments for the initial
  529. // scan.
  530. func (s *Session) MapExecuteBatchCAS(batch *Batch, dest map[string]interface{}) (applied bool, iter *Iter, err error) {
  531. iter = s.executeBatch(batch)
  532. if err := iter.checkErrAndNotFound(); err != nil {
  533. iter.Close()
  534. return false, nil, err
  535. }
  536. iter.MapScan(dest)
  537. applied = dest["[applied]"].(bool)
  538. delete(dest, "[applied]")
  539. // we usually close here, but instead of closing, just returin an error
  540. // if MapScan failed. Although Close just returns err, using Close
  541. // here might be confusing as we are not actually closing the iter
  542. return applied, iter, iter.err
  543. }
  544. func (s *Session) connect(host *HostInfo, errorHandler ConnErrorHandler) (*Conn, error) {
  545. if s.connectObserver != nil {
  546. obs := ObservedConnect{
  547. Host: host,
  548. Start: time.Now(),
  549. }
  550. conn, err := s.dial(host, s.connCfg, errorHandler)
  551. obs.End = time.Now()
  552. obs.Err = err
  553. s.connectObserver.ObserveConnect(obs)
  554. return conn, err
  555. }
  556. return s.dial(host, s.connCfg, errorHandler)
  557. }
  558. type hostMetrics struct {
  559. Attempts int
  560. TotalLatency int64
  561. }
  562. type queryMetrics struct {
  563. l sync.RWMutex
  564. m map[string]*hostMetrics
  565. }
  566. // Query represents a CQL statement that can be executed.
  567. type Query struct {
  568. stmt string
  569. values []interface{}
  570. cons Consistency
  571. pageSize int
  572. routingKey []byte
  573. routingKeyBuffer []byte
  574. pageState []byte
  575. prefetch float64
  576. trace Tracer
  577. observer QueryObserver
  578. session *Session
  579. rt RetryPolicy
  580. spec SpeculativeExecutionPolicy
  581. binding func(q *QueryInfo) ([]interface{}, error)
  582. serialCons SerialConsistency
  583. defaultTimestamp bool
  584. defaultTimestampValue int64
  585. disableSkipMetadata bool
  586. context context.Context
  587. idempotent bool
  588. customPayload map[string][]byte
  589. metrics *queryMetrics
  590. disableAutoPage bool
  591. }
  592. func (q *Query) defaultsFromSession() {
  593. s := q.session
  594. s.mu.RLock()
  595. q.cons = s.cons
  596. q.pageSize = s.pageSize
  597. q.trace = s.trace
  598. q.observer = s.queryObserver
  599. q.prefetch = s.prefetch
  600. q.rt = s.cfg.RetryPolicy
  601. q.serialCons = s.cfg.SerialConsistency
  602. q.defaultTimestamp = s.cfg.DefaultTimestamp
  603. q.idempotent = s.cfg.DefaultIdempotence
  604. q.metrics = &queryMetrics{m: make(map[string]*hostMetrics)}
  605. q.spec = &NonSpeculativeExecution{}
  606. s.mu.RUnlock()
  607. }
  608. func (q *Query) getHostMetrics(host *HostInfo) *hostMetrics {
  609. q.metrics.l.Lock()
  610. metrics, exists := q.metrics.m[host.ConnectAddress().String()]
  611. if !exists {
  612. // if the host is not in the map, it means it's been accessed for the first time
  613. metrics = &hostMetrics{}
  614. q.metrics.m[host.ConnectAddress().String()] = metrics
  615. }
  616. q.metrics.l.Unlock()
  617. return metrics
  618. }
  619. // Statement returns the statement that was used to generate this query.
  620. func (q Query) Statement() string {
  621. return q.stmt
  622. }
  623. // String implements the stringer interface.
  624. func (q Query) String() string {
  625. return fmt.Sprintf("[query statement=%q values=%+v consistency=%s]", q.stmt, q.values, q.cons)
  626. }
  627. //Attempts returns the number of times the query was executed.
  628. func (q *Query) Attempts() int {
  629. q.metrics.l.Lock()
  630. var attempts int
  631. for _, metric := range q.metrics.m {
  632. attempts += metric.Attempts
  633. }
  634. q.metrics.l.Unlock()
  635. return attempts
  636. }
  637. func (q *Query) AddAttempts(i int, host *HostInfo) {
  638. hostMetric := q.getHostMetrics(host)
  639. q.metrics.l.Lock()
  640. hostMetric.Attempts += i
  641. q.metrics.l.Unlock()
  642. }
  643. //Latency returns the average amount of nanoseconds per attempt of the query.
  644. func (q *Query) Latency() int64 {
  645. q.metrics.l.Lock()
  646. var attempts int
  647. var latency int64
  648. for _, metric := range q.metrics.m {
  649. attempts += metric.Attempts
  650. latency += metric.TotalLatency
  651. }
  652. q.metrics.l.Unlock()
  653. if attempts > 0 {
  654. return latency / int64(attempts)
  655. }
  656. return 0
  657. }
  658. func (q *Query) AddLatency(l int64, host *HostInfo) {
  659. hostMetric := q.getHostMetrics(host)
  660. q.metrics.l.Lock()
  661. hostMetric.TotalLatency += l
  662. q.metrics.l.Unlock()
  663. }
  664. // Consistency sets the consistency level for this query. If no consistency
  665. // level have been set, the default consistency level of the cluster
  666. // is used.
  667. func (q *Query) Consistency(c Consistency) *Query {
  668. q.cons = c
  669. return q
  670. }
  671. // GetConsistency returns the currently configured consistency level for
  672. // the query.
  673. func (q *Query) GetConsistency() Consistency {
  674. return q.cons
  675. }
  676. // Same as Consistency but without a return value
  677. func (q *Query) SetConsistency(c Consistency) {
  678. q.cons = c
  679. }
  680. // CustomPayload sets the custom payload level for this query.
  681. func (q *Query) CustomPayload(customPayload map[string][]byte) *Query {
  682. q.customPayload = customPayload
  683. return q
  684. }
  685. func (q *Query) Context() context.Context {
  686. if q.context == nil {
  687. return context.Background()
  688. }
  689. return q.context
  690. }
  691. // Trace enables tracing of this query. Look at the documentation of the
  692. // Tracer interface to learn more about tracing.
  693. func (q *Query) Trace(trace Tracer) *Query {
  694. q.trace = trace
  695. return q
  696. }
  697. // Observer enables query-level observer on this query.
  698. // The provided observer will be called every time this query is executed.
  699. func (q *Query) Observer(observer QueryObserver) *Query {
  700. q.observer = observer
  701. return q
  702. }
  703. // PageSize will tell the iterator to fetch the result in pages of size n.
  704. // This is useful for iterating over large result sets, but setting the
  705. // page size too low might decrease the performance. This feature is only
  706. // available in Cassandra 2 and onwards.
  707. func (q *Query) PageSize(n int) *Query {
  708. q.pageSize = n
  709. return q
  710. }
  711. // DefaultTimestamp will enable the with default timestamp flag on the query.
  712. // If enable, this will replace the server side assigned
  713. // timestamp as default timestamp. Note that a timestamp in the query itself
  714. // will still override this timestamp. This is entirely optional.
  715. //
  716. // Only available on protocol >= 3
  717. func (q *Query) DefaultTimestamp(enable bool) *Query {
  718. q.defaultTimestamp = enable
  719. return q
  720. }
  721. // WithTimestamp will enable the with default timestamp flag on the query
  722. // like DefaultTimestamp does. But also allows to define value for timestamp.
  723. // It works the same way as USING TIMESTAMP in the query itself, but
  724. // should not break prepared query optimization
  725. //
  726. // Only available on protocol >= 3
  727. func (q *Query) WithTimestamp(timestamp int64) *Query {
  728. q.DefaultTimestamp(true)
  729. q.defaultTimestampValue = timestamp
  730. return q
  731. }
  732. // RoutingKey sets the routing key to use when a token aware connection
  733. // pool is used to optimize the routing of this query.
  734. func (q *Query) RoutingKey(routingKey []byte) *Query {
  735. q.routingKey = routingKey
  736. return q
  737. }
  738. func (q *Query) withContext(ctx context.Context) ExecutableQuery {
  739. // I really wish go had covariant types
  740. return q.WithContext(ctx)
  741. }
  742. // WithContext returns a shallow copy of q with its context
  743. // set to ctx.
  744. //
  745. // The provided context controls the entire lifetime of executing a
  746. // query, queries will be canceled and return once the context is
  747. // canceled.
  748. func (q *Query) WithContext(ctx context.Context) *Query {
  749. q2 := *q
  750. q2.context = ctx
  751. return &q2
  752. }
  753. // Deprecate: does nothing, cancel the context passed to WithContext
  754. func (q *Query) Cancel() {
  755. // TODO: delete
  756. }
  757. func (q *Query) execute(ctx context.Context, conn *Conn) *Iter {
  758. return conn.executeQuery(ctx, q)
  759. }
  760. func (q *Query) attempt(keyspace string, end, start time.Time, iter *Iter, host *HostInfo) {
  761. q.AddAttempts(1, host)
  762. q.AddLatency(end.Sub(start).Nanoseconds(), host)
  763. if q.observer != nil {
  764. q.observer.ObserveQuery(q.Context(), ObservedQuery{
  765. Keyspace: keyspace,
  766. Statement: q.stmt,
  767. Start: start,
  768. End: end,
  769. Rows: iter.numRows,
  770. Host: host,
  771. Metrics: q.getHostMetrics(host),
  772. Err: iter.err,
  773. })
  774. }
  775. }
  776. func (q *Query) retryPolicy() RetryPolicy {
  777. return q.rt
  778. }
  779. // Keyspace returns the keyspace the query will be executed against.
  780. func (q *Query) Keyspace() string {
  781. if q.session == nil {
  782. return ""
  783. }
  784. // TODO(chbannis): this should be parsed from the query or we should let
  785. // this be set by users.
  786. return q.session.cfg.Keyspace
  787. }
  788. // GetRoutingKey gets the routing key to use for routing this query. If
  789. // a routing key has not been explicitly set, then the routing key will
  790. // be constructed if possible using the keyspace's schema and the query
  791. // info for this query statement. If the routing key cannot be determined
  792. // then nil will be returned with no error. On any error condition,
  793. // an error description will be returned.
  794. func (q *Query) GetRoutingKey() ([]byte, error) {
  795. if q.routingKey != nil {
  796. return q.routingKey, nil
  797. } else if q.binding != nil && len(q.values) == 0 {
  798. // If this query was created using session.Bind we wont have the query
  799. // values yet, so we have to pass down to the next policy.
  800. // TODO: Remove this and handle this case
  801. return nil, nil
  802. }
  803. // try to determine the routing key
  804. routingKeyInfo, err := q.session.routingKeyInfo(q.Context(), q.stmt)
  805. if err != nil {
  806. return nil, err
  807. }
  808. if routingKeyInfo == nil {
  809. return nil, nil
  810. }
  811. if len(routingKeyInfo.indexes) == 1 {
  812. // single column routing key
  813. routingKey, err := Marshal(
  814. routingKeyInfo.types[0],
  815. q.values[routingKeyInfo.indexes[0]],
  816. )
  817. if err != nil {
  818. return nil, err
  819. }
  820. return routingKey, nil
  821. }
  822. // We allocate that buffer only once, so that further re-bind/exec of the
  823. // same query don't allocate more memory.
  824. if q.routingKeyBuffer == nil {
  825. q.routingKeyBuffer = make([]byte, 0, 256)
  826. }
  827. // composite routing key
  828. buf := bytes.NewBuffer(q.routingKeyBuffer)
  829. for i := range routingKeyInfo.indexes {
  830. encoded, err := Marshal(
  831. routingKeyInfo.types[i],
  832. q.values[routingKeyInfo.indexes[i]],
  833. )
  834. if err != nil {
  835. return nil, err
  836. }
  837. lenBuf := []byte{0x00, 0x00}
  838. binary.BigEndian.PutUint16(lenBuf, uint16(len(encoded)))
  839. buf.Write(lenBuf)
  840. buf.Write(encoded)
  841. buf.WriteByte(0x00)
  842. }
  843. routingKey := buf.Bytes()
  844. return routingKey, nil
  845. }
  846. func (q *Query) shouldPrepare() bool {
  847. stmt := strings.TrimLeftFunc(strings.TrimRightFunc(q.stmt, func(r rune) bool {
  848. return unicode.IsSpace(r) || r == ';'
  849. }), unicode.IsSpace)
  850. var stmtType string
  851. if n := strings.IndexFunc(stmt, unicode.IsSpace); n >= 0 {
  852. stmtType = strings.ToLower(stmt[:n])
  853. }
  854. if stmtType == "begin" {
  855. if n := strings.LastIndexFunc(stmt, unicode.IsSpace); n >= 0 {
  856. stmtType = strings.ToLower(stmt[n+1:])
  857. }
  858. }
  859. switch stmtType {
  860. case "select", "insert", "update", "delete", "batch":
  861. return true
  862. }
  863. return false
  864. }
  865. // SetPrefetch sets the default threshold for pre-fetching new pages. If
  866. // there are only p*pageSize rows remaining, the next page will be requested
  867. // automatically.
  868. func (q *Query) Prefetch(p float64) *Query {
  869. q.prefetch = p
  870. return q
  871. }
  872. // RetryPolicy sets the policy to use when retrying the query.
  873. func (q *Query) RetryPolicy(r RetryPolicy) *Query {
  874. q.rt = r
  875. return q
  876. }
  877. // SetSpeculativeExecutionPolicy sets the execution policy
  878. func (q *Query) SetSpeculativeExecutionPolicy(sp SpeculativeExecutionPolicy) *Query {
  879. q.spec = sp
  880. return q
  881. }
  882. // speculativeExecutionPolicy fetches the policy
  883. func (q *Query) speculativeExecutionPolicy() SpeculativeExecutionPolicy {
  884. return q.spec
  885. }
  886. func (q *Query) IsIdempotent() bool {
  887. return q.idempotent
  888. }
  889. // Idempotent marks the query as being idempotent or not depending on
  890. // the value.
  891. func (q *Query) Idempotent(value bool) *Query {
  892. q.idempotent = value
  893. return q
  894. }
  895. // Bind sets query arguments of query. This can also be used to rebind new query arguments
  896. // to an existing query instance.
  897. func (q *Query) Bind(v ...interface{}) *Query {
  898. q.values = v
  899. return q
  900. }
  901. // SerialConsistency sets the consistency level for the
  902. // serial phase of conditional updates. That consistency can only be
  903. // either SERIAL or LOCAL_SERIAL and if not present, it defaults to
  904. // SERIAL. This option will be ignored for anything else that a
  905. // conditional update/insert.
  906. func (q *Query) SerialConsistency(cons SerialConsistency) *Query {
  907. q.serialCons = cons
  908. return q
  909. }
  910. // PageState sets the paging state for the query to resume paging from a specific
  911. // point in time. Setting this will disable to query paging for this query, and
  912. // must be used for all subsequent pages.
  913. func (q *Query) PageState(state []byte) *Query {
  914. q.pageState = state
  915. q.disableAutoPage = true
  916. return q
  917. }
  918. // NoSkipMetadata will override the internal result metadata cache so that the driver does not
  919. // send skip_metadata for queries, this means that the result will always contain
  920. // the metadata to parse the rows and will not reuse the metadata from the prepared
  921. // staement. This should only be used to work around cassandra bugs, such as when using
  922. // CAS operations which do not end in Cas.
  923. //
  924. // See https://issues.apache.org/jira/browse/CASSANDRA-11099
  925. // https://github.com/gocql/gocql/issues/612
  926. func (q *Query) NoSkipMetadata() *Query {
  927. q.disableSkipMetadata = true
  928. return q
  929. }
  930. // Exec executes the query without returning any rows.
  931. func (q *Query) Exec() error {
  932. return q.Iter().Close()
  933. }
  934. func isUseStatement(stmt string) bool {
  935. if len(stmt) < 3 {
  936. return false
  937. }
  938. return strings.EqualFold(stmt[0:3], "use")
  939. }
  940. // Iter executes the query and returns an iterator capable of iterating
  941. // over all results.
  942. func (q *Query) Iter() *Iter {
  943. if isUseStatement(q.stmt) {
  944. return &Iter{err: ErrUseStmt}
  945. }
  946. return q.session.executeQuery(q)
  947. }
  948. // MapScan executes the query, copies the columns of the first selected
  949. // row into the map pointed at by m and discards the rest. If no rows
  950. // were selected, ErrNotFound is returned.
  951. func (q *Query) MapScan(m map[string]interface{}) error {
  952. iter := q.Iter()
  953. if err := iter.checkErrAndNotFound(); err != nil {
  954. return err
  955. }
  956. iter.MapScan(m)
  957. return iter.Close()
  958. }
  959. // Scan executes the query, copies the columns of the first selected
  960. // row into the values pointed at by dest and discards the rest. If no rows
  961. // were selected, ErrNotFound is returned.
  962. func (q *Query) Scan(dest ...interface{}) error {
  963. iter := q.Iter()
  964. if err := iter.checkErrAndNotFound(); err != nil {
  965. return err
  966. }
  967. iter.Scan(dest...)
  968. return iter.Close()
  969. }
  970. // ScanCAS executes a lightweight transaction (i.e. an UPDATE or INSERT
  971. // statement containing an IF clause). If the transaction fails because
  972. // the existing values did not match, the previous values will be stored
  973. // in dest.
  974. func (q *Query) ScanCAS(dest ...interface{}) (applied bool, err error) {
  975. q.disableSkipMetadata = true
  976. iter := q.Iter()
  977. if err := iter.checkErrAndNotFound(); err != nil {
  978. return false, err
  979. }
  980. if len(iter.Columns()) > 1 {
  981. dest = append([]interface{}{&applied}, dest...)
  982. iter.Scan(dest...)
  983. } else {
  984. iter.Scan(&applied)
  985. }
  986. return applied, iter.Close()
  987. }
  988. // MapScanCAS executes a lightweight transaction (i.e. an UPDATE or INSERT
  989. // statement containing an IF clause). If the transaction fails because
  990. // the existing values did not match, the previous values will be stored
  991. // in dest map.
  992. //
  993. // As for INSERT .. IF NOT EXISTS, previous values will be returned as if
  994. // SELECT * FROM. So using ScanCAS with INSERT is inherently prone to
  995. // column mismatching. MapScanCAS is added to capture them safely.
  996. func (q *Query) MapScanCAS(dest map[string]interface{}) (applied bool, err error) {
  997. q.disableSkipMetadata = true
  998. iter := q.Iter()
  999. if err := iter.checkErrAndNotFound(); err != nil {
  1000. return false, err
  1001. }
  1002. iter.MapScan(dest)
  1003. applied = dest["[applied]"].(bool)
  1004. delete(dest, "[applied]")
  1005. return applied, iter.Close()
  1006. }
  1007. // Release releases a query back into a pool of queries. Released Queries
  1008. // cannot be reused.
  1009. //
  1010. // Example:
  1011. // qry := session.Query("SELECT * FROM my_table")
  1012. // qry.Exec()
  1013. // qry.Release()
  1014. func (q *Query) Release() {
  1015. q.reset()
  1016. queryPool.Put(q)
  1017. }
  1018. // reset zeroes out all fields of a query so that it can be safely pooled.
  1019. func (q *Query) reset() {
  1020. *q = Query{}
  1021. }
  1022. // Iter represents an iterator that can be used to iterate over all rows that
  1023. // were returned by a query. The iterator might send additional queries to the
  1024. // database during the iteration if paging was enabled.
  1025. type Iter struct {
  1026. err error
  1027. pos int
  1028. meta resultMetadata
  1029. numRows int
  1030. next *nextIter
  1031. host *HostInfo
  1032. framer *framer
  1033. closed int32
  1034. }
  1035. // Host returns the host which the query was sent to.
  1036. func (iter *Iter) Host() *HostInfo {
  1037. return iter.host
  1038. }
  1039. // Columns returns the name and type of the selected columns.
  1040. func (iter *Iter) Columns() []ColumnInfo {
  1041. return iter.meta.columns
  1042. }
  1043. type Scanner interface {
  1044. // Next advances the row pointer to point at the next row, the row is valid until
  1045. // the next call of Next. It returns true if there is a row which is available to be
  1046. // scanned into with Scan.
  1047. // Next must be called before every call to Scan.
  1048. Next() bool
  1049. // Scan copies the current row's columns into dest. If the length of dest does not equal
  1050. // the number of columns returned in the row an error is returned. If an error is encountered
  1051. // when unmarshalling a column into the value in dest an error is returned and the row is invalidated
  1052. // until the next call to Next.
  1053. // Next must be called before calling Scan, if it is not an error is returned.
  1054. Scan(...interface{}) error
  1055. // Err returns the if there was one during iteration that resulted in iteration being unable to complete.
  1056. // Err will also release resources held by the iterator, the Scanner should not used after being called.
  1057. Err() error
  1058. }
  1059. type iterScanner struct {
  1060. iter *Iter
  1061. cols [][]byte
  1062. valid bool
  1063. }
  1064. func (is *iterScanner) Next() bool {
  1065. iter := is.iter
  1066. if iter.err != nil {
  1067. return false
  1068. }
  1069. if iter.pos >= iter.numRows {
  1070. if iter.next != nil {
  1071. is.iter = iter.next.fetch()
  1072. return is.Next()
  1073. }
  1074. return false
  1075. }
  1076. for i := 0; i < len(is.cols); i++ {
  1077. col, err := iter.readColumn()
  1078. if err != nil {
  1079. iter.err = err
  1080. return false
  1081. }
  1082. is.cols[i] = col
  1083. }
  1084. iter.pos++
  1085. is.valid = true
  1086. return true
  1087. }
  1088. func scanColumn(p []byte, col ColumnInfo, dest []interface{}) (int, error) {
  1089. if dest[0] == nil {
  1090. return 1, nil
  1091. }
  1092. if col.TypeInfo.Type() == TypeTuple {
  1093. // this will panic, actually a bug, please report
  1094. tuple := col.TypeInfo.(TupleTypeInfo)
  1095. count := len(tuple.Elems)
  1096. // here we pass in a slice of the struct which has the number number of
  1097. // values as elements in the tuple
  1098. if err := Unmarshal(col.TypeInfo, p, dest[:count]); err != nil {
  1099. return 0, err
  1100. }
  1101. return count, nil
  1102. } else {
  1103. if err := Unmarshal(col.TypeInfo, p, dest[0]); err != nil {
  1104. return 0, err
  1105. }
  1106. return 1, nil
  1107. }
  1108. }
  1109. func (is *iterScanner) Scan(dest ...interface{}) error {
  1110. if !is.valid {
  1111. return errors.New("gocql: Scan called without calling Next")
  1112. }
  1113. iter := is.iter
  1114. // currently only support scanning into an expand tuple, such that its the same
  1115. // as scanning in more values from a single column
  1116. if len(dest) != iter.meta.actualColCount {
  1117. return fmt.Errorf("gocql: not enough columns to scan into: have %d want %d", len(dest), iter.meta.actualColCount)
  1118. }
  1119. // i is the current position in dest, could posible replace it and just use
  1120. // slices of dest
  1121. i := 0
  1122. var err error
  1123. for _, col := range iter.meta.columns {
  1124. var n int
  1125. n, err = scanColumn(is.cols[i], col, dest[i:])
  1126. if err != nil {
  1127. break
  1128. }
  1129. i += n
  1130. }
  1131. is.valid = false
  1132. return err
  1133. }
  1134. func (is *iterScanner) Err() error {
  1135. iter := is.iter
  1136. is.iter = nil
  1137. is.cols = nil
  1138. is.valid = false
  1139. return iter.Close()
  1140. }
  1141. // Scanner returns a row Scanner which provides an interface to scan rows in a manner which is
  1142. // similar to database/sql. The iter should NOT be used again after calling this method.
  1143. func (iter *Iter) Scanner() Scanner {
  1144. if iter == nil {
  1145. return nil
  1146. }
  1147. return &iterScanner{iter: iter, cols: make([][]byte, len(iter.meta.columns))}
  1148. }
  1149. func (iter *Iter) readColumn() ([]byte, error) {
  1150. return iter.framer.readBytesInternal()
  1151. }
  1152. // Scan consumes the next row of the iterator and copies the columns of the
  1153. // current row into the values pointed at by dest. Use nil as a dest value
  1154. // to skip the corresponding column. Scan might send additional queries
  1155. // to the database to retrieve the next set of rows if paging was enabled.
  1156. //
  1157. // Scan returns true if the row was successfully unmarshaled or false if the
  1158. // end of the result set was reached or if an error occurred. Close should
  1159. // be called afterwards to retrieve any potential errors.
  1160. func (iter *Iter) Scan(dest ...interface{}) bool {
  1161. if iter.err != nil {
  1162. return false
  1163. }
  1164. if iter.pos >= iter.numRows {
  1165. if iter.next != nil {
  1166. *iter = *iter.next.fetch()
  1167. return iter.Scan(dest...)
  1168. }
  1169. return false
  1170. }
  1171. if iter.next != nil && iter.pos >= iter.next.pos {
  1172. go iter.next.fetch()
  1173. }
  1174. // currently only support scanning into an expand tuple, such that its the same
  1175. // as scanning in more values from a single column
  1176. if len(dest) != iter.meta.actualColCount {
  1177. iter.err = fmt.Errorf("gocql: not enough columns to scan into: have %d want %d", len(dest), iter.meta.actualColCount)
  1178. return false
  1179. }
  1180. // i is the current position in dest, could posible replace it and just use
  1181. // slices of dest
  1182. i := 0
  1183. for _, col := range iter.meta.columns {
  1184. colBytes, err := iter.readColumn()
  1185. if err != nil {
  1186. iter.err = err
  1187. return false
  1188. }
  1189. n, err := scanColumn(colBytes, col, dest[i:])
  1190. if err != nil {
  1191. iter.err = err
  1192. return false
  1193. }
  1194. i += n
  1195. }
  1196. iter.pos++
  1197. return true
  1198. }
  1199. // GetCustomPayload returns any parsed custom payload results if given in the
  1200. // response from Cassandra. Note that the result is not a copy.
  1201. //
  1202. // This additional feature of CQL Protocol v4
  1203. // allows additional results and query information to be returned by
  1204. // custom QueryHandlers running in your C* cluster.
  1205. // See https://datastax.github.io/java-driver/manual/custom_payloads/
  1206. func (iter *Iter) GetCustomPayload() map[string][]byte {
  1207. return iter.framer.customPayload
  1208. }
  1209. // Warnings returns any warnings generated if given in the response from Cassandra.
  1210. //
  1211. // This is only available starting with CQL Protocol v4.
  1212. func (iter *Iter) Warnings() []string {
  1213. if iter.framer != nil {
  1214. return iter.framer.header.warnings
  1215. }
  1216. return nil
  1217. }
  1218. // Close closes the iterator and returns any errors that happened during
  1219. // the query or the iteration.
  1220. func (iter *Iter) Close() error {
  1221. if atomic.CompareAndSwapInt32(&iter.closed, 0, 1) {
  1222. if iter.framer != nil {
  1223. iter.framer = nil
  1224. }
  1225. }
  1226. return iter.err
  1227. }
  1228. // WillSwitchPage detects if iterator reached end of current page
  1229. // and the next page is available.
  1230. func (iter *Iter) WillSwitchPage() bool {
  1231. return iter.pos >= iter.numRows && iter.next != nil
  1232. }
  1233. // checkErrAndNotFound handle error and NotFound in one method.
  1234. func (iter *Iter) checkErrAndNotFound() error {
  1235. if iter.err != nil {
  1236. return iter.err
  1237. } else if iter.numRows == 0 {
  1238. return ErrNotFound
  1239. }
  1240. return nil
  1241. }
  1242. // PageState return the current paging state for a query which can be used for
  1243. // subsequent queries to resume paging this point.
  1244. func (iter *Iter) PageState() []byte {
  1245. return iter.meta.pagingState
  1246. }
  1247. // NumRows returns the number of rows in this pagination, it will update when new
  1248. // pages are fetched, it is not the value of the total number of rows this iter
  1249. // will return unless there is only a single page returned.
  1250. func (iter *Iter) NumRows() int {
  1251. return iter.numRows
  1252. }
  1253. type nextIter struct {
  1254. qry *Query
  1255. pos int
  1256. once sync.Once
  1257. next *Iter
  1258. }
  1259. func (n *nextIter) fetch() *Iter {
  1260. n.once.Do(func() {
  1261. n.next = n.qry.session.executeQuery(n.qry)
  1262. })
  1263. return n.next
  1264. }
  1265. type Batch struct {
  1266. Type BatchType
  1267. Entries []BatchEntry
  1268. Cons Consistency
  1269. CustomPayload map[string][]byte
  1270. rt RetryPolicy
  1271. spec SpeculativeExecutionPolicy
  1272. observer BatchObserver
  1273. serialCons SerialConsistency
  1274. defaultTimestamp bool
  1275. defaultTimestampValue int64
  1276. context context.Context
  1277. cancelBatch func()
  1278. keyspace string
  1279. metrics *queryMetrics
  1280. }
  1281. // NewBatch creates a new batch operation without defaults from the cluster
  1282. //
  1283. // Deprecated: use session.NewBatch instead
  1284. func NewBatch(typ BatchType) *Batch {
  1285. return &Batch{
  1286. Type: typ,
  1287. metrics: &queryMetrics{m: make(map[string]*hostMetrics)},
  1288. spec: &NonSpeculativeExecution{},
  1289. }
  1290. }
  1291. // NewBatch creates a new batch operation using defaults defined in the cluster
  1292. func (s *Session) NewBatch(typ BatchType) *Batch {
  1293. s.mu.RLock()
  1294. batch := &Batch{
  1295. Type: typ,
  1296. rt: s.cfg.RetryPolicy,
  1297. serialCons: s.cfg.SerialConsistency,
  1298. observer: s.batchObserver,
  1299. Cons: s.cons,
  1300. defaultTimestamp: s.cfg.DefaultTimestamp,
  1301. keyspace: s.cfg.Keyspace,
  1302. metrics: &queryMetrics{m: make(map[string]*hostMetrics)},
  1303. spec: &NonSpeculativeExecution{},
  1304. }
  1305. s.mu.RUnlock()
  1306. return batch
  1307. }
  1308. func (b *Batch) getHostMetrics(host *HostInfo) *hostMetrics {
  1309. b.metrics.l.Lock()
  1310. metrics, exists := b.metrics.m[host.ConnectAddress().String()]
  1311. if !exists {
  1312. // if the host is not in the map, it means it's been accessed for the first time
  1313. metrics = &hostMetrics{}
  1314. b.metrics.m[host.ConnectAddress().String()] = metrics
  1315. }
  1316. b.metrics.l.Unlock()
  1317. return metrics
  1318. }
  1319. // Observer enables batch-level observer on this batch.
  1320. // The provided observer will be called every time this batched query is executed.
  1321. func (b *Batch) Observer(observer BatchObserver) *Batch {
  1322. b.observer = observer
  1323. return b
  1324. }
  1325. func (b *Batch) Keyspace() string {
  1326. return b.keyspace
  1327. }
  1328. // Attempts returns the number of attempts made to execute the batch.
  1329. func (b *Batch) Attempts() int {
  1330. b.metrics.l.Lock()
  1331. defer b.metrics.l.Unlock()
  1332. var attempts int
  1333. for _, metric := range b.metrics.m {
  1334. attempts += metric.Attempts
  1335. }
  1336. return attempts
  1337. }
  1338. func (b *Batch) AddAttempts(i int, host *HostInfo) {
  1339. hostMetric := b.getHostMetrics(host)
  1340. b.metrics.l.Lock()
  1341. hostMetric.Attempts += i
  1342. b.metrics.l.Unlock()
  1343. }
  1344. //Latency returns the average number of nanoseconds to execute a single attempt of the batch.
  1345. func (b *Batch) Latency() int64 {
  1346. b.metrics.l.Lock()
  1347. defer b.metrics.l.Unlock()
  1348. var (
  1349. attempts int
  1350. latency int64
  1351. )
  1352. for _, metric := range b.metrics.m {
  1353. attempts += metric.Attempts
  1354. latency += metric.TotalLatency
  1355. }
  1356. if attempts > 0 {
  1357. return latency / int64(attempts)
  1358. }
  1359. return 0
  1360. }
  1361. func (b *Batch) AddLatency(l int64, host *HostInfo) {
  1362. hostMetric := b.getHostMetrics(host)
  1363. b.metrics.l.Lock()
  1364. hostMetric.TotalLatency += l
  1365. b.metrics.l.Unlock()
  1366. }
  1367. // GetConsistency returns the currently configured consistency level for the batch
  1368. // operation.
  1369. func (b *Batch) GetConsistency() Consistency {
  1370. return b.Cons
  1371. }
  1372. // SetConsistency sets the currently configured consistency level for the batch
  1373. // operation.
  1374. func (b *Batch) SetConsistency(c Consistency) {
  1375. b.Cons = c
  1376. }
  1377. func (b *Batch) Context() context.Context {
  1378. if b.context == nil {
  1379. return context.Background()
  1380. }
  1381. return b.context
  1382. }
  1383. func (b *Batch) IsIdempotent() bool {
  1384. for _, entry := range b.Entries {
  1385. if !entry.Idempotent {
  1386. return false
  1387. }
  1388. }
  1389. return true
  1390. }
  1391. func (b *Batch) speculativeExecutionPolicy() SpeculativeExecutionPolicy {
  1392. return b.spec
  1393. }
  1394. func (b *Batch) SpeculativeExecutionPolicy(sp SpeculativeExecutionPolicy) *Batch {
  1395. b.spec = sp
  1396. return b
  1397. }
  1398. // Query adds the query to the batch operation
  1399. func (b *Batch) Query(stmt string, args ...interface{}) {
  1400. b.Entries = append(b.Entries, BatchEntry{Stmt: stmt, Args: args})
  1401. }
  1402. // Bind adds the query to the batch operation and correlates it with a binding callback
  1403. // that will be invoked when the batch is executed. The binding callback allows the application
  1404. // to define which query argument values will be marshalled as part of the batch execution.
  1405. func (b *Batch) Bind(stmt string, bind func(q *QueryInfo) ([]interface{}, error)) {
  1406. b.Entries = append(b.Entries, BatchEntry{Stmt: stmt, binding: bind})
  1407. }
  1408. func (b *Batch) retryPolicy() RetryPolicy {
  1409. return b.rt
  1410. }
  1411. // RetryPolicy sets the retry policy to use when executing the batch operation
  1412. func (b *Batch) RetryPolicy(r RetryPolicy) *Batch {
  1413. b.rt = r
  1414. return b
  1415. }
  1416. func (b *Batch) withContext(ctx context.Context) ExecutableQuery {
  1417. return b.WithContext(ctx)
  1418. }
  1419. // WithContext returns a shallow copy of b with its context
  1420. // set to ctx.
  1421. //
  1422. // The provided context controls the entire lifetime of executing a
  1423. // query, queries will be canceled and return once the context is
  1424. // canceled.
  1425. func (b *Batch) WithContext(ctx context.Context) *Batch {
  1426. b2 := *b
  1427. b2.context = ctx
  1428. return &b2
  1429. }
  1430. // Deprecate: does nothing, cancel the context passed to WithContext
  1431. func (*Batch) Cancel() {
  1432. // TODO: delete
  1433. }
  1434. // Size returns the number of batch statements to be executed by the batch operation.
  1435. func (b *Batch) Size() int {
  1436. return len(b.Entries)
  1437. }
  1438. // SerialConsistency sets the consistency level for the
  1439. // serial phase of conditional updates. That consistency can only be
  1440. // either SERIAL or LOCAL_SERIAL and if not present, it defaults to
  1441. // SERIAL. This option will be ignored for anything else that a
  1442. // conditional update/insert.
  1443. //
  1444. // Only available for protocol 3 and above
  1445. func (b *Batch) SerialConsistency(cons SerialConsistency) *Batch {
  1446. b.serialCons = cons
  1447. return b
  1448. }
  1449. // DefaultTimestamp will enable the with default timestamp flag on the query.
  1450. // If enable, this will replace the server side assigned
  1451. // timestamp as default timestamp. Note that a timestamp in the query itself
  1452. // will still override this timestamp. This is entirely optional.
  1453. //
  1454. // Only available on protocol >= 3
  1455. func (b *Batch) DefaultTimestamp(enable bool) *Batch {
  1456. b.defaultTimestamp = enable
  1457. return b
  1458. }
  1459. // WithTimestamp will enable the with default timestamp flag on the query
  1460. // like DefaultTimestamp does. But also allows to define value for timestamp.
  1461. // It works the same way as USING TIMESTAMP in the query itself, but
  1462. // should not break prepared query optimization
  1463. //
  1464. // Only available on protocol >= 3
  1465. func (b *Batch) WithTimestamp(timestamp int64) *Batch {
  1466. b.DefaultTimestamp(true)
  1467. b.defaultTimestampValue = timestamp
  1468. return b
  1469. }
  1470. func (b *Batch) attempt(keyspace string, end, start time.Time, iter *Iter, host *HostInfo) {
  1471. b.AddAttempts(1, host)
  1472. b.AddLatency(end.Sub(start).Nanoseconds(), host)
  1473. if b.observer == nil {
  1474. return
  1475. }
  1476. statements := make([]string, len(b.Entries))
  1477. for i, entry := range b.Entries {
  1478. statements[i] = entry.Stmt
  1479. }
  1480. b.observer.ObserveBatch(b.Context(), ObservedBatch{
  1481. Keyspace: keyspace,
  1482. Statements: statements,
  1483. Start: start,
  1484. End: end,
  1485. // Rows not used in batch observations // TODO - might be able to support it when using BatchCAS
  1486. Host: host,
  1487. Metrics: b.getHostMetrics(host),
  1488. Err: iter.err,
  1489. })
  1490. }
  1491. func (b *Batch) GetRoutingKey() ([]byte, error) {
  1492. // TODO: use the first statement in the batch as the routing key?
  1493. return nil, nil
  1494. }
  1495. type BatchType byte
  1496. const (
  1497. LoggedBatch BatchType = 0
  1498. UnloggedBatch BatchType = 1
  1499. CounterBatch BatchType = 2
  1500. )
  1501. type BatchEntry struct {
  1502. Stmt string
  1503. Args []interface{}
  1504. Idempotent bool
  1505. binding func(q *QueryInfo) ([]interface{}, error)
  1506. }
  1507. type ColumnInfo struct {
  1508. Keyspace string
  1509. Table string
  1510. Name string
  1511. TypeInfo TypeInfo
  1512. }
  1513. func (c ColumnInfo) String() string {
  1514. return fmt.Sprintf("[column keyspace=%s table=%s name=%s type=%v]", c.Keyspace, c.Table, c.Name, c.TypeInfo)
  1515. }
  1516. // routing key indexes LRU cache
  1517. type routingKeyInfoLRU struct {
  1518. lru *lru.Cache
  1519. mu sync.Mutex
  1520. }
  1521. type routingKeyInfo struct {
  1522. indexes []int
  1523. types []TypeInfo
  1524. }
  1525. func (r *routingKeyInfo) String() string {
  1526. return fmt.Sprintf("routing key index=%v types=%v", r.indexes, r.types)
  1527. }
  1528. func (r *routingKeyInfoLRU) Remove(key string) {
  1529. r.mu.Lock()
  1530. r.lru.Remove(key)
  1531. r.mu.Unlock()
  1532. }
  1533. //Max adjusts the maximum size of the cache and cleans up the oldest records if
  1534. //the new max is lower than the previous value. Not concurrency safe.
  1535. func (r *routingKeyInfoLRU) Max(max int) {
  1536. r.mu.Lock()
  1537. for r.lru.Len() > max {
  1538. r.lru.RemoveOldest()
  1539. }
  1540. r.lru.MaxEntries = max
  1541. r.mu.Unlock()
  1542. }
  1543. type inflightCachedEntry struct {
  1544. wg sync.WaitGroup
  1545. err error
  1546. value interface{}
  1547. }
  1548. // Tracer is the interface implemented by query tracers. Tracers have the
  1549. // ability to obtain a detailed event log of all events that happened during
  1550. // the execution of a query from Cassandra. Gathering this information might
  1551. // be essential for debugging and optimizing queries, but this feature should
  1552. // not be used on production systems with very high load.
  1553. type Tracer interface {
  1554. Trace(traceId []byte)
  1555. }
  1556. type traceWriter struct {
  1557. session *Session
  1558. w io.Writer
  1559. mu sync.Mutex
  1560. }
  1561. // NewTraceWriter returns a simple Tracer implementation that outputs
  1562. // the event log in a textual format.
  1563. func NewTraceWriter(session *Session, w io.Writer) Tracer {
  1564. return &traceWriter{session: session, w: w}
  1565. }
  1566. func (t *traceWriter) Trace(traceId []byte) {
  1567. var (
  1568. coordinator string
  1569. duration int
  1570. )
  1571. iter := t.session.control.query(`SELECT coordinator, duration
  1572. FROM system_traces.sessions
  1573. WHERE session_id = ?`, traceId)
  1574. iter.Scan(&coordinator, &duration)
  1575. if err := iter.Close(); err != nil {
  1576. t.mu.Lock()
  1577. fmt.Fprintln(t.w, "Error:", err)
  1578. t.mu.Unlock()
  1579. return
  1580. }
  1581. var (
  1582. timestamp time.Time
  1583. activity string
  1584. source string
  1585. elapsed int
  1586. )
  1587. t.mu.Lock()
  1588. defer t.mu.Unlock()
  1589. fmt.Fprintf(t.w, "Tracing session %016x (coordinator: %s, duration: %v):\n",
  1590. traceId, coordinator, time.Duration(duration)*time.Microsecond)
  1591. iter = t.session.control.query(`SELECT event_id, activity, source, source_elapsed
  1592. FROM system_traces.events
  1593. WHERE session_id = ?`, traceId)
  1594. for iter.Scan(&timestamp, &activity, &source, &elapsed) {
  1595. fmt.Fprintf(t.w, "%s: %s (source: %s, elapsed: %d)\n",
  1596. timestamp.Format("2006/01/02 15:04:05.999999"), activity, source, elapsed)
  1597. }
  1598. if err := iter.Close(); err != nil {
  1599. fmt.Fprintln(t.w, "Error:", err)
  1600. }
  1601. }
  1602. type ObservedQuery struct {
  1603. Keyspace string
  1604. Statement string
  1605. Start time.Time // time immediately before the query was called
  1606. End time.Time // time immediately after the query returned
  1607. // Rows is the number of rows in the current iter.
  1608. // In paginated queries, rows from previous scans are not counted.
  1609. // Rows is not used in batch queries and remains at the default value
  1610. Rows int
  1611. // Host is the informations about the host that performed the query
  1612. Host *HostInfo
  1613. // The metrics per this host
  1614. Metrics *hostMetrics
  1615. // Err is the error in the query.
  1616. // It only tracks network errors or errors of bad cassandra syntax, in particular selects with no match return nil error
  1617. Err error
  1618. }
  1619. // QueryObserver is the interface implemented by query observers / stat collectors.
  1620. //
  1621. // Experimental, this interface and use may change
  1622. type QueryObserver interface {
  1623. // ObserveQuery gets called on every query to cassandra, including all queries in an iterator when paging is enabled.
  1624. // It doesn't get called if there is no query because the session is closed or there are no connections available.
  1625. // The error reported only shows query errors, i.e. if a SELECT is valid but finds no matches it will be nil.
  1626. ObserveQuery(context.Context, ObservedQuery)
  1627. }
  1628. type ObservedBatch struct {
  1629. Keyspace string
  1630. Statements []string
  1631. Start time.Time // time immediately before the batch query was called
  1632. End time.Time // time immediately after the batch query returned
  1633. // Host is the informations about the host that performed the batch
  1634. Host *HostInfo
  1635. // Err is the error in the batch query.
  1636. // It only tracks network errors or errors of bad cassandra syntax, in particular selects with no match return nil error
  1637. Err error
  1638. // The metrics per this host
  1639. Metrics *hostMetrics
  1640. }
  1641. // BatchObserver is the interface implemented by batch observers / stat collectors.
  1642. type BatchObserver interface {
  1643. // ObserveBatch gets called on every batch query to cassandra.
  1644. // It also gets called once for each query in a batch.
  1645. // It doesn't get called if there is no query because the session is closed or there are no connections available.
  1646. // The error reported only shows query errors, i.e. if a SELECT is valid but finds no matches it will be nil.
  1647. // Unlike QueryObserver.ObserveQuery it does no reporting on rows read.
  1648. ObserveBatch(context.Context, ObservedBatch)
  1649. }
  1650. type ObservedConnect struct {
  1651. // Host is the information about the host about to connect
  1652. Host *HostInfo
  1653. Start time.Time // time immediately before the dial is called
  1654. End time.Time // time immediately after the dial returned
  1655. // Err is the connection error (if any)
  1656. Err error
  1657. }
  1658. // ConnectObserver is the interface implemented by connect observers / stat collectors.
  1659. type ConnectObserver interface {
  1660. // ObserveConnect gets called when a new connection to cassandra is made.
  1661. ObserveConnect(ObservedConnect)
  1662. }
  1663. type Error struct {
  1664. Code int
  1665. Message string
  1666. }
  1667. func (e Error) Error() string {
  1668. return e.Message
  1669. }
  1670. var (
  1671. ErrNotFound = errors.New("not found")
  1672. ErrUnavailable = errors.New("unavailable")
  1673. ErrUnsupported = errors.New("feature not supported")
  1674. ErrTooManyStmts = errors.New("too many statements")
  1675. ErrUseStmt = errors.New("use statements aren't supported. Please see https://github.com/gocql/gocql for explanation.")
  1676. ErrSessionClosed = errors.New("session has been closed")
  1677. ErrNoConnections = errors.New("gocql: no hosts available in the pool")
  1678. ErrNoKeyspace = errors.New("no keyspace provided")
  1679. ErrKeyspaceDoesNotExist = errors.New("keyspace does not exist")
  1680. ErrNoMetadata = errors.New("no metadata available")
  1681. )
  1682. type ErrProtocol struct{ error }
  1683. func NewErrProtocol(format string, args ...interface{}) error {
  1684. return ErrProtocol{fmt.Errorf(format, args...)}
  1685. }
  1686. // BatchSizeMaximum is the maximum number of statements a batch operation can have.
  1687. // This limit is set by cassandra and could change in the future.
  1688. const BatchSizeMaximum = 65535