From 8a7174378a645bfa9dde8cf6915c918f654a57bd Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Tue, 1 Jan 2019 13:32:40 -0500 Subject: [PATCH] SPARK-1948 Implement SecurityCache --- src/lib/md/SecurityCache.cpp | 104 +++++++++++++++++++++++++++++++++++ src/lib/md/SecurityCache.h | 34 ++++++++++++ src/lib/md/SecurityDB.h | 6 +- src/lib/md/SecurityPtr.h | 12 ++++ 4 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 src/lib/md/SecurityCache.cpp create mode 100644 src/lib/md/SecurityCache.h create mode 100644 src/lib/md/SecurityPtr.h diff --git a/src/lib/md/SecurityCache.cpp b/src/lib/md/SecurityCache.cpp new file mode 100644 index 000000000..4e5475515 --- /dev/null +++ b/src/lib/md/SecurityCache.cpp @@ -0,0 +1,104 @@ +#include "SecurityCache.h" +#include "SecurityDB.h" +#include "lib/utils/Dispatcher.h" + +#include +#include +#include + +/* +todo: + 0 - All new code uses SecurityKey + 1 - Implement SecurityCache. It is a simple structure + 2 - MDConsumer has a SecurityCache + 3 - scan_securities inserts securities into SecurityCache + + +*/ + +namespace bts { +namespace md { + +struct SecurityCacheImpl { + std::mutex cache_mutex; + std::unordered_map cache; + tbb::concurrent_queue update_queue; + void update_db(const SecurityPtr&); +}; + +SecurityPtr load_from_security_db(SecurityKey key) { + return db::find_security(key); +} + +void SecurityCacheImpl::update_db(const SecurityPtr& security) { + // Batch updates to the database for performance + // algo: + // push item into queue + // post work to dispatcher with delay + // When dispatcher services request drain update_queue. + // This will cause one update per security but most will be no-op + // This is a little simpler than running a timer and trying to stop it after security discovery is done + update_queue.push(security); + utils::Dispatcher::background().post([this]() { + if (!update_queue.empty()) { + std::vector work; + SecurityPtr temp; + while(update_queue.try_pop(temp)) { work.push_back(temp); } + if (!work.empty()) { + db::insert_or_replace(work); + } + } + }, 1000); +} + +////////////////// + +SecurityCache::SecurityCache() : _me(new SecurityCacheImpl()) {} +SecurityCache::~SecurityCache() {} + +SecurityPtr SecurityCache::try_get_security(SecurityKey key) const { + std::lock_guard lock(_me->cache_mutex); + auto iter = _me->cache.find(key); + if (iter != _me->cache.end()) { + return iter->second; + } + return nullptr; +} + +SecurityPtr SecurityCache::get_security(SecurityKey key) { + std::unique_lock lock(_me->cache_mutex); + auto iter = _me->cache.find(key); + if (iter != _me->cache.end()) { + return iter->second; + } + else { + // unlock for db read + lock.unlock(); + auto security = load_from_security_db(key); + if (security) { + return add_db_security(security); + } + return security; + } +} + +SecurityPtr SecurityCache::add_db_security(const SecurityPtr& security) { + bts_assert(security); + std::lock_guard lock(_me->cache_mutex); + SecurityKey key(security->id()); + auto result = _me->cache.insert(std::make_pair(SecurityKey(security->id()), security)); + // If result was inserted result.first->second is equal to security + // If the value was already present it must be a recently inserted 'live' security and we should use it + return result.first->second; +} +bool SecurityCache::add_live_security(const SecurityPtr& security) { + bts_assert(security); + std::lock_guard lock(_me->cache_mutex); + SecurityKey key(security->id()); + // Insert but don't overwrite existing + auto result = _me->cache.insert(std::make_pair(SecurityKey(security->id()), security)); + _me->update_db(security); + return result.second; +} + +}}//ns \ No newline at end of file diff --git a/src/lib/md/SecurityCache.h b/src/lib/md/SecurityCache.h new file mode 100644 index 000000000..388d66fd8 --- /dev/null +++ b/src/lib/md/SecurityCache.h @@ -0,0 +1,34 @@ +#pragma once + +#include "lib/domain_model/Security.pb.h" +#include "MDConsumerHandle.h" +#include "lib/domain_model/SecurityKey.h" +#include "SecurityPtr.h" +#include + +namespace bts { +namespace md { + +// Get security on demand +// Store security+handle +struct SecurityCacheImpl; +class SecurityCache { + std::unique_ptr _me; + +public: + SecurityCache(); + ~SecurityCache(); + + //Load security from cache with fallback to database + SecurityPtr get_security(SecurityKey key); + + // Get the security if it's in the cache otherwise return nullptr + SecurityPtr try_get_security(SecurityKey key) const; + + // returns true if the cache was updated + bool add_live_security(const SecurityPtr& security); + SecurityPtr add_db_security(const SecurityPtr& security); + +}; + +}}//ns \ No newline at end of file diff --git a/src/lib/md/SecurityDB.h b/src/lib/md/SecurityDB.h index 3e7b9e2ec..77948dcce 100644 --- a/src/lib/md/SecurityDB.h +++ b/src/lib/md/SecurityDB.h @@ -7,6 +7,8 @@ #include "lib/api/services/SyntheticSecurityService.pb.h" #include "common.h" #include "UserSymbol.h" +#include "lib/domain_model/SecurityKey.h" +#include "SecurityPtr.h" #include #include @@ -25,7 +27,9 @@ size_t load_securities( const std::set& symbols, const std::function& callback); -void store_if_new(const std::vector& handles); +SecurityPtr find_security(SecurityKey); + +void insert_or_replace(const std::vector&); // metadata std::vector load_metadata(); diff --git a/src/lib/md/SecurityPtr.h b/src/lib/md/SecurityPtr.h new file mode 100644 index 000000000..34c93965d --- /dev/null +++ b/src/lib/md/SecurityPtr.h @@ -0,0 +1,12 @@ +#pragma once + +#include "lib/domain_model/SecurityKey.h" +#include + +namespace bts { +namespace api {class Security;} +namespace md { + + typedef std::shared_ptr const SecurityPtr; + +}} \ No newline at end of file