| | varnish-cache/vmod/vmod_directors.c |
0 |
|
/*- |
1 |
|
* Copyright (c) 2013-2015 Varnish Software AS |
2 |
|
* Copyright 2019 UPLEX - Nils Goroll Systemoptimierung |
3 |
|
* All rights reserved. |
4 |
|
* |
5 |
|
* Author: Poul-Henning Kamp <phk@FreeBSD.org> |
6 |
|
* Author: Nils Goroll <nils.goroll@uplex.de> |
7 |
|
* |
8 |
|
* SPDX-License-Identifier: BSD-2-Clause |
9 |
|
* |
10 |
|
* Redistribution and use in source and binary forms, with or without |
11 |
|
* modification, are permitted provided that the following conditions |
12 |
|
* are met: |
13 |
|
* 1. Redistributions of source code must retain the above copyright |
14 |
|
* notice, this list of conditions and the following disclaimer. |
15 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
16 |
|
* notice, this list of conditions and the following disclaimer in the |
17 |
|
* documentation and/or other materials provided with the distribution. |
18 |
|
* |
19 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
20 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
21 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
22 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
23 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
24 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
25 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
26 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
27 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
28 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
29 |
|
* SUCH DAMAGE. |
30 |
|
*/ |
31 |
|
|
32 |
|
#include "config.h" |
33 |
|
|
34 |
|
#include <stdlib.h> |
35 |
|
#include <stdio.h> |
36 |
|
|
37 |
|
#include "cache/cache.h" |
38 |
|
|
39 |
|
#include "vbm.h" |
40 |
|
#include "vcl.h" |
41 |
|
#include "vsb.h" |
42 |
|
|
43 |
|
#include "vcc_directors_if.h" |
44 |
|
|
45 |
|
#include "vmod_directors.h" |
46 |
|
|
47 |
|
VCL_BACKEND |
48 |
40 |
VPFX(lookup)(VRT_CTX, VCL_STRING name) |
49 |
|
{ |
50 |
40 |
AN(ctx->method & VCL_MET_TASK_H); |
51 |
40 |
return (VRT_LookupDirector(ctx, name)); |
52 |
|
} |
53 |
|
|
54 |
|
static void |
55 |
1320 |
vdir_expand(struct vdir *vd, unsigned n) |
56 |
|
{ |
57 |
1320 |
CHECK_OBJ_NOTNULL(vd, VDIR_MAGIC); |
58 |
|
|
59 |
1320 |
vd->backend = realloc(vd->backend, n * sizeof *vd->backend); |
60 |
1320 |
AN(vd->backend); |
61 |
1320 |
vd->weight = realloc(vd->weight, n * sizeof *vd->weight); |
62 |
1320 |
AN(vd->weight); |
63 |
1320 |
if (n > vd->healthy->nbits) |
64 |
0 |
vbit_expand(vd->healthy, n); |
65 |
1320 |
AN(vd->healthy); |
66 |
1320 |
vd->l_backend = n; |
67 |
1320 |
} |
68 |
|
|
69 |
|
void |
70 |
1440 |
vdir_new(VRT_CTX, struct vdir **vdp, const char *vcl_name, |
71 |
|
const struct vdi_methods *m, void *priv) |
72 |
|
{ |
73 |
|
struct vdir *vd; |
74 |
|
|
75 |
1440 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
76 |
1440 |
CHECK_OBJ_NOTNULL(m, VDI_METHODS_MAGIC); |
77 |
1440 |
AN(vcl_name); |
78 |
1440 |
AN(vdp); |
79 |
1440 |
AZ(*vdp); |
80 |
1440 |
ALLOC_OBJ(vd, VDIR_MAGIC); |
81 |
1440 |
AN(vd); |
82 |
1440 |
*vdp = vd; |
83 |
1440 |
PTOK(pthread_rwlock_init(&vd->mtx, NULL)); |
84 |
1440 |
vd->dir = VRT_AddDirector(ctx, m, priv, "%s", vcl_name); |
85 |
1440 |
vd->healthy = vbit_new(8); |
86 |
1440 |
AN(vd->healthy); |
87 |
1440 |
} |
88 |
|
|
89 |
|
void |
90 |
320 |
vdir_release(struct vdir *vd) |
91 |
|
{ |
92 |
|
unsigned u; |
93 |
|
|
94 |
320 |
CHECK_OBJ_NOTNULL(vd, VDIR_MAGIC); |
95 |
|
|
96 |
320 |
vdir_wrlock(vd); |
97 |
800 |
for (u = 0; u < vd->n_backend; u++) |
98 |
480 |
VRT_Assign_Backend(&vd->backend[u], NULL); |
99 |
320 |
vd->n_backend = 0; |
100 |
320 |
vdir_unlock(vd); |
101 |
320 |
} |
102 |
|
|
103 |
|
void |
104 |
320 |
vdir_delete(struct vdir **vdp) |
105 |
|
{ |
106 |
|
struct vdir *vd; |
107 |
|
|
108 |
320 |
TAKE_OBJ_NOTNULL(vd, vdp, VDIR_MAGIC); |
109 |
|
|
110 |
320 |
AZ(vd->dir); |
111 |
320 |
AZ(vd->n_backend); |
112 |
320 |
free(vd->backend); |
113 |
320 |
free(vd->weight); |
114 |
320 |
PTOK(pthread_rwlock_destroy(&vd->mtx)); |
115 |
320 |
vbit_destroy(vd->healthy); |
116 |
320 |
FREE_OBJ(vd); |
117 |
320 |
} |
118 |
|
|
119 |
|
void |
120 |
6280 |
vdir_rdlock(struct vdir *vd) |
121 |
|
{ |
122 |
6280 |
CHECK_OBJ_NOTNULL(vd, VDIR_MAGIC); |
123 |
6280 |
PTOK(pthread_rwlock_rdlock(&vd->mtx)); |
124 |
6280 |
} |
125 |
|
|
126 |
|
void |
127 |
7240 |
vdir_wrlock(struct vdir *vd) |
128 |
|
{ |
129 |
7240 |
CHECK_OBJ_NOTNULL(vd, VDIR_MAGIC); |
130 |
7240 |
PTOK(pthread_rwlock_wrlock(&vd->mtx)); |
131 |
7240 |
} |
132 |
|
|
133 |
|
void |
134 |
13520 |
vdir_unlock(struct vdir *vd) |
135 |
|
{ |
136 |
13520 |
CHECK_OBJ_NOTNULL(vd, VDIR_MAGIC); |
137 |
13520 |
PTOK(pthread_rwlock_unlock(&vd->mtx)); |
138 |
13520 |
} |
139 |
|
|
140 |
|
|
141 |
|
void |
142 |
3600 |
vdir_add_backend(VRT_CTX, struct vdir *vd, VCL_BACKEND be, double weight) |
143 |
|
{ |
144 |
|
unsigned u; |
145 |
|
|
146 |
3600 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
147 |
3600 |
CHECK_OBJ_NOTNULL(vd, VDIR_MAGIC); |
148 |
3600 |
if (be == NULL) { |
149 |
240 |
VRT_fail(ctx, "%s: None backend cannot be added", |
150 |
120 |
VRT_BACKEND_string(vd->dir)); |
151 |
120 |
return; |
152 |
|
} |
153 |
3480 |
AN(be); |
154 |
3480 |
vdir_wrlock(vd); |
155 |
3480 |
if (vd->n_backend >= vd->l_backend) |
156 |
1320 |
vdir_expand(vd, vd->l_backend + 16); |
157 |
3480 |
assert(vd->n_backend < vd->l_backend); |
158 |
3480 |
u = vd->n_backend++; |
159 |
3480 |
vd->backend[u] = NULL; |
160 |
3480 |
VRT_Assign_Backend(&vd->backend[u], be); |
161 |
3480 |
vd->weight[u] = weight; |
162 |
3480 |
vdir_unlock(vd); |
163 |
3600 |
} |
164 |
|
|
165 |
|
void |
166 |
480 |
vdir_remove_backend(VRT_CTX, struct vdir *vd, VCL_BACKEND be, unsigned *cur) |
167 |
|
{ |
168 |
|
unsigned u, n; |
169 |
|
|
170 |
480 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
171 |
480 |
CHECK_OBJ_NOTNULL(vd, VDIR_MAGIC); |
172 |
480 |
if (be == NULL) { |
173 |
0 |
VRT_fail(ctx, "%s: None backend cannot be removed", |
174 |
0 |
VRT_BACKEND_string(vd->dir)); |
175 |
0 |
return; |
176 |
|
} |
177 |
480 |
CHECK_OBJ(be, DIRECTOR_MAGIC); |
178 |
480 |
vdir_wrlock(vd); |
179 |
680 |
for (u = 0; u < vd->n_backend; u++) |
180 |
680 |
if (vd->backend[u] == be) |
181 |
480 |
break; |
182 |
480 |
if (u == vd->n_backend) { |
183 |
0 |
vdir_unlock(vd); |
184 |
0 |
return; |
185 |
|
} |
186 |
480 |
VRT_Assign_Backend(&vd->backend[u], NULL); |
187 |
480 |
n = (vd->n_backend - u) - 1; |
188 |
480 |
memmove(&vd->backend[u], &vd->backend[u+1], n * sizeof(vd->backend[0])); |
189 |
480 |
memmove(&vd->weight[u], &vd->weight[u+1], n * sizeof(vd->weight[0])); |
190 |
480 |
vd->n_backend--; |
191 |
|
|
192 |
480 |
if (cur) { |
193 |
160 |
assert(*cur <= vd->n_backend); |
194 |
160 |
if (u < *cur) |
195 |
40 |
(*cur)--; |
196 |
120 |
else if (*cur == vd->n_backend) |
197 |
40 |
*cur = 0; |
198 |
160 |
} |
199 |
480 |
vdir_unlock(vd); |
200 |
480 |
} |
201 |
|
|
202 |
|
VCL_BOOL |
203 |
1960 |
vdir_any_healthy(VRT_CTX, struct vdir *vd, VCL_TIME *changed) |
204 |
|
{ |
205 |
1960 |
unsigned retval = 0; |
206 |
|
VCL_BACKEND be; |
207 |
|
unsigned u; |
208 |
|
vtim_real c; |
209 |
|
|
210 |
1960 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
211 |
1960 |
CHECK_OBJ_NOTNULL(vd, VDIR_MAGIC); |
212 |
1960 |
vdir_rdlock(vd); |
213 |
1960 |
if (changed != NULL) |
214 |
400 |
*changed = 0; |
215 |
2800 |
for (u = 0; u < vd->n_backend; u++) { |
216 |
2520 |
be = vd->backend[u]; |
217 |
2520 |
CHECK_OBJ_NOTNULL(be, DIRECTOR_MAGIC); |
218 |
2520 |
retval = VRT_Healthy(ctx, be, &c); |
219 |
2520 |
if (changed != NULL && c > *changed) |
220 |
520 |
*changed = c; |
221 |
2520 |
if (retval) |
222 |
1680 |
break; |
223 |
840 |
} |
224 |
1960 |
vdir_unlock(vd); |
225 |
1960 |
return (retval); |
226 |
|
} |
227 |
|
|
228 |
|
void |
229 |
2080 |
vdir_list(VRT_CTX, struct vdir *vd, struct vsb *vsb, int pflag, int jflag, |
230 |
|
int weight) |
231 |
|
{ |
232 |
|
VCL_BACKEND be; |
233 |
|
VCL_BOOL h; |
234 |
|
unsigned u, nh; |
235 |
|
double w; |
236 |
|
|
237 |
2080 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
238 |
2080 |
CHECK_OBJ_NOTNULL(vd, VDIR_MAGIC); |
239 |
|
|
240 |
2080 |
if (pflag) { |
241 |
320 |
if (jflag) { |
242 |
160 |
VSB_cat(vsb, "{\n"); |
243 |
160 |
VSB_indent(vsb, 2); |
244 |
160 |
if (weight) |
245 |
80 |
VSB_printf(vsb, "\"total_weight\": %f,\n", |
246 |
40 |
vd->total_weight); |
247 |
160 |
VSB_cat(vsb, "\"backends\": {\n"); |
248 |
160 |
VSB_indent(vsb, 2); |
249 |
160 |
} else { |
250 |
160 |
VSB_cat(vsb, "\n\n\tBackend\tWeight\tHealth\n"); |
251 |
|
} |
252 |
320 |
} |
253 |
|
|
254 |
2080 |
vdir_rdlock(vd); |
255 |
2080 |
vdir_update_health(ctx, vd); |
256 |
2960 |
for (u = 0; pflag && u < vd->n_backend; u++) { |
257 |
880 |
be = vd->backend[u]; |
258 |
880 |
CHECK_OBJ_NOTNULL(be, DIRECTOR_MAGIC); |
259 |
|
|
260 |
880 |
h = vbit_test(vd->healthy, u); |
261 |
|
|
262 |
880 |
w = h ? vd->weight[u] : 0.0; |
263 |
|
|
264 |
880 |
if (jflag) { |
265 |
440 |
if (u) |
266 |
280 |
VSB_cat(vsb, ",\n"); |
267 |
440 |
VSB_printf(vsb, "\"%s\": {\n", be->vcl_name); |
268 |
440 |
VSB_indent(vsb, 2); |
269 |
|
|
270 |
440 |
if (weight) |
271 |
160 |
VSB_printf(vsb, "\"weight\": %f,\n", w); |
272 |
|
|
273 |
440 |
if (h) |
274 |
320 |
VSB_cat(vsb, "\"health\": \"healthy\"\n"); |
275 |
|
else |
276 |
120 |
VSB_cat(vsb, "\"health\": \"sick\"\n"); |
277 |
|
|
278 |
440 |
VSB_indent(vsb, -2); |
279 |
440 |
VSB_cat(vsb, "}"); |
280 |
440 |
} else { |
281 |
440 |
VSB_cat(vsb, "\t"); |
282 |
440 |
VSB_cat(vsb, be->vcl_name); |
283 |
440 |
if (weight) |
284 |
320 |
VSB_printf(vsb, "\t%6.2f%%\t", |
285 |
160 |
100 * w / vd->total_weight); |
286 |
|
else |
287 |
280 |
VSB_cat(vsb, "\t-\t"); |
288 |
440 |
VSB_cat(vsb, h ? "healthy" : "sick"); |
289 |
440 |
VSB_cat(vsb, "\n"); |
290 |
|
} |
291 |
880 |
} |
292 |
2080 |
nh = vd->n_healthy; |
293 |
2080 |
u = vd->n_backend; |
294 |
2080 |
vdir_unlock(vd); |
295 |
|
|
296 |
2080 |
if (jflag && (pflag)) { |
297 |
160 |
VSB_cat(vsb, "\n"); |
298 |
160 |
VSB_indent(vsb, -2); |
299 |
160 |
VSB_cat(vsb, "}\n"); |
300 |
160 |
VSB_indent(vsb, -2); |
301 |
160 |
VSB_cat(vsb, "},\n"); |
302 |
160 |
} |
303 |
|
|
304 |
2080 |
if (pflag) |
305 |
320 |
return; |
306 |
|
|
307 |
1760 |
if (jflag) |
308 |
800 |
VSB_printf(vsb, "[%u, %u, \"%s\"]", nh, u, |
309 |
400 |
nh ? "healthy" : "sick"); |
310 |
|
else |
311 |
1360 |
VSB_printf(vsb, "%u/%u\t%s", nh, u, nh ? "healthy" : "sick"); |
312 |
2080 |
} |
313 |
|
|
314 |
|
/* |
315 |
|
* iterate backends and update |
316 |
|
* - healthy bitmap |
317 |
|
* - number of healthy backends |
318 |
|
* - total_weight |
319 |
|
* - last change time of the VCL_BACKEND |
320 |
|
* |
321 |
|
* must be called under the vdir lock (read or write). |
322 |
|
* |
323 |
|
* A write lock is required if consistency between the individual attributes is |
324 |
|
* a must, e.g. when total_weight is required to be the exact sum of the weights |
325 |
|
* |
326 |
|
* The read lock is safe because add_backend expands the healthy bitmap and all |
327 |
|
* other members are atomic and may be used if consistency is not required. |
328 |
|
*/ |
329 |
|
void |
330 |
4480 |
vdir_update_health(VRT_CTX, struct vdir *vd) |
331 |
|
{ |
332 |
4480 |
VCL_TIME c, changed = 0; |
333 |
|
VCL_BOOL h; |
334 |
|
VCL_BACKEND be; |
335 |
4480 |
unsigned u, nh = 0; |
336 |
4480 |
double tw = 0.0; |
337 |
|
struct vbitmap *healthy; |
338 |
|
|
339 |
4480 |
CHECK_OBJ_NOTNULL(vd, VDIR_MAGIC); |
340 |
4480 |
healthy = vd->healthy; |
341 |
14960 |
for (u = 0; u < vd->n_backend; u++) { |
342 |
10480 |
be = vd->backend[u]; |
343 |
10480 |
CHECK_OBJ_NOTNULL(be, DIRECTOR_MAGIC); |
344 |
10480 |
c = 0; |
345 |
10480 |
h = VRT_Healthy(ctx, vd->backend[u], &c); |
346 |
10480 |
if (h) { |
347 |
8120 |
nh++; |
348 |
8120 |
tw += vd->weight[u]; |
349 |
8120 |
} |
350 |
10480 |
if (c > changed) |
351 |
10080 |
changed = c; |
352 |
10480 |
if (h != vbit_test(healthy, u)) { |
353 |
5360 |
if (h) |
354 |
5280 |
vbit_set(healthy, u); |
355 |
|
else |
356 |
80 |
vbit_clr(healthy, u); |
357 |
5360 |
} |
358 |
10480 |
} |
359 |
4480 |
VRT_SetChanged(vd->dir, changed); |
360 |
4480 |
vd->total_weight = tw; |
361 |
4480 |
vd->n_healthy = nh; |
362 |
4480 |
} |
363 |
|
|
364 |
|
static unsigned |
365 |
1840 |
vdir_pick_by_weight(const struct vdir *vd, double w) |
366 |
|
{ |
367 |
1840 |
const struct vbitmap *healthy = vd->healthy; |
368 |
1840 |
double a = 0.0; |
369 |
|
unsigned u; |
370 |
|
|
371 |
1840 |
AN(healthy); |
372 |
2802 |
for (u = 0; u < vd->n_backend; u++) { |
373 |
2802 |
if (! vbit_test(healthy, u)) |
374 |
160 |
continue; |
375 |
2642 |
a += vd->weight[u]; |
376 |
2642 |
if (w < a) |
377 |
1840 |
return (u); |
378 |
802 |
} |
379 |
0 |
WRONG(""); |
380 |
|
} |
381 |
|
|
382 |
|
VCL_BACKEND |
383 |
1880 |
vdir_pick_be(VRT_CTX, struct vdir *vd, double w) |
384 |
|
{ |
385 |
|
unsigned u; |
386 |
1880 |
VCL_BACKEND be = NULL; |
387 |
|
|
388 |
1880 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
389 |
1880 |
CHECK_OBJ_NOTNULL(vd, VDIR_MAGIC); |
390 |
1880 |
vdir_wrlock(vd); |
391 |
1880 |
vdir_update_health(ctx, vd); |
392 |
1880 |
if (vd->total_weight > 0.0) { |
393 |
1840 |
u = vdir_pick_by_weight(vd, w * vd->total_weight); |
394 |
1840 |
assert(u < vd->n_backend); |
395 |
1840 |
be = vd->backend[u]; |
396 |
1840 |
CHECK_OBJ_NOTNULL(be, DIRECTOR_MAGIC); |
397 |
1840 |
} |
398 |
1880 |
vdir_unlock(vd); |
399 |
1880 |
return (be); |
400 |
|
} |