Code Coverage: vinyl-cache/vmod/vmod_directors_shard_cfg.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright 2009-2016 UPLEX - Nils Goroll Systemoptimierung
2
 * All rights reserved.
3
 *
4
 * Authors: Nils Goroll <nils.goroll@uplex.de>
5
 *          Geoffrey Simmons <geoff@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 <limits.h>
34
#include <stdlib.h>
35
#include <stdio.h>
36
#include <string.h>
37
38
#include "cache/cache.h"
39
40
#include "vmod_directors_shard_dir.h"
41
#include "vmod_directors_shard_cfg.h"
42
43
/*lint -esym(749,  shard_change_task_e::*) */
44
enum shard_change_task_e {
45
        _SHARD_TASK_E_INVALID = 0,
46
        CLEAR,
47
        ADD_BE,
48
        REMOVE_BE,
49
        _SHARD_TASK_E_MAX
50
};
51
52
struct shard_change_task {
53
        unsigned                                magic;
54
#define SHARD_CHANGE_TASK_MAGIC                 0x1e1168af
55
        enum shard_change_task_e                task;
56
        void                                    *priv;
57
        VCL_REAL                                weight;
58
        VSTAILQ_ENTRY(shard_change_task)        list;
59
};
60
61
struct shard_change {
62
        unsigned                                magic;
63
#define SHARD_CHANGE_MAGIC                      0xdff5c9a6
64
        struct vsl_log                          *vsl;
65
        struct sharddir                         *shardd;
66
        VSTAILQ_HEAD(,shard_change_task)        tasks;
67
};
68
69
struct backend_reconfig {
70
        struct sharddir * const shardd;
71
        unsigned                hint;   // on number of backends after reconfig
72
        unsigned                hole_n; // number of holes in backends array
73
        unsigned                hole_i; // index hint on first hole
74
};
75
76
/* forward decl */
77
static VCL_BOOL
78
change_reconfigure(VRT_CTX, struct shard_change *change, VCL_INT replicas);
79
80
/*
81
 * ============================================================
82
 * change / task list
83
 *
84
 * for backend reconfiguration, we create a change list on the VCL workspace in
85
 * a PRIV_TASK state, which we work in reconfigure.
86
 */
87
88
static void v_matchproto_(vmod_priv_fini_f)
89
shard_change_fini(VRT_CTX, void * priv)
90 19
{
91 19
        struct shard_change *change;
92
93 19
        if (priv == NULL)
94 0
                return;
95
96 19
        CAST_OBJ_NOTNULL(change, priv, SHARD_CHANGE_MAGIC);
97
98 19
        (void) change_reconfigure(ctx, change, 67);
99 19
}
100
101
static const struct vmod_priv_methods shard_change_priv_methods[1] = {{
102
        .magic = VMOD_PRIV_METHODS_MAGIC,
103
        .type = "vmod_directors_shard_cfg",
104
        .fini = shard_change_fini
105
}};
106
107
static struct shard_change *
108
shard_change_get(VRT_CTX, struct sharddir * const shardd)
109 197
{
110 197
        struct vmod_priv *task;
111 197
        struct shard_change *change;
112 197
        const void *id = (const char *)shardd + task_off_cfg;
113
114 197
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
115
116 197
        task = VRT_priv_task(ctx, id);
117 197
        if (task == NULL) {
118 0
                shard_fail(ctx, shardd->name, "%s", "no priv_task");
119 0
                return (NULL);
120 0
        }
121
122 197
        if (task->priv != NULL) {
123 178
                CAST_OBJ_NOTNULL(change, task->priv, SHARD_CHANGE_MAGIC);
124 178
                assert (change->vsl == ctx->vsl);
125 178
                assert (change->shardd == shardd);
126 178
                return (change);
127 178
        }
128
129 19
        WS_TASK_ALLOC_OBJ(ctx, change, SHARD_CHANGE_MAGIC);
130 19
        if (change == NULL)
131 0
                return (NULL);
132 19
        change->vsl = ctx->vsl;
133 19
        change->shardd = shardd;
134 19
        VSTAILQ_INIT(&change->tasks);
135 19
        task->priv = change;
136 19
        task->methods = shard_change_priv_methods;
137
138 19
        return (change);
139 19
}
140
141
static void
142
shard_change_finish(struct shard_change *change)
143 40
{
144 40
        CHECK_OBJ_NOTNULL(change, SHARD_CHANGE_MAGIC);
145
146 40
        VSTAILQ_INIT(&change->tasks);
147 40
}
148
149
static struct shard_change_task *
150
shard_change_task_add(VRT_CTX, struct shard_change *change,
151
    enum shard_change_task_e task_e, void *priv)
