| | varnish-cache/vmod/vmod_directors_random.c |
0 |
|
/*- |
1 |
|
* Copyright (c) 2013-2015 Varnish Software AS |
2 |
|
* All rights reserved. |
3 |
|
* |
4 |
|
* Author: Poul-Henning Kamp <phk@FreeBSD.org> |
5 |
|
* |
6 |
|
* SPDX-License-Identifier: BSD-2-Clause |
7 |
|
* |
8 |
|
* Redistribution and use in source and binary forms, with or without |
9 |
|
* modification, are permitted provided that the following conditions |
10 |
|
* are met: |
11 |
|
* 1. Redistributions of source code must retain the above copyright |
12 |
|
* notice, this list of conditions and the following disclaimer. |
13 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
14 |
|
* notice, this list of conditions and the following disclaimer in the |
15 |
|
* documentation and/or other materials provided with the distribution. |
16 |
|
* |
17 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
18 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
19 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
20 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
21 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
22 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
23 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
24 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
25 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
26 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
27 |
|
* SUCH DAMAGE. |
28 |
|
*/ |
29 |
|
|
30 |
|
#include "config.h" |
31 |
|
|
32 |
|
#include <stdlib.h> |
33 |
|
|
34 |
|
#include "cache/cache.h" |
35 |
|
|
36 |
|
#include "vbm.h" |
37 |
|
#include "vrnd.h" |
38 |
|
|
39 |
|
#include "vmod_directors.h" |
40 |
|
|
41 |
|
#include "vcc_directors_if.h" |
42 |
|
|
43 |
|
struct vmod_directors_random { |
44 |
|
unsigned magic; |
45 |
|
#define VMOD_DIRECTORS_RANDOM_MAGIC 0x4732d092 |
46 |
|
struct vdir *vd; |
47 |
|
}; |
48 |
|
|
49 |
|
static VCL_BOOL v_matchproto_(vdi_healthy) |
50 |
28 |
vmod_random_healthy(VRT_CTX, VCL_BACKEND dir, VCL_TIME *changed) |
51 |
|
{ |
52 |
|
struct vmod_directors_random *rr; |
53 |
|
|
54 |
28 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
55 |
28 |
CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC); |
56 |
28 |
CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_RANDOM_MAGIC); |
57 |
28 |
return (vdir_any_healthy(ctx, rr->vd, changed)); |
58 |
|
} |
59 |
|
|
60 |
|
static void v_matchproto_(vdi_list_f) |
61 |
224 |
vmod_random_list(VRT_CTX, VCL_BACKEND dir, struct vsb *vsb, int pflag, |
62 |
|
int jflag) |
63 |
|
{ |
64 |
|
struct vmod_directors_random *rr; |
65 |
|
|
66 |
224 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
67 |
224 |
CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC); |
68 |
224 |
CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_RANDOM_MAGIC); |
69 |
224 |
vdir_list(ctx, rr->vd, vsb, pflag, jflag, 1); |
70 |
224 |
} |
71 |
|
|
72 |
|
static VCL_BACKEND v_matchproto_(vdi_resolve_f) |
73 |
504 |
vmod_random_resolve(VRT_CTX, VCL_BACKEND dir) |
74 |
|
{ |
75 |
|
struct vmod_directors_random *rr; |
76 |
|
VCL_BACKEND be; |
77 |
|
double r; |
78 |
|
|
79 |
504 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
80 |
504 |
CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC); |
81 |
504 |
CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_RANDOM_MAGIC); |
82 |
504 |
r = scalbn(VRND_RandomTestable(), -31); |
83 |
504 |
assert(r >= 0 && r < 1.0); |
84 |
504 |
be = vdir_pick_be(ctx, rr->vd, r); |
85 |
504 |
return (be); |
86 |
|
} |
87 |
|
|
88 |
|
static void v_matchproto_(vdi_release_f) |
89 |
14 |
vmod_random_release(VCL_BACKEND dir) |
90 |
|
{ |
91 |
|
struct vmod_directors_random *rr; |
92 |
|
|
93 |
14 |
CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC); |
94 |
14 |
CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_RANDOM_MAGIC); |
95 |
14 |
vdir_release(rr->vd); |
96 |
14 |
} |
97 |
|
|
98 |
|
static void v_matchproto_(vdi_destroy_f) |
99 |
14 |
vmod_random_destroy(VCL_BACKEND dir) |
100 |
|
{ |
101 |
|
struct vmod_directors_random *rr; |
102 |
|
|
103 |
14 |
CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC); |
104 |
14 |
CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_RANDOM_MAGIC); |
105 |
14 |
vdir_delete(&rr->vd); |
106 |
14 |
FREE_OBJ(rr); |
107 |
14 |
} |
108 |
|
|
109 |
|
static const struct vdi_methods vmod_random_methods[1] = {{ |
110 |
|
.magic = VDI_METHODS_MAGIC, |
111 |
|
.type = "random", |
112 |
|
.healthy = vmod_random_healthy, |
113 |
|
.resolve = vmod_random_resolve, |
114 |
|
.release = vmod_random_release, |
115 |
|
.destroy = vmod_random_destroy, |
116 |
|
.list = vmod_random_list |
117 |
|
}}; |
118 |
|
|
119 |
|
|
120 |
|
VCL_VOID v_matchproto_() |
121 |
126 |
vmod_random__init(VRT_CTX, struct vmod_directors_random **rrp, |
122 |
|
const char *vcl_name) |
123 |
|
{ |
124 |
|
struct vmod_directors_random *rr; |
125 |
|
|
126 |
126 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
127 |
126 |
AN(rrp); |
128 |
126 |
AZ(*rrp); |
129 |
126 |
ALLOC_OBJ(rr, VMOD_DIRECTORS_RANDOM_MAGIC); |
130 |
126 |
AN(rr); |
131 |
126 |
*rrp = rr; |
132 |
126 |
vdir_new(ctx, &rr->vd, vcl_name, vmod_random_methods, rr); |
133 |
126 |
} |
134 |
|
|
135 |
|
VCL_VOID v_matchproto_() |
136 |
14 |
vmod_random__fini(struct vmod_directors_random **rrp) |
137 |
|
{ |
138 |
|
struct vmod_directors_random *rr; |
139 |
|
|
140 |
14 |
TAKE_OBJ_NOTNULL(rr, rrp, VMOD_DIRECTORS_RANDOM_MAGIC); |
141 |
14 |
VRT_DelDirector(&rr->vd->dir); |
142 |
14 |
} |
143 |
|
|
144 |
|
VCL_VOID v_matchproto_() |
145 |
364 |
vmod_random_add_backend(VRT_CTX, |
146 |
|
struct vmod_directors_random *rr, VCL_BACKEND be, double w) |
147 |
|
{ |
148 |
|
|
149 |
364 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
150 |
364 |
CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_RANDOM_MAGIC); |
151 |
364 |
vdir_add_backend(ctx, rr->vd, be, w); |
152 |
364 |
} |
153 |
|
|
154 |
14 |
VCL_VOID vmod_random_remove_backend(VRT_CTX, |
155 |
|
struct vmod_directors_random *rr, VCL_BACKEND be) v_matchproto_() |
156 |
|
{ |
157 |
14 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
158 |
14 |
CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_RANDOM_MAGIC); |
159 |
14 |
vdir_remove_backend(ctx, rr->vd, be, NULL); |
160 |
14 |
} |
161 |
|
|
162 |
|
VCL_BACKEND v_matchproto_() |
163 |
532 |
vmod_random_backend(VRT_CTX, struct vmod_directors_random *rr) |
164 |
|
{ |
165 |
532 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
166 |
532 |
CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_RANDOM_MAGIC); |
167 |
532 |
return (rr->vd->dir); |
168 |
|
} |