conn.go 27 KB

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