Skip to content

Commit 999cf4a

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 f433cf2 commit 999cf4a

12 files changed

+844
-56
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

+128
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.
@@ -294,8 +301,11 @@ type SslOpts struct {
294301
}
295302

296303
// Clone returns a copy of the Opts object.
304+
// Any changes in copy RequiredProtocolInfo will not affect the original
305+
// RequiredProtocolInfo value.
297306
func (opts Opts) Clone() Opts {
298307
optsCopy := opts
308+
optsCopy.RequiredProtocolInfo = opts.RequiredProtocolInfo.Clone()
299309

300310
return optsCopy
301311
}
@@ -509,6 +519,18 @@ func (conn *Connection) dial() (err error) {
509519
conn.Greeting.Version = bytes.NewBuffer(greeting[:64]).String()
510520
conn.Greeting.auth = bytes.NewBuffer(greeting[64:108]).String()
511521

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

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

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

Diff for: connection_pool/example_test.go

+36-18
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type Tuple struct {
1919

2020
var testRoles = []bool{true, true, false, true, true}
2121

22-
func examplePool(roles []bool) (*connection_pool.ConnectionPool, error) {
22+
func examplePool(roles []bool, connOpts tarantool.Opts) (*connection_pool.ConnectionPool, error) {
2323
err := test_helpers.SetClusterRO(servers, connOpts, roles)
2424
if err != nil {
2525
return nil, fmt.Errorf("ConnectionPool is not established")
@@ -33,7 +33,7 @@ func examplePool(roles []bool) (*connection_pool.ConnectionPool, error) {
3333
}
3434

3535
func ExampleConnectionPool_Select() {
36-
pool, err := examplePool(testRoles)
36+
pool, err := examplePool(testRoles, connOpts)
3737
if err != nil {
3838
fmt.Println(err)
3939
}
@@ -94,7 +94,7 @@ func ExampleConnectionPool_Select() {
9494
}
9595

9696
func ExampleConnectionPool_SelectTyped() {
97-
pool, err := examplePool(testRoles)
97+
pool, err := examplePool(testRoles, connOpts)
9898
if err != nil {
9999
fmt.Println(err)
100100
}
@@ -156,7 +156,7 @@ func ExampleConnectionPool_SelectTyped() {
156156
}
157157

158158
func ExampleConnectionPool_SelectAsync() {
159-
pool, err := examplePool(testRoles)
159+
pool, err := examplePool(testRoles, connOpts)
160160
if err != nil {
161161
fmt.Println(err)
162162
}
@@ -239,7 +239,7 @@ func ExampleConnectionPool_SelectAsync() {
239239

240240
func ExampleConnectionPool_SelectAsync_err() {
241241
roles := []bool{true, true, true, true, true}
242-
pool, err := examplePool(roles)
242+
pool, err := examplePool(roles, connOpts)
243243
if err != nil {
244244
fmt.Println(err)
245245
}
@@ -258,7 +258,7 @@ func ExampleConnectionPool_SelectAsync_err() {
258258
}
259259

260260
func ExampleConnectionPool_Ping() {
261-
pool, err := examplePool(testRoles)
261+
pool, err := examplePool(testRoles, connOpts)
262262
if err != nil {
263263
fmt.Println(err)
264264
}
@@ -276,7 +276,7 @@ func ExampleConnectionPool_Ping() {
276276
}
277277

278278
func ExampleConnectionPool_Insert() {
279-
pool, err := examplePool(testRoles)
279+
pool, err := examplePool(testRoles, connOpts)
280280
if err != nil {
281281
fmt.Println(err)
282282
}
@@ -325,7 +325,7 @@ func ExampleConnectionPool_Insert() {
325325
}
326326

327327
func ExampleConnectionPool_Delete() {
328-
pool, err := examplePool(testRoles)
328+
pool, err := examplePool(testRoles, connOpts)
329329
if err != nil {
330330
fmt.Println(err)
331331
}
@@ -377,7 +377,7 @@ func ExampleConnectionPool_Delete() {
377377
}
378378

379379
func ExampleConnectionPool_Replace() {
380-
pool, err := examplePool(testRoles)
380+
pool, err := examplePool(testRoles, connOpts)
381381
if err != nil {
382382
fmt.Println(err)
383383
}
@@ -448,7 +448,7 @@ func ExampleConnectionPool_Replace() {
448448
}
449449

450450
func ExampleConnectionPool_Update() {
451-
pool, err := examplePool(testRoles)
451+
pool, err := examplePool(testRoles, connOpts)
452452
if err != nil {
453453
fmt.Println(err)
454454
}
@@ -492,7 +492,7 @@ func ExampleConnectionPool_Update() {
492492
}
493493

494494
func ExampleConnectionPool_Call() {
495-
pool, err := examplePool(testRoles)
495+
pool, err := examplePool(testRoles, connOpts)
496496
if err != nil {
497497
fmt.Println(err)
498498
}
@@ -512,7 +512,7 @@ func ExampleConnectionPool_Call() {
512512
}
513513

514514
func ExampleConnectionPool_Eval() {
515-
pool, err := examplePool(testRoles)
515+
pool, err := examplePool(testRoles, connOpts)
516516
if err != nil {
517517
fmt.Println(err)
518518
}
@@ -532,7 +532,7 @@ func ExampleConnectionPool_Eval() {
532532
}
533533

534534
func ExampleConnectionPool_Do() {
535-
pool, err := examplePool(testRoles)
535+
pool, err := examplePool(testRoles, connOpts)
536536
if err != nil {
537537
fmt.Println(err)
538538
}
@@ -551,7 +551,7 @@ func ExampleConnectionPool_Do() {
551551
}
552552

553553
func ExampleConnectionPool_NewPrepared() {
554-
pool, err := examplePool(testRoles)
554+
pool, err := examplePool(testRoles, connOpts)
555555
if err != nil {
556556
fmt.Println(err)
557557
}
@@ -575,6 +575,21 @@ func ExampleConnectionPool_NewPrepared() {
575575
}
576576
}
577577

578+
func getTestTxnOpts() tarantool.Opts {
579+
txnOpts := connOpts.Clone()
580+
581+
// Assert that server supports expected protocol features
582+
txnOpts.RequiredProtocolInfo = tarantool.ProtocolInfo{
583+
Version: tarantool.ProtocolVersion(1),
584+
Features: []tarantool.ProtocolFeature{
585+
tarantool.StreamsFeature,
586+
tarantool.TransactionsFeature,
587+
},
588+
}
589+
590+
return txnOpts
591+
}
592+
578593
func ExampleCommitRequest() {
579594
var req tarantool.Request
580595
var resp *tarantool.Response
@@ -586,7 +601,8 @@ func ExampleCommitRequest() {
586601
return
587602
}
588603

589-
pool, err := examplePool(testRoles)
604+
txnOpts := getTestTxnOpts()
605+
pool, err := examplePool(testRoles, txnOpts)
590606
if err != nil {
591607
fmt.Println(err)
592608
return
@@ -672,8 +688,9 @@ func ExampleRollbackRequest() {
672688
return
673689
}
674690

691+
txnOpts := getTestTxnOpts()
675692
// example pool has only one rw instance
676-
pool, err := examplePool(testRoles)
693+
pool, err := examplePool(testRoles, txnOpts)
677694
if err != nil {
678695
fmt.Println(err)
679696
return
@@ -758,8 +775,9 @@ func ExampleBeginRequest_TxnIsolation() {
758775
return
759776
}
760777

778+
txnOpts := getTestTxnOpts()
761779
// example pool has only one rw instance
762-
pool, err := examplePool(testRoles)
780+
pool, err := examplePool(testRoles, txnOpts)
763781
if err != nil {
764782
fmt.Println(err)
765783
return
@@ -836,7 +854,7 @@ func ExampleBeginRequest_TxnIsolation() {
836854
}
837855

838856
func ExampleConnectorAdapter() {
839-
pool, err := examplePool(testRoles)
857+
pool, err := examplePool(testRoles, connOpts)
840858
if err != nil {
841859
fmt.Println(err)
842860
}

0 commit comments

Comments
 (0)