7df1bbce2284b24c647f7d5ccaa45b47e0ecc7ac
[invirt/third/libt4.git] / config.cc
1 #include "config.h"
2 #include "handle.h"
3
4 // The config module maintains views. As a node joins or leaves a
5 // view, the next view will be the same as previous view, except with
6 // the new node added or removed. The first view contains only node
7 // 1. If node 2 joins after the first node (it will download the views
8 // from node 1), it will learn about view 1 with the first node as the
9 // only member.  It will then invoke Paxos to create the next view.
10 // It will tell Paxos to ask the nodes in view 1 to agree on the value
11 // {1, 2}.  If Paxos returns success, then it moves to view 2 with
12 // {1,2} as the members. When node 3 joins, the config module runs
13 // Paxos with the nodes in view 2 and the proposed value to be
14 // {1,2,3}. And so on.  When a node discovers that some node of the
15 // current view is not responding, it kicks off Paxos to propose a new
16 // value (the current view minus the node that isn't responding). The
17 // config module uses Paxos to create a total order of views, and it
18 // is ensured that the majority of the previous view agrees to the
19 // next view.  The Paxos log contains all the values (i.e., views)
20 // agreed on.
21 //
22 // The RSM module informs config to add nodes. The config module
23 // runs a heartbeater thread that checks in with nodes.  If a node
24 // doesn't respond, the config module will invoke Paxos's proposer to
25 // remove the node.  Higher layers will learn about this change when a
26 // Paxos acceptor accepts the new proposed value through
27 // paxos_commit().
28 //
29 // To be able to bring other nodes up to date to the latest formed
30 // view, each node will have a complete history of all view numbers
31 // and their values that it knows about. At any time a node can reboot
32 // and when it re-joins, it may be many views behind; by remembering
33 // all views, the other nodes can bring this re-joined node up to
34 // date.
35
36 config::config(const string & _first, const string & _me, config_view_change *_vc)
37     : my_view_id(0), first(_first), me(_me), vc(_vc),
38       paxos(this, me == _first, me, me)
39 {
40     get_rpcs()->reg(paxos_protocol::heartbeat, &config::heartbeat, this);
41     lock cfg_mutex_lock(cfg_mutex);
42     reconstruct(cfg_mutex_lock);
43     thread(&config::heartbeater, this).detach();
44 }
45
46 void config::restore(const string & s) {
47     lock cfg_mutex_lock(cfg_mutex);
48     paxos.restore(s);
49     reconstruct(cfg_mutex_lock);
50 }
51
52 void config::get_view(unsigned instance, vector<string> & m) {
53     lock cfg_mutex_lock(cfg_mutex);
54     get_view(instance, m, cfg_mutex_lock);
55 }
56
57 void config::get_view(unsigned instance, vector<string> & m, lock & cfg_mutex_lock) {
58     VERIFY(cfg_mutex_lock);
59     string value = paxos.value(instance);
60     LOG("get_view(" << instance << "): returns " << value);
61     m = explode(value);
62 }
63
64 void config::reconstruct(lock & cfg_mutex_lock) {
65     VERIFY(cfg_mutex_lock);
66     my_view_id = paxos.instance();
67     if (my_view_id > 0) {
68         get_view(my_view_id, mems, cfg_mutex_lock);
69         LOG("view " << my_view_id << " " << mems);
70     }
71 }
72
73 // Called by Paxos's acceptor.
74 void config::paxos_commit(unsigned instance, const string & value) {
75     lock cfg_mutex_lock(cfg_mutex);
76
77     vector<string> newmem = explode(value);
78     LOG("instance " << instance << ": " << newmem);
79
80     for (auto mem : mems) {
81         LOG("is " << mem << " still a member?");
82         if (!isamember(mem, newmem) && me != mem) {
83             LOG("delete " << mem);
84             handle(mem).invalidate();
85         }
86     }
87
88     mems = newmem;
89     my_view_id = instance;
90     if (vc) {
91         cfg_mutex_lock.unlock();
92         vc->commit_change(instance);
93         cfg_mutex_lock.lock();
94     }
95 }
96
97 bool config::ismember(const string & m, unsigned vid) {
98     lock cfg_mutex_lock(cfg_mutex);
99     vector<string> v;
100     get_view(vid, v, cfg_mutex_lock);
101     return isamember(m, v);
102 }
103
104 bool config::add(const string & new_m, unsigned vid) {
105     lock cfg_mutex_lock(cfg_mutex);
106     LOG("adding " << new_m << " to " << vid);
107     if (vid != my_view_id) {
108         LOG("that's not my view id, " << my_view_id << "!");
109         return false;
110     }
111     LOG("calling down to paxos layer");
112     vector<string> m(mems), cmems(mems);
113     m.push_back(new_m);
114     LOG("old mems " << cmems << " " << implode(cmems));
115     LOG("new mems " << m << " " << implode(m));
116     unsigned nextvid = my_view_id + 1;
117     cfg_mutex_lock.unlock();
118     bool r = paxos.run(nextvid, cmems, implode(m));
119     cfg_mutex_lock.lock();
120     LOG("paxos proposer returned " << (r ? "success" : "failure"));
121     return r;
122 }
123
124 // caller should hold cfg_mutex
125 bool config::remove(const string & m, lock & cfg_mutex_lock) {
126     VERIFY(cfg_mutex_lock);
127     LOG("my_view_id " << my_view_id << " remove? " << m);
128     vector<string> n;
129     for (auto mem : mems) {
130         if (mem != m)
131             n.push_back(mem);
132     }
133     vector<string> cmems = mems;
134     unsigned nextvid = my_view_id + 1;
135     cfg_mutex_lock.unlock();
136     bool r = paxos.run(nextvid, cmems, implode(n));
137     cfg_mutex_lock.lock();
138     LOG("proposer returned " << (r ? "success" : "failure"));
139     return r;
140 }
141
142 void config::heartbeater() {
143     lock cfg_mutex_lock(cfg_mutex);
144
145     while (1) {
146         auto next_timeout = steady_clock::now() + milliseconds(300);
147         LOG("go to sleep");
148         config_cond.wait_until(cfg_mutex_lock, next_timeout);
149
150         unsigned vid = my_view_id;
151         vector<string> cmems;
152         get_view(vid, cmems, cfg_mutex_lock);
153         LOG("current membership " << cmems);
154
155         if (!isamember(me, cmems)) {
156             LOG("not member yet; skip hearbeat");
157             continue;
158         }
159
160         // who has the smallest ID?
161         string m = min(me, *min_element(cmems.begin(), cmems.end()));
162
163         if (m == me) {
164             // ping the other nodes
165             for (string mem : cmems) {
166                 if (mem == me || doheartbeat(mem, cfg_mutex_lock) == OK)
167                     continue;
168                 if (vid == my_view_id)
169                     remove(mem, cfg_mutex_lock);
170                 break;
171             }
172         } else {
173             // ping the node with the smallest ID
174             if (doheartbeat(m, cfg_mutex_lock) != OK && vid == my_view_id)
175                 remove(m, cfg_mutex_lock);
176         }
177     }
178 }
179
180 paxos_protocol::status config::heartbeat(int & r, string m, unsigned vid) {
181     lock cfg_mutex_lock(cfg_mutex);
182     r = (int) my_view_id;
183     LOG("heartbeat from " << m << "(" << vid << ") my_view_id " << my_view_id);
184     if (vid == my_view_id)
185         return paxos_protocol::OK;
186     else if (paxos.isrunning()) {
187         VERIFY (vid == my_view_id + 1 || vid + 1 == my_view_id);
188         return paxos_protocol::OK;
189     }
190     return paxos_protocol::ERR;
191 }
192
193 config::heartbeat_t config::doheartbeat(const string & m, lock & cfg_mutex_lock) {
194     VERIFY(cfg_mutex_lock);
195     unsigned vid = my_view_id;
196     LOG("heartbeat to " << m << " (" << vid << ")");
197     handle h(m);
198
199     cfg_mutex_lock.unlock();
200     int r = 0, ret = rpc_protocol::bind_failure;
201     if (rpcc *cl = h.safebind())
202         ret = cl->call_timeout(paxos_protocol::heartbeat, milliseconds(100), r, me, vid);
203     cfg_mutex_lock.lock();
204
205     heartbeat_t res = OK;
206     switch (ret) {
207         case paxos_protocol::OK:
208             break;
209         case rpc_protocol::atmostonce_failure:
210         case rpc_protocol::oldsrv_failure:
211             h.invalidate();
212             break;
213         default:
214             LOG("problem with " << m << " (" << ret << ") my vid " << vid << " his vid " << r);
215             res = (ret < 0) ? FAILURE : VIEWERR;
216     }
217     LOG("done " << res);
218     return res;
219 }