diff --git a/src/lib/jetfix/gateway/CoreGateway.cpp b/src/lib/jetfix/gateway/CoreGateway.cpp index 29c6b72a7..7b0974be3 100644 --- a/src/lib/jetfix/gateway/CoreGateway.cpp +++ b/src/lib/jetfix/gateway/CoreGateway.cpp @@ -12,13 +12,14 @@ #include "lib/jetfix/store/IFIXStore.h" #include "lib/orderman/orderman_utils/orderman_common.h" #include "lib/tracing/bts_trace.h" +#include #include #include #include #include -#include #include +#include namespace bts { namespace jetfix { @@ -110,7 +111,7 @@ struct CoreGateway::PImpl { State state; datetime::PTime start_of_pause; std::unique_ptr session_info; - moodycamel::ConcurrentQueue event_queue; + tbb::concurrent_queue event_queue; std::thread timer_thread; std::atomic destroy; bool is_on; @@ -140,50 +141,50 @@ CoreGateway::~CoreGateway() { void CoreGateway::send(orderman::TickToTradeCorID cid, OutFIXPtr const &fix) { tracepoint(bts_trace, trace_order, (char *)"CoreGateway.send", 0, cid,0); - me->event_queue.enqueue(EventData(&CoreGateway::sync_send, cid, fix)); + me->event_queue.push(EventData(&CoreGateway::sync_send, cid, fix)); } void CoreGateway::send_hb() { - me->event_queue.enqueue(EventData(&CoreGateway::sync_send_hb)); + me->event_queue.push(EventData(&CoreGateway::sync_send_hb)); } void CoreGateway::send_test_request() { - me->event_queue.enqueue(EventData(&CoreGateway::sync_send_test_request)); + me->event_queue.push(EventData(&CoreGateway::sync_send_test_request)); } void CoreGateway::start() { - me->event_queue.enqueue(EventData(&CoreGateway::sync_start)); + me->event_queue.push(EventData(&CoreGateway::sync_start)); } void CoreGateway::stop() { - me->event_queue.enqueue(EventData(&CoreGateway::sync_stop)); + me->event_queue.push(EventData(&CoreGateway::sync_stop)); } void CoreGateway::session_ready() { - me->event_queue.enqueue(EventData(&CoreGateway::sync_session_ready)); + me->event_queue.push(EventData(&CoreGateway::sync_session_ready)); } void CoreGateway::session_terminated() { - me->event_queue.enqueue(EventData(&CoreGateway::sync_session_terminated)); + me->event_queue.push(EventData(&CoreGateway::sync_session_terminated)); } void CoreGateway::reader_disconnected() { - me->event_queue.enqueue(EventData(&CoreGateway::sync_reader_disconnected)); + me->event_queue.push(EventData(&CoreGateway::sync_reader_disconnected)); } void CoreGateway::in_msg_raw(InMsgBuffer const &msg) { - me->event_queue.enqueue(EventData(&CoreGateway::sync_in_msg_raw, msg)); + me->event_queue.push(EventData(&CoreGateway::sync_in_msg_raw, msg)); } void CoreGateway::in_msg_sequenced(InMsgBuffer const &msg) { - me->event_queue.enqueue(EventData(&CoreGateway::sync_in_msg_sequenced, msg)); + me->event_queue.push(EventData(&CoreGateway::sync_in_msg_sequenced, msg)); } void CoreGateway::dispatch_events() { EventData ev; utils::TimeStamp cid; // we prioratize sends over receives - while (me->event_queue.try_dequeue(ev)) { + while (me->event_queue.try_pop(ev)) { if (ev.out_msgs) { cid = ev.out_msgs->get_ts(jetfix::OutFIX::MD_WRITE_TS); me->send_batch.push_back(ev); @@ -207,7 +208,7 @@ void CoreGateway::call_time() { std::this_thread::sleep_for(10ms); auto ts = std::make_shared( datetime::FIX_timestamp(datetime::now())); - me->event_queue.enqueue(EventData(&CoreGateway::sync_time, ts)); // NOLINT + me->event_queue.push(EventData(&CoreGateway::sync_time, ts)); // NOLINT } } diff --git a/src/lib/jetfix/store/FIXFileStore.cpp b/src/lib/jetfix/store/FIXFileStore.cpp index 14b13f56d..e1eb310b4 100644 --- a/src/lib/jetfix/store/FIXFileStore.cpp +++ b/src/lib/jetfix/store/FIXFileStore.cpp @@ -5,55 +5,41 @@ #include "lib/jetfix/common/InMsgBuffer.h" #include "lib/jetfix/common/OutFIX.h" #include "lib/jetfix/store/session_name.h" +#include "lib/os/os.h" +#include "lib/utils/bts_chrono.h" #include "lib/utils/datetime.h" #include "lib/utils/file_utils.h" -#include "lib/utils/bts_chrono.h" -#include "lib/os/os.h" -#include -#include +#include #include #include -#include -#include +#include + #include +#include +#include namespace bts { namespace jetfix { -struct FIXFileStore::Event -{ - Event () - : out_msgs (), in_msg (), - quit (false), - has_last_msg_processed (false), - last_msg_processed (0), - has_seq_num (false), - seq_num (0) - {} +struct FIXFileStore::Event { + Event() + : out_msgs(), in_msg(), quit(false), has_last_msg_processed(false), + last_msg_processed(0), has_seq_num(false), seq_num(0) {} - Event (Event const &src) - : out_msgs (src.out_msgs), - in_msg (src.in_msg), - quit (src.quit), - has_last_msg_processed (src.has_last_msg_processed), - last_msg_processed (src.last_msg_processed), - has_seq_num (src.has_seq_num), - seq_num (src.seq_num) - {} + Event(Event const &src) + : out_msgs(src.out_msgs), in_msg(src.in_msg), quit(src.quit), + has_last_msg_processed(src.has_last_msg_processed), + last_msg_processed(src.last_msg_processed), + has_seq_num(src.has_seq_num), seq_num(src.seq_num) {} - Event (Event &&src) - : out_msgs (std::move(src.out_msgs)), - in_msg (std::move(src.in_msg)), - quit (src.quit), - has_last_msg_processed (src.has_last_msg_processed), - last_msg_processed (src.last_msg_processed), - has_seq_num (src.has_seq_num), - seq_num (src.seq_num) - {} + Event(Event &&src) + : out_msgs(std::move(src.out_msgs)), in_msg(std::move(src.in_msg)), + quit(src.quit), has_last_msg_processed(src.has_last_msg_processed), + last_msg_processed(src.last_msg_processed), + has_seq_num(src.has_seq_num), seq_num(src.seq_num) {} - Event& operator=(Event const &src) - { + Event &operator=(Event const &src) { out_msgs = src.out_msgs; in_msg = src.in_msg; quit = src.quit; @@ -64,8 +50,7 @@ struct FIXFileStore::Event return *this; } - Event& operator=(Event const &&src) - { + Event &operator=(Event const &&src) { out_msgs = std::move(src.out_msgs); in_msg = std::move(src.in_msg); quit = src.quit; @@ -84,21 +69,12 @@ struct FIXFileStore::Event bool has_seq_num; uint32_t seq_num; }; - -struct FIXFileStore::PImpl -{ - PImpl(config::JetFIX::Connection const&cfg) : - active (false), - store_called (false), - replay_messages (), - last_seq_num (0), - last_seq_num_processed (0), - seq_num_path (), - fix_store (), - writer_thread (), - event_queue (), - cfg(cfg) - {} + +struct FIXFileStore::PImpl { + PImpl(config::JetFIX::Connection const &cfg) + : active(false), store_called(false), replay_messages(), last_seq_num(0), + last_seq_num_processed(0), seq_num_path(), fix_store(), + writer_thread(), event_queue(), cfg(cfg) {} bool active; bool store_called; @@ -108,237 +84,224 @@ struct FIXFileStore::PImpl std::string seq_num_path; std::ofstream fix_store; std::thread writer_thread; - moodycamel::ReaderWriterQueue event_queue; + tbb::concurrent_queue event_queue; config::JetFIX::Connection const cfg; }; -std::string fix_log_path (std::string const&base_path) -{ +std::string fix_log_path(std::string const &base_path) { boost::filesystem::path result = base_path; result /= "fix"; - return result.string (); + return result.string(); } -std::string seqnum_path (std::string const&base_path) -{ +std::string seqnum_path(std::string const &base_path) { boost::filesystem::path result = base_path; result /= "seqnum"; - return result.string (); + return result.string(); } -std::string store_path (std::string base_path, - std::string const&connection_name, - std::string const&session_name) -{ - if (base_path.empty ()) - base_path = os::bts_data_path (); - boost::filesystem::path result = utils::normalize_data_path (base_path); +std::string store_path(std::string base_path, + std::string const &connection_name, + std::string const &session_name) { + if (base_path.empty()) + base_path = os::bts_data_path(); + boost::filesystem::path result = utils::normalize_data_path(base_path); result /= "jetfix"; result /= connection_name; result /= session_name; - return result.string (); + return result.string(); } -std::map -load_replay (config::JetFIX::Connection const &cfg,std::string const&path, uint32_t max_sn) -{ - std::map result; - if (boost::filesystem::exists (path)) { +std::map +load_replay(config::JetFIX::Connection const &cfg, std::string const &path, + uint32_t max_sn) { + std::map result; + if (boost::filesystem::exists(path)) { - std::ifstream f (path); + std::ifstream f(path); std::string line_buffer; - line_buffer.reserve (10*1024); - auto header_size = sizeof("20150427-19:05:17.032 < ")-1; - std::string date_buffer ("20150427-19:05:17.032"); - + line_buffer.reserve(10 * 1024); + auto header_size = sizeof("20150427-19:05:17.032 < ") - 1; + std::string date_buffer("20150427-19:05:17.032"); + for (;;) { - std::getline (f,line_buffer); - if (!f.eof ()) { - bts_assert (line_buffer.size () > header_size); - if (line_buffer[22]=='<') { - auto ts = datetime::parse_FIX_timestamp (line_buffer.substr (0,21)); - if (!datetime::is_at_least_secs_old (ts,cfg.seconds_to_replay ())) { - InMsgBuffer msg (line_buffer.substr (24)); - auto sn = msg.get_seq_num (); + std::getline(f, line_buffer); + if (!f.eof()) { + bts_assert(line_buffer.size() > header_size); + if (line_buffer[22] == '<') { + auto ts = + datetime::parse_FIX_timestamp(line_buffer.substr(0, 21)); + if (!datetime::is_at_least_secs_old(ts, + cfg.seconds_to_replay())) { + InMsgBuffer msg(line_buffer.substr(24)); + auto sn = msg.get_seq_num(); if (sn <= max_sn) result[sn] = msg; } } - } - else + } else break; } } return result; } -void save_fix (std::ostream &out, const char*data, size_t size, bool is_outgoing) -{ - out << datetime::FIX_timestamp (datetime::now ()) << " " +void save_fix(std::ostream &out, const char *data, size_t size, + bool is_outgoing) { + out << datetime::FIX_timestamp(datetime::now()) << " " << (is_outgoing ? ">" : "<") << " "; - out.write (data,size); + out.write(data, size); out << std::endl; } -void save_seq_nums (std::string const&path,uint32_t last_seq_num, uint32_t last_seq_num_processed) -{ - std::ofstream f (path); +void save_seq_nums(std::string const &path, uint32_t last_seq_num, + uint32_t last_seq_num_processed) { + std::ofstream f(path); f << last_seq_num << " " << last_seq_num_processed << std::endl; } -void load_seq_nums (std::string const&path,uint32_t* last_seq_num, uint32_t *last_seq_num_processed) -{ - if (boost::filesystem::exists (path)) { - std::ifstream f (path); +void load_seq_nums(std::string const &path, uint32_t *last_seq_num, + uint32_t *last_seq_num_processed) { + if (boost::filesystem::exists(path)) { + std::ifstream f(path); f >> *last_seq_num >> *last_seq_num_processed; } else { *last_seq_num = 0; *last_seq_num_processed = 0; - save_seq_nums (path, *last_seq_num,*last_seq_num_processed); + save_seq_nums(path, *last_seq_num, *last_seq_num_processed); } } -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; +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) { - auto path = store_path (base_path,cfg.name (),sn); - if (!boost::filesystem::exists (path)) - bts_assert (boost::filesystem::create_directories (path)); + auto path = store_path(base_path, cfg.name(), sn); + if (!boost::filesystem::exists(path)) + bts_assert(boost::filesystem::create_directories(path)); // load seq numbers - auto seq_num_p = seqnum_path (path); - load_seq_nums (seq_num_p,&me->last_seq_num,&me->last_seq_num_processed); + auto seq_num_p = seqnum_path(path); + load_seq_nums(seq_num_p, &me->last_seq_num, &me->last_seq_num_processed); me->seq_num_path = seq_num_p; - + // load replay - auto fix_log_p = fix_log_path (path); - auto replay_messages = load_replay (cfg,fix_log_p,me->last_seq_num_processed); + auto fix_log_p = fix_log_path(path); + auto replay_messages = + load_replay(cfg, fix_log_p, me->last_seq_num_processed); uint32_t previous_sn = 0; for (auto &p : replay_messages) { auto sn = p.first; auto &m = p.second; - if (!(m.has_poss_dup_flag_flag () && m.get_poss_dup_flag ())) { - if (previous_sn != 0) - bts_assert (sn == (previous_sn + 1)); + if (!(m.has_poss_dup_flag_flag() && m.get_poss_dup_flag())) { + if (previous_sn != 0) + bts_assert(sn == (previous_sn + 1)); previous_sn = sn; - me->replay_messages.emplace_back (m); + me->replay_messages.emplace_back(m); } } - me->fix_store.open (fix_log_p,std::ofstream::app); - me->writer_thread = std::thread (std::bind (&FIXFileStore::write,this)); + me->fix_store.open(fix_log_p, std::ofstream::app); + me->writer_thread = std::thread(std::bind(&FIXFileStore::write, this)); } } -FIXFileStore::~FIXFileStore () -{ +FIXFileStore::~FIXFileStore() { if (me->active) { Event ev; ev.quit = true; - me->event_queue.enqueue (ev); - me->writer_thread.join (); + me->event_queue.push(ev); + me->writer_thread.join(); } } -bool FIXFileStore::session_active () const -{return me->active;} +bool FIXFileStore::session_active() const { return me->active; } -std::vector FIXFileStore::replay () const -{ - bts_assert (me->active && !me->store_called); +std::vector FIXFileStore::replay() const { + bts_assert(me->active && !me->store_called); return me->replay_messages; } -uint32_t FIXFileStore::last_seq_num () const -{ - bts_assert (me->active && !me->store_called); +uint32_t FIXFileStore::last_seq_num() const { + bts_assert(me->active && !me->store_called); return me->last_seq_num; } - -uint32_t FIXFileStore::last_seq_num_processed () const -{ - bts_assert (me->active && !me->store_called); + +uint32_t FIXFileStore::last_seq_num_processed() const { + bts_assert(me->active && !me->store_called); return me->last_seq_num_processed; } -void FIXFileStore::store (OutFIXPtr const&msgs) -{ - bts_assert (me->active); +void FIXFileStore::store(OutFIXPtr const &msgs) { + bts_assert(me->active); me->store_called = true; Event ev; ev.out_msgs = msgs; - me->event_queue.enqueue (ev); + me->event_queue.push(ev); } -void FIXFileStore::store (InMsgBuffer const&msg) -{ - bts_assert (me->active); +void FIXFileStore::store(InMsgBuffer const &msg) { + bts_assert(me->active); me->store_called = true; Event ev; ev.in_msg = msg; - me->event_queue.enqueue (ev); + me->event_queue.push(ev); } -void FIXFileStore::set_last_msg_processed (uint32_t last_msg_processed) -{ - bts_assert (me->active); +void FIXFileStore::set_last_msg_processed(uint32_t last_msg_processed) { + bts_assert(me->active); me->store_called = true; Event ev; ev.has_last_msg_processed = true; ev.last_msg_processed = last_msg_processed; - me->event_queue.enqueue (ev); + me->event_queue.push(ev); } -void FIXFileStore::set_last_seq_num(uint32_t last_seq_num) -{ - bts_assert (me->active); +void FIXFileStore::set_last_seq_num(uint32_t last_seq_num) { + bts_assert(me->active); me->store_called = true; Event ev; ev.has_seq_num = true; ev.seq_num = last_seq_num; - me->event_queue.enqueue (ev); + me->event_queue.push(ev); } -void FIXFileStore::write () -{ +void FIXFileStore::write() { Event ev; - os::set_thread_name(fmt::format("{0}_store",me->cfg.name()).c_str()); + os::set_thread_name(fmt::format("{0}_store", me->cfg.name()).c_str()); while (true) - if (me->event_queue.try_dequeue (ev)) { + if (me->event_queue.try_pop(ev)) { if (ev.quit) { return; - } - else if (ev.out_msgs) { + } else if (ev.out_msgs) { for (auto &encoded_stream : ev.out_msgs->fix) - for (auto &mm : encoded_stream.msg_map ()) { - me->last_seq_num = std::max (me->last_seq_num,mm.seq_num); - save_seq_nums (me->seq_num_path,me->last_seq_num,me->last_seq_num_processed); - save_fix (me->fix_store,encoded_stream.data ()+mm.offset,mm.size,true); + for (auto &mm : encoded_stream.msg_map()) { + me->last_seq_num = std::max(me->last_seq_num, mm.seq_num); + save_seq_nums(me->seq_num_path, me->last_seq_num, + me->last_seq_num_processed); + save_fix(me->fix_store, encoded_stream.data() + mm.offset, + mm.size, true); } - me->fix_store.flush (); - } - else if (ev.in_msg.size ()) - { - save_fix (me->fix_store,ev.in_msg.data (),ev.in_msg.size (),false); - me->fix_store.flush (); - } - else if (ev.has_last_msg_processed) { + me->fix_store.flush(); + } else if (ev.in_msg.size()) { + save_fix(me->fix_store, ev.in_msg.data(), ev.in_msg.size(), false); + me->fix_store.flush(); + } else if (ev.has_last_msg_processed) { me->last_seq_num_processed = ev.last_msg_processed; - save_seq_nums (me->seq_num_path,me->last_seq_num,me->last_seq_num_processed); - } - else if (ev.has_seq_num) { + save_seq_nums(me->seq_num_path, me->last_seq_num, + me->last_seq_num_processed); + } else if (ev.has_seq_num) { me->last_seq_num = ev.seq_num; - save_seq_nums (me->seq_num_path,me->last_seq_num,me->last_seq_num_processed); - } - else - bts_assert (false); - } - else - std::this_thread::sleep_for (100ms); + save_seq_nums(me->seq_num_path, me->last_seq_num, + me->last_seq_num_processed); + } else + bts_assert(false); + } else + std::this_thread::sleep_for(100ms); } -}} - +} // namespace jetfix +} // namespace bts diff --git a/src/lib/mercury/broker/AsyncEventBroker.cpp b/src/lib/mercury/broker/AsyncEventBroker.cpp index 139b1df66..9aa918e59 100644 --- a/src/lib/mercury/broker/AsyncEventBroker.cpp +++ b/src/lib/mercury/broker/AsyncEventBroker.cpp @@ -3,5 +3,7 @@ namespace bts { namespace mercury { +std::atomic AsyncEventBroker::_msg_id; + }} diff --git a/src/lib/mercury/broker/AsyncEventBroker.h b/src/lib/mercury/broker/AsyncEventBroker.h index 5a352d8aa..97ec3debc 100644 --- a/src/lib/mercury/broker/AsyncEventBroker.h +++ b/src/lib/mercury/broker/AsyncEventBroker.h @@ -4,8 +4,8 @@ #include #include #include -#include #include +#include #include "lib/mercury/broker/CoreEventBroker.h" #include "lib/mercury/mercury_utils/FatWrapper.h" @@ -14,24 +14,26 @@ namespace bts { namespace mercury { class AsyncEventBroker : public IEventBroker { -private: + private: struct BrokerMsg { + uint64_t msg_id; + EventBrokerID broker_src_id; utils::FatWrapper<127 - 8 - 4> wrapper; void (*handle_msg)(AsyncEventBroker *self, BrokerMsg *m); bool is_external; }; -public: + public: AsyncEventBroker() - : _core_broker(), _queue(), _internal_pool(), _external_pool(), - _external_pool_mtx(), _current_suid(0),_dequeued(10000) {} + : _core_broker(), _queue(5000), _internal_pool(), _external_pool(), + _external_pool_mtx(), _current_suid(0) {} - void init_neighbours(std::initializer_list const &neighbours) - { + void + init_neighbours(std::initializer_list const &neighbours) { for (auto const &n : neighbours) _core_broker.add_neighbour(n); } - + // ieventbroker methods, can be called from multiple threads EventBrokerID id() const override { return _core_broker.id(); } @@ -59,13 +61,13 @@ public: // can be called from outside void post_action(std::function const &action) { - post_external_msg(on_action, action); + post_external_msg(on_action, EventBrokerID(0), action); } // Internal API, i.e. API intended for use by a single thread, the // same thread calling do_work. struct InternalNotificationMsg { - public: + public: template T &wrap_in_place(Args &&... args) { auto &n = _msg->wrapper.unwrap(); return n.payload.wrap_in_place(std::forward(args)...); @@ -82,6 +84,7 @@ public: } void internal_notify(InternalNotificationMsg msg) { + msg._msg->broker_src_id = id(); enqueue(msg._msg); } @@ -109,37 +112,41 @@ public: } bool do_work() { - auto deq = _queue.try_dequeue_bulk(&_dequeued[0],_dequeued.size()); - if (deq) { - LOG(Mercury,Diag,"{} dequeing {} messages ({} in queue)", id().value(), deq, _queue.size_approx()); - for (size_t i = 0; i < deq;i++) { - auto m = _dequeued[i]; + BrokerMsg *m; + auto count = 0; + for(;;) { + m = nullptr; + _queue.pop(m); + if (m) { + LOG(Mercury, Diag, "{} dequeued msg {}:{} ", id().value(), m->broker_src_id.value(), m->msg_id); m->handle_msg(this, m); free_msg(m); + count ++; } - if (deq == _dequeued.size()) - _dequeued.resize(_dequeued.size()*2); - return true; - } else - return false; + else + break; + } + return count; } -private: + private: void add_neighbour(IEventBrokerPtr const &n) override { - post_external_msg(on_neighbour_addition, n); + post_external_msg(on_neighbour_addition, + EventBrokerID(0), n); } void remove_neighbour(EventBrokerID n) override { - post_external_msg(on_neighbour_removal, n); + post_external_msg(on_neighbour_removal, + EventBrokerID(0), n); } using MsgHandler = void(AsyncEventBroker *, BrokerMsg *); using Pool = boost::object_pool; - using Queue = moodycamel::ConcurrentQueue; + using Queue = boost::lockfree::queue; using DispatchMap = - boost::container::flat_map>; + boost::container::flat_map>; CoreEventBroker _core_broker; Queue _queue; Pool _internal_pool; @@ -147,8 +154,8 @@ private: std::atomic_flag _external_pool_mtx = ATOMIC_FLAG_INIT; DispatchMap _dispatch_map; InternalSubID _current_suid; - std::vector _dequeued; - + static std::atomic _msg_id; + void free_msg(BrokerMsg *m) { m->wrapper.clear(); if (m->is_external) { @@ -161,46 +168,51 @@ private: } template - void post_external_msg(MsgHandler handler, Args &&... args) { + void post_external_msg(MsgHandler handler, EventBrokerID eid, + Args &&... args) { while (_external_pool_mtx.test_and_set(std::memory_order_acquire)) ; auto msg = _external_pool.construct(); _external_pool_mtx.clear(std::memory_order_release); + msg->broker_src_id = eid; msg->is_external = true; msg->handle_msg = handler; - msg->wrapper.wrap_in_place(std::forward(args)...); + msg->wrapper.wrap_in_place(eid, std::forward(args)...); enqueue(msg); } template void post_internal_msg(MsgHandler handler, Args &&... args) { auto msg = _internal_pool.construct(); + msg->broker_src_id = _core_broker.id(); msg->handle_msg = handler; msg->is_external = false; msg->wrapper.wrap_in_place(std::forward(args)...); enqueue(msg); } - void enqueue (BrokerMsg *msg) - { - LOG(Mercury,Diag,"{} enqueing msg {} ({} in queue)", id().value(), (size_t) msg,_queue.size_approx()+1); - _queue.enqueue(msg); + void enqueue(BrokerMsg *msg) { + auto mid = _msg_id.fetch_add(1); + msg->msg_id = mid; + LOG(Mercury, Diag, "{} enqueing msg {}:{}", id().value(), msg->broker_src_id.value(), mid); + _queue.push(msg); } + struct NeighbourAddition { - NeighbourAddition(const IEventBrokerPtr &src) : ptr(src) {} + NeighbourAddition(EventBrokerID, const IEventBrokerPtr &src) : ptr(src) {} IEventBrokerPtr ptr; }; struct NeighbourRemoval { - NeighbourRemoval(EventBrokerID id) : id(id) {} + NeighbourRemoval(EventBrokerID, EventBrokerID id) : id(id) {} EventBrokerID id; }; struct Advertisement { Advertisement(EventBrokerID id, StreamID sid, TopicPtr topic) - : id(id), sid(sid), topic(topic) {} + : id(id), sid(sid), topic(topic) {} EventBrokerID id; StreamID sid; TopicPtr topic; @@ -214,7 +226,7 @@ private: struct Subscription { Subscription(EventBrokerID id, SubID suid, StreamID sid) - : id(id), suid(suid), sid(sid) {} + : id(id), suid(suid), sid(sid) {} EventBrokerID id; SubID suid; StreamID sid; @@ -222,7 +234,7 @@ private: struct Unsubscription { Unsubscription(EventBrokerID id, SubID suid, StreamID sid) - : id(id), suid(suid), sid(sid) {} + : id(id), suid(suid), sid(sid) {} EventBrokerID id; SubID suid; StreamID sid; @@ -231,7 +243,7 @@ private: struct Notification { Notification(EventBrokerID id, StreamID sid, SubID suid, EventPayload const &payload) - : id(id), sid(sid), suid(suid), payload(payload) {} + : id(id), sid(sid), suid(suid), payload(payload) {} EventBrokerID id; StreamID sid; SubID suid; @@ -247,7 +259,7 @@ private: struct InternalSubscription { InternalSubscription(InternalSubID suid, TopicFilter const &filter, InternalEventHandler const &handler) - : suid(suid), filter(filter), handler(handler) {} + : suid(suid), filter(filter), handler(handler) {} InternalSubID suid; TopicFilter filter; InternalEventHandler handler; @@ -259,18 +271,19 @@ private: }; struct Action { - Action(std::function const &action) : action(action) {} + Action(EventBrokerID, std::function const &action) + : action(action) {} std::function action; }; static void on_neighbour_addition(AsyncEventBroker *self, BrokerMsg *m) { self->_core_broker.add_neighbour( - m->wrapper.unwrap().ptr); + m->wrapper.unwrap().ptr); } static void on_neighbour_removal(AsyncEventBroker *self, BrokerMsg *m) { self->_core_broker.remove_neighbour( - m->wrapper.unwrap().id); + m->wrapper.unwrap().id); } static void on_advertisement(AsyncEventBroker *self, BrokerMsg *m) { @@ -322,6 +335,5 @@ private: action.action(); } }; -} -} - +} // namespace mercury +} // namespace bts diff --git a/src/lib/mercury/broker/CoreEventBroker.cpp b/src/lib/mercury/broker/CoreEventBroker.cpp index a38901e31..0b30588a5 100644 --- a/src/lib/mercury/broker/CoreEventBroker.cpp +++ b/src/lib/mercury/broker/CoreEventBroker.cpp @@ -335,6 +335,11 @@ void CoreEventBroker::notify(EventBrokerID sender, StreamID sid, SubID ex_subid, NoReenter nr(me->inside); auto &srec = me->streams[sid]; + if (!srec) + LOG(Mercury, Diag, + "Will crash CoreEventBroker {} received notify from broker {} sid: {} sub_id:{}", + id().value(), sender.value(), sid.value(), ex_subid.value()); + bts_assert(srec); bts_assert((srec->is_local() && sender == id()) || (!srec->is_local() && srec->src->id() == sender)); diff --git a/src/server/sparkd/cmds/Benchmark.cpp b/src/server/sparkd/cmds/Benchmark.cpp index 4d2f1c0e7..74f398b89 100644 --- a/src/server/sparkd/cmds/Benchmark.cpp +++ b/src/server/sparkd/cmds/Benchmark.cpp @@ -22,6 +22,7 @@ #include #include #include +#include namespace bts { namespace cmd { @@ -451,6 +452,18 @@ static void boost_lockless_queue(benchmark::State &state) { } BENCHMARK(boost_lockless_queue); +static void tbb_concurrent_queue(benchmark::State &state) { + + using FreeList = tbb::concurrent_queue; + FreeList queue; + for (auto _ : state) { + void *result; + queue.push(nullptr); + benchmark::DoNotOptimize(queue.try_pop(result)); + } +} +BENCHMARK(tbb_concurrent_queue); + static void moody_camel_queue(benchmark::State &state) { using Q = moodycamel::ConcurrentQueue; Q queue(1000000);