Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
fill_journal.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <cstddef>
6#include <cstdint>
7#include <string>
8#include <unordered_map>
9#include <vector>
10
11namespace slick::trader {
12
13// Every fill an order connector publishes, written down first. A process that dies
14// before its strategies have booked a fill can then hand it to them after the restart.
15//
16// Between an execution report arriving and its fill being booked lies the event queue,
17// which does not outlive the process; the report's sequence number, which does, says it
18// was received. Without the journal a crash in between loses the fill: the exchange is
19// never asked for it again. With it, the connector replays the journal at start-up
20// (OrderFillEvent::replayed), and the strategy manager drops what the strategies already
21// had (OrderFillEvent::journal_seq, PositionStore::journaled()).
22//
23// A fill's journal number increases by one per fill, from the wall-clock time in
24// nanoseconds at which the file was created: numbers never repeat, a journal that is
25// deleted and created again included.
26//
27// Each fill is kept with the report it came from (Source). The one report a process died
28// while handling is handed over again after the restart, and record() knows its fills
29// for ones it already has.
30//
31// A ring of `capacity` fills, memory-mapped: recording one is a copy into memory, never a
32// wait for the disk. The oldest are overwritten, so `capacity` is how many fills can still
33// be on their way to the strategies when the process dies.
34//
35// Single-threaded: owned by the order worker.
37public:
38 static constexpr size_t DEFAULT_CAPACITY = 4096;
39
40 // What a broker's journal file is: its magic, layout version, and the name its log
41 // lines and errors go by.
42 struct Format {
43 char magic[8];
44 uint32_t version;
45 const char* name;
46 };
47
48 // The report a fill came from, and which of its fills it is. Reports are ordered by
49 // epoch (a session's incarnation: whatever the exchange's sequence numbers restart
50 // under), then sequence number, then the fill's place in the report.
51 struct Source {
52 uint64_t session = 0;
53 uint64_t epoch = 0;
54 uint32_t seq = 0;
55 uint32_t index = 0;
56
57 [[nodiscard]] bool after(const Source& other) const noexcept {
58 if (epoch != other.epoch) {
59 return epoch > other.epoch;
60 }
61 return seq != other.seq ? seq > other.seq : index > other.index;
62 }
63 };
64
65 // Opens the journal at `path`, creating it with room for `capacity` fills and its
66 // numbering starting at `first_seq`, the wall-clock time in nanoseconds. An existing
67 // journal keeps its own capacity. Throws std::runtime_error for a file that is not
68 // this format's journal, or was written with a different layout.
69 FillJournal(const std::string& path, const Format& format, size_t capacity, uint64_t first_seq);
70
71 // Journals `fill` from `source` and gives it its journal number. False, with nothing
72 // journaled, for a fill that already is: its report handed over again.
73 bool record(OrderFillEvent& fill, const Source& source);
74 // Whether a fill from `source`, or from a report after it, is journaled already.
75 [[nodiscard]] bool journaled(const Source& source) const noexcept {
76 const auto it = last_.find(source.session);
77 return it != last_.end() && !source.after(it->second);
78 }
79
80 // The fills held, oldest first.
81 [[nodiscard]] std::vector<OrderFillEvent> fills() const;
82
83 [[nodiscard]] size_t size() const noexcept { return size_; }
84 [[nodiscard]] size_t capacity() const noexcept { return capacity_; }
85 [[nodiscard]] uint64_t next_seq() const noexcept { return next_seq_; }
86 [[nodiscard]] const std::string& path() const noexcept { return path_; }
87
88private:
89 struct FileHeader;
90 struct Entry;
91
92 [[nodiscard]] FileHeader& header() noexcept;
93 [[nodiscard]] Entry& entry(size_t index) noexcept;
94 [[nodiscard]] const Entry& entry(size_t index) const noexcept;
95
96 std::string path_;
97 MappedFile file_;
98 size_t capacity_ = 0;
99 size_t size_ = 0;
100 uint64_t next_seq_ = 0;
101 // The last fill journaled from each session.
102 std::unordered_map<uint64_t, Source> last_;
103};
104
105} // namespace slick::trader
std::vector< OrderFillEvent > fills() const
size_t size() const noexcept
bool journaled(const Source &source) const noexcept
uint64_t next_seq() const noexcept
size_t capacity() const noexcept
FillJournal(const std::string &path, const Format &format, size_t capacity, uint64_t first_seq)
static constexpr size_t DEFAULT_CAPACITY
bool record(OrderFillEvent &fill, const Source &source)
const std::string & path() const noexcept
bool after(const Source &other) const noexcept