r17681 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r17680‎ | r17681 | r17682 >
Date:21:09, 14 November 2006
Author:river
Status:old
Tags:
Comment:
allow a backend group to have a failover group specified; if all backends in the group are down, send requests to the failover group instead
Modified paths:
  • /trunk/willow/src/include/wbackend.h (modified) (history)
  • /trunk/willow/src/willow/wbackend.cc (modified) (history)
  • /trunk/willow/src/willow/wconfig.cc (modified) (history)
  • /trunk/willow/src/willow/whttp.cc (modified) (history)
  • /trunk/willow/willow.conf.example (modified) (history)

Diff [purge]

Index: trunk/willow/src/include/wbackend.h
@@ -58,6 +58,7 @@
5959 backend_list( backend_pool const &pool,
6060 imstring const &url,
6161 imstring const &host,
 62+ int failgroup,
6263 lb_type, int start);
6364
6465 int _get_impl (polycallback<backend *, wsocket *>);
@@ -65,6 +66,9 @@
6667 struct backend *_next_backend (void);
6768 void _carp_recalc (imstring const &, imstring const &, lb_type);
6869 static int _becarp_cmp (backend const *a, backend const *b);
 70+ bool failed (void) const {
 71+ return _delegate;
 72+ }
6973
7074 template<typename stringT>
7175 static uint32_t _carp_urlhash(stringT const &str) {
@@ -81,9 +85,16 @@
8286 return _get_impl(polycallback<backend *, wsocket *>(cb, t));
8387 }
8488
 89+ ~backend_list() {
 90+ if (_delegate)
 91+ delete _delegate;
 92+ }
 93+
8594 private:
8695 vector<backend *, pt_allocator<backend *> > backends;
87 - size_t _cur;
 96+ size_t _cur;
 97+ int _failgroup;
 98+ struct backend_list *_delegate;
8899 };
89100
90101 template<typename stringT>
@@ -95,7 +106,7 @@
96107 }
97108
98109 struct backend_pool {
99 - backend_pool(string const &name, lb_type);
 110+ backend_pool(string const &name, lb_type, int failgroup = -1);
100111 ~backend_pool();
101112
102113 void add (string const &, int, int);
@@ -118,6 +129,7 @@
119130 tss<size_t> _cur;
120131 lb_type _lbtype;
121132 string _name;
 133+ int _failgroup;
122134 };
123135
124136 extern map<int, backend_pool> bpools;
Index: trunk/willow/src/willow/wconfig.cc
@@ -303,7 +303,7 @@
304304 void
305305 set_backend_group(tree_entry &e)
306306 {
307 -int gn;
 307+int gn, fogroup = -1;
308308 string group;
309309 map<string, int>::iterator it;
310310 group = e.item_key;
@@ -327,8 +327,16 @@
328328 lbtype = lb_carp_hostonly;
329329 }
330330
 331+ if ((v = e/"failover-group") != NULL) {
 332+ if ((it = poolnames.find(v->cv_values[0].av_strval)) == poolnames.end()) {
 333+ v->report_error("failover-group does not exist");
 334+ } else {
 335+ fogroup = it->second;
 336+ }
 337+ }
 338+
331339 WDEBUG((WLOG_DEBUG, format("adding backend %d type = %d") % gn % (int) lbtype));
332 - bpools.insert(make_pair(gn, backend_pool(e.item_key, lbtype)));
 340+ bpools.insert(make_pair(gn, backend_pool(e.item_key, lbtype, fogroup)));
333341
334342 if ((v = e/"hosts") != NULL) {
335343 vector<avalue>::iterator it = v->cv_values.begin(), end = v->cv_values.end();
@@ -409,8 +417,9 @@
410418
411419 .block("backend-group", require_name)
412420 .end(func(set_backend_group))
413 - .value("lb-type", func(v_lb_type), ignore)
414 - .value("hosts", func(v_hosts), ignore)
 421+ .value("lb-type", func(v_lb_type), ignore)
 422+ .value("hosts", func(v_hosts), ignore)
 423+ .value("failover-group", nonempty_qstring, ignore)
415424
416425 .block("backend", require_name)
417426 .end(func(set_backend))
Index: trunk/willow/src/willow/whttp.cc
@@ -263,8 +263,6 @@
264264 _chunking_filter = NULL;
265265 delete _size_limit;
266266 _size_limit = NULL;
267 - delete _blist;
268 - _blist = NULL;
269267 delete _header_parser;
270268 _header_parser = NULL;
271269
@@ -272,13 +270,15 @@
273271 * Return the backend to the keepalive pool, if we can.
274272 */
275273 if (_backend_socket && !_backend_headers->_no_keepalive &&
276 - _backend_headers->_http_vers == http11) {
 274+ _backend_headers->_http_vers == http11 && (!_blist || !_blist->failed())) {
277275 bpools.find(_group)->second.add_keptalive(
278276 make_pair(_backend_socket, _backend));
279277 } else {
280278 delete _backend_socket;
281279 }
282280
 281+ delete _blist;
 282+ _blist = NULL;
283283 _backend_socket = NULL;
284284 delete _backend_headers;
285285 _backend_headers = NULL;
Index: trunk/willow/src/willow/wbackend.cc
@@ -58,9 +58,10 @@
5959 % be_straddr % be_hash));
6060 }
6161
62 -backend_pool::backend_pool(string const &name, lb_type lbt)
 62+backend_pool::backend_pool(string const &name, lb_type lbt, int failgroup)
