varnish-cache/vmod/vmod_directors_shard.c
0
/*-
1
 * Copyright 2009-2018 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
#include "config.h"
32
33
#include <stdlib.h>
34
#include <string.h>
35
36
#include "cache/cache.h"
37
#include "vcl.h"
38
39
#include "vend.h"
40
41
#include "vcc_directors_if.h"
42
#include "vmod_directors_shard_dir.h"
43
#include "vmod_directors_shard_cfg.h"
44
#include "vsb.h"
45
46
/* -------------------------------------------------------------------------
47
 *  shard director: LAZY mode (vdi resolve function), parameter objects
48
 *
49
 *  By associating a parameter object with a shard director, we enable LAZY
50
 *  lookups as with the other directors. Parameter objects are defined with VCL
51
 *  scope (normal vmod objects), but can be overridden per backend request using
52
 *  a task priv.
53
 *
54
 *  We use the same concept to carry shard.backend() parameters to vdi resolve
55
 *  for LAZY mode: They get saved in a per-director task scope parameter object.
56
 *
57
 *  Each object points to another object providing defaults for values which are
58
 *  not defined.
59
 *
60
 *  Actual resolution of the various parameter objects does not happen before
61
 *  they are used, which enables changing them independently (ie, shard
62
 *  .backend() parameters have precedence over an associated parameter object,
63
 *  which by itself can be overridden).
64
 *
65
 *  Overview of parameter objects (pointers are alternatives)
66
 *
67
 *  shard() director        shard_param() object    default param
68
 *
69
 *               --------------------------------->   vmod static
70
 *    VCL obj   /                                ->
71
 *    .param  -+--------->    VCL obj           /  _
72
 *                            .default  --------   /|
73
 *                                                /
74
 *                               ^               /
75
 *                               |              /
76
 *                                             /
77
 *                            .default        /
78
 *          ------------->    TASK priv      /
79
 *         /                                /
80
 *    .default -----------------------------
81
 *    TASK priv
82
 */
83
84
/* -------------------------------------------------------------------------
85
 * method arguments and set parameters bitmask in vmod_directors_shard_param
86
 */
87
88
#define arg_by          ((uint32_t)1)
89
#define arg_key         ((uint32_t)1 << 1)
90
#define arg_key_blob    ((uint32_t)1 << 2)
91
#define arg_alt         ((uint32_t)1 << 3)
92
#define arg_warmup      ((uint32_t)1 << 4)
93
#define arg_rampup      ((uint32_t)1 << 5)
94
#define arg_healthy     ((uint32_t)1 << 6)
95
#define arg_param       ((uint32_t)1 << 7)
96
#define arg_resolve     ((uint32_t)1 << 8)
97
#define arg_mask_       ((arg_resolve << 1) - 1)
98
/* allowed in shard_param.set */
99
#define arg_mask_set_   (arg_param - 1)
100
/* allowed in shard_param */
101
#define arg_mask_param_ ( arg_mask_set_         \
102
                          & ~arg_key                    \
103
                          & ~arg_key_blob )
104
105
/* -------------------------------------------------------------------------
106
 * shard parameters - declaration & defaults
107
 */
108
enum vmod_directors_shard_param_scope {
109
        _SCOPE_INVALID = 0,
110
        SCOPE_VMOD,
111
        SCOPE_VCL,
112
        SCOPE_TASK,
113
        SCOPE_STACK
114
};
115
116
struct vmod_directors_shard_param;
117
118
#define VMOD_SHARD_SHARD_PARAM_BLOB             0xdf5ca116
119
120
struct vmod_directors_shard_param {
121
        unsigned                                magic;
122
#define VMOD_SHARD_SHARD_PARAM_MAGIC            0xdf5ca117
123
124
        /* internals */
125
        uint32_t                                key;
126
        const char                              *vcl_name;
127
        const struct vmod_directors_shard_param *defaults;
128
        enum vmod_directors_shard_param_scope   scope;
129
130
        /* parameters */
131
        VCL_ENUM                                by;
132
        VCL_ENUM                                healthy;
133
        uint32_t                                mask;
134
        VCL_BOOL                                rampup;
135
        VCL_INT                                 alt;
136
        VCL_REAL                                warmup;
137
};
138
139
static const struct vmod_directors_shard_param shard_param_default = {
140
        .magic          = VMOD_SHARD_SHARD_PARAM_MAGIC,
141
142
        .key            = 0,
143
        .vcl_name       = "builtin defaults",
144
        .defaults       = NULL,
145
        .scope          = SCOPE_VMOD,
146
147
        .mask           = arg_mask_param_,
148
        .rampup = 1,
149
        .alt            = 0,
150
        .warmup         = -1,
151
};
152
153
#define default_by(ptr) (ptr == NULL ? VENUM(HASH) : ptr)
154
#define default_healthy(ptr) (ptr == NULL ? VENUM(CHOSEN) : ptr)
155
156
static struct vmod_directors_shard_param *
157
shard_param_stack(struct vmod_directors_shard_param *p,
158
    const struct vmod_directors_shard_param *pa, const char *who);
159
160
static const struct vmod_directors_shard_param *
161
shard_param_task_r(VRT_CTX, const void *id, const char *who,
162
    const struct vmod_directors_shard_param *pa);
163
164
static struct vmod_directors_shard_param *
165
shard_param_task_l(VRT_CTX, const void *id, const char *who,
166
    const struct vmod_directors_shard_param *pa);
167
168
static const struct vmod_directors_shard_param *
169
shard_param_blob(VCL_BLOB blob);
170
171
static const struct vmod_directors_shard_param *
172
vmod_shard_param_read(VRT_CTX, const void *id, const char *who,
173
    const struct vmod_directors_shard_param *p,
174
    struct vmod_directors_shard_param *pstk);
