476 lines
15 KiB
C++
476 lines
15 KiB
C++
#include "lib/mercury/broker/Broker.h"
|
|
|
|
#include "lib/mercury/synopsis/ISynopsisBuilder.h"
|
|
#include "lib/mercury/topics/ITopic.h"
|
|
#include "lib/utils/Puid.h"
|
|
#include "lib/utils/bts_assert.h"
|
|
|
|
#include <algorithm>
|
|
#include <boost/container/flat_map.hpp>
|
|
|
|
namespace bts {
|
|
namespace mercury {
|
|
|
|
template <class K, class V> using Map = boost::container::flat_map<K, V>;
|
|
|
|
struct Broker::NoReenter {
|
|
NoReenter(bool &inside) : inside(inside) {
|
|
bts_assert(!inside);
|
|
inside = true;
|
|
}
|
|
~NoReenter() { inside = false; }
|
|
bool &inside;
|
|
};
|
|
|
|
struct Broker::LocalSubFilter {
|
|
TopicFilter matches;
|
|
LocalEventHandler handler;
|
|
};
|
|
|
|
struct Broker::StreamRec {
|
|
StreamRec(Broker &owner, IRemoteBroker &src, StreamID sid,
|
|
TopicPtr const &topic)
|
|
: owner(owner), local(false), src(&src), sid(sid), topic(topic),
|
|
local_subs(), external_subs(), sub_id(),
|
|
synopsis_builder(topic->make_synopsis_builder()),
|
|
topic_tag(topic->to_str()) {}
|
|
|
|
StreamRec(Broker &owner, StreamID sid, TopicPtr const &topic)
|
|
: owner(owner), local(true), src(), sid(sid), topic(topic), local_subs(),
|
|
external_subs(), sub_id(),
|
|
synopsis_builder(topic->make_synopsis_builder()),
|
|
topic_tag(topic->to_str()) {}
|
|
|
|
~StreamRec() {
|
|
for (auto &isub : local_subs)
|
|
isub.on_event(sid, EventType::END_STREAM, nullptr);
|
|
}
|
|
|
|
struct LocalSubRec {
|
|
LocalSubID sub_id;
|
|
LocalEventHandler on_event;
|
|
};
|
|
|
|
struct SubRec {
|
|
SubID sub_id;
|
|
IRemoteBroker *neighbour;
|
|
};
|
|
|
|
Broker &owner;
|
|
const bool local;
|
|
IRemoteBroker *const src;
|
|
const StreamID sid;
|
|
const TopicPtr topic;
|
|
|
|
std::vector<LocalSubRec> local_subs;
|
|
std::vector<SubRec> external_subs;
|
|
|
|
SubID sub_id;
|
|
SynopsisBuilderPtr synopsis_builder;
|
|
const std::string topic_tag;
|
|
|
|
bool is_local() const { return local; }
|
|
bool is_active() const {
|
|
return is_local() || local_subs.size() || external_subs.size();
|
|
}
|
|
Notification *get_synopsis() { return synopsis_builder->get_synopsis(); }
|
|
|
|
void add_external_sub(IRemoteBroker *const &node, SubID subid) {
|
|
auto sender = node->id();
|
|
bts_assert(std::find_if(external_subs.begin(), external_subs.end(),
|
|
[sender](auto const &sub_rec) {
|
|
return sub_rec.neighbour->id() == sender;
|
|
}) == external_subs.end());
|
|
|
|
// record wheter we are already active for this stream
|
|
auto was_active = is_active();
|
|
|
|
// add subscriber to collection
|
|
external_subs.push_back(SubRec{subid, node});
|
|
|
|
// trigger a subscription if appropriate
|
|
if (!was_active)
|
|
forward_sub();
|
|
else {
|
|
// we were already active so if we have a synopsis forward it
|
|
auto synopsis = synopsis_builder->get_synopsis();
|
|
if (synopsis) {
|
|
LOG(Mercury, Info,
|
|
"Broker {} sending synopsis for sid: "
|
|
"{} to broker {} (sub_id:{} topic: {})",
|
|
owner.id().value(), sid.value(), sender.value(), subid.value(),
|
|
topic_tag);
|
|
node->notify(owner.id(), sid, subid, *synopsis);
|
|
} else
|
|
LOG(Mercury, Info,
|
|
"Broker {} does not have synopsis for sid: "
|
|
"{} for broker {} (sub_id:{} topic:{})",
|
|
owner.id().value(), sid.value(), sender.value(), subid.value(),
|
|
topic_tag);
|
|
}
|
|
}
|
|
|
|
void remove_external_sub(BrokerID sender, SubID subid) {
|
|
auto it = std::find_if(external_subs.begin(), external_subs.end(),
|
|
[sender](auto const &sub_rec) {
|
|
return sub_rec.neighbour->id() == sender;
|
|
});
|
|
bts_assert(it != external_subs.end());
|
|
bts_assert(it->sub_id == subid);
|
|
external_subs.erase(it);
|
|
if (!is_active())
|
|
forward_unsub();
|
|
}
|
|
|
|
void add_local_sub(LocalEventHandler const &handler, LocalSubID sub_id) {
|
|
auto it =
|
|
std::find_if(local_subs.begin(), local_subs.end(),
|
|
[sub_id](auto &sub) { return sub.sub_id == sub_id; });
|
|
bts_assert(it == local_subs.end());
|
|
auto was_active = is_active();
|
|
local_subs.push_back(LocalSubRec{sub_id, handler});
|
|
handler(sid, EventType::BEGIN_STREAM, get_synopsis());
|
|
if (!was_active)
|
|
forward_sub();
|
|
}
|
|
|
|
void remove_local_sub(LocalSubID sub_id) {
|
|
auto it =
|
|
std::find_if(local_subs.begin(), local_subs.end(),
|
|
[sub_id](auto &sub) { return sub.sub_id == sub_id; });
|
|
bts_assert(it != local_subs.end());
|
|
it->on_event(sid, EventType::END_SUB, nullptr);
|
|
local_subs.erase(it);
|
|
if (!is_active())
|
|
forward_unsub();
|
|
}
|
|
|
|
void notify(Notification const &payload) {
|
|
// notify local subs
|
|
for (auto &isub : local_subs)
|
|
isub.on_event(sid, EventType::NEW_EVENT, &payload);
|
|
|
|
// fwd to remote subs
|
|
for (auto &esub : external_subs)
|
|
esub.neighbour->notify(owner.id(), sid, esub.sub_id, payload);
|
|
|
|
// update synopsis
|
|
synopsis_builder->update(payload);
|
|
}
|
|
|
|
void forward_sub() {
|
|
bts_assert(!is_local());
|
|
bts_assert(local_subs.size() || external_subs.size());
|
|
|
|
sub_id = owner.gen_remote_sub_id();
|
|
|
|
LOG(Mercury, Info,
|
|
"Broker {} issuing subscription for topic: {} sid: {} "
|
|
"to broker {} (sub_id:{})",
|
|
owner.id().value(), topic_tag, sid.value(), src->id().value(),
|
|
sub_id.value());
|
|
|
|
src->subscribe(owner.id(), sub_id, sid);
|
|
synopsis_builder->clear_synopsis();
|
|
}
|
|
|
|
void forward_unsub() {
|
|
bts_assert(!is_local());
|
|
bts_assert(local_subs.empty() && external_subs.empty());
|
|
LOG(Mercury, Info,
|
|
"Broker {} canceling subscription for {} "
|
|
"from {} (sub_id: {})",
|
|
owner.id().value(), sid.value(), src->id().value(), sub_id.value());
|
|
src->unsubscribe(owner.id(), sub_id, sid);
|
|
}
|
|
};
|
|
|
|
struct Broker::PImpl {
|
|
PImpl()
|
|
: streams(), neighbours(), local_subs(),
|
|
my_id(utils::gen_id("event_broker")), last_external_sub_id(),
|
|
inside(false) {}
|
|
|
|
Map<StreamID, std::unique_ptr<StreamRec>> streams;
|
|
Map<BrokerID, IRemoteBroker *> neighbours;
|
|
Map<LocalSubID, LocalSubFilter> local_subs;
|
|
BrokerID my_id;
|
|
SubID last_external_sub_id;
|
|
bool inside;
|
|
};
|
|
|
|
Broker::Broker() : me(new PImpl()) {}
|
|
Broker::~Broker() {}
|
|
|
|
BrokerID Broker::id() const { return me->my_id; }
|
|
|
|
void Broker::add_neighbour(IRemoteBroker &neighbour) {
|
|
LOG(Mercury, Info, "Broker {} add_neighbour: {}", me->my_id.value(),
|
|
neighbour.id().value());
|
|
NoReenter nr(me->inside);
|
|
|
|
auto &n = me->neighbours[neighbour.id()];
|
|
bts_assert(!n);
|
|
n = &neighbour;
|
|
|
|
// Since we assume direct connections between all brokers (i.e. no
|
|
// forwarding of notifications is needed), we only need to
|
|
// advertise our own streams to this new neighbour
|
|
for (auto const &s : me->streams)
|
|
if (s.second->is_local())
|
|
n->advertise(me->my_id, s.first, s.second->topic);
|
|
}
|
|
|
|
void Broker::remove_neighbour(BrokerID removed_neighbour_id) {
|
|
LOG(Mercury, Info, "Broker {} remove_neighbour broker {} ", id().value(),
|
|
removed_neighbour_id.value());
|
|
NoReenter nr(me->inside);
|
|
|
|
auto &n_rec = me->neighbours[removed_neighbour_id];
|
|
bts_assert(n_rec);
|
|
|
|
// collect removed neigbhours's streams and subscriptions
|
|
std::vector<StreamID> its_streams;
|
|
std::vector<std::pair<SubID, StreamID>> its_subscriptions;
|
|
for (auto &kvp : me->streams) {
|
|
auto &sid = kvp.first;
|
|
auto &sr = kvp.second;
|
|
|
|
// does the removed neighbour own this table?
|
|
if (sr->src->id() == removed_neighbour_id)
|
|
its_streams.push_back(sid);
|
|
else
|
|
// the removed node does not own this table, but it might be
|
|
// subscribed to it.
|
|
for (auto &s : sr->external_subs)
|
|
if (s.neighbour->id() == removed_neighbour_id) {
|
|
its_subscriptions.push_back(std::make_pair(sr->sub_id, sid));
|
|
break;
|
|
}
|
|
}
|
|
|
|
// unadvertise orphan streams
|
|
for (auto &sid : its_streams) {
|
|
LOG(Mercury, Info,
|
|
"Broker {} sending unadvertisement for stream owned by dead "
|
|
"node: {} sid: {}",
|
|
id().value(), removed_neighbour_id.value(), sid.value());
|
|
_unadvertise(removed_neighbour_id, sid);
|
|
}
|
|
|
|
// cancel the orphan subscriptions
|
|
for (auto &p : its_subscriptions) {
|
|
auto sub_id = p.first;
|
|
auto sid = p.second;
|
|
LOG(Mercury, Info,
|
|
"Broker {} canceling subscription for stream requested by "
|
|
"dead node: {} sid: {} (sub_id:{})",
|
|
id().value(), removed_neighbour_id.value(), sid.value(),
|
|
sub_id.value());
|
|
_unsubscribe(removed_neighbour_id, sub_id, sid);
|
|
}
|
|
|
|
// finally remove the node from our index
|
|
me->neighbours.erase(removed_neighbour_id);
|
|
}
|
|
|
|
void Broker::advertise(BrokerID sender, StreamID sid, TopicPtr const &topic) {
|
|
LOG(Mercury, Info,
|
|
"Broker {} received advertisement from broker {} "
|
|
"sid: {} topic: {}",
|
|
id().value(), sender.value(), sid.value(), topic->to_str());
|
|
NoReenter nr(me->inside);
|
|
|
|
// create an entry for this stream
|
|
auto &srec = me->streams[sid];
|
|
bts_assert(!srec);
|
|
|
|
if (sender != id()) {
|
|
// external advertisement, must come from a known neighbour
|
|
auto const &node = me->neighbours[sender];
|
|
bts_assert(node);
|
|
srec = std::make_unique<StreamRec>(*this, *node, sid, topic);
|
|
} else {
|
|
srec = std::make_unique<StreamRec>(*this, sid, topic);
|
|
bts_assert(srec->is_local());
|
|
// only advertisments for streams we produce will be forwarded
|
|
// to our neighbours (since all nodes are directly
|
|
// interconnected)
|
|
for (auto &kvp : me->neighbours) {
|
|
LOG(Mercury, Info,
|
|
"Broker {} forwarding advertisement for stream {} owned "
|
|
"by broker {} to: {} sid: {} ",
|
|
id().value(), topic->to_str(), sender.value(), kvp.first.value(),
|
|
sid.value());
|
|
kvp.second->advertise(me->my_id, sid, topic);
|
|
}
|
|
}
|
|
|
|
// match to existing local subscriptions
|
|
bool found = false;
|
|
for (auto const &kvp : me->local_subs) {
|
|
auto local_sub_id = kvp.first;
|
|
auto const &f = kvp.second;
|
|
if (f.matches(*topic)) {
|
|
found = true;
|
|
LOG(Mercury, Info, "Broker {} matched local sub {} to topic {} sid {}",
|
|
id().value(), local_sub_id, topic->to_str(), sid.value());
|
|
srec->add_local_sub(f.handler, local_sub_id);
|
|
}
|
|
}
|
|
if (!found)
|
|
LOG(Mercury, Info,
|
|
"Broker {} has no local sub matches to topic {} sid {}", id().value(),
|
|
topic->to_str(), sid.value());
|
|
}
|
|
|
|
void Broker::unadvertise(BrokerID sender, StreamID sid) {
|
|
LOG(Mercury, Info,
|
|
"Broker {} received unadvertisement from broker {} sid: {}",
|
|
id().value(), sender.value(), sid.value());
|
|
NoReenter nr(me->inside);
|
|
_unadvertise(sender, sid);
|
|
}
|
|
|
|
#ifndef NDEBUG
|
|
void Broker::notify(BrokerID sender, StreamID sid, SubID ex_subid,
|
|
Notification const &payload) {
|
|
NoReenter nr(me->inside);
|
|
#else
|
|
void Broker::notify(BrokerID , StreamID sid, SubID ex_subid,
|
|
Notification const &payload) {
|
|
#endif
|
|
auto &srec = me->streams[sid];
|
|
bts_assert_d(srec);
|
|
bts_assert_d((srec->is_local() && sender == id()) ||
|
|
(!srec->is_local() && srec->src->id() == sender));
|
|
if (srec->is_local() || srec->sub_id == ex_subid)
|
|
srec->notify(payload);
|
|
}
|
|
|
|
void Broker::subscribe(BrokerID sender, SubID ex_subid, StreamID sid) {
|
|
LOG(Mercury, Info,
|
|
"Broker {} received subscribe from broker {} "
|
|
"sid: {} sub_id:{}",
|
|
id().value(), sender.value(), sid.value(), ex_subid.value());
|
|
NoReenter nr(me->inside);
|
|
|
|
bts_assert(sender != id());
|
|
auto &node = me->neighbours[sender];
|
|
bts_assert(node);
|
|
|
|
auto it = me->streams.find(sid);
|
|
if (it != me->streams.end())
|
|
it->second->add_external_sub(node, ex_subid);
|
|
else
|
|
LOG(Mercury, Info,
|
|
"Broker {} received external subscribe for "
|
|
"dead stream {}. ignoring",
|
|
id().value(), sid.value());
|
|
}
|
|
|
|
void Broker::unsubscribe(BrokerID sender, SubID ex_subid, StreamID sid) {
|
|
LOG(Mercury, Info,
|
|
"Broker {} received unsubscribe from broker {} "
|
|
"sid: {} sub_id:{}",
|
|
id().value(), sender.value(), sid.value(), ex_subid.value());
|
|
NoReenter nr(me->inside);
|
|
_unsubscribe(sender, ex_subid, sid);
|
|
}
|
|
|
|
void Broker::local_subscribe(LocalSubID in_subid, TopicFilter const &f,
|
|
LocalEventHandler const &cb) {
|
|
LOG(Mercury, Info, "Broker {} received local_subscribe (sub_id: {})",
|
|
id().value(), in_subid.value());
|
|
NoReenter nr(me->inside);
|
|
|
|
// no duplicates
|
|
bts_assert(me->local_subs.find(in_subid) == me->local_subs.end());
|
|
|
|
// add to local sub list
|
|
auto &sf = me->local_subs[in_subid];
|
|
sf.matches = f;
|
|
sf.handler = cb;
|
|
|
|
// cross check new sub with all known streams, and trigger sub/synopsis
|
|
// update if appropriate
|
|
bool found = false;
|
|
for (auto &kvp : me->streams) {
|
|
auto &srec = kvp.second;
|
|
if (sf.matches(*srec->topic)) {
|
|
found = true;
|
|
LOG(Mercury, Info, "Broker {} matched local sub {} to topic {} sid {}",
|
|
id().value(), in_subid, srec->topic->to_str(), kvp.first.value());
|
|
srec->add_local_sub(cb, in_subid);
|
|
}
|
|
}
|
|
if (!found)
|
|
LOG(Mercury, Info, "Broker {} found no topics to match local sub {}",
|
|
id().value(), in_subid.value());
|
|
}
|
|
|
|
void Broker::local_unsubscribe(LocalSubID in_subid) {
|
|
LOG(Mercury, Info, "Broker {} received local_unsubscribe (sub_id: {})",
|
|
id().value(), in_subid.value());
|
|
NoReenter nr(me->inside);
|
|
|
|
// must be a previously entered sub
|
|
auto it = me->local_subs.find(in_subid);
|
|
bts_assert(it != me->local_subs.end());
|
|
auto &matches = it->second.matches;
|
|
|
|
// for each stream that matched this filter remove the corresponding local
|
|
// sub
|
|
for (auto &kvp : me->streams) {
|
|
auto &srec = kvp.second;
|
|
if (matches(*srec->topic))
|
|
srec->remove_local_sub(in_subid);
|
|
}
|
|
}
|
|
|
|
SubID Broker::gen_remote_sub_id() {
|
|
me->last_external_sub_id = SubID(me->last_external_sub_id.value() + 1);
|
|
return me->last_external_sub_id;
|
|
}
|
|
|
|
void Broker::_unadvertise(BrokerID sender, StreamID sid) {
|
|
auto it = me->streams.find(sid);
|
|
bts_assert(it != me->streams.end());
|
|
|
|
auto &srec = it->second;
|
|
|
|
// either the stream is local and therefore the sender is us or the
|
|
// stream is not local and the sender is in srec
|
|
bts_assert((sender == id() && srec->is_local()) ||
|
|
(!srec->is_local() && sender == srec->src->id()));
|
|
|
|
// remove from our index
|
|
me->streams.erase(it);
|
|
|
|
// We only need to send unadvertisements for streams we own, since
|
|
// we assume direct connections between all brokers.
|
|
if (srec->is_local()) {
|
|
auto topic_str = srec->topic->to_str();
|
|
for (auto &kvp : me->neighbours)
|
|
if (kvp.first != sender) {
|
|
LOG(Mercury, Info,
|
|
"Broker {} forwarding unadvertisement for "
|
|
"stream {} owned "
|
|
"by broker {} to: {} sid: {}",
|
|
id().value(), topic_str, sender.value(), kvp.first.value(),
|
|
sid.value());
|
|
kvp.second->unadvertise(me->my_id, sid);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Broker::_unsubscribe(BrokerID sender, SubID sub_id, StreamID sid) {
|
|
auto it = me->streams.find(sid);
|
|
if (it != me->streams.end()) {
|
|
auto &srec = it->second;
|
|
srec->remove_external_sub(sender, sub_id);
|
|
}
|
|
}
|
|
} // namespace mercury
|
|
} // namespace bts
|