conn.go 31 KB

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