175
176
// XXX #3329 #3330 revisit - for now, treat pipe like backend
177
#define SHARD_VCL_TASK_REQ (VCL_MET_TASK_C & ~VCL_MET_PIPE)
178
#define SHARD_VCL_TASK_BEREQ (VCL_MET_TASK_B | VCL_MET_PIPE)
179
/* -------------------------------------------------------------------------
180
 * shard vmod interface
181
 */
182
static vdi_healthy_f vmod_shard_healthy;
183
static vdi_resolve_f vmod_shard_resolve;
184
static vdi_list_f vmod_shard_list;
185
186
struct vmod_directors_shard {
187
        unsigned                                magic;
188
#define VMOD_SHARD_SHARD_MAGIC                  0x6e63e1bf
189
        struct sharddir                         *shardd;
190
        VCL_BACKEND                             dir;
191
};
192
193
static void
194 1000
shard__assert(void)
195
{
196
        VCL_INT t1;
197
        uint32_t t2a, t2b;
198
199
        /* we put our uint32 key in a VCL_INT container */
200 1000
        assert(sizeof(VCL_INT) >= sizeof(uint32_t));
201 1000
        t2a = UINT32_MAX;
202 1000
        t1 = (VCL_INT)t2a;
203 1000
        t2b = (uint32_t)t1;
204 1000
        assert(t2a == t2b);
205 1000
}
206
207
static void v_matchproto_(vdi_release_f)
208 200
vmod_shard_release(VCL_BACKEND dir)
209
{
210
        struct sharddir *shardd;
211
212 200
        CAST_OBJ_NOTNULL(shardd, dir->priv, SHARDDIR_MAGIC);
213 200
        sharddir_release(shardd);
214 200
}
215
216
static void v_matchproto_(vdi_destroy_f)
217 200
vmod_shard_destroy(VCL_BACKEND dir)
218
{
219
        struct sharddir *shardd;
220
221 200
        CAST_OBJ_NOTNULL(shardd, dir->priv, SHARDDIR_MAGIC);
222 200
        sharddir_delete(&shardd);
223 200
}
224
225
static const struct vdi_methods vmod_shard_methods[1] = {{
226
        .magic =        VDI_METHODS_MAGIC,
227
        .type =         "shard",
228
        .resolve =      vmod_shard_resolve,
229
        .healthy =      vmod_shard_healthy,
230
        .release =      vmod_shard_release,
231
        .destroy =      vmod_shard_destroy,
232
        .list =         vmod_shard_list
233
}};
234
235
236
VCL_VOID v_matchproto_(td_directors_shard__init)
237 1000
vmod_shard__init(VRT_CTX, struct vmod_directors_shard **vshardp,
238
    const char *vcl_name)
239
{
240
        struct vmod_directors_shard *vshard;
241
242 1000
        shard__assert();
243
244 1000
        AN(vshardp);
245 1000
        AZ(*vshardp);
246 1000
        ALLOC_OBJ(vshard, VMOD_SHARD_SHARD_MAGIC);
247 1000
        AN(vshard);
248
249 1000
        *vshardp = vshard;
250 1000
        sharddir_new(&vshard->shardd, vcl_name, &shard_param_default);
251
252 2000
        vshard->dir = VRT_AddDirector(ctx, vmod_shard_methods, vshard->shardd,
253 1000
            "%s", vcl_name);
254 1000
}
255
256
VCL_VOID v_matchproto_(td_directors_shard__fini)
257 200
vmod_shard__fini(struct vmod_directors_shard **vshardp)
258
{
259
        struct vmod_directors_shard *vshard;
260
261 200
        TAKE_OBJ_NOTNULL(vshard, vshardp, VMOD_SHARD_SHARD_MAGIC);
262 200
        VRT_DelDirector(&vshard->dir);
263 200
        FREE_OBJ(vshard);
264 200
}
265
266
VCL_INT v_matchproto_(td_directors_shard_key)
267 440
vmod_shard_key(VRT_CTX, struct vmod_directors_shard *vshard, VCL_STRANDS s)
268
{
269
270 440
        (void)ctx;
271 440
        (void)vshard;
272
273 440
        return ((VCL_INT)VRT_HashStrands32(s));
274
}
275
276
VCL_VOID v_matchproto_(td_directors_set_warmup)
277 160
vmod_shard_set_warmup(VRT_CTX, struct vmod_directors_shard *vshard,
278
    VCL_REAL probability)
279
{
280 160
        CHECK_OBJ_NOTNULL(vshard, VMOD_SHARD_SHARD_MAGIC);
281 160
        if (probability < 0 || probability >= 1) {
282 80
                shard_notice(ctx->vsl, vshard->shardd->name,
283
                    ".set_warmup(%f) ignored", probability);
284 80
                return;
285
        }
286 80
        shardcfg_set_warmup(vshard->shardd, probability);
287 160
}
288
289
VCL_VOID v_matchproto_(td_directors_set_rampup)
290 80
vmod_shard_set_rampup(VRT_CTX, struct vmod_directors_shard *vshard,
291
    VCL_DURATION duration)
292
{
293 80
        (void)ctx;
294 80
        CHECK_OBJ_NOTNULL(vshard, VMOD_SHARD_SHARD_MAGIC);
295 80
        shardcfg_set_rampup(vshard->shardd, duration);
296 80
}
297
298
VCL_VOID v_matchproto_(td_directors_shard_associate)
299 160
vmod_shard_associate(VRT_CTX,
300
    struct vmod_directors_shard *vshard, VCL_BLOB b)
301
{
302
        const struct vmod_directors_shard_param *ppt;
303 160
        CHECK_OBJ_NOTNULL(vshard, VMOD_SHARD_SHARD_MAGIC);
304
305 160
        if (b == NULL) {
306 40
                sharddir_set_param(vshard->shardd, &shard_param_default);
307 40
                return;
308
        }
309
310 120
        ppt = shard_param_blob(b);
311
312 120
        if (ppt == NULL) {
313 40
                shard_fail(ctx, vshard->shardd->name, "%s",
314
                    "shard .associate param invalid");
315 40
                return;
316
        }
317
318 80
        sharddir_set_param(vshard->shardd, ppt);
319 160
}
320
321
VCL_BOOL v_matchproto_(td_directors_shard_add_backend)
322 4560
vmod_shard_add_backend(VRT_CTX, struct vmod_directors_shard *vshard,
323
    struct VARGS(shard_add_backend) *args)
