conn.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. // Copyright (c) 2012 The gocql Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gocql
  5. import (
  6. "bufio"
  7. "crypto/tls"
  8. "errors"
  9. "fmt"
  10. "io"
  11. "log"
  12. "net"
  13. "strconv"
  14. "strings"
  15. "sync"
  16. "time"
  17. )
  18. const (
  19. defaultFrameSize = 4096
  20. flagResponse = 0x80
  21. maskVersion = 0x7F
  22. )
  23. //JoinHostPort is a utility to return a address string that can be used
  24. //gocql.Conn to form a connection with a host.
  25. func JoinHostPort(addr string, port int) string {
  26. addr = strings.TrimSpace(addr)
  27. if _, _, err := net.SplitHostPort(addr); err != nil {
  28. addr = net.JoinHostPort(addr, strconv.Itoa(port))
  29. }
  30. return addr
  31. }
  32. type Authenticator interface {
  33. Challenge(req []byte) (resp []byte, auth Authenticator, err error)
  34. Success(data []byte) error
  35. }
  36. type PasswordAuthenticator struct {
  37. Username string
  38. Password string
  39. }
  40. func (p PasswordAuthenticator) Challenge(req []byte) ([]byte, Authenticator, error) {
  41. if string(req) != "org.apache.cassandra.auth.PasswordAuthenticator" {
  42. return nil, nil, fmt.Errorf("unexpected authenticator %q", req)
  43. }
  44. resp := make([]byte, 2+len(p.Username)+len(p.Password))
  45. resp[0] = 0
  46. copy(resp[1:], p.Username)
  47. resp[len(p.Username)+1] = 0
  48. copy(resp[2+len(p.Username):], p.Password)
  49. return resp, nil, nil
  50. }
  51. func (p PasswordAuthenticator) Success(data []byte) error {
  52. return nil
  53. }
  54. type SslOptions struct {
  55. CertPath string
  56. KeyPath string
  57. CaPath string //optional depending on server config
  58. // If you want to verify the hostname and server cert (like a wildcard for cass cluster) then you should turn this on
  59. // This option is basically the inverse of InSecureSkipVerify
  60. // See InSecureSkipVerify in http://golang.org/pkg/crypto/tls/ for more info
  61. EnableHostVerification bool
  62. }
  63. type ConnConfig struct {
  64. ProtoVersion int
  65. CQLVersion string
  66. Timeout time.Duration
  67. NumStreams int
  68. Compressor Compressor
  69. Authenticator Authenticator
  70. Keepalive time.Duration
  71. tlsConfig *tls.Config
  72. }
  73. // Conn is a single connection to a Cassandra node. It can be used to execute
  74. // queries, but users are usually advised to use a more reliable, higher
  75. // level API.
  76. type Conn struct {
  77. conn net.Conn
  78. r *bufio.Reader
  79. timeout time.Duration
  80. headerBuf []byte
  81. uniq chan int
  82. calls []callReq
  83. pool ConnectionPool
  84. compressor Compressor
  85. auth Authenticator
  86. addr string
  87. version uint8
  88. currentKeyspace string
  89. started bool
  90. closedMu sync.RWMutex
  91. isClosed bool
  92. }
  93. // Connect establishes a connection to a Cassandra node.
  94. // You must also call the Serve method before you can execute any queries.
  95. func Connect(addr string, cfg ConnConfig, pool ConnectionPool) (*Conn, error) {
  96. var (
  97. err error
  98. conn net.Conn
  99. )
  100. if cfg.tlsConfig != nil {
  101. // the TLS config is safe to be reused by connections but it must not
  102. // be modified after being used.
  103. if conn, err = tls.Dial("tcp", addr, cfg.tlsConfig); err != nil {
  104. return nil, err
  105. }
  106. } else if conn, err = net.DialTimeout("tcp", addr, cfg.Timeout); err != nil {
  107. return nil, err
  108. }
  109. // going to default to proto 2
  110. if cfg.ProtoVersion < protoVersion1 || cfg.ProtoVersion > protoVersion3 {
  111. log.Printf("unsupported protocol version: %d using 2\n", cfg.ProtoVersion)
  112. cfg.ProtoVersion = 2
  113. }
  114. headerSize := 8
  115. maxStreams := 128
  116. if cfg.ProtoVersion > protoVersion2 {
  117. maxStreams = 32768
  118. headerSize = 9
  119. }
  120. if cfg.NumStreams <= 0 || cfg.NumStreams > maxStreams {
  121. cfg.NumStreams = maxStreams
  122. }
  123. c := &Conn{
  124. conn: conn,
  125. r: bufio.NewReader(conn),
  126. uniq: make(chan int, cfg.NumStreams),
  127. calls: make([]callReq, cfg.NumStreams),
  128. timeout: cfg.Timeout,
  129. version: uint8(cfg.ProtoVersion),
  130. addr: conn.RemoteAddr().String(),
  131. pool: pool,
  132. compressor: cfg.Compressor,
  133. auth: cfg.Authenticator,
  134. headerBuf: make([]byte, headerSize),
  135. }
  136. if cfg.Keepalive > 0 {
  137. c.setKeepalive(cfg.Keepalive)
  138. }
  139. for i := 0; i < cfg.NumStreams; i++ {
  140. c.calls[i].resp = make(chan error, 1)
  141. c.uniq <- i
  142. }
  143. go c.serve()
  144. if err := c.startup(&cfg); err != nil {
  145. conn.Close()
  146. return nil, err
  147. }
  148. c.started = true
  149. return c, nil
  150. }
  151. func (c *Conn) Write(p []byte) (int, error) {
  152. if c.timeout > 0 {
  153. c.conn.SetWriteDeadline(time.Now().Add(c.timeout))
  154. }
  155. return c.conn.Write(p)
  156. }
  157. func (c *Conn) Read(p []byte) (n int, err error) {
  158. const maxAttempts = 5
  159. for i := 0; i < maxAttempts; i++ {
  160. var nn int
  161. if c.timeout > 0 {
  162. c.conn.SetReadDeadline(time.Now().Add(c.timeout))
  163. }
  164. nn, err = io.ReadFull(c.r, p[n:])
  165. n += nn
  166. if err == nil {
  167. break
  168. }
  169. if verr, ok := err.(net.Error); !ok || !verr.Temporary() {
  170. break
  171. }
  172. }
  173. return
  174. }
  175. func (c *Conn) startup(cfg *ConnConfig) error {
  176. m := map[string]string{
  177. "CQL_VERSION": cfg.CQLVersion,
  178. }
  179. if c.compressor != nil {
  180. m["COMPRESSION"] = c.compressor.Name()
  181. }
  182. frame, err := c.exec(&writeStartupFrame{opts: m}, nil)
  183. if err != nil {
  184. return err
  185. }
  186. switch v := frame.(type) {
  187. case error:
  188. return v
  189. case *readyFrame:
  190. return nil
  191. case *authenticateFrame:
  192. return c.authenticateHandshake(v)
  193. default:
  194. return NewErrProtocol("Unknown type of response to startup frame: %s", v)
  195. }
  196. }
  197. func (c *Conn) authenticateHandshake(authFrame *authenticateFrame) error {
  198. if c.auth == nil {
  199. return fmt.Errorf("authentication required (using %q)", authFrame.class)
  200. }
  201. resp, challenger, err := c.auth.Challenge([]byte(authFrame.class))
  202. if err != nil {
  203. return err
  204. }
  205. req := &writeAuthResponseFrame{data: resp}
  206. for {
  207. frame, err := c.exec(req, nil)
  208. if err != nil {
  209. return err
  210. }
  211. switch v := frame.(type) {
  212. case error:
  213. return v
  214. case *authSuccessFrame:
  215. if challenger != nil {
  216. return challenger.Success(v.data)
  217. }
  218. return nil
  219. case *authChallengeFrame:
  220. resp, challenger, err = challenger.Challenge(v.data)
  221. if err != nil {
  222. return err
  223. }
  224. req = &writeAuthResponseFrame{
  225. data: resp,
  226. }
  227. }
  228. }
  229. }
  230. // Serve starts the stream multiplexer for this connection, which is required
  231. // to execute any queries. This method runs as long as the connection is
  232. // open and is therefore usually called in a separate goroutine.
  233. func (c *Conn) serve() {
  234. var (
  235. err error
  236. )
  237. for {
  238. err = c.recv()
  239. if err != nil {
  240. break
  241. }
  242. }
  243. c.Close()
  244. for id := 0; id < len(c.calls); id++ {
  245. req := &c.calls[id]
  246. // we need to send the error to all waiting queries, put the state
  247. // of this conn into not active so that it can not execute any queries.
  248. select {
  249. case req.resp <- err:
  250. default:
  251. }
  252. close(req.resp)
  253. }
  254. if c.started {
  255. c.pool.HandleError(c, err, true)
  256. }
  257. }
  258. func (c *Conn) recv() error {
  259. // not safe for concurrent reads
  260. // read a full header, ignore timeouts, as this is being ran in a loop
  261. // TODO: TCP level deadlines? or just query level deadlines?
  262. if c.timeout > 0 {
  263. c.conn.SetReadDeadline(time.Time{})
  264. }
  265. // were just reading headers over and over and copy bodies
  266. head, err := readHeader(c.r, c.headerBuf)
  267. if err != nil {
  268. return err
  269. }
  270. call := &c.calls[head.stream]
  271. call.mu.Lock()
  272. err = call.framer.readFrame(&head)
  273. call.mu.Unlock()
  274. if err != nil {
  275. return err
  276. }
  277. // once we get to here we know that the caller must be waiting and that there
  278. // is no error.
  279. call.resp <- nil
  280. c.uniq <- head.stream
  281. return nil
  282. }
  283. type callReq struct {
  284. // could use a waitgroup but this allows us to do timeouts on the read/send
  285. resp chan error
  286. mu sync.Mutex
  287. framer *framer
  288. }
  289. func (c *Conn) exec(req frameWriter, tracer Tracer) (frame, error) {
  290. // TODO: move tracer onto conn
  291. stream := <-c.uniq
  292. call := &c.calls[stream]
  293. // resp is basically a waiting semaphore protecting the framer
  294. framer := newFramer(c, c, c.compressor, c.version)
  295. defer framerPool.Put(framer)
  296. call.framer = framer
  297. if tracer != nil {
  298. framer.trace()
  299. }
  300. // there is a race that we can read and write to the same buffer, I dont think
  301. // the data will actually corrupt but to be safe and appease the race detector gods,
  302. // guard it.
  303. // We could fix this by using seperate read and write buffers, which may end up
  304. // being faster and easier to reason about.
  305. call.mu.Lock()
  306. err := req.writeFrame(framer, stream)
  307. call.mu.Unlock()
  308. if err != nil {
  309. return nil, err
  310. }
  311. err = <-call.resp
  312. if err != nil {
  313. return nil, err
  314. }
  315. if v := framer.header.version.version(); v != c.version {
  316. return nil, NewErrProtocol("unexpected protocol version in response: got %d expected %d", v, c.version)
  317. }
  318. frame, err := framer.parseFrame()
  319. if err != nil {
  320. return nil, err
  321. }
  322. if len(framer.traceID) > 0 {
  323. tracer.Trace(framer.traceID)
  324. }
  325. return frame, nil
  326. }
  327. func (c *Conn) prepareStatement(stmt string, trace Tracer) (*resultPreparedFrame, error) {
  328. stmtsLRU.Lock()
  329. if stmtsLRU.lru == nil {
  330. initStmtsLRU(defaultMaxPreparedStmts)
  331. }
  332. stmtCacheKey := c.addr + c.currentKeyspace + stmt
  333. if val, ok := stmtsLRU.lru.Get(stmtCacheKey); ok {
  334. stmtsLRU.Unlock()
  335. flight := val.(*inflightPrepare)
  336. flight.wg.Wait()
  337. return flight.info, flight.err
  338. }
  339. flight := new(inflightPrepare)
  340. flight.wg.Add(1)
  341. stmtsLRU.lru.Add(stmtCacheKey, flight)
  342. stmtsLRU.Unlock()
  343. prep := &writePrepareFrame{
  344. statement: stmt,
  345. }
  346. resp, err := c.exec(prep, trace)
  347. if err != nil {
  348. flight.err = err
  349. flight.wg.Done()
  350. return nil, err
  351. }
  352. switch x := resp.(type) {
  353. case *resultPreparedFrame:
  354. flight.info = x
  355. case error:
  356. flight.err = x
  357. default:
  358. flight.err = NewErrProtocol("Unknown type in response to prepare frame: %s", x)
  359. }
  360. flight.wg.Done()
  361. if flight.err != nil {
  362. stmtsLRU.Lock()
  363. stmtsLRU.lru.Remove(stmtCacheKey)
  364. stmtsLRU.Unlock()
  365. }
  366. return flight.info, flight.err
  367. }
  368. func (c *Conn) executeQuery(qry *Query) *Iter {
  369. params := queryParams{
  370. consistency: qry.cons,
  371. }
  372. // TODO: Add DefaultTimestamp, SerialConsistency
  373. if len(qry.pageState) > 0 {
  374. params.pagingState = qry.pageState
  375. }
  376. if qry.pageSize > 0 {
  377. params.pageSize = qry.pageSize
  378. }
  379. var frame frameWriter
  380. if qry.shouldPrepare() {
  381. // Prepare all DML queries. Other queries can not be prepared.
  382. info, err := c.prepareStatement(qry.stmt, qry.trace)
  383. if err != nil {
  384. return &Iter{err: err}
  385. }
  386. var values []interface{}
  387. if qry.binding == nil {
  388. values = qry.values
  389. } else {
  390. binding := &QueryInfo{
  391. Id: info.preparedID,
  392. Args: info.reqMeta.columns,
  393. Rval: info.respMeta.columns,
  394. }
  395. values, err = qry.binding(binding)
  396. if err != nil {
  397. return &Iter{err: err}
  398. }
  399. }
  400. if len(values) != len(info.reqMeta.columns) {
  401. return &Iter{err: ErrQueryArgLength}
  402. }
  403. params.values = make([]queryValues, len(values))
  404. for i := 0; i < len(values); i++ {
  405. val, err := Marshal(info.reqMeta.columns[i].TypeInfo, values[i])
  406. if err != nil {
  407. return &Iter{err: err}
  408. }
  409. v := &params.values[i]
  410. v.value = val
  411. // TODO: handle query binding names
  412. }
  413. frame = &writeExecuteFrame{
  414. preparedID: info.preparedID,
  415. params: params,
  416. }
  417. } else {
  418. frame = &writeQueryFrame{
  419. statement: qry.stmt,
  420. params: params,
  421. }
  422. }
  423. resp, err := c.exec(frame, qry.trace)
  424. if err != nil {
  425. return &Iter{err: err}
  426. }
  427. switch x := resp.(type) {
  428. case *resultVoidFrame:
  429. return &Iter{}
  430. case *resultRowsFrame:
  431. iter := &Iter{
  432. columns: x.meta.columns,
  433. rows: x.rows,
  434. }
  435. if len(x.meta.pagingState) > 0 {
  436. iter.next = &nextIter{
  437. qry: *qry,
  438. pos: int((1 - qry.prefetch) * float64(len(iter.rows))),
  439. }
  440. iter.next.qry.pageState = x.meta.pagingState
  441. if iter.next.pos < 1 {
  442. iter.next.pos = 1
  443. }
  444. }
  445. return iter
  446. case *resultKeyspaceFrame, *resultSchemaChangeFrame:
  447. return &Iter{}
  448. case RequestErrUnprepared:
  449. stmtsLRU.Lock()
  450. stmtCacheKey := c.addr + c.currentKeyspace + qry.stmt
  451. if _, ok := stmtsLRU.lru.Get(stmtCacheKey); ok {
  452. stmtsLRU.lru.Remove(stmtCacheKey)
  453. stmtsLRU.Unlock()
  454. return c.executeQuery(qry)
  455. }
  456. stmtsLRU.Unlock()
  457. return &Iter{err: x}
  458. case error:
  459. return &Iter{err: x}
  460. default:
  461. return &Iter{err: NewErrProtocol("Unknown type in response to execute query: %s", x)}
  462. }
  463. }
  464. func (c *Conn) Pick(qry *Query) *Conn {
  465. if c.Closed() {
  466. return nil
  467. }
  468. return c
  469. }
  470. func (c *Conn) Closed() bool {
  471. c.closedMu.RLock()
  472. closed := c.isClosed
  473. c.closedMu.RUnlock()
  474. return closed
  475. }
  476. func (c *Conn) Close() {
  477. c.closedMu.Lock()
  478. if c.isClosed {
  479. c.closedMu.Unlock()
  480. return
  481. }
  482. c.isClosed = true
  483. c.closedMu.Unlock()
  484. c.conn.Close()
  485. }
  486. func (c *Conn) Address() string {
  487. return c.addr
  488. }
  489. func (c *Conn) AvailableStreams() int {
  490. return len(c.uniq)
  491. }
  492. func (c *Conn) UseKeyspace(keyspace string) error {
  493. q := &writeQueryFrame{statement: `USE "` + keyspace + `"`}
  494. q.params.consistency = Any
  495. resp, err := c.exec(q, nil)
  496. if err != nil {
  497. return err
  498. }
  499. switch x := resp.(type) {
  500. case *resultKeyspaceFrame:
  501. case error:
  502. return x
  503. default:
  504. return NewErrProtocol("Unknown type in response to USE: %s", x)
  505. }
  506. c.currentKeyspace = keyspace
  507. return nil
  508. }
  509. func (c *Conn) executeBatch(batch *Batch) error {
  510. if c.version == protoVersion1 {
  511. return ErrUnsupported
  512. }
  513. n := len(batch.Entries)
  514. req := &writeBatchFrame{
  515. typ: batch.Type,
  516. statements: make([]batchStatment, n),
  517. consistency: batch.Cons,
  518. }
  519. stmts := make(map[string]string)
  520. for i := 0; i < n; i++ {
  521. entry := &batch.Entries[i]
  522. b := &req.statements[i]
  523. if len(entry.Args) > 0 || entry.binding != nil {
  524. info, err := c.prepareStatement(entry.Stmt, nil)
  525. if err != nil {
  526. return err
  527. }
  528. var args []interface{}
  529. if entry.binding == nil {
  530. args = entry.Args
  531. } else {
  532. binding := &QueryInfo{
  533. Id: info.preparedID,
  534. Args: info.reqMeta.columns,
  535. Rval: info.respMeta.columns,
  536. }
  537. args, err = entry.binding(binding)
  538. if err != nil {
  539. return err
  540. }
  541. }
  542. if len(args) != len(info.reqMeta.columns) {
  543. return ErrQueryArgLength
  544. }
  545. b.preparedID = info.preparedID
  546. stmts[string(info.preparedID)] = entry.Stmt
  547. b.values = make([]queryValues, len(info.reqMeta.columns))
  548. for j := 0; j < len(info.reqMeta.columns); j++ {
  549. val, err := Marshal(info.reqMeta.columns[j].TypeInfo, args[j])
  550. if err != nil {
  551. return err
  552. }
  553. b.values[j].value = val
  554. // TODO: add names
  555. }
  556. } else {
  557. b.statement = entry.Stmt
  558. }
  559. }
  560. // TODO: should batch support tracing?
  561. resp, err := c.exec(req, nil)
  562. if err != nil {
  563. return err
  564. }
  565. switch x := resp.(type) {
  566. case *resultVoidFrame:
  567. return nil
  568. case RequestErrUnprepared:
  569. stmt, found := stmts[string(x.StatementId)]
  570. if found {
  571. stmtsLRU.Lock()
  572. stmtsLRU.lru.Remove(c.addr + c.currentKeyspace + stmt)
  573. stmtsLRU.Unlock()
  574. }
  575. if found {
  576. return c.executeBatch(batch)
  577. } else {
  578. return x
  579. }
  580. case error:
  581. return x
  582. default:
  583. return NewErrProtocol("Unknown type in response to batch statement: %s", x)
  584. }
  585. }
  586. func (c *Conn) setKeepalive(d time.Duration) error {
  587. if tc, ok := c.conn.(*net.TCPConn); ok {
  588. err := tc.SetKeepAlivePeriod(d)
  589. if err != nil {
  590. return err
  591. }
  592. return tc.SetKeepAlive(true)
  593. }
  594. return nil
  595. }
  596. type inflightPrepare struct {
  597. info *resultPreparedFrame
  598. err error
  599. wg sync.WaitGroup
  600. }
  601. var (
  602. ErrQueryArgLength = errors.New("query argument length mismatch")
  603. )