refactor: Replace all uses of lazy_static with once_cell

Went through and remove all lazy_static uses with once_cell (while waiting for the project to compile). There are still dependencies using lazy_static so it is still in the crate graph but at least there isn't an explicit dependency on it (and it is easier to update to `std::lazy::Lazy` once that is stable).
pull/24376/head
Markus Westerlind 2022-06-29 13:27:43 +02:00
parent cfcc4b8426
commit edf3f08e81
13 changed files with 71 additions and 81 deletions

7
Cargo.lock generated
View File

@ -2206,12 +2206,12 @@ dependencies = [
"iox_catalog",
"iox_query",
"iox_time",
"lazy_static",
"metric",
"mutable_batch",
"mutable_batch_lp",
"object_store",
"observability_deps",
"once_cell",
"parking_lot 0.12.1",
"parquet_file",
"paste",
@ -3919,8 +3919,8 @@ dependencies = [
"datafusion 0.1.0",
"datafusion_util",
"itertools",
"lazy_static",
"observability_deps",
"once_cell",
"regex",
"regex-syntax",
"schema",
@ -4242,13 +4242,13 @@ dependencies = [
"hyper",
"iox_catalog",
"iox_time",
"lazy_static",
"metric",
"mutable_batch",
"mutable_batch_lp",
"mutable_batch_pb",
"object_store",
"observability_deps",
"once_cell",
"parking_lot 0.12.1",
"paste",
"predicate",
@ -5243,7 +5243,6 @@ dependencies = [
"http",
"hyper",
"influxdb_iox_client",
"lazy_static",
"nix 0.24.1",
"observability_deps",
"once_cell",

View File

@ -49,7 +49,7 @@ trace = { path = "../trace" }
[dev-dependencies]
assert_matches = "1.5.0"
bitflags = {version = "1.3.2"}
lazy_static = "1.4.0"
once_cell = "1"
paste = "1.0.7"
test_helpers = { path = "../test_helpers", features = ["future_timeout"] }
tokio-stream = {version = "0.1.9", default_features = false }

View File

@ -443,16 +443,15 @@ mod tests {
use iox_time::{SystemProvider, Time};
use metric::Metric;
use mutable_batch_lp::lines_to_batches;
use once_cell::sync::Lazy;
use std::sync::Arc;
use test_helpers::timeout::FutureTimeout;
use tokio::sync::{mpsc, oneshot};
use tokio_stream::wrappers::ReceiverStream;
use write_buffer::core::WriteBufferError;
lazy_static::lazy_static! {
static ref TEST_TIME: Time = SystemProvider::default().now();
static ref TEST_KAFKA_PARTITION: KafkaPartition = KafkaPartition::new(42);
}
static TEST_TIME: Lazy<Time> = Lazy::new(|| SystemProvider::default().now());
static TEST_KAFKA_PARTITION: Lazy<KafkaPartition> = Lazy::new(|| KafkaPartition::new(42));
static TEST_KAFKA_TOPIC: &str = "kafka_topic_name";
// Return a DmlWrite with the given namespace and a single table.

View File

@ -239,6 +239,7 @@ mod tests {
use iox_time::Time;
use metric::{Metric, MetricObserver, Observation};
use mutable_batch_lp::lines_to_batches;
use once_cell::sync::Lazy;
use trace::{ctx::SpanContext, span::SpanStatus, RingBufferTraceCollector, TraceCollector};
use crate::stream_handler::{
@ -253,16 +254,16 @@ mod tests {
static TEST_KAFKA_TOPIC: &str = "kafka_topic_name";
lazy_static::lazy_static! {
static ref TEST_TIME: Time = SystemProvider::default().now();
static TEST_TIME: Lazy<Time> = Lazy::new(|| SystemProvider::default().now());
/// The attributes assigned to the metrics emitted by the
/// instrumentation when using the above sequencer / kafka topic values.
static ref DEFAULT_ATTRS: Attributes = Attributes::from([
/// The attributes assigned to the metrics emitted by the
/// instrumentation when using the above sequencer / kafka topic values.
static DEFAULT_ATTRS: Lazy<Attributes> = Lazy::new(|| {
Attributes::from([
("kafka_partition", SEQUENCER_ID.to_string().into()),
("kafka_topic", TEST_KAFKA_TOPIC.into()),
]);
}
])
});
/// Return a DmlWrite with the given metadata and a single table.
fn make_write(meta: DmlMeta) -> DmlWrite {

View File

@ -10,8 +10,8 @@ arrow = { version = "16.0.0", features = ["prettyprint"] }
chrono = { version = "0.4", default-features = false }
datafusion = { path = "../datafusion" }
itertools = "0.10.2"
lazy_static = "1.4.0"
observability_deps = { path = "../observability_deps" }
once_cell = "1"
regex = "1"
regex-syntax = "0.6.26"
schema = { path = "../schema" }

View File

@ -11,6 +11,7 @@ use datafusion::{
physical_plan::ColumnarValue,
scalar::ScalarValue,
};
use once_cell::sync::Lazy;
/// The name of the regex_match UDF given to DataFusion.
pub(crate) const REGEX_MATCH_UDF_NAME: &str = "RegexMatch";
@ -18,33 +19,29 @@ pub(crate) const REGEX_MATCH_UDF_NAME: &str = "RegexMatch";
/// The name of the not_regex_match UDF given to DataFusion.
pub(crate) const REGEX_NOT_MATCH_UDF_NAME: &str = "RegexNotMatch";
lazy_static::lazy_static! {
/// Implementation of regexp_match
pub(crate) static ref REGEX_MATCH_UDF: Arc<ScalarUDF> = Arc::new(
create_udf(
REGEX_MATCH_UDF_NAME,
// takes two arguments: regex, pattern
vec![DataType::Utf8, DataType::Utf8],
Arc::new(DataType::Boolean),
Volatility::Stable,
regex_match_expr_impl(true),
)
);
}
/// Implementation of regexp_match
pub(crate) static REGEX_MATCH_UDF: Lazy<Arc<ScalarUDF>> = Lazy::new(|| {
Arc::new(create_udf(
REGEX_MATCH_UDF_NAME,
// takes two arguments: regex, pattern
vec![DataType::Utf8, DataType::Utf8],
Arc::new(DataType::Boolean),
Volatility::Stable,
regex_match_expr_impl(true),
))
});
lazy_static::lazy_static! {
/// Implementation of regexp_not_match
pub(crate) static ref REGEX_NOT_MATCH_UDF: Arc<ScalarUDF> = Arc::new(
create_udf(
REGEX_NOT_MATCH_UDF_NAME,
// takes two arguments: regex, pattern
vec![DataType::Utf8, DataType::Utf8],
Arc::new(DataType::Boolean),
Volatility::Stable,
regex_match_expr_impl(false),
)
);
}
/// Implementation of regexp_not_match
pub(crate) static REGEX_NOT_MATCH_UDF: Lazy<Arc<ScalarUDF>> = Lazy::new(|| {
Arc::new(create_udf(
REGEX_NOT_MATCH_UDF_NAME,
// takes two arguments: regex, pattern
vec![DataType::Utf8, DataType::Utf8],
Arc::new(DataType::Boolean),
Volatility::Stable,
regex_match_expr_impl(false),
))
});
/// Given a column containing string values and a single regex pattern,
/// `regex_match_expr` determines which values satisfy the pattern and which do

View File

@ -5,12 +5,11 @@ use datafusion::{
logical_expr::{AggregateUDF, ScalarUDF},
logical_plan::FunctionRegistry,
};
use once_cell::sync::Lazy;
use crate::{regex, window};
lazy_static::lazy_static! {
static ref REGISTRY: IOxFunctionRegistry = IOxFunctionRegistry::new();
}
static REGISTRY: Lazy<IOxFunctionRegistry> = Lazy::new(IOxFunctionRegistry::new);
/// Lookup for all DataFusion User Defined Functions used by IOx
#[derive(Debug)]
@ -52,5 +51,5 @@ impl FunctionRegistry for IOxFunctionRegistry {
/// Return a reference to the global function registry
pub(crate) fn instance() -> &'static IOxFunctionRegistry {
&REGISTRY
&*REGISTRY
}

View File

@ -15,6 +15,7 @@ use datafusion::{
prelude::*,
scalar::ScalarValue,
};
use once_cell::sync::Lazy;
use crate::group_by::WindowDuration;
@ -24,29 +25,27 @@ pub use datafusion::error::{DataFusionError, Result as DataFusionResult};
/// The name of the window_bounds UDF given to DataFusion.
pub(crate) const WINDOW_BOUNDS_UDF_NAME: &str = "WindowBounds";
lazy_static::lazy_static! {
/// Implementation of window_bounds
pub(crate) static ref WINDOW_BOUNDS_UDF: Arc<ScalarUDF> = Arc::new(
create_udf(
WINDOW_BOUNDS_UDF_NAME,
// takes 7 arguments (see [`window_bounds_udf`] for details)
vec![
TIME_DATA_TYPE(),
// encoded every
DataType::Utf8,
DataType::Int64,
DataType::Boolean,
// encoded offset
DataType::Utf8,
DataType::Int64,
DataType::Boolean,
],
Arc::new(TIME_DATA_TYPE()),
Volatility::Stable,
Arc::new(window_bounds_udf),
)
);
}
/// Implementation of window_bounds
pub(crate) static WINDOW_BOUNDS_UDF: Lazy<Arc<ScalarUDF>> = Lazy::new(|| {
Arc::new(create_udf(
WINDOW_BOUNDS_UDF_NAME,
// takes 7 arguments (see [`window_bounds_udf`] for details)
vec![
TIME_DATA_TYPE(),
// encoded every
DataType::Utf8,
DataType::Int64,
DataType::Boolean,
// encoded offset
DataType::Utf8,
DataType::Int64,
DataType::Boolean,
],
Arc::new(TIME_DATA_TYPE()),
Volatility::Stable,
Arc::new(window_bounds_udf),
))
});
/// Implement the window bounds function as a DataFusion UDF where
/// each of the two `WindowDuration` argument has been encoded as

View File

@ -41,7 +41,7 @@ write_summary = { path = "../write_summary" }
[dev-dependencies]
assert_matches = "1.5"
criterion = { version = "0.3.4", features = ["async_tokio", "html_reports"] }
lazy_static = "1.4.0"
once_cell = "1"
paste = "1.0.7"
pretty_assertions = "1.2.1"
rand = "0.8.3"

View File

@ -6,6 +6,7 @@ use data_types::DatabaseName;
use hashbrown::HashMap;
use iox_catalog::mem::MemCatalog;
use mutable_batch::MutableBatch;
use once_cell::sync::Lazy;
use router::{
dml_handlers::{DmlHandler, SchemaValidator},
namespace_cache::{MemoryNamespaceCache, ShardedCache},
@ -14,9 +15,7 @@ use schema::selection::Selection;
use std::{iter, sync::Arc};
use tokio::runtime::Runtime;
lazy_static::lazy_static! {
static ref NAMESPACE: DatabaseName<'static> = "bananas".try_into().unwrap();
}
static NAMESPACE: Lazy<DatabaseName<'static>> = Lazy::new(|| "bananas".try_into().unwrap());
fn runtime() -> Runtime {
tokio::runtime::Builder::new_current_thread()

View File

@ -258,11 +258,10 @@ mod tests {
use assert_matches::assert_matches;
use data_types::{ColumnType, KafkaTopicId, QueryPoolId, TimestampRange};
use iox_catalog::mem::MemCatalog;
use once_cell::sync::Lazy;
use std::sync::Arc;
lazy_static::lazy_static! {
static ref NAMESPACE: DatabaseName<'static> = "bananas".try_into().unwrap();
}
static NAMESPACE: Lazy<DatabaseName<'static>> = Lazy::new(|| "bananas".try_into().unwrap());
// Parse `lp` into a table-keyed MutableBatch map.
fn lp_to_writes(lp: &str) -> HashMap<String, MutableBatch> {

View File

@ -14,7 +14,6 @@ http = "0.2.8"
hyper = "0.14"
influxdb_iox_client = { path = "../influxdb_iox_client", features = ["flight", "format", "write_lp"] }
nix = "0.24"
lazy_static = "1.4.0"
observability_deps = { path = "../observability_deps" }
once_cell = { version = "1.12.0", features = ["parking_lot"] }
parking_lot = "0.12"

View File

@ -6,6 +6,7 @@ use http::Response;
use hyper::Body;
use influxdb_iox_client::write::generated_types::{TableBatch, WriteResponse};
use observability_deps::tracing::{debug, info};
use once_cell::sync::Lazy;
use std::{
sync::{Arc, Weak},
time::Instant,
@ -360,6 +361,4 @@ fn server_from_weak(server: Option<&Weak<TestServer>>) -> Option<Option<Arc<Test
}
}
lazy_static::lazy_static! {
static ref GLOBAL_SHARED_SERVERS: Mutex<Option<SharedServers>> = Mutex::new(None);
}
static GLOBAL_SHARED_SERVERS: Lazy<Mutex<Option<SharedServers>>> = Lazy::new(|| Mutex::new(None));