Skip to content

Commit

Permalink
Move the backend.* CLI functions to VDI level
Browse files Browse the repository at this point in the history
  • Loading branch information
bsdphk committed Nov 14, 2017
1 parent bcda8d9 commit 08cea68
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 146 deletions.
143 changes: 0 additions & 143 deletions bin/varnishd/cache/cache_backend_cfg.c
Expand Up @@ -32,13 +32,11 @@

#include "config.h"

#include <fnmatch.h>
#include <stdio.h>
#include <stdlib.h>

#include "cache_varnishd.h"

#include "vcli_serve.h"
#include "vtim.h"
#include "waiter/waiter.h"

Expand Down Expand Up @@ -193,146 +191,6 @@ VBE_Delete(const struct director *d)
FREE_OBJ(be);
}

/*--------------------------------------------------------------------
* Test if backend is healthy and report when it last changed
*/

unsigned
VDI_Healthy(const struct director *d, double *changed)
{
CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC);

if (changed != NULL)
*changed = d->health_changed;

if (d->admin_health == VDI_AH_PROBE)
return (d->health);

if (d->admin_health == VDI_AH_SICK)
return (0);

if (d->admin_health == VDI_AH_DELETED)
return (0);

if (d->admin_health == VDI_AH_HEALTHY)
return (1);

WRONG("Wrong admin health");
}

/*---------------------------------------------------------------------*/

static int __match_proto__(vcl_be_func)
do_list(struct cli *cli, struct director *d, void *priv)
{
int *probes;
char time_str[VTIM_FORMAT_SIZE];
struct backend *be;

AN(priv);
probes = priv;
CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC);
CAST_OBJ_NOTNULL(be, d->priv, BACKEND_MAGIC);
if (d->admin_health == VDI_AH_DELETED)
return (0);

VCLI_Out(cli, "\n%-30s", d->display_name);

VCLI_Out(cli, " %-10s", VDI_Ahealth(d));

if (be->probe == NULL)
VCLI_Out(cli, " %-20s", "Healthy (no probe)");
else {
if (d->health)
VCLI_Out(cli, " %-20s", "Healthy ");
else
VCLI_Out(cli, " %-20s", "Sick ");
VBP_Status(cli, be, *probes);
}

VTIM_format(d->health_changed, time_str);
VCLI_Out(cli, " %s", time_str);

return (0);
}

static void __match_proto__(cli_func_t)
cli_backend_list(struct cli *cli, const char * const *av, void *priv)
{
int probes = 0;

(void)priv;
ASSERT_CLI();
if (av[2] != NULL && !strcmp(av[2], "-p")) {
av++;
probes = 1;
} else if (av[2] != NULL && av[2][0] == '-') {
VCLI_Out(cli, "Invalid flags %s", av[2]);
VCLI_SetResult(cli, CLIS_PARAM);
return;
} else if (av[3] != NULL) {
VCLI_Out(cli, "Too many arguments");
VCLI_SetResult(cli, CLIS_PARAM);
return;
}
VCLI_Out(cli, "%-30s %-10s %-20s %s", "Backend name", "Admin",
"Probe", "Last updated");
(void)VCL_IterDirector(cli, av[2], do_list, &probes);
}

/*---------------------------------------------------------------------*/

static int __match_proto__(vcl_be_func)
do_set_health(struct cli *cli, struct director *d, void *priv)
{
unsigned prev;

(void)cli;
AN(priv);
CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC);
if (d->admin_health == VDI_AH_DELETED)
return (0);
prev = VDI_Healthy(d, NULL);
d->admin_health = *(const struct vdi_ahealth **)priv;
(void)VDI_Ahealth(d); // Acts like type-check
if (prev != VDI_Healthy(d, NULL))
d->health_changed = VTIM_real();

return (0);
}

