varnish-cache/vmod/vmod_directors_shard_dir.h
1
/*-
2
 * Copyright 2009-2016 UPLEX - Nils Goroll Systemoptimierung
3
 * All rights reserved.
4
 *
5
 * Authors: Julian Wiesener <jw@uplex.de>
6
 *          Nils Goroll <slink@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
struct vbitmap;
33
34
struct shard_circlepoint {
35
        uint32_t                point;
36
        unsigned int            host;
37
};
38
39
struct shard_backend {
40
        VCL_BACKEND             backend;
41
        union {
42
                const char      *ident;
43
                void            *freeptr;
44
        };
45
        VCL_DURATION            rampup;
46
        uint32_t                replicas;
47
};
48
49
struct vmod_directors_shard_param;
50
51
#define SHDBG_LOOKUP     1
52
#define SHDBG_CIRCLE    (1<<1)
53
#define SHDBG_RAMPWARM  (1<<2)
54
55
struct sharddir {
56
        unsigned                                magic;
57
#define SHARDDIR_MAGIC                          0xdbb7d59f
58
        uint32_t                                debug_flags;
59
60
        pthread_rwlock_t                        mtx;
61
62
        unsigned                                n_backend;
63
        unsigned                                l_backend;
64
        struct shard_backend                    *backend;
65
66
        const char                              *name;
67
        struct shard_circlepoint                *hashcircle;
68
        const struct vmod_directors_shard_param *param;
69
70
        VCL_DURATION                            rampup_duration;
71
        VCL_REAL                                warmup;
72
73
        uint32_t                                n_points;
74
};
75
76
/* VRT_priv_task() id offsets */
77
enum shard_task_off_e {
78
        task_off_param = 0,
79
        task_off_cfg = 1
80
};
81
82
static inline VCL_BACKEND
83 1780
sharddir_backend(const struct sharddir *shardd, unsigned id)
84
{
85 1780
        assert(id < shardd->n_backend);
86 1780
        return (shardd->backend[id].backend);
87
}
88
89
#define SHDBG(flag, shardd, ...)                                        \
90
        do {                                                            \
91
                if ((shardd)->debug_flags & (flag))                     \
92
                        VSL(SLT_Debug, 0, "vmod_directors: shard: "     \
93
                            __VA_ARGS__);                               \
94
        } while (0)
95
96
#define shard_log(vsl, tag, name, fmt, ...)                             \
97
        sharddir_log(vsl, tag, "vmod_directors: shard %s: " fmt,        \
98
            name, __VA_ARGS__)
99
100
#define shard_fail(ctx, name, fmt, ...)                         \
101
        VRT_fail(ctx, "vmod_directors: shard %s: " fmt, name, __VA_ARGS__)
102
#define shard_err(vsl, name, fmt, ...)                          \
103
        shard_log(vsl, SLT_Error, name, fmt, __VA_ARGS__)
104
#define shard_err0(vsl, name, msg)                      \
105
        shard_log(vsl, SLT_Error, name, "%s", msg)
106
#define shard_notice(vsl, name, fmt, ...)               \
107
        shard_log(vsl, SLT_Notice, name, fmt, __VA_ARGS__)
108
109
void sharddir_debug(struct sharddir *shardd, const uint32_t flags);
110
void sharddir_log(struct vsl_log *, enum VSL_tag_e tag,  const char *fmt, ...);
111
void sharddir_new(struct sharddir **sharddp, const char *vcl_name,
112
    const struct vmod_directors_shard_param *param);
113
void sharddir_set_param(struct sharddir *shardd,
114
    const struct vmod_directors_shard_param *param);
115
void sharddir_delete(struct sharddir **sharddp);
116
void sharddir_rdlock(struct sharddir *shardd);
117
void sharddir_wrlock(struct sharddir *shardd);
118
void sharddir_unlock(struct sharddir *shardd);
119
VCL_BOOL sharddir_any_healthy(VRT_CTX, struct sharddir *, VCL_TIME *);
120
VCL_BACKEND sharddir_pick_be(VRT_CTX, struct sharddir *, uint32_t, VCL_INT,
121
   VCL_REAL, VCL_BOOL, VCL_ENUM healthy);
122
123
/* in shard_cfg.c */
124
void shardcfg_delete(const struct sharddir *shardd);
125
VCL_DURATION shardcfg_get_rampup(const struct sharddir *shardd, unsigned host);