X-Git-Url: http://xvm.mit.edu/gitweb/invirt/third/libt4.git/blobdiff_plain/eeab3e6cade87c1fe0a5f3d93522e12ccb9ec2ab..2546a41ad36fdc9ef6471cb35a1d56930ae1b527:/rsm.cc diff --git a/rsm.cc b/rsm.cc index 65f60c7..1321f7e 100644 --- a/rsm.cc +++ b/rsm.cc @@ -86,7 +86,7 @@ #include "handle.h" #include "rsm.h" -#include "tprintf.h" +#include "threaded_log.h" #include "lang/verify.h" #include "rsm_client.h" #include "lock.h" @@ -140,9 +140,9 @@ void rsm::recovery() [[noreturn]] { 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)) { + if (join(primary, ml)) { LOG("recovery: joined"); - commit_change_wo(cfg->view_id()); + commit_change(cfg->view_id(), ml); } else { ml.unlock(); std::this_thread::sleep_for(std::chrono::seconds(30)); // XXX make another node in cfg primary? @@ -152,9 +152,9 @@ void rsm::recovery() [[noreturn]] { vid_insync = vid_commit; LOG("recovery: sync vid_insync " << vid_insync); if (primary == cfg->myaddr()) { - r = sync_with_backups(); + r = sync_with_backups(ml); } else { - r = sync_with_primary(); + r = sync_with_primary(ml); } LOG("recovery: sync done"); @@ -173,52 +173,39 @@ void rsm::recovery() [[noreturn]] { } } -template -std::ostream & operator<<(std::ostream &o, const std::vector &d) { - o << "["; - for (typename std::vector::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(); { // Make sure that the state of lock_server is stable during // synchronization; otherwise, the primary's state may be more recent // than replicas after the synchronization. - lock ml2(invoke_mutex); + lock invoke_mutex_lock(invoke_mutex); // By acquiring and releasing the invoke_mutex once, we make sure that // the state of lock_server will not be changed until all // replicas are synchronized. The reason is that client_invoke arrives // after this point of time will see inviewchange == true, and returns // BUSY. } - ml.lock(); + rsm_mutex_lock.lock(); // Start accepting synchronization request (statetransferreq) now! insync = true; cfg->get_view(vid_insync, backups); backups.erase(find(backups.begin(), backups.end(), cfg->myaddr())); - LOG("rsm::sync_with_backups " << backups); - sync_cond.wait(ml); + LOG("rsm::sync_with_backups " << make_iterator_pair(backups.begin(), backups.end())); + sync_cond.wait(rsm_mutex_lock); insync = false; return true; } -bool rsm::sync_with_primary() { +bool rsm::sync_with_primary(lock & rsm_mutex_lock) { // Remember the primary of vid_insync std::string m = primary; while (vid_insync == vid_commit) { - if (statetransfer(m)) + if (statetransfer(m, rsm_mutex_lock)) break; } - return statetransferdone(m); + return statetransferdone(m, rsm_mutex_lock); } @@ -226,55 +213,50 @@ bool rsm::sync_with_primary() { * Call to transfer state from m to the local node. * Assumes that rsm_mutex is already held. */ -bool rsm::statetransfer(std::string m) +bool rsm::statetransfer(std::string m, lock & rsm_mutex_lock) { rsm_protocol::transferres r; handle h(m); int ret = 0; - tprintf("rsm::statetransfer: contact %s w. my last_myvs(%d,%d)\n", - m.c_str(), last_myvs.vid, last_myvs.seqno); + LOG("rsm::statetransfer: contact " << m << " w. my last_myvs(" << last_myvs.vid << "," << last_myvs.seqno << ")"); rpcc *cl; { - adopt_lock ml(rsm_mutex); - ml.unlock(); + rsm_mutex_lock.unlock(); cl = h.safebind(); if (cl) { ret = cl->call_timeout(rsm_protocol::transferreq, rpcc::to(1000), r, cfg->myaddr(), last_myvs, vid_insync); } - ml.lock(); + rsm_mutex_lock.lock(); } if (cl == 0 || ret != rsm_protocol::OK) { - tprintf("rsm::statetransfer: couldn't reach %s %lx %d\n", m.c_str(), - (long unsigned) cl, ret); + LOG("rsm::statetransfer: couldn't reach " << m << " " << std::hex << cl << " " << std::dec << ret); return false; } if (stf && last_myvs != r.last) { stf->unmarshal_state(r.state); } last_myvs = r.last; - tprintf("rsm::statetransfer transfer from %s success, vs(%d,%d)\n", - m.c_str(), last_myvs.vid, last_myvs.seqno); + LOG("rsm::statetransfer transfer from " << m << " success, vs(" << last_myvs.vid << "," << last_myvs.seqno << ")"); return true; } -bool rsm::statetransferdone(std::string m) { - adopt_lock ml(rsm_mutex); - ml.unlock(); +bool rsm::statetransferdone(std::string m, lock & rsm_mutex_lock) { + rsm_mutex_lock.unlock(); handle h(m); rpcc *cl = h.safebind(); bool done = false; if (cl) { int r; - 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); done = (ret == rsm_protocol::OK); } - ml.lock(); + rsm_mutex_lock.lock(); return done; } -bool rsm::join(std::string m) { +bool rsm::join(std::string m, lock & rsm_mutex_lock) { handle h(m); int ret = 0; rsm_protocol::joinres r; @@ -282,14 +264,13 @@ bool rsm::join(std::string m) { LOG("rsm::join: " << m << " mylast (" << last_myvs.vid << "," << last_myvs.seqno << ")"); rpcc *cl; { - adopt_lock ml(rsm_mutex); - ml.unlock(); + rsm_mutex_lock.unlock(); cl = h.safebind(); if (cl != 0) { ret = cl->call_timeout(rsm_protocol::joinreq, rpcc::to(120000), r, cfg->myaddr(), last_myvs); } - ml.lock(); + rsm_mutex_lock.lock(); } if (cl == 0 || ret != rsm_protocol::OK) { @@ -307,16 +288,16 @@ bool rsm::join(std::string m) { */ void rsm::commit_change(unsigned vid) { lock ml(rsm_mutex); - commit_change_wo(vid); + commit_change(vid, ml); if (cfg->ismember(cfg->myaddr(), vid_commit)) breakpoint2(); } -void rsm::commit_change_wo(unsigned vid) { +void rsm::commit_change(unsigned vid, lock &) { if (vid <= vid_commit) return; - 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("commit_change: new view (" << vid << ") last vs (" << last_myvs.vid << "," << + last_myvs.seqno << ") " << primary << " insync " << insync); vid_commit = vid; inviewchange = true; set_primary(vid); @@ -328,13 +309,13 @@ void rsm::commit_change_wo(unsigned vid) { void rsm::execute(int procno, std::string req, std::string &r) { - tprintf("execute\n"); + LOG("execute"); handler *h = procs[procno]; VERIFY(h); unmarshall args(req); marshall rep; std::string reps; - rsm_protocol::status ret = (*h)(args, rep); + auto ret = (rsm_protocol::status)(*h)(args, rep); marshall rep1; rep1 << ret; rep1 << rep.str(); @@ -379,14 +360,14 @@ rsm_client_protocol::status rsm::client_invoke(std::string &r, int procno, std:: rpcc *cl = h.safebind(); if (!cl) return rsm_client_protocol::BUSY; - rsm_protocol::status ret; int ignored_rval; - ret = cl->call_timeout(rsm_protocol::invoke, rpcc::to(1000), ignored_rval, procno, vs, req); + auto ret = (rsm_protocol::status)cl->call_timeout(rsm_protocol::invoke, rpcc::to(1000), ignored_rval, procno, vs, req); LOG("Invoke returned " << ret); if (ret != rsm_protocol::OK) return rsm_client_protocol::BUSY; breakpoint1(); - partition1(); + lock rsm_mutex_lock(rsm_mutex); + partition1(rsm_mutex_lock); } } execute(procno, req, r); @@ -439,16 +420,14 @@ rsm_protocol::status rsm::invoke(int &, int proc, viewstamp vs, std::string req) rsm_protocol::status rsm::transferreq(rsm_protocol::transferres &r, std::string src, viewstamp last, unsigned vid) { lock ml(rsm_mutex); - 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) return rsm_protocol::BUSY; - } if (stf && last != last_myvs) r.state = stf->marshal_state(); r.last = last_myvs; - return ret; + return rsm_protocol::OK; } /** @@ -469,16 +448,16 @@ rsm_protocol::status rsm::transferdonereq(int &, std::string m, unsigned vid) { // joinreq to the RSM's current primary; this is the // handler for that RPC. rsm_protocol::status rsm::joinreq(rsm_protocol::joinres &r, std::string m, viewstamp last) { - int ret = rsm_protocol::OK; + auto ret = rsm_protocol::OK; lock ml(rsm_mutex); - 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("joinreq: src " << m << " last (" << last.vid << "," << last.seqno << ") mylast (" << + last_myvs.vid << "," << last_myvs.seqno << ")"); if (cfg->ismember(m, vid_commit)) { - tprintf("joinreq: is still a member\n"); + LOG("joinreq: is still a member"); r.log = cfg->dump(); } else if (cfg->myaddr() != primary) { - tprintf("joinreq: busy\n"); + LOG("joinreq: busy"); ret = rsm_protocol::BUSY; } else { // We cache vid_commit to avoid adding m to a view which already contains @@ -492,9 +471,9 @@ rsm_protocol::status rsm::joinreq(rsm_protocol::joinres &r, std::string m, views } if (cfg->ismember(m, cfg->view_id())) { r.log = cfg->dump(); - tprintf("joinreq: ret %d log %s\n:", ret, r.log.c_str()); + LOG("joinreq: ret " << ret << " log " << r.log); } else { - tprintf("joinreq: failed; proposer couldn't add %d\n", succ); + LOG("joinreq: failed; proposer couldn't add " << succ); ret = rsm_protocol::BUSY; } } @@ -512,8 +491,7 @@ rsm_client_protocol::status rsm::client_members(std::vector &r, int cfg->get_view(vid_commit, m); m.push_back(primary); r = m; - tprintf("rsm::client_members return %s m %s\n", print_members(m).c_str(), - primary.c_str()); + LOG("rsm::client_members return " << print_members(m) << " m " << primary); return rsm_client_protocol::OK; } @@ -527,7 +505,7 @@ void rsm::set_primary(unsigned vid) { VERIFY (c.size() > 0); if (isamember(primary,c)) { - tprintf("set_primary: primary stays %s\n", primary.c_str()); + LOG("set_primary: primary stays " << primary); return; } @@ -535,7 +513,7 @@ void rsm::set_primary(unsigned vid) { for (unsigned i = 0; i < p.size(); i++) { if (isamember(p[i], c)) { primary = p[i]; - tprintf("set_primary: primary is %s\n", primary.c_str()); + LOG("set_primary: primary is " << primary); return; } } @@ -553,25 +531,25 @@ bool rsm::amiprimary() { // Simulate partitions // assumes caller holds rsm_mutex -void rsm::net_repair_wo(bool heal) { +void rsm::net_repair(bool heal, lock &) { std::vector m; 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::net_repair_wo: %s %d\n", m[i].c_str(), heal); + LOG("rsm::net_repair: " << m[i] << " " << heal); if (h.safebind()) h.safebind()->set_reachable(heal); } } rsmrpc->set_reachable(heal); } -rsm_test_protocol::status rsm::test_net_repairreq(int &r, int heal) { +rsm_test_protocol::status rsm::test_net_repairreq(rsm_test_protocol::status &r, int heal) { lock ml(rsm_mutex); - tprintf("rsm::test_net_repairreq: %d (dopartition %d, partitioned %d)\n", - heal, dopartition, partitioned); + LOG("rsm::test_net_repairreq: " << heal << " (dopartition " << + dopartition << ", partitioned " << partitioned << ")"); if (heal) { - net_repair_wo(heal); + net_repair(heal, ml); partitioned = false; } else { dopartition = true; @@ -585,30 +563,30 @@ rsm_test_protocol::status rsm::test_net_repairreq(int &r, int heal) { void rsm::breakpoint1() { if (break1) { - tprintf("Dying at breakpoint 1 in rsm!\n"); + LOG("Dying at breakpoint 1 in rsm!"); exit(1); } } void rsm::breakpoint2() { if (break2) { - tprintf("Dying at breakpoint 2 in rsm!\n"); + LOG("Dying at breakpoint 2 in rsm!"); exit(1); } } -void rsm::partition1() { +void rsm::partition1(lock & rsm_mutex_lock) { if (dopartition) { - net_repair_wo(false); + net_repair(false, rsm_mutex_lock); dopartition = false; partitioned = true; } } -rsm_test_protocol::status rsm::breakpointreq(int &r, int b) { +rsm_test_protocol::status rsm::breakpointreq(rsm_test_protocol::status &r, int b) { r = rsm_test_protocol::OK; lock ml(rsm_mutex); - tprintf("rsm::breakpointreq: %d\n", b); + LOG("rsm::breakpointreq: " << b); if (b == 1) break1 = true; else if (b == 2) break2 = true; else if (b == 3 || b == 4) cfg->breakpoint(b);