Skip to content

Commit

Permalink
streams: interactive transactions and support
Browse files Browse the repository at this point in the history
The main purpose of streams is transactions via iproto.
Since v. 2.10.0, Tarantool supports streams and
interactive transactions over them. Each stream
can start its own transaction, so they allows
multiplexing several transactions over one connection.

API for this feature is the following:

* `NewStream()` method to create a stream object for `Connection`
   and `NewStream(userMode Mode)` method to create a stream object
   for `ConnectionPool`
*  stream object `Stream` with `Do()` method and
   new request objects to work with stream,
   `BeginRequest` - start transaction via iproto stream;
   `CommitRequest` - commit transaction;
   `RollbackRequest` - rollback transaction.

Closes #101
  • Loading branch information
AnaNek committed Jul 27, 2022
1 parent 5fd3042 commit 03e44f6
Show file tree
Hide file tree
Showing 19 changed files with 1,809 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
- Support datetime type in msgpack (#118)
- Prepared SQL statements (#117)
- Context support for request objects (#48)
- Streams and interactive transactions support (#101)

### Changed

Expand Down
1 change: 1 addition & 0 deletions config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
-- able to send requests until everything is configured.
box.cfg{
work_dir = os.getenv("TEST_TNT_WORK_DIR"),
memtx_use_mvcc_engine = os.getenv("TEST_TNT_MEMTX_USE_MVCC_ENGINE") == 'true' or nil,
}

box.once("init", function()
Expand Down
47 changes: 37 additions & 10 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
)

const requestsMap = 128
const ignoreStreamId = 0
const (
connDisconnected = 0
connConnected = 1
Expand Down Expand Up @@ -143,6 +144,8 @@ type Connection struct {
state uint32
dec *msgpack.Decoder
lenbuf [PacketLengthBytes]byte

lastStreamId uint64
}

var _ = Connector(&Connection{}) // Check compatibility with connector interface.
Expand Down Expand Up @@ -528,16 +531,27 @@ func (conn *Connection) dial() (err error) {
return
}

func pack(h *smallWBuf, enc *msgpack.Encoder, reqid uint32, req Request, res SchemaResolver) (err error) {
func pack(h *smallWBuf, enc *msgpack.Encoder, reqid uint32,
req Request, streamId uint64, res SchemaResolver) (err error) {
hl := h.Len()
h.Write([]byte{

hMapLen := byte(0x82) // 2 element map.
if streamId != ignoreStreamId {
hMapLen = byte(0x83) // 3 element map.
}
hBytes := []byte{
0xce, 0, 0, 0, 0, // Length.
0x82, // 2 element map.
hMapLen,
KeyCode, byte(req.Code()), // Request code.
KeySync, 0xce,
byte(reqid >> 24), byte(reqid >> 16),
byte(reqid >> 8), byte(reqid),
})
}
if streamId != ignoreStreamId {
hBytes = append(hBytes, KeyStreamId, byte(streamId))
}

h.Write(hBytes)

if err = req.Body(res, enc); err != nil {
return
Expand All @@ -555,7 +569,7 @@ func pack(h *smallWBuf, enc *msgpack.Encoder, reqid uint32, req Request, res Sch
func (conn *Connection) writeAuthRequest(w *bufio.Writer, scramble []byte) (err error) {
var packet smallWBuf
req := newAuthRequest(conn.opts.User, string(scramble))
err = pack(&packet, msgpack.NewEncoder(&packet), 0, req, conn.Schema)
err = pack(&packet, msgpack.NewEncoder(&packet), 0, req, ignoreStreamId, conn.Schema)

if err != nil {
return errors.New("auth: pack error " + err.Error())
Expand Down Expand Up @@ -869,7 +883,7 @@ func (conn *Connection) contextWatchdog(fut *Future, ctx context.Context) {
}
}

func (conn *Connection) send(req Request) *Future {
func (conn *Connection) send(req Request, streamId uint64) *Future {
fut := conn.newFuture(req.Ctx())
if fut.ready == nil {
return fut
Expand All @@ -882,14 +896,14 @@ func (conn *Connection) send(req Request) *Future {
default:
}
}
conn.putFuture(fut, req)
conn.putFuture(fut, req, streamId)
if req.Ctx() != nil {
go conn.contextWatchdog(fut, req.Ctx())
}
return fut
}

func (conn *Connection) putFuture(fut *Future, req Request) {
func (conn *Connection) putFuture(fut *Future, req Request, streamId uint64) {
shardn := fut.requestId & (conn.opts.Concurrency - 1)
shard := &conn.shard[shardn]
shard.bufmut.Lock()
Expand All @@ -906,7 +920,7 @@ func (conn *Connection) putFuture(fut *Future, req Request) {
}
blen := shard.buf.Len()
reqid := fut.requestId
if err := pack(&shard.buf, shard.enc, reqid, req, conn.Schema); err != nil {
if err := pack(&shard.buf, shard.enc, reqid, req, streamId, conn.Schema); err != nil {
shard.buf.Trunc(blen)
shard.bufmut.Unlock()
if f := conn.fetchFuture(reqid); f == fut {
Expand Down Expand Up @@ -1095,7 +1109,7 @@ func (conn *Connection) Do(req Request) *Future {
default:
}
}
return conn.send(req)
return conn.send(req, ignoreStreamId)
}

// ConfiguredTimeout returns a timeout from connection config.
Expand All @@ -1121,3 +1135,16 @@ func (conn *Connection) NewPrepared(expr string) (*Prepared, error) {
}
return NewPreparedFromResponse(conn, resp)
}

// NewStream creates new Stream object for connection.
//
// Since v. 2.10.0, Tarantool supports streams and interactive transactions over them.
// To use interactive transactions, memtx_use_mvcc_engine box option should be set to true.
// Since 1.7.0
func (conn *Connection) NewStream() (*Stream, error) {
next := atomic.AddUint64(&conn.lastStreamId, 1)
return &Stream{
Id: next,
Conn: conn,
}, nil
}
1 change: 1 addition & 0 deletions connection_pool/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
-- able to send requests until everything is configured.
box.cfg{
work_dir = os.getenv("TEST_TNT_WORK_DIR"),
memtx_use_mvcc_engine = os.getenv("TEST_TNT_MEMTX_USE_MVCC_ENGINE") == 'true' or nil,
}

box.once("init", function()
Expand Down
14 changes: 14 additions & 0 deletions connection_pool/connection_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,20 @@ func (connPool *ConnectionPool) Do(req tarantool.Request, userMode Mode) *tarant
return conn.Do(req)
}

// NewStream creates new Stream object for connection selected
// by userMode from connPool.
//
// Since v. 2.10.0, Tarantool supports streams and interactive transactions over them.
// To use interactive transactions, memtx_use_mvcc_engine box option should be set to true.
// Since 1.7.0
func (connPool *ConnectionPool) NewStream(userMode Mode) (*tarantool.Stream, error) {
conn, err := connPool.getNextConnection(userMode)
if err != nil {
return nil, err
}
return conn.NewStream()
}

//
// private
//
Expand Down
Loading

0 comments on commit 03e44f6

Please sign in to comment.