324
{
325 4560
        VCL_REAL weight = 1;
326
327 4560
        CHECK_OBJ_NOTNULL(vshard, VMOD_SHARD_SHARD_MAGIC);
328
329 4560
        if (args->backend == NULL) {
330 40
                shard_fail(ctx, vshard->shardd->name, "%s",
331
                    "None backend cannot be added");
332 40
                return (0);
333
        }
334
335 4520
        if (args->valid_weight) {
336 120
                if (args->weight >= 1)
337 80
                        weight = args->weight;
338
                else
339 40
                        shard_notice(ctx->vsl, vshard->shardd->name,
340
                            ".add_backend(weight=%f) ignored", args->weight);
341 120
        }
342
343 9040
        return (shardcfg_add_backend(ctx, vshard->shardd, args->backend,
344 4520
            args->valid_ident ? args->ident : NULL,
345 4520
            args->valid_rampup ? args->rampup : nan(""),
346 4520
            weight));
347 4560
}
348
349
VCL_BOOL v_matchproto_(td_directors_shard_remove_backend)
350 1200
vmod_shard_remove_backend(VRT_CTX, struct vmod_directors_shard *vshard,
351
    struct VARGS(shard_remove_backend) *args)
352
{
353 1200
        VCL_BACKEND be = args->valid_backend ? args->backend : NULL;
354 1200
        VCL_STRING ident = args->valid_ident ? args->ident : NULL;
355
356 1200
        CHECK_OBJ_NOTNULL(vshard, VMOD_SHARD_SHARD_MAGIC);
357
358 1200
        if (be == NULL && ident == NULL) {
359 40
                shard_fail(ctx, vshard->shardd->name, "%s",
360
                    ".remove_backend(): either backend or ident are required");
361 40
                return (0);
362
        }
363
364 1160
        return (shardcfg_remove_backend(ctx, vshard->shardd, be, ident));
365 1200
}
366
367
VCL_BOOL v_matchproto_(td_directors_shard_clear)
368 600
vmod_shard_clear(VRT_CTX, struct vmod_directors_shard *vshard)
369
{
370 600
        CHECK_OBJ_NOTNULL(vshard, VMOD_SHARD_SHARD_MAGIC);
371 600
        return (shardcfg_clear(ctx, vshard->shardd));
372
}
373
374
VCL_BOOL v_matchproto_(td_directors_shard_reconfigure)
375 1680
vmod_shard_reconfigure(VRT_CTX, struct vmod_directors_shard *vshard,
376
    VCL_INT replicas)
377
{
378 1680
        return (shardcfg_reconfigure(ctx, vshard->shardd, replicas));
379
}
380
381
static inline uint32_t
382 5160
shard_get_key(VRT_CTX, const struct vmod_directors_shard_param *p)
383
{
384
        struct http *http;
385 5160
        VCL_ENUM by = default_by(p->by);
386
387 5160
        if (by == VENUM(KEY) || by == VENUM(BLOB))
388 4080
                return (p->key);
389 1080
        if (by == VENUM(HASH) && ctx->bo != NULL) {
390 280
                CHECK_OBJ(ctx->bo, BUSYOBJ_MAGIC);
391 280
                return (vbe32dec(ctx->bo->digest));
392
        }
393 800
        if (by == VENUM(HASH) || by == VENUM(URL)) {
394 800
                if (ctx->http_req) {
395 560
                        AN(http = ctx->http_req);
396 560
                } else {
397 240
                        AN(ctx->http_bereq);
398 240
                        AN(http = ctx->http_bereq);
399
                }
400 800
                return (VRT_HashStrands32(TOSTRAND(http->hd[HTTP_HDR_URL].b)));
401
        }
402 0
        WRONG("by enum");
403 5160
}
404
405
/*
406
 * merge parameters to resolve all undef values
407
 * key is to be calculated after merging
408
 */
409
static void
410 23040
shard_param_merge(struct vmod_directors_shard_param *to,
411
                  const struct vmod_directors_shard_param *from)
