pub struct WorkloadContext(/* private fields */);Expand description
Wrapper around the C FDBWorkloadContext
Implementations§
Source§impl WorkloadContext
impl WorkloadContext
Sourcepub fn trace<S, S2, S3>(
&self,
severity: Severity,
name: S,
details: &[(S2, S3)],
)
pub fn trace<S, S2, S3>( &self, severity: Severity, name: S, details: &[(S2, S3)], )
Add a log entry in the FoundationDB logs
Examples found in repository?
examples/noop/lib.rs (lines 15-19)
13 async fn setup(&mut self, _db: SimDatabase) {
14 println!("rust_setup({}_{})", self.name, self.client_id);
15 self.context.trace(
16 Severity::Debug,
17 "Test",
18 &[("Layer", "Rust"), ("Stage", "Setup")],
19 );
20 }
21 async fn start(&mut self, _db: SimDatabase) {
22 println!("rust_start({}_{})", self.name, self.client_id);
23 self.context.trace(
24 Severity::Debug,
25 "Test",
26 &[("Layer", "Rust"), ("Stage", "Start")],
27 );
28 }
29 async fn check(&mut self, _db: SimDatabase) {
30 println!("rust_check({}_{})", self.name, self.client_id);
31 self.context.trace(
32 Severity::Debug,
33 "Test",
34 &[("Layer", "Rust"), ("Stage", "Check")],
35 );
36 }More examples
examples/atomic/lib.rs (lines 62-69)
43 async fn start(&mut self, db: SimDatabase) {
44 println!("rust_start({})", self.client_id);
45 // Only use a single client
46 if self.client_id == 0 {
47 for _ in 0..self.expected_count {
48 let trx = db.create_trx().expect("Could not create transaction");
49
50 // Enable idempotent txn
51 trx.set_option(TransactionOption::AutomaticIdempotency)
52 .expect("could not setup automatic idempotency");
53
54 let buf: [u8; 8] = 1i64.to_le_bytes();
55
56 trx.atomic_op(&Subspace::all().pack(&COUNT_KEY), &buf, MutationType::Add);
57
58 match trx.commit().await {
59 Ok(_) => self.success_count += 1,
60 Err(err) => {
61 if err.is_maybe_committed() {
62 self.context.trace(
63 Severity::Warn,
64 "Detected an maybe_committed transactions with idempotency",
65 details![
66 "Layer" => "Rust",
67 "Client" => self.client_id
68 ],
69 );
70 self.maybe_committed_count += 1;
71 } else {
72 self.error_count += 1;
73 }
74 }
75 }
76
77 self.context.trace(
78 Severity::Info,
79 "Successfully setup workload",
80 details![
81 "Layer" => "Rust",
82 "Client" => self.client_id
83 ],
84 );
85 }
86 }
87 }
88 async fn check(&mut self, db: SimDatabase) {
89 println!("rust_check({})", self.client_id);
90 if self.client_id == 0 {
91 // even if buggify is off in checks, transactions can failed because of the randomized knob,
92 // so we need to wrap the check in a db.run
93 let count = db
94 .run(|trx, _maybe_committed| async move {
95 match trx.get(&Subspace::all().pack(&COUNT_KEY), true).await {
96 Err(e) => Err(FdbBindingError::from(e)),
97 Ok(None) => Ok(0),
98 Ok(Some(byte_count)) => {
99 let count = i64::from_le_bytes(byte_count[..8].try_into().unwrap());
100 Ok(count as usize)
101 }
102 }
103 })
104 .await
105 .expect("could not check using db.run");
106
107 if self.success_count == count {
108 self.context.trace(
109 Severity::Info,
110 "Atomic count match",
111 details![
112 "Layer" => "Rust",
113 "Client" => self.client_id,
114 "Expected" => self.expected_count,
115 "Found" => count,
116 "CommittedCount" => self.success_count,
117 "MaybeCommitted" => self.maybe_committed_count,
118 ],
119 );
120 } else {
121 self.context.trace(
122 Severity::Error,
123 "Atomic count doesn't match",
124 details![
125 "Layer" => "Rust",
126 "Client" => self.client_id,
127 "Expected" => self.expected_count,
128 "Found" => count,
129 "CommittedCount" => self.success_count,
130 "MaybeCommitted" => self.maybe_committed_count,
131 ],
132 );
133 }
134 }
135 }Sourcepub fn get_process_id(&self) -> u64
pub fn get_process_id(&self) -> u64
Get the process id of the workload
Sourcepub fn set_process_id(&self, id: u64)
pub fn set_process_id(&self, id: u64)
Set the process id of the workload
Sourcepub fn get_option<T>(&self, name: &str) -> Option<T>where
T: FromStr,
pub fn get_option<T>(&self, name: &str) -> Option<T>where
T: FromStr,
Get the value of a parameter from the simulation config file
/!\ getting an option consumes it, following call on that option will return None
Examples found in repository?
More examples
examples/noop/lib.rs (line 70)
64 fn create(name: String, context: WorkloadContext) -> WrappedWorkload {
65 let client_id = context.client_id();
66 let client_count = context.client_count();
67 println!("RustWorkloadFactory::create({name})[{client_id}/{client_count}]");
68 println!(
69 "my_c_option: {:?}",
70 context.get_option::<String>("my_c_option")
71 );
72 println!(
73 "my_c_option: {:?}",
74 context.get_option::<String>("my_c_option")
75 );
76 match name.as_str() {
77 "NoopWorkload" => WrappedWorkload::new(NoopWorkload::new(name, client_id, context)),
78 _ => panic!("Unknown workload name: {name}"),
79 }
80 }Sourcepub fn client_id(&self) -> i32
pub fn client_id(&self) -> i32
Get the client id of the workload
Examples found in repository?
More examples
examples/noop/lib.rs (line 65)
64 fn create(name: String, context: WorkloadContext) -> WrappedWorkload {
65 let client_id = context.client_id();
66 let client_count = context.client_count();
67 println!("RustWorkloadFactory::create({name})[{client_id}/{client_count}]");
68 println!(
69 "my_c_option: {:?}",
70 context.get_option::<String>("my_c_option")
71 );
72 println!(
73 "my_c_option: {:?}",
74 context.get_option::<String>("my_c_option")
75 );
76 match name.as_str() {
77 "NoopWorkload" => WrappedWorkload::new(NoopWorkload::new(name, client_id, context)),
78 _ => panic!("Unknown workload name: {name}"),
79 }
80 }Sourcepub fn client_count(&self) -> i32
pub fn client_count(&self) -> i32
Get the client id of the workload
Examples found in repository?
examples/noop/lib.rs (line 66)
64 fn create(name: String, context: WorkloadContext) -> WrappedWorkload {
65 let client_id = context.client_id();
66 let client_count = context.client_count();
67 println!("RustWorkloadFactory::create({name})[{client_id}/{client_count}]");
68 println!(
69 "my_c_option: {:?}",
70 context.get_option::<String>("my_c_option")
71 );
72 println!(
73 "my_c_option: {:?}",
74 context.get_option::<String>("my_c_option")
75 );
76 match name.as_str() {
77 "NoopWorkload" => WrappedWorkload::new(NoopWorkload::new(name, client_id, context)),
78 _ => panic!("Unknown workload name: {name}"),
79 }
80 }Get a determinist 64-bit random number
Auto Trait Implementations§
impl Freeze for WorkloadContext
impl RefUnwindSafe for WorkloadContext
impl !Send for WorkloadContext
impl !Sync for WorkloadContext
impl Unpin for WorkloadContext
impl UnwindSafe for WorkloadContext
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more