conn.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. "fmt"
  8. "net"
  9. "sync"
  10. "sync/atomic"
  11. "time"
  12. "code.google.com/p/snappy-go/snappy"
  13. )
  14. const defaultFrameSize = 4096
  15. const flagResponse = 0x80
  16. const maskVersion = 0x7F
  17. type Cluster interface {
  18. HandleError(conn *Conn, err error, closed bool)
  19. HandleKeyspace(conn *Conn, keyspace string)
  20. }
  21. type Authenticator interface {
  22. Challenge(req []byte) (resp []byte, auth Authenticator, err error)
  23. Success(data []byte) error
  24. }
  25. type PasswordAuthenticator struct {
  26. Username string
  27. Password string
  28. }
  29. func (p PasswordAuthenticator) Challenge(req []byte) ([]byte, Authenticator, error) {
  30. if string(req) != "org.apache.cassandra.auth.PasswordAuthenticator" {
  31. return nil, nil, fmt.Errorf("unexpected authenticator %q", req)
  32. }
  33. resp := make([]byte, 2+len(p.Username)+len(p.Password))
  34. resp[0] = 0
  35. copy(resp[1:], p.Username)
  36. resp[len(p.Username)+1] = 0
  37. copy(resp[2+len(p.Username):], p.Password)
  38. return resp, nil, nil
  39. }
  40. func (p PasswordAuthenticator) Success(data []byte) error {
  41. return nil
  42. }
  43. type ConnConfig struct {
  44. ProtoVersion int
  45. CQLVersion string
  46. Timeout time.Duration
  47. NumStreams int
  48. Compressor Compressor
  49. Authenticator Authenticator
  50. }
  51. // Conn is a single connection to a Cassandra node. It can be used to execute
  52. // queries, but users are usually advised to use a more reliable, higher
  53. // level API.
  54. type Conn struct {
  55. conn net.Conn
  56. r *bufio.Reader
  57. timeout time.Duration
  58. uniq chan uint8
  59. calls []callReq
  60. nwait int32
  61. prepMu sync.Mutex
  62. prep map[string]*inflightPrepare
  63. cluster Cluster
  64. compressor Compressor
  65. auth Authenticator
  66. addr string
  67. version uint8
  68. }
  69. // Connect establishes a connection to a Cassandra node.
  70. // You must also call the Serve method before you can execute any queries.
  71. func Connect(addr string, cfg ConnConfig, cluster Cluster) (*Conn, error) {
  72. conn, err := net.DialTimeout("tcp", addr, cfg.Timeout)
  73. if err != nil {
  74. return nil, err
  75. }
  76. if cfg.NumStreams <= 0 || cfg.NumStreams > 128 {
  77. cfg.NumStreams = 128
  78. }
  79. if cfg.ProtoVersion != 1 && cfg.ProtoVersion != 2 {
  80. cfg.ProtoVersion = 2
  81. }
  82. c := &Conn{
  83. conn: conn,
  84. r: bufio.NewReader(conn),
  85. uniq: make(chan uint8, cfg.NumStreams),
  86. calls: make([]callReq, cfg.NumStreams),
  87. prep: make(map[string]*inflightPrepare),
  88. timeout: cfg.Timeout,
  89. version: uint8(cfg.ProtoVersion),
  90. addr: conn.RemoteAddr().String(),
  91. cluster: cluster,
  92. compressor: cfg.Compressor,
  93. auth: cfg.Authenticator,
  94. }
  95. for i := 0; i < cap(c.uniq); i++ {
  96. c.uniq <- uint8(i)
  97. }
  98. if err := c.startup(&cfg); err != nil {
  99. return nil, err
  100. }
  101. go c.serve()
  102. return c, nil
  103. }
  104. func (c *Conn) startup(cfg *ConnConfig) error {
  105. compression := ""
  106. if c.compressor != nil {
  107. compression = c.compressor.Name()
  108. }
  109. var req operation = &startupFrame{
  110. CQLVersion: cfg.CQLVersion,
  111. Compression: compression,
  112. }
  113. var challenger Authenticator
  114. for {
  115. resp, err := c.execSimple(req)
  116. if err != nil {
  117. return err
  118. }
  119. switch x := resp.(type) {
  120. case readyFrame:
  121. return nil
  122. case error:
  123. return x
  124. case authenticateFrame:
  125. if c.auth == nil {
  126. return fmt.Errorf("authentication required (using %q)", x.Authenticator)
  127. }
  128. var resp []byte
  129. resp, challenger, err = c.auth.Challenge([]byte(x.Authenticator))
  130. if err != nil {
  131. return err
  132. }
  133. req = &authResponseFrame{resp}
  134. case authChallengeFrame:
  135. if challenger == nil {
  136. return fmt.Errorf("authentication error (invalid challenge)")
  137. }
  138. var resp []byte
  139. resp, challenger, err = challenger.Challenge(x.Data)
  140. if err != nil {
  141. return err
  142. }
  143. req = &authResponseFrame{resp}
  144. case authSuccessFrame:
  145. if challenger != nil {
  146. return challenger.Success(x.Data)
  147. }
  148. return nil
  149. default:
  150. return ErrProtocol
  151. }
  152. }
  153. }
  154. // Serve starts the stream multiplexer for this connection, which is required
  155. // to execute any queries. This method runs as long as the connection is
  156. // open and is therefore usually called in a separate goroutine.
  157. func (c *Conn) serve() {
  158. for {
  159. resp, err := c.recv()
  160. if err != nil {
  161. break
  162. }
  163. c.dispatch(resp)
  164. }
  165. c.conn.Close()
  166. for id := 0; id < len(c.calls); id++ {
  167. req := &c.calls[id]
  168. if atomic.LoadInt32(&req.active) == 1 {
  169. req.resp <- callResp{nil, ErrProtocol}
  170. }
  171. }
  172. c.cluster.HandleError(c, ErrProtocol, true)
  173. }
  174. func (c *Conn) recv() (frame, error) {
  175. resp := make(frame, headerSize, headerSize+512)
  176. c.conn.SetReadDeadline(time.Now().Add(c.timeout))
  177. n, last, pinged := 0, 0, false
  178. for n < len(resp) {
  179. nn, err := c.r.Read(resp[n:])
  180. n += nn
  181. if err != nil {
  182. if nerr, ok := err.(net.Error); ok && nerr.Timeout() {
  183. if n > last {
  184. // we hit the deadline but we made progress.
  185. // simply extend the deadline
  186. c.conn.SetReadDeadline(time.Now().Add(c.timeout))
  187. last = n
  188. } else if n == 0 && !pinged {
  189. c.conn.SetReadDeadline(time.Now().Add(c.timeout))
  190. if atomic.LoadInt32(&c.nwait) > 0 {
  191. go c.ping()
  192. pinged = true
  193. }
  194. } else {
  195. return nil, err
  196. }
  197. } else {
  198. return nil, err
  199. }
  200. }
  201. if n == headerSize && len(resp) == headerSize {
  202. if resp[0] != c.version|flagResponse {
  203. return nil, ErrProtocol
  204. }
  205. resp.grow(resp.Length())
  206. }
  207. }
  208. return resp, nil
  209. }
  210. func (c *Conn) execSimple(op operation) (interface{}, error) {
  211. f, err := op.encodeFrame(c.version, nil)
  212. f.setLength(len(f) - headerSize)
  213. if _, err := c.conn.Write([]byte(f)); err != nil {
  214. c.conn.Close()
  215. return nil, err
  216. }
  217. if f, err = c.recv(); err != nil {
  218. return nil, err
  219. }
  220. return c.decodeFrame(f, nil)
  221. }
  222. func (c *Conn) exec(op operation, trace Tracer) (interface{}, error) {
  223. req, err := op.encodeFrame(c.version, nil)
  224. if err != nil {
  225. return nil, err
  226. }
  227. if trace != nil {
  228. req[1] |= flagTrace
  229. }
  230. if len(req) > headerSize && c.compressor != nil {
  231. body, err := c.compressor.Encode([]byte(req[headerSize:]))
  232. if err != nil {
  233. return nil, err
  234. }
  235. req = append(req[:headerSize], frame(body)...)
  236. req[1] |= flagCompress
  237. }
  238. req.setLength(len(req) - headerSize)
  239. id := <-c.uniq
  240. req[2] = id
  241. call := &c.calls[id]
  242. call.resp = make(chan callResp, 1)
  243. atomic.AddInt32(&c.nwait, 1)
  244. atomic.StoreInt32(&call.active, 1)
  245. if n, err := c.conn.Write(req); err != nil {
  246. c.conn.Close()
  247. if n > 0 {
  248. return nil, ErrProtocol
  249. }
  250. return nil, ErrUnavailable
  251. }
  252. reply := <-call.resp
  253. call.resp = nil
  254. c.uniq <- id
  255. if reply.err != nil {
  256. return nil, reply.err
  257. }
  258. return c.decodeFrame(reply.buf, trace)
  259. }
  260. func (c *Conn) dispatch(resp frame) {
  261. id := int(resp[2])
  262. if id >= len(c.calls) {
  263. return
  264. }
  265. call := &c.calls[id]
  266. if !atomic.CompareAndSwapInt32(&call.active, 1, 0) {
  267. return
  268. }
  269. atomic.AddInt32(&c.nwait, -1)
  270. call.resp <- callResp{resp, nil}
  271. }
  272. func (c *Conn) ping() error {
  273. _, err := c.exec(&optionsFrame{}, nil)
  274. return err
  275. }
  276. func (c *Conn) prepareStatement(stmt string, trace Tracer) (*queryInfo, error) {
  277. c.prepMu.Lock()
  278. flight := c.prep[stmt]
  279. if flight != nil {
  280. c.prepMu.Unlock()
  281. flight.wg.Wait()
  282. return flight.info, flight.err
  283. }
  284. flight = new(inflightPrepare)
  285. flight.wg.Add(1)
  286. c.prep[stmt] = flight
  287. c.prepMu.Unlock()
  288. resp, err := c.exec(&prepareFrame{Stmt: stmt}, trace)
  289. if err != nil {
  290. flight.err = err
  291. } else {
  292. switch x := resp.(type) {
  293. case resultPreparedFrame:
  294. flight.info = &queryInfo{
  295. id: x.PreparedId,
  296. args: x.Values,
  297. }
  298. case error:
  299. flight.err = x
  300. default:
  301. flight.err = ErrProtocol
  302. }
  303. }
  304. flight.wg.Done()
  305. if err != nil {
  306. c.prepMu.Lock()
  307. delete(c.prep, stmt)
  308. c.prepMu.Unlock()
  309. }
  310. return flight.info, flight.err
  311. }
  312. func (c *Conn) executeQuery(qry *Query) *Iter {
  313. op := &queryFrame{
  314. Stmt: qry.stmt,
  315. Cons: qry.cons,
  316. PageSize: qry.pageSize,
  317. PageState: qry.pageState,
  318. }
  319. if len(qry.values) > 0 {
  320. info, err := c.prepareStatement(qry.stmt, qry.trace)
  321. if err != nil {
  322. return &Iter{err: err}
  323. }
  324. op.Prepared = info.id
  325. op.Values = make([][]byte, len(qry.values))
  326. for i := 0; i < len(qry.values); i++ {
  327. val, err := Marshal(info.args[i].TypeInfo, qry.values[i])
  328. if err != nil {
  329. return &Iter{err: err}
  330. }
  331. op.Values[i] = val
  332. }
  333. }
  334. resp, err := c.exec(op, qry.trace)
  335. if err != nil {
  336. return &Iter{err: err}
  337. }
  338. switch x := resp.(type) {
  339. case resultVoidFrame:
  340. return &Iter{}
  341. case resultRowsFrame:
  342. iter := &Iter{columns: x.Columns, rows: x.Rows}
  343. if len(x.PagingState) > 0 {
  344. iter.next = &nextIter{
  345. qry: *qry,
  346. pos: int((1 - qry.prefetch) * float64(len(iter.rows))),
  347. }
  348. iter.next.qry.pageState = x.PagingState
  349. if iter.next.pos < 1 {
  350. iter.next.pos = 1
  351. }
  352. }
  353. return iter
  354. case resultKeyspaceFrame:
  355. c.cluster.HandleKeyspace(c, x.Keyspace)
  356. return &Iter{}
  357. case errorFrame:
  358. if x.Code == errUnprepared && len(qry.values) > 0 {
  359. c.prepMu.Lock()
  360. if val, ok := c.prep[qry.stmt]; ok && val != nil {
  361. delete(c.prep, qry.stmt)
  362. c.prepMu.Unlock()
  363. return c.executeQuery(qry)
  364. }
  365. c.prepMu.Unlock()
  366. return &Iter{err: x}
  367. } else {
  368. return &Iter{err: x}
  369. }
  370. case error:
  371. return &Iter{err: x}
  372. default:
  373. return &Iter{err: ErrProtocol}
  374. }
  375. }
  376. func (c *Conn) Pick(qry *Query) *Conn {
  377. return c
  378. }
  379. func (c *Conn) Close() {
  380. c.conn.Close()
  381. }
  382. func (c *Conn) Address() string {
  383. return c.addr
  384. }
  385. func (c *Conn) UseKeyspace(keyspace string) error {
  386. resp, err := c.exec(&queryFrame{Stmt: `USE "` + keyspace + `"`, Cons: Any}, nil)
  387. if err != nil {
  388. return err
  389. }
  390. switch x := resp.(type) {
  391. case resultKeyspaceFrame:
  392. case error:
  393. return x
  394. default:
  395. return ErrProtocol
  396. }
  397. return nil
  398. }
  399. func (c *Conn) executeBatch(batch *Batch) error {
  400. if c.version == 1 {
  401. return ErrUnsupported
  402. }
  403. f := make(frame, headerSize, defaultFrameSize)
  404. f.setHeader(c.version, 0, 0, opBatch)
  405. f.writeByte(byte(batch.Type))
  406. f.writeShort(uint16(len(batch.Entries)))
  407. for i := 0; i < len(batch.Entries); i++ {
  408. entry := &batch.Entries[i]
  409. var info *queryInfo
  410. if len(entry.Args) > 0 {
  411. var err error
  412. info, err = c.prepareStatement(entry.Stmt, nil)
  413. if err != nil {
  414. return err
  415. }
  416. f.writeByte(1)
  417. f.writeShortBytes(info.id)
  418. } else {
  419. f.writeByte(0)
  420. f.writeLongString(entry.Stmt)
  421. }
  422. f.writeShort(uint16(len(entry.Args)))
  423. for j := 0; j < len(entry.Args); j++ {
  424. val, err := Marshal(info.args[j].TypeInfo, entry.Args[j])
  425. if err != nil {
  426. return err
  427. }
  428. f.writeBytes(val)
  429. }
  430. }
  431. f.writeConsistency(batch.Cons)
  432. resp, err := c.exec(f, nil)
  433. if err != nil {
  434. return err
  435. }
  436. switch x := resp.(type) {
  437. case resultVoidFrame:
  438. return nil
  439. case error:
  440. return x
  441. default:
  442. return ErrProtocol
  443. }
  444. }
  445. func (c *Conn) decodeFrame(f frame, trace Tracer) (rval interface{}, err error) {
  446. defer func() {
  447. if r := recover(); r != nil {
  448. if e, ok := r.(error); ok && e == ErrProtocol {
  449. err = e
  450. return
  451. }
  452. panic(r)
  453. }
  454. }()
  455. if len(f) < headerSize || (f[0] != c.version|flagResponse) {
  456. return nil, ErrProtocol
  457. }
  458. flags, op, f := f[1], f[3], f[headerSize:]
  459. if flags&flagCompress != 0 && len(f) > 0 && c.compressor != nil {
  460. if buf, err := c.compressor.Decode([]byte(f)); err != nil {
  461. return nil, err
  462. } else {
  463. f = frame(buf)
  464. }
  465. }
  466. if flags&flagTrace != 0 {
  467. if len(f) < 16 {
  468. return nil, ErrProtocol
  469. }
  470. traceId := []byte(f[:16])
  471. f = f[16:]
  472. trace.Trace(traceId)
  473. }
  474. switch op {
  475. case opReady:
  476. return readyFrame{}, nil
  477. case opResult:
  478. switch kind := f.readInt(); kind {
  479. case resultKindVoid:
  480. return resultVoidFrame{}, nil
  481. case resultKindRows:
  482. columns, pageState := f.readMetaData()
  483. numRows := f.readInt()
  484. values := make([][]byte, numRows*len(columns))
  485. for i := 0; i < len(values); i++ {
  486. values[i] = f.readBytes()
  487. }
  488. rows := make([][][]byte, numRows)
  489. for i := 0; i < numRows; i++ {
  490. rows[i], values = values[:len(columns)], values[len(columns):]
  491. }
  492. return resultRowsFrame{columns, rows, pageState}, nil
  493. case resultKindKeyspace:
  494. keyspace := f.readString()
  495. return resultKeyspaceFrame{keyspace}, nil
  496. case resultKindPrepared:
  497. id := f.readShortBytes()
  498. values, _ := f.readMetaData()
  499. return resultPreparedFrame{id, values}, nil
  500. case resultKindSchemaChanged:
  501. return resultVoidFrame{}, nil
  502. default:
  503. return nil, ErrProtocol
  504. }
  505. case opAuthenticate:
  506. return authenticateFrame{f.readString()}, nil
  507. case opAuthChallenge:
  508. return authChallengeFrame{f.readBytes()}, nil
  509. case opAuthSuccess:
  510. return authSuccessFrame{f.readBytes()}, nil
  511. case opSupported:
  512. return supportedFrame{}, nil
  513. case opError:
  514. code := f.readInt()
  515. msg := f.readString()
  516. return errorFrame{code, msg}, nil
  517. default:
  518. return nil, ErrProtocol
  519. }
  520. }
  521. type queryInfo struct {
  522. id []byte
  523. args []ColumnInfo
  524. rval []ColumnInfo
  525. }
  526. type callReq struct {
  527. active int32
  528. resp chan callResp
  529. }
  530. type callResp struct {
  531. buf frame
  532. err error
  533. }
  534. type Compressor interface {
  535. Name() string
  536. Encode(data []byte) ([]byte, error)
  537. Decode(data []byte) ([]byte, error)
  538. }
  539. type inflightPrepare struct {
  540. info *queryInfo
  541. err error
  542. wg sync.WaitGroup
  543. }
  544. // SnappyCompressor implements the Compressor interface and can be used to
  545. // compress incoming and outgoing frames. The snappy compression algorithm
  546. // aims for very high speeds and reasonable compression.
  547. type SnappyCompressor struct{}
  548. func (s SnappyCompressor) Name() string {
  549. return "snappy"
  550. }
  551. func (s SnappyCompressor) Encode(data []byte) ([]byte, error) {
  552. return snappy.Encode(nil, data)
  553. }
  554. func (s SnappyCompressor) Decode(data []byte) ([]byte, error) {
  555. return snappy.Decode(nil, data)
  556. }