152 157
{
153 157
        struct shard_change_task *task;
154
155 157
        CHECK_OBJ_NOTNULL(change, SHARD_CHANGE_MAGIC);
156
157 157
        WS_TASK_ALLOC_OBJ(ctx, task, SHARD_CHANGE_TASK_MAGIC);
158 157
        if (task == NULL)
159 0
                return (NULL);
160 157
        task->task = task_e;
161 157
        task->priv = priv;
162 157
        VSTAILQ_INSERT_TAIL(&change->tasks, task, list);
163
164 157
        return (task);
165 157
}
166
167
static inline struct shard_change_task *
168
shard_change_task_backend(VRT_CTX, struct sharddir *shardd,
169
    enum shard_change_task_e task_e, VCL_BACKEND be, VCL_STRING ident,
170
    VCL_DURATION rampup)
171 142
{
172 142
        struct shard_change *change;
173 142
        struct shard_backend *b;
174
175 142
        CHECK_OBJ_NOTNULL(shardd, SHARDDIR_MAGIC);
176 142
        assert(task_e == ADD_BE || task_e == REMOVE_BE);
177
178 142
        change = shard_change_get(ctx, shardd);
179 142
        if (change == NULL)
180 0
                return (NULL);
181
182 142
        b = WS_Alloc(ctx->ws, sizeof(*b));
183 142
        if (b == NULL) {
184 0
                shard_fail(ctx, change->shardd->name, "%s",
185 0
                    "could not get workspace for task");
186 0
                return (NULL);
187 0
        }
188
189 142
        b->backend = NULL;
190 142
        VRT_Assign_Backend(&b->backend, be);
191 142
        b->ident = ident != NULL && *ident != '\0' ? ident : NULL;
192 142
        b->rampup = rampup;
193
194 142
        return (shard_change_task_add(ctx, change, task_e, b));
195 142
}
196
197
/*
198
 * ============================================================
199
 * director reconfiguration tasks
200
 */
201
VCL_BOOL
202
shardcfg_add_backend(VRT_CTX, struct sharddir *shardd,
203
    VCL_BACKEND be, VCL_STRING ident, VCL_DURATION rampup, VCL_REAL weight)
204 113
{
205 113
        struct shard_change_task *task;
206
207 113
        assert (weight >= 1);
208 113
        AN(be);
209
210 113
        task = shard_change_task_backend(ctx, shardd, ADD_BE,
211 113
            be, ident, rampup);
212
213 113
        if (task == NULL)
214 0
                return (0);
215
216 113
        task->weight = weight;
217 113
        return (1);
218 113
}
219
220
VCL_BOOL
221
shardcfg_remove_backend(VRT_CTX, struct sharddir *shardd,
222
    VCL_BACKEND be, VCL_STRING ident)
223 29
{
224 29
        return (shard_change_task_backend(ctx, shardd, REMOVE_BE,
225 29
            be, ident, 0) != NULL);
226 29
}
227
228
VCL_BOOL
229
shardcfg_clear(VRT_CTX, struct sharddir *shardd)
230 15
{
231 15
        struct shard_change *change;
232
233 15
        CHECK_OBJ_NOTNULL(shardd, SHARDDIR_MAGIC);
234
235 15
        change = shard_change_get(ctx, shardd);
236 15
        if (change == NULL)
237 0
                return (0);
238
239 15
        return (shard_change_task_add(ctx, change, CLEAR, NULL) != NULL);
240 15
}
241
242
/*
243
 * ============================================================
244
 * consistent hashing circle init
245
 */
246
247
typedef int (*compar)( const void*, const void* );
248
249
static int
250
circlepoint_compare(const struct shard_circlepoint *a,
251
    const struct shard_circlepoint *b)
