conn.go 15 KB

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