static void __match_proto__()
cli_backend_set_health(struct cli *cli, const char * const *av, void *priv)
{
const struct vdi_ahealth *ah;
int n;

(void)av;
(void)priv;
ASSERT_CLI();
AN(av[2]);
AN(av[3]);
ah = VDI_Str2Ahealth(av[3]);
if (ah == NULL || ah == VDI_AH_DELETED) {
VCLI_Out(cli, "Invalid state %s", av[3]);
VCLI_SetResult(cli, CLIS_PARAM);
return;
}
n = VCL_IterDirector(cli, av[2], do_set_health, &ah);
if (n == 0) {
VCLI_Out(cli, "No Backends matches");
VCLI_SetResult(cli, CLIS_PARAM);
}
}

/*---------------------------------------------------------------------*/

static struct cli_proto backend_cmds[] = {
{ CLICMD_BACKEND_LIST, "", cli_backend_list },
{ CLICMD_BACKEND_SET_HEALTH, "", cli_backend_set_health },
{ NULL }
};

/*---------------------------------------------------------------------*/

void
Expand Down Expand Up @@ -362,6 +220,5 @@ void
VBE_InitCfg(void)
{

CLI_AddFuncs(backend_cmds);
Lck_New(&backends_mtx, lck_vbe);
}
157 changes: 155 additions & 2 deletions bin/varnishd/cache/cache_director.c
Expand Up @@ -38,6 +38,10 @@
#include "cache_varnishd.h"

#include "cache_director.h"
#include "cache_backend.h"

#include "vcli_serve.h"
#include "vtim.h"

/* -------------------------------------------------------------------*/

Expand All @@ -51,8 +55,8 @@ struct vdi_ahealth {
VBE_AHEALTH_LIST
#undef VBE_AHEALTH

const struct vdi_ahealth *
VDI_Str2Ahealth(const char *t)
static const struct vdi_ahealth *
vdi_str2ahealth(const char *t)
{
#define VBE_AHEALTH(l,u) if (!strcasecmp(t, #l)) return (VDI_AH_##u);
VBE_AHEALTH_LIST
Expand Down Expand Up @@ -249,3 +253,152 @@ VDI_Panic(const struct director *d, struct vsb *vsb, const char *nm)
VSB_indent(vsb, -2);
VSB_printf(vsb, "},\n");
}

/*--------------------------------------------------------------------
* Test if backend is healthy and report when it last changed
*/

unsigned
VDI_Healthy(const struct director *d, double *changed)
{
CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC);

if (changed != NULL)
*changed = d->health_changed;

if (d->admin_health == VDI_AH_PROBE)
return (d->health);

if (d->admin_health == VDI_AH_SICK)
return (0);

if (d->admin_health == VDI_AH_DELETED)
return (0);

if (d->admin_health == VDI_AH_HEALTHY)
return (1);

WRONG("Wrong admin health");
}

/*---------------------------------------------------------------------*/

static int __match_proto__(vcl_be_func)
do_list(struct cli *cli, struct director *d, void *priv)
{
int *probes;
char time_str[VTIM_FORMAT_SIZE];
struct backend *be;

AN(priv);
probes = priv;
CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC);
CAST_OBJ_NOTNULL(be, d->priv, BACKEND_MAGIC);
if (d->admin_health == VDI_AH_DELETED)
return (0);

VCLI_Out(cli, "\n%-30s", d->display_name);

VCLI_Out(cli, " %-10s", VDI_Ahealth(d));

if (be->probe == NULL)
VCLI_Out(cli, " %-20s", "Healthy (no probe)");
else {
if (d->health)
VCLI_Out(cli, " %-20s", "Healthy ");
else
VCLI_Out(cli, " %-20s", "Sick ");
VBP_Status(cli, be, *probes);
}

VTIM_format(d->health_changed, time_str);
VCLI_Out(cli, " %s", time_str);

return (0);
}