252 10343
{
253 10343
        return ((a->point == b->point) ? 0 : ((a->point > b->point) ? 1 : -1));
254 10343
}
255
256
static void
257
shardcfg_hashcircle(struct sharddir *shardd)
258 36
{
259 36
        const struct shard_backend *backends, *b;
260 36
        unsigned h;
261 36
        uint32_t i, j, n_points, r, rmax;
262 36
        const char *ident;
263 36
        char s[12]; // log10(UINT32_MAX) + 2;
264
265 36
        CHECK_OBJ_NOTNULL(shardd, SHARDDIR_MAGIC);
266 36
        AZ(shardd->hashcircle);
267
268 36
        assert(shardd->n_backend > 0);
269 36
        backends=shardd->backend;
270 36
        AN(backends);
271
272 36
        n_points = 0;
273 36
        rmax = (UINT32_MAX - 1) / shardd->n_backend;
274 187
        for (b = backends; b < backends + shardd->n_backend; b++) {
275 151
                CHECK_OBJ_NOTNULL(b->backend, DIRECTOR_MAGIC);
276 151
                n_points += vmin_t(uint32_t, b->replicas, rmax);
277 151
        }
278
279 36
        assert(n_points < UINT32_MAX);
280
281 36
        shardd->n_points = n_points;
282 36
        shardd->hashcircle = calloc(n_points, sizeof(struct shard_circlepoint));
283 36
        AN(shardd->hashcircle);
284
285 36
        i = 0;
286 187
        for (h = 0, b = backends; h < shardd->n_backend; h++, b++) {
287 151
                ident = b->ident ? b->ident : VRT_BACKEND_string(b->backend);
288
289 151
                AN(ident);
290 151
                assert(ident[0] != '\0');
291
292 151
                r = vmin_t(uint32_t, b->replicas, rmax);
293
294 1835
                for (j = 0; j < r; j++) {
295 1684
                        bprintf(s, "%d", j);
296 1684
                        assert (i < n_points);
297 1684
                        shardd->hashcircle[i].point =
298 1684
                            VRT_HashStrands32(TOSTRANDS(2, ident, s));
299 1684
                        shardd->hashcircle[i].host = h;
300 1684
                        i++;
301 1684
                }
302 151
        }
303 36
        assert (i == n_points);
304 36
        qsort( (void *) shardd->hashcircle, n_points,
305 36
            sizeof (struct shard_circlepoint), (compar) circlepoint_compare);
306
307 36
        if ((shardd->debug_flags & SHDBG_CIRCLE) == 0)
308 13
                return;
309
310 629
        for (i = 0; i < n_points; i++)
311 606
                SHDBG(SHDBG_CIRCLE, shardd,
312 23
                    "hashcircle[%5jd] = {point = %8x, host = %2u}\n",
313 23
                    (intmax_t)i, shardd->hashcircle[i].point,
314 23
                    shardd->hashcircle[i].host);
315 23
}
316
317
/*
318
 * ============================================================
319
 * configure the director backends
320
 */
321
322
static void
323
shardcfg_backend_free(struct shard_backend *f)
324 46
{
325 46
        if (f->freeptr)
326 35
                free (f->freeptr);
327 46
        VRT_Assign_Backend(&f->backend, NULL);
328 46
        memset(f, 0, sizeof(*f));
329 46
}
330
331
static void
332
shardcfg_backend_copyin(struct shard_backend *dst,
333
    const struct shard_backend *src)
334 97
{
335 97
        dst->backend = src->backend;
336 97
        dst->ident = src->ident ? strdup(src->ident) : NULL;
337 97
        dst->rampup = src->rampup;
338 97
}
339
340
static int
341
shardcfg_backend_cmp(const struct shard_backend *a,
342
    const struct shard_backend *b)
343 714
{
344 714
        const char *ai, *bi;
345
346 714
        ai = a->ident;
347 714
        bi = b->ident;
348
349 714
        assert(ai || a->backend);
350 714
        assert(bi || b->backend);
351
352
        /* vcl_names are unique, so we can compare the backend pointers */
353 714
        if (ai == NULL && bi == NULL)
354 47
                return (a->backend != b->backend);
355
356 667
        if (ai == NULL)
357 6
                ai = VRT_BACKEND_string(a->backend);
358
359 667
        if (bi == NULL)
360 15
                bi = VRT_BACKEND_string(b->backend);
361
362 667
        AN(ai);
363 667
        AN(bi);
364 667
        return (vstrcmp(ai, bi));
365 667
}
366
367
/* for removal, we delete all instances if the backend matches */
368
static int
369
shardcfg_backend_del_cmp(const struct shard_backend *task,
370
    const struct shard_backend *b)
