Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 248 lines (194 sloc) 5.371 kb
66d088f @alobbs Implements a new 'Failover' load balancing plug-in. Implements RFE
alobbs authored
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 /* Cherokee
4 *
5 * Authors:
6 * Alvaro Lopez Ortega <alvaro@alobbs.com>
7 *
920e20b @alobbs Happy new year!
alobbs authored
8 * Copyright (C) 2001-2011 Alvaro Lopez Ortega
66d088f @alobbs Implements a new 'Failover' load balancing plug-in. Implements RFE
alobbs authored
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * 02110-1301, USA.
23 */
24
25 #include "common-internal.h"
26
27 #include "balancer_failover.h"
28 #include "plugin_loader.h"
29 #include "bogotime.h"
30 #include "connection-protected.h"
31 #include "util.h"
32
33 #define ENTRIES "balancer,iphash"
34
35
36 /* Plug-in initialization
37 */
38 PLUGIN_INFO_BALANCER_EASIEST_INIT (failover);
39
40
41 ret_t
42 cherokee_balancer_failover_configure (cherokee_balancer_t *balancer,
43 cherokee_server_t *srv,
44 cherokee_config_node_t *conf)
45 {
46 ret_t ret;
47
48 /* Configure the generic balancer
49 */
50 ret = cherokee_balancer_configure_base (balancer, srv, conf);
51 if (ret != ret_ok)
52 return ret;
53
54 /* Sanity check
55 */
56 if (balancer->entries_len <= 0) {
57 LOG_CRITICAL_S (CHEROKEE_ERROR_BALANCER_EMPTY);
58 return ret_error;
59 }
60
61 return ret_ok;
62 }
63
64
65 static ret_t
e248c08 @alobbs Balancer 'failover' clean up.
alobbs authored
66 reactivate_entry_guts (cherokee_balancer_failover_t *balancer,
67 cherokee_balancer_entry_t *entry)
66d088f @alobbs Implements a new 'Failover' load balancing plug-in. Implements RFE
alobbs authored
68 {
69 /* balancer->mutex is LOCKED
70 */
71 UNUSED (balancer);
72
73 /* Disable
74 */
75 if (entry->disabled == false)
76 return ret_ok;
77
78 entry->disabled = false;
e248c08 @alobbs Balancer 'failover' clean up.
alobbs authored
79 return ret_ok;
80 }
81
82
83 static ret_t
84 reactivate_entry (cherokee_balancer_failover_t *balancer,
85 cherokee_balancer_entry_t *entry)
86 {
87 /* balancer->mutex is LOCKED
88 */
89 ret_t ret;
90 cherokee_buffer_t tmp = CHEROKEE_BUF_INIT;
91
92 /* Reactivate
93 */
94 ret = reactivate_entry_guts (balancer, entry);
66d088f @alobbs Implements a new 'Failover' load balancing plug-in. Implements RFE
alobbs authored
95
96 /* Notify
97 */
98 cherokee_source_copy_name (entry->source, &tmp);
99 LOG_WARNING (CHEROKEE_ERROR_BALANCER_FAILOVER_REACTIVE, tmp.buf);
100 cherokee_buffer_mrproper (&tmp);
101
e248c08 @alobbs Balancer 'failover' clean up.
alobbs authored
102 return ret;
66d088f @alobbs Implements a new 'Failover' load balancing plug-in. Implements RFE
alobbs authored
103 }
104
105 static ret_t
106 reactivate_all_entries (cherokee_balancer_failover_t *balancer)
107 {
108 cherokee_list_t *i;
109
110 list_for_each (i, &BAL(balancer)->entries) {
e248c08 @alobbs Balancer 'failover' clean up.
alobbs authored
111 reactivate_entry_guts (balancer, BAL_ENTRY(i));
66d088f @alobbs Implements a new 'Failover' load balancing plug-in. Implements RFE
alobbs authored
112 }
113
e248c08 @alobbs Balancer 'failover' clean up.
alobbs authored
114 LOG_WARNING_S (CHEROKEE_ERROR_BALANCER_FAILOVER_ENABLE_ALL);
66d088f @alobbs Implements a new 'Failover' load balancing plug-in. Implements RFE
alobbs authored
115 return ret_ok;
116 }
117
118
119 static ret_t
120 report_fail (cherokee_balancer_failover_t *balancer,
121 cherokee_connection_t *conn,
122 cherokee_source_t *src)
123 {
124 ret_t ret;
125 cherokee_list_t *i;
126 cherokee_balancer_entry_t *entry;
76d368b @alobbs "'entry' might be used uninitialized in this function." (klocwork.com
alobbs authored
127 cherokee_buffer_t tmp = CHEROKEE_BUF_INIT;
66d088f @alobbs Implements a new 'Failover' load balancing plug-in. Implements RFE
alobbs authored
128
129 UNUSED(conn);
130
131 CHEROKEE_MUTEX_LOCK (&balancer->mutex);
132
133 list_for_each (i, &BAL(balancer)->entries) {
134 entry = BAL_ENTRY(i);
135
136 /* Find the right source
137 */
138 if (entry->source != src)
139 continue;
140
141 if (entry->disabled) {
142 ret = ret_ok;
143 goto out;
144 }
145
146 /* Disable the source
147 */
148 entry->disabled = true;
149 entry->disabled_until = cherokee_bogonow_now + BAL_DISABLE_TIMEOUT;
150
151 /* Notify what has happened
152 */
153 cherokee_source_copy_name (entry->source, &tmp);
154 LOG_WARNING (CHEROKEE_ERROR_BALANCER_FAILOVER_DISABLE, tmp.buf);
155 cherokee_buffer_mrproper (&tmp);
156
157 CHEROKEE_MUTEX_UNLOCK (&balancer->mutex);
158 return ret_ok;
159 }
160
161 ret = ret_error;
162 SHOULDNT_HAPPEN;
163
164 out:
165 CHEROKEE_MUTEX_UNLOCK (&balancer->mutex);
166 return ret;
167 }
168
169
170 static ret_t
171 dispatch (cherokee_balancer_failover_t *balancer,
172 cherokee_connection_t *conn,
173 cherokee_source_t **src)
174 {
175 cherokee_list_t *i;
76d368b @alobbs "'entry' might be used uninitialized in this function." (klocwork.com
alobbs authored
176 cherokee_balancer_entry_t *entry = NULL;
177 cherokee_balancer_t *gbal = BAL(balancer);
66d088f @alobbs Implements a new 'Failover' load balancing plug-in. Implements RFE
alobbs authored
178
179 UNUSED(conn);
180 CHEROKEE_MUTEX_LOCK (&balancer->mutex);
181
182 /* Pick the first available source
183 */
184 list_for_each (i, &gbal->entries) {
185 entry = BAL_ENTRY(i);
186
187 /* Active */
188 if (! entry->disabled) {
189 break;
190 }
191
192 /* Reactive? */
193 if (cherokee_bogonow_now >= entry->disabled_until) {
194 reactivate_entry (balancer, entry);
195 break;
196 }
197
198 entry = NULL;
199 }
200
201 /* No source, reactive all, and take first
202 */
203 if (! entry) {
204 reactivate_all_entries (balancer);
205 entry = BAL_ENTRY(gbal->entries.next);
206 }
207
208 /* Return source
209 */
210 *src = entry->source;
211
212 CHEROKEE_MUTEX_UNLOCK (&balancer->mutex);
213 return ret_ok;
214 }
215
216
217 ret_t
218 cherokee_balancer_failover_new (cherokee_balancer_t **bal)
219 {
220 CHEROKEE_NEW_STRUCT (n, balancer_failover);
221
222 /* Init
223 */
224 cherokee_balancer_init_base (BAL(n), PLUGIN_INFO_PTR(failover));
225
226 MODULE(n)->free = (module_func_free_t) cherokee_balancer_failover_free;
227 BAL(n)->configure = (balancer_configure_func_t) cherokee_balancer_failover_configure;
228 BAL(n)->dispatch = (balancer_dispatch_func_t) dispatch;
229 BAL(n)->report_fail = (balancer_report_fail_func_t) report_fail;
230
231 /* Init properties
232 */
233 CHEROKEE_MUTEX_INIT (&n->mutex, CHEROKEE_MUTEX_FAST);
234
235 /* Return obj
236 */
237 *bal = BAL(n);
238 return ret_ok;
239 }
240
241
242 ret_t
243 cherokee_balancer_failover_free (cherokee_balancer_failover_t *balancer)
244 {
245 CHEROKEE_MUTEX_DESTROY (&balancer->mutex);
246 return ret_ok;
247 }
Something went wrong with that request. Please try again.