conn.go 28 KB

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