All random numbers generated via one PRNG seeded in one place.
[invirt/third/libt4.git] / rsm.cc
diff --git a/rsm.cc b/rsm.cc
index c766145..711f0ad 100644 (file)
--- a/rsm.cc
+++ b/rsm.cc
 // upcalls, but can keep its locks when calling down.
 
 #include "rsm.h"
-#include "handle.h"
 #include "rsm_client.h"
 #include <unistd.h>
 
+using std::vector;
+
+rsm_state_transfer::~rsm_state_transfer() {}
+
 rsm::rsm(const string & _first, const string & _me) : primary(_first)
 {
     cfg = unique_ptr<config>(new config(_first, _me, this));
@@ -101,7 +104,7 @@ rsm::rsm(const string & _first, const string & _me) : primary(_first)
     rsmrpc->reg(rsm_protocol::joinreq, &rsm::joinreq, this);
 
     // tester must be on different port, otherwise it may partition itself
-    testsvr.reset(new rpcs((in_port_t)stoi(_me) + 1));
+    testsvr.reset(new rpcs((in_port_t)std::stoi(_me) + 1));
     testsvr->reg(rsm_test_protocol::net_repair, &rsm::test_net_repairreq, this);
     testsvr->reg(rsm_test_protocol::breakpoint, &rsm::breakpointreq, this);
 }
@@ -123,22 +126,22 @@ void rsm::recovery() {
             // 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, ml)) {
-                LOG("joined");
+                LOG << "joined";
                 commit_change(cfg->view_id(), ml);
             } else {
                 ml.unlock();
-                this_thread::sleep_for(seconds(3)); // XXX make another node in cfg primary?
+                std::this_thread::sleep_for(milliseconds(3000)); // XXX make another node in cfg primary?
                 ml.lock();
             }
         }
         vid_insync = vid_commit;
-        LOG("sync vid_insync " << vid_insync);
+        LOG << "sync vid_insync " << vid_insync;
         if (primary == cfg->myaddr()) {
             r = sync_with_backups(ml);
         } else {
             r = sync_with_primary(ml);
         }
-        LOG("sync done");
+        LOG << "sync done";
 
         // If there was a commited viewchange during the synchronization, restart
         // the recovery
@@ -150,7 +153,7 @@ void rsm::recovery() {
             myvs.seqno = 1;
             inviewchange = false;
         }
-        LOG("go to sleep " << insync << " " << inviewchange);
+        LOG << "go to sleep " << insync << " " << inviewchange;
         recovery_cond.wait(ml);
     }
 }
