varnish-cache/lib/libvmod_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
 * 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
enum by_e {
31
        _BY_E_INVALID = 0,
32
#define VMODENUM(x) BY_ ## x,
33
#include "tbl_by.h"
34
        _BY_E_MAX
35
};
36
37
enum healthy_e {
38
        _HEALTHY_E_INVALID = 0,
39
#define VMODENUM(x) x,
40
#include "tbl_healthy.h"
41
        _HEALTHY_E_MAX
42
};
43
44
enum resolve_e {
45
        _RESOLVE_E_INVALID = 0,
46
#define VMODENUM(x) x,
47
#include "tbl_resolve.h"
48
        _RESOLVE_E_MAX
49
};
50
51
struct vbitmap;
52
53
struct shard_circlepoint {
54
        uint32_t                point;
55
        unsigned int            host;
56
};
57
58
struct shard_backend {
59
        VCL_BACKEND             backend;
60
        union {
61
                const char      *ident;
62
                void            *freeptr;
63
        };
64
        VCL_DURATION            rampup;
65
        uint32_t                canon_point;
66
};
67
68
struct vmod_directors_shard_param;
69
70
#define SHDBG_LOOKUP     1
71
#define SHDBG_CIRCLE    (1<<1)
72
#define SHDBG_RAMPWARM  (1<<2)
73
74
struct sharddir {
75
        unsigned                                magic;
76
#define SHARDDIR_MAGIC                          0xdbb7d59f
77
        uint32_t                                debug_flags;
78
79
        pthread_rwlock_t                        mtx;
80
81
        unsigned                                n_backend;
82
        unsigned                                l_backend;
83
        struct shard_backend                    *backend;
84
85
        const char                              *name;
86
        struct shard_circlepoint                *hashcircle;
87
        const struct vmod_directors_shard_param *param;
88
89
        VCL_DURATION                            rampup_duration;
90
        VCL_REAL                                warmup;
91
        VCL_INT                                 replicas;
92
};
93
94
static inline VCL_BACKEND
95 864
sharddir_backend(const struct sharddir *shardd, int id)
96
{
97 864
        assert(id >= 0);
98 864
        assert(id < shardd->n_backend);
99 864
        return (shardd->backend[id].backend);
100
}
101
102
static inline const char *
103
sharddir_backend_ident(const struct sharddir *shardd, int host)
104
{
105
        assert(host >= 0);
106
        assert(host < shardd->n_backend);
107
        return (shardd->backend[host].ident);
108
}
109
110
#define SHDBG(flag, shardd, ...)                                        \
111
        do {                                                            \
112
                if ((shardd)->debug_flags & (flag))                     \
113
                        VSL(SLT_Debug, 0, "shard: " __VA_ARGS__);       \
114
        } while (0)
115
116
#define shard_err(ctx, shardd, fmt, ...)                                \
117
        do {                                                            \
118
                sharddir_err(ctx, SLT_Error, "shard %s: " fmt,          \
119
                    (shardd)->name, __VA_ARGS__);                       \
120
        } while (0)
121
122
#define shard_err0(ctx, shardd, msg)                                    \
123
        do {                                                            \
124
                sharddir_err(ctx, SLT_Error, "shard %s: %s",            \
125
                    (shardd)->name, (msg));                             \
126
        } while (0)
127
128
void sharddir_debug(struct sharddir *shardd, const uint32_t flags);
129
void sharddir_err(VRT_CTX, enum VSL_tag_e tag,  const char *fmt, ...);
130
uint32_t sharddir_sha256v(const char *s, va_list ap);
131
uint32_t sharddir_sha256(const char *s, ...);
132
void sharddir_new(struct sharddir **sharddp, const char *vcl_name,
133
    const struct vmod_directors_shard_param *param);
134
void sharddir_set_param(struct sharddir *shardd,
135
    const struct vmod_directors_shard_param *param);
136
void sharddir_delete(struct sharddir **sharddp);
137
void sharddir_wrlock(struct sharddir *shardd);
138
void sharddir_unlock(struct sharddir *shardd);
139
VCL_BOOL sharddir_any_healthy(VRT_CTX, struct sharddir *, VCL_TIME *);
140
VCL_BACKEND sharddir_pick_be(VRT_CTX, struct sharddir *, uint32_t, VCL_INT,
141
   VCL_REAL, VCL_BOOL, enum healthy_e);
142
143
/* in shard_cfg.c */
144
void shardcfg_delete(const struct sharddir *shardd);
145
VCL_DURATION shardcfg_get_rampup(const struct sharddir *shardd, int host);