371 325
{
372 325
        assert(task->backend || task->ident);
373
374 325
        if (task->ident == NULL)
375 9
                return (task->backend != b->backend);
376
377 316
        return (shardcfg_backend_cmp(task, b));
378 325
}
379
380
static const struct shard_backend *
381
shardcfg_backend_lookup(const struct backend_reconfig *re,
382
    const struct shard_backend *b)
383 105
{
384 105
        unsigned i, max = re->shardd->n_backend + re->hole_n;
385 105
        const struct shard_backend *bb = re->shardd->backend;
386
387 105
        if (max > 0)
388 80
                AN(bb);
389
390 501
        for (i = 0; i < max; i++) {
391 404
                if (bb[i].backend == NULL)
392 6
                        continue;       // hole
393 398
                if (!shardcfg_backend_cmp(b, &bb[i]))
394 8
                        return (&bb[i]);
395 398
        }
396 97
        return (NULL);
397 105
}
398
399
static void
400
shardcfg_backend_expand(const struct backend_reconfig *re)
401 19
{
402 19
        unsigned min = re->hint;
403
404 19
        CHECK_OBJ_NOTNULL(re->shardd, SHARDDIR_MAGIC);
405
406 19
        min = vmax_t(unsigned, min, 16);
407
408 19
        if (re->shardd->l_backend < min)
409 19
                re->shardd->l_backend = min;
410 0
        else
411 0
                re->shardd->l_backend *= 2;
412
413 19
        re->shardd->backend = realloc(re->shardd->backend,
414 19
            re->shardd->l_backend * sizeof *re->shardd->backend);
415
416 19
        AN(re->shardd->backend);
417 19
}
418
419
static void
420
shardcfg_backend_add(struct backend_reconfig *re,
421
    const struct shard_backend *b, uint32_t replicas)
422 97
{
423 97
        unsigned i;
424 97
        struct shard_backend *bb = re->shardd->backend;
425
426 97
        if (re->hole_n == 0) {
427 93
                if (re->shardd->n_backend >= re->shardd->l_backend) {
428 19
                        shardcfg_backend_expand(re);
429 19
                        bb = re->shardd->backend;
430 19
                }
431 93
                assert(re->shardd->n_backend < re->shardd->l_backend);
432 93
                i = re->shardd->n_backend;
433 93
        } else {
434 4
                assert(re->hole_i != UINT_MAX);
435 4
                do {
436 4
                        if (!bb[re->hole_i].backend)
437 4
                                break;
438 4
                } while (++(re->hole_i) < re->shardd->n_backend + re->hole_n);
439 4
                assert(re->hole_i < re->shardd->n_backend + re->hole_n);
440
441 4
                i = (re->hole_i)++;
442 4
                (re->hole_n)--;
443 4
        }
444
445 97
        re->shardd->n_backend++;
446 97
        shardcfg_backend_copyin(&bb[i], b);
447 97
        bb[i].replicas = replicas;
448 97
}
449
450
void
451
shardcfg_backend_clear(struct sharddir *shardd)
452 18
{
453 18
        unsigned i;
454 33
        for (i = 0; i < shardd->n_backend; i++)
455 15
                shardcfg_backend_free(&shardd->backend[i]);
456 18
        shardd->n_backend = 0;
457 18
}
458
459
460
static void
461
shardcfg_backend_del(struct backend_reconfig *re, struct shard_backend *spec)
462 29
{
463 29
        unsigned i, max = re->shardd->n_backend + re->hole_n;
464 29
        struct shard_backend * const bb = re->shardd->backend;
465
466 476
        for (i = 0; i < max; i++) {
467 447
                if (bb[i].backend == NULL)
468 122
                        continue;       // hole
469 325
                if (shardcfg_backend_del_cmp(spec, &bb[i]))
470 294
                        continue;
471
472 31
                shardcfg_backend_free(&bb[i]);
473 31
                re->shardd->n_backend--;
474 31
                if (i < re->shardd->n_backend + re->hole_n) {
475 27
                        (re->hole_n)++;
476 27
                        re->hole_i = vmin(re->hole_i, i);
477 27
                }
478 31
        }
479 29
        VRT_Assign_Backend(&spec->backend, NULL);
480 29
}
481
482
static void
483
shardcfg_backend_finalize(struct backend_reconfig *re)
484 36
{
485 36
        unsigned i;
486 36
        struct shard_backend * const bb = re->shardd->backend;
487
488 42
        while (re->hole_n > 0) {
489
                // trim end
490 9
                i = re->shardd->n_backend + re->hole_n - 1;
491 26
                while (re->hole_n && bb[i].backend == NULL) {
492 17
                        (re->hole_n)--;
493 17
                        i--;
494 17
                }
495
496 9
                if (re->hole_n == 0)
497 3
                        break;
498
499 6
                assert(re->hole_i < i);
500
501 6
                do {
502 6
                        if (!bb[re->hole_i].backend)
503 6
                                break;
504 6
                } while (++(re->hole_i) <= i);
505
506 6
                assert(re->hole_i < i);
507 6
                assert(bb[re->hole_i].backend == NULL);
508 6
                assert(bb[i].backend != NULL);
509
510 6
                vmemcpy(&bb[re->hole_i], &bb[i], sizeof(*bb));
511 6
                memset(&bb[i], 0, sizeof(*bb));
512
513 6
                (re->hole_n)--;
514 6
                (re->hole_i)++;
515 6
        }
516
517 36
        assert(re->hole_n == 0);
518 36
}
519
520
/*
521
 * ============================================================
522
 * work the change tasks
523
 */
