tarantool 11.0.0

Tarantool rust bindings
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//! The `net_box` module contains connector to remote Tarantool server instances via a network.
//!
//! You can call the following methods:
//! - [Conn::new()](struct.Conn.html#method.new) to connect and get a connection object (named `conn` for examples in this section),
//! - other `net_box` routines, to execute requests on the remote database system,
//! - [conn.close()](struct.Conn.html#method.close) to disconnect.
//!
//! All [Conn](struct.Conn.html) methods are fiber-safe, that is, it is safe to share and use the same connection object
//! across multiple concurrent fibers. In fact that is perhaps the best programming practice with Tarantool. When
//! multiple fibers use the same connection, all requests are pipelined through the same network socket, but each fiber
//! gets back a correct response. Reducing the number of active sockets lowers the overhead of system calls and increases
//! the overall server performance. However for some cases a single connection is not enough — for example, when it is
//! necessary to prioritize requests or to use different authentication IDs.
//!
//! Most [Conn](struct.Conn.html) methods allow a `options` argument. See [Options](struct.Options.html) structure docs
//! for details.
//!
//! The diagram below shows possible connection states and transitions:
//! ```text
//! connecting -> initial +-> active
//!                        \
//!                         +-> auth -> fetch_schema <-> active
//!
//!  (any state, on error) -> error_reconnect -> connecting -> ...
//!                                           \
//!                                             -> [error]
//!  (any_state, but [error]) -> [closed]
//! ```
//!
//! On this diagram:
//! - The state machine starts in the `initial` state.
//! - [Conn::new()](struct.Conn.html#method.new) method changes the state to `connecting` and spawns a worker fiber.
//! - If authentication and schema upload are required, it’s possible later on to re-enter the `fetch_schema` state
//!   from `active` if a request fails due to a schema version mismatch error, so schema reload is triggered.
//! - [conn.close()](struct.Conn.html#method.close) method sets the state to `closed` and kills the worker. If the
//!   transport is already in the `error` state, [close()](struct.Conn.html#method.close) does nothing.
//!
//! See also:
//! - [Lua reference: Module net.box](https://www.tarantool.io/en/doc/latest/reference/reference_lua/net_box/)
#![cfg(feature = "net_box")]

use core::time::Duration;
use std::net::ToSocketAddrs;
use std::rc::Rc;

pub use index::{RemoteIndex, RemoteIndexIterator};
use inner::ConnInner;
pub use options::{ConnOptions, ConnTriggers, Options};
use promise::Promise;
pub use space::RemoteSpace;

use crate::error::Error;
use crate::network::protocol;
use crate::tuple::{Decode, ToTupleBuffer, Tuple};

mod index;
mod inner;
mod options;
pub mod promise;
mod recv_queue;
mod schema;
mod send_queue;
mod space;
mod stream;

#[deprecated = "use `TarantoolError` instead"]
pub type ResponseError = crate::error::TarantoolError;

/// Connection to remote Tarantool server
pub struct Conn {
    inner: Rc<ConnInner>,
    is_master: bool,
}

impl Conn {
    /// Create a new connection.
    ///
    /// The connection is established on demand, at the time of the first request. It can be re-established
    /// automatically after a disconnect (see [reconnect_after](struct.ConnOptions.html#structfield.reconnect_after) option).
    /// The returned conn object supports methods for making remote requests, such as select, update or delete.
    ///
    /// See also: [ConnOptions](struct.ConnOptions.html)
    #[inline(always)]
    pub fn new(
        addr: impl ToSocketAddrs,
        options: ConnOptions,
        triggers: Option<Rc<dyn ConnTriggers>>,
    ) -> Result<Self, Error> {
        Ok(Conn {
            inner: ConnInner::new(addr.to_socket_addrs()?.collect(), options, triggers)?,
            is_master: true,
        })
    }

    #[inline(always)]
    fn downgrade(inner: Rc<ConnInner>) -> Self {
        Conn {
            inner,
            is_master: false,
        }
    }

    /// Wait for connection to be active or closed.
    ///
    /// Returns:
    /// - `Ok(true)`: if active
    /// - `Ok(true)`: if closed
    /// - `Err(...TimedOut...)`: on timeout
    pub fn wait_connected(&self, timeout: Option<Duration>) -> Result<bool, Error> {
        self.inner.wait_connected(timeout)
    }

    /// Show whether connection is active or closed.
    pub fn is_connected(&self) -> bool {
        self.inner.is_connected()
    }

    /// Close a connection.
    pub fn close(&self) {
        self.inner.close()
    }

