Skip to content

Commit 5f0c9eb

Browse files
committed
Comment out remote calls
1 parent 075c055 commit 5f0c9eb

3 files changed

Lines changed: 137 additions & 6 deletions

File tree

modules/tpcc/src/new_order.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::collections::HashMap;
22

33
use crate::{
44
district, find_customer_by_id, find_district, find_stock, find_warehouse, item, order_line, pack_order_key,
5-
remote::{call_remote_reducer, remote_warehouse_home},
5+
remote::{remote_warehouse_home, simulate_remote_call},
66
stock, District, Item, OrderLine, Stock, WarehouseId, DISTRICTS_PER_WAREHOUSE, TAX_SCALE,
77
};
88
use spacetimedb::{log_stopwatch::LogStopwatch, reducer, Identity, ReducerContext, SpacetimeType, Table, Timestamp};
@@ -183,12 +183,19 @@ fn call_remote_order_multiple_items_and_decrement_stock(
183183
remote_database_identity: Identity,
184184
input: OrderMultipleItemsInput,
185185
) -> Result<Vec<OrderItemOutput>, String> {
186-
call_remote_reducer(
186+
// call_remote_reducer(
187+
// ctx,
188+
// remote_database_identity,
189+
// "order_multiple_items_and_decrement_stocks",
190+
// &input,
191+
// )
192+
simulate_remote_call(
187193
ctx,
188194
remote_database_identity,
189195
"order_multiple_items_and_decrement_stocks",
190196
&input,
191-
)
197+
)?;
198+
Ok(simulated_remote_order_outputs(input))
192199
}
193200

194201
struct ProcessedNewOrderItem {
@@ -262,6 +269,29 @@ pub struct OrderMultipleItemsInput {
262269
terminal_warehouse: WarehouseId,
263270
}
264271

272+
fn simulated_remote_order_outputs(input: OrderMultipleItemsInput) -> Vec<OrderItemOutput> {
273+
input
274+
.lines
275+
.into_iter()
276+
.map(|line| simulated_remote_order_output(line, input.district))
277+
.collect()
278+
}
279+
280+
fn simulated_remote_order_output(line: NewOrderLineAndIndex, district: u8) -> OrderItemOutput {
281+
let stock_data = if line.line.item_id % 10 == 0 {
282+
"SIMULATED ORIGINAL STOCK"
283+
} else {
284+
"SIMULATED REMOTE STOCK"
285+
};
286+
287+
OrderItemOutput {
288+
s_dist: format!("REMOTE-D{district:02}"),
289+
s_data: stock_data.to_string(),
290+
updated_quantity: adjust_stock_quantity(100, line.line.quantity as i32),
291+
index: line.index,
292+
}
293+
}
294+
265295
#[reducer]
266296
fn order_multiple_items_and_decrement_stocks(
267297
ctx: &ReducerContext,

modules/tpcc/src/payment.rs

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
customer, district, find_district, find_warehouse, history,
3-
remote::{call_remote_reducer, remote_warehouse_home},
3+
remote::{remote_warehouse_home, simulate_remote_call},
44
resolve_customer, warehouse, Customer, CustomerSelector, District, History, Warehouse, MAX_C_DATA_LEN,
55
};
66
use spacetimedb::{
@@ -128,12 +128,19 @@ fn call_remote_resolve_and_update_customer_for_payment(
128128
remote_database_identity: Identity,
129129
request: &PaymentRequest,
130130
) -> Result<Customer, String> {
131-
call_remote_reducer(
131+
// call_remote_reducer(
132+
// ctx,
133+
// remote_database_identity,
134+
// "resolve_and_update_customer_for_payment",
135+
// request,
136+
// )
137+
simulate_remote_call(
132138
ctx,
133139
remote_database_identity,
134140
"resolve_and_update_customer_for_payment",
135141
request,
136-
)
142+
)?;
143+
Ok(simulated_remote_customer(request))
137144
}
138145

139146
#[procedure]
@@ -149,6 +156,53 @@ fn process_remote_payment(ctx: &mut ProcedureContext, request: PaymentRequest) -
149156
})
150157
}
151158

