From c8bd96e9e0a70fc11dd9bc188934faadb96bf080 Mon Sep 17 00:00:00 2001 From: Pedro Pinto Date: Fri, 25 Aug 2017 15:33:23 +0000 Subject: [PATCH] Refactored JetFIX --- src/server/sparkd/CMakeLists.txt | 2 +- .../sparkd/jetfix/gateway/CoreGateway.cpp | 6 ++- .../sparkd/jetfix/gateway/CoreGateway.h | 4 +- .../sparkd/jetfix/gateway/JetFIXGateway.h | 4 +- src/server/sparkd/jetfix/gateway/Session.cpp | 8 ++-- src/server/sparkd/jetfix/gateway/Session.h | 4 +- .../store/{FileStore.cpp => FIXFileStore.cpp} | 37 +++++++++++-------- .../store/{FileStore.h => FIXFileStore.h} | 8 ++-- .../jetfix/store/{IStore.h => IFIXStore.h} | 4 +- .../sparkd/jetfix/tests/store_tests.cpp | 16 ++++---- 10 files changed, 50 insertions(+), 43 deletions(-) rename src/server/sparkd/jetfix/store/{FileStore.cpp => FIXFileStore.cpp} (90%) rename src/server/sparkd/jetfix/store/{FileStore.h => FIXFileStore.h} (75%) rename src/server/sparkd/jetfix/store/{IStore.h => IFIXStore.h} (92%) diff --git a/src/server/sparkd/CMakeLists.txt b/src/server/sparkd/CMakeLists.txt index 7ab0e86f0..a3b530727 100644 --- a/src/server/sparkd/CMakeLists.txt +++ b/src/server/sparkd/CMakeLists.txt @@ -30,7 +30,7 @@ set (Sparkd_Sources jetfix/gateway/CoreGateway.cpp jetfix/gateway/JetFIXGateway.cpp jetfix/gateway/Session.cpp - jetfix/store/FileStore.cpp + jetfix/store/FIXFileStore.cpp jetfix/store/session_name.cpp jetfix/tests/async_reader_tests.cpp jetfix/tests/decoding_tests.cpp diff --git a/src/server/sparkd/jetfix/gateway/CoreGateway.cpp b/src/server/sparkd/jetfix/gateway/CoreGateway.cpp index d10a55b6d..2c7fd3074 100644 --- a/src/server/sparkd/jetfix/gateway/CoreGateway.cpp +++ b/src/server/sparkd/jetfix/gateway/CoreGateway.cpp @@ -5,7 +5,7 @@ #include "server/sparkd/jetfix/connection/IConnection.h" #include "server/sparkd/jetfix/gateway/ICoreGatewayObserver.h" #include "server/sparkd/jetfix/gateway/Session.h" -#include "server/sparkd/jetfix/store/IStore.h" +#include "server/sparkd/jetfix/store/IFIXStore.h" #include "lib/logging/logging.h" #include "lib/utils/TimeStamp.h" #include "lib/utils/datetime.h" @@ -16,6 +16,7 @@ #include #include #include +#include namespace bts { namespace jetfix { @@ -28,7 +29,7 @@ struct CoreGateway::SessionInfo {} std::unique_ptr connection; - std::unique_ptr store; + std::unique_ptr store; std::unique_ptr reader; std::unique_ptr session; }; @@ -197,6 +198,7 @@ void CoreGateway::dispatch_events () void CoreGateway::call_time () { + os::set_thread_name(fmt::format("{0}_timer",me->cfg.name()).c_str()); while (!me->destroy) { std::this_thread::sleep_for(10ms); auto ts = std::make_shared ( diff --git a/src/server/sparkd/jetfix/gateway/CoreGateway.h b/src/server/sparkd/jetfix/gateway/CoreGateway.h index 83c28e7b9..a05100b83 100644 --- a/src/server/sparkd/jetfix/gateway/CoreGateway.h +++ b/src/server/sparkd/jetfix/gateway/CoreGateway.h @@ -12,14 +12,14 @@ namespace bts { namespace jetfix { class ICoreGatewayObserver; class IConnection; -class IStore; +class IFIXStore; class OutMsgsBuffer; class CoreGateway : ICoreGatewayEventSink { public: typedef std::function ConnectionFactory; - typedef std::function StoreFactory; + typedef std::function StoreFactory; CoreGateway(config::JetFIX::Connection const &cfg, ICoreGatewayObserver &observer, diff --git a/src/server/sparkd/jetfix/gateway/JetFIXGateway.h b/src/server/sparkd/jetfix/gateway/JetFIXGateway.h index 9520cc605..be8ce0d46 100644 --- a/src/server/sparkd/jetfix/gateway/JetFIXGateway.h +++ b/src/server/sparkd/jetfix/gateway/JetFIXGateway.h @@ -4,7 +4,7 @@ #include "server/sparkd/jetfix/gateway/CoreGateway.h" #include "server/sparkd/jetfix/gateway/ICoreGatewayObserver.h" #include "server/sparkd/jetfix/connection/SocketConnection.h" -#include "server/sparkd/jetfix/store/FileStore.h" +#include "server/sparkd/jetfix/store/FIXFileStore.h" #include "lib/utils/meta_programming.h" #include "lib/config/protos/jetfix.pb.h" @@ -25,7 +25,7 @@ public: {return new SocketConnection (cfg);}, [] (config::JetFIX::Connection const &cfg) - { return new FileStore (cfg);}), + { return new FIXFileStore (cfg);}), _dispatchers (), _dispatcher_idx (), _on_ready ([](){}), diff --git a/src/server/sparkd/jetfix/gateway/Session.cpp b/src/server/sparkd/jetfix/gateway/Session.cpp index c5fb09663..0ebce5de9 100644 --- a/src/server/sparkd/jetfix/gateway/Session.cpp +++ b/src/server/sparkd/jetfix/gateway/Session.cpp @@ -8,7 +8,7 @@ #include "server/sparkd/jetfix/common/OutFIX.h" #include "server/sparkd/jetfix/connection/IConnection.h" #include "server/sparkd/jetfix/gateway/ICoreGatewayEventSink.h" -#include "server/sparkd/jetfix/store/IStore.h" +#include "server/sparkd/jetfix/store/IFIXStore.h" #include "lib/utils/TimeStamp.h" #include "lib/utils/datetime.h" #include "lib/logging/logging.h" @@ -46,7 +46,7 @@ struct Session::PImpl { PImpl(config::JetFIX::Connection const &cfg, IConnection &connection, - IStore &store, + IFIXStore &store, ICoreGatewayEventSink &sink, Clock const &clock) : @@ -73,7 +73,7 @@ struct Session::PImpl config::JetFIX::Connection cfg; State state; IConnection &connection; - IStore &store; + IFIXStore &store; ICoreGatewayEventSink &event_sink; Clock now; uint32_t begin_of_this_session; @@ -90,7 +90,7 @@ struct Session::PImpl Session::Session(config::JetFIX::Connection const &cfg, IConnection &connection, - IStore &store, + IFIXStore &store, ICoreGatewayEventSink &sink, Clock const &clock) : me (new PImpl(cfg,connection,store,sink,clock)) diff --git a/src/server/sparkd/jetfix/gateway/Session.h b/src/server/sparkd/jetfix/gateway/Session.h index fa27e7697..4237d920a 100644 --- a/src/server/sparkd/jetfix/gateway/Session.h +++ b/src/server/sparkd/jetfix/gateway/Session.h @@ -14,7 +14,7 @@ namespace bts { namespace utils {class TimeStamp;} namespace jetfix { class IConnection; -class IStore; +class IFIXStore; class ICoreGatewayEventSink; class Session @@ -24,7 +24,7 @@ public: Session(config::JetFIX::Connection const &cfg, IConnection &connection, - IStore &store, + IFIXStore &store, ICoreGatewayEventSink &sink, Clock const &clock); ~Session(); diff --git a/src/server/sparkd/jetfix/store/FileStore.cpp b/src/server/sparkd/jetfix/store/FIXFileStore.cpp similarity index 90% rename from src/server/sparkd/jetfix/store/FileStore.cpp rename to src/server/sparkd/jetfix/store/FIXFileStore.cpp index a2c404628..528321ada 100644 --- a/src/server/sparkd/jetfix/store/FileStore.cpp +++ b/src/server/sparkd/jetfix/store/FIXFileStore.cpp @@ -1,4 +1,4 @@ -#include "FileStore.h" +#include "FIXFileStore.h" #include "lib/config/config.h" #include "server/sparkd/jetfix/codec/FIXEncodingStream.h" @@ -16,11 +16,12 @@ #include #include #include +#include namespace bts { namespace jetfix { -struct FileStore::Event +struct FIXFileStore::Event { Event () : out_msgs (), in_msg (), @@ -72,9 +73,9 @@ struct FileStore::Event uint32_t last_msg_processed; }; -struct FileStore::PImpl +struct FIXFileStore::PImpl { - PImpl() : + PImpl(config::JetFIX::Connection const&cfg) : active (false), store_called (false), replay_messages (), @@ -83,7 +84,8 @@ struct FileStore::PImpl seq_num_path (), fix_store (), writer_thread (), - event_queue () + event_queue (), + cfg(cfg) {} bool active; @@ -95,6 +97,7 @@ struct FileStore::PImpl std::ofstream fix_store; std::thread writer_thread; moodycamel::ReaderWriterQueue event_queue; + config::JetFIX::Connection const cfg; }; std::string fix_log_path (std::string const&base_path) @@ -194,7 +197,8 @@ void load_seq_nums (std::string const&path,uint32_t* last_seq_num, uint32_t *las } } -FileStore::FileStore (config::JetFIX::Connection const&cfg,std::string base_path) : me (new PImpl()) { +FIXFileStore::FIXFileStore (config::JetFIX::Connection const&cfg,std::string base_path) + : me (new PImpl(cfg)) { auto sn = session_name (cfg); me->active = sn.size () > 0; if (me->active) { @@ -225,11 +229,11 @@ FileStore::FileStore (config::JetFIX::Connection const&cfg,std::string base_path me->fix_store.open (fix_log_p,std::ofstream::app); create_current_simlink (path); - me->writer_thread = std::thread (std::bind (&FileStore::write,this)); + me->writer_thread = std::thread (std::bind (&FIXFileStore::write,this)); } } -FileStore::~FileStore () +FIXFileStore::~FIXFileStore () { if (me->active) { Event ev; @@ -239,28 +243,28 @@ FileStore::~FileStore () } } -bool FileStore::session_active () const +bool FIXFileStore::session_active () const {return me->active;} -std::vector FileStore::replay () const +std::vector FIXFileStore::replay () const { bts_assert (me->active && !me->store_called); return me->replay_messages; } -uint32_t FileStore::last_seq_num () const +uint32_t FIXFileStore::last_seq_num () const { bts_assert (me->active && !me->store_called); return me->last_seq_num; } -uint32_t FileStore::last_seq_num_processed () const +uint32_t FIXFileStore::last_seq_num_processed () const { bts_assert (me->active && !me->store_called); return me->last_seq_num_processed; } -void FileStore::store (OutFIXPtr const&msgs) +void FIXFileStore::store (OutFIXPtr const&msgs) { bts_assert (me->active); me->store_called = true; @@ -269,7 +273,7 @@ void FileStore::store (OutFIXPtr const&msgs) me->event_queue.enqueue (ev); } -void FileStore::store (InMsgBuffer const&msg) +void FIXFileStore::store (InMsgBuffer const&msg) { bts_assert (me->active); me->store_called = true; @@ -278,7 +282,7 @@ void FileStore::store (InMsgBuffer const&msg) me->event_queue.enqueue (ev); } -void FileStore::set_last_msg_processed (uint32_t last_msg_processed) +void FIXFileStore::set_last_msg_processed (uint32_t last_msg_processed) { bts_assert (me->active); me->store_called = true; @@ -288,9 +292,10 @@ void FileStore::set_last_msg_processed (uint32_t last_msg_processed) me->event_queue.enqueue (ev); } -void FileStore::write () +void FIXFileStore::write () { Event ev; + os::set_thread_name(fmt::format("{0}_store",me->cfg.name()).c_str()); while (true) if (me->event_queue.try_dequeue (ev)) { diff --git a/src/server/sparkd/jetfix/store/FileStore.h b/src/server/sparkd/jetfix/store/FIXFileStore.h similarity index 75% rename from src/server/sparkd/jetfix/store/FileStore.h rename to src/server/sparkd/jetfix/store/FIXFileStore.h index 9be698474..5bf373a23 100644 --- a/src/server/sparkd/jetfix/store/FileStore.h +++ b/src/server/sparkd/jetfix/store/FIXFileStore.h @@ -1,17 +1,17 @@ #pragma once #include -#include "server/sparkd/jetfix/store/IStore.h" +#include "server/sparkd/jetfix/store/IFIXStore.h" #include "lib/config/protos/jetfix.pb.h" namespace bts { namespace jetfix { -class FileStore : public IStore +class FIXFileStore : public IFIXStore { public: - FileStore(config::JetFIX::Connection const&cfg,std::string base_path=""); - ~FileStore(); + FIXFileStore(config::JetFIX::Connection const&cfg,std::string base_path=""); + ~FIXFileStore(); bool session_active () const override; std::vector replay () const override; diff --git a/src/server/sparkd/jetfix/store/IStore.h b/src/server/sparkd/jetfix/store/IFIXStore.h similarity index 92% rename from src/server/sparkd/jetfix/store/IStore.h rename to src/server/sparkd/jetfix/store/IFIXStore.h index 885795b41..7c66b72f0 100644 --- a/src/server/sparkd/jetfix/store/IStore.h +++ b/src/server/sparkd/jetfix/store/IFIXStore.h @@ -9,10 +9,10 @@ namespace bts { namespace jetfix { class InMsgBuffer; -class IStore +class IFIXStore { public: - virtual ~IStore() {}; + virtual ~IFIXStore() {}; virtual bool session_active () const = 0; virtual std::vector replay () const = 0; diff --git a/src/server/sparkd/jetfix/tests/store_tests.cpp b/src/server/sparkd/jetfix/tests/store_tests.cpp index f03496e78..065ebcbae 100644 --- a/src/server/sparkd/jetfix/tests/store_tests.cpp +++ b/src/server/sparkd/jetfix/tests/store_tests.cpp @@ -4,7 +4,7 @@ #include "server/sparkd/jetfix/common/InMsgBuffer.h" #include "server/sparkd/jetfix/common/OutFIX.h" #include "server/sparkd/jetfix/common/OutFIXPtr.h" -#include "server/sparkd/jetfix/store/FileStore.h" +#include "server/sparkd/jetfix/store/FIXFileStore.h" #include "server/sparkd/jetfix/store/session_name.h" #include "lib/utils/TempDir.h" #include "lib/utils/TimeStamp.h" @@ -149,7 +149,7 @@ TEST_CASE ("JetFIX FileStoreTests", "[jetfix]") utils::TempDir td; - FileStore fs (c,td.name ()); + FIXFileStore fs (c,td.name ()); REQUIRE (fs.session_active ()); REQUIRE (fs.replay ().size ()==0); REQUIRE (fs.last_seq_num ()==0); @@ -182,12 +182,12 @@ TEST_CASE ("JetFIX FileStoreTests", "[jetfix]") auto fix = stream.to_str (); { - FileStore fs (c,td->name ()); + FIXFileStore fs (c,td->name ()); fs.store (InMsgBuffer (fix)); fs.set_last_msg_processed (3); } - FileStore fs (c,td->name ()); + FIXFileStore fs (c,td->name ()); auto replay = fs.replay (); REQUIRE (replay.size ()==1); REQUIRE (replay[0].type ()==MessageTypes::Logon); @@ -222,11 +222,11 @@ TEST_CASE ("JetFIX FileStoreTests", "[jetfix]") stream.stamp (2,3,datetime::FIX_timestamp (datetime::now ())); { - FileStore fs (c,td->name ()); + FIXFileStore fs (c,td->name ()); fs.store (msg_vec); } - FileStore fs (c,td->name ()); + FIXFileStore fs (c,td->name ()); auto replay = fs.replay (); REQUIRE (replay.size ()==0); REQUIRE (fs.last_seq_num ()==3); @@ -248,7 +248,7 @@ TEST_CASE ("JetFIX FileStoreTests", "[jetfix]") auto start = utils::TimeStamp::now (); { - FileStore fs (c,td->name ()); + FIXFileStore fs (c,td->name ()); for (int i = 0; i < 10000;i++) { auto msg_vec = std::make_shared>(); msg_vec->emplace_back (c); @@ -269,7 +269,7 @@ TEST_CASE ("JetFIX FileStoreTests", "[jetfix]") std::cout << "\tjetfix store gen: " << (stop-start).milliseconds () << "ms\n"; start = utils::TimeStamp::now (); - FileStore fs (c,td->name ()); + FIXFileStore fs (c,td->name ()); stop = utils::TimeStamp::now (); std::cout << "\tjetfix store load: " << (stop-start).milliseconds () << "ms\n"; }