1use std::{collections::VecDeque, fmt::Debug, ops::ControlFlow, pin::Pin, time::Duration};
17
18use ahash::AHashMap;
19use bytes::Bytes;
20#[cfg(feature = "defi")]
21use nautilus_common::defi::cache::InstrumentMigration;
22use nautilus_common::{
23 cache::{
24 CacheConfig,
25 database::{
26 CacheDatabaseAdapter, CacheDatabaseFactory, CacheMap, register_loaded_currencies,
27 },
28 },
29 live::{block_on_nautilus_with, get_runtime},
30 logging::{log_task_awaiting, log_task_started, log_task_stopped},
31 signal::Signal,
32};
33use nautilus_core::{UUID4, UnixNanos};
34use nautilus_model::{
35 accounts::AccountAny,
36 data::{
37 Bar, CustomData, DataType, FundingRateUpdate, InstrumentClose, QuoteTick, TradeTick,
38 greeks::{GreeksData, YieldCurve},
39 },
40 events::{
41 AccountState, OrderEventAny, OrderFilled, OrderInitialized, OrderSnapshot,
42 position::snapshot::PositionSnapshot,
43 },
44 identifiers::{
45 AccountId, ActorId, ClientId, ClientOrderId, InstrumentId, PositionId, StrategyId,
46 TraderId, VenueOrderId,
47 },
48 instruments::{Instrument, InstrumentAny, SyntheticInstrument},
49 orderbook::OrderBook,
50 orders::{Order, OrderAny},
51 position::Position,
52 types::{Currency, Money},
53};
54use serde::{Deserialize, Serialize};
55use sqlx::{PgPool, postgres::PgConnectOptions};
56use tokio::{time::Instant, try_join};
57use ustr::Ustr;
58
59use crate::sql::{
60 pg::{connect_pg, get_postgres_connect_options},
61 queries::{CACHE_DATABASE_KEY_PREFIX, DatabaseQueries, trader_key_prefix},
62};
63
64const CACHE_PROCESS: &str = "cache-process";
66const SCHEMA_MIGRATION_COMMAND: &str = "run `nautilus database init` to migrate";
67
68#[cfg_attr(
72 feature = "python",
73 expect(
74 clippy::unsafe_derive_deserialize,
75 reason = "config deserializes plain fields; unsafe methods come from generated PyO3 integration"
76 )
77)]
78#[derive(Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
79#[serde(default, deny_unknown_fields)]
80#[cfg_attr(
81 feature = "python",
82 pyo3::pyclass(module = "nautilus_trader.infrastructure", from_py_object)
83)]
84#[cfg_attr(
85 feature = "python",
86 pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.infrastructure")
87)]
88pub struct PostgresCacheConfig {
89 pub host: Option<String>,
91 pub port: Option<u16>,
93 pub username: Option<String>,
95 pub password: Option<String>,
97 pub database: Option<String>,
99}
100
101impl Debug for PostgresCacheConfig {
102 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
103 let redacted = self.password.as_ref().map(|_| "***");
104 f.debug_struct(stringify!(PostgresCacheConfig))
105 .field("host", &self.host)
106 .field("port", &self.port)
107 .field("username", &self.username)
108 .field("password", &redacted)
109 .field("database", &self.database)
110 .finish()
111 }
112}
113
114#[cfg(test)]
115mod tests {
116 use nautilus_common::cache::Cache;
117 use nautilus_model::enums::BookType;
118 use rstest::rstest;
119 use serde_json::json;
120
121 use super::*;
122 #[cfg(feature = "defi")]
123 use crate::defi::IntegrityError;
124
125 #[rstest]
126 fn test_default_postgres_cache_config() {
127 let config = PostgresCacheConfig::default();
128
129 assert_eq!(config.host, None);
130 assert_eq!(config.port, None);
131 assert_eq!(config.username, None);
132 assert_eq!(config.password, None);
133 assert_eq!(config.database, None);
134 }
135
136 #[rstest]
137 fn test_deserialize_postgres_cache_config() {
138 let config_json = json!({
139 "host": "localhost",
140 "port": 5432,
141 "username": "user",
142 "password": "pass",
143 "database": "nautilus"
144 });
145
146 let config: PostgresCacheConfig = serde_json::from_value(config_json).unwrap();
147
148 assert_eq!(config.host, Some("localhost".to_string()));
149 assert_eq!(config.port, Some(5432));
150 assert_eq!(config.username, Some("user".to_string()));
151 assert_eq!(config.password, Some("pass".to_string()));
152 assert_eq!(config.database, Some("nautilus".to_string()));
153 }
154
155 #[cfg(feature = "defi")]
156 #[tokio::test]
157 async fn test_defi_write_closed_channel_preserves_transport_error() {
158 let pool = sqlx::postgres::PgPoolOptions::new()
159 .connect_lazy_with(PgConnectOptions::new().host("127.0.0.1").port(1));
160 pool.close().await;
161 let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
162 drop(rx);
163
164 let handle = tokio::spawn(async {});
165
166 let database = PostgresCacheDatabase {
167 pool,
168 trader_id: TraderId::from("TRADER-001"),
169 tx,
170 handle,
171 };
172
173 let error = database
174 .write_defi(|reply| DatabaseQuery::DefiCurrency(Currency::USD(), reply))
175 .unwrap_err();
176 assert!(!error.is::<IntegrityError>());
177 assert_eq!(error.to_string(), "channel closed");
178 }
179
180 #[tokio::test]
181 async fn test_unsupported_order_book_persistence_keeps_memory_current() {
182 let pool = sqlx::postgres::PgPoolOptions::new()
183 .connect_lazy_with(PgConnectOptions::new().host("127.0.0.1").port(1));
184 pool.close().await;
185 let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
186 drop(rx);
187
188 let handle = tokio::spawn(async {});
189
190 let database = PostgresCacheDatabase {
191 pool,
192 trader_id: TraderId::from("TRADER-001"),
193 tx,
194 handle,
195 };
196
197 let instrument_id = InstrumentId::from("AUD/USD.SIM");
198 let book_type = BookType::L2_MBP;
199 let book = OrderBook::new(instrument_id, book_type);
200 let error = database.add_order_book(&book).unwrap_err();
201
202 let mut cache = Cache::new(
203 Some(CacheConfig {
204 save_market_data: true,
205 ..Default::default()
206 }),
207 Some(Box::new(database)),
208 );
209
210 cache.add_order_book(book).unwrap();
211 let book = cache.order_book(&instrument_id).unwrap();
212
213 assert_eq!(
214 error.to_string(),
215 "Postgres order book persistence is not supported"
216 );
217 assert_eq!(book.instrument_id, instrument_id);
218 assert_eq!(book.book_type, book_type);
219 }
220
221 #[tokio::test]
222 async fn test_check_account_ownership_propagates_query_failure() {
223 let pool = sqlx::postgres::PgPoolOptions::new()
224 .connect_lazy_with(PgConnectOptions::new().host("127.0.0.1").port(1));
225 pool.close().await;
226
227 let error = check_account_ownership(&pool).await.unwrap_err();
228
229 assert!(
230 error
231 .to_string()
232 .contains("Failed to check account event ownership"),
233 "was: {error}"
234 );
235 }
236
237 #[rstest]
238 fn test_deserialize_postgres_cache_config_rejects_type_selector() {
239 let config_json = json!({
240 "type": "postgres",
241 });
242
243 let error = serde_json::from_value::<PostgresCacheConfig>(config_json).unwrap_err();
244
245 assert!(error.to_string().contains("unknown field `type`"));
246 }
247}
248
249#[async_trait::async_trait]
250impl CacheDatabaseFactory for PostgresCacheConfig {
251 async fn create(
252 &self,
253 trader_id: TraderId,
254 _instance_id: UUID4,
255 _config: CacheConfig,
256 ) -> anyhow::Result<Box<dyn CacheDatabaseAdapter>> {
257 let database = PostgresCacheDatabase::connect(self.clone(), trader_id).await?;
258 Ok(Box::new(database))
259 }
260}
261
262#[derive(Debug)]
263pub struct PostgresCacheDatabase {
264 pub pool: PgPool,
265 trader_id: TraderId,
266 tx: tokio::sync::mpsc::UnboundedSender<DatabaseQuery>,
267 handle: tokio::task::JoinHandle<()>,
268}
269
270#[allow(
271 clippy::large_enum_variant,
272 reason = "variant sizes vary with feature unification; allow stays silent when the lint does not fire"
273)]
274#[derive(Debug, Clone)]
275pub enum DatabaseQuery {
276 #[cfg(feature = "defi")]
277 DefiCurrency(Currency, std::sync::mpsc::SyncSender<anyhow::Result<()>>),
278 #[cfg(feature = "defi")]
279 DefiInstrument(
280 InstrumentAny,
281 Option<InstrumentMigration>,
282 std::sync::mpsc::SyncSender<anyhow::Result<()>>,
283 ),
284 Close,
285 Add(String, Vec<u8>),
286 Delete(String),
287 DeleteOrder(ClientOrderId),
288 DeletePosition(PositionId),
289 AddCurrency(Currency),
290 AddInstrument(InstrumentAny),
291 AddInstrumentClose(InstrumentClose),
292 AddOrder(OrderInitialized, Option<ClientId>),
293 AddOrderSnapshot(OrderSnapshot),
294 AddPosition(PositionId, OrderFilled),
295 AddPositionSnapshot(PositionSnapshot),
296 AddAccount(AccountState, bool),
297 AddSignal(Signal),
298 AddCustom(CustomData),
299 AddQuote(QuoteTick),
300 AddTrade(TradeTick),
301 AddBar(Bar),
302 AddGreeks(GreeksData),
303 AddYieldCurve(YieldCurve),
304 UpdateOrder(OrderEventAny),
305 UpdatePosition(OrderFilled),
306 IndexOrderPosition(ClientOrderId, PositionId),
307 IndexOrderClients(Vec<(ClientOrderId, ClientId)>),
308}
309
310impl PostgresCacheDatabase {
311 pub async fn connect(
326 config: PostgresCacheConfig,
327 trader_id: TraderId,
328 ) -> Result<Self, sqlx::Error> {
329 let pg_connect_options = get_postgres_connect_options(
330 config.host,
331 config.port,
332 config.username,
333 config.password,
334 config.database,
335 );
336 let pool = connect_pg(pg_connect_options.clone().into()).await.unwrap();
337 check_schema_migrated(&pool).await?;
338 check_account_ownership(&pool).await?;
339 let (tx, rx) = tokio::sync::mpsc::unbounded_channel::<DatabaseQuery>();
340
341 let handle = get_runtime().spawn(async move {
342 Box::pin(Self::process_commands(
343 rx,
344 pg_connect_options.clone().into(),
345 trader_id,
346 ))
347 .await;
348 });
349 Ok(Self {
350 pool,
351 trader_id,
352 tx,
353 handle,
354 })
355 }
356
357 #[cfg(feature = "defi")]
358 fn write_defi(
359 &self,
360 query: impl FnOnce(std::sync::mpsc::SyncSender<anyhow::Result<()>>) -> DatabaseQuery,
361 ) -> anyhow::Result<()> {
362 let (reply, receiver) = std::sync::mpsc::sync_channel(1);
363 self.tx.send(query(reply))?;
364 let on_runtime = tokio::runtime::Handle::try_current()
365 .is_ok_and(|handle| handle.id() == get_runtime().handle().id());
366
367 if on_runtime {
368 tokio::task::block_in_place(|| receiver.recv())?
369 } else {
370 receiver.recv()?
371 }
372 }
373
374 #[must_use]
376 pub const fn trader_id(&self) -> TraderId {
377 self.trader_id
378 }
379
380 async fn process_commands(
381 mut rx: tokio::sync::mpsc::UnboundedReceiver<DatabaseQuery>,
382 pg_connect_options: PgConnectOptions,
383 trader_id: TraderId,
384 ) {
385 log_task_started(CACHE_PROCESS);
386
387 let pool = connect_pg(pg_connect_options).await.unwrap();
388
389 let mut buffer: VecDeque<DatabaseQuery> = VecDeque::new();
391
392 let buffer_interval = Duration::from_millis(0);
394
395 let flush_timer = tokio::time::sleep(buffer_interval);
399 tokio::pin!(flush_timer);
400
401 loop {
403 tokio::select! {
404 maybe_msg = rx.recv() => {
405 let result = Box::pin(handle_query(
406 maybe_msg,
407 &mut buffer,
408 buffer_interval,
409 &pool,
410 &trader_id,
411 ))
412 .await;
413
414 if result.is_break() {
415 break;
416 }
417 }
418 () = &mut flush_timer, if !buffer_interval.is_zero() => {
419 flush_buffer(
420 &mut buffer,
421 &pool,
422 &trader_id,
423 &mut flush_timer,
424 buffer_interval,
425 )
426 .await;
427 }
428 }
429 }
430
431 if !buffer.is_empty() {
432 drain_buffer(&pool, &trader_id, &mut buffer).await;
433 }
434
435 log_task_stopped(CACHE_PROCESS);
436 }
437
438 fn load_state(&self, key: String) -> anyhow::Result<AHashMap<String, Bytes>> {
439 let pool = self.pool.clone();
440 block_on_nautilus_with(move || async move {
441 let value = DatabaseQueries::load_value(&pool, &key).await?;
442 match value {
443 Some(value) => serde_json::from_slice(&value).map_err(Into::into),
444 None => Ok(AHashMap::new()),
445 }
446 })
447 }
448}
449
450async fn check_schema_migrated(pool: &PgPool) -> Result<(), sqlx::Error> {
459 let has_instrument_close: bool = sqlx::query_scalar(
460 "SELECT EXISTS (
461 SELECT 1
462 FROM information_schema.tables
463 WHERE table_schema = current_schema()
464 AND table_name = 'instrument_close'
465 )",
466 )
467 .fetch_one(pool)
468 .await?;
469
470 if !has_instrument_close {
471 return Err(sqlx::Error::Configuration(
472 format!(
473 "Postgres schema is out of date, missing `instrument_close` table: {SCHEMA_MIGRATION_COMMAND}"
474 )
475 .into(),
476 ));
477 }
478
479 let stale: Vec<String> = sqlx::query_scalar(
480 "SELECT table_name || '.' || column_name || ' (' || data_type || ')'
481 FROM information_schema.columns
482 WHERE table_schema = current_schema()
483 AND (table_name, column_name) IN (('order', 'avg_px'), ('order', 'slippage'),
484 ('position', 'signed_qty'), ('position', 'avg_px_open'), ('position', 'avg_px_close'))
485 AND data_type <> 'numeric'
486 ORDER BY table_name, column_name",
487 )
488 .fetch_all(pool)
489 .await?;
490
491 if stale.iter().any(|column| column.starts_with("position.")) {
492 return Err(sqlx::Error::Configuration(
493 format!("Postgres position accounting requires numeric columns, found {}; create a new cache with the current schema", stale.join(", ")).into(),
494 ));
495 }
496
497 if !stale.is_empty() {
498 return Err(sqlx::Error::Configuration(
499 format!(
500 "Postgres schema is out of date, {} should be `numeric`: {SCHEMA_MIGRATION_COMMAND}",
501 stale.join(", "),
502 )
503 .into(),
504 ));
505 }
506
507 check_trader_keys(pool).await?;
508
509 let missing: Vec<String> = sqlx::query_scalar(
510 "SELECT required.table_name || '.' || required.column_name
511 FROM (VALUES
512 ('greeks', 'instrument_id'),
513 ('greeks', 'value'),
514 ('yield_curve', 'curve_name'),
515 ('yield_curve', 'value'),
516 ('instrument', 'info'),
517 ('instrument', 'tick_scheme'),
518 ('instrument', 'underlying_instrument_id'),
519 ('instrument', 'exercise_style'),
520 ('instrument', 'settlement_type'),
521 ('account_event', 'info'),
522 ('account_event', 'trader_id'),
523 ('order_position_index', 'trader_id'),
524 ('order_event', 'released_price'),
525 ('order_event', 'protection_price'),
526 ('order_event', 'due_post_only'),
527 ('order_event', 'correction_id'),
528 ('order_event', 'is_reopened'),
529 ('order_event', 'info'),
530 ('order_event', 'causation_id'),
531 ('position_event', 'reconciliation'),
532 ('position_event', 'info'),
533 ('position_event', 'causation_id')
534 UNION ALL
535 SELECT * FROM (VALUES
536 ('instrument', 'defi_currency_codes'),
537 ('defi_currency', 'currency_id')
538 ) AS defi(table_name, column_name)
539 WHERE $1
540 ) AS required(table_name, column_name)
541 WHERE NOT EXISTS (
542 SELECT 1
543 FROM information_schema.columns
544 WHERE table_schema = current_schema()
545 AND table_name = required.table_name
546 AND column_name = required.column_name
547 )
548 ORDER BY 1",
549 )
550 .bind(cfg!(feature = "defi"))
551 .fetch_all(pool)
552 .await?;
553
554 if missing.is_empty() {
555 return Ok(());
556 }
557
558 Err(sqlx::Error::Configuration(
559 format!(
560 "Postgres schema is out of date, missing cache columns {}: {SCHEMA_MIGRATION_COMMAND}",
561 missing.join(", "),
562 )
563 .into(),
564 ))
565}
566
567async fn check_trader_keys(pool: &PgPool) -> Result<(), sqlx::Error> {
570 let unscoped_keys: Vec<String> = sqlx::query_scalar(
571 "SELECT constraints.table_name
572 FROM information_schema.table_constraints AS constraints
573 WHERE constraints.table_schema = current_schema()
574 AND constraints.constraint_type = 'PRIMARY KEY'
575 AND constraints.table_name IN ('order', 'position', 'order_position_index')
576 AND NOT EXISTS (
577 SELECT 1
578 FROM information_schema.key_column_usage AS keys
579 WHERE keys.constraint_schema = constraints.constraint_schema
580 AND keys.constraint_name = constraints.constraint_name
581 AND keys.column_name = 'trader_id'
582 )
583 ORDER BY 1",
584 )
585 .fetch_all(pool)
586 .await?;
587
588 if unscoped_keys.is_empty() {
589 return Ok(());
590 }
591
592 Err(sqlx::Error::Configuration(
593 format!(
594 "Postgres schema is out of date, primary keys of {} are not trader-qualified: \
595 {SCHEMA_MIGRATION_COMMAND}",
596 unscoped_keys.join(", "),
597 )
598 .into(),
599 ))
600}
601
602async fn check_account_ownership(pool: &PgPool) -> Result<(), sqlx::Error> {
607 let account_ids = DatabaseQueries::load_unassigned_account_ids(pool)
608 .await
609 .map_err(|e| {
610 sqlx::Error::Configuration(
611 format!("Failed to check account event ownership: {e}").into(),
612 )
613 })?;
614
615 if account_ids.is_empty() {
616 return Ok(());
617 }
618
619 let account_ids: Vec<String> = account_ids.iter().map(ToString::to_string).collect();
620 Err(sqlx::Error::Configuration(
621 format!(
622 "Postgres cache has account events with no trader for {}: assign each account with \
623 `nautilus database assign-account --account-id <ACCOUNT_ID> --trader-id <TRADER_ID>` \
624 before connecting",
625 account_ids.join(", "),
626 )
627 .into(),
628 ))
629}
630
631async fn handle_query(
632 maybe_msg: Option<DatabaseQuery>,
633 buffer: &mut VecDeque<DatabaseQuery>,
634 buffer_interval: Duration,
635 pool: &PgPool,
636 trader_id: &TraderId,
637) -> ControlFlow<()> {
638 let Some(msg) = maybe_msg else {
639 log::debug!("Command channel closed");
640 return ControlFlow::Break(());
641 };
642
643 #[cfg(feature = "defi")]
644 let msg = match msg {
645 DatabaseQuery::DefiCurrency(currency, reply) => {
646 if !buffer.is_empty() {
647 drain_buffer(pool, trader_id, buffer).await;
648 }
649
650 let result = DatabaseQueries::add_currency(pool, currency).await;
651 let _ = reply.send(result);
652 return ControlFlow::Continue(());
653 }
654 DatabaseQuery::DefiInstrument(instrument, migration, reply) => {
655 if !buffer.is_empty() {
656 drain_buffer(pool, trader_id, buffer).await;
657 }
658
659 let result = DatabaseQueries::add_defi_instrument(pool, instrument, migration).await;
660 let _ = reply.send(result);
661 return ControlFlow::Continue(());
662 }
663 other => other,
664 };
665
666 if matches!(msg, DatabaseQuery::Close) {
667 if !buffer.is_empty() {
668 drain_buffer(pool, trader_id, buffer).await;
669 }
670 return ControlFlow::Break(());
671 }
672
673 buffer.push_back(msg);
674
675 if buffer_interval.is_zero() {
676 drain_buffer(pool, trader_id, buffer).await;
677 }
678
679 ControlFlow::Continue(())
680}
681
682async fn flush_buffer(
683 buffer: &mut VecDeque<DatabaseQuery>,
684 pool: &PgPool,
685 trader_id: &TraderId,
686 flush_timer: &mut Pin<&mut tokio::time::Sleep>,
687 buffer_interval: Duration,
688) {
689 if !buffer.is_empty() {
690 drain_buffer(pool, trader_id, buffer).await;
691 }
692 flush_timer.as_mut().reset(Instant::now() + buffer_interval);
693}
694
695pub async fn get_pg_cache_database(trader_id: TraderId) -> anyhow::Result<PostgresCacheDatabase> {
701 Ok(PostgresCacheDatabase::connect(PostgresCacheConfig::default(), trader_id).await?)
702}
703
704#[async_trait::async_trait]
705impl CacheDatabaseAdapter for PostgresCacheDatabase {
706 fn close(&mut self) -> anyhow::Result<()> {
707 let pool = self.pool.clone();
708 let (tx, rx) = std::sync::mpsc::channel();
709
710 if let Err(e) = self.tx.send(DatabaseQuery::Close) {
712 log::warn!("Error sending close: {e:?}");
713 }
714
715 log_task_awaiting("cache-write");
716
717 tokio::task::block_in_place(|| {
718 if let Err(e) = get_runtime().block_on(&mut self.handle) {
719 log::error!("Error awaiting task 'cache-write': {e:?}");
720 }
721 });
722
723 log::debug!("Closing connection pool");
724
725 tokio::task::block_in_place(|| {
726 get_runtime().block_on(async {
727 pool.close().await;
728
729 if let Err(e) = tx.send(()) {
730 log::error!("Error closing pool: {e:?}");
731 }
732 });
733 });
734
735 log::debug!("Closed");
736
737 Ok(rx.recv()?)
738 }
739
740 fn flush(&mut self) -> anyhow::Result<()> {
741 let pool = self.pool.clone();
742 let trader_id = self.trader_id;
743
744 tokio::task::block_in_place(|| {
745 get_runtime().block_on(DatabaseQueries::delete_trader(&pool, &trader_id))
746 })
747 .map_err(|e| anyhow::anyhow!("Failed to flush Postgres cache for {trader_id}: {e}"))
748 }
749
750 async fn load_all(&self) -> anyhow::Result<CacheMap> {
751 let mut currencies = self.load_currencies().await?;
752 register_loaded_currencies(&mut currencies)?;
753
754 let (
755 instruments,
756 instrument_closes,
757 synthetics,
758 accounts,
759 orders,
760 positions,
761 greeks,
762 yield_curves,
763 ) = try_join!(
764 self.load_instruments(),
765 self.load_instrument_closes(),
766 self.load_synthetics(),
767 self.load_accounts(),
768 self.load_orders(),
769 self.load_positions(),
770 self.load_greeks(),
771 self.load_yield_curves()
772 )
773 .map_err(|e| anyhow::anyhow!("Error loading cache data: {e}"))?;
774
775 Ok(CacheMap {
776 currencies,
777 instruments,
778 instrument_closes,
779 synthetics,
780 accounts,
781 orders,
782 positions,
783 greeks,
784 yield_curves,
785 })
786 }
787
788 fn load(&self) -> anyhow::Result<AHashMap<String, Bytes>> {
789 let pool = self.pool.clone();
790 let (tx, rx) = std::sync::mpsc::channel();
791
792 tokio::spawn(async move {
793 let result = DatabaseQueries::load(&pool).await;
794 match result {
795 Ok(items) => {
796 let mapping = items
797 .into_iter()
798 .filter(|(key, _)| !key.starts_with(CACHE_DATABASE_KEY_PREFIX))
799 .map(|(k, v)| (k, Bytes::from(v)))
800 .collect();
801
802 if let Err(e) = tx.send(mapping) {
803 log::error!("Failed to send general items: {e:?}");
804 }
805 }
806 Err(e) => {
807 log::error!("Failed to load general items: {e:?}");
808 if let Err(e) = tx.send(AHashMap::new()) {
809 log::error!("Failed to send empty general items: {e:?}");
810 }
811 }
812 }
813 });
814 Ok(rx.recv()?)
815 }
816
817 async fn load_currencies(&self) -> anyhow::Result<AHashMap<Ustr, Currency>> {
818 match DatabaseQueries::load_currencies(&self.pool).await {
819 Ok(items) => Ok(items
820 .into_iter()
821 .map(|currency| (currency.code, currency))
822 .collect()),
823 Err(e) => {
824 #[cfg(feature = "defi")]
825 if e.is::<crate::defi::IntegrityError>() {
826 return Err(e);
827 }
828
829 log::error!("Failed to load_currencies: {e}");
830 Ok(AHashMap::new())
831 }
832 }
833 }
834
835 async fn load_instruments(&self) -> anyhow::Result<AHashMap<InstrumentId, InstrumentAny>> {
836 let instruments = DatabaseQueries::load_instruments(&self.pool).await?;
837 Ok(instruments
838 .into_iter()
839 .map(|instrument| (instrument.id(), instrument))
840 .collect())
841 }
842
843 async fn load_instrument_closes(
844 &self,
845 ) -> anyhow::Result<AHashMap<InstrumentId, InstrumentClose>> {
846 Ok(DatabaseQueries::load_instrument_closes(&self.pool)
847 .await?
848 .into_iter()
849 .map(|close| (close.instrument_id, close))
850 .collect())
851 }
852
853 async fn load_synthetics(&self) -> anyhow::Result<AHashMap<InstrumentId, SyntheticInstrument>> {
854 let prefix = format!("{CACHE_DATABASE_KEY_PREFIX}synthetic:");
855 let values: Vec<Vec<u8>> =
856 sqlx::query_scalar("SELECT value FROM general WHERE starts_with(id, $1)")
857 .bind(prefix)
858 .fetch_all(&self.pool)
859 .await?;
860 values
861 .into_iter()
862 .map(|value| {
863 let synthetic: SyntheticInstrument = serde_json::from_slice(&value)?;
864 Ok((synthetic.id, synthetic))
865 })
866 .collect()
867 }
868
869 async fn load_accounts(&self) -> anyhow::Result<AHashMap<AccountId, AccountAny>> {
870 let pool = self.pool.clone();
871 let trader_id = self.trader_id;
872 let (tx, rx) = std::sync::mpsc::channel();
873
874 tokio::spawn(async move {
875 let result = DatabaseQueries::load_accounts(&pool, &trader_id).await;
876 match result {
877 Ok(accounts) => {
878 let mapping = accounts
879 .into_iter()
880 .map(|account| (account.id(), account))
881 .collect();
882
883 if let Err(e) = tx.send(mapping) {
884 log::error!("Failed to send accounts: {e:?}");
885 }
886 }
887 Err(e) => {
888 log::error!("Failed to load accounts: {e:?}");
889 if let Err(e) = tx.send(AHashMap::new()) {
890 log::error!("Failed to send empty accounts: {e:?}");
891 }
892 }
893 }
894 });
895 Ok(rx.recv()?)
896 }
897
898 async fn load_orders(&self) -> anyhow::Result<AHashMap<ClientOrderId, OrderAny>> {
899 let pool = self.pool.clone();
900 let trader_id = self.trader_id;
901 let (tx, rx) = std::sync::mpsc::channel();
902
903 tokio::spawn(async move {
904 let result = DatabaseQueries::load_orders(&pool, &trader_id).await;
905 match result {
906 Ok(orders) => {
907 let mapping = orders
908 .into_iter()
909 .map(|order| (order.client_order_id(), order))
910 .collect();
911
912 if let Err(e) = tx.send(mapping) {
913 log::error!("Failed to send orders: {e:?}");
914 }
915 }
916 Err(e) => {
917 log::error!("Failed to load orders: {e:?}");
918 if let Err(e) = tx.send(AHashMap::new()) {
919 log::error!("Failed to send empty orders: {e:?}");
920 }
921 }
922 }
923 });
924 Ok(rx.recv()?)
925 }
926
927 async fn load_positions(&self) -> anyhow::Result<AHashMap<PositionId, Position>> {
928 let pool = self.pool.clone();
929 let trader_id = self.trader_id;
930 let (tx, rx) = std::sync::mpsc::channel();
931
932 tokio::spawn(async move {
933 let result = DatabaseQueries::load_positions(&pool, &trader_id)
934 .await
935 .map(|positions| {
936 positions
937 .into_iter()
938 .map(|position| (position.id, position))
939 .collect()
940 });
941
942 if let Err(e) = tx.send(result) {
943 log::error!("Failed to send positions: {e:?}");
944 }
945 });
946 rx.recv()?
947 }
948
949 async fn load_greeks(&self) -> anyhow::Result<AHashMap<InstrumentId, GreeksData>> {
950 DatabaseQueries::load_greeks(&self.pool).await
951 }
952
953 async fn load_yield_curves(&self) -> anyhow::Result<AHashMap<String, YieldCurve>> {
954 DatabaseQueries::load_yield_curves(&self.pool).await
955 }
956
957 fn load_index_order_position(&self) -> anyhow::Result<AHashMap<ClientOrderId, PositionId>> {
958 let pool = self.pool.clone();
959 let trader_id = self.trader_id;
960 let (tx, rx) = std::sync::mpsc::channel();
961
962 tokio::spawn(async move {
963 let result = DatabaseQueries::load_index_order_position(&pool, &trader_id).await;
964 match result {
965 Ok(index) => {
966 if let Err(e) = tx.send(index) {
967 log::error!("Failed to send load_index_order_position result: {e:?}");
968 }
969 }
970 Err(e) => {
971 log::error!("Failed to run query load_index_order_position: {e:?}");
972 if let Err(e) = tx.send(AHashMap::new()) {
973 log::error!("Failed to send empty load_index_order_position result: {e:?}");
974 }
975 }
976 }
977 });
978 Ok(rx.recv()?)
979 }
980
981 fn load_index_order_client(&self) -> anyhow::Result<AHashMap<ClientOrderId, ClientId>> {
982 let pool = self.pool.clone();
983 let trader_id = self.trader_id;
984 let (tx, rx) = std::sync::mpsc::channel();
985
986 tokio::spawn(async move {
987 let result =
988 DatabaseQueries::load_distinct_order_event_client_ids(&pool, &trader_id).await;
989
990 match result {
991 Ok(currency) => {
992 if let Err(e) = tx.send(currency) {
993 log::error!("Failed to send load_index_order_client result: {e:?}");
994 }
995 }
996 Err(e) => {
997 log::error!("Failed to run query load_distinct_order_event_client_ids: {e:?}");
998 if let Err(e) = tx.send(AHashMap::new()) {
999 log::error!("Failed to send empty load_index_order_client result: {e:?}");
1000 }
1001 }
1002 }
1003 });
1004 Ok(rx.recv()?)
1005 }
1006
1007 async fn load_currency(&self, code: &Ustr) -> anyhow::Result<Option<Currency>> {
1008 match DatabaseQueries::load_currency(&self.pool, code).await {
1009 Ok(items) => Ok(items),
1010 Err(e) => {
1011 #[cfg(feature = "defi")]
1012 if e.is::<crate::defi::IntegrityError>() {
1013 return Err(e);
1014 }
1015
1016 log::error!("Failed to load_currency: {e}");
1017 Ok(None)
1018 }
1019 }
1020 }
1021
1022 async fn load_instrument(
1023 &self,
1024 instrument_id: &InstrumentId,
1025 ) -> anyhow::Result<Option<InstrumentAny>> {
1026 load_instrument(&self.pool, instrument_id).await
1027 }
1028
1029 async fn load_synthetic(
1030 &self,
1031 instrument_id: &InstrumentId,
1032 ) -> anyhow::Result<Option<SyntheticInstrument>> {
1033 DatabaseQueries::load_synthetic(&self.pool, instrument_id).await
1034 }
1035
1036 fn load_instrument_sync(
1037 &self,
1038 instrument_id: &InstrumentId,
1039 ) -> anyhow::Result<Option<InstrumentAny>> {
1040 let pool = self.pool.clone();
1041 let instrument_id = *instrument_id;
1042 block_on_nautilus_with(move || async move { load_instrument(&pool, &instrument_id).await })
1043 }
1044
1045 fn load_synthetic_sync(
1046 &self,
1047 instrument_id: &InstrumentId,
1048 ) -> anyhow::Result<Option<SyntheticInstrument>> {
1049 let pool = self.pool.clone();
1050 let instrument_id = *instrument_id;
1051 block_on_nautilus_with(move || async move {
1052 DatabaseQueries::load_synthetic(&pool, &instrument_id).await
1053 })
1054 }
1055
1056 async fn load_account(&self, account_id: &AccountId) -> anyhow::Result<Option<AccountAny>> {
1057 let pool = self.pool.clone();
1058 let trader_id = self.trader_id;
1059 let account_id = account_id.to_owned();
1060 let (tx, rx) = std::sync::mpsc::channel();
1061
1062 tokio::spawn(async move {
1063 let result = DatabaseQueries::load_account(&pool, &account_id, &trader_id).await;
1064 match result {
1065 Ok(account) => {
1066 if let Err(e) = tx.send(account) {
1067 log::error!("Failed to send account {account_id}: {e:?}");
1068 }
1069 }
1070 Err(e) => {
1071 log::error!("Failed to load account {account_id}: {e:?}");
1072 if let Err(e) = tx.send(None) {
1073 log::error!("Failed to send None for account {account_id}: {e:?}");
1074 }
1075 }
1076 }
1077 });
1078 Ok(rx.recv()?)
1079 }
1080
1081 async fn load_order(
1082 &self,
1083 client_order_id: &ClientOrderId,
1084 ) -> anyhow::Result<Option<OrderAny>> {
1085 let pool = self.pool.clone();
1086 let trader_id = self.trader_id;
1087 let client_order_id = client_order_id.to_owned();
1088 let (tx, rx) = std::sync::mpsc::channel();
1089
1090 tokio::spawn(async move {
1091 let result = DatabaseQueries::load_order(&pool, &client_order_id, &trader_id).await;
1092 match result {
1093 Ok(order) => {
1094 if let Err(e) = tx.send(order) {
1095 log::error!("Failed to send order {client_order_id}: {e:?}");
1096 }
1097 }
1098 Err(e) => {
1099 log::error!("Failed to load order {client_order_id}: {e:?}");
1100 let _ = tx.send(None);
1101 }
1102 }
1103 });
1104 Ok(rx.recv()?)
1105 }
1106
1107 async fn load_position(&self, position_id: &PositionId) -> anyhow::Result<Option<Position>> {
1108 let pool = self.pool.clone();
1109 let trader_id = self.trader_id;
1110 let position_id = position_id.to_owned();
1111 let (tx, rx) = std::sync::mpsc::channel();
1112
1113 tokio::spawn(async move {
1114 let result = DatabaseQueries::load_position(&pool, &position_id, &trader_id).await;
1115 if let Err(e) = tx.send(result) {
1116 log::error!("Failed to send position {position_id}: {e:?}");
1117 }
1118 });
1119 rx.recv()?
1120 }
1121
1122 fn load_actor(&self, actor_id: &ActorId) -> anyhow::Result<AHashMap<String, Bytes>> {
1123 self.load_state(format!(
1124 "{}actor:{actor_id}",
1125 trader_key_prefix(self.trader_id)
1126 ))
1127 }
1128
1129 fn delete_actor(&self, actor_id: &ActorId) -> anyhow::Result<()> {
1130 let query = DatabaseQuery::Delete(format!(
1131 "{}actor:{actor_id}",
1132 trader_key_prefix(self.trader_id)
1133 ));
1134 self.tx
1135 .send(query)
1136 .map_err(|e| anyhow::anyhow!("Failed to queue state deletion: {e}"))
1137 }
1138
1139 fn load_strategy(&self, strategy_id: &StrategyId) -> anyhow::Result<AHashMap<String, Bytes>> {
1140 self.load_state(format!(
1141 "{}strategy:{strategy_id}",
1142 trader_key_prefix(self.trader_id)
1143 ))
1144 }
1145
1146 fn delete_strategy(&self, strategy_id: &StrategyId) -> anyhow::Result<()> {
1147 let query = DatabaseQuery::Delete(format!(
1148 "{}strategy:{strategy_id}",
1149 trader_key_prefix(self.trader_id)
1150 ));
1151 self.tx
1152 .send(query)
1153 .map_err(|e| anyhow::anyhow!("Failed to queue state deletion: {e}"))
1154 }
1155
1156 fn delete_order(&self, client_order_id: &ClientOrderId) -> anyhow::Result<()> {
1157 self.tx
1158 .send(DatabaseQuery::DeleteOrder(*client_order_id))
1159 .map_err(|e| anyhow::anyhow!("Failed to queue order deletion: {e}"))
1160 }
1161
1162 fn delete_position(&self, position_id: &PositionId) -> anyhow::Result<()> {
1163 self.tx
1164 .send(DatabaseQuery::DeletePosition(*position_id))
1165 .map_err(|e| anyhow::anyhow!("Failed to queue position deletion: {e}"))
1166 }
1167
1168 fn delete_account_event(&self, account_id: &AccountId, event_id: &str) -> anyhow::Result<()> {
1169 log::warn!(
1170 "Deleting account events is a no-op pending replay-safe compaction, {account_id}: {event_id}"
1171 );
1172 Ok(())
1173 }
1174
1175 fn add(&self, key: String, value: Bytes) -> anyhow::Result<()> {
1176 let query = DatabaseQuery::Add(key, value.into());
1177 self.tx
1178 .send(query)
1179 .map_err(|e| anyhow::anyhow!("Failed to send query to database message handler: {e}"))
1180 }
1181
1182 fn add_currency(&self, currency: &Currency) -> anyhow::Result<()> {
1183 #[cfg(feature = "defi")]
1184 if nautilus_model::defi::currency::check(*currency)
1185 .map_err(crate::defi::integrity_error)?
1186 .is_some()
1187 || currency.code.contains('@')
1188 {
1189 return self.write_defi(|reply| DatabaseQuery::DefiCurrency(*currency, reply));
1190 }
1191
1192 let query = DatabaseQuery::AddCurrency(*currency);
1193 self.tx.send(query).map_err(|e| {
1194 anyhow::anyhow!("Failed to query add_currency to database message handler: {e}")
1195 })
1196 }
1197
1198 fn add_instrument(&self, instrument: &InstrumentAny) -> anyhow::Result<()> {
1199 #[cfg(feature = "defi")]
1200 {
1201 self.write_defi(|reply| DatabaseQuery::DefiInstrument(instrument.clone(), None, reply))
1202 }
1203
1204 #[cfg(not(feature = "defi"))]
1205 {
1206 let query = DatabaseQuery::AddInstrument(instrument.clone());
1207 self.tx.send(query).map_err(|e| {
1208 anyhow::anyhow!(
1209 "Failed to send query add_instrument to database message handler: {e}"
1210 )
1211 })
1212 }
1213 }
1214
1215 #[cfg(feature = "defi")]
1216 fn add_defi_instrument(
1217 &self,
1218 instrument: &InstrumentAny,
1219 migration: InstrumentMigration,
1220 ) -> anyhow::Result<()> {
1221 self.write_defi(|reply| {
1222 DatabaseQuery::DefiInstrument(instrument.clone(), Some(migration), reply)
1223 })
1224 }
1225
1226 fn add_instrument_close(&self, close: &InstrumentClose) -> anyhow::Result<()> {
1227 self.tx
1228 .send(DatabaseQuery::AddInstrumentClose(*close))
1229 .map_err(|e| {
1230 anyhow::anyhow!(
1231 "Failed to send query add_instrument_close to database message handler: {e}"
1232 )
1233 })
1234 }
1235
1236 fn add_synthetic(&self, synthetic: &SyntheticInstrument) -> anyhow::Result<()> {
1237 self.add(
1238 format!("{CACHE_DATABASE_KEY_PREFIX}synthetic:{}", synthetic.id),
1239 Bytes::from(serde_json::to_vec(synthetic)?),
1240 )
1241 }
1242
1243 fn add_account(&self, account: &AccountAny) -> anyhow::Result<()> {
1244 let query = DatabaseQuery::AddAccount(account_last_event(account)?, false);
1245 self.tx.send(query).map_err(|e| {
1246 anyhow::anyhow!("Failed to send query add_account to database message handler: {e}")
1247 })
1248 }
1249
1250 fn add_order(&self, order: &OrderAny, client_id: Option<ClientId>) -> anyhow::Result<()> {
1251 let query = DatabaseQuery::AddOrder(order_initialized_event(order), client_id);
1252 self.tx.send(query).map_err(|e| {
1253 anyhow::anyhow!("Failed to send query add_order to database message handler: {e}")
1254 })
1255 }
1256
1257 fn add_order_snapshot(&self, snapshot: &OrderSnapshot) -> anyhow::Result<()> {
1258 let query = DatabaseQuery::AddOrderSnapshot(snapshot.to_owned());
1259 self.tx.send(query).map_err(|e| {
1260 anyhow::anyhow!(
1261 "Failed to send query add_order_snapshot to database message handler: {e}"
1262 )
1263 })
1264 }
1265
1266 fn add_position(&self, position: &Position) -> anyhow::Result<()> {
1267 let event = position_last_event(position)?;
1268 let query = DatabaseQuery::AddPosition(position.id, event);
1269 self.tx.send(query).map_err(|e| {
1270 anyhow::anyhow!("Failed to send query add_position to database message handler: {e}")
1271 })?;
1272
1273 if position.requires_replay_state() {
1274 self.add_position_snapshot(&PositionSnapshot::from_replay_state(position, None))?;
1275 }
1276
1277 Ok(())
1278 }
1279
1280 fn add_position_snapshot(&self, snapshot: &PositionSnapshot) -> anyhow::Result<()> {
1281 let query = DatabaseQuery::AddPositionSnapshot(snapshot.to_owned());
1282 self.tx.send(query).map_err(|e| {
1283 anyhow::anyhow!(
1284 "Failed to send query add_position_snapshot to database message handler: {e}"
1285 )
1286 })
1287 }
1288
1289 fn add_order_book(&self, _order_book: &OrderBook) -> anyhow::Result<()> {
1290 anyhow::bail!("Postgres order book persistence is not supported")
1291 }
1292
1293 fn add_quote(&self, quote: &QuoteTick) -> anyhow::Result<()> {
1294 let query = DatabaseQuery::AddQuote(quote.to_owned());
1295 self.tx.send(query).map_err(|e| {
1296 anyhow::anyhow!("Failed to send query add_quote to database message handler: {e}")
1297 })
1298 }
1299
1300 fn load_quotes(&self, instrument_id: &InstrumentId) -> anyhow::Result<Vec<QuoteTick>> {
1301 let pool = self.pool.clone();
1302 let instrument_id = instrument_id.to_owned();
1303 let (tx, rx) = std::sync::mpsc::channel();
1304
1305 tokio::spawn(async move {
1306 let result = DatabaseQueries::load_quotes(&pool, &instrument_id).await;
1307 match result {
1308 Ok(quotes) => {
1309 if let Err(e) = tx.send(quotes) {
1310 log::error!("Failed to send quotes for instrument {instrument_id}: {e:?}");
1311 }
1312 }
1313 Err(e) => {
1314 log::error!("Failed to load quotes for instrument {instrument_id}: {e:?}");
1315 if let Err(e) = tx.send(Vec::new()) {
1316 log::error!(
1317 "Failed to send empty quotes for instrument {instrument_id}: {e:?}"
1318 );
1319 }
1320 }
1321 }
1322 });
1323 Ok(rx.recv()?)
1324 }
1325
1326 fn add_trade(&self, trade: &TradeTick) -> anyhow::Result<()> {
1327 let query = DatabaseQuery::AddTrade(trade.to_owned());
1328 self.tx.send(query).map_err(|e| {
1329 anyhow::anyhow!("Failed to send query add_trade to database message handler: {e}")
1330 })
1331 }
1332
1333 fn load_trades(&self, instrument_id: &InstrumentId) -> anyhow::Result<Vec<TradeTick>> {
1334 let pool = self.pool.clone();
1335 let instrument_id = instrument_id.to_owned();
1336 let (tx, rx) = std::sync::mpsc::channel();
1337
1338 tokio::spawn(async move {
1339 let result = DatabaseQueries::load_trades(&pool, &instrument_id).await;
1340 match result {
1341 Ok(trades) => {
1342 if let Err(e) = tx.send(trades) {
1343 log::error!("Failed to send trades for instrument {instrument_id}: {e:?}");
1344 }
1345 }
1346 Err(e) => {
1347 log::error!("Failed to load trades for instrument {instrument_id}: {e:?}");
1348 if let Err(e) = tx.send(Vec::new()) {
1349 log::error!(
1350 "Failed to send empty trades for instrument {instrument_id}: {e:?}"
1351 );
1352 }
1353 }
1354 }
1355 });
1356 Ok(rx.recv()?)
1357 }
1358
1359 fn add_funding_rate(&self, _funding_rate: &FundingRateUpdate) -> anyhow::Result<()> {
1360 anyhow::bail!("add_funding_rate not implemented for PostgreSQL cache adapter")
1361 }
1362
1363 fn load_funding_rates(
1364 &self,
1365 _instrument_id: &InstrumentId,
1366 ) -> anyhow::Result<Vec<FundingRateUpdate>> {
1367 anyhow::bail!("load_funding_rates not implemented for PostgreSQL cache adapter")
1368 }
1369
1370 fn add_bar(&self, bar: &Bar) -> anyhow::Result<()> {
1371 let query = DatabaseQuery::AddBar(bar.to_owned());
1372 self.tx.send(query).map_err(|e| {
1373 anyhow::anyhow!("Failed to send query add_bar to database message handler: {e}")
1374 })
1375 }
1376
1377 fn load_bars(&self, instrument_id: &InstrumentId) -> anyhow::Result<Vec<Bar>> {
1378 let pool = self.pool.clone();
1379 let instrument_id = instrument_id.to_owned();
1380 let (tx, rx) = std::sync::mpsc::channel();
1381
1382 tokio::spawn(async move {
1383 let result = DatabaseQueries::load_bars(&pool, &instrument_id).await;
1384 match result {
1385 Ok(bars) => {
1386 if let Err(e) = tx.send(bars) {
1387 log::error!("Failed to send bars for instrument {instrument_id}: {e:?}");
1388 }
1389 }
1390 Err(e) => {
1391 log::error!("Failed to load bars for instrument {instrument_id}: {e:?}");
1392 if let Err(e) = tx.send(Vec::new()) {
1393 log::error!(
1394 "Failed to send empty bars for instrument {instrument_id}: {e:?}"
1395 );
1396 }
1397 }
1398 }
1399 });
1400 Ok(rx.recv()?)
1401 }
1402
1403 fn add_greeks(&self, greeks: &GreeksData) -> anyhow::Result<()> {
1404 anyhow::ensure!(
1405 [
1406 greeks.strike,
1407 greeks.expiry_in_years,
1408 greeks.multiplier,
1409 greeks.quantity,
1410 greeks.underlying_price,
1411 greeks.interest_rate,
1412 greeks.cost_of_carry,
1413 greeks.vol,
1414 greeks.pnl,
1415 greeks.price,
1416 greeks.delta,
1417 greeks.gamma,
1418 greeks.vega,
1419 greeks.theta,
1420 greeks.rho,
1421 greeks.itm_prob,
1422 ]
1423 .iter()
1424 .all(|value| value.is_finite()),
1425 "Cannot persist non-finite GreeksData"
1426 );
1427 self.tx
1428 .send(DatabaseQuery::AddGreeks(greeks.clone()))
1429 .map_err(|e| {
1430 anyhow::anyhow!("Failed to send query add_greeks to database message handler: {e}")
1431 })
1432 }
1433
1434 fn add_yield_curve(&self, curve: &YieldCurve) -> anyhow::Result<()> {
1435 curve.validate()?;
1436 self.tx
1437 .send(DatabaseQuery::AddYieldCurve(curve.clone()))
1438 .map_err(|e| {
1439 anyhow::anyhow!(
1440 "Failed to send query add_yield_curve to database message handler: {e}"
1441 )
1442 })
1443 }
1444
1445 fn add_signal(&self, signal: &Signal) -> anyhow::Result<()> {
1446 let query = DatabaseQuery::AddSignal(signal.to_owned());
1447 self.tx.send(query).map_err(|e| {
1448 anyhow::anyhow!("Failed to send query add_signal to database message handler: {e}")
1449 })
1450 }
1451
1452 fn load_signals(&self, name: &str) -> anyhow::Result<Vec<Signal>> {
1453 let pool = self.pool.clone();
1454 let name = name.to_owned();
1455 let (tx, rx) = std::sync::mpsc::channel();
1456
1457 tokio::spawn(async move {
1458 let result = DatabaseQueries::load_signals(&pool, &name).await;
1459 match result {
1460 Ok(signals) => {
1461 if let Err(e) = tx.send(signals) {
1462 log::error!("Failed to send signals for '{name}': {e:?}");
1463 }
1464 }
1465 Err(e) => {
1466 log::error!("Failed to load signals for '{name}': {e:?}");
1467 if let Err(e) = tx.send(Vec::new()) {
1468 log::error!("Failed to send empty signals for '{name}': {e:?}");
1469 }
1470 }
1471 }
1472 });
1473 Ok(rx.recv()?)
1474 }
1475
1476 fn add_custom_data(&self, data: &CustomData) -> anyhow::Result<()> {
1477 let query = DatabaseQuery::AddCustom(data.to_owned());
1478 self.tx.send(query).map_err(|e| {
1479 anyhow::anyhow!("Failed to send query add_signal to database message handler: {e}")
1480 })
1481 }
1482
1483 fn load_custom_data(&self, data_type: &DataType) -> anyhow::Result<Vec<CustomData>> {
1484 let pool = self.pool.clone();
1485 let data_type = data_type.to_owned();
1486 let (tx, rx) = std::sync::mpsc::channel();
1487
1488 tokio::spawn(async move {
1489 let result = DatabaseQueries::load_custom_data(&pool, &data_type).await;
1490 match result {
1491 Ok(signals) => {
1492 if let Err(e) = tx.send(signals) {
1493 log::error!("Failed to send custom data for '{data_type}': {e:?}");
1494 }
1495 }
1496 Err(e) => {
1497 log::error!("Failed to load custom data for '{data_type}': {e:?}");
1498 if let Err(e) = tx.send(Vec::new()) {
1499 log::error!("Failed to send empty custom data for '{data_type}': {e:?}");
1500 }
1501 }
1502 }
1503 });
1504 Ok(rx.recv()?)
1505 }
1506
1507 fn load_order_snapshot(
1508 &self,
1509 client_order_id: &ClientOrderId,
1510 ) -> anyhow::Result<Option<OrderSnapshot>> {
1511 let pool = self.pool.clone();
1512 let trader_id = self.trader_id;
1513 let client_order_id = client_order_id.to_owned();
1514 let (tx, rx) = std::sync::mpsc::channel();
1515
1516 tokio::spawn(async move {
1517 let result =
1518 DatabaseQueries::load_order_snapshot(&pool, &client_order_id, &trader_id).await;
1519
1520 match result {
1521 Ok(snapshot) => {
1522 if let Err(e) = tx.send(snapshot) {
1523 log::error!("Failed to send order snapshot {client_order_id}: {e:?}");
1524 }
1525 }
1526 Err(e) => {
1527 log::error!("Failed to load order snapshot {client_order_id}: {e:?}");
1528 if let Err(e) = tx.send(None) {
1529 log::error!(
1530 "Failed to send None for order snapshot {client_order_id}: {e:?}"
1531 );
1532 }
1533 }
1534 }
1535 });
1536 Ok(rx.recv()?)
1537 }
1538
1539 fn load_position_snapshot(
1540 &self,
1541 position_id: &PositionId,
1542 ) -> anyhow::Result<Option<PositionSnapshot>> {
1543 let pool = self.pool.clone();
1544 let trader_id = self.trader_id;
1545 let position_id = position_id.to_owned();
1546 let (tx, rx) = std::sync::mpsc::channel();
1547
1548 tokio::spawn(async move {
1549 let result =
1550 DatabaseQueries::load_position_snapshot(&pool, &position_id, &trader_id).await;
1551
1552 match result {
1553 Ok(snapshot) => {
1554 if let Err(e) = tx.send(snapshot) {
1555 log::error!("Failed to send position snapshot {position_id}: {e:?}");
1556 }
1557 }
1558 Err(e) => {
1559 log::error!("Failed to load position snapshot {position_id}: {e:?}");
1560 if let Err(e) = tx.send(None) {
1561 log::error!(
1562 "Failed to send None for position snapshot {position_id}: {e:?}"
1563 );
1564 }
1565 }
1566 }
1567 });
1568 Ok(rx.recv()?)
1569 }
1570
1571 fn index_venue_order_id(
1572 &self,
1573 _client_order_id: ClientOrderId,
1574 _venue_order_id: VenueOrderId,
1575 ) -> anyhow::Result<()> {
1576 todo!()
1577 }
1578
1579 fn index_order_position(
1580 &self,
1581 client_order_id: ClientOrderId,
1582 position_id: PositionId,
1583 ) -> anyhow::Result<()> {
1584 let query = DatabaseQuery::IndexOrderPosition(client_order_id, position_id);
1585 self.tx.send(query).map_err(|e| {
1586 anyhow::anyhow!(
1587 "Failed to send query index_order_position to database message handler: {e}"
1588 )
1589 })
1590 }
1591
1592 fn index_order_clients(&self, claims: &[(ClientOrderId, ClientId)]) -> anyhow::Result<()> {
1593 if claims.is_empty() {
1594 return Ok(());
1595 }
1596
1597 let query = DatabaseQuery::IndexOrderClients(claims.to_vec());
1598 self.tx.send(query).map_err(|e| {
1599 anyhow::anyhow!(
1600 "Failed to send query index_order_clients to database message handler: {e}"
1601 )
1602 })
1603 }
1604
1605 fn update_actor(
1606 &self,
1607 actor_id: &ActorId,
1608 state: &AHashMap<String, Bytes>,
1609 ) -> anyhow::Result<()> {
1610 self.add(
1611 format!("{}actor:{actor_id}", trader_key_prefix(self.trader_id)),
1612 Bytes::from(serde_json::to_vec(state)?),
1613 )
1614 }
1615
1616 fn update_strategy(
1617 &self,
1618 strategy_id: &StrategyId,
1619 state: &AHashMap<String, Bytes>,
1620 ) -> anyhow::Result<()> {
1621 self.add(
1622 format!(
1623 "{}strategy:{strategy_id}",
1624 trader_key_prefix(self.trader_id)
1625 ),
1626 Bytes::from(serde_json::to_vec(state)?),
1627 )
1628 }
1629
1630 fn update_account(&self, account: &AccountAny) -> anyhow::Result<()> {
1631 let query = DatabaseQuery::AddAccount(account_last_event(account)?, true);
1632 self.tx.send(query).map_err(|e| {
1633 anyhow::anyhow!("Failed to send query add_account to database message handler: {e}")
1634 })
1635 }
1636
1637 fn update_order(&self, event: &OrderEventAny) -> anyhow::Result<()> {
1638 let query = DatabaseQuery::UpdateOrder(event.clone());
1639 self.tx.send(query).map_err(|e| {
1640 anyhow::anyhow!("Failed to send query update_order to database message handler: {e}")
1641 })
1642 }
1643
1644 fn update_position(&self, position: &Position) -> anyhow::Result<()> {
1645 let query = if position.requires_replay_state() {
1646 DatabaseQuery::AddPositionSnapshot(PositionSnapshot::from_replay_state(position, None))
1647 } else {
1648 DatabaseQuery::UpdatePosition(position_last_event(position)?)
1649 };
1650 self.tx.send(query).map_err(|e| {
1651 anyhow::anyhow!("Failed to send query update_position to database message handler: {e}")
1652 })
1653 }
1654
1655 fn snapshot_order_state(&self, order: &OrderAny) -> anyhow::Result<()> {
1656 let snapshot = OrderSnapshot::from(order.clone());
1657 self.add_order_snapshot(&snapshot)
1658 }
1659
1660 fn snapshot_position_state(
1661 &self,
1662 position: &Position,
1663 ts_snapshot: UnixNanos,
1664 unrealized_pnl: Option<Money>,
1665 ) -> anyhow::Result<()> {
1666 let mut snapshot = if position.requires_replay_state() {
1667 PositionSnapshot::from_replay_state(position, unrealized_pnl)
1668 } else {
1669 PositionSnapshot::from(position, unrealized_pnl)
1670 };
1671 snapshot.ts_init = ts_snapshot;
1672 self.add_position_snapshot(&snapshot)
1673 }
1674
1675 fn heartbeat(&self, timestamp: UnixNanos) -> anyhow::Result<()> {
1676 self.add(
1677 format!("{}health:heartbeat", trader_key_prefix(self.trader_id)),
1678 Bytes::from(serde_json::to_vec(×tamp)?),
1679 )
1680 }
1681}
1682
1683async fn load_instrument(
1684 pool: &PgPool,
1685 instrument_id: &InstrumentId,
1686) -> anyhow::Result<Option<InstrumentAny>> {
1687 let mut currencies = DatabaseQueries::load_currencies(pool)
1688 .await?
1689 .into_iter()
1690 .map(|currency| (currency.code, currency))
1691 .collect();
1692 register_loaded_currencies(&mut currencies)?;
1693 DatabaseQueries::load_instrument(pool, instrument_id).await
1694}
1695
1696fn account_last_event(account: &AccountAny) -> anyhow::Result<AccountState> {
1697 account
1698 .last_event()
1699 .ok_or_else(|| anyhow::anyhow!("Cannot persist account with no events: {}", account.id()))
1700}
1701
1702fn order_initialized_event(order: &OrderAny) -> OrderInitialized {
1703 order.init_event().clone()
1704}
1705
1706fn position_last_event(position: &Position) -> anyhow::Result<OrderFilled> {
1707 position
1708 .last_event()
1709 .ok_or_else(|| anyhow::anyhow!("Cannot persist position with no events: {}", position.id))
1710}
1711
1712async fn drain_buffer(pool: &PgPool, trader_id: &TraderId, buffer: &mut VecDeque<DatabaseQuery>) {
1713 for cmd in buffer.drain(..) {
1714 let result: anyhow::Result<()> = match cmd {
1715 #[cfg(feature = "defi")]
1716 DatabaseQuery::DefiCurrency(..) | DatabaseQuery::DefiInstrument(..) => {
1717 unreachable!("DeFi writes execute before buffering")
1718 }
1719 DatabaseQuery::Close => Ok(()),
1720 DatabaseQuery::Add(key, value) => DatabaseQueries::add(pool, key, value).await,
1721 DatabaseQuery::Delete(key) => DatabaseQueries::delete(pool, &key).await,
1722 DatabaseQuery::DeleteOrder(id) => {
1723 DatabaseQueries::delete_order(pool, *trader_id, id).await
1724 }
1725 DatabaseQuery::DeletePosition(id) => {
1726 DatabaseQueries::delete_position(pool, *trader_id, id).await
1727 }
1728 DatabaseQuery::AddCurrency(currency) => {
1729 DatabaseQueries::add_currency(pool, currency).await
1730 }
1731 DatabaseQuery::AddInstrument(instrument) => {
1732 DatabaseQueries::add_instrument_any(pool, instrument).await
1733 }
1734 DatabaseQuery::AddInstrumentClose(close) => {
1735 DatabaseQueries::add_instrument_close(pool, &close).await
1736 }
1737 DatabaseQuery::AddOrder(event, client_id) => {
1738 DatabaseQueries::add_order(pool, event, client_id).await
1739 }
1740 DatabaseQuery::AddOrderSnapshot(snapshot) => {
1741 DatabaseQueries::add_order_snapshot(pool, snapshot).await
1742 }
1743 DatabaseQuery::AddPosition(position_id, event) => {
1744 DatabaseQueries::add_position(pool, position_id, &event).await
1745 }
1746 DatabaseQuery::AddPositionSnapshot(snapshot) => {
1747 DatabaseQueries::add_position_snapshot(pool, snapshot).await
1748 }
1749 DatabaseQuery::AddAccount(event, updated) => {
1750 DatabaseQueries::add_account(pool, updated, event, trader_id).await
1751 }
1752 DatabaseQuery::AddSignal(signal) => DatabaseQueries::add_signal(pool, &signal).await,
1753 DatabaseQuery::AddCustom(data) => DatabaseQueries::add_custom_data(pool, &data).await,
1754 DatabaseQuery::AddQuote(quote) => DatabaseQueries::add_quote(pool, "e).await,
1755 DatabaseQuery::AddTrade(trade) => DatabaseQueries::add_trade(pool, &trade).await,
1756 DatabaseQuery::AddBar(bar) => DatabaseQueries::add_bar(pool, &bar).await,
1757 DatabaseQuery::AddGreeks(greeks) => DatabaseQueries::add_greeks(pool, &greeks).await,
1758 DatabaseQuery::AddYieldCurve(curve) => {
1759 DatabaseQueries::add_yield_curve(pool, &curve).await
1760 }
1761 DatabaseQuery::UpdateOrder(event) => {
1762 DatabaseQueries::add_order_event(pool, event.into_boxed(), None).await
1763 }
1764 DatabaseQuery::UpdatePosition(event) => {
1765 DatabaseQueries::update_position(pool, &event).await
1766 }
1767 DatabaseQuery::IndexOrderPosition(client_order_id, position_id) => {
1768 DatabaseQueries::index_order_position(pool, trader_id, client_order_id, position_id)
1769 .await
1770 }
1771 DatabaseQuery::IndexOrderClients(claims) => {
1772 DatabaseQueries::index_order_clients(pool, trader_id, &claims).await
1773 }
1774 };
1775
1776 if let Err(e) = result {
1777 log::error!("Error on query: {e:?}");
1778 }
1779 }
1780}