412
{
413 23040
        CHECK_OBJ_NOTNULL(to, VMOD_SHARD_SHARD_PARAM_MAGIC);
414 23040
        assert((to->mask & ~arg_mask_param_) == 0);
415
416 23040
        if (to->mask == arg_mask_param_)
417 0
                return;
418
419 23040
        CHECK_OBJ_NOTNULL(from, VMOD_SHARD_SHARD_PARAM_MAGIC);
420 23040
        assert((from->mask & ~arg_mask_param_) == 0);
421
422 23040
        if ((to->mask & arg_by) == 0 && (from->mask & arg_by) != 0) {
423 8360
                to->by = from->by;
424 8360
                if (from->by == VENUM(KEY) || from->by == VENUM(BLOB))
425 4080
                        to->key = from->key;
426 8360
        }
427
428
#define mrg(to, from, field) do {                                       \
429
                if (((to)->mask & arg_ ## field) == 0 &&                \
430
                    ((from)->mask & arg_ ## field) != 0)                \
431
                        (to)->field = (from)->field;                    \
432
        } while(0)
433
434 23040
        mrg(to, from, healthy);
435 23040
        mrg(to, from, rampup);
436 23040
        mrg(to, from, alt);
437 23040
        mrg(to, from, warmup);
438
#undef mrg
439
440 23040
        to->mask |= from->mask;
441
442 23040
        if (to->mask == arg_mask_param_)
443 10160
                return;
444
445 12880
        AN(from->defaults);
446 12880
        shard_param_merge(to, from->defaults);
447 23040
}
448
449
static uint32_t
450 400
shard_blob_key(VCL_BLOB key_blob)
451
{
452 400
        uint8_t k[4] = { 0 };
453
        const uint8_t *b;
454
        size_t i, ki;
455
456 400
        AN(key_blob);
457 400
        AN(key_blob->blob);
458 400
        assert(key_blob->len > 0);
459
460 400
        if (key_blob->len >= 4)
461 400
                ki = 0;
462
        else
463 0
                ki = 4 - key_blob->len;
464
465 400
        b = key_blob->blob;
466 2000
        for (i = 0; ki < 4; i++, ki++)
467 1600
                k[ki] = b[i];
468 400
        assert(i <= key_blob->len);
469
470 400
        return (vbe32dec(k));
471
}
472
473
/*
474
 * convert vmod interface valid_* to our bitmask
475
 */
476
477
#define tobit(args, name) ((args)->valid_##name ? arg_##name : 0)
478
479
static uint32_t
480 4280
shard_backendarg_mask_(const struct VARGS(shard_backend) * const a)
481
{
482 12840
        return (tobit(a, by)            |
483 8560
                tobit(a, key)           |
484 8560
                tobit(a, key_blob)      |
485 8560
                tobit(a, alt)           |
486 8560
                tobit(a, warmup)        |
487 8560
                tobit(a, rampup)        |
488 8560
                tobit(a, healthy)       |
489 8560
                tobit(a, param)         |
490 4280
                tobit(a, resolve));
491
}
492
static uint32_t
493 3920
shard_param_set_mask(const struct VARGS(shard_param_set) * const a)
494
{
495 11760
        return (tobit(a, by)            |
496 7840
                tobit(a, key)           |
497 7840
                tobit(a, key_blob)      |
498 7840
                tobit(a, alt)           |
499 7840
                tobit(a, warmup)        |
500 7840
                tobit(a, rampup)        |
501 3920
                tobit(a, healthy));
502
}
503
#undef tobit
504
505
/*
506
 * check arguments and return in a struct param
507
 */
508
static struct vmod_directors_shard_param *
509 7800
shard_param_args(VRT_CTX,
510
    struct vmod_directors_shard_param *p, const char *func,
511
    uint32_t args, VCL_ENUM by_s, VCL_INT key_int, VCL_BLOB key_blob,
512
    VCL_INT alt, VCL_REAL warmup, VCL_BOOL rampup, VCL_ENUM healthy_s)
513
{
514
515 7800
        CHECK_OBJ_NOTNULL(p, VMOD_SHARD_SHARD_PARAM_MAGIC);
516 7800
        AN(p->vcl_name);
517
518 7800
        assert((args & ~arg_mask_set_) == 0);
519
520 7800
        if (!(args & arg_by))
521 2000
                by_s = NULL;
522 7800
        by_s = default_by(by_s);
523
524
        /* by_s / key_int / key_blob */
525 7800
        if (by_s == VENUM(KEY)) {
526 3200
                if ((args & arg_key) == 0) {
527 40
                        shard_fail(ctx, p->vcl_name,
528
                            "%s missing key argument with by=%s",
529
                            func, by_s);
530 40
                        return (NULL);
531
                }
532 3160
                if (key_int < 0 || key_int > UINT32_MAX) {
533 40
                        shard_fail(ctx, p->vcl_name,
534
                            "%s invalid key argument %jd with by=%s",
535
                            func, (intmax_t)key_int, by_s);
536 40
                        return (NULL);
537
                }
538 3120
                assert(key_int >= 0);
539 3120
                assert(key_int <= UINT32_MAX);
540 3120
                p->key = (uint32_t)key_int;
541 7720
        } else if (by_s == VENUM(BLOB)) {
542 480
                if ((args & arg_key_blob) == 0) {
543 40
                        shard_fail(ctx, p->vcl_name,
544
                            "%s missing key_blob argument with by=%s",
545
                            func, by_s);
546 40
                        return (NULL);
547
                }
548 440
                if (key_blob == NULL || key_blob->len == 0 ||
549 400
                    key_blob->blob == NULL) {
550 40
                        shard_err(ctx->vsl, p->vcl_name,
551
                            "%s by=BLOB but no or empty key_blob - using key 0",
552
                            func);
553 40
                        p->key = 0;
554 40
                } else
555 400
                        p->key = shard_blob_key(key_blob);
556 4560
        } else if (by_s == VENUM(HASH) || by_s == VENUM(URL)) {
557 4120
                if (args & (arg_key|arg_key_blob)) {
558 80
                        shard_fail(ctx, p->vcl_name,
559
                            "%s key and key_blob arguments are "
560
                            "invalid with by=%s", func, by_s);
561 80
                        return (NULL);
562
                }
563 4040
        } else {
564 0
                WRONG("by enum");
565
        }
566 7600
        p->by = by_s;
567
568 7600
        if (args & arg_alt) {
569 2520
                if (alt < 0) {
570 40
                        shard_fail(ctx, p->vcl_name,
571
                            "%s invalid alt argument %jd",
572
                            func, (intmax_t)alt);
573 40
                        return (NULL);
574
                }
575 2480
                p->alt = alt;
576 2480
        }
577
578 7560
        if (args & arg_warmup) {
579 760
                if ((warmup < 0 && warmup != -1) || warmup > 1) {
580 80
                        shard_fail(ctx, p->vcl_name,
581
                            "%s invalid warmup argument %f",
582
                            func, warmup);
583 80
                        return (NULL);
584
                }
585 680
                p->warmup = warmup;
586 680
        }
587
588 7480
        if (args & arg_rampup)
589 240
                p->rampup = !!rampup;
590
591 7480
        if (args & arg_healthy)
592 1320
                p->healthy = healthy_s;
593
594 7480
        p->mask = args & arg_mask_param_;
595 7480
        return (p);
596 7800
}
597
598
VCL_BACKEND v_matchproto_(td_directors_shard_backend)
599 4280
vmod_shard_backend(VRT_CTX, struct vmod_directors_shard *vshard,
600
                   struct VARGS(shard_backend) *a)
601
{
602
        struct sharddir *shardd;
603
        struct vmod_directors_shard_param pstk;
604 4280
        struct vmod_directors_shard_param *pp = NULL;
605
        const struct vmod_directors_shard_param *ppt;
606
        VCL_ENUM resolve;
607 4280
        uint32_t args = shard_backendarg_mask_(a);
608
609 4280
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
610 4280
        CHECK_OBJ_NOTNULL(vshard, VMOD_SHARD_SHARD_MAGIC);
611 4280
        shardd = vshard->shardd;
612 4280
        CHECK_OBJ_NOTNULL(shardd, SHARDDIR_MAGIC);
613 4280
        assert((args & ~arg_mask_) == 0);
614
615 4280
        if (args & arg_resolve)
616 1960
                resolve = a->resolve;
617 2320
        else if (ctx->method & VCL_MET_TASK_H)
618 40
                resolve = VENUM(LAZY);
619
        else
620 2280
                resolve = VENUM(NOW);
621
622 4280
        if (resolve == VENUM(LAZY)) {
623 640
                if ((args & ~arg_resolve) == 0) {
624 280
                        AN(vshard->dir);
625 280
                        return (vshard->dir);
626
                }
627
628 360
                if ((ctx->method & SHARD_VCL_TASK_BEREQ) == 0) {
629 40
                        shard_fail(ctx, shardd->name, "%s",
630
                            ".backend(resolve=LAZY) with other "
631
                            "parameters can only be used in backend/pipe "
632
                            "context");
633 40
                        return (NULL);
634
                }
635
636 640
                pp = shard_param_task_l(ctx, shardd, shardd->name,
637 320
                    shardd->param);
638 320
                if (pp == NULL)
639 0
                        return (NULL);
640 3960
        } else if (resolve == VENUM(NOW)) {
641 3640
                if (ctx->method & VCL_MET_TASK_H) {
642 40
                        shard_fail(ctx, shardd->name, "%s",
643
                            ".backend(resolve=NOW) cannot be "
644
                            "used in vcl_init{}/vcl_fini{}");
645 40
                        return (NULL);
646
                }
647 7200
                ppt = shard_param_task_r(ctx, shardd, shardd->name,
648 3600
                    shardd->param);
649 3600
                AN(ppt);
650 3600
                pp = shard_param_stack(&pstk, ppt, shardd->name);
651 3600
        } else {
652 0
                WRONG("resolve enum");
653
        }
654
655 3920
        AN(pp);
656
657 3920
        if (args & arg_param) {
658 120
                ppt = shard_param_blob(a->param);
659 120
                if (ppt == NULL) {
660 40
                        shard_fail(ctx, shardd->name, "%s",
661
                            ".backend(key_blob) param invalid");
662 40
                        return (NULL);
663
                }
664 80
                pp->defaults = ppt;
665 80
        }
666
667 7760
        pp = shard_param_args(ctx, pp, "shard.backend()",
668 3880
                              args & arg_mask_set_,
669 3880
                              a->by, a->key, a->key_blob, a->alt, a->warmup,
670 3880
                              a->rampup, a->healthy);
671 3880
        if (pp == NULL)
672 0
                return (NULL);
673
674 3880
        if (resolve == VENUM(LAZY))
675 320
                return (vshard->dir);
676
677 3560
        assert(resolve == VENUM(NOW));
678 3560
        shard_param_merge(pp, pp->defaults);
679 7120
        return (sharddir_pick_be(ctx, shardd, shard_get_key(ctx, pp),
680 3560
            pp->alt, pp->warmup, pp->rampup, pp->healthy));
681 4280
}
682
683
static VCL_BOOL v_matchproto_(vdi_healthy)
684 640
vmod_shard_healthy(VRT_CTX, VCL_BACKEND dir, VCL_TIME *changed)
685
{
686
        struct sharddir *shardd;
687
688 640
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
689 640
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
690 640
        CAST_OBJ_NOTNULL(shardd, dir->priv, SHARDDIR_MAGIC);
691 640
        return (sharddir_any_healthy(ctx, shardd, changed));
692
}
693
694
static VCL_BACKEND v_matchproto_(vdi_resolve_f)
695 600
vmod_shard_resolve(VRT_CTX, VCL_BACKEND dir)
696
{
697
        struct sharddir *shardd;
698
        struct vmod_directors_shard_param pstk[1];
699
        const struct vmod_directors_shard_param *pp;
700
701 600
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
702 600
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
703 600
        CAST_OBJ_NOTNULL(shardd, dir->priv, SHARDDIR_MAGIC);
704
705 1200
        pp = vmod_shard_param_read(ctx, shardd, shardd->name,
706 600
            shardd->param, pstk);
707 600
        CHECK_OBJ_NOTNULL(pp, VMOD_SHARD_SHARD_PARAM_MAGIC);
708
709 1200
        return (sharddir_pick_be(ctx, shardd,
710 600
                                 shard_get_key(ctx, pp), pp->alt, pp->warmup,
711 600
                                 pp->rampup, pp->healthy));
712
}
713
714
static void v_matchproto_(vdi_list_f)
715 1200
vmod_shard_list(VRT_CTX, VCL_BACKEND dir, struct vsb *vsb, int pflag, int jflag)
716
{
717
        struct sharddir *shardd;
718
        struct shard_backend *sbe;
719 1200
        VCL_TIME c, changed = 0;
720
        VCL_DURATION rampup_d, d;
721
        VCL_BACKEND be;
722
        VCL_BOOL h;
723 1200
        unsigned i, nh = 0;
724
        double rampup_p;
725
726 1200
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
727 1200
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
728 1200
        CAST_OBJ_NOTNULL(shardd, dir->priv, SHARDDIR_MAGIC);
729
730 1200
        if (pflag) {
731 160
                if (jflag) {
732 40
                        VSB_cat(vsb, "{\n");
733 40
                        VSB_indent(vsb, 2);
734 40
                        VSB_printf(vsb, "\"warmup\": %f,\n", shardd->warmup);
735 80
                        VSB_printf(vsb, "\"rampup_duration\": %f,\n",
736 40
                            shardd->rampup_duration);
737 40
                        VSB_cat(vsb, "\"backends\": {\n");
738 40
                        VSB_indent(vsb, 2);
739 40
                } else {
740 120
                        VSB_cat(vsb, "\n\n\tBackend\tIdent\tHealth\t"
741
                            "Rampup  Remaining\n");
742
                }
743 160
        }
744
745 1200
        sharddir_rdlock(shardd);
746 4840
        for (i = 0; i < shardd->n_backend; i++) {
747 3640
                sbe = &shardd->backend[i];
748 3640
                AN(sbe);
749 3640
                be = sbe->backend;
750 3640
                CHECK_OBJ_NOTNULL(be, DIRECTOR_MAGIC);
751
752 3640
                c = 0;
753 3640
                h = VRT_Healthy(ctx, be, &c);
754 3640
                if (h)
755 3280
                        nh++;
756 3640
                if (c > changed)
757 2800
                        changed = c;
758 3640
                if ((pflag) == 0)
759 3000
                        continue;
760
761 640
                d = ctx->now - c;
762 640
                rampup_d = shardcfg_get_rampup(shardd, i);
763 640
                if (! h) {
764 80
                        rampup_p = 0.0;
765 80
                        rampup_d = 0.0;
766 640
                } else if (d < rampup_d) {
767 240
                        rampup_p = d / rampup_d;
768 240
                        rampup_d -= d;
769 240
                } else {
770 320
                        rampup_p = 1.0;
771 320
                        rampup_d = 0.0;
772
                }
773
774 640
                if (jflag) {
775 160
                        if (i)
776 120
                                VSB_cat(vsb, ",\n");
777 320
                        VSB_printf(vsb, "\"%s\": {\n",
778 160
                            be->vcl_name);
779 160
                        VSB_indent(vsb, 2);
780 320
                        VSB_printf(vsb, "\"ident\": \"%s\",\n",
781 160
                            sbe->ident ? sbe->ident : be->vcl_name);
782 320
                        VSB_printf(vsb, "\"health\": \"%s\",\n",
783 160
                            h ? "healthy" : "sick");
784 160
                        VSB_printf(vsb, "\"rampup\": %f,\n", rampup_p);
785 320
                        VSB_printf(vsb, "\"rampup_remaining\": %.3f\n",
786 160
                            rampup_d);
787 160
                        VSB_indent(vsb, -2);
788 160
                        VSB_cat(vsb, "}");
789 160
                } else {
790 960
                        VSB_printf(vsb, "\t%s\t%s\t%s\t%6.2f%% %8.3fs\n",
791 480
                            be->vcl_name,
792 480
                            sbe->ident ? sbe->ident : be->vcl_name,
793 480
                            h ? "healthy" : "sick",
794 480
                            rampup_p * 100, rampup_d);
795
                }
796 640
        }
797 1200
        sharddir_unlock(shardd);
798
799 1200
        if (jflag && (pflag)) {
800 40
                VSB_cat(vsb, "\n");
801 40
                VSB_indent(vsb, -2);
802 40
                VSB_cat(vsb, "}\n");
803 40
                VSB_indent(vsb, -2);
804 40
                VSB_cat(vsb, "},\n");
805 40
        }
806
807 1200
        if (pflag)
808 160
                return;
809
810 1040
        if (jflag)
811 160
                VSB_printf(vsb, "[%u, %u, \"%s\"]", nh, i,
812 80
                    nh ? "healthy" : "sick");
813
        else
814 960
                VSB_printf(vsb, "%u/%u\t%s", nh, i, nh ? "healthy" : "sick");
815 1200
}
816
817
VCL_VOID v_matchproto_(td_directors_shard_backend)
818 280
vmod_shard_debug(VRT_CTX, struct vmod_directors_shard *vshard,
819
    VCL_INT i)
820
{
821 280
        CHECK_OBJ_NOTNULL(vshard, VMOD_SHARD_SHARD_MAGIC);
822
823 280
        (void)ctx;
824 280
        sharddir_debug(vshard->shardd, i & UINT32_MAX);
825 280
}
826
827
/* =============================================================
828
 * shard_param
829
 */
830
831
VCL_VOID v_matchproto_(td_directors_shard_param__init)
832 680
vmod_shard_param__init(VRT_CTX,
833
    struct vmod_directors_shard_param **pp, const char *vcl_name)
834
{
835
        struct vmod_directors_shard_param *p;
836
837 680
        (void) ctx;
838 680
        AN(pp);
839 680
        AZ(*pp);
840 680
        ALLOC_OBJ(p, VMOD_SHARD_SHARD_PARAM_MAGIC);
841 680
        AN(p);
842 680
        p->vcl_name = vcl_name;
843 680
        p->scope = SCOPE_VCL;
844 680
        p->defaults = &shard_param_default;
845
846 680
        *pp = p;
847 680
}
848
849
VCL_VOID v_matchproto_(td_directors_shard_param__fini)
850 320
vmod_shard_param__fini(struct vmod_directors_shard_param **pp)
851
{
852
        struct vmod_directors_shard_param *p;
853
854 320
        TAKE_OBJ_NOTNULL(p, pp, VMOD_SHARD_SHARD_PARAM_MAGIC);
855 320
        FREE_OBJ(p);
856 320
}
857
858
/*
859
 * init a stack param struct defaulting to pa with the given name
860
 */
861
static struct vmod_directors_shard_param *
862 10200
shard_param_stack(struct vmod_directors_shard_param *p,
863
    const struct vmod_directors_shard_param *pa, const char *who)
864
{
865 10200
        CHECK_OBJ_NOTNULL(pa, VMOD_SHARD_SHARD_PARAM_MAGIC);
866 10200
        assert(pa->scope > _SCOPE_INVALID);
867
868 10200
        AN(p);
869 10200
        INIT_OBJ(p, VMOD_SHARD_SHARD_PARAM_MAGIC);
870 10200
        p->vcl_name = who;
871 10200
        p->scope = SCOPE_STACK;
872 10200
        p->defaults = pa;
873
874 10200
        return (p);
875
}
876
877
static const struct vmod_directors_shard_param *
878 10360
shard_param_task_r(VRT_CTX, const void *id, const char *who,
879
   const struct vmod_directors_shard_param *pa)
880
{
881
        const struct vmod_directors_shard_param *p;
882
        const struct vmod_priv *task;
883
        const void *task_id;
884
885 10360
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
886 10360
        CHECK_OBJ_NOTNULL(pa, VMOD_SHARD_SHARD_PARAM_MAGIC);
887 10360
        assert(pa->scope > _SCOPE_INVALID);
888
889 10360
        task_id = (const char *)id + task_off_param;
890 10360
        task = VRT_priv_task_get(ctx, task_id);
891
892 10360
        if (task) {
893 5440
                CAST_OBJ_NOTNULL(p, task->priv, VMOD_SHARD_SHARD_PARAM_MAGIC);
894 5440
                assert(p->scope == SCOPE_TASK);
895 5440
                assert(who == p->vcl_name);
896 5440
                return (p);
897
        }
898
899 4920
        if (id == pa || pa->scope != SCOPE_VCL)
900 3560
                return (pa);
901
902 1360
        return (shard_param_task_r(ctx, pa, pa->vcl_name, pa));
903 10360
}
904
905
/*
906
 * get a task scoped param struct for id defaulting to pa
907
 * if id != pa and pa has VCL scope, also get a task scoped param struct for pa
908
 */
909
static struct vmod_directors_shard_param *
910 3920
shard_param_task_l(VRT_CTX, const void *id, const char *who,
911
   const struct vmod_directors_shard_param *pa)
912
{
913
        struct vmod_directors_shard_param *p;
914
        struct vmod_priv *task;
915
        const void *task_id;
916
917 3920
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
918 3920
        CHECK_OBJ_NOTNULL(pa, VMOD_SHARD_SHARD_PARAM_MAGIC);
919 3920
        assert(pa->scope > _SCOPE_INVALID);
920
921 3920
        task_id = (const char *)id + task_off_param;
922 3920
        task = VRT_priv_task(ctx, task_id);
923
924 3920
        if (task == NULL) {
925 0
                shard_fail(ctx, who, "%s", "no priv_task");
926 0
                return (NULL);
927
        }
928
929 3920
        if (task->priv) {
930 320
                CAST_OBJ_NOTNULL(p, task->priv, VMOD_SHARD_SHARD_PARAM_MAGIC);
931 320
                assert(p->scope == SCOPE_TASK);
932 320
                assert(who == p->vcl_name);
933 320
                return (p);
934
        }
935
936 7200
        WS_TASK_ALLOC_OBJ(ctx, p, VMOD_SHARD_SHARD_PARAM_MAGIC);
937 3600
        if (p == NULL)
938 0
                return (NULL);
939 3600
        task->priv = p;
940 3600
        p->vcl_name = who;
941 3600
        p->scope = SCOPE_TASK;
942
943 3600
        if (id == pa || pa->scope != SCOPE_VCL)
944 3520
                p->defaults = pa;
945
        else
946 80
                p->defaults = shard_param_task_l(ctx, pa, pa->vcl_name, pa);
947
948 3600
        if (p->defaults == NULL)
949 0
                return (NULL);
950
951 3600
        return (p);
952 3920
}
953
954
static struct vmod_directors_shard_param *
955 4120
shard_param_prep(VRT_CTX, struct vmod_directors_shard_param *p,
956
    const char *who)
957
{
958 4120
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
959 4120
        CHECK_OBJ_NOTNULL(p, VMOD_SHARD_SHARD_PARAM_MAGIC);
960
961 4120
        if (ctx->method & SHARD_VCL_TASK_REQ) {
962 0
                shard_fail(ctx, p->vcl_name, "%s may only be used "
963
                    "in vcl_init and in backend/pipe context", who);
964 0
                return (NULL);
965 4120
        } else if (ctx->method & SHARD_VCL_TASK_BEREQ)
966 3520
                p = shard_param_task_l(ctx, p, p->vcl_name, p);
967
        else
968 600
                assert(ctx->method & VCL_MET_TASK_H);
969
970 4120
        return (p);
971 4120
}
972
973
VCL_VOID v_matchproto_(td_directors_shard_param_set)
974 3920
vmod_shard_param_set(VRT_CTX, struct vmod_directors_shard_param *p,
975
                     struct VARGS(shard_param_set) *a)
976
{
977 3920
        uint32_t args = shard_param_set_mask(a);
978
979 3920
        assert((args & ~arg_mask_set_) == 0);
980
981 3920
        p = shard_param_prep(ctx, p, "shard_param.set()");
982 3920
        if (p == NULL)
983 0
                return;
984 7840
        (void) shard_param_args(ctx, p, "shard_param.set()", args,
985 3920
                                a->by, a->key, a->key_blob, a->alt, a->warmup,
986 3920
                                a->rampup, a->healthy);
987 3920
}
988
989
VCL_VOID v_matchproto_(td_directors_shard_param_clear)
990 200
vmod_shard_param_clear(VRT_CTX,
991
    struct vmod_directors_shard_param *p)
992
{
993 200
        p = shard_param_prep(ctx, p, "shard_param.clear()");
994 200
        if (p == NULL)
995 0
                return;
996 200
        p->mask = 0;
997 200
}
998
999
static const struct vmod_directors_shard_param *
1000 6600
vmod_shard_param_read(VRT_CTX, const void *id, const char *who,
1001
    const struct vmod_directors_shard_param *p,
1002
    struct vmod_directors_shard_param *pstk)
1003
{
1004
        struct vmod_directors_shard_param *pp;
1005
1006 6600
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
1007 6600
        CHECK_OBJ_NOTNULL(p, VMOD_SHARD_SHARD_PARAM_MAGIC);
1008
1009 6600
        if (ctx->method == 0 || (ctx->method & SHARD_VCL_TASK_BEREQ))
1010 5400
                p = shard_param_task_r(ctx, id, who, p);
1011
1012 6600
        CHECK_OBJ_NOTNULL(p, VMOD_SHARD_SHARD_PARAM_MAGIC);
1013 6600
        pp = shard_param_stack(pstk, p, p->vcl_name);
1014 6600
        shard_param_merge(pp, p);
1015 6600
        return (pp);
1016
}
1017
1018
VCL_STRING v_matchproto_(td_directors_shard_param_get_by)
1019 1000
vmod_shard_param_get_by(VRT_CTX,
1020
    struct vmod_directors_shard_param *p)
1021
{
1022
        struct vmod_directors_shard_param pstk;
1023
        const struct vmod_directors_shard_param *pp;
1024
1025 1000
        pp = vmod_shard_param_read(ctx, p, p->vcl_name, p, &pstk);
1026 1000
        CHECK_OBJ_NOTNULL(pp, VMOD_SHARD_SHARD_PARAM_MAGIC);
1027 1000
        return (default_by(pp->by));
1028
}
1029
1030
VCL_INT v_matchproto_(td_directors_shard_param_get_key)
1031 1000
vmod_shard_param_get_key(VRT_CTX,
1032
    struct vmod_directors_shard_param *p)
1033
{
1034
        struct vmod_directors_shard_param pstk;
1035
        const struct vmod_directors_shard_param *pp;
1036
1037 1000
        pp = vmod_shard_param_read(ctx, p, p->vcl_name, p, &pstk);
1038 1000
        CHECK_OBJ_NOTNULL(pp, VMOD_SHARD_SHARD_PARAM_MAGIC);
1039 1000
        return ((VCL_INT)shard_get_key(ctx, pp));
1040
}
1041
VCL_INT v_matchproto_(td_directors_shard_param_get_alt)
1042 1000
vmod_shard_param_get_alt(VRT_CTX,
1043
    struct vmod_directors_shard_param *p)
1044
{
1045
        struct vmod_directors_shard_param pstk;
1046
        const struct vmod_directors_shard_param *pp;
1047
1048 1000
        pp = vmod_shard_param_read(ctx, p, p->vcl_name, p, &pstk);
1049 1000
        CHECK_OBJ_NOTNULL(pp, VMOD_SHARD_SHARD_PARAM_MAGIC);
1050 1000
        return (pp->alt);
1051
}
1052
1053
VCL_REAL v_matchproto_(td_directors_shard_param_get_warmup)
1054 1000
vmod_shard_param_get_warmup(VRT_CTX,
1055
    struct vmod_directors_shard_param *p)
1056
{
1057
        struct vmod_directors_shard_param pstk;
1058
        const struct vmod_directors_shard_param *pp;
1059
1060 1000
        pp = vmod_shard_param_read(ctx, p, p->vcl_name, p, &pstk);
1061 1000
        CHECK_OBJ_NOTNULL(pp, VMOD_SHARD_SHARD_PARAM_MAGIC);
1062 1000
        return (pp->warmup);
1063
}
1064
1065
VCL_BOOL v_matchproto_(td_directors_shard_param_get_rampup)
1066 1000
vmod_shard_param_get_rampup(VRT_CTX,
1067
    struct vmod_directors_shard_param *p)
1068
{
1069
        struct vmod_directors_shard_param pstk;
1070
        const struct vmod_directors_shard_param *pp;
1071
1072 1000
        pp = vmod_shard_param_read(ctx, p, p->vcl_name, p, &pstk);
1073 1000
        CHECK_OBJ_NOTNULL(pp, VMOD_SHARD_SHARD_PARAM_MAGIC);
1074 1000
        return (pp->rampup);
1075
}
1076
1077
VCL_STRING v_matchproto_(td_directors_shard_param_get_healthy)
1078 1000
vmod_shard_param_get_healthy(VRT_CTX,
1079
    struct vmod_directors_shard_param *p)
1080
{
1081
        struct vmod_directors_shard_param pstk;
1082
        const struct vmod_directors_shard_param *pp;
1083
1084 1000
        pp = vmod_shard_param_read(ctx, p, p->vcl_name, p, &pstk);
1085 1000
        CHECK_OBJ_NOTNULL(pp, VMOD_SHARD_SHARD_PARAM_MAGIC);
1086 1000
        return (default_healthy(pp->healthy));
1087
}
1088
1089
static const struct vmod_directors_shard_param *
1090 240
shard_param_blob(VCL_BLOB blob)
1091
{
1092
        const struct vmod_directors_shard_param *p;
1093
1094 400
        if (blob && blob->type == VMOD_SHARD_SHARD_PARAM_BLOB &&
1095 160
            blob->blob != NULL &&
1096 160
            blob->len == sizeof(struct vmod_directors_shard_param)) {
1097 160
                CAST_OBJ_NOTNULL(p, blob->blob, VMOD_SHARD_SHARD_PARAM_MAGIC);
1098 160
                return (p);
1099
        }
1100
1101 80
        return (NULL);
1102 240
}
1103
1104
VCL_BLOB v_matchproto_(td_directors_shard_param_use)
1105 160
vmod_shard_param_use(VRT_CTX,
1106
    struct vmod_directors_shard_param *p)
1107
{
1108 160
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
1109 160
        CHECK_OBJ_NOTNULL(p, VMOD_SHARD_SHARD_PARAM_MAGIC);
1110
1111 160
        return (VRT_blob(ctx, "xshard_param.use()", p, sizeof *p,
1112
            VMOD_SHARD_SHARD_PARAM_BLOB));
1113
}