Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace once_cell with std::sync::{OnceLock,LazyLock} #827

Merged
merged 8 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ license-file = "LICENSE.txt"
[workspace.dependencies]
derive_builder = "0.20"
derive_more = { version = "1.0", features = ["constructor", "display", "from", "into", "debug"] }
once_cell = "1.16"
tonic = "0.12"
tonic-build = "0.12"
opentelemetry = { version = "0.24", features = ["metrics"] }
Expand Down
1 change: 0 additions & 1 deletion client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ http = "1.1.0"
http-body-util = "0.1"
hyper = { version = "1.4.1" }
hyper-util = "0.1.6"
once_cell = { workspace = true }
opentelemetry = { workspace = true, features = ["metrics"], optional = true }
parking_lot = "0.12"
prost-types = { workspace = true }
Expand Down
24 changes: 12 additions & 12 deletions client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use crate::{
};
use backoff::{exponential, ExponentialBackoff, SystemClock};
use http::{uri::InvalidUri, Uri};
use once_cell::sync::OnceCell;
use std::sync::OnceLock;
use parking_lot::RwLock;
use std::{
collections::HashMap,
Expand Down Expand Up @@ -546,17 +546,17 @@ impl Interceptor for ServiceCallInterceptor {
#[derive(Debug, Clone)]
pub struct TemporalServiceClient<T> {
svc: T,
workflow_svc_client: OnceCell<WorkflowServiceClient<T>>,
operator_svc_client: OnceCell<OperatorServiceClient<T>>,
cloud_svc_client: OnceCell<CloudServiceClient<T>>,
test_svc_client: OnceCell<TestServiceClient<T>>,
health_svc_client: OnceCell<HealthClient<T>>,
workflow_svc_client: OnceLock<WorkflowServiceClient<T>>,
operator_svc_client: OnceLock<OperatorServiceClient<T>>,
cloud_svc_client: OnceLock<CloudServiceClient<T>>,
test_svc_client: OnceLock<TestServiceClient<T>>,
health_svc_client: OnceLock<HealthClient<T>>,
}

/// We up the limit on incoming messages from server from the 4Mb default to 128Mb. If for
/// whatever reason this needs to be changed by the user, we support overriding it via env var.
fn get_decode_max_size() -> usize {
static _DECODE_MAX_SIZE: OnceCell<usize> = OnceCell::new();
static _DECODE_MAX_SIZE: OnceLock<usize> = OnceLock::new();
*_DECODE_MAX_SIZE.get_or_init(|| {
std::env::var("TEMPORAL_MAX_INCOMING_GRPC_BYTES")
.ok()
Expand All @@ -576,11 +576,11 @@ where
fn new(svc: T) -> Self {
Self {
svc,
workflow_svc_client: OnceCell::new(),
operator_svc_client: OnceCell::new(),
cloud_svc_client: OnceCell::new(),
test_svc_client: OnceCell::new(),
health_svc_client: OnceCell::new(),
workflow_svc_client: OnceLock::new(),
operator_svc_client: OnceLock::new(),
cloud_svc_client: OnceLock::new(),
test_svc_client: OnceLock::new(),
health_svc_client: OnceLock::new(),
}
}
/// Get the underlying workflow service client
Expand Down
1 change: 0 additions & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ hyper-util = { version = "0.1", features = ["server", "http1", "http2", "tokio"]
itertools = "0.13"
lru = "0.12"
mockall = "0.13"
once_cell = { workspace = true }
opentelemetry = { workspace = true, features = ["metrics"], optional = true }
opentelemetry_sdk = { version = "0.24", features = ["rt-tokio", "metrics"], optional = true }
opentelemetry-otlp = { version = "0.17", features = ["tokio", "metrics", "tls"], optional = true }
Expand Down
2 changes: 1 addition & 1 deletion core/src/abstractions/take_cell.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use parking_lot::Mutex;
use std::sync::atomic::{AtomicBool, Ordering};

/// Implements something a bit like a `OnceCell`, but starts already initialized and allows you
/// Implements something a bit like a `OnceLock`, but starts already initialized and allows you
/// to take everything out of it only once in a thread-safe way. This isn't optimized for super
/// fast-path usage.
pub(crate) struct TakeCell<T> {
Expand Down
4 changes: 2 additions & 2 deletions core/src/core_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
Worker,
};
use futures::FutureExt;
use once_cell::sync::Lazy;
use std::sync::LazyLock;
use std::time::Duration;
use temporal_sdk_core_api::Worker as WorkerTrait;
use temporal_sdk_core_protos::coresdk::workflow_completion::WorkflowActivationCompletion;
Expand Down Expand Up @@ -46,7 +46,7 @@ async fn after_shutdown_server_is_not_polled() {
}

// Better than cloning a billion arcs...
static BARR: Lazy<Barrier> = Lazy::new(|| Barrier::new(3));
static BARR: LazyLock<Barrier> = LazyLock::new(|| Barrier::new(3));

#[tokio::test]
async fn shutdown_interrupts_both_polls() {
Expand Down
6 changes: 3 additions & 3 deletions core/src/replay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
Worker,
};
use futures::{FutureExt, Stream, StreamExt};
use once_cell::sync::OnceCell;
use std::sync::OnceLock;
use parking_lot::Mutex;
use std::{
pin::Pin,
Expand Down Expand Up @@ -179,7 +179,7 @@ impl Stream for HistoryFeederStream {
pub(crate) struct Historator {
iter: Pin<Box<dyn Stream<Item = HistoryForReplay> + Send>>,
allow_stream: UnboundedReceiverStream<String>,
worker_closer: Arc<OnceCell<CancellationToken>>,
worker_closer: Arc<OnceLock<CancellationToken>>,
dat: Arc<Mutex<HistoratorDat>>,
replay_done_tx: UnboundedSender<String>,
}
Expand All @@ -192,7 +192,7 @@ impl Historator {
Self {
iter: Box::pin(histories.fuse()),
allow_stream: UnboundedReceiverStream::new(replay_done_rx),
worker_closer: Arc::new(OnceCell::new()),
worker_closer: Arc::new(OnceLock::new()),
dat,
replay_done_tx,
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/telemetry/otel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,9 @@ impl GaugeF64 for MemoryGauge<f64> {
}

fn default_resource_instance() -> &'static Resource {
use once_cell::sync::OnceCell;
use std::sync::OnceLock;

static INSTANCE: OnceCell<Resource> = OnceCell::new();
static INSTANCE: OnceLock<Resource> = OnceLock::new();
INSTANCE.get_or_init(|| {
let resource = Resource::default();
if resource.get(Key::from("service.name")) == Some(Value::from("unknown_service")) {
Expand Down
6 changes: 3 additions & 3 deletions core/src/worker/client/mocks.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use super::*;
use futures::Future;
use once_cell::sync::Lazy;
use std::sync::Arc;
use std::sync::LazyLock;
use temporal_client::SlotManager;

static DEFAULT_WORKERS_REGISTRY: Lazy<Arc<SlotManager>> =
Lazy::new(|| Arc::new(SlotManager::new()));
static DEFAULT_WORKERS_REGISTRY: LazyLock<Arc<SlotManager>> =
LazyLock::new(|| Arc::new(SlotManager::new()));

pub(crate) static DEFAULT_TEST_CAPABILITIES: &Capabilities = &Capabilities {
signal_and_query_header: true,
Expand Down
8 changes: 4 additions & 4 deletions core/src/worker/workflow/history_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
};
use futures::{future::BoxFuture, FutureExt, Stream, TryFutureExt};
use itertools::Itertools;
use once_cell::sync::Lazy;
use std::sync::LazyLock;
use std::{
collections::VecDeque,
fmt::Debug,
Expand All @@ -24,9 +24,9 @@ use temporal_sdk_core_protos::temporal::api::{
};
use tracing::Instrument;

static EMPTY_FETCH_ERR: Lazy<tonic::Status> =
Lazy::new(|| tonic::Status::unknown("Fetched empty history page"));
static EMPTY_TASK_ERR: Lazy<tonic::Status> = Lazy::new(|| {
static EMPTY_FETCH_ERR: LazyLock<tonic::Status> =
LazyLock::new(|| tonic::Status::unknown("Fetched empty history page"));
static EMPTY_TASK_ERR: LazyLock<tonic::Status> = LazyLock::new(|| {
tonic::Status::unknown("Received an empty workflow task with no queries or history")
});

Expand Down
12 changes: 6 additions & 6 deletions core/src/worker/workflow/machines/transition_coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! never ever be removed from behind `#[cfg(test)]` compilation.

use dashmap::{mapref::entry::Entry, DashMap, DashSet};
use once_cell::sync::Lazy;
use std::sync::LazyLock;
use std::{
path::PathBuf,
sync::{
Expand All @@ -16,11 +16,11 @@ use std::{
};

// During test we want to know about which transitions we've covered in state machines
static COVERED_TRANSITIONS: Lazy<DashMap<String, DashSet<CoveredTransition>>> =
Lazy::new(DashMap::new);
static COVERAGE_SENDER: Lazy<SyncSender<(String, CoveredTransition)>> =
Lazy::new(spawn_save_coverage_at_end);
static THREAD_HANDLE: Lazy<Mutex<Option<JoinHandle<()>>>> = Lazy::new(|| Mutex::new(None));
static COVERED_TRANSITIONS: LazyLock<DashMap<String, DashSet<CoveredTransition>>> =
LazyLock::new(DashMap::new);
static COVERAGE_SENDER: LazyLock<SyncSender<(String, CoveredTransition)>> =
LazyLock::new(spawn_save_coverage_at_end);
static THREAD_HANDLE: LazyLock<Mutex<Option<JoinHandle<()>>>> = LazyLock::new(|| Mutex::new(None));

#[derive(Eq, PartialEq, Hash, Debug)]
struct CoveredTransition {
Expand Down
1 change: 0 additions & 1 deletion test-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ base64 = "0.22"
bytes = "1.3"
futures = "0.3"
log = "0.4"
once_cell = { workspace = true }
parking_lot = "0.12"
prost = { workspace = true }
prost-types = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ pub async fn history_from_proto_binary(path_from_root: &str) -> Result<History,
Ok(History::decode(&*bytes)?)
}

static INTEG_TESTS_RT: once_cell::sync::OnceCell<CoreRuntime> = once_cell::sync::OnceCell::new();
static INTEG_TESTS_RT: std::sync::OnceLock<CoreRuntime> = std::sync::OnceLock::new();
pub fn init_integ_telem() -> &'static CoreRuntime {
INTEG_TESTS_RT.get_or_init(|| {
let telemetry_options = get_integ_telem_options();
Expand Down
4 changes: 2 additions & 2 deletions tests/integ_tests/update_tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::anyhow;
use assert_matches::assert_matches;
use futures_util::{future, future::join_all, StreamExt};
use once_cell::sync::Lazy;
use std::sync::OnceLock;
use std::{
sync::{
atomic::{AtomicBool, AtomicUsize, Ordering},
Expand Down Expand Up @@ -957,7 +957,7 @@ async fn worker_restarted_in_middle_of_update() {
let mut worker = starter.worker().await;
let client = starter.get_client().await;

static BARR: Lazy<Barrier> = Lazy::new(|| Barrier::new(2));
static BARR: OnceLock<Barrier> = Lazy::new(|| Barrier::new(2));
static ACT_RAN: AtomicBool = AtomicBool::new(false);
worker.register_wf(wf_name.to_owned(), |ctx: WfContext| async move {
ctx.update_handler(
Expand Down