    /// Execute a PING command.
    ///
    /// - `options` – the supported option is `timeout`
    pub fn ping(&self, options: &Options) -> Result<(), Error> {
        self.inner.request(&protocol::Ping, options)?;
        Ok(())
    }

    /// Call a remote stored procedure.
    ///
    /// `conn.call("func", &("1", "2", "3"))` is the remote-call equivalent of `func('1', '2', '3')`.
    /// That is, `conn.call` is a remote stored-procedure call.
    /// The return from `conn.call` is whatever the function returns.
    pub fn call<T>(
        &self,
        fn_name: &str,
        args: &T,
        options: &Options,
    ) -> Result<Option<Tuple>, Error>
    where
        T: ToTupleBuffer,
        T: ?Sized,
    {
        let res = self
            .inner
            .request(&protocol::Call { fn_name, args }, options)?;
        Ok(Some(res))
    }

    /// Call a remote stored procedure without yielding.
    ///
    /// If enqueuing a request succeeded a [`Promise`] is returned which will be
    /// kept once a response is received.
    pub fn call_async<A, R>(&self, fn_name: &str, args: A) -> crate::Result<Promise<R>>
    where
        A: ToTupleBuffer,
        R: for<'de> Decode<'de> + 'static,
    {
        self.inner.request_async(&protocol::Call {
            fn_name,
            args: &args,
        })
    }

    /// Evaluates and executes the expression in Lua-string, which may be any statement or series of statements.
    ///
    /// An execute privilege is required; if the user does not have it, an administrator may grant it with
    /// `box.schema.user.grant(username, 'execute', 'universe')`.
    ///
    /// To ensure that the return from `eval` is whatever the Lua expression returns, begin the Lua-string with the
    /// word `return`.
    pub fn eval<T>(&self, expr: &str, args: &T, options: &Options) -> Result<Option<Tuple>, Error>
    where
        T: ToTupleBuffer,
        T: ?Sized,
    {
        let res = self
            .inner
            .request(&protocol::Eval { expr, args }, options)?;
        Ok(Some(res))
    }

    /// Executes a series of lua statements on a remote host without yielding.
    ///
    /// If enqueuing a request succeeded a [`Promise`] is returned which will be
    /// kept once a response is received.
    pub fn eval_async<A, R>(&self, expr: &str, args: A) -> crate::Result<Promise<R>>
    where
        A: ToTupleBuffer,
        R: for<'de> Decode<'de> + 'static,
    {
        self.inner
            .request_async(&protocol::Eval { expr, args: &args })
    }

    /// Search space by name on remote server
    pub fn space(&self, name: &str) -> Result<Option<RemoteSpace>, Error> {
        Ok(self
            .inner
            .lookup_space(name)?
            .map(|space_id| RemoteSpace::new(self.inner.clone(), space_id)))
    }

    /// Remote execute of sql query.
    pub fn execute<P>(
        &self,
        sql: &str,
        bind_params: &P,
        options: &Options,
    ) -> Result<Vec<Tuple>, Error>
    where
        P: ToTupleBuffer + ?Sized,
    {
        self.inner
            .request(&protocol::Execute { sql, bind_params }, options)
    }
}

impl Drop for Conn {
    fn drop(&mut self) {
        if self.is_master {
            self.close();
        }
    }
}

#[cfg(feature = "internal_test")]
mod tests {
    use super::*;
    use crate::test::util::listen_port;

    fn test_user_conn() -> Conn {
        Conn::new(
            ("localhost", listen_port()),
            ConnOptions {
                user: "test_user".into(),
                password: "password".into(),
                auth_method: crate::auth::AuthMethod::ChapSha1,
                ..ConnOptions::default()
            },
            None,
        )
        .unwrap()
    }

    #[crate::test(tarantool = "crate")]
    fn dont_drop_worker_join_handles() {
        struct UnexpectedIOError;
        impl ToTupleBuffer for UnexpectedIOError {
            fn write_tuple_data(&self, _: &mut impl std::io::Write) -> Result<(), Error> {
                Err(Error::other("some io error"))
            }
        }

        let conn = test_user_conn();

        let e = conn
            .eval("return ...", &UnexpectedIOError, &Default::default())
            .unwrap_err();
        assert_eq!(e.to_string(), "some io error");

        let e = conn
            .eval("return ...", &[1], &Default::default())
            .unwrap_err();
        assert_eq!(e.to_string(), "io error: not connected");

        // At this point we used to panic
        conn.close();
    }

    #[crate::test(tarantool = "crate")]
    fn errors_in_a_row_bug() {
        let conn = test_user_conn();

        // There was a bug with this, but it's now fixed
        for _ in 0..5 {
            let e = conn
                .eval("error 'oops'", &(), &Default::default())
                .unwrap_err();
            #[rustfmt::skip]
            assert_eq!(e.to_string(), "server responded with error: ProcLua: eval:1: oops");
        }
    }

