picodata_plugin/internal/
mod.rs

1//! Picodata internal API.
2pub mod cas;
3pub(crate) mod ffi;
4pub mod types;
5
6use crate::internal::ffi::{
7    pico_ffi_authenticate, pico_ffi_instance_info, pico_ffi_raft_info, pico_ffi_rpc_version,
8    pico_ffi_version,
9};
10use crate::internal::types::InstanceInfo;
11use abi_stable::derive_macro_reexports::RResult;
12use std::{env, fs, io, process};
13use tarantool::error::BoxError;
14
15/// Return picodata version.
16pub fn picodata_version() -> &'static str {
17    let ptr_and_len = unsafe { pico_ffi_version() };
18    // SAFETY: ptr points to static string
19    let slice = unsafe { std::slice::from_raw_parts(ptr_and_len.0, ptr_and_len.1) };
20    std::str::from_utf8(slice).expect("should be valid utf8")
21}
22
23/// Return picodata RPC API version.
24pub fn rpc_version() -> &'static str {
25    let ptr_and_len = unsafe { pico_ffi_rpc_version() };
26    // SAFETY: ptr points to static string
27    let slice = unsafe { std::slice::from_raw_parts(ptr_and_len.0, ptr_and_len.1) };
28    std::str::from_utf8(slice).expect("should be valid utf8")
29}
30
31/// Return information about current picodata instance.
32pub fn instance_info() -> Result<InstanceInfo, BoxError> {
33    match unsafe { pico_ffi_instance_info() } {
34        RResult::ROk(info) => Ok(info),
35        RResult::RErr(_) => {
36            let error = BoxError::last();
37            Err(error)
38        }
39    }
40}
41
42/// Return information about RAFT protocol state.
43pub fn raft_info() -> types::RaftInfo {
44    unsafe { pico_ffi_raft_info() }
45}
46
47/// # Description
48///
49/// Tries to authenticate a user with specified password.
50/// Authentication method is determined via accessing `_pico_user`
51/// system table using `admin` session.
52///
53/// # FFI
54///
55/// Uses [`pico_ffi_authenticate`].
56///
57/// # Errors
58///
59/// - User was not found in the list of available users.
60/// - Authentication method was not initialized for the user.
61/// - Username length is greater than `u32`.
62/// - Password is not correct for the specified user.
63///
64/// # Panics
65///
66/// - Global Raft node is not initialized.
67/// - Authentication data is not set for the specified user.
68/// - Session of `admin` user is closed.
69/// - User `admin` is not found.
70/// - User `admin` does not have enough permissions.
71/// - Internal error on accessing underlying Tarantool space of `_pico_user` system table.
72pub fn authenticate(username: &str, password: impl AsRef<[u8]>) -> Result<(), BoxError> {
73    match unsafe { pico_ffi_authenticate(username.into(), password.as_ref().into()) } {
74        0 => Ok(()),
75        _ => {
76            let error = BoxError::last();
77            Err(error)
78        }
79    }
80}
81
82/// Dump the backtrace to a file to make debugging easier.
83/// This is also used in integration tests.
84fn dump_backtrace(msg: &str) -> Result<(), io::Error> {
85    let should_dump = env::var("PICODATA_INTERNAL_BACKTRACE_DUMP")
86        .map(|v| !v.is_empty())
87        .unwrap_or(false);
88
89    if !should_dump {
90        return Ok(());
91    }
92
93    let name = format!("picodata-{}.backtrace", process::id());
94    let path = env::current_dir()?.join(&name);
95
96    fs::write(&name, msg)
97        .map(|_| tarantool::say_info!("dumped panic backtrace to `{}`", path.display()))
98        .inspect_err(|e| tarantool::say_info!("{}", e))?;
99
100    Ok(())
101}
102
103#[inline]
104pub fn set_panic_hook() {
105    // NOTE: this function is called ASAP when starting up the process.
106    // Even if `say` isn't properly initialized yet, we
107    // still should be able to print a simplified line to stderr.
108    std::panic::set_hook(Box::new(|info| {
109        let version = crate::internal::picodata_version();
110
111        // Capture a backtrace regardless of RUST_BACKTRACE and such.
112        let backtrace = std::backtrace::Backtrace::force_capture();
113        let message = format!(
114            "Picodata {version}\n\n{info}\n\nbacktrace:\n{backtrace}\naborting due to panic"
115        );
116
117        // Dump backtrace to logs and file if needed
118        tarantool::say_crit!("\n\n{message}");
119        dump_backtrace(&message)
120            .unwrap_or_else(|e| tarantool::say_info!("Failed to dump panic backtrace: {}", e));
121
122        std::process::abort();
123    }));
124}
125
126#[derive(thiserror::Error, Debug)]
127pub enum InternalError {
128    #[error("timeout")]
129    Timeout,
130    #[error("internal error: {0}")]
131    Any(BoxError),
132}