f2eb5bd4e9c8641173b45a48d70e16d040bfe9ee
[invirt/third/libt4.git] / rsm.h
1 // replicated state machine interface.
2
3 #ifndef rsm_h
4 #define rsm_h
5
6 #include "types.h"
7 #include "rsm_protocol.h"
8 #include "rpc/rpc.h"
9 #include <arpa/inet.h>
10 #include "config.h"
11
12 class rsm_state_transfer {
13     public:
14         virtual string marshal_state() = 0;
15         virtual void unmarshal_state(string) = 0;
16         virtual ~rsm_state_transfer() {}
17 };
18
19 class rsm : public config_view_change {
20     private:
21         void reg1(int proc, handler *);
22     protected:
23         map<int, handler *> procs;
24         config *cfg;
25         class rsm_state_transfer *stf;
26         rpcs *rsmrpc;
27         // On slave: expected viewstamp of next invoke request
28         // On primary: viewstamp for the next request from rsm_client
29         viewstamp myvs;
30         viewstamp last_myvs;   // Viewstamp of the last executed request
31         string primary;
32         bool insync;
33         bool inviewchange;
34         unsigned vid_commit;  // Latest view id that is known to rsm layer
35         unsigned vid_insync;  // The view id that this node is synchronizing for
36         vector<string> backups;   // A list of unsynchronized backups
37
38         // For testing purposes
39         rpcs *testsvr;
40         bool partitioned;
41         bool dopartition;
42         bool break1;
43         bool break2;
44
45         rsm_client_protocol::status client_members(vector<string> &r, int i);
46         rsm_protocol::status invoke(int &, int proc, viewstamp vs, string mreq);
47         rsm_protocol::status transferreq(rsm_protocol::transferres &r, string src,
48                 viewstamp last, unsigned vid);
49         rsm_protocol::status transferdonereq(int &, string m, unsigned vid);
50         rsm_protocol::status joinreq(string & log, string src,
51                 viewstamp last);
52         rsm_test_protocol::status test_net_repairreq(rsm_test_protocol::status &r, int heal);
53         rsm_test_protocol::status breakpointreq(rsm_test_protocol::status &r, int b);
54
55         mutex rsm_mutex, invoke_mutex;
56         cond recovery_cond, sync_cond;
57
58         void execute(int procno, string req, string &r);
59         rsm_client_protocol::status client_invoke(string &r, int procno, string req);
60         bool statetransfer(string m, lock & rsm_mutex_lock);
61         bool statetransferdone(string m, lock & rsm_mutex_lock);
62         bool join(string m, lock & rsm_mutex_lock);
63         void set_primary(unsigned vid);
64         string find_highest(viewstamp &vs, string &m, unsigned &vid);
65         bool sync_with_backups(lock & rsm_mutex_lock);
66         bool sync_with_primary(lock & rsm_mutex_lock);
67         void net_repair(bool heal, lock & rsm_mutex_lock);
68         void breakpoint1();
69         void breakpoint2();
70         void partition1(lock & rsm_mutex_lock);
71         void commit_change(unsigned vid, lock & rsm_mutex_lock);
72     public:
73         rsm (string _first, string _me);
74         ~rsm() {}
75
76         bool amiprimary();
77         void set_state_transfer(rsm_state_transfer *_stf) { stf = _stf; }
78         void recovery();
79         void commit_change(unsigned vid);
80
81         template<class F, class C=void> void reg(int proc, F f, C *c=nullptr);
82 };
83
84 template<class F, class C> void rsm::reg(int proc, F f, C *c) {
85     reg1(proc, marshalled_func<F>::wrap(f, c));
86 }
87
88 #endif /* rsm_h */