| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- // Copyright (c) 2012 The gocql Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style
- // license that can be found in the LICENSE file.
- package gocql
- import (
- "fmt"
- "net"
- "sync"
- "sync/atomic"
- "time"
- )
- const defaultFrameSize = 4096
- // Conn is a single connection to a Cassandra node. It can be used to execute
- // queries, but users are usually advised to use a more reliable, higher
- // level API.
- type Conn struct {
- conn net.Conn
- timeout time.Duration
- uniq chan uint8
- calls []callReq
- nwait int32
- prepMu sync.Mutex
- prep map[string]*queryInfo
- }
- // Connect establishes a connection to a Cassandra node.
- // You must also call the Serve method before you can execute any queries.
- func Connect(addr, version string, timeout time.Duration) (*Conn, error) {
- conn, err := net.DialTimeout("tcp", addr, timeout)
- if err != nil {
- return nil, err
- }
- c := &Conn{
- conn: conn,
- uniq: make(chan uint8, 128),
- calls: make([]callReq, 128),
- prep: make(map[string]*queryInfo),
- timeout: timeout,
- }
- for i := 0; i < cap(c.uniq); i++ {
- c.uniq <- uint8(i)
- }
- if err := c.init(version); err != nil {
- return nil, err
- }
- return c, nil
- }
- func (c *Conn) init(version string) error {
- req := make(frame, headerSize, defaultFrameSize)
- req.setHeader(protoRequest, 0, 0, opStartup)
- req.writeStringMap(map[string]string{
- "CQL_VERSION": version,
- })
- resp, err := c.callSimple(req)
- if err != nil {
- return err
- } else if resp[3] == opError {
- return resp.readErrorFrame()
- } else if resp[3] != opReady {
- return ErrProtocol
- }
- /* if cfg.Keyspace != "" {
- qry := &Query{stmt: "USE " + cfg.Keyspace}
- frame, err = c.executeQuery(qry)
- if err != nil {
- return err
- }
- } */
- return nil
- }
- // Serve starts the stream multiplexer for this connection, which is required
- // to execute any queries. This method runs as long as the connection is
- // open and is therefore usually called in a separate goroutine.
- func (c *Conn) Serve() error {
- var err error
- for {
- var frame frame
- frame, err = c.recv()
- if err != nil {
- break
- }
- c.dispatch(frame)
- }
- c.conn.Close()
- for id := 0; id < len(c.calls); id++ {
- req := &c.calls[id]
- if atomic.LoadInt32(&req.active) == 1 {
- req.resp <- callResp{nil, err}
- }
- }
- return err
- }
- func (c *Conn) recv() (frame, error) {
- resp := make(frame, headerSize, headerSize+512)
- c.conn.SetReadDeadline(time.Now().Add(c.timeout))
- n, last, pinged := 0, 0, false
- for n < len(resp) {
- nn, err := c.conn.Read(resp[n:])
- n += nn
- if err != nil {
- if err, ok := err.(net.Error); ok && err.Timeout() {
- if n > last {
- // we hit the deadline but we made progress.
- // simply extend the deadline
- c.conn.SetReadDeadline(time.Now().Add(c.timeout))
- last = n
- } else if n == 0 && !pinged {
- c.conn.SetReadDeadline(time.Now().Add(c.timeout))
- if atomic.LoadInt32(&c.nwait) > 0 {
- go c.ping()
- pinged = true
- }
- } else {
- return nil, err
- }
- } else {
- return nil, err
- }
- }
- if n == headerSize && len(resp) == headerSize {
- if resp[0] != protoResponse {
- return nil, ErrProtocol
- }
- resp.grow(resp.Length())
- }
- }
- return resp, nil
- }
- func (c *Conn) callSimple(req frame) (frame, error) {
- req.setLength(len(req) - headerSize)
- if _, err := c.conn.Write(req); err != nil {
- c.conn.Close()
- return nil, err
- }
- return c.recv()
- }
- func (c *Conn) call(req frame) (frame, error) {
- id := <-c.uniq
- req[2] = id
- call := &c.calls[id]
- call.resp = make(chan callResp, 1)
- atomic.AddInt32(&c.nwait, 1)
- atomic.StoreInt32(&call.active, 1)
- req.setLength(len(req) - headerSize)
- if _, err := c.conn.Write(req); err != nil {
- c.conn.Close()
- return nil, err
- }
- reply := <-call.resp
- call.resp = nil
- c.uniq <- id
- return reply.buf, reply.err
- }
- func (c *Conn) dispatch(resp frame) {
- id := int(resp[2])
- if id >= len(c.calls) {
- return
- }
- call := &c.calls[id]
- if !atomic.CompareAndSwapInt32(&call.active, 1, 0) {
- return
- }
- atomic.AddInt32(&c.nwait, -1)
- call.resp <- callResp{resp, nil}
- }
- func (c *Conn) ping() error {
- req := make(frame, headerSize)
- req.setHeader(protoRequest, 0, 0, opOptions)
- _, err := c.call(req)
- return err
- }
- func (c *Conn) prepareStatement(stmt string) (*queryInfo, error) {
- c.prepMu.Lock()
- info := c.prep[stmt]
- if info != nil {
- c.prepMu.Unlock()
- info.wg.Wait()
- return info, nil
- }
- info = new(queryInfo)
- info.wg.Add(1)
- c.prep[stmt] = info
- c.prepMu.Unlock()
- frame := make(frame, headerSize, defaultFrameSize)
- frame.setHeader(protoRequest, 0, 0, opPrepare)
- frame.writeLongString(stmt)
- frame.setLength(len(frame) - headerSize)
- frame, err := c.call(frame)
- if err != nil {
- return nil, err
- }
- if frame[3] == opError {
- return nil, frame.readErrorFrame()
- }
- frame.skipHeader()
- frame.readInt() // kind
- info.id = frame.readShortBytes()
- info.args = frame.readMetaData()
- info.rval = frame.readMetaData()
- info.wg.Done()
- return info, nil
- }
- func (c *Conn) ExecuteQuery(qry *Query) (*Iter, error) {
- frame, err := c.executeQuery(qry)
- if err != nil {
- return nil, err
- }
- if frame[3] == opError {
- return nil, frame.readErrorFrame()
- } else if frame[3] == opResult {
- iter := new(Iter)
- iter.readFrame(frame)
- return iter, nil
- }
- return nil, nil
- }
- func (c *Conn) ExecuteBatch(batch *Batch) error {
- frame := make(frame, headerSize, defaultFrameSize)
- frame.setHeader(protoRequest, 0, 0, opBatch)
- frame.writeByte(byte(batch.Type))
- frame.writeShort(uint16(len(batch.Entries)))
- for i := 0; i < len(batch.Entries); i++ {
- entry := &batch.Entries[i]
- var info *queryInfo
- if len(entry.Args) > 0 {
- info, err := c.prepareStatement(entry.Stmt)
- if err != nil {
- return err
- }
- frame.writeByte(1)
- frame.writeShortBytes(info.id)
- } else {
- frame.writeByte(0)
- frame.writeLongString(entry.Stmt)
- }
- frame.writeShort(uint16(len(entry.Args)))
- for j := 0; j < len(entry.Args); j++ {
- val, err := Marshal(info.args[j].TypeInfo, entry.Args[i])
- if err != nil {
- return err
- }
- frame.writeBytes(val)
- }
- }
- frame.writeConsistency(batch.Cons)
- frame, err := c.call(frame)
- if err != nil {
- return err
- }
- if frame[3] == opError {
- return frame.readErrorFrame()
- }
- return nil
- }
- func (c *Conn) Close() {
- c.conn.Close()
- }
- func (c *Conn) executeQuery(query *Query) (frame, error) {
- var info *queryInfo
- if len(query.Args) > 0 {
- fmt.Println("ARGS:", query.Args)
- var err error
- info, err = c.prepareStatement(query.Stmt)
- if err != nil {
- return nil, err
- }
- }
- frame := make(frame, headerSize, defaultFrameSize)
- if info == nil {
- frame.setHeader(protoRequest, 0, 0, opQuery)
- frame.writeLongString(query.Stmt)
- } else {
- frame.setHeader(protoRequest, 0, 0, opExecute)
- frame.writeShortBytes(info.id)
- }
- frame.writeConsistency(query.Cons)
- flags := uint8(0)
- if len(query.Args) > 0 {
- flags |= flagQueryValues
- }
- frame.writeByte(flags)
- if len(query.Args) > 0 {
- frame.writeShort(uint16(len(query.Args)))
- for i := 0; i < len(query.Args); i++ {
- val, err := Marshal(info.args[i].TypeInfo, query.Args[i])
- if err != nil {
- return nil, err
- }
- frame.writeBytes(val)
- }
- }
- frame, err := c.call(frame)
- if err != nil {
- return nil, err
- }
- if frame[3] == opError {
- frame.skipHeader()
- code := frame.readInt()
- desc := frame.readString()
- return nil, Error{code, desc}
- }
- return frame, nil
- }
- type queryInfo struct {
- id []byte
- args []ColumnInfo
- rval []ColumnInfo
- wg sync.WaitGroup
- }
- type callReq struct {
- active int32
- resp chan callResp
- }
- type callResp struct {
- buf frame
- err error
- }
|