524
525
static void
526
shardcfg_apply_change(struct vsl_log *vsl, struct sharddir *shardd,
527
    const struct shard_change *change, VCL_INT replicas)
528 40
{
529 40
        struct shard_change_task *task, *clear;
530 40
        const struct shard_backend *b;
531 40
        uint32_t b_replicas;
532
533 40
        struct backend_reconfig re = {
534 40
                .shardd = shardd,
535 40
                .hint = shardd->n_backend,
536 40
                .hole_n = 0,
537 40
                .hole_i = UINT_MAX
538 40
        };
539
540
        // XXX assert sharddir_locked(shardd)
541
542 40
        clear = NULL;
543 157
        VSTAILQ_FOREACH(task, &change->tasks, list) {
544 157
                CHECK_OBJ_NOTNULL(task, SHARD_CHANGE_TASK_MAGIC);
545 157
                switch (task->task) {
546 15
                case CLEAR:
547 15
                        clear = task;
548 15
                        re.hint = 0;
549 15
                        break;
550 113
                case ADD_BE:
551 113
                        re.hint++;
552 113
                        break;
553 29
                case REMOVE_BE:
554 29
                        break;
555 0
                default:
556 0
                        INCOMPL();
557 157
                }
558 157
        }
559
560 40
        if (clear) {
561 13
                shardcfg_backend_clear(shardd);
562 13
                clear = VSTAILQ_NEXT(clear, list);
563 13
                if (clear == NULL)
564 4
                        return;
565 13
        }
566
567 36
        task = clear;
568 134
        VSTAILQ_FOREACH_FROM(task, &change->tasks, list) {
569 134
                CHECK_OBJ_NOTNULL(task, SHARD_CHANGE_TASK_MAGIC);
570 134
                switch (task->task) {
571 0
                case CLEAR:
572 0
                        assert(task->task != CLEAR);
573 0
                        break;
574 105
                case ADD_BE:
575 105
                        b = shardcfg_backend_lookup(&re, task->priv);
576
577 105
                        if (b == NULL) {
578 97
                                assert (task->weight >= 1);
579 97
                                if (replicas * task->weight > UINT32_MAX)
580 0
                                        b_replicas = UINT32_MAX;
581 97
                                else
582 97
                                        b_replicas = (uint32_t) // flint
583 97
                                                (replicas * task->weight);
584
585 97
                                shardcfg_backend_add(&re, task->priv,
586 97
                                    b_replicas);
587 97
                                break;
588 97
                        }
589
590 8
                        const char * const ident = b->ident;
591
592 8
                        shard_notice(vsl, shardd->name,
593 8
                            "backend %s%s%s already exists - skipping",
594 8
                            VRT_BACKEND_string(b->backend),
595 8
                            ident ? "/" : "",
596 8
                            ident ? ident : "");
597 8
                        break;
598 29
                case REMOVE_BE:
599 29
                        shardcfg_backend_del(&re, task->priv);
600 29
                        break;
601 0
                default:
602 0
                        INCOMPL();
603 134
                }
604 134
        }
605 36
        shardcfg_backend_finalize(&re);
606 36
}
607
608
/*
609
 * ============================================================
610
 * top reconfiguration function
611
 */