159+
fn simulated_remote_customer(request: &PaymentRequest) -> Customer {
160+
let (customer_id, customer_first, customer_last) = match &request.customer_selector {
161+
CustomerSelector::ById(customer_id) => (
162+
*customer_id,
163+
format!("Remote{customer_id}"),
164+
format!("Customer{customer_id}"),
165+
),
166+
CustomerSelector::ByLastName(last_name) => (
167+
simulated_customer_id_from_last_name(last_name),
168+
"Remote".to_string(),
169+
last_name.clone(),
170+
),
171+
};
172+
173+
Customer {
174+
customer_key: 0,
175+
c_w_id: request.customer_warehouse_id,
176+
c_d_id: request.customer_district_id,
177+
c_id: customer_id,
178+
c_first: customer_first,
179+
c_middle: "OE".to_string(),
180+
c_last: customer_last,
181+
c_street_1: "REMOTE".to_string(),
182+
c_street_2: "SIMULATED".to_string(),
183+
c_city: "REMOTE".to_string(),
184+
c_state: "RM".to_string(),
185+
c_zip: "000000000".to_string(),
186+
c_phone: "0000000000000000".to_string(),
187+
c_since: request.now,
188+
c_credit: "GC".to_string(),
189+
c_credit_lim_cents: 5_000_000,
190+
c_discount_bps: 0,
191+
c_balance_cents: -request.payment_amount_cents,
192+
c_ytd_payment_cents: request.payment_amount_cents,
193+
c_payment_cnt: 1,
194+
c_delivery_cnt: 0,
195+
c_data: "SIMULATED_REMOTE_PAYMENT".to_string(),
196+
}
197+
}
198+
199+
fn simulated_customer_id_from_last_name(last_name: &str) -> u32 {
200+
let hash = last_name.bytes().fold(0_u32, |acc, byte| {
201+
acc.wrapping_mul(31).wrapping_add(u32::from(byte))
202+
});
203+
(hash % crate::CUSTOMERS_PER_DISTRICT) + 1
204+
}
205+
152206
fn update_customer(tx: &ReducerContext, request: &PaymentRequest, customer: Customer) -> Customer {
153207
let mut updated_customer = Customer {
154208
c_balance_cents: customer.c_balance_cents - request.payment_amount_cents,

modules/tpcc/src/remote.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use spacetimedb::{
44
};
55
use spacetimedb_sats::bsatn;
66

7+
const SIMULATED_REMOTE_CALL_WORK: u32 = 1_000_000;
8+
79
/// For warehouses not managed by this database, stores the [`Identity`] of the remote database which manages that warehouse.
810
///
911
/// Will not have a row present for a warehouse managed by the local database.
@@ -41,6 +43,43 @@ pub fn remote_warehouse_home(ctx: &ReducerContext, warehouse_id: u32) -> Option<
4143
.map(|row| row.remote_database_home)
4244
}
4345

46+
pub fn simulate_remote_call<Args>(
47+
_ctx: &ReducerContext,
48+
database_ident: Identity,
49+
reducer_name: &str,
50+
args: &Args,
51+
) -> Result<(), String>
52+
where
53+
Args: SpacetimeType + Serialize,
54+
{
55+
let args = bsatn::to_vec(args).map_err(|e| {
56+
format!(
57+
"Failed to BSATN-serialize args for simulated remote reducer {reducer_name} on database {database_ident}: {e}"
58+
)
59+
})?;
60+
61+
// Hold the reducer busy to approximate a synchronous remote call while we debug the real remote-call path.
62+
let mut state = mix_remote_work(0x9E37_79B9_7F4A_7C15 ^ u64::try_from(args.len()).unwrap_or(u64::MAX));
63+
for byte in format!("{database_ident}:{reducer_name}").bytes() {
64+
state = mix_remote_work(state ^ u64::from(byte));
65+
}
66+
for &byte in &args {
67+
state = mix_remote_work(state ^ u64::from(byte));
68+
}
69+
70+
let rounds = SIMULATED_REMOTE_CALL_WORK.saturating_add(
71+
u32::try_from(args.len())
72+
.unwrap_or(u32::MAX)
73+
.saturating_mul(128),
74+
);
75+
for _ in 0..rounds {
76+
state = mix_remote_work(state);
77+
}
78+
std::hint::black_box(state);
79+
Ok(())
80+
}
81+
82+
#[allow(dead_code)]
4483
pub fn call_remote_reducer<Args, Output>(
4584
_ctx: &ReducerContext,
4685
database_ident: Identity,
@@ -62,3 +101,11 @@ where
62101
)
63102
})
64103
}
104+
105+
fn mix_remote_work(mut state: u64) -> u64 {
106+
state ^= state >> 33;
107+
state = state.wrapping_mul(0xFF51_AFD7_ED55_8CCD);
108+
state ^= state >> 33;
109+
state = state.wrapping_mul(0xC4CE_B9FE_1A85_EC53);
110+
state ^ (state >> 33)
111+
}

0 commit comments

Comments
 (0)