@@ -172,8 +175,8 @@ bool rsm::sync_with_backups(lock & rsm_mutex_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("backups " << backups);
+    backups.erase(std::find(backups.begin(), backups.end(), cfg->myaddr()));
+    LOG << "backups " << backups;
     sync_cond.wait(rsm_mutex_lock);
     insync = false;
     return true;
@@ -198,13 +201,12 @@ bool rsm::sync_with_primary(lock & rsm_mutex_lock) {
 bool rsm::statetransfer(const string & m, lock & rsm_mutex_lock)
 {
     rsm_protocol::transferres r;
-    handle h(m);
     int ret = 0;
-    LOG("contact " << m << " w. my last_myvs(" << last_myvs.vid << "," << last_myvs.seqno << ")");
-    rpcc *cl;
+    LOG << "contact " << m << " w. my last_myvs(" << last_myvs.vid << "," << last_myvs.seqno << ")";
+    shared_ptr<rpcc> cl;
     {
         rsm_mutex_lock.unlock();
-        cl = h.safebind();
+        cl = rpcc::bind_cached(m);
         if (cl) {
             ret = cl->call_timeout(rsm_protocol::transferreq, milliseconds(100),
                     r, cfg->myaddr(), last_myvs, vid_insync);
@@ -212,23 +214,21 @@ bool rsm::statetransfer(const string & m, lock & rsm_mutex_lock)
         rsm_mutex_lock.lock();
     }
     if (cl == 0 || ret != rsm_protocol::OK) {
-        LOG("couldn't reach " << m << " " << hex << cl << " " << dec << ret);
+        LOG << "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;
-    LOG("transfer from " << m << " success, vs(" << last_myvs.vid << "," << last_myvs.seqno << ")");
+    LOG << "transfer from " << m << " success, vs(" << last_myvs.vid << "," << last_myvs.seqno << ")";
     return true;
 }
 
 bool rsm::statetransferdone(const string & m, lock & rsm_mutex_lock) {
     rsm_mutex_lock.unlock();
-    handle h(m);
-    rpcc *cl = h.safebind();
     bool done = false;
-    if (cl) {
+    if (auto cl = rpcc::bind_cached(m)) {
         int r;
         auto ret = (rsm_protocol::status)cl->call(rsm_protocol::transferdonereq, r, cfg->myaddr(), vid_insync);
         done = (ret == rsm_protocol::OK);
@@ -239,27 +239,22 @@ bool rsm::statetransferdone(const string & m, lock & rsm_mutex_lock) {
 
 
 bool rsm::join(const string & m, lock & rsm_mutex_lock) {
-    handle h(m);
     int ret = 0;
     string log;
 
-    LOG("contacting " << m << " mylast (" << last_myvs.vid << "," << last_myvs.seqno << ")");
-    rpcc *cl;
-    {
-        rsm_mutex_lock.unlock();
-        cl = h.safebind();
-        if (cl != 0) {
-            ret = cl->call_timeout(rsm_protocol::joinreq, milliseconds(12000), log,
-                    cfg->myaddr(), last_myvs);
-        }
-        rsm_mutex_lock.lock();
-    }
+    LOG << "contacting " << m << " mylast (" << last_myvs.vid << "," << last_myvs.seqno << ")";
+
+    rsm_mutex_lock.unlock();
+    auto cl = rpcc::bind_cached(m);
+    if (cl)
+        ret = cl->call_timeout(rsm_protocol::joinreq, milliseconds(12000), log, cfg->myaddr(), last_myvs);
+    rsm_mutex_lock.lock();
 
     if (cl == 0 || ret != rsm_protocol::OK) {
-        LOG("couldn't reach " << m << " " << hex << cl << " " << dec << ret);
+        LOG << "couldn't reach " << m << " " << std::hex << cl << " " << std::dec << ret;
         return false;
     }
-    LOG("succeeded " << log);
+    LOG << "succeeded " << log;
     cfg->restore(log);
     return true;
 }
@@ -278,8 +273,8 @@ void rsm::commit_change(unsigned vid) {
 void rsm::commit_change(unsigned vid, lock &) {
     if (vid <= vid_commit)
         return;
-    LOG("new view (" << vid << ") last vs (" << last_myvs.vid << "," <<
-            last_myvs.seqno << ") " << primary << " insync " << insync);
+    LOG << "new view (" << vid << ") last vs (" << last_myvs.vid << ","
+        << last_myvs.seqno << ") " << primary << " insync " << insync;
     vid_commit = vid;
     inviewchange = true;
     set_primary(vid);
@@ -291,7 +286,7 @@ void rsm::commit_change(unsigned vid, lock &) {
 
 
 void rsm::execute(rpc_protocol::proc_id_t procno, const string & req, string & r) {
-    LOG("execute");
+    LOG << "execute";
     handler *h = procs[procno];
     VERIFY(h);
     marshall rep;
@@ -299,6 +294,12 @@ void rsm::execute(rpc_protocol::proc_id_t procno, const string & req, string & r
     r = marshall(ret, rep.content()).content();
 }
 
+static void logHexString(locked_ostream && log, const string & s) {
+    log << std::setfill('0') << std::setw(2) << std::hex;
+    for (size_t i=0; i<s.size(); i++)
+        log << (unsigned int)(unsigned char)s[i];
+}
+
 //
 // Clients call client_invoke to invoke a procedure on the replicated state
 // machine: the primary receives the request, assigns it a sequence
@@ -306,21 +307,21 @@ void rsm::execute(rpc_protocol::proc_id_t procno, const string & req, string & r
 // machine.
 //
 rsm_client_protocol::status rsm::client_invoke(string & r, rpc_protocol::proc_id_t procno, const string & req) {
-    LOG("invoke procno 0x" << hex << procno);
+    LOG << "invoke procno 0x" << std::hex << procno;
     lock ml(invoke_mutex);
     vector<string> m;
     string myaddr;
     viewstamp vs;
     {
         lock ml2(rsm_mutex);
-        LOG("Checking for inviewchange");
+        LOG << "Checking for inviewchange";
         if (inviewchange)
             return rsm_client_protocol::BUSY;
-        LOG("Checking for primacy");
+        LOG << "Checking for primacy";
         myaddr = cfg->myaddr();
         if (primary != myaddr)
             return rsm_client_protocol::NOTPRIMARY;
-        LOG("Assigning a viewstamp");
+        LOG << "Assigning a viewstamp";
         cfg->get_view(vid_commit, m);
         // assign the RPC the next viewstamp number
         vs = myvs;
@@ -328,18 +329,17 @@ rsm_client_protocol::status rsm::client_invoke(string & r, rpc_protocol::proc_id
     }
 
     // send an invoke RPC to all slaves in the current view with a timeout of 1 second
-    LOG("Invoking slaves");
+    LOG << "Invoking slaves";
     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]);
-            LOG("Sending invoke to " << m[i]);
-            rpcc *cl = h.safebind();
+            LOG << "Sending invoke to " << m[i];
+            auto cl = rpcc::bind_cached(m[i]);
             if (!cl)
                 return rsm_client_protocol::BUSY;
             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);
+            LOG << "Invoke returned " << ret;
             if (ret != rsm_protocol::OK)
                 return rsm_client_protocol::BUSY;
             breakpoint(1);
@@ -347,10 +347,9 @@ rsm_client_protocol::status rsm::client_invoke(string & r, rpc_protocol::proc_id
             partition1(rsm_mutex_lock);
         }
     }
+    logHexString(LOG, req);
     execute(procno, req, r);
-    for (size_t i=0; i<r.size(); i++) {
-        LOG(hex << setfill('0') << setw(2) << (unsigned int)(unsigned char)r[i]);
-    }
+    logHexString(LOG, r);
     last_myvs = vs;
     return rsm_client_protocol::OK;
 }
@@ -363,26 +362,26 @@ rsm_client_protocol::status rsm::client_invoke(string & r, rpc_protocol::proc_id
 // according to requests' seqno
 
 rsm_protocol::status rsm::invoke(int &, rpc_protocol::proc_id_t proc, viewstamp vs, const string & req) {
-    LOG("invoke procno 0x" << hex << proc);
+    LOG << "invoke procno 0x" << std::hex << proc;
     lock ml(invoke_mutex);
     vector<string> m;
     string myaddr;
     {
         lock ml2(rsm_mutex);
         // check if !inviewchange
-        LOG("Checking for view change");
+        LOG << "Checking for view change";
         if (inviewchange)
             return rsm_protocol::ERR;
         // check if slave
-        LOG("Checking for slave status");
+        LOG << "Checking for slave status";
         myaddr = cfg->myaddr();
         if (primary == myaddr)
             return rsm_protocol::ERR;
         cfg->get_view(vid_commit, m);
-        if (find(m.begin(), m.end(), myaddr) == m.end())
+        if (std::find(m.begin(), m.end(), myaddr) == m.end())
             return rsm_protocol::ERR;
         // check sequence number
-        LOG("Checking sequence number");
+        LOG << "Checking sequence number";
         if (vs != myvs)
             return rsm_protocol::ERR;
         myvs++;
@@ -400,8 +399,8 @@ rsm_protocol::status rsm::invoke(int &, rpc_protocol::proc_id_t proc, viewstamp
 rsm_protocol::status rsm::transferreq(rsm_protocol::transferres & r, const string & src,
         viewstamp last, unsigned vid) {
     lock ml(rsm_mutex);
-    LOG("transferreq from " << src << " (" << last.vid << "," << last.seqno << ") vs (" <<
-            last_myvs.vid << "," << last_myvs.seqno << ")");
+    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)
@@ -418,7 +417,7 @@ rsm_protocol::status rsm::transferdonereq(int &, const string & m, unsigned vid)
     lock ml(rsm_mutex);
     if (!insync || vid != vid_insync)
         return rsm_protocol::BUSY;
-    backups.erase(find(backups.begin(), backups.end(), m));
+    backups.erase(std::find(backups.begin(), backups.end(), m));
     if (backups.empty())
         sync_cond.notify_one();
     return rsm_protocol::OK;
@@ -431,18 +430,18 @@ rsm_protocol::status rsm::joinreq(string & log, const string & m, viewstamp last
     auto ret = rsm_protocol::OK;
 
     lock ml(rsm_mutex);
-    LOG("join request from " << m << "; last=(" << last.vid << "," << last.seqno << "), mylast=(" <<
-            last_myvs.vid << "," << last_myvs.seqno << ")");
+    LOG << "join request from " << m << "; last=(" << last.vid << "," << last.seqno << "), mylast=("
+        << last_myvs.vid << "," << last_myvs.seqno << ")";
     if (cfg->ismember(m, vid_commit)) {
-        LOG(m << " is still a member -- nothing to do");
+        LOG << m << " is still a member -- nothing to do";
         log = cfg->dump();
     } else if (cfg->myaddr() != primary) {
-        LOG("but I, " << cfg->myaddr() << ", am not the primary, " << primary << "!");
+        LOG << "but I, " << cfg->myaddr() << ", am not the primary, " << primary << "!";
         ret = rsm_protocol::BUSY;
     } else {
         // We cache vid_commit to avoid adding m to a view which already contains
         // m due to race condition
-        LOG("calling down to config layer");
+        LOG << "calling down to config layer";
         unsigned vid_cache = vid_commit;
         bool succ;
         {
@@ -452,9 +451,9 @@ rsm_protocol::status rsm::joinreq(string & log, const string & m, viewstamp last
         }
         if (cfg->ismember(m, cfg->view_id())) {
             log = cfg->dump();
-            LOG("ret " << ret << " log " << log);
+            LOG << "ret " << ret << " log " << log;
         } else {
-            LOG("failed; proposer couldn't add " << succ);
+            LOG << "failed; proposer couldn't add " << succ;
             ret = rsm_protocol::BUSY;
         }
     }
@@ -471,7 +470,7 @@ rsm_client_protocol::status rsm::client_members(vector<string> & r, int) {
     cfg->get_view(vid_commit, m);
     m.push_back(primary);
     r = m;
-    LOG("return " << m << " m " << primary);
+    LOG << "return " << m << " m " << primary;
     return rsm_client_protocol::OK;
 }
 
@@ -485,7 +484,7 @@ void rsm::set_primary(unsigned vid) {
     VERIFY (c.size() > 0);
 
     if (isamember(primary,c)) {
-        LOG("primary stays " << primary);
+        LOG << "primary stays " << primary;
         return;
     }
 
@@ -493,7 +492,7 @@ void rsm::set_primary(unsigned vid) {
     for (unsigned i = 0; i < p.size(); i++) {
         if (isamember(p[i], c)) {
             primary = p[i];
-            LOG("primary is " << primary);
+            LOG << "primary is " << primary;
             return;
         }
     }
@@ -514,9 +513,9 @@ void rsm::net_repair(bool heal, lock & rsm_mutex_lock) {
     cfg->get_view(vid_commit, m);
     for (unsigned i  = 0; i < m.size(); i++) {
         if (m[i] != cfg->myaddr()) {
-            handle h(m[i]);
-            LOG("member " << m[i] << " " << heal);
-            if (h.safebind()) h.safebind()->set_reachable(heal);
+            LOG << "member " << m[i] << " " << heal;
+            if (auto cl = rpcc::bind_cached(m[i]))
+                cl->set_reachable(heal);
         }
     }
     rsmrpc->set_reachable(heal);
@@ -524,8 +523,8 @@ void rsm::net_repair(bool heal, lock & rsm_mutex_lock) {
 
 rsm_test_protocol::status rsm::test_net_repairreq(rsm_test_protocol::status & r, int heal) {
     lock ml(rsm_mutex);
-    LOG("heal " << heal << " (dopartition " <<
-            dopartition << ", partitioned " << partitioned << ")");
+    LOG << "heal " << heal << " (dopartition "
+        << dopartition << ", partitioned " << partitioned << ")";
     if (heal)
         net_repair(heal, ml);
     else
@@ -538,7 +537,7 @@ rsm_test_protocol::status rsm::test_net_repairreq(rsm_test_protocol::status & r,
 
 void rsm::breakpoint(int b) {
     if (breakpoints[b-1]) {
-        LOG("Dying at breakpoint " << b << " in rsm!");
+        LOG << "Dying at breakpoint " << b << " in rsm!";
         exit(1);
     }
 }
@@ -554,7 +553,7 @@ void rsm::partition1(lock & rsm_mutex_lock) {
 rsm_test_protocol::status rsm::breakpointreq(rsm_test_protocol::status & r, int b) {
     r = rsm_test_protocol::OK;
     lock ml(rsm_mutex);
-    LOG("breakpoint " << b);
+    LOG << "breakpoint " << b;
     if (b == 1) breakpoints[1-1] = true;
     else if (b == 2) breakpoints[2-1] = true;
     else if (b == 3 || b == 4) cfg->breakpoint(b);