Lots more clean-ups
[invirt/third/libt4.git] / paxos.h
diff --git a/paxos.h b/paxos.h
index 8561dd5..642d3ff 100644 (file)
--- a/paxos.h
+++ b/paxos.h
 #ifndef paxos_h
 #define paxos_h
 
 #ifndef paxos_h
 #define paxos_h
 
-#include <string>
-#include <vector>
-#include <map>
+#include "types.h"
 #include "rpc/rpc.h"
 #include "paxos_protocol.h"
 #include "log.h"
 #include "rpc/rpc.h"
 #include "paxos_protocol.h"
 #include "log.h"
-#include "lock.h"
 
 
-using std::string;
-using std::map;
-using std::vector;
+using prepareres = paxos_protocol::prepareres;
+
+using node_t = string;
+using nodes_t = vector<node_t>;
+using value_t = string;
 
 class paxos_change {
     public:
 
 class paxos_change {
     public:
-        virtual void paxos_commit(unsigned instance, const string & v) = 0;
+        virtual void paxos_commit(unsigned instance, const value_t & v) = 0;
         virtual ~paxos_change() {}
 };
 
         virtual ~paxos_change() {}
 };
 
-class acceptor {
+extern bool isamember(const node_t & m, const nodes_t & nodes);
+extern bool majority(const nodes_t & l1, const nodes_t & l2);
+
+class proposer_acceptor {
     private:
     private:
-        log *l;
-        rpcs *pxs;
-        paxos_change *cfg;
-        string me;
-        mutex pxs_mutex;
+        mutex proposer_mutex;
+        mutex acceptor_mutex;
 
 
-        // Acceptor state
-        prop_t n_h;            // number of the highest proposal seen in a prepare
-        prop_t n_a;            // number of highest proposal accepted
-        string v_a;    // value of highest proposal accepted
-        unsigned instance_h;   // number of the highest instance we have decided
-        map<unsigned,string> values;   // vals of each instance
-
-        void commit(unsigned instance, const string & v, lock & pxs_mutex_lock);
-        paxos_protocol::status preparereq(paxos_protocol::prepareres & r,
-                const string & src, paxos_protocol::preparearg a);
-        paxos_protocol::status acceptreq(bool & r, const string & src,
-                paxos_protocol::acceptarg a);
-        paxos_protocol::status decidereq(int & r, const string & src,
-                paxos_protocol::decidearg a);
+        paxos_change *delegate;
+        node_t me;
 
 
-        friend class log;
+        rpcs pxs{(in_port_t)stoi(me)};
 
 
-    public:
-        acceptor(class paxos_change *cfg, bool _first, const string & _me,
-                const string & _value);
-        ~acceptor() {}
-        void commit(unsigned instance, const string & v);
-        unsigned instance() { return instance_h; }
-        const string & value(unsigned instance) { return values[instance]; }
-        string dump();
-        void restore(const string &);
-        rpcs *get_rpcs() { return pxs; }
-        prop_t get_n_h() { return n_h; }
-        unsigned get_instance_h() { return instance_h; }
-};
+        bool break1 = false;
+        bool break2 = false;
 
 
-extern bool isamember(const string & m, const vector<string> & nodes);
-extern string print_members(const vector<string> & nodes);
+        // Proposer state
+        bool stable = true;
+        prop_t proposal = {0, me};  // number of the last proposal used in this instance
 
 
-class proposer {
-    private:
-        log *l;
-        paxos_change *cfg;
-        acceptor *acc;
-        string me;
-        bool break1;
-        bool break2;
+        // Acceptor state
+        prop_t promise = {0, me};   // number of the highest proposal seen in a prepare
+        prop_t accepted = {0, me};  // number of highest proposal accepted
+        value_t accepted_value;     // value of highest proposal accepted
+        unsigned instance_h = 0;    // number of the highest instance we have decided
+        map<unsigned,value_t> values;   // vals of each instance
 
 
-        mutex pxs_mutex;
+        friend class log;
+        log l = {this, me};
 
 
-        // Proposer state
-        bool stable;
-        prop_t my_n;           // number of the last proposal used in this instance
-
-        void setn();
-        bool prepare(unsigned instance, vector<string> & accepts,
-                const vector<string> & nodes,
-                string & v);
-        void accept(unsigned instance, vector<string> & accepts,
-                const vector<string> & nodes, const string & v);
-        void decide(unsigned instance, const vector<string> & accepts,
-                const string & v);
+        void commit(unsigned instance, const value_t & v);
+        void commit(unsigned instance, const value_t & v, lock & pxs_mutex_lock);
+
+        paxos_protocol::status preparereq(prepareres & r, const node_t & src, unsigned instance, prop_t n);
+        paxos_protocol::status acceptreq(bool & r, const node_t & src, unsigned instance, prop_t n, const value_t & v);
+        paxos_protocol::status decidereq(int & r, const node_t & src, unsigned instance, const value_t & v);
+
+        bool prepare(unsigned instance, nodes_t & accepts, const nodes_t & nodes, value_t & v);
+        void accept(unsigned instance, nodes_t & accepts, const nodes_t & nodes, const value_t & v);
+        void decide(unsigned instance, const nodes_t & accepts, const value_t & v);
 
         void breakpoint1();
         void breakpoint2();
 
         void breakpoint1();
         void breakpoint2();
-        bool majority(const vector<string> & l1, const vector<string> & l2);
 
 
-        friend class log;
     public:
     public:
-        proposer(class paxos_change *cfg, class acceptor *_acceptor, const string &_me);
-        ~proposer() {}
-        bool run(unsigned instance, const vector<string> & cnodes, const string & v);
-        bool isrunning();
+        proposer_acceptor(paxos_change *delegate, bool _first, const node_t & _me, const value_t & _value);
+        unsigned instance() { return instance_h; }
+        const value_t & value(unsigned instance) { return values[instance]; }
+        string dump() { return l.dump(); }
+        void restore(const string &s) { l.restore(s); l.logread(); }
+        rpcs *get_rpcs() { return &pxs; }
+
+        bool run(unsigned instance, const nodes_t & cnodes, const value_t & v);
+        bool isrunning() { lock ml(proposer_mutex); return !stable; }
         void breakpoint(int b);
 };
 
         void breakpoint(int b);
 };
 
-
-
-#endif /* paxos_h */
+#endif