conn.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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) startup(cfg *ConnConfig) error {
  152. m := map[string]string{
  153. "CQL_VERSION": cfg.CQLVersion,
  154. }
  155. if c.compressor != nil {
  156. m["COMPRESSION"] = c.compressor.Name()
  157. }
  158. frame, err := c.exec(&writeStartupFrame{opts: m}, nil)
  159. if err != nil {
  160. return err
  161. }
  162. switch v := frame.(type) {
  163. case error:
  164. return v
  165. case *readyFrame:
  166. return nil
  167. case *authenticateFrame:
  168. return c.authenticateHandshake(v)
  169. default:
  170. return NewErrProtocol("Unknown type of response to startup frame: %s", v)
  171. }
  172. }
  173. func (c *Conn) authenticateHandshake(authFrame *authenticateFrame) error {
  174. if c.auth == nil {
  175. return fmt.Errorf("authentication required (using %q)", authFrame.class)
  176. }
  177. resp, challenger, err := c.auth.Challenge([]byte(authFrame.class))
  178. if err != nil {
  179. return err
  180. }
  181. req := &writeAuthResponseFrame{data: resp}
  182. for {
  183. frame, err := c.exec(req, nil)
  184. if err != nil {
  185. return err
  186. }
  187. switch v := frame.(type) {
  188. case error:
  189. return v
  190. case authSuccessFrame:
  191. if challenger != nil {
  192. return challenger.Success(v.data)
  193. }
  194. return nil
  195. case authChallengeFrame:
  196. resp, challenger, err = challenger.Challenge(v.data)
  197. if err != nil {
  198. return err
  199. }
  200. req = &writeAuthResponseFrame{
  201. data: resp,
  202. }
  203. }
  204. }
  205. }
  206. func (c *Conn) exec(req frameWriter, tracer Tracer) (frame, error) {
  207. // TODO: move tracer onto conn
  208. stream := <-c.uniq
  209. call := &c.calls[stream]
  210. atomic.StoreInt32(&call.active, 1)
  211. defer atomic.StoreInt32(&call.active, 0)
  212. call.resp = make(chan callResp, 1)
  213. // log.Printf("%v: OUT stream=%d (%T) req=%v\n", c.conn.LocalAddr(), stream, req, req)
  214. framer := newFramer(c, c, c.compressor, c.version)
  215. // unfortunatly this part of the protocol leaks in conn, somehow move this
  216. // out into framer. One way to do it would be to use the same framer to send
  217. // and recv for a single stream.
  218. if tracer != nil {
  219. framer.flags |= flagTracing
  220. }
  221. err := req.writeFrame(framer, stream)
  222. framerPool.Put(framer)
  223. if err != nil {
  224. return nil, err
  225. }
  226. resp := <-call.resp
  227. call.resp = nil
  228. if resp.err != nil {
  229. return nil, resp.err
  230. }
  231. defer framerPool.Put(resp.framer)
  232. frame, err := resp.framer.parseFrame()
  233. if err != nil {
  234. return nil, err
  235. }
  236. if len(framer.traceID) > 0 {
  237. tracer.Trace(framer.traceID)
  238. }
  239. // log.Printf("%v: IN stream=%d (%T) resp=%v\n", c.conn.LocalAddr(), stream, frame, frame)
  240. return frame, nil
  241. }
  242. // Serve starts the stream multiplexer for this connection, which is required
  243. // to execute any queries. This method runs as long as the connection is
  244. // open and is therefore usually called in a separate goroutine.
  245. func (c *Conn) serve() {
  246. var (
  247. err error
  248. framer *framer
  249. )
  250. for {
  251. framer, err = c.recv()
  252. if err != nil {
  253. break
  254. }
  255. c.dispatch(framer)
  256. }
  257. c.Close()
  258. for id := 0; id < len(c.calls); id++ {
  259. req := &c.calls[id]
  260. if atomic.CompareAndSwapInt32(&req.active, 1, 0) {
  261. req.resp <- callResp{nil, err}
  262. close(req.resp)
  263. }
  264. }
  265. if c.started {
  266. c.pool.HandleError(c, err, true)
  267. }
  268. }
  269. func (c *Conn) Write(p []byte) (int, error) {
  270. c.conn.SetWriteDeadline(time.Now().Add(c.timeout))
  271. return c.conn.Write(p)
  272. }
  273. func (c *Conn) Read(p []byte) (int, error) {
  274. return c.r.Read(p)
  275. }
  276. func (c *Conn) recv() (*framer, error) {
  277. // read a full header, ignore timeouts, as this is being ran in a loop
  278. // TODO: TCP level deadlines? or just query level dealines?
  279. // were just reading headers over and over and copy bodies
  280. head, err := readHeader(c.r, c.headerBuf)
  281. if err != nil {
  282. return nil, err
  283. }
  284. // log.Printf("header=%v\n", head)
  285. if head.version.version() != c.version {
  286. return nil, NewErrProtocol("unexpected protocol version in response: got %d expected %d", head.version.version(), c.version)
  287. }
  288. framer := newFramer(c.r, c, c.compressor, c.version)
  289. if err := framer.readFrame(&head); err != nil {
  290. return nil, err
  291. }
  292. return framer, nil
  293. }
  294. func (c *Conn) dispatch(f *framer) {
  295. id := f.header.stream
  296. if id >= len(c.calls) {
  297. return
  298. }
  299. // TODO: replace this with a sparse map
  300. call := &c.calls[id]
  301. call.resp <- callResp{f, nil}
  302. atomic.AddInt32(&c.nwait, -1)
  303. c.uniq <- id
  304. }
  305. func (c *Conn) prepareStatement(stmt string, trace Tracer) (*resultPreparedFrame, error) {
  306. stmtsLRU.Lock()
  307. if stmtsLRU.lru == nil {
  308. initStmtsLRU(defaultMaxPreparedStmts)
  309. }
  310. stmtCacheKey := c.addr + c.currentKeyspace + stmt
  311. if val, ok := stmtsLRU.lru.Get(stmtCacheKey); ok {
  312. stmtsLRU.Unlock()
  313. flight := val.(*inflightPrepare)
  314. flight.wg.Wait()
  315. return flight.info, flight.err
  316. }
  317. flight := new(inflightPrepare)
  318. flight.wg.Add(1)
  319. stmtsLRU.lru.Add(stmtCacheKey, flight)
  320. stmtsLRU.Unlock()
  321. prep := &writePrepareFrame{
  322. statement: stmt,
  323. }
  324. resp, err := c.exec(prep, trace)
  325. if err != nil {
  326. flight.err = err
  327. flight.wg.Done()
  328. return nil, err
  329. }
  330. switch x := resp.(type) {
  331. case *resultPreparedFrame:
  332. // log.Printf("prepared %q => %x\n", stmt, x.preparedID)
  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. // log.Printf("%+#v\n", qry)
  359. var frame frameWriter
  360. if qry.shouldPrepare() {
  361. // Prepare all DML queries. Other queries can not be prepared.
  362. info, err := c.prepareStatement(qry.stmt, qry.trace)
  363. if err != nil {
  364. return &Iter{err: err}
  365. }
  366. var values []interface{}
  367. if qry.binding == nil {
  368. values = qry.values
  369. } else {
  370. binding := &QueryInfo{
  371. Id: info.preparedID,
  372. Args: info.reqMeta.columns,
  373. Rval: info.respMeta.columns,
  374. }
  375. values, err = qry.binding(binding)
  376. if err != nil {
  377. return &Iter{err: err}
  378. }
  379. }
  380. if len(values) != len(info.reqMeta.columns) {
  381. return &Iter{err: ErrQueryArgLength}
  382. }
  383. params.values = make([]queryValues, len(values))
  384. for i := 0; i < len(values); i++ {
  385. val, err := Marshal(info.reqMeta.columns[i].TypeInfo, values[i])
  386. if err != nil {
  387. return &Iter{err: err}
  388. }
  389. v := &params.values[i]
  390. v.value = val
  391. // TODO: handle query binding names
  392. }
  393. frame = &writeExecuteFrame{
  394. preparedID: info.preparedID,
  395. params: params,
  396. }
  397. } else {
  398. frame = &writeQueryFrame{
  399. statement: qry.stmt,
  400. params: params,
  401. }
  402. }
  403. resp, err := c.exec(frame, qry.trace)
  404. if err != nil {
  405. return &Iter{err: err}
  406. }
  407. // log.Printf("resp=%T\n", resp)
  408. switch x := resp.(type) {
  409. case *resultVoidFrame:
  410. return &Iter{}
  411. case *resultRowsFrame:
  412. iter := &Iter{
  413. columns: x.meta.columns,
  414. rows: x.rows,
  415. }
  416. // log.Printf("result meta=%v\n", x.meta)
  417. if len(x.meta.pagingState) > 0 {
  418. iter.next = &nextIter{
  419. qry: *qry,
  420. pos: int((1 - qry.prefetch) * float64(len(iter.rows))),
  421. }
  422. iter.next.qry.pageState = x.meta.pagingState
  423. if iter.next.pos < 1 {
  424. iter.next.pos = 1
  425. }
  426. }
  427. return iter
  428. case *resultKeyspaceFrame, *resultSchemaChangeFrame:
  429. return &Iter{}
  430. case RequestErrUnprepared:
  431. stmtsLRU.Lock()
  432. stmtCacheKey := c.addr + c.currentKeyspace + qry.stmt
  433. if _, ok := stmtsLRU.lru.Get(stmtCacheKey); ok {
  434. stmtsLRU.lru.Remove(stmtCacheKey)
  435. stmtsLRU.Unlock()
  436. return c.executeQuery(qry)
  437. }
  438. stmtsLRU.Unlock()
  439. panic(x)
  440. return &Iter{err: x}
  441. case error:
  442. return &Iter{err: x}
  443. default:
  444. return &Iter{err: NewErrProtocol("Unknown type in response to execute query: %s", x)}
  445. }
  446. }
  447. func (c *Conn) Pick(qry *Query) *Conn {
  448. if c.Closed() {
  449. return nil
  450. }
  451. return c
  452. }
  453. func (c *Conn) Closed() bool {
  454. c.closedMu.RLock()
  455. closed := c.isClosed
  456. c.closedMu.RUnlock()
  457. return closed
  458. }
  459. func (c *Conn) Close() {
  460. c.closedMu.Lock()
  461. if c.isClosed {
  462. c.closedMu.Unlock()
  463. return
  464. }
  465. c.isClosed = true
  466. c.closedMu.Unlock()
  467. c.conn.Close()
  468. }
  469. func (c *Conn) Address() string {
  470. return c.addr
  471. }
  472. func (c *Conn) AvailableStreams() int {
  473. return len(c.uniq)
  474. }
  475. func (c *Conn) UseKeyspace(keyspace string) error {
  476. q := &writeQueryFrame{statement: `USE "` + keyspace + `"`}
  477. q.params.consistency = Any
  478. resp, err := c.exec(q, nil)
  479. if err != nil {
  480. return err
  481. }
  482. switch x := resp.(type) {
  483. case *resultKeyspaceFrame:
  484. case error:
  485. return x
  486. default:
  487. return NewErrProtocol("Unknown type in response to USE: %s", x)
  488. }
  489. c.currentKeyspace = keyspace
  490. return nil
  491. }
  492. func (c *Conn) executeBatch(batch *Batch) error {
  493. if c.version == protoVersion1 {
  494. return ErrUnsupported
  495. }
  496. n := len(batch.Entries)
  497. req := &writeBatchFrame{
  498. typ: batch.Type,
  499. statements: make([]batchStatment, n),
  500. consistency: batch.Cons,
  501. }
  502. stmts := make(map[string]string)
  503. for i := 0; i < n; i++ {
  504. entry := &batch.Entries[i]
  505. b := &req.statements[i]
  506. if len(entry.Args) > 0 || entry.binding != nil {
  507. info, err := c.prepareStatement(entry.Stmt, nil)
  508. if err != nil {
  509. return err
  510. }
  511. var args []interface{}
  512. if entry.binding == nil {
  513. args = entry.Args
  514. } else {
  515. binding := &QueryInfo{
  516. Id: info.preparedID,
  517. Args: info.reqMeta.columns,
  518. Rval: info.respMeta.columns,
  519. }
  520. args, err = entry.binding(binding)
  521. if err != nil {
  522. return err
  523. }
  524. }
  525. if len(args) != len(info.reqMeta.columns) {
  526. return ErrQueryArgLength
  527. }
  528. b.preparedID = info.preparedID
  529. stmts[string(info.preparedID)] = entry.Stmt
  530. b.values = make([]queryValues, len(info.reqMeta.columns))
  531. for j := 0; j < len(info.reqMeta.columns); j++ {
  532. val, err := Marshal(info.reqMeta.columns[j].TypeInfo, args[j])
  533. if err != nil {
  534. return err
  535. }
  536. b.values[j].value = val
  537. // TODO: add names
  538. }
  539. } else {
  540. b.statement = entry.Stmt
  541. }
  542. }
  543. // TODO: should batch support tracing?
  544. resp, err := c.exec(req, nil)
  545. if err != nil {
  546. return err
  547. }
  548. switch x := resp.(type) {
  549. case *resultVoidFrame:
  550. return nil
  551. case RequestErrUnprepared:
  552. stmt, found := stmts[string(x.StatementId)]
  553. if found {
  554. stmtsLRU.Lock()
  555. stmtsLRU.lru.Remove(c.addr + c.currentKeyspace + stmt)
  556. stmtsLRU.Unlock()
  557. }
  558. if found {
  559. return c.executeBatch(batch)
  560. } else {
  561. return x
  562. }
  563. case error:
  564. return x
  565. default:
  566. return NewErrProtocol("Unknown type in response to batch statement: %s", x)
  567. }
  568. }
  569. func (c *Conn) setKeepalive(d time.Duration) error {
  570. if tc, ok := c.conn.(*net.TCPConn); ok {
  571. err := tc.SetKeepAlivePeriod(d)
  572. if err != nil {
  573. return err
  574. }
  575. return tc.SetKeepAlive(true)
  576. }
  577. return nil
  578. }
  579. type callReq struct {
  580. active int32
  581. resp chan callResp
  582. }
  583. type callResp struct {
  584. framer *framer
  585. err error
  586. }
  587. type inflightPrepare struct {
  588. info *resultPreparedFrame
  589. err error
  590. wg sync.WaitGroup
  591. }
  592. var (
  593. ErrQueryArgLength = errors.New("query argument length mismatch")
  594. )