conn.go 14 KB

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