From c639b0c16240311beec797eaf05bad97256f719c Mon Sep 17 00:00:00 2001 From: Anton Fetisov <a.fetisov@picodata.io> Date: Thu, 6 Feb 2025 15:15:29 +0300 Subject: [PATCH 1/2] refactor: remove usage of static mut in sbroad This fixes the `static-mut-refs` lint in sbroad. --- sbroad/sbroad-core/src/executor/engine.rs | 26 +++++++++---------- .../src/executor/engine/helpers.rs | 4 +-- .../src/executor/engine/helpers/proxy.rs | 13 +++++----- .../src/executor/engine/helpers/storage.rs | 24 +++++++++-------- 4 files changed, 33 insertions(+), 34 deletions(-) diff --git a/sbroad/sbroad-core/src/executor/engine.rs b/sbroad/sbroad-core/src/executor/engine.rs index e1faafc98f..2f091007e1 100644 --- a/sbroad/sbroad-core/src/executor/engine.rs +++ b/sbroad/sbroad-core/src/executor/engine.rs @@ -73,20 +73,18 @@ pub trait Metadata: Sized { pub fn get_builtin_functions() -> &'static [Function] { // Once lock is used because of concurrent access in tests. - static mut BUILTINS: OnceLock<Vec<Function>> = OnceLock::new(); - - unsafe { - BUILTINS.get_or_init(|| { - vec![ - Function::new_stable("to_date".into(), DerivedType::new(Type::Datetime), false), - Function::new_stable("to_char".into(), DerivedType::new(Type::String), false), - Function::new_stable("substr".into(), DerivedType::new(Type::String), true), - Function::new_stable("lower".into(), DerivedType::new(Type::String), true), - Function::new_stable("upper".into(), DerivedType::new(Type::String), true), - Function::new_stable("coalesce".into(), DerivedType::new(Type::Any), true), - ] - }) - } + static BUILTINS: OnceLock<Vec<Function>> = OnceLock::new(); + + BUILTINS.get_or_init(|| { + vec![ + Function::new_stable("to_date".into(), DerivedType::new(Type::Datetime), false), + Function::new_stable("to_char".into(), DerivedType::new(Type::String), false), + Function::new_stable("substr".into(), DerivedType::new(Type::String), true), + Function::new_stable("lower".into(), DerivedType::new(Type::String), true), + Function::new_stable("upper".into(), DerivedType::new(Type::String), true), + Function::new_stable("coalesce".into(), DerivedType::new(Type::Any), true), + ] + }) } pub trait StorageCache { diff --git a/sbroad/sbroad-core/src/executor/engine/helpers.rs b/sbroad/sbroad-core/src/executor/engine/helpers.rs index 4d14546a6d..2953f6ef92 100644 --- a/sbroad/sbroad-core/src/executor/engine/helpers.rs +++ b/sbroad/sbroad-core/src/executor/engine/helpers.rs @@ -1760,9 +1760,9 @@ pub struct UpdateArgs<'vtable_tuple> { pub fn eq_op() -> &'static Value { // Once lock is used because of concurrent access in tests. - static mut EQ: OnceLock<Value> = OnceLock::new(); + static EQ: OnceLock<Value> = OnceLock::new(); - unsafe { EQ.get_or_init(|| Value::String("=".into())) } + EQ.get_or_init(|| Value::String("=".into())) } /// Convert vtable tuple to tuple diff --git a/sbroad/sbroad-core/src/executor/engine/helpers/proxy.rs b/sbroad/sbroad-core/src/executor/engine/helpers/proxy.rs index 5a47a63919..4d7f6ebc5f 100644 --- a/sbroad/sbroad-core/src/executor/engine/helpers/proxy.rs +++ b/sbroad/sbroad-core/src/executor/engine/helpers/proxy.rs @@ -1,4 +1,3 @@ -use std::cell::OnceCell; use std::rc::Rc; use std::time::Duration; @@ -36,6 +35,10 @@ pub(crate) struct SqlCacheProxy { response: Rc<Channel<CacheResponse>>, } +thread_local! { + pub(crate) static SQL_CACHE_PROXY: SqlCacheProxy = SqlCacheProxy::new(); +} + fn proxy_start(rq: Rc<Channel<CacheRequest>>, rsp: Rc<Channel<CacheResponse>>) -> fiber::FiberId { fiber::Builder::new() .name("sql_cache") @@ -98,7 +101,8 @@ impl SqlCacheProxy { } fn send(&self, request: CacheRequest) -> Result<(), Error> { - fiber::wakeup(sql_cache_proxy().id); + let id = SQL_CACHE_PROXY.with(|proxy| proxy.id); + fiber::wakeup(id); if self.request.send(request).is_err() { if fiber::is_cancelled() { return Err(Error::Other("current fiber is cancelled".into())); @@ -138,8 +142,3 @@ impl SqlCacheProxy { } } } - -pub(crate) fn sql_cache_proxy() -> &'static SqlCacheProxy { - static mut PROXY: OnceCell<SqlCacheProxy> = OnceCell::new(); - unsafe { PROXY.get_or_init(SqlCacheProxy::new) } -} diff --git a/sbroad/sbroad-core/src/executor/engine/helpers/storage.rs b/sbroad/sbroad-core/src/executor/engine/helpers/storage.rs index 478393b5b6..84d90a6571 100644 --- a/sbroad/sbroad-core/src/executor/engine/helpers/storage.rs +++ b/sbroad/sbroad-core/src/executor/engine/helpers/storage.rs @@ -12,7 +12,7 @@ use tarantool::tuple::{Tuple, TupleBuffer}; use crate::backend::sql::space::ADMIN_ID; use crate::error; use crate::errors::SbroadError; -use crate::executor::engine::helpers::proxy::sql_cache_proxy; +use crate::executor::engine::helpers::proxy::SQL_CACHE_PROXY; use crate::executor::engine::helpers::table_name; use crate::executor::lru::DEFAULT_CAPACITY; use crate::ir::node::NodeId; @@ -84,11 +84,12 @@ impl StorageMetadata { } pub fn prepare(pattern: String) -> Result<Statement, SbroadError> { - let proxy = sql_cache_proxy(); - let stmt = proxy.prepare(pattern).map_err(|e| { - error!(Option::from("prepare"), &format!("{e:?}")); - SbroadError::from(e) - })?; + let stmt = SQL_CACHE_PROXY + .with(|proxy| proxy.prepare(pattern)) + .map_err(|e| { + error!(Option::from("prepare"), &format!("{e:?}")); + SbroadError::from(e) + })?; Ok(stmt) } @@ -99,11 +100,12 @@ pub fn unprepare( let (stmt, table_ids) = std::mem::take(entry); // Remove the statement from the instance cache. - let proxy = sql_cache_proxy(); - proxy.unprepare(stmt).map_err(|e| { - error!(Option::from("unprepare"), &format!("{e:?}")); - SbroadError::from(e) - })?; + SQL_CACHE_PROXY + .with(|proxy| proxy.unprepare(stmt)) + .map_err(|e| { + error!(Option::from("unprepare"), &format!("{e:?}")); + SbroadError::from(e) + })?; // Remove temporary tables from the instance. for node_id in table_ids { -- GitLab From e9619912c268a058a7e9e8f8ad900fbe049fe666 Mon Sep 17 00:00:00 2001 From: Anton Fetisov <a.fetisov@picodata.io> Date: Thu, 6 Feb 2025 15:16:55 +0300 Subject: [PATCH 2/2] chore: fix elided_named_lifetimes lint in sbroad --- sbroad/sbroad-core/src/ir/expression.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sbroad/sbroad-core/src/ir/expression.rs b/sbroad/sbroad-core/src/ir/expression.rs index 358f2f97fd..1e59d05f81 100644 --- a/sbroad/sbroad-core/src/ir/expression.rs +++ b/sbroad/sbroad-core/src/ir/expression.rs @@ -945,7 +945,7 @@ impl<'source> NewColumnsSource<'source> { } } - fn iter(&'source self) -> NewColumnSourceIterator { + fn iter(&'source self) -> NewColumnSourceIterator<'source> { <&Self as IntoIterator>::into_iter(self) } } -- GitLab