conn.go 23 KB

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