6363 : _lbtype(lbt)
6464 , _name(name)
 65+ , _failgroup(failgroup)
6566 {
6667 WDEBUG((WLOG_DEBUG, format("creating backend_pool, lbt=%d") % (int) lbt));
6768 }
@@ -97,6 +98,12 @@
9899 static time_t last_nfile;
99100 time_t now = time(NULL);
100101
 102+ /*
 103+ * If we're delegating (for failover), pass this request off.
 104+ */
 105+ if (_delegate)
 106+ return _delegate->_get_impl(cb);
 107+
101108 cbd = new backend_cb_data;
102109 cbd->bc_func = cb;
103110
@@ -104,7 +111,15 @@
105112 cbd->bc_backend = _next_backend();
106113
107114 if (cbd->bc_backend == NULL) {
 115+ /*
 116+ * All out of backends. See if we have a failover
 117+ * group to try.
 118+ */
108119 delete cbd;
 120+ if (_failgroup != -1) {
 121+ _delegate = bpools.find(_failgroup)->second.get_list("", "");
 122+ return _delegate->_get_impl(cb);
 123+ }
109124 return -1;
110125 }
111126
@@ -179,11 +194,14 @@
180195 backend_pool const &bp,
181196 imstring const &url,
182197 imstring const &host,
 198+ int failgroup,
183199 lb_type lbt,
184200 int cur)
185201
186202 : backends(bp.backends)
187203 , _cur(0)
 204+ , _failgroup(failgroup)
 205+ , _delegate(NULL)
188206 {
189207 WDEBUG((WLOG_DEBUG, format("lbt = %d") % (int)lbt));
190208 rotate(backends.begin(), backends.begin() + cur, backends.end());
@@ -205,7 +223,7 @@
206224 if (*_cur >= backends.size())
207225 *_cur = 0;
208226
209 - return new backend_list(*this, url, host, _lbtype, (*_cur)++);
 227+ return new backend_list(*this, url, host, _failgroup, _lbtype, (*_cur)++);
210228 }
211229
212230 struct backend *
@@ -216,9 +234,6 @@
217235 while (tried++ <= backends.size()) {
218236 time_t now = time(NULL);
219237
220 - WDEBUG((WLOG_DEBUG, format("_next_backend: considering %d %s")
221 - % _cur % backends[_cur]->be_name));
222 -
223238 if (_cur >= backends.size())
224239 _cur = 0;
225240
Index: trunk/willow/willow.conf.example
@@ -52,7 +52,13 @@
5353 port = 8081;
5454 };
5555
56 -backend-group "backup" {
 56+/* A place to send requests for which no backend was found */
 57+backend-group "failover";
 58+backend "failover.mycompany.com" {
 59+ group = "failover";
 60+};
 61+
 62+backend-group "primary" {
5763 /*
5864 * lb-type can be "rr" (plain roundrobin), "carp" (use CARP
5965 * algorithm) or "carp-host" (use CARP but only consider the
@@ -65,6 +71,12 @@
6672 * matter which listener received the request.
6773 */
6874 hosts = "www.bar.com", "www2.bar.com";
 75+
 76+ /*
 77+ * If all backends in the group are down, send requests to this
 78+ * group instead.
 79+ */
 80+ failover-group = "failover";
6981 };
7082
7183 backend "backends.mycompany.com" {
@@ -79,7 +91,7 @@
8092 * also has this group, requests to that listener will only
8193 * go to backends in that group.
8294 */
83 - group = "backup";
 95+ group = "primary";
8496 };
8597
8698 /*