612
613
static VCL_BOOL
614
change_reconfigure(VRT_CTX, struct shard_change *change, VCL_INT replicas)
615 59
{
616 59
        struct sharddir *shardd;
617
618 59
        CHECK_OBJ_NOTNULL(change, SHARD_CHANGE_MAGIC);
619 59
        assert (replicas > 0);
620 59
        shardd = change->shardd;
621 59
        CHECK_OBJ_NOTNULL(shardd, SHARDDIR_MAGIC);
622
623 59
        if (VSTAILQ_FIRST(&change->tasks) == NULL)
624 19
                return (1);
625
626 40
        sharddir_wrlock(shardd);
627
628 40
        shardcfg_apply_change(ctx->vsl, shardd, change, replicas);
629 40
        shard_change_finish(change);
630
631 40
        if (shardd->hashcircle)
632 18
                free(shardd->hashcircle);
633 40
        shardd->hashcircle = NULL;
634
635 40
        if (shardd->n_backend == 0) {
636 4
                shard_err0(ctx->vsl, shardd->name,
637 4
                    ".reconfigure() no backends");
638 4
                sharddir_unlock(shardd);
639 4
                return (0);
640 4
        }
641
642 36
        shardcfg_hashcircle(shardd);
643 36
        sharddir_unlock(shardd);
644 36
        return (1);
645 40
}
646
647
VCL_BOOL
648
shardcfg_reconfigure(VRT_CTX, struct sharddir *shardd, VCL_INT replicas)
649 42
{
650 42
        struct shard_change *change;
651
652 42
        CHECK_OBJ_NOTNULL(shardd, SHARDDIR_MAGIC);
653 42
        if (replicas <= 0) {
654 2
                shard_err(ctx->vsl, shardd->name,
655 2
                    ".reconfigure() invalid replicas argument %jd",
656 2
                    (intmax_t)replicas);
657 2
                return (0);
658 2
        }
659
660 40
        change = shard_change_get(ctx, shardd);
661 40
        if (change == NULL)
662 0
                return (0);
663
664 40
        return (change_reconfigure(ctx, change, replicas));
665 40
}
666
667
/*
668
 * ============================================================
669
 * misc config related
670
 */
671
672
/* only for sharddir_delete() */
673
void
674
shardcfg_delete(const struct sharddir *shardd)
675 5
{
676
677 5
        AZ(shardd->n_backend);
678 5
        if (shardd->backend)
679 0
                free(shardd->backend);
680 5
        if (shardd->hashcircle)
681 0
                free(shardd->hashcircle);
682 5
}
683
684
VCL_VOID
685
shardcfg_set_warmup(struct sharddir *shardd, VCL_REAL ratio)
686 2
{
687 2
        CHECK_OBJ_NOTNULL(shardd, SHARDDIR_MAGIC);
688 2
        assert(ratio >= 0 && ratio < 1);
689 2
        sharddir_wrlock(shardd);
690 2
        shardd->warmup = ratio;
691 2
        sharddir_unlock(shardd);
692 2
}
693
694
VCL_VOID
695
shardcfg_set_rampup(struct sharddir *shardd, VCL_DURATION duration)
696 2
{
697 2
        CHECK_OBJ_NOTNULL(shardd, SHARDDIR_MAGIC);
698 2
        assert(duration >= 0);
699 2
        sharddir_wrlock(shardd);
700 2
        shardd->rampup_duration = duration;
701 2
        sharddir_unlock(shardd);
702 2
}
703
704
VCL_DURATION
705
shardcfg_get_rampup(const struct sharddir *shardd, unsigned host)
706 162
{
707 162
        VCL_DURATION r;
708
709 162
        CHECK_OBJ_NOTNULL(shardd, SHARDDIR_MAGIC);
710
        // assert sharddir_rdlock_held(shardd);
711 162
        assert (host < shardd->n_backend);
712
713 162
        if (isnan(shardd->backend[host].rampup))
714 158
                r = shardd->rampup_duration;
715 4
        else
716 4
                r = shardd->backend[host].rampup;
717
718 162
        return (r);
719 162
}