conn.go 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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. "bufio"
  7. "crypto/tls"
  8. "errors"
  9. "fmt"
  10. "github.com/gocql/gocql/internal/lru"
  11. "io"
  12. "io/ioutil"
  13. "log"
  14. "net"
  15. "strconv"
  16. "strings"
  17. "sync"
  18. "sync/atomic"
  19. "time"
  20. "github.com/gocql/gocql/internal/streams"
  21. )
  22. var (
  23. approvedAuthenticators = [...]string{
  24. "org.apache.cassandra.auth.PasswordAuthenticator",
  25. "com.instaclustr.cassandra.auth.SharedSecretAuthenticator",
  26. }
  27. )
  28. func approve(authenticator string) bool {
  29. for _, s := range approvedAuthenticators {
  30. if authenticator == s {
  31. return true
  32. }
  33. }
  34. return false
  35. }
  36. //JoinHostPort is a utility to return a address string that can be used
  37. //gocql.Conn to form a connection with a host.
  38. func JoinHostPort(addr string, port int) string {
  39. addr = strings.TrimSpace(addr)
  40. if _, _, err := net.SplitHostPort(addr); err != nil {
  41. addr = net.JoinHostPort(addr, strconv.Itoa(port))
  42. }
  43. return addr
  44. }
  45. type Authenticator interface {
  46. Challenge(req []byte) (resp []byte, auth Authenticator, err error)
  47. Success(data []byte) error
  48. }
  49. type PasswordAuthenticator struct {
  50. Username string
  51. Password string
  52. }
  53. func (p PasswordAuthenticator) Challenge(req []byte) ([]byte, Authenticator, error) {
  54. if !approve(string(req)) {
  55. return nil, nil, fmt.Errorf("unexpected authenticator %q", req)
  56. }
  57. resp := make([]byte, 2+len(p.Username)+len(p.Password))
  58. resp[0] = 0
  59. copy(resp[1:], p.Username)
  60. resp[len(p.Username)+1] = 0
  61. copy(resp[2+len(p.Username):], p.Password)
  62. return resp, nil, nil
  63. }
  64. func (p PasswordAuthenticator) Success(data []byte) error {
  65. return nil
  66. }
  67. type SslOptions struct {
  68. tls.Config
  69. // CertPath and KeyPath are optional depending on server
  70. // config, but both fields must be omitted to avoid using a
  71. // client certificate
  72. CertPath string
  73. KeyPath string
  74. CaPath string //optional depending on server config
  75. // If you want to verify the hostname and server cert (like a wildcard for cass cluster) then you should turn this on
  76. // This option is basically the inverse of InSecureSkipVerify
  77. // See InSecureSkipVerify in http://golang.org/pkg/crypto/tls/ for more info
  78. EnableHostVerification bool
  79. }
  80. type ConnConfig struct {
  81. ProtoVersion int
  82. CQLVersion string
  83. Timeout time.Duration
  84. Compressor Compressor
  85. Authenticator Authenticator
  86. Keepalive time.Duration
  87. tlsConfig *tls.Config
  88. }
  89. type ConnErrorHandler interface {
  90. HandleError(conn *Conn, err error, closed bool)
  91. }
  92. type connErrorHandlerFn func(conn *Conn, err error, closed bool)
  93. func (fn connErrorHandlerFn) HandleError(conn *Conn, err error, closed bool) {
  94. fn(conn, err, closed)
  95. }
  96. // How many timeouts we will allow to occur before the connection is closed
  97. // and restarted. This is to prevent a single query timeout from killing a connection
  98. // which may be serving more queries just fine.
  99. // Default is 10, should not be changed concurrently with queries.
  100. var TimeoutLimit int64 = 10
  101. // Conn is a single connection to a Cassandra node. It can be used to execute
  102. // queries, but users are usually advised to use a more reliable, higher
  103. // level API.
  104. type Conn struct {
  105. conn net.Conn
  106. r *bufio.Reader
  107. timeout time.Duration
  108. cfg *ConnConfig
  109. headerBuf []byte
  110. streams *streams.IDGenerator
  111. mu sync.RWMutex
  112. calls map[int]*callReq
  113. errorHandler ConnErrorHandler
  114. compressor Compressor
  115. auth Authenticator
  116. addr string
  117. version uint8
  118. currentKeyspace string
  119. started bool
  120. session *Session
  121. closed int32
  122. quit chan struct{}
  123. timeouts int64
  124. }
  125. // Connect establishes a connection to a Cassandra node.
  126. func Connect(addr string, cfg *ConnConfig, errorHandler ConnErrorHandler, session *Session) (*Conn, error) {
  127. var (
  128. err error
  129. conn net.Conn
  130. )
  131. dialer := &net.Dialer{
  132. Timeout: cfg.Timeout,
  133. }
  134. if cfg.tlsConfig != nil {
  135. // the TLS config is safe to be reused by connections but it must not
  136. // be modified after being used.
  137. conn, err = tls.DialWithDialer(dialer, "tcp", addr, cfg.tlsConfig)
  138. } else {
  139. conn, err = dialer.Dial("tcp", addr)
  140. }
  141. if err != nil {
  142. return nil, err
  143. }
  144. // going to default to proto 2
  145. if cfg.ProtoVersion < protoVersion1 || cfg.ProtoVersion > protoVersion4 {
  146. log.Printf("unsupported protocol version: %d using 2\n", cfg.ProtoVersion)
  147. cfg.ProtoVersion = 2
  148. }
  149. headerSize := 8
  150. if cfg.ProtoVersion > protoVersion2 {
  151. headerSize = 9
  152. }
  153. c := &Conn{
  154. conn: conn,
  155. r: bufio.NewReader(conn),
  156. cfg: cfg,
  157. calls: make(map[int]*callReq),
  158. timeout: cfg.Timeout,
  159. version: uint8(cfg.ProtoVersion),
  160. addr: conn.RemoteAddr().String(),
  161. errorHandler: errorHandler,
  162. compressor: cfg.Compressor,
  163. auth: cfg.Authenticator,
  164. headerBuf: make([]byte, headerSize),
  165. quit: make(chan struct{}),
  166. session: session,
  167. streams: streams.New(cfg.ProtoVersion),
  168. }
  169. if cfg.Keepalive > 0 {
  170. c.setKeepalive(cfg.Keepalive)
  171. }
  172. go c.serve()
  173. if err := c.startup(); err != nil {
  174. conn.Close()
  175. return nil, err
  176. }
  177. c.started = true
  178. return c, nil
  179. }
  180. func (c *Conn) Write(p []byte) (int, error) {
  181. if c.timeout > 0 {
  182. c.conn.SetWriteDeadline(time.Now().Add(c.timeout))
  183. }
  184. return c.conn.Write(p)
  185. }
  186. func (c *Conn) Read(p []byte) (n int, err error) {
  187. const maxAttempts = 5
  188. for i := 0; i < maxAttempts; i++ {
  189. var nn int
  190. if c.timeout > 0 {
  191. c.conn.SetReadDeadline(time.Now().Add(c.timeout))
  192. }
  193. nn, err = io.ReadFull(c.r, p[n:])
  194. n += nn
  195. if err == nil {
  196. break
  197. }
  198. if verr, ok := err.(net.Error); !ok || !verr.Temporary() {
  199. break
  200. }
  201. }
  202. return
  203. }
  204. func (c *Conn) startup() error {
  205. m := map[string]string{
  206. "CQL_VERSION": c.cfg.CQLVersion,
  207. }
  208. if c.compressor != nil {
  209. m["COMPRESSION"] = c.compressor.Name()
  210. }
  211. framer, err := c.exec(&writeStartupFrame{opts: m}, nil)
  212. if err != nil {
  213. return err
  214. }
  215. frame, err := framer.parseFrame()
  216. if err != nil {
  217. return err
  218. }
  219. switch v := frame.(type) {
  220. case error:
  221. return v
  222. case *readyFrame:
  223. return nil
  224. case *authenticateFrame:
  225. return c.authenticateHandshake(v)
  226. default:
  227. return NewErrProtocol("Unknown type of response to startup frame: %s", v)
  228. }
  229. }
  230. func (c *Conn) authenticateHandshake(authFrame *authenticateFrame) error {
  231. if c.auth == nil {
  232. return fmt.Errorf("authentication required (using %q)", authFrame.class)
  233. }
  234. resp, challenger, err := c.auth.Challenge([]byte(authFrame.class))
  235. if err != nil {
  236. return err
  237. }
  238. req := &writeAuthResponseFrame{data: resp}
  239. for {
  240. framer, err := c.exec(req, nil)
  241. if err != nil {
  242. return err
  243. }
  244. frame, err := framer.parseFrame()
  245. if err != nil {
  246. return err
  247. }
  248. switch v := frame.(type) {
  249. case error:
  250. return v
  251. case *authSuccessFrame:
  252. if challenger != nil {
  253. return challenger.Success(v.data)
  254. }
  255. return nil
  256. case *authChallengeFrame:
  257. resp, challenger, err = challenger.Challenge(v.data)
  258. if err != nil {
  259. return err
  260. }
  261. req = &writeAuthResponseFrame{
  262. data: resp,
  263. }
  264. default:
  265. return fmt.Errorf("unknown frame response during authentication: %v", v)
  266. }
  267. framerPool.Put(framer)
  268. }
  269. }
  270. func (c *Conn) closeWithError(err error) {
  271. if !atomic.CompareAndSwapInt32(&c.closed, 0, 1) {
  272. return
  273. }
  274. if err != nil {
  275. // we should attempt to deliver the error back to the caller if it
  276. // exists
  277. c.mu.RLock()
  278. for _, req := range c.calls {
  279. // we need to send the error to all waiting queries, put the state
  280. // of this conn into not active so that it can not execute any queries.
  281. if err != nil {
  282. select {
  283. case req.resp <- err:
  284. default:
  285. }
  286. }
  287. }
  288. c.mu.RUnlock()
  289. }
  290. // if error was nil then unblock the quit channel
  291. close(c.quit)
  292. c.conn.Close()
  293. if c.started && err != nil {
  294. c.errorHandler.HandleError(c, err, true)
  295. }
  296. }
  297. func (c *Conn) Close() {
  298. c.closeWithError(nil)
  299. }
  300. // Serve starts the stream multiplexer for this connection, which is required
  301. // to execute any queries. This method runs as long as the connection is
  302. // open and is therefore usually called in a separate goroutine.
  303. func (c *Conn) serve() {
  304. var (
  305. err error
  306. )
  307. for {
  308. err = c.recv()
  309. if err != nil {
  310. break
  311. }
  312. }
  313. c.closeWithError(err)
  314. }
  315. func (c *Conn) discardFrame(head frameHeader) error {
  316. _, err := io.CopyN(ioutil.Discard, c, int64(head.length))
  317. if err != nil {
  318. return err
  319. }
  320. return nil
  321. }
  322. func (c *Conn) recv() error {
  323. // not safe for concurrent reads
  324. // read a full header, ignore timeouts, as this is being ran in a loop
  325. // TODO: TCP level deadlines? or just query level deadlines?
  326. if c.timeout > 0 {
  327. c.conn.SetReadDeadline(time.Time{})
  328. }
  329. // were just reading headers over and over and copy bodies
  330. head, err := readHeader(c.r, c.headerBuf)
  331. if err != nil {
  332. return err
  333. }
  334. if head.stream > c.streams.NumStreams {
  335. return fmt.Errorf("gocql: frame header stream is beyond call exepected bounds: %d", head.stream)
  336. } else if head.stream == -1 {
  337. // TODO: handle cassandra event frames, we shouldnt get any currently
  338. framer := newFramer(c, c, c.compressor, c.version)
  339. if err := framer.readFrame(&head); err != nil {
  340. return err
  341. }
  342. go c.session.handleEvent(framer)
  343. return nil
  344. } else if head.stream <= 0 {
  345. // reserved stream that we dont use, probably due to a protocol error
  346. // or a bug in Cassandra, this should be an error, parse it and return.
  347. framer := newFramer(c, c, c.compressor, c.version)
  348. if err := framer.readFrame(&head); err != nil {
  349. return err
  350. }
  351. defer framerPool.Put(framer)
  352. frame, err := framer.parseFrame()
  353. if err != nil {
  354. return err
  355. }
  356. switch v := frame.(type) {
  357. case error:
  358. return fmt.Errorf("gocql: error on stream %d: %v", head.stream, v)
  359. default:
  360. return fmt.Errorf("gocql: received frame on stream %d: %v", head.stream, frame)
  361. }
  362. }
  363. c.mu.RLock()
  364. call, ok := c.calls[head.stream]
  365. c.mu.RUnlock()
  366. if call == nil || call.framer == nil || !ok {
  367. log.Printf("gocql: received response for stream which has no handler: header=%v\n", head)
  368. return c.discardFrame(head)
  369. }
  370. err = call.framer.readFrame(&head)
  371. if err != nil {
  372. // only net errors should cause the connection to be closed. Though
  373. // cassandra returning corrupt frames will be returned here as well.
  374. if _, ok := err.(net.Error); ok {
  375. return err
  376. }
  377. }
  378. // we either, return a response to the caller, the caller timedout, or the
  379. // connection has closed. Either way we should never block indefinatly here
  380. select {
  381. case call.resp <- err:
  382. case <-call.timeout:
  383. c.releaseStream(head.stream)
  384. case <-c.quit:
  385. }
  386. return nil
  387. }
  388. type callReq struct {
  389. // could use a waitgroup but this allows us to do timeouts on the read/send
  390. resp chan error
  391. framer *framer
  392. timeout chan struct{} // indicates to recv() that a call has timedout
  393. streamID int // current stream in use
  394. }
  395. func (c *Conn) releaseStream(stream int) {
  396. c.mu.Lock()
  397. call := c.calls[stream]
  398. if call != nil && stream != call.streamID {
  399. panic(fmt.Sprintf("attempt to release streamID with ivalid stream: %d -> %+v\n", stream, call))
  400. } else if call == nil {
  401. panic(fmt.Sprintf("releasing a stream not in use: %d", stream))
  402. }
  403. delete(c.calls, stream)
  404. c.mu.Unlock()
  405. streamPool.Put(call)
  406. c.streams.Clear(stream)
  407. }
  408. func (c *Conn) handleTimeout() {
  409. if atomic.AddInt64(&c.timeouts, 1) > TimeoutLimit {
  410. c.closeWithError(ErrTooManyTimeouts)
  411. }
  412. }
  413. var (
  414. streamPool = sync.Pool{
  415. New: func() interface{} {
  416. return &callReq{
  417. resp: make(chan error),
  418. }
  419. },
  420. }
  421. )
  422. func (c *Conn) exec(req frameWriter, tracer Tracer) (*framer, error) {
  423. // TODO: move tracer onto conn
  424. stream, ok := c.streams.GetStream()
  425. if !ok {
  426. fmt.Println(c.streams)
  427. return nil, ErrNoStreams
  428. }
  429. // resp is basically a waiting semaphore protecting the framer
  430. framer := newFramer(c, c, c.compressor, c.version)
  431. c.mu.Lock()
  432. call := c.calls[stream]
  433. if call != nil {
  434. c.mu.Unlock()
  435. return nil, fmt.Errorf("attempting to use stream already in use: %d -> %d", stream, call.streamID)
  436. } else {
  437. call = streamPool.Get().(*callReq)
  438. }
  439. c.calls[stream] = call
  440. c.mu.Unlock()
  441. call.framer = framer
  442. call.timeout = make(chan struct{})
  443. call.streamID = stream
  444. if tracer != nil {
  445. framer.trace()
  446. }
  447. err := req.writeFrame(framer, stream)
  448. if err != nil {
  449. // I think this is the correct thing to do, im not entirely sure. It is not
  450. // ideal as readers might still get some data, but they probably wont.
  451. // Here we need to be careful as the stream is not available and if all
  452. // writes just timeout or fail then the pool might use this connection to
  453. // send a frame on, with all the streams used up and not returned.
  454. c.closeWithError(err)
  455. return nil, err
  456. }
  457. var timeoutCh <-chan time.Time
  458. if c.timeout > 0 {
  459. timeoutCh = time.After(c.timeout)
  460. }
  461. select {
  462. case err := <-call.resp:
  463. if err != nil {
  464. if !c.Closed() {
  465. // if the connection is closed then we cant release the stream,
  466. // this is because the request is still outstanding and we have
  467. // been handed another error from another stream which caused the
  468. // connection to close.
  469. c.releaseStream(stream)
  470. }
  471. return nil, err
  472. }
  473. case <-timeoutCh:
  474. close(call.timeout)
  475. c.handleTimeout()
  476. return nil, ErrTimeoutNoResponse
  477. case <-c.quit:
  478. return nil, ErrConnectionClosed
  479. }
  480. // dont release the stream if detect a timeout as another request can reuse
  481. // that stream and get a response for the old request, which we have no
  482. // easy way of detecting.
  483. //
  484. // Ensure that the stream is not released if there are potentially outstanding
  485. // requests on the stream to prevent nil pointer dereferences in recv().
  486. defer c.releaseStream(stream)
  487. if v := framer.header.version.version(); v != c.version {
  488. return nil, NewErrProtocol("unexpected protocol version in response: got %d expected %d", v, c.version)
  489. }
  490. return framer, nil
  491. }
  492. type preparedStatment struct {
  493. id []byte
  494. request preparedMetadata
  495. response resultMetadata
  496. }
  497. type inflightPrepare struct {
  498. wg sync.WaitGroup
  499. err error
  500. preparedStatment *preparedStatment
  501. }
  502. func (c *Conn) prepareStatement(stmt string, tracer Tracer) (*preparedStatment, error) {
  503. stmtCacheKey := c.session.stmtsLRU.keyFor(c.addr, c.currentKeyspace, stmt)
  504. flight, ok := c.session.stmtsLRU.execIfMissing(stmtCacheKey, func(lru *lru.Cache) *inflightPrepare {
  505. flight := new(inflightPrepare)
  506. flight.wg.Add(1)
  507. lru.Add(stmtCacheKey, flight)
  508. return flight
  509. })
  510. if ok {
  511. flight.wg.Wait()
  512. return flight.preparedStatment, flight.err
  513. }
  514. prep := &writePrepareFrame{
  515. statement: stmt,
  516. }
  517. framer, err := c.exec(prep, tracer)
  518. if err != nil {
  519. flight.err = err
  520. flight.wg.Done()
  521. return nil, err
  522. }
  523. frame, err := framer.parseFrame()
  524. if err != nil {
  525. flight.err = err
  526. flight.wg.Done()
  527. return nil, err
  528. }
  529. // TODO(zariel): tidy this up, simplify handling of frame parsing so its not duplicated
  530. // everytime we need to parse a frame.
  531. if len(framer.traceID) > 0 {
  532. tracer.Trace(framer.traceID)
  533. }
  534. switch x := frame.(type) {
  535. case *resultPreparedFrame:
  536. flight.preparedStatment = &preparedStatment{
  537. // defensivly copy as we will recycle the underlying buffer after we
  538. // return.
  539. id: copyBytes(x.preparedID),
  540. // the type info's should _not_ have a reference to the framers read buffer,
  541. // therefore we can just copy them directly.
  542. request: x.reqMeta,
  543. response: x.respMeta,
  544. }
  545. case error:
  546. flight.err = x
  547. default:
  548. flight.err = NewErrProtocol("Unknown type in response to prepare frame: %s", x)
  549. }
  550. flight.wg.Done()
  551. if flight.err != nil {
  552. c.session.stmtsLRU.remove(stmtCacheKey)
  553. }
  554. framerPool.Put(framer)
  555. return flight.preparedStatment, flight.err
  556. }
  557. func (c *Conn) executeQuery(qry *Query) *Iter {
  558. params := queryParams{
  559. consistency: qry.cons,
  560. }
  561. // frame checks that it is not 0
  562. params.serialConsistency = qry.serialCons
  563. params.defaultTimestamp = qry.defaultTimestamp
  564. if len(qry.pageState) > 0 {
  565. params.pagingState = qry.pageState
  566. }
  567. if qry.pageSize > 0 {
  568. params.pageSize = qry.pageSize
  569. }
  570. var (
  571. frame frameWriter
  572. info *preparedStatment
  573. )
  574. if qry.shouldPrepare() {
  575. // Prepare all DML queries. Other queries can not be prepared.
  576. var err error
  577. info, err = c.prepareStatement(qry.stmt, qry.trace)
  578. if err != nil {
  579. return &Iter{err: err}
  580. }
  581. var values []interface{}
  582. if qry.binding == nil {
  583. values = qry.values
  584. } else {
  585. values, err = qry.binding(&QueryInfo{
  586. Id: info.id,
  587. Args: info.request.columns,
  588. Rval: info.response.columns,
  589. PKeyColumns: info.request.pkeyColumns,
  590. })
  591. if err != nil {
  592. return &Iter{err: err}
  593. }
  594. }
  595. if len(values) != info.request.actualColCount {
  596. return &Iter{err: fmt.Errorf("gocql: expected %d values send got %d", info.request.actualColCount, len(values))}
  597. }
  598. params.values = make([]queryValues, len(values))
  599. for i := 0; i < len(values); i++ {
  600. val, err := Marshal(info.request.columns[i].TypeInfo, values[i])
  601. if err != nil {
  602. return &Iter{err: err}
  603. }
  604. v := &params.values[i]
  605. v.value = val
  606. // TODO: handle query binding names
  607. }
  608. params.skipMeta = !qry.isCAS
  609. frame = &writeExecuteFrame{
  610. preparedID: info.id,
  611. params: params,
  612. }
  613. } else {
  614. frame = &writeQueryFrame{
  615. statement: qry.stmt,
  616. params: params,
  617. }
  618. }
  619. framer, err := c.exec(frame, qry.trace)
  620. if err != nil {
  621. return &Iter{err: err}
  622. }
  623. resp, err := framer.parseFrame()
  624. if err != nil {
  625. return &Iter{err: err}
  626. }
  627. if len(framer.traceID) > 0 {
  628. qry.trace.Trace(framer.traceID)
  629. }
  630. switch x := resp.(type) {
  631. case *resultVoidFrame:
  632. return &Iter{framer: framer}
  633. case *resultRowsFrame:
  634. iter := &Iter{
  635. meta: x.meta,
  636. framer: framer,
  637. numRows: x.numRows,
  638. }
  639. if params.skipMeta {
  640. if info != nil {
  641. iter.meta = info.response
  642. iter.meta.pagingState = x.meta.pagingState
  643. } else {
  644. return &Iter{framer: framer, err: errors.New("gocql: did not receive metadata but prepared info is nil")}
  645. }
  646. } else {
  647. iter.meta = x.meta
  648. }
  649. if len(x.meta.pagingState) > 0 && !qry.disableAutoPage {
  650. iter.next = &nextIter{
  651. qry: *qry,
  652. pos: int((1 - qry.prefetch) * float64(x.numRows)),
  653. }
  654. iter.next.qry.pageState = copyBytes(x.meta.pagingState)
  655. if iter.next.pos < 1 {
  656. iter.next.pos = 1
  657. }
  658. }
  659. return iter
  660. case *resultKeyspaceFrame:
  661. return &Iter{framer: framer}
  662. case *schemaChangeKeyspace, *schemaChangeTable, *schemaChangeFunction:
  663. iter := &Iter{framer: framer}
  664. if err := c.awaitSchemaAgreement(); err != nil {
  665. // TODO: should have this behind a flag
  666. log.Println(err)
  667. }
  668. // dont return an error from this, might be a good idea to give a warning
  669. // though. The impact of this returning an error would be that the cluster
  670. // is not consistent with regards to its schema.
  671. return iter
  672. case *RequestErrUnprepared:
  673. stmtCacheKey := c.session.stmtsLRU.keyFor(c.addr, c.currentKeyspace, qry.stmt)
  674. if c.session.stmtsLRU.remove(stmtCacheKey) {
  675. return c.executeQuery(qry)
  676. }
  677. return &Iter{err: x, framer: framer}
  678. case error:
  679. return &Iter{err: x, framer: framer}
  680. default:
  681. return &Iter{
  682. err: NewErrProtocol("Unknown type in response to execute query (%T): %s", x, x),
  683. framer: framer,
  684. }
  685. }
  686. }
  687. func (c *Conn) Pick(qry *Query) *Conn {
  688. if c.Closed() {
  689. return nil
  690. }
  691. return c
  692. }
  693. func (c *Conn) Closed() bool {
  694. return atomic.LoadInt32(&c.closed) == 1
  695. }
  696. func (c *Conn) Address() string {
  697. return c.addr
  698. }
  699. func (c *Conn) AvailableStreams() int {
  700. return c.streams.Available()
  701. }
  702. func (c *Conn) UseKeyspace(keyspace string) error {
  703. q := &writeQueryFrame{statement: `USE "` + keyspace + `"`}
  704. q.params.consistency = Any
  705. framer, err := c.exec(q, nil)
  706. if err != nil {
  707. return err
  708. }
  709. resp, err := framer.parseFrame()
  710. if err != nil {
  711. return err
  712. }
  713. switch x := resp.(type) {
  714. case *resultKeyspaceFrame:
  715. case error:
  716. return x
  717. default:
  718. return NewErrProtocol("unknown frame in response to USE: %v", x)
  719. }
  720. c.currentKeyspace = keyspace
  721. return nil
  722. }
  723. func (c *Conn) executeBatch(batch *Batch) *Iter {
  724. if c.version == protoVersion1 {
  725. return &Iter{err: ErrUnsupported}
  726. }
  727. n := len(batch.Entries)
  728. req := &writeBatchFrame{
  729. typ: batch.Type,
  730. statements: make([]batchStatment, n),
  731. consistency: batch.Cons,
  732. serialConsistency: batch.serialCons,
  733. defaultTimestamp: batch.defaultTimestamp,
  734. }
  735. stmts := make(map[string]string, len(batch.Entries))
  736. for i := 0; i < n; i++ {
  737. entry := &batch.Entries[i]
  738. b := &req.statements[i]
  739. if len(entry.Args) > 0 || entry.binding != nil {
  740. info, err := c.prepareStatement(entry.Stmt, nil)
  741. if err != nil {
  742. return &Iter{err: err}
  743. }
  744. var values []interface{}
  745. if entry.binding == nil {
  746. values = entry.Args
  747. } else {
  748. values, err = entry.binding(&QueryInfo{
  749. Id: info.id,
  750. Args: info.request.columns,
  751. Rval: info.response.columns,
  752. PKeyColumns: info.request.pkeyColumns,
  753. })
  754. if err != nil {
  755. return &Iter{err: err}
  756. }
  757. }
  758. if len(values) != info.request.actualColCount {
  759. return &Iter{err: fmt.Errorf("gocql: batch statment %d expected %d values send got %d", i, info.request.actualColCount, len(values))}
  760. }
  761. b.preparedID = info.id
  762. stmts[string(info.id)] = entry.Stmt
  763. b.values = make([]queryValues, info.request.actualColCount)
  764. for j := 0; j < info.request.actualColCount; j++ {
  765. val, err := Marshal(info.request.columns[j].TypeInfo, values[j])
  766. if err != nil {
  767. return &Iter{err: err}
  768. }
  769. b.values[j].value = val
  770. // TODO: add names
  771. }
  772. } else {
  773. b.statement = entry.Stmt
  774. }
  775. }
  776. // TODO: should batch support tracing?
  777. framer, err := c.exec(req, nil)
  778. if err != nil {
  779. return &Iter{err: err}
  780. }
  781. resp, err := framer.parseFrame()
  782. if err != nil {
  783. return &Iter{err: err, framer: framer}
  784. }
  785. switch x := resp.(type) {
  786. case *resultVoidFrame:
  787. framerPool.Put(framer)
  788. return &Iter{}
  789. case *RequestErrUnprepared:
  790. stmt, found := stmts[string(x.StatementId)]
  791. if found {
  792. key := c.session.stmtsLRU.keyFor(c.addr, c.currentKeyspace, stmt)
  793. c.session.stmtsLRU.remove(key)
  794. }
  795. framerPool.Put(framer)
  796. if found {
  797. return c.executeBatch(batch)
  798. } else {
  799. return &Iter{err: err, framer: framer}
  800. }
  801. case *resultRowsFrame:
  802. iter := &Iter{
  803. meta: x.meta,
  804. framer: framer,
  805. numRows: x.numRows,
  806. }
  807. return iter
  808. case error:
  809. return &Iter{err: err, framer: framer}
  810. default:
  811. return &Iter{err: NewErrProtocol("Unknown type in response to batch statement: %s", x), framer: framer}
  812. }
  813. }
  814. func (c *Conn) setKeepalive(d time.Duration) error {
  815. if tc, ok := c.conn.(*net.TCPConn); ok {
  816. err := tc.SetKeepAlivePeriod(d)
  817. if err != nil {
  818. return err
  819. }
  820. return tc.SetKeepAlive(true)
  821. }
  822. return nil
  823. }
  824. func (c *Conn) query(statement string, values ...interface{}) (iter *Iter) {
  825. q := c.session.Query(statement, values...).Consistency(One)
  826. return c.executeQuery(q)
  827. }
  828. func (c *Conn) awaitSchemaAgreement() (err error) {
  829. const (
  830. peerSchemas = "SELECT schema_version FROM system.peers"
  831. localSchemas = "SELECT schema_version FROM system.local WHERE key='local'"
  832. )
  833. var versions map[string]struct{}
  834. endDeadline := time.Now().Add(c.session.cfg.MaxWaitSchemaAgreement)
  835. for time.Now().Before(endDeadline) {
  836. iter := c.query(peerSchemas)
  837. versions = make(map[string]struct{})
  838. var schemaVersion string
  839. for iter.Scan(&schemaVersion) {
  840. versions[schemaVersion] = struct{}{}
  841. schemaVersion = ""
  842. }
  843. if err = iter.Close(); err != nil {
  844. goto cont
  845. }
  846. iter = c.query(localSchemas)
  847. for iter.Scan(&schemaVersion) {
  848. versions[schemaVersion] = struct{}{}
  849. schemaVersion = ""
  850. }
  851. if err = iter.Close(); err != nil {
  852. goto cont
  853. }
  854. if len(versions) <= 1 {
  855. return nil
  856. }
  857. cont:
  858. time.Sleep(200 * time.Millisecond)
  859. }
  860. if err != nil {
  861. return
  862. }
  863. schemas := make([]string, 0, len(versions))
  864. for schema := range versions {
  865. schemas = append(schemas, schema)
  866. }
  867. // not exported
  868. return fmt.Errorf("gocql: cluster schema versions not consistent: %+v", schemas)
  869. }
  870. var (
  871. ErrQueryArgLength = errors.New("gocql: query argument length mismatch")
  872. ErrTimeoutNoResponse = errors.New("gocql: no response received from cassandra within timeout period")
  873. ErrTooManyTimeouts = errors.New("gocql: too many query timeouts on the connection")
  874. ErrConnectionClosed = errors.New("gocql: connection closed waiting for response")
  875. ErrNoStreams = errors.New("gocql: no streams available on connection")
  876. )