conn.go 11 KB

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