Files
spark/src/lib/md/MDConsumer.cpp
T
John Colvin 3f6c904ccc cleanup: Get rid of redundant SecurityKey def
Make bts::Security an alias for bts::md::Security instead
2024-04-01 10:09:35 -04:00

1141 lines
39 KiB
C++

#ifndef NO_CPP0X
#include "MDConsumer.h"
#include "MBOProcessor.h"
#include "MemorySegmentUtils.h"
#include "SecurityCache.h"
#include "lib/config/config.h"
#include "lib/config/protos/market_data.pb.h"
#include "lib/domain_model/Security.pb.h"
#include "lib/logging/logging.h"
#include "lib/md/ShmSegmentName.h"
#include "lib/md/SymbolLoader.h"
#include "lib/utils/SharedList.h"
#include "lib/utils/WaitFreeQueue.h"
#include "lib/utils/bts_chrono.h"
#include "lib/utils/sane_algorithm.h"
#include <algorithm>
#include <btree/map.h>
#include <map>
#include <set>
#include <string>
#include <tbb/queuing_mutex.h>
#include <tuple>
using namespace std;
using namespace bts::api;
// using namespace btree;
namespace bts {
namespace md {
/*
* Implementation notes:
* ColdHeader keeps a security count to know when a list has changed
* When a security is made visible increment count (non-atomic)
* When user hits walk_security_list just walk all of them
* When scanning for new securities we can short circuit the search if the
* security_count hasn't incremented
*
*/
bool match_type(SecurityType val, SecurityType b) {
return (val == b || b == SECURITYTYPE_UNSET);
}
bool match_symbol(const string &val, const string &b) {
bts_assert_d(!val.empty());
return b.empty() || (val == b);
}
typedef shared_ptr<Security> MutSecurityPtr;
struct SecurityLess {
bool operator()(const SecurityPtr &left, const SecurityPtr &right) {
return left->id() < right->id();
}
};
MutSecurityPtr convert_psecurity(const FixedSecurity &security) {
auto psec = make_shared<Security>();
convert_security(security, psec.get());
return psec;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// Represents one pair of shared memory segments for a single Symbol+type
///////////////////////////////////////////////////////////////////////////////////////////////////
struct QueueStruct {
utils::WaitFreeQueue<co2::TradeMessage> _trade_queue;
utils::WaitFreeQueue<ColdHeader::RFQBuffer> _rfq_queue;
QueueStruct(const ColdHeader &header)
: _trade_queue(
(void *)header.trade_buffer, BTS_ARRAYSIZE(header.trade_buffer)),
_rfq_queue(
(void *)header.rfq_buffer, BTS_ARRAYSIZE(header.rfq_buffer)) {}
};
bool is_heart_beat_ok(const ColdHeader &);
// this is for a <symbol, type, exchange> tuple
class MDConsumerInst {
public:
MDConsumerInst(
const string &symbol, SecurityType type, Venue exchange, bool shared)
: _symbol(symbol), _type(type), _exchange(exchange), _security_count(0),
_found_keys(), _hot(), _hot_depth(),
_cold(ShmSegmentName::cold_name(symbol, type, exchange), shared),
_queues() {
LOG(md, Diag,
"MDConsumerInst::MDConsumerInst symbol: {} venue: {} type: {}",
symbol, static_cast<int>(exchange), static_cast<int>(type));
if (enable_depth(symbol, type)) {
_hot_depth.reset(new utils::SharedListReader<HotHeader, HotDepthEntry>(
ShmSegmentName::hot_name(symbol, type, exchange), shared));
} else {
_hot.reset(new utils::SharedListReader<HotHeader, HotEntry>(
ShmSegmentName::hot_name(symbol, type, exchange), shared));
}
}
~MDConsumerInst() {
if (_queues)
delete _queues;
}
template <typename CB>
size_t walk_sec_list(
CB cb,
const string &symbol = "",
SecurityType type = SECURITYTYPE_UNSET) {
size_t result = 0;
_cold.all_entries([&symbol, type, &cb, &result](const ColdEntry &entry) {
// not sure this double check is needed since the caller also filters
if (entry.security.key != invalid_security_key &&
match_type(entry.security.type, type) &&
match_symbol(entry.security.symbol(), symbol)) {
cb(convert_psecurity(entry.security));
++result;
}
});
return result;
}
template <class T>
const void *find_hot(
utils::SharedListReader<HotHeader, T> *phot, security_key key) {
const void *hot_entry = nullptr;
phot->all_entries([&](const T &entry) {
if (entry.key == key) {
hot_entry = &entry;
}
});
return hot_entry;
}
bool find_handle(
security_key key, const ColdEntry **ppcold, const void **ppvhot) {
*ppcold = nullptr;
*ppvhot = nullptr;
_cold.all_entries([&](const ColdEntry &entry) {
if (entry.security.key == key) {
*ppcold = &entry;
}
});
// Only search the hot list if we found cold
if (*ppcold) {
// It is possible that cold is initialized and hot is not yet due to
// race between process mapped memory being visible
size_t count = 0;
do {
++count;
*ppvhot = cold_header().has_depth ? find_hot(_hot_depth.get(), key)
: find_hot(_hot.get(), key);
if (!*ppvhot) {
std::this_thread::sleep_for(1ms);
if (count == 4096)
LOG(md, Error, "Waiting an awfully long time for key: {}",
key);
if (count == 60000) {
LOG(md, Error,
"Waited far too long for key: {} security discovery may "
"be hung."
" symbol: {} type: {}",
key, std::string((*ppcold)->security.symbol()),
SecurityType_Name((*ppcold)->security.type));
return false;
}
}
} while (!*ppvhot);
return true;
}
// Nothing found
return false;
}
btree::map<security_key, tuple<const ColdEntry *, const void *>>
search_handles(const std::set<security_key> &keys) {
btree::map<security_key, tuple<const ColdEntry *, const void *>> result;
// Walk hot and cold lists and fill in any found handles
if (!_cold.is_initialized())
return result;
_cold.all_entries([&](const ColdEntry &entry) {
if (keys.find(entry.security.key) != keys.end()) {
result.insert(std::make_pair(
entry.security.key, make_tuple(&entry, nullptr)));
}
});
// Do the same for hot
if (_hot_depth) {
_hot_depth->all_entries([&](const HotDepthEntry &entry) {
auto iter = result.find(entry.key);
if (iter != result.end()) {
iter->second = make_tuple(get<0>(iter->second), &entry);
}
});
} else if (_hot) {
_hot->all_entries([&](const HotEntry &entry) {
auto iter = result.find(entry.key);
if (iter != result.end()) {
iter->second = make_tuple(get<0>(iter->second), &entry);
}
});
}
return result;
}
template <typename CB>
void scan_new_securities(CB cb) {
// If the ver matches no securities have been added
if (_security_count == cold_header().security_count)
return;
_security_count = cold_header().security_count;
_cold.all_entries([&](const ColdEntry &entry) {
LOG(md, Diag,
"MDConsumerInst::scan_new_securities rcvd entry: {} key: {}",
std::string(entry.security.security_desc()), entry.security.key);
if (entry.security.key != invalid_security_key &&
_found_keys.count(entry.security.key) == 0) {
_found_keys.insert(entry.security.key);
LOG(md, Diag,
"MDConsumerInst::scan_new_securities firing cb entry: {}",
std::string(entry.security.security_desc()));
cb(entry);
}
});
}
void forget_security(security_key key) {
_security_count -= _found_keys.erase(key);
}
template <typename CB>
void walk_old_securities(CB cb) {
// Fire callback only for securities which *have* been seen already
if (_found_keys.empty())
return;
_cold.all_entries([&](const ColdEntry &entry) {
if (entry.security.key != invalid_security_key &&
_found_keys.count(entry.security.key) != 0) {
cb(entry);
}
});
}
uint32_t security_count() { return cold_header().security_count; }
const ColdHeader &cold_header() { return _cold.header(); }
const HotHeader &hot_header() {
return (has_depth() ? _hot_depth->header() : _hot->header());
}
bool has_depth() { return cold_header().has_depth; }
const string &symbol() const { return _symbol; }
SecurityType type() const { return _type; }
Venue exchange() const { return _exchange; }
//
// Queued data
int64_t read_trades(
google::protobuf::RepeatedPtrField<co2::TradeMessage> &trades,
int64_t excursor) {
if (init_queues()) {
bts_assert(_queues);
utils::WaitFreeQueue<co2::TradeMessage>::Cursor cursor(
_queues->_trade_queue, excursor);
bool overrun;
cursor.read(trades, &overrun);
// Preserve the overrun flag in the message
if (overrun && !trades.empty()) {
trades.begin()->set_gap(true);
}
return cursor.current_position();
}
return 0;
}
int64_t read_rfqs(
google::protobuf::RepeatedPtrField<co2::QuoteRequestMessage> &rfqs,
int64_t extcursor) {
if (init_queues()) {
bts_assert(_queues);
bool overrun;
vector<ColdHeader::RFQBuffer> temp_result;
utils::WaitFreeQueue<ColdHeader::RFQBuffer>::Cursor cursor(
_queues->_rfq_queue, extcursor);
cursor.read(temp_result, &overrun);
// deserialize the rfqs
for (const auto &entry : temp_result) {
rfqs.Add()->ParsePartialFromArray(&entry[0], entry.size());
}
if (overrun && rfqs.size() > 0) {
rfqs.begin()->set_gap(true);
}
return cursor.current_position();
}
return 0;
}
bool is_heart_beating() {
// Channel Heartbeat - not in a tight loop
if (!_cold.is_initialized())
return true;
return is_heart_beat_ok(cold_header());
}
void dump_shm_to(const char *directory) const {
_cold.dump_to(directory);
if (_hot) {
_hot->dump_to(directory);
}
if (_hot_depth) {
_hot_depth->dump_to(directory);
}
}
private:
bool init_queues() {
if (_queues)
return true;
if (!_cold.is_initialized())
return false;
// double assignment is OK. Leak instead of lock.
_queues = new QueueStruct(_cold.header());
return true;
}
string _symbol;
SecurityType _type;
Venue _exchange;
size_t _security_count;
set<security_key> _found_keys;
unique_ptr<utils::SharedListReader<HotHeader, HotEntry>> _hot;
unique_ptr<utils::SharedListReader<HotHeader, HotDepthEntry>> _hot_depth;
utils::SharedListReader<ColdHeader, ColdEntry> _cold;
QueueStruct *_queues;
};
///////////////////////////////////////////////////////////////////////////////////////////////////
// Instance for synthetic securities (no shm)
///////////////////////////////////////////////////////////////////////////////////////////////////
class SyntheticConsumerInst {
typedef tbb::queuing_mutex Mutex;
typedef function<map<security_key, MDConsumerHandle>(
const set<security_key> &)>
IGetHandle;
typedef shared_ptr<SyntheticStrategy> StrategyPtr;
struct IncompleteStrategy {
shared_ptr<api::Security> security;
spark::ExecutionStrategy strategy;
};
// LOCK only public methods
Mutex _workmutex;
api::ISyntheticSecurityService *_ss;
map<security_key, StrategyPtr> _data;
vector<shared_ptr<Security>> _pending;
set<security_key> _seen;
IGetHandle _get_handle;
bool _initialized;
int _security_sub;
int _meta_sub;
std::vector<std::unique_ptr<spark::ExecutionStrategy>> _old_strategies;
public:
SyntheticConsumerInst(
api::ISyntheticSecurityService *ss, const IGetHandle &gh)
: _workmutex(), _ss(ss), _data(), _pending(), _seen(), _get_handle(gh),
_initialized(false), _security_sub(0), _meta_sub(0),
_old_strategies() {}
~SyntheticConsumerInst() {
if (_meta_sub) {
_ss->on_metadata().unsubscribe(_meta_sub);
}
if (_security_sub) {
_ss->on_securities().unsubscribe(_security_sub);
}
}
size_t security_count() { return _ss->securities().size(); }
const vector<StrategyPtr> get_old_synthetics() {
Mutex::scoped_lock lock(_workmutex);
vector<StrategyPtr> result;
for (const auto &i : _data) {
if (_seen.find(i.first) != _seen.end()) {
result.push_back(i.second);
}
}
return result;
}
const StrategyPtr get_synthetic(security_key key) {
Mutex::scoped_lock lock(_workmutex);
const auto result = _data.find(key);
if (result != _data.end()) {
return result->second;
}
return nullptr;
}
const vector<StrategyPtr> scan_new_synthetics() {
Mutex::scoped_lock lock(_workmutex);
init();
if (!_pending.empty()) {
vector<shared_ptr<Security>> temp;
temp.swap(_pending);
add_snapshot(temp);
}
vector<StrategyPtr> result;
for (const auto &i : _data) {
if (_seen.find(i.first) == _seen.end()) {
result.push_back(i.second);
_seen.insert(i.first);
}
}
return result;
}
private:
void process_pending() {}
void init() {
if (!_initialized) {
_initialized = true;
vector<shared_ptr<Security>> snapshot;
for (const auto &pair : _ss->securities()) {
snapshot.push_back(make_shared<api::Security>(pair.second));
}
add_snapshot(snapshot);
_security_sub = _ss->on_securities().subscribe(
[=, this](const api::Security &sec) { add_security(sec); });
_meta_sub = _ss->on_metadata().subscribe(
[=, this](const synthetic::Metadata &m) { on_metadata(m); });
}
}
void add_security(const api::Security &sec) {
Mutex::scoped_lock lock(_workmutex);
if (_data.find(sec.id()) == _data.end()) {
auto ptr = to_strategy(make_shared<api::Security>(sec));
vector<IncompleteStrategy> vec;
vec.push_back(ptr);
auto handles = get_synthetic_handles(vec, _data, _get_handle);
_data.insert(handles.begin(), handles.end());
for (const auto &s : vec) {
if (handles.find(s.security->id()) == handles.end()) {
_pending.push_back(s.security);
}
}
}
}
void on_metadata(const api::synthetic::Metadata &m) {
Mutex::scoped_lock lock(_workmutex);
// update security metadata
auto iter = _data.find(m.security_key());
if (iter != _data.end()) {
auto updated = std::make_unique<spark::ExecutionStrategy>();
updated->CopyFrom(m.strategy());
iter->second->strategy.swap(updated);
iter->second->updated = TimeStamp::now();
// Keep the old strategy alive because it could be in use.
// metadata is changed so seldomly this is not a significant use of
// memory
if (updated != nullptr)
_old_strategies.push_back(std::move(updated));
}
}
private:
static map<security_key, StrategyPtr> get_synthetic_handles(
vector<IncompleteStrategy> synthetics,
const map<security_key, StrategyPtr> &existing,
const std::function<map<security_key, MDConsumerHandle>(
const set<security_key> &)> &get_handles) {
// gather leg keys for batch processing
std::set<security_key> leg_keys;
for (const auto &p : synthetics) {
for (const auto &leg : p.security->synthetic().legs()) {
leg_keys.insert(leg.security().id());
}
}
// Get Handles for all of the 'real' legs
auto real_handles = get_handles(leg_keys);
// result will be merged with _data
map<security_key, StrategyPtr> result;
size_t result_count;
do {
result_count = result.size();
for (const auto &strategy : synthetics) {
// skip any that have already been found
if (result.find(strategy.security->id()) != result.end())
continue;
vector<MDConsumerHandle> legs;
legs.reserve(strategy.security->synthetic().legs_size());
for (const auto &leg : strategy.security->synthetic().legs()) {
auto leg_key = leg.security().id();
auto iter = real_handles.find(leg_key);
if (iter != real_handles.end()) {
// real handle
legs.push_back(iter->second);
} else {
auto iter2 = existing.find(leg_key);
if (iter2 != existing.end()) {
// existing synthetic handle
legs.push_back(MDConsumerHandle(iter2->second));
} else {
auto iter3 = result.find(leg_key);
if (iter3 != result.end()) {
// new synthetic handle?
legs.push_back(MDConsumerHandle(iter3->second));
}
}
}
}
// If we found all the legs
if (legs.size() ==
(size_t)strategy.security->synthetic().legs_size()) {
auto s = make_shared<SyntheticStrategy>();
s->security.CopyFrom(*strategy.security);
s->strategy = std::make_unique<spark::ExecutionStrategy>(
strategy.strategy);
s->legs = legs;
s->updated = TimeStamp::now();
result.insert(make_pair(strategy.security->id(), s));
}
}
// iterate as long as progress is made
} while (result_count < result.size());
return result;
}
IncompleteStrategy to_strategy(const shared_ptr<api::Security> &security) {
IncompleteStrategy res;
res.security = security;
auto meta = _ss->get_metadata(bts::SecurityKey(security->id()));
// If metadata is set copy it otherwise we use the default proto
if (meta) {
res.strategy.CopyFrom(meta->strategy());
}
return res;
}
void add_snapshot(const vector<shared_ptr<Security>> &securities) {
std::vector<IncompleteStrategy> strategies;
for (const auto &sec : securities) {
if (_data.find(sec->id()) == _data.end()) {
strategies.push_back(to_strategy(sec));
}
}
if (strategies.size() > 0) {
auto handles = get_synthetic_handles(strategies, _data, _get_handle);
// put strategies that are incomplete into a backlog
for (const auto &s : strategies) {
if (handles.find(s.security->id()) == handles.end()) {
_pending.push_back(s.security);
}
}
_data.insert(handles.begin(), handles.end());
}
}
};
///////////////////////////////////////////////////////////////////////////////////////////////////
// Pimple for MDConsumer
///////////////////////////////////////////////////////////////////////////////////////////////////
class MDConsumerImpl {
typedef tbb::queuing_mutex Mutex;
Mutex _workmutex;
vector<shared_ptr<MDConsumerInst>> _instances;
std::shared_ptr<SyntheticConsumerInst> _sci;
MBOReader *_mbo;
map<security_key, shared_ptr<MDConsumerHandle>> _deferred_handles;
vector<SecurityCallback> _callbacks;
mutable SecurityCache _cache;
public:
MDConsumerImpl(
const set<UserSymbol> &symbols,
SecurityCache &cache,
api::ISyntheticSecurityService *ss,
MBOReader *mbo,
bool shared)
: _workmutex(), _instances(), _sci(), _mbo(mbo), _deferred_handles(),
_callbacks(), _cache(cache) {
std::ostringstream msg;
for (const auto &symbol : symbols) {
if (msg.tellp() > 0) {
msg << ", ";
}
msg << symbol.symbol() << "-" << SecurityType_Name(symbol.type())
<< " " << Venue_Name(symbol.exchange());
}
LOG(md, Info, "Created MDConsumerImpl for securities: {}", msg.str());
for (auto &s : symbols) {
if (s.type() == SECURITYTYPE_FUTURE ||
s.type() == SECURITYTYPE_OPTION ||
s.type() == api::SECURITYTYPE_SPOT) {
_instances.push_back(make_shared<MDConsumerInst>(
s.symbol(), s.type(), s.exchange(), shared));
}
// TODO, add options here
}
bool has_synthetic_instance = false;
for (auto &s : symbols) {
if (s.type() != SECURITYTYPE_FUTURE) {
if (s.type() == SECURITYTYPE_SYNTHETIC) {
// Should I look for the shared memory if we have a
// ISyntheticSecurityService?
if (has_synthetic_instance == false) {
_instances.push_back(make_shared<MDConsumerInst>(
SYNTHETIC_SYMBOL, s.type(), api::VENUE_BTS, shared));
}
has_synthetic_instance = true;
} else {
_instances.push_back(make_shared<MDConsumerInst>(
s.symbol(), s.type(), s.exchange(), shared));
}
}
}
if (ss != nullptr) {
_sci = std::make_shared<SyntheticConsumerInst>(
ss, [=, this](const set<security_key> &keys) {
return get_handles(keys);
});
}
// Add the SecurityCache to on_security so it can cache discovered
// securities
_callbacks.push_back(
[this](const SecurityPtr &security, MDConsumerHandle) {
_cache.add_live_security(security);
});
}
~MDConsumerImpl() {}
// This is a terrible name
size_t walk_sec_list(
SecurityCallback cb, const string &symbol, SecurityType type) {
size_t result = 0;
vector<MutSecurityPtr> securities;
for (auto &inst : _instances) {
if (match_symbol(inst->symbol(), symbol) &&
match_type(inst->type(), type)) {
result += inst->walk_sec_list(
[&](MutSecurityPtr security) {
securities.push_back(security);
},
symbol, type);
}
}
auto complete_securities = fill_leg_details(securities);
auto handles = get_handles(complete_securities);
for (const auto &security : complete_securities) {
auto find_result = handles.find(security->id());
if (find_result != handles.end()) {
cb(security, find_result->second);
}
}
return result;
}
MDConsumerHandle get_handle(security_key key) {
// Check synthetics first because it's fast.
if (_sci) {
auto handle = _sci->get_synthetic(key);
if (handle)
return MDConsumerHandle(handle);
}
const void *hot = nullptr;
const ColdEntry *cold = nullptr;
// Search through the instances for one that has this handle
for (auto &inst : _instances) {
if (inst->find_handle(key, &cold, &hot)) {
MDConsumerHandle handle;
handle.init(inst->cold_header(), *cold, hot, _mbo);
return handle;
}
}
// If we can't find it return an empty handle. This handle could be later
// initialized when the security is available.
MDConsumerHandle empty_handle;
return empty_handle;
}
template <class Security> // maybe const
map<security_key, MDConsumerHandle> get_handles(
const vector<Security> &securities) {
set<security_key> keys;
for (const auto &sec : securities) {
keys.insert(sec->id());
}
return get_handles(keys);
}
map<security_key, MDConsumerHandle> get_handles(
const set<security_key> &keys) {
map<security_key, MDConsumerHandle> result;
if (keys.empty())
return result;
for (auto &inst : _instances) {
auto found = inst->search_handles(keys);
for (const auto &t : found) {
// auto [cold,hot] = t.second;
auto cold = get<0>(t.second);
auto hot = get<1>(t.second);
if (cold && hot) {
result.insert(make_pair(
cold->security.key,
MDConsumerHandle{inst->cold_header(), *cold, hot, _mbo}));
}
}
}
return result;
}
shared_ptr<MDConsumerHandle> get_managed_handle(security_key key) {
shared_ptr<MDConsumerHandle> handle =
make_shared<MDConsumerHandle>(get_handle(key));
if (!handle->is_initialized()) {
Mutex::scoped_lock lock(_workmutex);
_deferred_handles[key] = handle;
}
return handle;
}
vector<SecurityPtr> fill_leg_details(
const vector<MutSecurityPtr> &securities, int depth = 0) {
vector<SecurityPtr> result;
if (securities.empty() || depth > 64)
return result;
//
// Gather ids for legs
set<security_key> keys;
for (const auto &sec : securities) {
if (securitytype_of(*sec) == SECURITYTYPE_SPREAD) {
for (const auto &leg : sec->spread().legs()) {
keys.insert(leg.security().id());
}
}
}
// If there are no spreads everything is (by definition) resolved already
if (keys.empty())
return vector<SecurityPtr>{securities.begin(), securities.end()};
//
// gather handles for direct legs
auto handles = get_handles(keys);
//
// Some of the legs will themselves be spreads
vector<MutSecurityPtr> legs;
legs.reserve(handles.size());
for (const auto &pair : handles) {
bts_assert_d(pair.second.is_initialized());
if (pair.second.is_initialized()) {
legs.push_back(
make_shared<Security>(pair.second.get_normalized_security()));
}
}
// recurse to find details
map<security_key, SecurityPtr> leg_map;
for (auto &leg : fill_leg_details(legs, depth + 1)) {
leg_map.insert(make_pair(leg->id(), std::move(leg)));
}
//
// update legs
for (auto &sec : securities) {
if (securitytype_of(*sec) == SECURITYTYPE_SPREAD) {
for (auto &leg : *sec->mutable_spread()->mutable_legs()) {
auto key = leg.security().id();
auto leg_iter = leg_map.find(key);
if (leg_iter == leg_map.end())
goto next;
bts_assert(leg.security().id() == leg_iter->first);
leg.mutable_security()->CopyFrom(*leg_iter->second);
}
sec->mutable_spread()->set_normalized(false);
result.push_back(sec);
} else {
result.push_back(sec);
}
next:
continue;
}
return result;
}
// Check for new securities and call callbacks
size_t scan_securities(bool resolve_legs) {
Mutex::scoped_lock lock(_workmutex);
size_t count = 0;
if (_callbacks.empty() && _deferred_handles.empty())
return 0;
// Call each _instance and pass it a callback
for (auto &inst : _instances) {
vector<MutSecurityPtr> found_securities;
inst->scan_new_securities([&](const ColdEntry &entry) {
// Check if any deferred handles need this security (could be sped
// up with get_handles() but it isn't hot
auto iter = _deferred_handles.find(entry.security.key);
if (iter != _deferred_handles.end()) {
(*iter->second) = get_handle(entry.security.key);
_deferred_handles.erase(iter);
}
found_securities.push_back(convert_psecurity(entry.security));
});
if (resolve_legs && !found_securities.empty()) {
sort(
found_securities.begin(), found_securities.end(),
SecurityLess());
vector<SecurityPtr> resolved_securities =
fill_leg_details(found_securities);
sort(
resolved_securities.begin(), resolved_securities.end(),
SecurityLess());
// Extract the list of unresolved securities
vector<SecurityPtr> unresolved_securities;
set_difference(
found_securities.begin(), found_securities.end(),
resolved_securities.begin(), resolved_securities.end(),
inserter(unresolved_securities, unresolved_securities.begin()),
SecurityLess());
// Return any unresolved spreads to the source so they can be
// 'discovered' again We can safely send a security key to every
// _instance because by definition a security cannot appear in 2
// instances
if (unresolved_securities.size() > 250) {
LOG(md, Info,
"Failed to resolve legs for {}"
" spreads. This can happen when a spread is subscribed "
"without "
"it's underliers and may lead to higher than expected CPU "
"use.",
unresolved_securities.size());
}
for (auto &security : unresolved_securities) {
bts_assert(securitytype_of(*security) == SECURITYTYPE_SPREAD);
bts_assert(security->id() != invalid_security_key);
inst->forget_security(security->id());
}
auto handles = get_handles(resolved_securities);
for (const auto &security : resolved_securities) {
auto find_result = handles.find(security->id());
if (find_result != handles.end()) {
// Pass new securities to each callback
for (auto &cb : _callbacks) {
cb(security, find_result->second);
}
} else {
// "should never happen"
inst->forget_security(security->id());
}
}
count += resolved_securities.size();
} else /* do NOT resolve_legs*/ {
auto handles = get_handles(found_securities);
for (const auto &security : found_securities) {
auto find_result = handles.find(security->id());
if (find_result != handles.end()) {
for (auto &cb : _callbacks) {
cb(security, find_result->second);
}
} else {
inst->forget_security(security->id());
}
}
count += found_securities.size();
}
}
if (_sci) {
for (const auto &item : _sci->scan_new_synthetics()) {
count++;
const auto &security = item->security;
auto handle = MDConsumerHandle(item);
// check deferred handles
auto iter = _deferred_handles.find(security.id());
if (iter != _deferred_handles.end()) {
(*iter->second) = handle;
_deferred_handles.erase(iter);
}
// call all subscribers
for (auto &cb : _callbacks) {
cb(std::make_shared<Security>(security), handle);
}
}
}
_cache.flush_to_disk();
return count;
}
// Subscribe to new securities
void on_security(SecurityCallback cb, bool resolve_legs) {
Mutex::scoped_lock lock(_workmutex);
// Walk the current security list and fire the callbacks for existing
// securities Note: We have to lock over the initial walk because if
// scan_securities() finds a new security concurrently with this walk it
// might mark it seen without this callback getting called.
for (auto &inst : _instances) {
vector<MutSecurityPtr> securities;
inst->walk_old_securities([&](const ColdEntry &entry) {
securities.push_back(convert_psecurity(entry.security));
});
auto filled_securities =
resolve_legs
? fill_leg_details(securities)
: vector<SecurityPtr>{securities.begin(), securities.end()};
auto handles = get_handles(filled_securities);
for (const auto &security : filled_securities) {
auto find_result = handles.find(security->id());
bts_assert_d(find_result != handles.end());
if (find_result != handles.end()) {
cb(security, find_result->second);
}
}
}
// Get all existing synthetic securities
if (_sci) {
for (const auto &item : _sci->get_old_synthetics()) {
const auto &security = item->security;
auto handle = MDConsumerHandle(item);
cb(std::make_shared<api::Security>(security), handle);
}
}
// store the callback
_callbacks.push_back(cb);
lock.release();
// Scan for new securities too
scan_securities(resolve_legs);
}
const Security *get_security(SecurityKey key) const {
return _cache.get_security(key);
}
std::vector<api::Security const *> get_securities() const {
return _cache.get_securities();
}
MDConsumerChannel get_channel(
const std::string &symbol, SecurityType type, Venue exchange) const {
for (auto &inst : _instances) {
if ((inst->symbol() == symbol && inst->type() == type &&
inst->exchange() == exchange) ||
(inst->type() == SECURITYTYPE_SYNTHETIC &&
type == SECURITYTYPE_SYNTHETIC)) {
return MDConsumerChannel(inst);
}
}
bts_assert_msgf(
false, "Invalid MDConsumer::get_channel. sym: {}; typ: {}; exch: {}",
symbol, SecurityType_Name(type), Venue_Name(exchange));
}
bool all_heartbeats_ok() const {
for (auto &inst : _instances) {
if (!inst->is_heart_beating())
return false;
}
return true;
}
size_t security_count() const {
size_t result = 0;
for (auto &inst : _instances) {
result += inst->security_count();
}
if (_sci) {
result += _sci->security_count();
}
return result;
}
void dump_shm_to(const char *p) const {
// To be safe we should lock _work_mutex -but-
// we need this function to diagnose problems happening in the on_security
// callback which holds the _work_mutex. Since anyone calling this
// function is going down anyway we as well live dangerously.
for (auto &inst : _instances) {
inst->dump_shm_to(p);
}
}
};
///////////////////////////////////////////////////////////////////////////////////////////////////
// MDConsumer public interface, forwards to pimple
///////////////////////////////////////////////////////////////////////////////////////////////////
void log_cons(const std::set<UserSymbol> *symbols) {
std::string log_str = "";
if (symbols != nullptr) {
for (const auto &s : *symbols) {
log_str += s.symbol() + "-" + SecurityType_Name(s.type()) + "-" +
Venue_Name(s.exchange()) + ",";
}
}
LOG(md, Info, "MDConsumer::MDConsumer symbols: {}", log_str);
}
MDConsumer::MDConsumer(
SecurityCache &cache,
api::ISyntheticSecurityService *ss,
MBOReader *mbo,
const std::set<UserSymbol> *symbols,
bool shared)
: _pimpl() {
log_cons(symbols);
set<UserSymbol> all_symbols;
if (symbols == nullptr || symbols->empty()) {
all_symbols = SymbolLoader::load();
symbols = &all_symbols;
}
_pimpl.reset(new MDConsumerImpl(*symbols, cache, ss, mbo, shared));
}
MDConsumer::~MDConsumer() {}
bool MDConsumer::all_heartbeats_ok() const {
return _pimpl->all_heartbeats_ok();
}
size_t MDConsumer::security_count() const { return _pimpl->security_count(); }
MDConsumerHandle MDConsumer::get_handle(security_key key) {
return _pimpl->get_handle(key);
}
std::map<security_key, MDConsumerHandle> MDConsumer::get_handles(
const std::set<security_key> &keys) {
return _pimpl->get_handles(keys);
}
shared_ptr<MDConsumerHandle> MDConsumer::get_managed_handle(security_key key) {
return _pimpl->get_managed_handle(key);
}
size_t MDConsumer::walk_security_list(
SecurityCallback cb, const string &symbol, SecurityType type) {
return _pimpl->walk_sec_list(cb, symbol, type);
}
size_t MDConsumer::scan_securities(bool resolve_legs) {
return _pimpl->scan_securities(resolve_legs);
}
void MDConsumer::on_security(SecurityCallback cb, bool resolve_legs) {
_pimpl->on_security(cb, resolve_legs);
}
const api::Security *MDConsumer::get_security(SecurityKey key) const {
return _pimpl->get_security(key);
}
std::vector<api::Security const *> MDConsumer::get_securities() const {
return _pimpl->get_securities();
}
MDConsumerChannel MDConsumer::get_channel(
const std::string &symbol, SecurityType type, Venue exchange) const {
return _pimpl->get_channel(symbol, type, exchange);
}
MDConsumerChannel MDConsumer::get_channel(const UserSymbol &u) const {
return _pimpl->get_channel(u.symbol(), u.type(), u.exchange());
}
void MDConsumer::dump_shm_to(const char *p) const { _pimpl->dump_shm_to(p); }
MDConsumerChannel::MDConsumerChannel(const shared_ptr<MDConsumerInst> &inst)
: _pimpl(inst) {}
int64_t MDConsumerChannel::read_trades(
google::protobuf::RepeatedPtrField<co2::TradeMessage> &trades,
int64_t cursor) {
return _pimpl->read_trades(trades, cursor);
}
int64_t MDConsumerChannel::read_rfqs(
google::protobuf::RepeatedPtrField<co2::QuoteRequestMessage> &quotes,
int64_t cursor) {
return _pimpl->read_rfqs(quotes, cursor);
}
const std::string &MDConsumerChannel::symbol() const {
return _pimpl->symbol();
}
SecurityType MDConsumerChannel::type() const { return _pimpl->type(); }
Venue MDConsumerChannel::exchange() const { return _pimpl->exchange(); }
bool MDConsumerChannel::is_heart_beating() const {
return _pimpl->is_heart_beating();
}
bool MDConsumerChannel::is_initialized() const {
return _pimpl && security_count() > 0;
}
TimeStamp MDConsumerChannel::create_time() const {
return _pimpl ? _pimpl->cold_header().create_time : TimeStamp::invalid();
}
size_t MDConsumerChannel::security_count() const {
return _pimpl->cold_header().security_count;
}
size_t MDConsumerChannel::direct_count() const {
return is_initialized() ? _pimpl->hot_header().direct_count : 0;
}
size_t MDConsumerChannel::implied_count() const {
return is_initialized() ? _pimpl->hot_header().implied_count : 0;
}
size_t MDConsumerChannel::trade_count() const {
return _pimpl->cold_header().trade_count;
}
size_t MDConsumerChannel::rfq_count() const {
return _pimpl->cold_header().rfq_count;
}
size_t MDConsumerChannel::gap_count() const {
return _pimpl->cold_header().gap_count;
}
bool is_heart_beat_ok(const ColdHeader &header) {
uint32_t s_heartbeat_ms = header.heartbeat_ms;
bool s_disable_heartbeat = header.disable_heartbeat;
uint32_t s_grace_period_ms = header.heartbeat_boot_seconds * 1000;
TimeStamp hb = header.heartbeat;
TimeStamp now = TimeStamp::now();
return (header.version == shm_version) &&
(s_disable_heartbeat || (now - hb).milliseconds() < s_heartbeat_ms ||
(now - header.create_time).milliseconds() < s_grace_period_ms);
}
} // namespace md
} // namespace bts
#endif