// The rule is that a module releases its internal locks before it
// upcalls, but can keep its locks when calling down.
// The rule is that a module releases its internal locks before it
// upcalls, but can keep its locks when calling down.
-rsm::rsm(std::string _first, std::string _me) :
- stf(0), primary(_first), insync (false), inviewchange (true), vid_commit(0),
- partitioned (false), dopartition(false), break1(false), break2(false)
-{
- last_myvs.vid = 0;
- last_myvs.seqno = 0;
- myvs = last_myvs;
- myvs.seqno = 1;
+rsm_state_transfer::~rsm_state_transfer() {}
- cfg = new config(_first, _me, this);
+rsm::rsm(const string & _first, const string & _me) : primary(_first)
+{
+ cfg = unique_ptr<config>(new config(_first, _me, this));
- rsmrpc->reg(rsm_client_protocol::invoke, this, &rsm::client_invoke);
- rsmrpc->reg(rsm_client_protocol::members, this, &rsm::client_members);
- rsmrpc->reg(rsm_protocol::invoke, this, &rsm::invoke);
- rsmrpc->reg(rsm_protocol::transferreq, this, &rsm::transferreq);
- rsmrpc->reg(rsm_protocol::transferdonereq, this, &rsm::transferdonereq);
- rsmrpc->reg(rsm_protocol::joinreq, this, &rsm::joinreq);
+ rsmrpc->reg(rsm_client_protocol::invoke, &rsm::client_invoke, this);
+ rsmrpc->reg(rsm_client_protocol::members, &rsm::client_members, this);
+ rsmrpc->reg(rsm_protocol::invoke, &rsm::invoke, this);
+ rsmrpc->reg(rsm_protocol::transferreq, &rsm::transferreq, this);
+ rsmrpc->reg(rsm_protocol::transferdonereq, &rsm::transferdonereq, this);
+ rsmrpc->reg(rsm_protocol::joinreq, &rsm::joinreq, this);
- testsvr = new rpcs(atoi(_me.c_str()) + 1);
- testsvr->reg(rsm_test_protocol::net_repair, this, &rsm::test_net_repairreq);
- testsvr->reg(rsm_test_protocol::breakpoint, this, &rsm::breakpointreq);
-
- {
- lock ml(rsm_mutex);
- std::thread(&rsm::recovery, this).detach();
- }
+ testsvr.reset(new rpcs((in_port_t)stoi(_me) + 1));
+ testsvr->reg(rsm_test_protocol::net_repair, &rsm::test_net_repairreq, this);
+ testsvr->reg(rsm_test_protocol::breakpoint, &rsm::breakpointreq, this);
while (!cfg->ismember(cfg->myaddr(), vid_commit)) {
// XXX iannucci 2013/09/15 -- I don't understand whether accessing
// cfg->view_id in this manner involves a race. I suspect not.
while (!cfg->ismember(cfg->myaddr(), vid_commit)) {
// XXX iannucci 2013/09/15 -- I don't understand whether accessing
// cfg->view_id in this manner involves a race. I suspect not.
- if (join(primary)) {
- tprintf("recovery: joined\n");
- commit_change_wo(cfg->view_id());
+ if (join(primary, ml)) {
+ LOG << "joined";
+ commit_change(cfg->view_id(), ml);
-template <class A>
-std::ostream & operator<<(std::ostream &o, const std::vector<A> &d) {
- o << "[";
- for (typename std::vector<A>::const_iterator i=d.begin(); i!=d.end(); i++) {
- o << *i;
- if (i+1 != d.end())
- o << ", ";
- }
- o << "]";
- return o;
-}
-
-bool rsm::sync_with_backups() {
- adopt_lock ml(rsm_mutex);
- ml.unlock();
+bool rsm::sync_with_backups(lock & rsm_mutex_lock) {
+ rsm_mutex_lock.unlock();
// synchronization; otherwise, the primary's state may be more recent
// than replicas after the synchronization.
// synchronization; otherwise, the primary's state may be more recent
// than replicas after the synchronization.
// replicas are synchronized. The reason is that client_invoke arrives
// after this point of time will see inviewchange == true, and returns
// BUSY.
}
// replicas are synchronized. The reason is that client_invoke arrives
// after this point of time will see inviewchange == true, and returns
// BUSY.
}
// Start accepting synchronization request (statetransferreq) now!
insync = true;
cfg->get_view(vid_insync, backups);
backups.erase(find(backups.begin(), backups.end(), cfg->myaddr()));
// Start accepting synchronization request (statetransferreq) now!
insync = true;
cfg->get_view(vid_insync, backups);
backups.erase(find(backups.begin(), backups.end(), cfg->myaddr()));
-/**
- * Call to transfer state from m to the local node.
- * Assumes that rsm_mutex is already held.
- */
-bool rsm::statetransfer(std::string m)
+//
+// Call to transfer state from m to the local node.
+// Assumes that rsm_mutex is already held.
+//
+bool rsm::statetransfer(const string & m, lock & rsm_mutex_lock)
- tprintf("rsm::statetransfer: contact %s w. my last_myvs(%d,%d)\n",
- m.c_str(), last_myvs.vid, last_myvs.seqno);
+ LOG << "contact " << m << " w. my last_myvs(" << last_myvs.vid << "," << last_myvs.seqno << ")";
r, cfg->myaddr(), last_myvs, vid_insync);
}
r, cfg->myaddr(), last_myvs, vid_insync);
}
- tprintf("rsm::statetransfer: couldn't reach %s %lx %d\n", m.c_str(),
- (long unsigned) cl, ret);
+ LOG << "couldn't reach " << m << " " << hex << cl << " " << dec << ret;
- tprintf("rsm::statetransfer transfer from %s success, vs(%d,%d)\n",
- m.c_str(), last_myvs.vid, last_myvs.seqno);
+ LOG << "transfer from " << m << " success, vs(" << last_myvs.vid << "," << last_myvs.seqno << ")";
-bool rsm::statetransferdone(std::string m) {
- adopt_lock ml(rsm_mutex);
- ml.unlock();
+bool rsm::statetransferdone(const string & m, lock & rsm_mutex_lock) {
+ rsm_mutex_lock.unlock();
- rsm_protocol::status ret = cl->call(rsm_protocol::transferdonereq, r, cfg->myaddr(), vid_insync);
+ auto ret = (rsm_protocol::status)cl->call(rsm_protocol::transferdonereq, r, cfg->myaddr(), vid_insync);
- tprintf("rsm::join: %s mylast (%d,%d)\n", m.c_str(), last_myvs.vid,
- last_myvs.seqno);
+ LOG << "contacting " << m << " mylast (" << last_myvs.vid << "," << last_myvs.seqno << ")";
- ret = cl->call_timeout(rsm_protocol::joinreq, rpcc::to(120000), r,
+ ret = cl->call_timeout(rsm_protocol::joinreq, milliseconds(12000), log,
- tprintf("commit_change: new view (%d) last vs (%d,%d) %s insync %d\n",
- vid, last_myvs.vid, last_myvs.seqno, primary.c_str(), insync);
+ LOG << "new view (" << vid << ") last vs (" << last_myvs.vid << ","
+ << last_myvs.seqno << ") " << primary << " insync " << insync;
vid_commit = vid;
inviewchange = true;
set_primary(vid);
recovery_cond.notify_one();
sync_cond.notify_one();
if (cfg->ismember(cfg->myaddr(), vid_commit))
vid_commit = vid;
inviewchange = true;
set_primary(vid);
recovery_cond.notify_one();
sync_cond.notify_one();
if (cfg->ismember(cfg->myaddr(), vid_commit))
- std::string reps;
- rsm_protocol::status ret = h->fn(args, rep);
- marshall rep1;
- rep1 << ret;
- rep1 << rep.str();
- r = rep1.str();
+ auto ret = (rsm_protocol::status)(*h)(unmarshall(req, false), rep);
+ r = marshall(ret, rep.content()).content();
-rsm_client_protocol::status rsm::client_invoke(int procno, std::string req, std::string &r) {
- LOG("rsm::client_invoke: procno 0x" << std::hex << procno);
+rsm_client_protocol::status rsm::client_invoke(string & r, rpc_protocol::proc_id_t procno, const string & req) {
+ LOG << "invoke procno 0x" << hex << procno;
cfg->get_view(vid_commit, m);
// assign the RPC the next viewstamp number
vs = myvs;
cfg->get_view(vid_commit, m);
// assign the RPC the next viewstamp number
vs = myvs;
for (unsigned i = 0; i < m.size(); i++) {
if (m[i] != myaddr) {
// if invoke on slave fails, return rsm_client_protocol::BUSY
handle h(m[i]);
for (unsigned i = 0; i < m.size(); i++) {
if (m[i] != myaddr) {
// if invoke on slave fails, return rsm_client_protocol::BUSY
handle h(m[i]);
- rsm_protocol::status ret;
- int r;
- ret = cl->call_timeout(rsm_protocol::invoke, rpcc::to(1000), r, procno, vs, req);
- LOG("Invoke returned " << ret);
+ int ignored_rval;
+ auto ret = (rsm_protocol::status)cl->call_timeout(rsm_protocol::invoke, milliseconds(100), ignored_rval, procno, vs, req);
+ LOG << "Invoke returned " << ret;
-rsm_protocol::status rsm::invoke(int proc, viewstamp vs, std::string req, int &dummy) {
- LOG("rsm::invoke: procno 0x" << std::hex << proc);
+rsm_protocol::status rsm::invoke(int &, rpc_protocol::proc_id_t proc, viewstamp vs, const string & req) {
+ LOG << "invoke procno 0x" << hex << proc;
if (find(m.begin(), m.end(), myaddr) == m.end())
return rsm_protocol::ERR;
// check sequence number
if (find(m.begin(), m.end(), myaddr) == m.end())
return rsm_protocol::ERR;
// check sequence number
-/**
- * RPC handler: Send back the local node's state to the caller
- */
-rsm_protocol::status rsm::transferreq(std::string src, viewstamp last, unsigned vid,
- rsm_protocol::transferres &r) {
+//
+// RPC handler: Send back the local node's state to the caller
+//
+rsm_protocol::status rsm::transferreq(rsm_protocol::transferres & r, const string & src,
+ viewstamp last, unsigned vid) {
- int ret = rsm_protocol::OK;
- tprintf("transferreq from %s (%d,%d) vs (%d,%d)\n", src.c_str(),
- last.vid, last.seqno, last_myvs.vid, last_myvs.seqno);
- if (!insync || vid != vid_insync) {
+ LOG << "transferreq from " << src << " (" << last.vid << "," << last.seqno << ") vs ("
+ << last_myvs.vid << "," << last_myvs.seqno << ")";
+ if (!insync || vid != vid_insync)
-/**
- * RPC handler: Inform the local node (the primary) that node m has synchronized
- * for view vid
- */
-rsm_protocol::status rsm::transferdonereq(std::string m, unsigned vid, int &) {
+//
+// RPC handler: Inform the local node (the primary) that node m has synchronized
+// for view vid
+//
+rsm_protocol::status rsm::transferdonereq(int &, const string & m, unsigned vid) {
// a node that wants to join an RSM as a server sends a
// joinreq to the RSM's current primary; this is the
// handler for that RPC.
// a node that wants to join an RSM as a server sends a
// joinreq to the RSM's current primary; this is the
// handler for that RPC.
-rsm_protocol::status rsm::joinreq(std::string m, viewstamp last, rsm_protocol::joinres &r) {
- int ret = rsm_protocol::OK;
+rsm_protocol::status rsm::joinreq(string & log, const string & m, viewstamp last) {
+ auto ret = rsm_protocol::OK;
- tprintf("joinreq: src %s last (%d,%d) mylast (%d,%d)\n", m.c_str(),
- last.vid, last.seqno, last_myvs.vid, last_myvs.seqno);
+ LOG << "join request from " << m << "; last=(" << last.vid << "," << last.seqno << "), mylast=("
+ << last_myvs.vid << "," << last_myvs.seqno << ")";
ret = rsm_protocol::BUSY;
} else {
// We cache vid_commit to avoid adding m to a view which already contains
// m due to race condition
ret = rsm_protocol::BUSY;
} else {
// We cache vid_commit to avoid adding m to a view which already contains
// m due to race condition
-/*
- * RPC handler: Send back all the nodes this local knows about to client
- * so the client can switch to a different primary
- * when it existing primary fails
- */
-rsm_client_protocol::status rsm::client_members(int i, std::vector<std::string> &r) {
- std::vector<std::string> m;
+//
+// RPC handler: Responds with the list of known nodes for fall-back on a
+// primary failure
+//
+rsm_client_protocol::status rsm::client_members(vector<string> & r, int) {
+ vector<string> m;
// otherwise, the lowest number node of the previous view.
// caller should hold rsm_mutex
void rsm::set_primary(unsigned vid) {
// otherwise, the lowest number node of the previous view.
// caller should hold rsm_mutex
void rsm::set_primary(unsigned vid) {
cfg->get_view(vid, c);
cfg->get_view(vid - 1, p);
VERIFY (c.size() > 0);
if (isamember(primary,c)) {
cfg->get_view(vid, c);
cfg->get_view(vid - 1, p);
VERIFY (c.size() > 0);
if (isamember(primary,c)) {
for (unsigned i = 0; i < p.size(); i++) {
if (isamember(p[i], c)) {
primary = p[i];
for (unsigned i = 0; i < p.size(); i++) {
if (isamember(p[i], c)) {
primary = p[i];
-// assumes caller holds rsm_mutex
-void rsm::net_repair_wo(bool heal) {
- std::vector<std::string> m;
+void rsm::net_repair(bool heal, lock & rsm_mutex_lock) {
+ VERIFY(rsm_mutex_lock);
+ vector<string> m;
cfg->get_view(vid_commit, m);
for (unsigned i = 0; i < m.size(); i++) {
if (m[i] != cfg->myaddr()) {
handle h(m[i]);
cfg->get_view(vid_commit, m);
for (unsigned i = 0; i < m.size(); i++) {
if (m[i] != cfg->myaddr()) {
handle h(m[i]);
- tprintf("rsm::test_net_repairreq: %d (dopartition %d, partitioned %d)\n",
- heal, dopartition, partitioned);
- if (heal) {
- net_repair_wo(heal);
- partitioned = false;
- } else {
+ LOG << "heal " << heal << " (dopartition "
+ << dopartition << ", partitioned " << partitioned << ")";
+ if (heal)
+ net_repair(heal, ml);
+ else
-void rsm::breakpoint1() {
- if (break1) {
- tprintf("Dying at breakpoint 1 in rsm!\n");
- exit(1);
- }
-}
-
-void rsm::breakpoint2() {
- if (break2) {
- tprintf("Dying at breakpoint 2 in rsm!\n");
+void rsm::breakpoint(int b) {
+ if (breakpoints[b-1]) {
+ LOG << "Dying at breakpoint " << b << " in rsm!";
- tprintf("rsm::breakpointreq: %d\n", b);
- if (b == 1) break1 = true;
- else if (b == 2) break2 = true;
+ LOG << "breakpoint " << b;
+ if (b == 1) breakpoints[1-1] = true;
+ else if (b == 2) breakpoints[2-1] = true;