1030 lines
38 KiB
C++
1030 lines
38 KiB
C++
#ifndef NO_CPP0X
|
|
#include "MDConsumer.h"
|
|
#include "MemorySegmentUtils.h"
|
|
#include "PlatformLoader.h"
|
|
#include "SecurityCache.h"
|
|
#include "MBOProcessor.h"
|
|
#include "lib/config/config.h"
|
|
#include "lib/utils/SharedList.h"
|
|
#include "lib/utils/WaitFreeQueue.h"
|
|
#include "lib/utils/bts_chrono.h"
|
|
|
|
#include "lib/config/protos/MarketData.pb.h"
|
|
#include "lib/utils/sane_algorithm.h"
|
|
|
|
#include <tbb/queuing_mutex.h>
|
|
#include <string>
|
|
#include <map>
|
|
#include <set>
|
|
#include <algorithm>
|
|
#include <tuple>
|
|
|
|
using namespace std;
|
|
using namespace bts::api;
|
|
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 &);
|
|
|
|
class MDConsumerInst {
|
|
public:
|
|
MDConsumerInst(const string& symbol, SecurityType type, Venue exchange, bool shared, co2::Platform platform) :
|
|
_symbol(symbol),
|
|
_type(type),
|
|
_exchange(exchange),
|
|
_platform(platform == co2::PLATFORM_UNSET ? platform_for_exchange(exchange) : platform),
|
|
_security_count(0),
|
|
_found_keys(),
|
|
_hot(),
|
|
_hot_depth(),
|
|
_cold(cold_segment_name(symbol, type, exchange, _platform), shared),
|
|
_queues()
|
|
{
|
|
if (enable_depth(symbol, type)) {
|
|
_hot_depth.reset(new utils::SharedListReader<HotHeader, HotDepthEntry>(hot_segment_name(symbol, type, exchange, _platform), shared));
|
|
}
|
|
else {
|
|
_hot.reset(new utils::SharedListReader<HotHeader, HotEntry>(hot_segment_name(symbol, type, exchange, _platform), 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;
|
|
}
|
|
|
|
void search_handles(map<security_key, MDConsumerHandle>& handles) {
|
|
// Walk hot and cold lists and fill in any found handles
|
|
if (!_cold.is_initialized()) return;
|
|
_cold.all_entries([&](const ColdEntry& entry) {
|
|
auto iter = handles.find(entry.security.key);
|
|
if (iter != handles.end()) {
|
|
iter->second.set_cold(&cold_header(), &entry);
|
|
}
|
|
});
|
|
// Do the same for hot
|
|
if (_hot_depth) {
|
|
_hot_depth->all_entries([&](const HotDepthEntry& entry) {
|
|
auto iter = handles.find(entry.key);
|
|
if (iter != handles.end() && iter->second.is_initialized()) {
|
|
iter->second.set_hot(&entry);
|
|
}
|
|
});
|
|
}
|
|
else if (_hot){
|
|
_hot->all_entries([&](const HotEntry& entry) {
|
|
auto iter = handles.find(entry.key);
|
|
if (iter != handles.end() && iter->second.is_initialized()) {
|
|
iter->second.set_hot(&entry);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
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) {
|
|
if (entry.security.key != invalid_security_key && _found_keys.count(entry.security.key) == 0) {
|
|
_found_keys.insert(entry.security.key);
|
|
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; }
|
|
co2::Platform platform() const { return _platform; }
|
|
|
|
//
|
|
// 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;
|
|
co2::Platform _platform;
|
|
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([=](const api::Security& sec) { add_security(sec); });
|
|
_meta_sub = _ss->on_metadata().subscribe([=](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(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;
|
|
// co2::Platform _platform;
|
|
|
|
public:
|
|
MDConsumerImpl(
|
|
const set<UserSymbol>& symbols,
|
|
SecurityCache& cache,
|
|
api::ISyntheticSecurityService* ss,
|
|
MBOReader* mbo,
|
|
bool shared, co2::Platform platform) :
|
|
_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 MDConsumer for securities: {} on {}" ,msg.str(), co2::Platform_Name(platform));
|
|
|
|
for (auto& s : symbols) {
|
|
if (s.type() == SECURITYTYPE_FUTURE) {
|
|
_instances.push_back(make_shared<MDConsumerInst>(s.symbol(), s.type(), s.exchange(), shared, platform));
|
|
}
|
|
}
|
|
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, platform));
|
|
}
|
|
has_synthetic_instance = true;
|
|
}
|
|
else {
|
|
_instances.push_back(make_shared<MDConsumerInst>(s.symbol(), s.type(), s.exchange(), shared, platform));
|
|
}
|
|
}
|
|
}
|
|
|
|
if (ss != nullptr) {
|
|
_sci = std::make_shared<SyntheticConsumerInst>(ss, [=](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;
|
|
// Initialize the result to match the input
|
|
for (security_key key : keys) {
|
|
result.insert(std::make_pair(key, MDConsumerHandle()));
|
|
}
|
|
|
|
for (auto& inst : _instances) {
|
|
inst->search_handles(result);
|
|
}
|
|
|
|
// Erase anything that wasn't found from the resulting map
|
|
// Set the mbo pointer on anything else
|
|
for (auto it = result.begin(); it != result.end(); ) {
|
|
if (!it->second.is_initialized()) {
|
|
it = result.erase(it);
|
|
}
|
|
else {
|
|
it->second.set_mbo(_mbo);
|
|
++it;
|
|
}
|
|
}
|
|
|
|
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) {
|
|
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(), 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,Warn,"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);
|
|
}
|
|
|
|
SecurityPtr get_security(SecurityKey key) const {
|
|
return _cache.get_security(key);
|
|
}
|
|
|
|
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_msg(false, "Invalid symbol/type/exchange in MDConsumer::get_channel");
|
|
}
|
|
|
|
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
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
MDConsumer::MDConsumer(
|
|
SecurityCache& cache,
|
|
api::ISyntheticSecurityService* ss,
|
|
MBOReader* mbo,
|
|
const std::set<UserSymbol>* symbols,
|
|
bool shared,
|
|
co2::Platform platform) : _pimpl() {
|
|
|
|
set<UserSymbol> all_symbols;
|
|
if (symbols == nullptr || symbols->empty()) {
|
|
all_symbols = get_system_md_symbols();
|
|
symbols = &all_symbols;
|
|
}
|
|
|
|
_pimpl.reset(new MDConsumerImpl(*symbols, cache, ss, mbo, shared, platform));
|
|
}
|
|
|
|
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).get(); }
|
|
|
|
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(); }
|
|
Platform MDConsumerChannel::platform() const { return _pimpl->platform(); }
|
|
|
|
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
|
|
|
|
#endif
|