conn.go 24 KB

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