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 ('account_event', 'trader_id'),
518 ('order_position_index', 'trader_id'),
519 ('order_event', 'released_price'),
520 ('order_event', 'protection_price'),
521 ('order_event', 'due_post_only'),
522 ('order_event', 'correction_id'),
523 ('order_event', 'is_reopened'),
524 ('order_event', 'info'),
525 ('order_event', 'causation_id'),
526 ('position_event', 'reconciliation'),
527 ('position_event', 'info'),
528 ('position_event', 'causation_id')
529 UNION ALL
530 SELECT * FROM (VALUES
531 ('instrument', 'defi_currency_codes'),
532 ('defi_currency', 'currency_id')
533 ) AS defi(table_name, column_name)
534 WHERE $1
535 ) AS required(table_name, column_name)
536 WHERE NOT EXISTS (
537 SELECT 1
538 FROM information_schema.columns
539 WHERE table_schema = current_schema()
540 AND table_name = required.table_name
541 AND column_name = required.column_name
542 )
543 ORDER BY 1",
544 )
545 .bind(cfg!(feature = "defi"))
546 .fetch_all(pool)
547 .await?;
548
549 if missing.is_empty() {
550 return Ok(());
551 }
552
553 Err(sqlx::Error::Configuration(
554 format!(
555 "Postgres schema is out of date, missing cache columns {}: {SCHEMA_MIGRATION_COMMAND}",
556 missing.join(", "),
557 )
558 .into(),
559 ))
560}
561
562async fn check_trader_keys(pool: &PgPool) -> Result<(), sqlx::Error> {
565 let unscoped_keys: Vec<String> = sqlx::query_scalar(
566 "SELECT constraints.table_name
567 FROM information_schema.table_constraints AS constraints
568 WHERE constraints.table_schema = current_schema()
569 AND constraints.constraint_type = 'PRIMARY KEY'
570 AND constraints.table_name IN ('order', 'position', 'order_position_index')
571 AND NOT EXISTS (
572 SELECT 1
573 FROM information_schema.key_column_usage AS keys
574 WHERE keys.constraint_schema = constraints.constraint_schema
575 AND keys.constraint_name = constraints.constraint_name
576 AND keys.column_name = 'trader_id'
577 )
578 ORDER BY 1",
579 )
580 .fetch_all(pool)
581 .await?;
582
583 if unscoped_keys.is_empty() {
584 return Ok(());
585 }
586
587 Err(sqlx::Error::Configuration(
588 format!(
589 "Postgres schema is out of date, primary keys of {} are not trader-qualified: \
590 {SCHEMA_MIGRATION_COMMAND}",
591 unscoped_keys.join(", "),
592 )
593 .into(),
594 ))
595}
596
597async fn check_account_ownership(pool: &PgPool) -> Result<(), sqlx::Error> {
602 let account_ids = DatabaseQueries::load_unassigned_account_ids(pool)
603 .await
604 .map_err(|e| {
605 sqlx::Error::Configuration(
606 format!("Failed to check account event ownership: {e}").into(),
607 )
608 })?;
609
610 if account_ids.is_empty() {
611 return Ok(());
612 }
613
614 let account_ids: Vec<String> = account_ids.iter().map(ToString::to_string).collect();
615 Err(sqlx::Error::Configuration(
616 format!(
617 "Postgres cache has account events with no trader for {}: assign each account with \
618 `nautilus database assign-account --account-id <ACCOUNT_ID> --trader-id <TRADER_ID>` \
619 before connecting",
620 account_ids.join(", "),
621 )
622 .into(),
623 ))
624}
625
626async fn handle_query(
627 maybe_msg: Option<DatabaseQuery>,
628 buffer: &mut VecDeque<DatabaseQuery>,
629 buffer_interval: Duration,
630 pool: &PgPool,
631 trader_id: &TraderId,
632) -> ControlFlow<()> {
633 let Some(msg) = maybe_msg else {
634 log::debug!("Command channel closed");
635 return ControlFlow::Break(());
636 };
637
638 #[cfg(feature = "defi")]
639 let msg = match msg {
640 DatabaseQuery::DefiCurrency(currency, reply) => {
641 if !buffer.is_empty() {
642 drain_buffer(pool, trader_id, buffer).await;
643 }
644
645 let result = DatabaseQueries::add_currency(pool, currency).await;
646 let _ = reply.send(result);
647 return ControlFlow::Continue(());
648 }
649 DatabaseQuery::DefiInstrument(instrument, migration, reply) => {
650 if !buffer.is_empty() {
651 drain_buffer(pool, trader_id, buffer).await;
652 }
653
654 let result = DatabaseQueries::add_defi_instrument(pool, instrument, migration).await;
655 let _ = reply.send(result);
656 return ControlFlow::Continue(());
657 }
658 other => other,
659 };
660
661 if matches!(msg, DatabaseQuery::Close) {
662 if !buffer.is_empty() {
663 drain_buffer(pool, trader_id, buffer).await;
664 }
665 return ControlFlow::Break(());
666 }
667
668 buffer.push_back(msg);
669
670 if buffer_interval.is_zero() {
671 drain_buffer(pool, trader_id, buffer).await;
672 }
673
674 ControlFlow::Continue(())
675}
676
677async fn flush_buffer(
678 buffer: &mut VecDeque<DatabaseQuery>,
679 pool: &PgPool,
680 trader_id: &TraderId,
681 flush_timer: &mut Pin<&mut tokio::time::Sleep>,
682 buffer_interval: Duration,
683) {
684 if !buffer.is_empty() {
685 drain_buffer(pool, trader_id, buffer).await;
686 }
687 flush_timer.as_mut().reset(Instant::now() + buffer_interval);
688}
689
690pub async fn get_pg_cache_database(trader_id: TraderId) -> anyhow::Result<PostgresCacheDatabase> {
696 Ok(PostgresCacheDatabase::connect(PostgresCacheConfig::default(), trader_id).await?)
697}
698
699#[async_trait::async_trait]
700impl CacheDatabaseAdapter for PostgresCacheDatabase {
701 fn close(&mut self) -> anyhow::Result<()> {
702 let pool = self.pool.clone();
703 let (tx, rx) = std::sync::mpsc::channel();
704
705 if let Err(e) = self.tx.send(DatabaseQuery::Close) {
707 log::warn!("Error sending close: {e:?}");
708 }
709
710 log_task_awaiting("cache-write");
711
712 tokio::task::block_in_place(|| {
713 if let Err(e) = get_runtime().block_on(&mut self.handle) {
714 log::error!("Error awaiting task 'cache-write': {e:?}");
715 }
716 });
717
718 log::debug!("Closing connection pool");
719
720 tokio::task::block_in_place(|| {
721 get_runtime().block_on(async {
722 pool.close().await;
723
724 if let Err(e) = tx.send(()) {
725 log::error!("Error closing pool: {e:?}");
726 }
727 });
728 });
729
730 log::debug!("Closed");
731
732 Ok(rx.recv()?)
733 }
734
735 fn flush(&mut self) -> anyhow::Result<()> {
736 let pool = self.pool.clone();
737 let trader_id = self.trader_id;
738
739 tokio::task::block_in_place(|| {
740 get_runtime().block_on(DatabaseQueries::delete_trader(&pool, &trader_id))
741 })
742 .map_err(|e| anyhow::anyhow!("Failed to flush Postgres cache for {trader_id}: {e}"))
743 }
744
745 async fn load_all(&self) -> anyhow::Result<CacheMap> {
746 let mut currencies = self.load_currencies().await?;
747 register_loaded_currencies(&mut currencies)?;
748
749 let (
750 instruments,
751 instrument_closes,
752 synthetics,
753 accounts,
754 orders,
755 positions,
756 greeks,
757 yield_curves,
758 ) = try_join!(
759 self.load_instruments(),
760 self.load_instrument_closes(),
761 self.load_synthetics(),
762 self.load_accounts(),
763 self.load_orders(),
764 self.load_positions(),
765 self.load_greeks(),
766 self.load_yield_curves()
767 )
768 .map_err(|e| anyhow::anyhow!("Error loading cache data: {e}"))?;
769
770 Ok(CacheMap {
771 currencies,
772 instruments,
773 instrument_closes,
774 synthetics,
775 accounts,
776 orders,
777 positions,
778 greeks,
779 yield_curves,
780 })
781 }
782
783 fn load(&self) -> anyhow::Result<AHashMap<String, Bytes>> {
784 let pool = self.pool.clone();
785 let (tx, rx) = std::sync::mpsc::channel();
786
787 tokio::spawn(async move {
788 let result = DatabaseQueries::load(&pool).await;
789 match result {
790 Ok(items) => {
791 let mapping = items
792 .into_iter()
793 .filter(|(key, _)| !key.starts_with(CACHE_DATABASE_KEY_PREFIX))
794 .map(|(k, v)| (k, Bytes::from(v)))
795 .collect();
796
797 if let Err(e) = tx.send(mapping) {
798 log::error!("Failed to send general items: {e:?}");
799 }
800 }
801 Err(e) => {
802 log::error!("Failed to load general items: {e:?}");
803 if let Err(e) = tx.send(AHashMap::new()) {
804 log::error!("Failed to send empty general items: {e:?}");
805 }
806 }
807 }
808 });
809 Ok(rx.recv()?)
810 }
811
812 async fn load_currencies(&self) -> anyhow::Result<AHashMap<Ustr, Currency>> {
813 match DatabaseQueries::load_currencies(&self.pool).await {
814 Ok(items) => Ok(items
815 .into_iter()
816 .map(|currency| (currency.code, currency))
817 .collect()),
818 Err(e) => {
819 #[cfg(feature = "defi")]
820 if e.is::<crate::defi::IntegrityError>() {
821 return Err(e);
822 }
823
824 log::error!("Failed to load_currencies: {e}");
825 Ok(AHashMap::new())
826 }
827 }
828 }
829
830 async fn load_instruments(&self) -> anyhow::Result<AHashMap<InstrumentId, InstrumentAny>> {
831 match DatabaseQueries::load_instruments(&self.pool).await {
832 Ok(items) => Ok(items
833 .into_iter()
834 .map(|instrument| (instrument.id(), instrument))
835 .collect()),
836 Err(e) => {
837 #[cfg(feature = "defi")]
838 if e.is::<crate::defi::IntegrityError>() {
839 return Err(e);
840 }
841
842 log::error!("Failed to load_instruments: {e}");
843 Ok(AHashMap::new())
844 }
845 }
846 }
847
848 async fn load_instrument_closes(
849 &self,
850 ) -> anyhow::Result<AHashMap<InstrumentId, InstrumentClose>> {
851 Ok(DatabaseQueries::load_instrument_closes(&self.pool)
852 .await?
853 .into_iter()
854 .map(|close| (close.instrument_id, close))
855 .collect())
856 }
857
858 async fn load_synthetics(&self) -> anyhow::Result<AHashMap<InstrumentId, SyntheticInstrument>> {
859 let prefix = format!("{CACHE_DATABASE_KEY_PREFIX}synthetic:");
860 let values: Vec<Vec<u8>> =
861 sqlx::query_scalar("SELECT value FROM general WHERE starts_with(id, $1)")
862 .bind(prefix)
863 .fetch_all(&self.pool)
864 .await?;
865 values
866 .into_iter()
867 .map(|value| {
868 let synthetic: SyntheticInstrument = serde_json::from_slice(&value)?;
869 Ok((synthetic.id, synthetic))
870 })
871 .collect()
872 }
873
874 async fn load_accounts(&self) -> anyhow::Result<AHashMap<AccountId, AccountAny>> {
875 let pool = self.pool.clone();
876 let trader_id = self.trader_id;
877 let (tx, rx) = std::sync::mpsc::channel();
878
879 tokio::spawn(async move {
880 let result = DatabaseQueries::load_accounts(&pool, &trader_id).await;
881 match result {
882 Ok(accounts) => {
883 let mapping = accounts
884 .into_iter()
885 .map(|account| (account.id(), account))
886 .collect();
887
888 if let Err(e) = tx.send(mapping) {
889 log::error!("Failed to send accounts: {e:?}");
890 }
891 }
892 Err(e) => {
893 log::error!("Failed to load accounts: {e:?}");
894 if let Err(e) = tx.send(AHashMap::new()) {
895 log::error!("Failed to send empty accounts: {e:?}");
896 }
897 }
898 }
899 });
900 Ok(rx.recv()?)
901 }
902
903 async fn load_orders(&self) -> anyhow::Result<AHashMap<ClientOrderId, OrderAny>> {
904 let pool = self.pool.clone();
905 let trader_id = self.trader_id;
906 let (tx, rx) = std::sync::mpsc::channel();
907
908 tokio::spawn(async move {
909 let result = DatabaseQueries::load_orders(&pool, &trader_id).await;
910 match result {
911 Ok(orders) => {
912 let mapping = orders
913 .into_iter()
914 .map(|order| (order.client_order_id(), order))
915 .collect();
916
917 if let Err(e) = tx.send(mapping) {
918 log::error!("Failed to send orders: {e:?}");
919 }
920 }
921 Err(e) => {
922 log::error!("Failed to load orders: {e:?}");
923 if let Err(e) = tx.send(AHashMap::new()) {
924 log::error!("Failed to send empty orders: {e:?}");
925 }
926 }
927 }
928 });
929 Ok(rx.recv()?)
930 }
931
932 async fn load_positions(&self) -> anyhow::Result<AHashMap<PositionId, Position>> {
933 let pool = self.pool.clone();
934 let trader_id = self.trader_id;
935 let (tx, rx) = std::sync::mpsc::channel();
936
937 tokio::spawn(async move {
938 let result = DatabaseQueries::load_positions(&pool, &trader_id)
939 .await
940 .map(|positions| {
941 positions
942 .into_iter()
943 .map(|position| (position.id, position))
944 .collect()
945 });
946
947 if let Err(e) = tx.send(result) {
948 log::error!("Failed to send positions: {e:?}");
949 }
950 });
951 rx.recv()?
952 }
953
954 async fn load_greeks(&self) -> anyhow::Result<AHashMap<InstrumentId, GreeksData>> {
955 DatabaseQueries::load_greeks(&self.pool).await
956 }
957
958 async fn load_yield_curves(&self) -> anyhow::Result<AHashMap<String, YieldCurve>> {
959 DatabaseQueries::load_yield_curves(&self.pool).await
960 }
961
962 fn load_index_order_position(&self) -> anyhow::Result<AHashMap<ClientOrderId, PositionId>> {
963 let pool = self.pool.clone();
964 let trader_id = self.trader_id;
965 let (tx, rx) = std::sync::mpsc::channel();
966
967 tokio::spawn(async move {
968 let result = DatabaseQueries::load_index_order_position(&pool, &trader_id).await;
969 match result {
970 Ok(index) => {
971 if let Err(e) = tx.send(index) {
972 log::error!("Failed to send load_index_order_position result: {e:?}");
973 }
974 }
975 Err(e) => {
976 log::error!("Failed to run query load_index_order_position: {e:?}");
977 if let Err(e) = tx.send(AHashMap::new()) {
978 log::error!("Failed to send empty load_index_order_position result: {e:?}");
979 }
980 }
981 }
982 });
983 Ok(rx.recv()?)
984 }
985
986 fn load_index_order_client(&self) -> anyhow::Result<AHashMap<ClientOrderId, ClientId>> {
987 let pool = self.pool.clone();
988 let trader_id = self.trader_id;
989 let (tx, rx) = std::sync::mpsc::channel();
990
991 tokio::spawn(async move {
992 let result =
993 DatabaseQueries::load_distinct_order_event_client_ids(&pool, &trader_id).await;
994
995 match result {
996 Ok(currency) => {
997 if let Err(e) = tx.send(currency) {
998 log::error!("Failed to send load_index_order_client result: {e:?}");
999 }
1000 }
1001 Err(e) => {
1002 log::error!("Failed to run query load_distinct_order_event_client_ids: {e:?}");
1003 if let Err(e) = tx.send(AHashMap::new()) {
1004 log::error!("Failed to send empty load_index_order_client result: {e:?}");
1005 }
1006 }
1007 }
1008 });
1009 Ok(rx.recv()?)
1010 }
1011
1012 async fn load_currency(&self, code: &Ustr) -> anyhow::Result<Option<Currency>> {
1013 match DatabaseQueries::load_currency(&self.pool, code).await {
1014 Ok(items) => Ok(items),
1015 Err(e) => {
1016 #[cfg(feature = "defi")]
1017 if e.is::<crate::defi::IntegrityError>() {
1018 return Err(e);
1019 }
1020
1021 log::error!("Failed to load_currency: {e}");
1022 Ok(None)
1023 }
1024 }
1025 }
1026
1027 async fn load_instrument(
1028 &self,
1029 instrument_id: &InstrumentId,
1030 ) -> anyhow::Result<Option<InstrumentAny>> {
1031 load_instrument(&self.pool, instrument_id).await
1032 }
1033
1034 async fn load_synthetic(
1035 &self,
1036 instrument_id: &InstrumentId,
1037 ) -> anyhow::Result<Option<SyntheticInstrument>> {
1038 DatabaseQueries::load_synthetic(&self.pool, instrument_id).await
1039 }
1040
1041 fn load_instrument_sync(
1042 &self,
1043 instrument_id: &InstrumentId,
1044 ) -> anyhow::Result<Option<InstrumentAny>> {
1045 let pool = self.pool.clone();
1046 let instrument_id = *instrument_id;
1047 block_on_nautilus_with(move || async move { load_instrument(&pool, &instrument_id).await })
1048 }
1049
1050 fn load_synthetic_sync(
1051 &self,
1052 instrument_id: &InstrumentId,
1053 ) -> anyhow::Result<Option<SyntheticInstrument>> {
1054 let pool = self.pool.clone();
1055 let instrument_id = *instrument_id;
1056 block_on_nautilus_with(move || async move {
1057 DatabaseQueries::load_synthetic(&pool, &instrument_id).await
1058 })
1059 }
1060
1061 async fn load_account(&self, account_id: &AccountId) -> anyhow::Result<Option<AccountAny>> {
1062 let pool = self.pool.clone();
1063 let trader_id = self.trader_id;
1064 let account_id = account_id.to_owned();
1065 let (tx, rx) = std::sync::mpsc::channel();
1066
1067 tokio::spawn(async move {
1068 let result = DatabaseQueries::load_account(&pool, &account_id, &trader_id).await;
1069 match result {
1070 Ok(account) => {
1071 if let Err(e) = tx.send(account) {
1072 log::error!("Failed to send account {account_id}: {e:?}");
1073 }
1074 }
1075 Err(e) => {
1076 log::error!("Failed to load account {account_id}: {e:?}");
1077 if let Err(e) = tx.send(None) {
1078 log::error!("Failed to send None for account {account_id}: {e:?}");
1079 }
1080 }
1081 }
1082 });
1083 Ok(rx.recv()?)
1084 }
1085
1086 async fn load_order(
1087 &self,
1088 client_order_id: &ClientOrderId,
1089 ) -> anyhow::Result<Option<OrderAny>> {
1090 let pool = self.pool.clone();
1091 let trader_id = self.trader_id;
1092 let client_order_id = client_order_id.to_owned();
1093 let (tx, rx) = std::sync::mpsc::channel();
1094
1095 tokio::spawn(async move {
1096 let result = DatabaseQueries::load_order(&pool, &client_order_id, &trader_id).await;
1097 match result {
1098 Ok(order) => {
1099 if let Err(e) = tx.send(order) {
1100 log::error!("Failed to send order {client_order_id}: {e:?}");
1101 }
1102 }
1103 Err(e) => {
1104 log::error!("Failed to load order {client_order_id}: {e:?}");
1105 let _ = tx.send(None);
1106 }
1107 }
1108 });
1109 Ok(rx.recv()?)
1110 }
1111
1112 async fn load_position(&self, position_id: &PositionId) -> anyhow::Result<Option<Position>> {
1113 let pool = self.pool.clone();
1114 let trader_id = self.trader_id;
1115 let position_id = position_id.to_owned();
1116 let (tx, rx) = std::sync::mpsc::channel();
1117
1118 tokio::spawn(async move {
1119 let result = DatabaseQueries::load_position(&pool, &position_id, &trader_id).await;
1120 if let Err(e) = tx.send(result) {
1121 log::error!("Failed to send position {position_id}: {e:?}");
1122 }
1123 });
1124 rx.recv()?
1125 }
1126
1127 fn load_actor(&self, actor_id: &ActorId) -> anyhow::Result<AHashMap<String, Bytes>> {
1128 self.load_state(format!(
1129 "{}actor:{actor_id}",
1130 trader_key_prefix(self.trader_id)
1131 ))
1132 }
1133
1134 fn delete_actor(&self, actor_id: &ActorId) -> anyhow::Result<()> {
1135 let query = DatabaseQuery::Delete(format!(
1136 "{}actor:{actor_id}",
1137 trader_key_prefix(self.trader_id)
1138 ));
1139 self.tx
1140 .send(query)
1141 .map_err(|e| anyhow::anyhow!("Failed to queue state deletion: {e}"))
1142 }
1143
1144 fn load_strategy(&self, strategy_id: &StrategyId) -> anyhow::Result<AHashMap<String, Bytes>> {
1145 self.load_state(format!(
1146 "{}strategy:{strategy_id}",
1147 trader_key_prefix(self.trader_id)
1148 ))
1149 }
1150
1151 fn delete_strategy(&self, strategy_id: &StrategyId) -> anyhow::Result<()> {
1152 let query = DatabaseQuery::Delete(format!(
1153 "{}strategy:{strategy_id}",
1154 trader_key_prefix(self.trader_id)
1155 ));
1156 self.tx
1157 .send(query)
1158 .map_err(|e| anyhow::anyhow!("Failed to queue state deletion: {e}"))
1159 }
1160
1161 fn delete_order(&self, client_order_id: &ClientOrderId) -> anyhow::Result<()> {
1162 self.tx
1163 .send(DatabaseQuery::DeleteOrder(*client_order_id))
1164 .map_err(|e| anyhow::anyhow!("Failed to queue order deletion: {e}"))
1165 }
1166
1167 fn delete_position(&self, position_id: &PositionId) -> anyhow::Result<()> {
1168 self.tx
1169 .send(DatabaseQuery::DeletePosition(*position_id))
1170 .map_err(|e| anyhow::anyhow!("Failed to queue position deletion: {e}"))
1171 }
1172
1173 fn delete_account_event(&self, account_id: &AccountId, event_id: &str) -> anyhow::Result<()> {
1174 log::warn!(
1175 "Deleting account events is a no-op pending replay-safe compaction, {account_id}: {event_id}"
1176 );
1177 Ok(())
1178 }
1179
1180 fn add(&self, key: String, value: Bytes) -> anyhow::Result<()> {
1181 let query = DatabaseQuery::Add(key, value.into());
1182 self.tx
1183 .send(query)
1184 .map_err(|e| anyhow::anyhow!("Failed to send query to database message handler: {e}"))
1185 }
1186
1187 fn add_currency(&self, currency: &Currency) -> anyhow::Result<()> {
1188 #[cfg(feature = "defi")]
1189 if nautilus_model::defi::currency::check(*currency)
1190 .map_err(crate::defi::integrity_error)?
1191 .is_some()
1192 || currency.code.contains('@')
1193 {
1194 return self.write_defi(|reply| DatabaseQuery::DefiCurrency(*currency, reply));
1195 }
1196
1197 let query = DatabaseQuery::AddCurrency(*currency);
1198 self.tx.send(query).map_err(|e| {
1199 anyhow::anyhow!("Failed to query add_currency to database message handler: {e}")
1200 })
1201 }
1202
1203 fn add_instrument(&self, instrument: &InstrumentAny) -> anyhow::Result<()> {
1204 #[cfg(feature = "defi")]
1205 {
1206 self.write_defi(|reply| DatabaseQuery::DefiInstrument(instrument.clone(), None, reply))
1207 }
1208
1209 #[cfg(not(feature = "defi"))]
1210 {
1211 let query = DatabaseQuery::AddInstrument(instrument.clone());
1212 self.tx.send(query).map_err(|e| {
1213 anyhow::anyhow!(
1214 "Failed to send query add_instrument to database message handler: {e}"
1215 )
1216 })
1217 }
1218 }
1219
1220 #[cfg(feature = "defi")]
1221 fn add_defi_instrument(
1222 &self,
1223 instrument: &InstrumentAny,
1224 migration: InstrumentMigration,
1225 ) -> anyhow::Result<()> {
1226 self.write_defi(|reply| {
1227 DatabaseQuery::DefiInstrument(instrument.clone(), Some(migration), reply)
1228 })
1229 }
1230
1231 fn add_instrument_close(&self, close: &InstrumentClose) -> anyhow::Result<()> {
1232 self.tx
1233 .send(DatabaseQuery::AddInstrumentClose(*close))
1234 .map_err(|e| {
1235 anyhow::anyhow!(
1236 "Failed to send query add_instrument_close to database message handler: {e}"
1237 )
1238 })
1239 }
1240
1241 fn add_synthetic(&self, synthetic: &SyntheticInstrument) -> anyhow::Result<()> {
1242 self.add(
1243 format!("{CACHE_DATABASE_KEY_PREFIX}synthetic:{}", synthetic.id),
1244 Bytes::from(serde_json::to_vec(synthetic)?),
1245 )
1246 }
1247
1248 fn add_account(&self, account: &AccountAny) -> anyhow::Result<()> {
1249 let query = DatabaseQuery::AddAccount(account_last_event(account)?, false);
1250 self.tx.send(query).map_err(|e| {
1251 anyhow::anyhow!("Failed to send query add_account to database message handler: {e}")
1252 })
1253 }
1254
1255 fn add_order(&self, order: &OrderAny, client_id: Option<ClientId>) -> anyhow::Result<()> {
1256 let query = DatabaseQuery::AddOrder(order_initialized_event(order), client_id);
1257 self.tx.send(query).map_err(|e| {
1258 anyhow::anyhow!("Failed to send query add_order to database message handler: {e}")
1259 })
1260 }
1261
1262 fn add_order_snapshot(&self, snapshot: &OrderSnapshot) -> anyhow::Result<()> {
1263 let query = DatabaseQuery::AddOrderSnapshot(snapshot.to_owned());
1264 self.tx.send(query).map_err(|e| {
1265 anyhow::anyhow!(
1266 "Failed to send query add_order_snapshot to database message handler: {e}"
1267 )
1268 })
1269 }
1270
1271 fn add_position(&self, position: &Position) -> anyhow::Result<()> {
1272 let event = position_last_event(position)?;
1273 let query = DatabaseQuery::AddPosition(position.id, event);
1274 self.tx.send(query).map_err(|e| {
1275 anyhow::anyhow!("Failed to send query add_position to database message handler: {e}")
1276 })?;
1277
1278 if position.requires_replay_state() {
1279 self.add_position_snapshot(&PositionSnapshot::from_replay_state(position, None))?;
1280 }
1281
1282 Ok(())
1283 }
1284
1285 fn add_position_snapshot(&self, snapshot: &PositionSnapshot) -> anyhow::Result<()> {
1286 let query = DatabaseQuery::AddPositionSnapshot(snapshot.to_owned());
1287 self.tx.send(query).map_err(|e| {
1288 anyhow::anyhow!(
1289 "Failed to send query add_position_snapshot to database message handler: {e}"
1290 )
1291 })
1292 }
1293
1294 fn add_order_book(&self, _order_book: &OrderBook) -> anyhow::Result<()> {
1295 anyhow::bail!("Postgres order book persistence is not supported")
1296 }
1297
1298 fn add_quote(&self, quote: &QuoteTick) -> anyhow::Result<()> {
1299 let query = DatabaseQuery::AddQuote(quote.to_owned());
1300 self.tx.send(query).map_err(|e| {
1301 anyhow::anyhow!("Failed to send query add_quote to database message handler: {e}")
1302 })
1303 }
1304
1305 fn load_quotes(&self, instrument_id: &InstrumentId) -> anyhow::Result<Vec<QuoteTick>> {
1306 let pool = self.pool.clone();
1307 let instrument_id = instrument_id.to_owned();
1308 let (tx, rx) = std::sync::mpsc::channel();
1309
1310 tokio::spawn(async move {
1311 let result = DatabaseQueries::load_quotes(&pool, &instrument_id).await;
1312 match result {
1313 Ok(quotes) => {
1314 if let Err(e) = tx.send(quotes) {
1315 log::error!("Failed to send quotes for instrument {instrument_id}: {e:?}");
1316 }
1317 }
1318 Err(e) => {
1319 log::error!("Failed to load quotes for instrument {instrument_id}: {e:?}");
1320 if let Err(e) = tx.send(Vec::new()) {
1321 log::error!(
1322 "Failed to send empty quotes for instrument {instrument_id}: {e:?}"
1323 );
1324 }
1325 }
1326 }
1327 });
1328 Ok(rx.recv()?)
1329 }
1330
1331 fn add_trade(&self, trade: &TradeTick) -> anyhow::Result<()> {
1332 let query = DatabaseQuery::AddTrade(trade.to_owned());
1333 self.tx.send(query).map_err(|e| {
1334 anyhow::anyhow!("Failed to send query add_trade to database message handler: {e}")
1335 })
1336 }
1337
1338 fn load_trades(&self, instrument_id: &InstrumentId) -> anyhow::Result<Vec<TradeTick>> {
1339 let pool = self.pool.clone();
1340 let instrument_id = instrument_id.to_owned();
1341 let (tx, rx) = std::sync::mpsc::channel();
1342
1343 tokio::spawn(async move {
1344 let result = DatabaseQueries::load_trades(&pool, &instrument_id).await;
1345 match result {
1346 Ok(trades) => {
1347 if let Err(e) = tx.send(trades) {
1348 log::error!("Failed to send trades for instrument {instrument_id}: {e:?}");
1349 }
1350 }
1351 Err(e) => {
1352 log::error!("Failed to load trades for instrument {instrument_id}: {e:?}");
1353 if let Err(e) = tx.send(Vec::new()) {
1354 log::error!(
1355 "Failed to send empty trades for instrument {instrument_id}: {e:?}"
1356 );
1357 }
1358 }
1359 }
1360 });
1361 Ok(rx.recv()?)
1362 }
1363
1364 fn add_funding_rate(&self, _funding_rate: &FundingRateUpdate) -> anyhow::Result<()> {
1365 anyhow::bail!("add_funding_rate not implemented for PostgreSQL cache adapter")
1366 }
1367
1368 fn load_funding_rates(
1369 &self,
1370 _instrument_id: &InstrumentId,
1371 ) -> anyhow::Result<Vec<FundingRateUpdate>> {
1372 anyhow::bail!("load_funding_rates not implemented for PostgreSQL cache adapter")
1373 }
1374
1375 fn add_bar(&self, bar: &Bar) -> anyhow::Result<()> {
1376 let query = DatabaseQuery::AddBar(bar.to_owned());
1377 self.tx.send(query).map_err(|e| {
1378 anyhow::anyhow!("Failed to send query add_bar to database message handler: {e}")
1379 })
1380 }
1381
1382 fn load_bars(&self, instrument_id: &InstrumentId) -> anyhow::Result<Vec<Bar>> {
1383 let pool = self.pool.clone();
1384 let instrument_id = instrument_id.to_owned();
1385 let (tx, rx) = std::sync::mpsc::channel();
1386
1387 tokio::spawn(async move {
1388 let result = DatabaseQueries::load_bars(&pool, &instrument_id).await;
1389 match result {
1390 Ok(bars) => {
1391 if let Err(e) = tx.send(bars) {
1392 log::error!("Failed to send bars for instrument {instrument_id}: {e:?}");
1393 }
1394 }
1395 Err(e) => {
1396 log::error!("Failed to load bars for instrument {instrument_id}: {e:?}");
1397 if let Err(e) = tx.send(Vec::new()) {
1398 log::error!(
1399 "Failed to send empty bars for instrument {instrument_id}: {e:?}"
1400 );
1401 }
1402 }
1403 }
1404 });
1405 Ok(rx.recv()?)
1406 }
1407
1408 fn add_greeks(&self, greeks: &GreeksData) -> anyhow::Result<()> {
1409 anyhow::ensure!(
1410 [
1411 greeks.strike,
1412 greeks.expiry_in_years,
1413 greeks.multiplier,
1414 greeks.quantity,
1415 greeks.underlying_price,
1416 greeks.interest_rate,
1417 greeks.cost_of_carry,
1418 greeks.vol,
1419 greeks.pnl,
1420 greeks.price,
1421 greeks.delta,
1422 greeks.gamma,
1423 greeks.vega,
1424 greeks.theta,
1425 greeks.rho,
1426 greeks.itm_prob,
1427 ]
1428 .iter()
1429 .all(|value| value.is_finite()),
1430 "Cannot persist non-finite GreeksData"
1431 );
1432 self.tx
1433 .send(DatabaseQuery::AddGreeks(greeks.clone()))
1434 .map_err(|e| {
1435 anyhow::anyhow!("Failed to send query add_greeks to database message handler: {e}")
1436 })
1437 }
1438
1439 fn add_yield_curve(&self, curve: &YieldCurve) -> anyhow::Result<()> {
1440 curve.validate()?;
1441 self.tx
1442 .send(DatabaseQuery::AddYieldCurve(curve.clone()))
1443 .map_err(|e| {
1444 anyhow::anyhow!(
1445 "Failed to send query add_yield_curve to database message handler: {e}"
1446 )
1447 })
1448 }
1449
1450 fn add_signal(&self, signal: &Signal) -> anyhow::Result<()> {
1451 let query = DatabaseQuery::AddSignal(signal.to_owned());
1452 self.tx.send(query).map_err(|e| {
1453 anyhow::anyhow!("Failed to send query add_signal to database message handler: {e}")
1454 })
1455 }
1456
1457 fn load_signals(&self, name: &str) -> anyhow::Result<Vec<Signal>> {
1458 let pool = self.pool.clone();
1459 let name = name.to_owned();
1460 let (tx, rx) = std::sync::mpsc::channel();
1461
1462 tokio::spawn(async move {
1463 let result = DatabaseQueries::load_signals(&pool, &name).await;
1464 match result {
1465 Ok(signals) => {
1466 if let Err(e) = tx.send(signals) {
1467 log::error!("Failed to send signals for '{name}': {e:?}");
1468 }
1469 }
1470 Err(e) => {
1471 log::error!("Failed to load signals for '{name}': {e:?}");
1472 if let Err(e) = tx.send(Vec::new()) {
1473 log::error!("Failed to send empty signals for '{name}': {e:?}");
1474 }
1475 }
1476 }
1477 });
1478 Ok(rx.recv()?)
1479 }
1480
1481 fn add_custom_data(&self, data: &CustomData) -> anyhow::Result<()> {
1482 let query = DatabaseQuery::AddCustom(data.to_owned());
1483 self.tx.send(query).map_err(|e| {
1484 anyhow::anyhow!("Failed to send query add_signal to database message handler: {e}")
1485 })
1486 }
1487
1488 fn load_custom_data(&self, data_type: &DataType) -> anyhow::Result<Vec<CustomData>> {
1489 let pool = self.pool.clone();
1490 let data_type = data_type.to_owned();
1491 let (tx, rx) = std::sync::mpsc::channel();
1492
1493 tokio::spawn(async move {
1494 let result = DatabaseQueries::load_custom_data(&pool, &data_type).await;
1495 match result {
1496 Ok(signals) => {
1497 if let Err(e) = tx.send(signals) {
1498 log::error!("Failed to send custom data for '{data_type}': {e:?}");
1499 }
1500 }
1501 Err(e) => {
1502 log::error!("Failed to load custom data for '{data_type}': {e:?}");
1503 if let Err(e) = tx.send(Vec::new()) {
1504 log::error!("Failed to send empty custom data for '{data_type}': {e:?}");
1505 }
1506 }
1507 }
1508 });
1509 Ok(rx.recv()?)
1510 }
1511
1512 fn load_order_snapshot(
1513 &self,
1514 client_order_id: &ClientOrderId,
1515 ) -> anyhow::Result<Option<OrderSnapshot>> {
1516 let pool = self.pool.clone();
1517 let trader_id = self.trader_id;
1518 let client_order_id = client_order_id.to_owned();
1519 let (tx, rx) = std::sync::mpsc::channel();
1520
1521 tokio::spawn(async move {
1522 let result =
1523 DatabaseQueries::load_order_snapshot(&pool, &client_order_id, &trader_id).await;
1524
1525 match result {
1526 Ok(snapshot) => {
1527 if let Err(e) = tx.send(snapshot) {
1528 log::error!("Failed to send order snapshot {client_order_id}: {e:?}");
1529 }
1530 }
1531 Err(e) => {
1532 log::error!("Failed to load order snapshot {client_order_id}: {e:?}");
1533 if let Err(e) = tx.send(None) {
1534 log::error!(
1535 "Failed to send None for order snapshot {client_order_id}: {e:?}"
1536 );
1537 }
1538 }
1539 }
1540 });
1541 Ok(rx.recv()?)
1542 }
1543
1544 fn load_position_snapshot(
1545 &self,
1546 position_id: &PositionId,
1547 ) -> anyhow::Result<Option<PositionSnapshot>> {
1548 let pool = self.pool.clone();
1549 let trader_id = self.trader_id;
1550 let position_id = position_id.to_owned();
1551 let (tx, rx) = std::sync::mpsc::channel();
1552
1553 tokio::spawn(async move {
1554 let result =
1555 DatabaseQueries::load_position_snapshot(&pool, &position_id, &trader_id).await;
1556
1557 match result {
1558 Ok(snapshot) => {
1559 if let Err(e) = tx.send(snapshot) {
1560 log::error!("Failed to send position snapshot {position_id}: {e:?}");
1561 }
1562 }
1563 Err(e) => {
1564 log::error!("Failed to load position snapshot {position_id}: {e:?}");
1565 if let Err(e) = tx.send(None) {
1566 log::error!(
1567 "Failed to send None for position snapshot {position_id}: {e:?}"
1568 );
1569 }
1570 }
1571 }
1572 });
1573 Ok(rx.recv()?)
1574 }
1575
1576 fn index_venue_order_id(
1577 &self,
1578 _client_order_id: ClientOrderId,
1579 _venue_order_id: VenueOrderId,
1580 ) -> anyhow::Result<()> {
1581 todo!()
1582 }
1583
1584 fn index_order_position(
1585 &self,
1586 client_order_id: ClientOrderId,
1587 position_id: PositionId,
1588 ) -> anyhow::Result<()> {
1589 let query = DatabaseQuery::IndexOrderPosition(client_order_id, position_id);
1590 self.tx.send(query).map_err(|e| {
1591 anyhow::anyhow!(
1592 "Failed to send query index_order_position to database message handler: {e}"
1593 )
1594 })
1595 }
1596
1597 fn index_order_clients(&self, claims: &[(ClientOrderId, ClientId)]) -> anyhow::Result<()> {
1598 if claims.is_empty() {
1599 return Ok(());
1600 }
1601
1602 let query = DatabaseQuery::IndexOrderClients(claims.to_vec());
1603 self.tx.send(query).map_err(|e| {
1604 anyhow::anyhow!(
1605 "Failed to send query index_order_clients to database message handler: {e}"
1606 )
1607 })
1608 }
1609
1610 fn update_actor(
1611 &self,
1612 actor_id: &ActorId,
1613 state: &AHashMap<String, Bytes>,
1614 ) -> anyhow::Result<()> {
1615 self.add(
1616 format!("{}actor:{actor_id}", trader_key_prefix(self.trader_id)),
1617 Bytes::from(serde_json::to_vec(state)?),
1618 )
1619 }
1620
1621 fn update_strategy(
1622 &self,
1623 strategy_id: &StrategyId,
1624 state: &AHashMap<String, Bytes>,
1625 ) -> anyhow::Result<()> {
1626 self.add(
1627 format!(
1628 "{}strategy:{strategy_id}",
1629 trader_key_prefix(self.trader_id)
1630 ),
1631 Bytes::from(serde_json::to_vec(state)?),
1632 )
1633 }
1634
1635 fn update_account(&self, account: &AccountAny) -> anyhow::Result<()> {
1636 let query = DatabaseQuery::AddAccount(account_last_event(account)?, true);
1637 self.tx.send(query).map_err(|e| {
1638 anyhow::anyhow!("Failed to send query add_account to database message handler: {e}")
1639 })
1640 }
1641
1642 fn update_order(&self, event: &OrderEventAny) -> anyhow::Result<()> {
1643 let query = DatabaseQuery::UpdateOrder(event.clone());
1644 self.tx.send(query).map_err(|e| {
1645 anyhow::anyhow!("Failed to send query update_order to database message handler: {e}")
1646 })
1647 }
1648
1649 fn update_position(&self, position: &Position) -> anyhow::Result<()> {
1650 let query = if position.requires_replay_state() {
1651 DatabaseQuery::AddPositionSnapshot(PositionSnapshot::from_replay_state(position, None))
1652 } else {
1653 DatabaseQuery::UpdatePosition(position_last_event(position)?)
1654 };
1655 self.tx.send(query).map_err(|e| {
1656 anyhow::anyhow!("Failed to send query update_position to database message handler: {e}")
1657 })
1658 }
1659
1660 fn snapshot_order_state(&self, order: &OrderAny) -> anyhow::Result<()> {
1661 let snapshot = OrderSnapshot::from(order.clone());
1662 self.add_order_snapshot(&snapshot)
1663 }
1664
1665 fn snapshot_position_state(
1666 &self,
1667 position: &Position,
1668 ts_snapshot: UnixNanos,
1669 unrealized_pnl: Option<Money>,
1670 ) -> anyhow::Result<()> {
1671 let mut snapshot = if position.requires_replay_state() {
1672 PositionSnapshot::from_replay_state(position, unrealized_pnl)
1673 } else {
1674 PositionSnapshot::from(position, unrealized_pnl)
1675 };
1676 snapshot.ts_init = ts_snapshot;
1677 self.add_position_snapshot(&snapshot)
1678 }
1679
1680 fn heartbeat(&self, timestamp: UnixNanos) -> anyhow::Result<()> {
1681 self.add(
1682 format!("{}health:heartbeat", trader_key_prefix(self.trader_id)),
1683 Bytes::from(serde_json::to_vec(×tamp)?),
1684 )
1685 }
1686}
1687
1688async fn load_instrument(
1689 pool: &PgPool,
1690 instrument_id: &InstrumentId,
1691) -> anyhow::Result<Option<InstrumentAny>> {
1692 let mut currencies = DatabaseQueries::load_currencies(pool)
1693 .await?
1694 .into_iter()
1695 .map(|currency| (currency.code, currency))
1696 .collect();
1697 register_loaded_currencies(&mut currencies)?;
1698 DatabaseQueries::load_instrument(pool, instrument_id).await
1699}
1700
1701fn account_last_event(account: &AccountAny) -> anyhow::Result<AccountState> {
1702 account
1703 .last_event()
1704 .ok_or_else(|| anyhow::anyhow!("Cannot persist account with no events: {}", account.id()))
1705}
1706
1707fn order_initialized_event(order: &OrderAny) -> OrderInitialized {
1708 order.init_event().clone()
1709}
1710
1711fn position_last_event(position: &Position) -> anyhow::Result<OrderFilled> {
1712 position
1713 .last_event()
1714 .ok_or_else(|| anyhow::anyhow!("Cannot persist position with no events: {}", position.id))
1715}
1716
1717async fn drain_buffer(pool: &PgPool, trader_id: &TraderId, buffer: &mut VecDeque<DatabaseQuery>) {
1718 for cmd in buffer.drain(..) {
1719 let result: anyhow::Result<()> = match cmd {
1720 #[cfg(feature = "defi")]
1721 DatabaseQuery::DefiCurrency(..) | DatabaseQuery::DefiInstrument(..) => {
1722 unreachable!("DeFi writes execute before buffering")
1723 }
1724 DatabaseQuery::Close => Ok(()),
1725 DatabaseQuery::Add(key, value) => DatabaseQueries::add(pool, key, value).await,
1726 DatabaseQuery::Delete(key) => DatabaseQueries::delete(pool, &key).await,
1727 DatabaseQuery::DeleteOrder(id) => {
1728 DatabaseQueries::delete_order(pool, *trader_id, id).await
1729 }
1730 DatabaseQuery::DeletePosition(id) => {
1731 DatabaseQueries::delete_position(pool, *trader_id, id).await
1732 }
1733 DatabaseQuery::AddCurrency(currency) => {
1734 DatabaseQueries::add_currency(pool, currency).await
1735 }
1736 DatabaseQuery::AddInstrument(instrument) => {
1737 DatabaseQueries::add_instrument_any(pool, instrument).await
1738 }
1739 DatabaseQuery::AddInstrumentClose(close) => {
1740 DatabaseQueries::add_instrument_close(pool, &close).await
1741 }
1742 DatabaseQuery::AddOrder(event, client_id) => {
1743 DatabaseQueries::add_order(pool, event, client_id).await
1744 }
1745 DatabaseQuery::AddOrderSnapshot(snapshot) => {
1746 DatabaseQueries::add_order_snapshot(pool, snapshot).await
1747 }
1748 DatabaseQuery::AddPosition(position_id, event) => {
1749 DatabaseQueries::add_position(pool, position_id, &event).await
1750 }
1751 DatabaseQuery::AddPositionSnapshot(snapshot) => {
1752 DatabaseQueries::add_position_snapshot(pool, snapshot).await
1753 }
1754 DatabaseQuery::AddAccount(event, updated) => {
1755 DatabaseQueries::add_account(pool, updated, event, trader_id).await
1756 }
1757 DatabaseQuery::AddSignal(signal) => DatabaseQueries::add_signal(pool, &signal).await,
1758 DatabaseQuery::AddCustom(data) => DatabaseQueries::add_custom_data(pool, &data).await,
1759 DatabaseQuery::AddQuote(quote) => DatabaseQueries::add_quote(pool, "e).await,
1760 DatabaseQuery::AddTrade(trade) => DatabaseQueries::add_trade(pool, &trade).await,
1761 DatabaseQuery::AddBar(bar) => DatabaseQueries::add_bar(pool, &bar).await,
1762 DatabaseQuery::AddGreeks(greeks) => DatabaseQueries::add_greeks(pool, &greeks).await,
1763 DatabaseQuery::AddYieldCurve(curve) => {
1764 DatabaseQueries::add_yield_curve(pool, &curve).await
1765 }
1766 DatabaseQuery::UpdateOrder(event) => {
1767 DatabaseQueries::add_order_event(pool, event.into_boxed(), None).await
1768 }
1769 DatabaseQuery::UpdatePosition(event) => {
1770 DatabaseQueries::update_position(pool, &event).await
1771 }
1772 DatabaseQuery::IndexOrderPosition(client_order_id, position_id) => {
1773 DatabaseQueries::index_order_position(pool, trader_id, client_order_id, position_id)
1774 .await
1775 }
1776 DatabaseQuery::IndexOrderClients(claims) => {
1777 DatabaseQueries::index_order_clients(pool, trader_id, &claims).await
1778 }
1779 };
1780
1781 if let Err(e) = result {
1782 log::error!("Error on query: {e:?}");
1783 }
1784 }
1785}