Skip to content

Commit 7964fb0

Browse files
api: support iproto feature discovery
Since version 2.10.0 Tarantool supports feature discovery [1]. Client can send client protocol version and supported features and receive server protocol version and supported features information to tune its behavior. After this patch, the request will be sent on `dial`, before authentication is performed. Connector stores server info in connection internals. User can also set option RequiredProtocolInfo to fast fail on connect if server does not provide some expected feature, similar to net.box opts [2]. It is not clear how connector should behave in case if client doesn't support a protocol feature or protocol version, see [3]. For now we decided not to check requirements on the client side. Feature check iterates over lists to check if feature is enabled. It seems that iterating over a small list is way faster than building a map, see [4]. Benchmark tests show that this check is rather fast (0.5 ns for both client and server check on HP ProBook 440 G5) so it is not necessary to cache it in any way. Traces of IPROTO_FEATURE_GRACEFUL_SHUTDOWN flag and protocol version 4 could be found in Tarantool source code but they were removed in the following commits before the release and treated like they never existed. We also ignore them here too. See [5] for more info. In latest master commit new feature with code 4 and protocol version 4 were introduced [6]. 1. tarantool/tarantool#6253 2. https://www.tarantool.io/en/doc/latest/reference/reference_lua/net_box/#lua-function.net_box.new 3. tarantool/tarantool#7953 4. https://stackoverflow.com/a/52710077/11646599 5. tarantool/tarantool-python#262 6. tarantool/tarantool@948e5cd Closes #120
1 parent a20f033 commit 7964fb0

14 files changed

+849
-61
lines changed

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
1010

1111
### Added
1212

