conn.go 22 KB

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