varnish-cache/lib/libvmod_directors/random.c
1
/*-
2
 * Copyright (c) 2013-2015 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@FreeBSD.org>
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 */
28
29
#include "config.h"
30
31
#include <stdlib.h>
32
33
#include "cache/cache.h"
34
#include "cache/cache_director.h"
35
36
#include "vbm.h"
37
#include "vrnd.h"
38
39
#include "vdir.h"
40
41
#include "vcc_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 unsigned v_matchproto_(vdi_healthy)
50 0
vmod_random_healthy(const struct director *dir, const struct busyobj *bo,
51
    double *changed)
52
{
53
        struct vmod_directors_random *rr;
54
55 0
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_RANDOM_MAGIC);
56 0
        return (vdir_any_healthy(rr->vd, bo, changed));
57
}
58
59
static const struct director * v_matchproto_(vdi_resolve_f)
60 70
vmod_random_resolve(const struct director *dir, struct worker *wrk,
61
    struct busyobj *bo)
62
{
63
        struct vmod_directors_random *rr;
64
        VCL_BACKEND be;
65
        double r;
66
67 70
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
68 70
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
69 70
        CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
70 70
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_RANDOM_MAGIC);
71 70
        r = scalbn(VRND_RandomTestable(), -31);
72 70
        assert(r >= 0 && r < 1.0);
73 70
        be = vdir_pick_be(rr->vd, r, bo);
74 70
        return (be);
75
}
76
77
VCL_VOID v_matchproto_()
78 16
vmod_random__init(VRT_CTX, struct vmod_directors_random **rrp,
79
    const char *vcl_name)
80
{
81
        struct vmod_directors_random *rr;
82
83 16
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
84 16
        AN(rrp);
85 16
        AZ(*rrp);
86 16
        ALLOC_OBJ(rr, VMOD_DIRECTORS_RANDOM_MAGIC);
87 16
        AN(rr);
88 16
        *rrp = rr;
89 16
        vdir_new(&rr->vd, "random", vcl_name, vmod_random_healthy,
90
            vmod_random_resolve, rr);
91 16
}
92
93
VCL_VOID v_matchproto_()
94 2
vmod_random__fini(struct vmod_directors_random **rrp)
95
{
96
        struct vmod_directors_random *rr;
97
98 2
        rr = *rrp;
99 2
        *rrp = NULL;
100 2
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_RANDOM_MAGIC);
101 2
        vdir_delete(&rr->vd);
102 2
        FREE_OBJ(rr);
103 2
}
104
105
VCL_VOID v_matchproto_()
106 44
vmod_random_add_backend(VRT_CTX,
107
    struct vmod_directors_random *rr, VCL_BACKEND be, double w)
108
{
109
110 44
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
111 44
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_RANDOM_MAGIC);
112 44
        vdir_add_backend(ctx, rr->vd, be, w);
113 44
}
114
115 2
VCL_VOID vmod_random_remove_backend(VRT_CTX,
116
    struct vmod_directors_random *rr, VCL_BACKEND be) v_matchproto_()
117
{
118 2
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
119 2
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_RANDOM_MAGIC);
120 2
        vdir_remove_backend(ctx, rr->vd, be, NULL);
121 2
}
122
123
VCL_BACKEND v_matchproto_()
124 72
vmod_random_backend(VRT_CTX, struct vmod_directors_random *rr)
125
{
126 72
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
127 72
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_RANDOM_MAGIC);
128 72
        return (rr->vd->dir);
129
}