13+
- Support iproto feature discovery (#120).
14+
1315
### Changed
1416

1517
### Fixed

Diff for: connection.go

+131-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"math"
1515
"net"
1616
"runtime"
17+
"strings"
1718
"sync"
1819
"sync/atomic"
1920
"time"
@@ -146,6 +147,8 @@ type Connection struct {
146147
lenbuf [PacketLengthBytes]byte
147148

148149
lastStreamId uint64
150+
151+
serverProtocolInfo ProtocolInfo
149152
}
150153

151154
var _ = Connector(&Connection{}) // Check compatibility with connector interface.
@@ -269,6 +272,10 @@ type Opts struct {
269272
Transport string
270273
// SslOpts is used only if the Transport == 'ssl' is set.
271274
Ssl SslOpts
275+
// RequiredProtocolInfo contains minimal protocol version and
276+
// list of protocol features that should be supported by
277+
// Tarantool server. By default there are no restrictions
278+
RequiredProtocolInfo ProtocolInfo
272279
}
273280

274281
// SslOpts is a way to configure ssl transport.
@@ -293,10 +300,13 @@ type SslOpts struct {
293300
Ciphers string
294301
}
295302

296-
// Copy returns the copy of an Opts object.
303+
// Clone returns a copy of the Opts object.
297304
// Beware that Notify channel, Logger and Handle are not copied.
298-
func (opts Opts) Copy() Opts {
305+
// Any changes in copy RequiredProtocolInfo will not affect the original
306+
// RequiredProtocolInfo value.
307+
func (opts Opts) Clone() Opts {
299308
optsCopy := opts
309+
optsCopy.RequiredProtocolInfo = opts.RequiredProtocolInfo.Clone()
300310

301311
return optsCopy
302312
}
@@ -327,7 +337,7 @@ func Connect(addr string, opts Opts) (conn *Connection, err error) {
327337
contextRequestId: 1,
328338
Greeting: &Greeting{},
329339
control: make(chan struct{}),
330-
opts: opts.Copy(),
340+
opts: opts.Clone(),
331341
dec: newDecoder(&smallBuf{}),
332342
}
333343
maxprocs := uint32(runtime.GOMAXPROCS(-1))
@@ -510,6 +520,18 @@ func (conn *Connection) dial() (err error) {
510520
conn.Greeting.Version = bytes.NewBuffer(greeting[:64]).String()
511521
conn.Greeting.auth = bytes.NewBuffer(greeting[64:108]).String()
512522

523+
// IPROTO_ID requests can be processed without authentication.
524+
// https://www.tarantool.io/en/doc/latest/dev_guide/internals/iproto/requests/#iproto-id
525+
if err = conn.identify(w, r); err != nil {
526+
connection.Close()
527+
return err
528+
}
529+
530+
if err = checkProtocolInfo(opts.RequiredProtocolInfo, conn.serverProtocolInfo); err != nil {
531+
connection.Close()
532+
return fmt.Errorf("identify: %w", err)
533+
}
534+
513535
// Auth
514536
if opts.User != "" {
515537
scr, err := scramble(conn.Greeting.auth, opts.Pass)
@@ -616,6 +638,17 @@ func (conn *Connection) writeAuthRequest(w *bufio.Writer, scramble []byte) error
616638
return nil
617639
}
618640

641+
func (conn *Connection) writeIdRequest(w *bufio.Writer, protocolInfo ProtocolInfo) error {
642+
req := NewIdRequest(protocolInfo)
643+
644+
err := conn.writeRequest(w, req)
645+
if err != nil {
646+
return fmt.Errorf("identify: %w", err)
647+
}
648+
649+
return nil
650+
}
651+
619652
func (conn *Connection) readResponse(r io.Reader) (Response, error) {
620653
respBytes, err := conn.read(r)
621654
if err != nil {
@@ -648,6 +681,15 @@ func (conn *Connection) readAuthResponse(r io.Reader) error {
648681
return nil
649682
}
650683

684+
func (conn *Connection) readIdResponse(r io.Reader) (Response, error) {
685+
resp, err := conn.readResponse(r)
686+
if err != nil {
687+
return resp, fmt.Errorf("identify: %w", err)
688+
}
689+
690+
return resp, nil
691+
}
692+
651693
func (conn *Connection) createConnection(reconnect bool) (err error) {
652694
var reconnects uint
653695
for conn.c == nil && conn.state == connDisconnected {
@@ -1191,3 +1233,89 @@ func (conn *Connection) NewStream() (*Stream, error) {
11911233
Conn: conn,
11921234
}, nil
11931235
}
1236+
1237+
// checkProtocolInfo checks that expected protocol version is
1238+
// and protocol features are supported.
1239+
func checkProtocolInfo(expected ProtocolInfo, actual ProtocolInfo) error {
1240+
var found bool
1241+
var missingFeatures []ProtocolFeature
1242+
1243+
if expected.Version > actual.Version {
1244+
return fmt.Errorf("protocol version %d is not supported", expected.Version)
1245+
}
1246+
1247+
// It seems that iterating over a small list is way faster
1248+
// than building a map: https://stackoverflow.com/a/52710077/11646599
1249+
for _, expectedFeature := range expected.Features {
1250+
found = false
1251+
for _, actualFeature := range actual.Features {
1252+
if expectedFeature == actualFeature {
1253+
found = true
1254+
}
1255+
}
1256+
if !found {
1257+
missingFeatures = append(missingFeatures, expectedFeature)
1258+
}
1259+
}
1260+
1261+
if len(missingFeatures) == 1 {
1262+
return fmt.Errorf("protocol feature %s is not supported", missingFeatures[0])
1263+
}
1264+
1265+
if len(missingFeatures) > 1 {
1266+
var sarr []string
1267+
for _, missingFeature := range missingFeatures {
1268+
sarr = append(sarr, missingFeature.String())
1269+
}
1270+
return fmt.Errorf("protocol features %s are not supported", strings.Join(sarr, ", "))
1271+
}
1272+
1273+
return nil
1274+
}
1275+
1276+
// identify sends info about client protocol, receives info
1277+
// about server protocol in response and stores it in the connection.
1278+
func (conn *Connection) identify(w *bufio.Writer, r *bufio.Reader) error {
1279+
var ok bool
1280+
1281+
werr := conn.writeIdRequest(w, clientProtocolInfo)
1282+
if werr != nil {
1283+
return werr
1284+
}
1285+
1286+
resp, rerr := conn.readIdResponse(r)
1287+
if rerr != nil {
1288+
if resp.Code == ErrUnknownRequestType {
1289+
// IPROTO_ID requests are not supported by server.
1290+
return nil
1291+
}
1292+
1293+
return rerr
1294+
}
1295+
1296+
if len(resp.Data) == 0 {
1297+
return fmt.Errorf("identify: unexpected response: no data")
1298+
}
1299+
1300+
conn.serverProtocolInfo, ok = resp.Data[0].(ProtocolInfo)
1301+
if !ok {
1302+
return fmt.Errorf("identify: unexpected response: wrong data")
1303+
}
1304+
1305+
return nil
1306+
}
1307+
1308+
// ServerProtocolVersion returns protocol version and protocol features
1309+
// supported by connected Tarantool server. Beware that values might be
1310+
// outdated if connection is in a disconnected state.
1311+
// Since 1.10.0
1312+
func (conn *Connection) ServerProtocolInfo() ProtocolInfo {
1313+
return conn.serverProtocolInfo.Clone()
1314+
}
1315+
1316+
// ClientProtocolVersion returns protocol version and protocol features
1317+
// supported by Go connection client.
1318+
// Since 1.10.0
1319+
func (conn *Connection) ClientProtocolInfo() ProtocolInfo {
1320+
return clientProtocolInfo.Clone()
1321+
}

Diff for: connection_pool/connection_pool.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func ConnectWithOpts(addrs []string, connOpts tarantool.Opts, opts OptsPool) (co
125125

126126
connPool = &ConnectionPool{
127127
addrs: make([]string, 0, len(addrs)),
128-
connOpts: connOpts.Copy(),
128+
connOpts: connOpts.Clone(),
129129
opts: opts,
130130
state: unknownState,
131131
done: make(chan struct{}),

0 commit comments

Comments
 (0)