FastMM documentation
FastMM C++ API 0.6.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
backend.hpp
Go to the documentation of this file.
1#pragma once
2// Storage backend interface: what a session's fills, orders, positions, PnL and kill events are
3// handed to, so they can be queried without replaying the journal.
4//
5// engine thread --RecordWriter--> MsgRing --StoreThread--> Backend --> rows
6//
7// The journal stays the byte-exact stream a replay consumes (core/journal.hpp); a backend holds
8// the interpreted record. Backends are selected by name from [storage] backend and built by the
9// registry (store/registry.hpp); "sqlite" ships with FastMM and "none" means no backend at all.
10//
11// What a backend must guarantee (docs/reference/storage.md):
12// * Ordering. Records arrive in the order the engine made them, on one thread, and seq is
13// strictly increasing within a session. Calls between begin() and commit() are one batch.
14// * At most once. A record the ring dropped, or a batch the process died inside of, is gone; a
15// backend must not invent one. Records carry keys (session id + seq, and an exec id on a
16// fill) so re-ingesting the same record is a no-op.
17// * Never block the caller for long. StoreThread runs the backend on its own thread, so a slow
18// commit costs ring space, not engine latency, but a backend that blocks for seconds will
19// overflow the ring and lose records.
20// * Never throw across the interface, and never abort the process. A failure is counted in
21// errors() and described by last_error(); the session keeps trading.
22//
23// Everything here runs off the engine thread and may allocate.
25#include "fastmm/core/engine_runner.hpp"
26#include "fastmm/core/enums.hpp"
30
31#include <cstdint>
32#include <span>
33#include <string>
34#include <string_view>
35#include <vector>
36
37namespace fastmm::store {
38
39// What a backend is built with. `config` is the [storage] section verbatim: a backend reads its
40// own keys from it and the central schema (config/schema.hpp) knows none of them.
41struct BackendOptions {
42 const GenericSection* config = nullptr;
43 std::string engine_name; // [engine] name
44 std::string default_dir; // [engine] journal_dir; where a backend may default its files
45 bool read_only = false; // opened by a query tool, not by a session
46};
48// Written once, before any record.
49struct SessionOpen {
50 std::uint64_t session_id = 0;
51 std::uint16_t session_epoch = 0;
52 std::int64_t started_ns = 0;
53 std::string engine_name;
54 std::string strategy;
55 std::string version; // FASTMM_VERSION_STRING
56 std::string build_info; // compiler, flags, build type
57 std::uint64_t config_hash = 0;
58 std::string config_toml; // effective configuration, secrets omitted
59 std::string journal_path; // first journal part; empty when journaling is off
60 std::string host;
61 std::uint32_t pid = 0;
62 bool dry_run = false;
63 std::int64_t pnl_carry_raw = 0; // net PnL carried in from earlier sessions
64 std::vector<std::string> venues; // [venues.<name>] by VenueId, so a restart finds its own again
65 // The strategy's parameter table in schema order: a ParamRecord's field index names an entry.
66 // `type` is the journal's parameter type (ParamType: 0 int, 1 double, 2 bool, 3 decimal, 4 bps,
67 // 5 ms).
68 struct ParamField {
69 std::string name;
70 std::uint8_t type = 0;
71 };
72 std::vector<ParamField> param_table;
73 // The parameters the session starts with. Every parameter of the table when params_complete;
74 // otherwise the configured ones only ([strategy.params], --param), with no raw value.
75 struct ParamValue {
76 std::string name;
77 std::string text;
78 std::int64_t raw = 0;
79 bool has_raw = false;
80 };
81 std::vector<ParamValue> params;
82 bool params_complete = false;
83};
85// Written once, after the last record.
86struct SessionClose {
87 std::uint64_t session_id = 0;
88 std::int64_t stopped_ns = 0;
89 int exit_code = 0;
91 bool kill_latched = false;
92 bool journal_complete = true; // the journal was closed cleanly
93 std::uint64_t journal_bytes = 0;
94 std::vector<std::string> journal_paths; // every part, in order
95 RunnerStats stats;
96};
98class Backend {
99 public:
100 Backend() = default;
101 virtual ~Backend() = default;
102 Backend(const Backend&) = delete;
103 Backend& operator=(const Backend&) = delete;
105 [[nodiscard]] virtual std::string_view name() const noexcept = 0;
106 // Opens or creates the store and brings its schema up to date. A failure here stops the
107 // session from starting: an operator who does not want a store sets backend = "none".
108 [[nodiscard]] virtual Result<void, std::string> open(const BackendOptions& opts) = 0;
109 [[nodiscard]] virtual Result<void, std::string> session_open(const SessionOpen& s) = 0;
110 // The instrument table of this session, by dense id.
111 [[nodiscard]] virtual Result<void, std::string> instruments(
112 std::uint64_t session_id, std::span<const Instrument> table) = 0;
114 // One batch. StoreThread calls begin(), then zero or more records, then commit().
115 virtual void begin() = 0;
116 virtual void fill(const FillRecord& r) = 0;
117 virtual void order(const OrderRecord& r) = 0;
118 virtual void position(const PositionRecord& r) = 0;
119 virtual void kill(const KillRecord& r) = 0;
120 // A funding payment. The default drops it: a backend written before funding was booked keeps
121 // building (its positions still carry funding inside realized).
122 virtual void funding(const FundingRecord& /*r*/) {}
123 // A venue's execution replay ended complete. The default drops it: a restart from such a store
124 // judges a session by whether it placed an order on the venue or shut down (Recovery).
125 virtual void replayed(const ReplayedRecord& /*r*/) {}
126 // A strategy parameter update the engine applied. The default drops it.
127 virtual void param(const ParamRecord& /*r*/) {}
128 // An order refused by the pre-trade checks or by its venue. The default drops it.
129 virtual void reject(const RejectRecord& /*r*/) {}
130 virtual void commit() = 0;
132 [[nodiscard]] virtual Result<void, std::string> session_close(const SessionClose& s) = 0;
133 virtual void close() = 0;
135 [[nodiscard]] virtual std::uint64_t rows() const noexcept = 0;
136 [[nodiscard]] virtual std::uint64_t errors() const noexcept = 0;
137 [[nodiscard]] virtual std::string last_error() const = 0;
140} // namespace fastmm::store
Definition result.hpp:30
Definition backend.hpp:100
virtual void reject(const RejectRecord &)
An order refused by the pre-trade checks or by its venue. The default drops it.
Definition backend.hpp:131
virtual void begin()=0
One batch. StoreThread calls begin(), then zero or more records, then commit().
virtual void commit()=0
virtual ~Backend()=default
virtual void close()=0
Backend & operator=(const Backend &)=delete
virtual Result< void, std::string > session_close(const SessionClose &s)=0
virtual Result< void, std::string > open(const BackendOptions &opts)=0
Opens or creates the store and brings its schema up to date.
virtual void position(const PositionRecord &r)=0
virtual void param(const ParamRecord &)
A strategy parameter update the engine applied. The default drops it.
Definition backend.hpp:129
virtual std::uint64_t errors() const noexcept=0
virtual void order(const OrderRecord &r)=0
virtual void replayed(const ReplayedRecord &)
A venue's execution replay ended complete.
Definition backend.hpp:127
virtual Result< void, std::string > instruments(std::uint64_t session_id, std::span< const Instrument > table)=0
The instrument table of this session, by dense id.
virtual void funding(const FundingRecord &)
A funding payment.
Definition backend.hpp:124
virtual std::string last_error() const =0
virtual void kill(const KillRecord &r)=0
virtual std::string_view name() const noexcept=0
virtual Result< void, std::string > session_open(const SessionOpen &s)=0
virtual void fill(const FillRecord &r)=0
virtual std::uint64_t rows() const noexcept=0
Typed configuration (8.7).
Minimal expected<T, E>: the hot path never throws, it returns Result.
All engine enumerations.
Instrument: the single definition of a tradable contract (5.3 / 5.15).
Definition backend.hpp:39
KillReason
Why a kill switch (global or one venue's) was tripped.
Definition enums.hpp:523
Record stream: the engine's second outbound queue, next to the journal.
One execution.
Definition record_stream.hpp:106
A funding payment the engine booked (FundingMsg): amount in the instrument's settlement currency,...
Definition record_stream.hpp:160
Free-form section used by the sim / backtest libraries: dotted keys -> stringified value.
Definition config.hpp:287
Definition instrument.hpp:29
A kill switch trip. hdr.flags & kVenue marks a per-venue trip (hdr.venue names it).
Definition record_stream.hpp:174
The OMS record after a transition.
Definition record_stream.hpp:137
A strategy parameter update the engine applied (ParamUpdateMsg): after the header,...
Definition record_stream.hpp:203
One instrument's position and the portfolio totals at the same instant.
Definition record_stream.hpp:144
One order refused: by the pre-trade checks (the engine's) or by its venue (an OrderReject),...
Definition record_stream.hpp:272
A venue's execution replay ended complete (a reconciliation began with ReconcileMsg::kExecutionsExact...
Definition record_stream.hpp:194
What a backend is built with.
Definition backend.hpp:43
const GenericSection * config
Definition backend.hpp:44
std::string default_dir
[engine] journal_dir; where a backend may default its files
Definition backend.hpp:46
bool read_only
opened by a query tool, not by a session
Definition backend.hpp:47
std::string engine_name
[engine] name
Definition backend.hpp:45
Written once, after the last record.
Definition backend.hpp:88
RunnerStats stats
Definition backend.hpp:97
std::uint64_t session_id
Definition backend.hpp:89
std::vector< std::string > journal_paths
every part, in order
Definition backend.hpp:96
int exit_code
Definition backend.hpp:91
KillReason kill_reason
Definition backend.hpp:92
bool journal_complete
the journal was closed cleanly
Definition backend.hpp:94
std::int64_t stopped_ns
Definition backend.hpp:90
std::uint64_t journal_bytes
Definition backend.hpp:95
bool kill_latched
Definition backend.hpp:93
The strategy's parameter table in schema order: a ParamRecord's field index names an entry.
Definition backend.hpp:70
std::string name
Definition backend.hpp:71
std::uint8_t type
Definition backend.hpp:72
The parameters the session starts with.
Definition backend.hpp:77
std::string text
Definition backend.hpp:79
std::int64_t raw
Definition backend.hpp:80
bool has_raw
Definition backend.hpp:81
std::string name
Definition backend.hpp:78
Written once, before any record.
Definition backend.hpp:51
std::string host
Definition backend.hpp:62
std::vector< ParamField > param_table
Definition backend.hpp:74
std::string journal_path
first journal part; empty when journaling is off
Definition backend.hpp:61
std::uint64_t session_id
Definition backend.hpp:52
std::vector< std::string > venues
[venues.
Definition backend.hpp:66
std::string build_info
compiler, flags, build type
Definition backend.hpp:58
std::vector< ParamValue > params
Definition backend.hpp:83
bool params_complete
Definition backend.hpp:84
std::uint16_t session_epoch
Definition backend.hpp:53
std::uint32_t pid
Definition backend.hpp:63
std::string config_toml
effective configuration, secrets omitted
Definition backend.hpp:60
std::string version
FASTMM_VERSION_STRING.
Definition backend.hpp:57
std::int64_t pnl_carry_raw
net PnL carried in from earlier sessions
Definition backend.hpp:65
std::int64_t started_ns
Definition backend.hpp:54
std::string strategy
Definition backend.hpp:56
std::uint64_t config_hash
Definition backend.hpp:59
std::string engine_name
Definition backend.hpp:55
bool dry_run
Definition backend.hpp:64