FastMM documentation
FastMM C++ API 0.14.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
store_thread.hpp
Go to the documentation of this file.
1#pragma once
2// StoreThread: drains the engine's record ring into a Backend, on its own thread.
3//
4// The mirror of JournalFileWriter: the engine writes into an SPSC MsgRing and never waits, this
5// side batches what it finds into one backend transaction. A pass over the ring commits every
6// max_batch records and whatever is left at its end, and the thread sleeps 2 ms when the ring is
7// empty; so a busy session does not commit per fill and an idle one commits within 2 ms. Before
8// it sleeps it gives the backend its idle moment (Backend::idle: the SQLite retention).
9//
10// It never signals the engine. A backend that fails counts the error and keeps going; a ring that
11// fills drops records on the engine side (EngineStats::records_dropped). Both are logged when the
12// session ends and stored in the session's row; neither stops trading (docs/reference/storage.md).
13#include "fastmm/core/msg_ring.hpp"
15
16#include <atomic>
17#include <cstddef>
18#include <cstdint>
19#include <memory>
20#include <thread>
21
22namespace fastmm::store {
23
24struct StoreThreadOptions {
25 std::size_t max_batch = 512; // records per backend transaction
26};
28struct StoreThreadStats {
29 std::uint64_t records = 0;
30 std::uint64_t batches = 0;
31 std::uint64_t unknown = 0; // records of a type this build does not know (a newer writer)
32};
34class StoreThread {
35 public:
36 StoreThread(MsgRing& ring, std::unique_ptr<Backend> backend, const StoreThreadOptions& opts = {});
38 StoreThread(const StoreThread&) = delete;
41 [[nodiscard]] Backend& backend() noexcept { return *backend_; }
42
43 void start(); // spawns the drain thread
44 void stop(); // joins, drains what is left, commits
46 // Single-threaded driving (tests): drains and commits everything the ring holds now.
47 std::size_t drain_once();
48
49 [[nodiscard]] StoreThreadStats stats() const noexcept;
50
51 private:
52 void run();
53 std::size_t drain(bool force_commit);
54
55 MsgRing& ring_;
56 std::unique_ptr<Backend> backend_;
58 std::thread thread_;
59 std::atomic<bool> stop_{false};
60 std::atomic<std::uint64_t> records_{0};
61 std::atomic<std::uint64_t> batches_{0};
62 std::atomic<std::uint64_t> unknown_{0};
63 bool stopped_ = false;
64};
65
66} // namespace fastmm::store
Storage backend interface: what a session's fills, orders, positions, PnL and kill events are handed ...
Definition backend.hpp:109
Definition store_thread.hpp:36
void start()
spawns the drain thread
std::size_t drain_once()
Single-threaded driving (tests): drains and commits everything the ring holds now.
StoreThread(MsgRing &ring, std::unique_ptr< Backend > backend, const StoreThreadOptions &opts={})
StoreThreadStats stats() const noexcept
Backend & backend() noexcept
Definition store_thread.hpp:43
StoreThread & operator=(const StoreThread &)=delete
void stop()
joins, drains what is left, commits
Definition backend.hpp:40
Definition store_thread.hpp:26
std::size_t max_batch
records per backend transaction
Definition store_thread.hpp:27
Definition store_thread.hpp:30
std::uint64_t records
Definition store_thread.hpp:31
std::uint64_t unknown
records of a type this build does not know (a newer writer)
Definition store_thread.hpp:33
std::uint64_t batches
Definition store_thread.hpp:32