static void __match_proto__(cli_func_t)
cli_backend_list(struct cli *cli, const char * const *av, void *priv)
{
int probes = 0;

(void)priv;
ASSERT_CLI();
if (av[2] != NULL && !strcmp(av[2], "-p")) {
av++;
probes = 1;
} else if (av[2] != NULL && av[2][0] == '-') {
VCLI_Out(cli, "Invalid flags %s", av[2]);
VCLI_SetResult(cli, CLIS_PARAM);
return;
} else if (av[3] != NULL) {
VCLI_Out(cli, "Too many arguments");
VCLI_SetResult(cli, CLIS_PARAM);
return;
}
VCLI_Out(cli, "%-30s %-10s %-20s %s", "Backend name", "Admin",
"Probe", "Last updated");
(void)VCL_IterDirector(cli, av[2], do_list, &probes);
}

/*---------------------------------------------------------------------*/

static int __match_proto__(vcl_be_func)
do_set_health(struct cli *cli, struct director *d, void *priv)
{
unsigned prev;

(void)cli;
AN(priv);
CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC);
if (d->admin_health == VDI_AH_DELETED)
return (0);
prev = VDI_Healthy(d, NULL);
d->admin_health = *(const struct vdi_ahealth **)priv;
(void)VDI_Ahealth(d); // Acts like type-check
if (prev != VDI_Healthy(d, NULL))
d->health_changed = VTIM_real();

return (0);
}

static void __match_proto__()
cli_backend_set_health(struct cli *cli, const char * const *av, void *priv)
{
const struct vdi_ahealth *ah;
int n;

(void)av;
(void)priv;
ASSERT_CLI();
AN(av[2]);
AN(av[3]);
ah = vdi_str2ahealth(av[3]);
if (ah == NULL || ah == VDI_AH_DELETED) {
VCLI_Out(cli, "Invalid state %s", av[3]);
VCLI_SetResult(cli, CLIS_PARAM);
return;
}
n = VCL_IterDirector(cli, av[2], do_set_health, &ah);
if (n == 0) {
VCLI_Out(cli, "No Backends matches");
VCLI_SetResult(cli, CLIS_PARAM);
}
}

/*---------------------------------------------------------------------*/

static struct cli_proto backend_cmds[] = {
{ CLICMD_BACKEND_LIST, "", cli_backend_list },
{ CLICMD_BACKEND_SET_HEALTH, "", cli_backend_set_health },
{ NULL }
};

/*---------------------------------------------------------------------*/

void
VDI_Init(void)
{

CLI_AddFuncs(backend_cmds);
}
1 change: 0 additions & 1 deletion bin/varnishd/cache/cache_director.h
Expand Up @@ -106,5 +106,4 @@ void VCL_DelDirector(struct director *);
VBE_AHEALTH_LIST
#undef VBE_AHEALTH

const struct vdi_ahealth *VDI_Str2Ahealth(const char *t);
const char *VDI_Ahealth(const struct director *d);
1 change: 1 addition & 0 deletions bin/varnishd/cache/cache_main.c
Expand Up @@ -339,6 +339,7 @@ child_main(int sigmagic, size_t altstksz)
VBO_Init();
VTP_Init();
VBP_Init();
VDI_Init();
VBE_InitCfg();
Pool_Init();
V1P_Init();
Expand Down
1 change: 1 addition & 0 deletions bin/varnishd/cache/cache_varnishd.h
Expand Up @@ -71,6 +71,7 @@ void VDI_Finish(struct worker *wrk, struct busyobj *bo);
enum sess_close VDI_Http1Pipe(struct req *, struct busyobj *);
void VDI_Panic(const struct director *, struct vsb *, const char *nm);
void VDI_Event(const struct director *d, enum vcl_event_e ev);
void VDI_Init(void);

/* cache_exp.c */
double EXP_Ttl(const struct req *, const struct objcore *);
Expand Down
3 changes: 3 additions & 0 deletions bin/varnishd/flint.lnt
Expand Up @@ -6,6 +6,9 @@
-printf(2, http_PrintfHeader)
-printf(2, VSB_printf)

-esym(759, VDI_AH_*) // could be moved from header to module
-esym(765, VDI_AH_*) // could be made static

-esym(755, vct_*)
-esym(759, vpf_*)
-esym(765, vpf_*)
Expand Down

0 comments on commit 08cea68

Please sign in to comment.