    #[cfg(feature = "picodata")]
    #[crate::test(tarantool = "crate")]
    async fn md5_auth_method() {
        use crate::auth::AuthMethod;

        let username = "Worry";
        let password = "B Gone";

        // NOTE: because we test our fork of `tarantool` here (see `picodata` feature flag on a test), we can
        // pass `auth_type` parameter right into `box.schema.user.create`. This won't work in default `tarantool`.
        crate::lua_state()
            .exec_with(
                "local username, password = ...
                box.cfg { }
                box.schema.user.create(username, { if_not_exists = true, auth_type = 'md5', password = password })
                box.schema.user.grant(username, 'super', nil, nil, { if_not_exists = true })",
                (username, password),
            )
            .unwrap();

        // Successful connection
        {
            let conn = Conn::new(
                ("localhost", listen_port()),
                ConnOptions {
                    user: username.into(),
                    password: password.into(),
                    auth_method: AuthMethod::Md5,
                    ..ConnOptions::default()
                },
                None,
            )
            .unwrap();

            conn.eval(
                "print('\\x1b[32mit works!\\x1b[0m')",
                &(),
                &Default::default(),
            )
            .unwrap();
        }

        // Wrong password
        {
            let conn = Conn::new(
                ("localhost", listen_port()),
                ConnOptions {
                    user: username.into(),
                    password: "wrong password".into(),
                    auth_method: AuthMethod::Md5,
                    ..ConnOptions::default()
                },
                None,
            )
            .unwrap();

            let err = conn
                .eval("return", &(), &Default::default())
                .unwrap_err()
                .to_string();
            assert_eq!(
                err,
                "server responded with error: PasswordMismatch: User not found or supplied credentials are invalid"
            );
        }

        // Wrong auth method
        {
            let conn = Conn::new(
                ("localhost", listen_port()),
                ConnOptions {
                    user: username.into(),
                    password: "wrong password".into(),
                    auth_method: AuthMethod::ChapSha1,
                    ..ConnOptions::default()
                },
                None,
            )
            .unwrap();

            let err = conn
                .eval("return", &(), &Default::default())
                .unwrap_err()
                .to_string();
            assert_eq!(
                err,
                "server responded with error: PasswordMismatch: User not found or supplied credentials are invalid"
            );
        }

        crate::lua_state()
            // This is the default
            .exec_with(
                "local username = ...
                box.cfg { auth_type = 'chap-sha1' }
                box.schema.user.drop(username)",
                username,
            )
            .unwrap();
    }

    #[cfg(feature = "picodata")]
    #[crate::test(tarantool = "crate")]
    async fn ldap_auth_method() {
        use crate::auth::AuthMethod;

        let username = "Worry";
        let password = "B Gone";

        let _guard = crate::unwrap_ok_or!(
            crate::test::util::setup_ldap_auth(username, password),
            Err(e) => {
                println!("{e}, skipping ldap test");
                return;
            }
        );

        // Successfull connection
        {
            let conn = Conn::new(
                ("localhost", listen_port()),
                ConnOptions {
                    user: username.into(),
                    password: password.into(),
                    auth_method: AuthMethod::Ldap,
                    ..ConnOptions::default()
                },
                None,
            )
            .unwrap();

            conn.eval(
                "print('\\x1b[32mit works!\\x1b[0m')",
                &(),
                &Default::default(),
            )
            .unwrap();
        }

        // Wrong password
        {
            let conn = Conn::new(
                ("localhost", listen_port()),
                ConnOptions {
                    user: username.into(),
                    password: "wrong password".into(),
                    auth_method: AuthMethod::Ldap,
                    ..ConnOptions::default()
                },
                None,
            )
            .unwrap();

            let err = conn
                .eval("return", &(), &Default::default())
                .unwrap_err()
                .to_string();
            assert_eq!(
                err,
                "server responded with error: System: Invalid credentials"
            );
        }

        // Wrong auth method
        {
            let conn = Conn::new(
                ("localhost", listen_port()),
                ConnOptions {
                    user: username.into(),
                    password: "wrong password".into(),
                    auth_method: AuthMethod::ChapSha1,
                    ..ConnOptions::default()
                },
                None,
            )
            .unwrap();

            let err = conn
                .eval("return", &(), &Default::default())
                .unwrap_err()
                .to_string();
            assert_eq!(
                err,
                "server responded with error: PasswordMismatch: User not found or supplied credentials are invalid"
            );
        }
    }
}