varnish-cache/vmod/vmod_directors_shard_dir.h
0
/*-
1
 * Copyright 2009-2016 UPLEX - Nils Goroll Systemoptimierung
2
 * All rights reserved.
3
 *
4
 * Authors: Julian Wiesener <jw@uplex.de>
5
 *          Nils Goroll <slink@uplex.de>
6
 *
7
 * SPDX-License-Identifier: BSD-2-Clause
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 */
30
31
struct vbitmap;
32
33
struct shard_circlepoint {
34
        uint32_t                point;
35
        unsigned int            host;
36
};
37
38
struct shard_backend {
39
        VCL_BACKEND             backend;
40
        union {
41
                const char      *ident;
42
                void            *freeptr;
43
        };
44
        VCL_DURATION            rampup;
45
        uint32_t                replicas;
46
};
47
48
struct vmod_directors_shard_param;
49
50
#define SHDBG_LOOKUP     1
51
#define SHDBG_CIRCLE    (1<<1)
52
#define SHDBG_RAMPWARM  (1<<2)
53
54
struct sharddir {
55
        unsigned                                magic;
56
#define SHARDDIR_MAGIC                          0xdbb7d59f
57
        uint32_t                                debug_flags;
58
59
        pthread_rwlock_t                        mtx;
60
61
        unsigned                                n_backend;
62
        unsigned                                l_backend;
63
        struct shard_backend                    *backend;
64
65
        const char                              *name;
66
        struct shard_circlepoint                *hashcircle;
67
        const struct vmod_directors_shard_param *param;
68
69
        VCL_DURATION                            rampup_duration;
70
        VCL_REAL                                warmup;
71
72
        uint32_t                                n_points;
73
};
74
75
/* VRT_priv_task() id offsets */
76
enum shard_task_off_e {
77
        task_off_param = 0,
78
        task_off_cfg = 1
79
};
80
81
static inline VCL_BACKEND
82 7120
sharddir_backend(const struct sharddir *shardd, unsigned id)
83
{
84 7120
        assert(id < shardd->n_backend);
85 7120
        return (shardd->backend[id].backend);
86
}
87
88
#define SHDBG(flag, shardd, ...)                                        \
89
        do {                                                            \
90
                if ((shardd)->debug_flags & (flag))                     \
91
                        VSL(SLT_Debug, NO_VXID, "vmod_directors: shard: " \
92
                            __VA_ARGS__);                               \
93
        } while (0)
94
95
#define shard_log(vsl, tag, name, fmt, ...)                             \
96
        sharddir_log(vsl, tag, "vmod_directors: shard %s: " fmt,        \
97
            name, __VA_ARGS__)
98
99
#define shard_fail(ctx, name, fmt, ...)                         \
100
        VRT_fail(ctx, "vmod_directors: shard %s: " fmt, name, __VA_ARGS__)
101
#define shard_err(vsl, name, fmt, ...)                          \
102
        shard_log(vsl, SLT_Error, name, fmt, __VA_ARGS__)
103
#define shard_err0(vsl, name, msg)                      \
104
        shard_log(vsl, SLT_Error, name, "%s", msg)
105
#define shard_notice(vsl, name, fmt, ...)               \
106
        shard_log(vsl, SLT_Notice, name, fmt, __VA_ARGS__)
107
108
void sharddir_debug(struct sharddir *shardd, const uint32_t flags);
109
void sharddir_log(struct vsl_log *, enum VSL_tag_e tag,  const char *fmt, ...);
110
void sharddir_new(struct sharddir **sharddp, const char *vcl_name,
111
    const struct vmod_directors_shard_param *param);
112
void sharddir_set_param(struct sharddir *shardd,
113
    const struct vmod_directors_shard_param *param);
114
void sharddir_delete(struct sharddir **sharddp);
115
void sharddir_rdlock(struct sharddir *shardd);
116
void sharddir_wrlock(struct sharddir *shardd);
117
void sharddir_unlock(struct sharddir *shardd);
118
VCL_BOOL sharddir_any_healthy(VRT_CTX, struct sharddir *, VCL_TIME *);
119
VCL_BACKEND sharddir_pick_be(VRT_CTX, struct sharddir *, uint32_t, VCL_INT,
120
   VCL_REAL, VCL_BOOL, VCL_ENUM healthy);
121
122
/* in shard_cfg.c */
123
void shardcfg_delete(const struct sharddir *shardd);
124
VCL_DURATION shardcfg_get_rampup(const struct sharddir *shardd, unsigned host);