Skip to content

Commit f5e7901

Browse files
committed
fix tests
1 parent 931393f commit f5e7901

File tree

17 files changed

+503
-317
lines changed

17 files changed

+503
-317
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ The general theme of this release is performance improvement by eliminating thre
1515
* `BeeBuilder` and `FullBuilder` are intermediate types that generally should not be instantiated directly.
1616
* `beekeeper::bee::Queen::create` now takes `&self` rather than `&mut self`. There is a new type, `beekeeper::bee::QueenMut`, with a `create(&mut self)` method, and needs to be wrapped in a `beekeeper::bee::QueenCell` to implement the `Queen` trait. This enables the `Hive` to create new workers without locking in the case of a `Queen` that does not need mutable state.
1717
* `beekeeper::bee::Context` now takes a generic parameter that must be input type of the `Worker`.
18+
* `beekeeper::hive::Hive::try_into_husk` now has an `urgent` parameter to indicate whether queued tasks should be abandoned when shutting down the hive (`true`) or if they should be allowed to finish processing (`false`).
1819
* Features
1920
* Added the `TaskQueues` trait, which enables `Hive` to be specialized for different implementations of global (i.e., sending tasks from the `Hive` to worker threads) and local (i.e., worker thread-specific) queues.
2021
* `ChannelTaskQueues` implements the existing behavior, using a channel for sending tasks.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ name = "perf"
3939
harness = false
4040

