Fixed a race condition!
[invirt/third/libt4.git] / rsm.cc
diff --git a/rsm.cc b/rsm.cc
index 1321f7e..a44c24b 100644 (file)
--- a/rsm.cc
+++ b/rsm.cc
 // The rule is that a module releases its internal locks before it
 // upcalls, but can keep its locks when calling down.
 
-#include <fstream>
-#include <iostream>
-#include <algorithm>
 #include <sys/types.h>
 #include <unistd.h>
 
+#include "types.h"
 #include "handle.h"
 #include "rsm.h"
-#include "threaded_log.h"
-#include "lang/verify.h"
 #include "rsm_client.h"
-#include "lock.h"
 
-rsm::rsm(std::string _first, std::string _me) :
+rsm::rsm(const string & _first, const 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;
-
-    cfg = new config(_first, _me, this);
+    cfg = unique_ptr<config>(new config(_first, _me, this));
 
     if (_first == _me) {
         // Commit the first view here. We can not have acceptor::acceptor
@@ -116,14 +106,16 @@ rsm::rsm(std::string _first, std::string _me) :
     rsmrpc->reg(rsm_protocol::joinreq, &rsm::joinreq, this);
 
     // tester must be on different port, otherwise it may partition itself
-    testsvr = new rpcs((uint32_t)std::stoi(_me) + 1);
+    testsvr = unique_ptr<rpcs>(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);
+}
 
-    {
-        lock ml(rsm_mutex);
-        std::thread(&rsm::recovery, this).detach();
-    }
+void rsm::start() {
+    lock ml(rsm_mutex);
+    rsmrpc->start();
+    testsvr->start();
+    thread(&rsm::recovery, this).detach();
 }
 
 void rsm::reg1(int proc, handler *h) {
@@ -145,7 +137,7 @@ void rsm::recovery() [[noreturn]] {
                 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?
+                this_thread::sleep_for(seconds(3)); // XXX make another node in cfg primary?
                 ml.lock();
             }
         }
@@ -191,7 +183,7 @@ bool rsm::sync_with_backups(lock & rsm_mutex_lock) {
     insync = true;
     cfg->get_view(vid_insync, backups);
     backups.erase(find(backups.begin(), backups.end(), cfg->myaddr()));
-    LOG("rsm::sync_with_backups " << make_iterator_pair(backups.begin(), backups.end()));
+    LOG("backups " << backups);
     sync_cond.wait(rsm_mutex_lock);
     insync = false;
     return true;
@@ -200,7 +192,7 @@ bool rsm::sync_with_backups(lock & rsm_mutex_lock) {
 
 bool rsm::sync_with_primary(lock & rsm_mutex_lock) {
     // Remember the primary of vid_insync
-    std::string m = primary;
+    string m = primary;
     while (vid_insync == vid_commit) {
         if (statetransfer(m, rsm_mutex_lock))
             break;
@@ -209,39 +201,39 @@ bool rsm::sync_with_primary(lock & rsm_mutex_lock) {
 }
 
 
-/**
- * Call to transfer state from m to the local node.
- * Assumes that rsm_mutex is already held.
- */
-bool rsm::statetransfer(std::string m, lock & rsm_mutex_lock)
+//
+// 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)
 {
     rsm_protocol::transferres r;
     handle h(m);
     int ret = 0;
-    LOG("rsm::statetransfer: contact " << m << " w. my last_myvs(" << last_myvs.vid << "," << last_myvs.seqno << ")");
+    LOG("contact " << m << " w. my last_myvs(" << last_myvs.vid << "," << last_myvs.seqno << ")");
     rpcc *cl;
     {
         rsm_mutex_lock.unlock();
         cl = h.safebind();
         if (cl) {
-            ret = cl->call_timeout(rsm_protocol::transferreq, rpcc::to(1000),
+            ret = cl->call_timeout(rsm_protocol::transferreq, milliseconds(100),
                     r, cfg->myaddr(), last_myvs, vid_insync);
         }
         rsm_mutex_lock.lock();
     }
     if (cl == 0 || ret != rsm_protocol::OK) {
-        LOG("rsm::statetransfer: couldn't reach " << m << " " << std::hex << cl << " " << std::dec << ret);
+        LOG("couldn't reach " << m << " " << hex << cl << " " << dec << ret);
         return false;
     }
     if (stf && last_myvs != r.last) {
         stf->unmarshal_state(r.state);
     }
     last_myvs = r.last;
-    LOG("rsm::statetransfer 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(std::string m, lock & rsm_mutex_lock) {
+bool rsm::statetransferdone(const string & m, lock & rsm_mutex_lock) {
     rsm_mutex_lock.unlock();
     handle h(m);
     rpcc *cl = h.safebind();
@@ -256,36 +248,36 @@ bool rsm::statetransferdone(std::string m, lock & rsm_mutex_lock) {
 }
 
 
-bool rsm::join(std::string m, lock & rsm_mutex_lock) {
+bool rsm::join(const string & m, lock & rsm_mutex_lock) {
     handle h(m);
     int ret = 0;
-    rsm_protocol::joinres r;
+    string log;
 
-    LOG("rsm::join: " << m << " mylast (" << last_myvs.vid << "," << last_myvs.seqno << ")");
+    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, rpcc::to(120000), r,
+            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("rsm::join: couldn't reach " << m << " " << std::hex << cl << " " << std::dec << ret);
+        LOG("couldn't reach " << m << " " << hex << cl << " " << dec << ret);
         return false;
     }
-    LOG("rsm::join: succeeded " << r.log);
-    cfg->restore(r.log);
+    LOG("succeeded " << log);
+    cfg->restore(log);
     return true;
 }
 
-/*
- * Config informs rsm whenever it has successfully
- * completed a view change
- */
+//
+// Config informs rsm whenever it has successfully
+// completed a view change
+//
 void rsm::commit_change(unsigned vid) {
     lock ml(rsm_mutex);
     commit_change(vid, ml);
@@ -308,18 +300,14 @@ void rsm::commit_change(unsigned vid, lock &) {
 }
 
 
-void rsm::execute(int procno, std::string req, std::string &r) {
+void rsm::execute(int procno, const string & req, string & r) {
     LOG("execute");
     handler *h = procs[procno];
     VERIFY(h);
-    unmarshall args(req);
+    unmarshall args(req, false);
     marshall rep;
-    std::string reps;
     auto ret = (rsm_protocol::status)(*h)(args, rep);
-    marshall rep1;
-    rep1 << ret;
-    rep1 << rep.str();
-    r = rep1.str();
+    r = marshall{ret, rep.content()}.content();
 }
 
 //
@@ -328,11 +316,11 @@ void rsm::execute(int procno, std::string req, std::string &r) {
 // number, and invokes it on all members of the replicated state
 // machine.
 //
-rsm_client_protocol::status rsm::client_invoke(std::string &r, int procno, std::string req) {
-    LOG("rsm::client_invoke: procno 0x" << std::hex << procno);
+rsm_client_protocol::status rsm::client_invoke(string & r, int procno, const string & req) {
+    LOG("invoke procno 0x" << hex << procno);
     lock ml(invoke_mutex);
-    std::vector<std::string> m;
-    std::string myaddr;
+    vector<string> m;
+    string myaddr;
     viewstamp vs;
     {
         lock ml2(rsm_mutex);
@@ -361,7 +349,7 @@ rsm_client_protocol::status rsm::client_invoke(std::string &r, int procno, std::
             if (!cl)
                 return rsm_client_protocol::BUSY;
             int ignored_rval;
-            auto ret = (rsm_protocol::status)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, milliseconds(100), ignored_rval, procno, vs, req);
             LOG("Invoke returned " << ret);
             if (ret != rsm_protocol::OK)
                 return rsm_client_protocol::BUSY;
@@ -382,11 +370,11 @@ rsm_client_protocol::status rsm::client_invoke(std::string &r, int procno, std::
 // the replica must execute requests in order (with no gaps)
 // according to requests' seqno
 
-rsm_protocol::status rsm::invoke(int &, int proc, viewstamp vs, std::string req) {
-    LOG("rsm::invoke: procno 0x" << std::hex << proc);
+rsm_protocol::status rsm::invoke(int &, int proc, viewstamp vs, const string & req) {
+    LOG("invoke procno 0x" << hex << proc);
     lock ml(invoke_mutex);
-    std::vector<std::string> m;
-    std::string myaddr;
+    vector<string> m;
+    string myaddr;
     {
         lock ml2(rsm_mutex);
         // check if !inviewchange
@@ -407,17 +395,17 @@ rsm_protocol::status rsm::invoke(int &, int proc, viewstamp vs, std::string req)
             return rsm_protocol::ERR;
         myvs++;
     }
-    std::string r;
+    string r;
     execute(proc, req, r);
     last_myvs = vs;
     breakpoint1();
     return rsm_protocol::OK;
 }
 
-/**
- * RPC handler: Send back the local node's state to the caller
- */
-rsm_protocol::status rsm::transferreq(rsm_protocol::transferres &r, std::string src,
+//
+// 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) {
     lock ml(rsm_mutex);
     LOG("transferreq from " << src << " (" << last.vid << "," << last.seqno << ") vs (" <<
@@ -430,11 +418,11 @@ rsm_protocol::status rsm::transferreq(rsm_protocol::transferres &r, std::string
     return rsm_protocol::OK;
 }
 
-/**
- * RPC handler: Inform the local node (the primary) that node m has synchronized
- * for view vid
- */
-rsm_protocol::status rsm::transferdonereq(int &, std::string m, unsigned vid) {
+//
+// 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) {
     lock ml(rsm_mutex);
     if (!insync || vid != vid_insync)
         return rsm_protocol::BUSY;
@@ -447,21 +435,22 @@ rsm_protocol::status rsm::transferdonereq(int &, std::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.
-rsm_protocol::status rsm::joinreq(rsm_protocol::joinres &r, std::string m, viewstamp last) {
+rsm_protocol::status rsm::joinreq(string & log, const string & m, viewstamp last) {
     auto ret = rsm_protocol::OK;
 
     lock ml(rsm_mutex);
-    LOG("joinreq: src " << m << " last (" << last.vid << "," << last.seqno << ") mylast (" <<
+    LOG("join request from " << m << "; last=(" << last.vid << "," << last.seqno << "), mylast=(" <<
             last_myvs.vid << "," << last_myvs.seqno << ")");
     if (cfg->ismember(m, vid_commit)) {
-        LOG("joinreq: is still a member");
-        r.log = cfg->dump();
+        LOG(m << " is still a member -- nothing to do");
+        log = cfg->dump();
     } else if (cfg->myaddr() != primary) {
-        LOG("joinreq: busy");
+        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");
         unsigned vid_cache = vid_commit;
         bool succ;
         {
@@ -470,28 +459,28 @@ rsm_protocol::status rsm::joinreq(rsm_protocol::joinres &r, std::string m, views
             ml.lock();
         }
         if (cfg->ismember(m, cfg->view_id())) {
-            r.log = cfg->dump();
-            LOG("joinreq: ret " << ret << " log " << r.log);
+            log = cfg->dump();
+            LOG("ret " << ret << " log " << log);
         } else {
-            LOG("joinreq: failed; proposer couldn't add " << succ);
+            LOG("failed; proposer couldn't add " << succ);
             ret = rsm_protocol::BUSY;
         }
     }
     return ret;
 }
 
-/*
- * 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(std::vector<std::string> &r, int) {
-    std::vector<std::string> m;
+//
+// 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(vector<string> &r, int) {
+    vector<string> m;
     lock ml(rsm_mutex);
     cfg->get_view(vid_commit, m);
     m.push_back(primary);
     r = m;
-    LOG("rsm::client_members return " << print_members(m) << " m " << primary);
+    LOG("return " << m << " m " << primary);
     return rsm_client_protocol::OK;
 }
 
@@ -499,7 +488,7 @@ rsm_client_protocol::status rsm::client_members(std::vector<std::string> &r, int
 // otherwise, the lowest number node of the previous view.
 // caller should hold rsm_mutex
 void rsm::set_primary(unsigned vid) {
-    std::vector<std::string> c, p;
+    vector<string> c, p;
     cfg->get_view(vid, c);
     cfg->get_view(vid - 1, p);
     VERIFY (c.size() > 0);
@@ -532,12 +521,12 @@ bool rsm::amiprimary() {
 
 // assumes caller holds rsm_mutex
 void rsm::net_repair(bool heal, lock &) {
-    std::vector<std::string> m;
+    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]);
-            LOG("rsm::net_repair: " << m[i] << " " << heal);
+            LOG("member " << m[i] << " " << heal);
             if (h.safebind()) h.safebind()->set_reachable(heal);
         }
     }
@@ -546,7 +535,7 @@ void rsm::net_repair(bool heal, lock &) {
 
 rsm_test_protocol::status rsm::test_net_repairreq(rsm_test_protocol::status &r, int heal) {
     lock ml(rsm_mutex);
-    LOG("rsm::test_net_repairreq: " << heal << " (dopartition " <<
+    LOG("heal " << heal << " (dopartition " <<
             dopartition << ", partitioned " << partitioned << ")");
     if (heal) {
         net_repair(heal, ml);
@@ -586,7 +575,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("rsm::breakpointreq: " << b);
+    LOG("breakpoint " << b);
     if (b == 1) break1 = true;
     else if (b == 2) break2 = true;
     else if (b == 3 || b == 4) cfg->breakpoint(b);