conn.go 27 KB

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