4141
[features]
42-
default = ["affinity", "batching", "retry"]
42+
default = ["batching", "retry"]
4343
affinity = ["dep:core_affinity"]
4444
batching = []
4545
retry = []

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ There are multiple methods in each group that differ by how the task results (ca
108108
* The methods with the `_unordered` suffix instead return an unordered iterator, which may be
109109
more performant than the ordered iterator
110110
* The methods with the `_send` suffix accept a channel `Sender` and send the `Outcome`s to that
111-
channel as they are completed
111+
channel as they are completed.
112+
* Note that, for these methods, the `tx` parameter is of type `Borrow<Sender<_>>`, which allows you to pass in either a value or a reference. Passing a value causes the `Sender` to be dropped after the call, while passing a reference allows you to use the same `Sender` for multiple `_send` calls. Note that in the later case, you need to explicitly drop the sender (e.g., `drop(tx)`), pass it by value to the last `_send` call, or be careful about how you obtain outcomes from the `Receiver` as methods such as `recv` and `iter` will block until the `Sender` is dropped. You should *not* pass clones of the `Sender` to `_send` methods as this results in slightly worse performance and still has the requirement that you manually drop the original `Sender` value.
112113
* The methods with the `_store` suffix store the `Outcome`s in the `Hive`; these may be
113114
retrieved later using the [`Hive::take_stored()`](https://docs.rs/beekeeper/latest/beekeeper/hive/struct.Hive.html#method.take_stored) method, using
114115
one of the `remove*` methods (which requires

src/atomic.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ macro_rules! atomic_int {
142142
}
143143

144144
atomic!(bool);
145+
atomic_int!(u8);
145146
atomic_int!(u32);
146147
atomic_int!(u64);
147148
atomic_int!(usize);
@@ -458,6 +459,7 @@ mod tests {
458459
};
459460
}
460461

462+
test_numeric_type!(AtomicU8);
461463
test_numeric_type!(AtomicU32);
462464
test_numeric_type!(AtomicU64);
463465
test_numeric_type!(AtomicUsize);

src/bee/queen.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl<Q: QueenMut> From<Q> for QueenCell<Q> {
7878
/// type Output = u8;
7979
/// type Error = ();
8080
///
81-
/// fn apply(&mut self, input: u8, _: &Context) -> WorkerResult<Self> {
81+
/// fn apply(&mut self, input: u8, _: &Context<Self::Input>) -> WorkerResult<Self> {
8282
/// Ok(self.0.saturating_add(input))
8383
/// }
8484
/// }
@@ -88,7 +88,7 @@ impl<Q: QueenMut> From<Q> for QueenCell<Q> {
8888
/// impl Queen for MyQueen {
8989
/// type Kind = MyWorker;
9090
///
91-
/// fn create(&mut self) -> Self::Kind {
91+
/// fn create(&self) -> Self::Kind {
9292
/// MyWorker::default()
9393
/// }
9494
/// }

src/hive/builder/mod.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
//! There are a few different builder types. All builders implement the `BuilderConfig` trait,
2-
//! which provides methods to set configuration parameters.
1+
//! There are a few different builder types.
32
//!
43
//! * Open: has no type parameters; can only set config parameters. Has methods to create
54
//! typed builders.
@@ -13,6 +12,33 @@
1312
//! Bee /
1413
//! | /
1514
//! Full
15+
//!
16+
//! All builders implement the `BuilderConfig` trait, which provides methods to set configuration
17+
//! parameters. The configuration options available:
18+
//! * [`Builder::num_threads`]: number of worker threads that will be spawned by the built `Hive`.
19+
//! * [`Builder::with_default_num_threads`] will set `num_threads` to the global default value.
20+
//! * [`Builder::with_thread_per_core`] will set `num_threads` to the number of available CPU
21+
//! cores.
22+
//! * [`Builder::thread_name`]: thread name for each of the threads spawned by the built `Hive`. By
23+
//! default, threads are unnamed.
24+
//! * [`Builder::thread_stack_size`]: stack size (in bytes) for each of the threads spawned by the
25+
//! built `Hive`. See the
26+
//! [`std::thread`](https://doc.rust-lang.org/stable/std/thread/index.html#stack-size)
27+
//! documentation for details on the default stack size.
28+
//!
29+
//! The following configuration options are available when the `retry` feature is enabled:
30+
//! * [`Builder::max_retries`]: maximum number of times a `Worker` will retry an
31+
//! [`ApplyError::Retryable`](crate::bee::ApplyError#Retryable) before giving up.
32+
//! * [`Builder::retry_factor`]: [`Duration`](std::time::Duration) factor for exponential backoff
33+
//! when retrying an `ApplyError::Retryable` error.
34+
//! * [`Builder::with_default_retries`] sets the retry options to the global defaults, while
35+
//! [`Builder::with_no_retries`] disabled retrying.
36+
//!
37+
//! The following configuration options are available when the `affinity` feature is enabled:
38+
//! * [`Builder::core_affinity`]: List of CPU core indices to which the threads should be pinned.
39+
//! * [`Builder::with_default_core_affinity`] will set the list to all CPU core indices, though
40+
//! only the first `num_threads` indices will be used.
41+
//!
1642
mod bee;
1743
mod channel;
1844
mod full;

src/hive/builder/open.rs

Lines changed: 40 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,33 @@ use crate::hive::Config;
44

55
/// A builder for a [`Hive`](crate::hive::Hive).
66
///
7-
/// Calling [`Builder::new()`] creates an unconfigured `Builder`, while calling
8-
/// [`Builder::default()`] creates a `Builder` with fields preset to the global default values.
7+
/// Calling [`OpenBuilder::empty()`] creates an unconfigured `Builder`, while calling
8+
/// [`OpenBuilder::default()`] creates a `Builder` with fields preset to the global default values.
99
/// Global defaults can be changed using the
1010
/// [`beekeeper::hive::set_*_default`](crate::hive#functions) functions.
1111
///
12-
/// The configuration options available:
13-
/// * [`Builder::num_threads`]: number of worker threads that will be spawned by the built `Hive`.
14-
/// * [`Builder::with_default_num_threads`] will set `num_threads` to the global default value.
15-
/// * [`Builder::with_thread_per_core`] will set `num_threads` to the number of available CPU
16-
/// cores.
17-
/// * [`Builder::thread_name`]: thread name for each of the threads spawned by the built `Hive`. By
18-
/// default, threads are unnamed.
19-
/// * [`Builder::thread_stack_size`]: stack size (in bytes) for each of the threads spawned by the
20-
/// built `Hive`. See the
21-
/// [`std::thread`](https://doc.rust-lang.org/stable/std/thread/index.html#stack-size)
22-
/// documentation for details on the default stack size.
12+
/// See the [module documentation](crate::hive::builder) for details on the available configuration
13+
/// options.
2314
///
24-
/// The following configuration options are available when the `retry` feature is enabled:
25-
/// * [`Builder::max_retries`]: maximum number of times a `Worker` will retry an
26-
/// [`ApplyError::Retryable`](crate::bee::ApplyError#Retryable) before giving up.
27-
/// * [`Builder::retry_factor`]: [`Duration`](std::time::Duration) factor for exponential backoff
28-
/// when retrying an `ApplyError::Retryable` error.
29-
/// * [`Builder::with_default_retries`] sets the retry options to the global defaults, while
30-
/// [`Builder::with_no_retries`] disabled retrying.
15+
/// This builder needs to be specialized to both the `Queen` and `TaskQueues` types. You can do
16+
/// this in either order.
3117
///
32-
/// The following configuration options are available when the `affinity` feature is enabled:
33-
/// * [`Builder::core_affinity`]: List of CPU core indices to which the threads should be pinned.
34-
/// * [`Builder::with_default_core_affinity`] will set the list to all CPU core indices, though
35-
/// only the first `num_threads` indices will be used.
36-
///
37-
/// To create the [`Hive`], call one of the `build*` methods:
38-
/// * [`Builder::build`] requires a [`Queen`] instance.
39-
/// * [`Builder::build_default`] requires a [`Queen`] type that implements [`Default`].
40-
/// * [`Builder::build_with`] requires a [`Worker`] instance that implements [`Clone`].
41-
/// * [`Builder::build_with_default`] requires a [`Worker`] type that implements [`Default`].
18+
/// * Calling one of the `with_queen*` methods returns a `BeeBuilder` specialized to a `Queen`.
19+
/// * Calling `with_worker` or `with_worker_default` returns a `BeeBuilder` specialized to a
20+
/// `CloneQueen` or `DefaultQueen` (respectively) for a specific `Worker` type.
21+
/// * Calling `with_channel_queues` or `with_workstealing_queues` returns a `ChannelBuilder` or
22+
/// `WorkstealingBuilder` specialized to a `TaskQueues` type.
4223
///
4324
/// # Examples
4425
///
4526
/// Build a [`Hive`] that uses a maximum of eight threads simultaneously and each thread has
4627
/// a 8 MB stack size:
4728
///
4829
/// ```
30+
/// # use beekeeper::hive::{Builder, OpenBuilder};
4931
/// type MyWorker = beekeeper::bee::stock::ThunkWorker<()>;
5032
///
51-
/// let hive = beekeeper::hive::Builder::empty()
33+
/// let hive = OpenBuilder::empty()
5234
/// .num_threads(8)
5335
/// .thread_stack_size(8_000_000)
5436
/// .with_worker_default::<MyWorker>()
@@ -70,8 +52,8 @@ impl OpenBuilder {
7052
/// # Examples
7153
///
7254
/// ```
73-
/// # use beekeeper::hive::{Builder, Hive};
74-
/// # use beekeeper::bee::{Context, Queen, Worker, WorkerResult};
55+
/// # use beekeeper::hive::{Builder, ChannelBuilder, Hive};
56+
/// # use beekeeper::bee::{Context, QueenMut, Worker, WorkerResult};
7557
///
7658
/// #[derive(Debug)]
7759
/// struct CounterWorker {
@@ -95,7 +77,7 @@ impl OpenBuilder {
9577
/// type Output = String;
9678
/// type Error = ();
9779
///
98-
/// fn apply(&mut self, input: Self::Input, _: &Context) -> WorkerResult<Self> {
80+
/// fn apply(&mut self, input: Self::Input, _: &Context<usize>) -> WorkerResult<Self> {
9981
/// self.input_count += 1;
10082
/// self.input_sum += input;
10183
/// let s = format!(
@@ -111,7 +93,7 @@ impl OpenBuilder {
11193
/// num_workers: usize
11294
/// }
11395
///
114-
/// impl Queen for CounterQueen {
96+
/// impl QueenMut for CounterQueen {
11597
/// type Kind = CounterWorker;
11698
///
11799
/// fn create(&mut self) -> Self::Kind {
@@ -121,16 +103,17 @@ impl OpenBuilder {
121103
/// }
122104
///
123105
/// # fn main() {
124-
/// let hive = Builder::new()
106+
/// let hive = ChannelBuilder::empty()
125107
/// .num_threads(8)
126108
/// .thread_stack_size(4_000_000)
127-
/// .build(CounterQueen::default());
109+
/// .with_queen_mut_default::<CounterQueen>()
110+
/// .build();
128111
///
129112
/// for i in 0..100 {
130113
/// hive.apply_store(i);
131114
/// }
132-
/// let husk = hive.try_into_husk().unwrap();
133-
/// assert_eq!(husk.queen().num_workers, 8);
115+
/// let husk = hive.try_into_husk(false).unwrap();
116+
/// assert_eq!(husk.queen().get().num_workers, 8);
134117
/// # }
135118
/// ```
136119
pub fn with_queen<Q: Queen, I: Into<Q>>(self, queen: I) -> BeeBuilder<Q> {
@@ -155,7 +138,7 @@ impl OpenBuilder {
155138
/// # Examples
156139
///
157140
/// ```
158-
/// # use beekeeper::hive::{Builder, OutcomeIteratorExt};
141+
/// # use beekeeper::hive::{Builder, ChannelBuilder, OutcomeIteratorExt};
159142
/// # use beekeeper::bee::{Context, Worker, WorkerResult};
160143
///
161144
/// #[derive(Debug, Clone)]
@@ -173,24 +156,25 @@ impl OpenBuilder {
173156
/// type Output = isize;
174157
/// type Error = ();
175158
///
176-
/// fn apply(&mut self, input: Self::Input, _: &Context) -> WorkerResult<Self> {
159+
/// fn apply(&mut self, input: Self::Input, _: &Context<Self::Input>) -> WorkerResult<Self> {
177160
/// let (operand, operator) = input;
178161
/// let value = match operator % 4 {
179-
/// 0 => operand + self.config(Token),
180-
/// 1 => operand - self.config(Token),
181-
/// 2 => operand * self.config(Token),
182-
/// 3 => operand / self.config(Token),
162+
/// 0 => operand + self.0,
163+
/// 1 => operand - self.0,
164+
/// 2 => operand * self.0,
165+
/// 3 => operand / self.0,
183166
/// _ => unreachable!(),
184167
/// };
185168
/// Ok(value)
186169
/// }
187170
/// }
188171
///
189172
/// # fn main() {
190-
/// let hive = Builder::new()
173+
/// let hive = ChannelBuilder::empty()
191174
/// .num_threads(8)
192175
/// .thread_stack_size(4_000_000)
193-
/// .build_with(MathWorker(5isize));
176+
/// .with_worker(MathWorker(5isize))
177+
/// .build();
194178
///
195179
/// let sum: isize = hive
196180
/// .map((0..100).zip((0..4).cycle()))
@@ -212,7 +196,7 @@ impl OpenBuilder {
212196
/// # Examples
213197
///
214198
/// ```
215-
/// # use beekeeper::hive::{Builder, OutcomeIteratorExt};
199+
/// # use beekeeper::hive::{Builder, ChannelBuilder, OutcomeIteratorExt};
216200
/// # use beekeeper::bee::{Context, Worker, WorkerResult};
217201
/// # use std::num::NonZeroIsize;
218202
///
@@ -224,24 +208,25 @@ impl OpenBuilder {
224208
/// type Output = isize;
225209
/// type Error = ();
226210
///
227-
/// fn apply(&mut self, input: Self::Input, _: &Context) -> WorkerResult<Self> {
211+
/// fn apply(&mut self, input: Self::Input, _: &Context<Self::Input>) -> WorkerResult<Self> {
228212
/// let (operand, operator) = input;
229213
/// let result = match operator % 4 {
230-
/// 0 => self.config(Token) + operand.get(),
231-
/// 1 => self.config(Token) - operand.get(),
232-
/// 2 => self.config(Token) * operand.get(),
233-
/// 3 => self.config(Token) / operand.get(),
214+
/// 0 => self.0 + operand.get(),
215+
/// 1 => self.0 - operand.get(),
216+
/// 2 => self.0 * operand.get(),
217+
/// 3 => self.0 / operand.get(),
234218
/// _ => unreachable!(),
235219
/// };
236220
/// Ok(result)
237221
/// }
238222
/// }
239223
///
240224
/// # fn main() {
241-
/// let hive = Builder::new()
225+
/// let hive = ChannelBuilder::empty()
242226
/// .num_threads(8)
243227
/// .thread_stack_size(4_000_000)
244-
/// .build_with_default::<MathWorker>();
228+
/// .with_worker_default::<MathWorker>()
229+
/// .build();
245230
///
246231
/// let sum: isize = hive
247232
/// .map((1..=100).map(|i| NonZeroIsize::new(i).unwrap()).zip((0..4).cycle()))

0 commit comments

Comments
 (0)