conn.go 26 KB

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