Skip to content

Commit

Permalink
Add a notify method for directors and implement it for VBE
Browse files Browse the repository at this point in the history
Pondering solutions for varnishcache#4183 it became apparent that we currently have no way
for core code to notify directors of changes, in particular changes of the
admin_health. I looked into re-using the healthy callback, but it is unsuitable
because VRT_Healthy() does not even call it for an "auto" admin_health.

So the cleanest option is another director callback, which is cheap and also
helps us maintain clean layering.
  • Loading branch information
nigoroll committed Sep 17, 2024
1 parent 240ef2c commit 898f34a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
21 changes: 19 additions & 2 deletions bin/varnishd/cache/cache_backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,21 @@ vbe_healthy(VRT_CTX, VCL_BACKEND d, VCL_TIME *t)
return (!bp->sick);
}

static VCL_VOID v_matchproto_(vdi_notify_f)
vbe_notify(VCL_BACKEND d)
{
const struct vdi_ahealth *ah;
struct backend *bp;

CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC);
CAST_OBJ_NOTNULL(bp, d->priv, BACKEND_MAGIC);
CHECK_OBJ_NOTNULL(d->vdir, VCLDIR_MAGIC);
ah = d->vdir->admin_health;

// sick == 0 for _noprobe
if (ah == VDI_AH_SICK || (ah == VDI_AH_AUTO && bp->sick))
VBE_connwait_signal_all(bp);
}

/*--------------------------------------------------------------------
*/
Expand All @@ -690,7 +705,8 @@ static const struct vdi_methods vbe_methods[1] = {{
.destroy = vbe_destroy,
.panic = vbe_panic,
.list = vbe_list,
.healthy = vbe_healthy
.healthy = vbe_healthy,
.notify = vbe_notify
}};

static const struct vdi_methods vbe_methods_noprobe[1] = {{
Expand All @@ -703,7 +719,8 @@ static const struct vdi_methods vbe_methods_noprobe[1] = {{
.event = vbe_dir_event,
.destroy = vbe_destroy,
.panic = vbe_panic,
.list = vbe_list
.list = vbe_list,
.notify = vbe_notify
}};

/*--------------------------------------------------------------------
Expand Down
16 changes: 16 additions & 0 deletions bin/varnishd/cache/cache_director.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,22 @@ VRT_SetChanged(VCL_BACKEND d, VCL_TIME changed)
d->vdir->health_changed = changed;
}

/*--------------------------------------------------------------------
* Notify of change (admin_health for now) outside backend
*/

VCL_VOID
VRT_Notify(VCL_BACKEND d)
{

CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC);
CHECK_OBJ_NOTNULL(d->vdir, VCLDIR_MAGIC);
CHECK_OBJ_NOTNULL(d->vdir->methods, VDI_METHODS_MAGIC);
if (d->vdir->methods->notify == NULL)
return;
d->vdir->methods->notify(d);
}

/* Send Event ----------------------------------------------------------
*/

Expand Down
10 changes: 9 additions & 1 deletion include/vrt.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
# error "include vdef.h before vrt.h"
#endif

#define VRT_MAJOR_VERSION 20U
#define VRT_MAJOR_VERSION 21U

#define VRT_MINOR_VERSION 0U

Expand All @@ -58,6 +58,9 @@
* binary/load-time compatible, increment MAJOR version
*
* NEXT (2024-03-15)
* 21.0
* notify callback added to struct vdi_methods
* VRT_Notify() added
* 20.0 (2024-09-13)
* struct vrt_backend.backend_wait_timeout added
* struct vrt_backend.backend_wait_limit added
Expand Down Expand Up @@ -737,6 +740,7 @@ typedef void vdi_release_f(VCL_BACKEND);
typedef void vdi_destroy_f(VCL_BACKEND);
typedef void vdi_panic_f(VCL_BACKEND, struct vsb *);
typedef void vdi_list_f(VRT_CTX, VCL_BACKEND, struct vsb *, int, int);
typedef void vdi_notify_f(VCL_BACKEND);

struct vdi_methods {
unsigned magic;
Expand All @@ -755,6 +759,9 @@ struct vdi_methods {
vdi_destroy_f *destroy;
vdi_panic_f *panic;
vdi_list_f *list;
// when something changes outside the backend's control
// (for now, admin health)
vdi_notify_f *notify;
};

struct director {
Expand All @@ -767,6 +774,7 @@ struct director {
};

VCL_BOOL VRT_Healthy(VRT_CTX, VCL_BACKEND, VCL_TIME *);
VCL_VOID VRT_Notify(VCL_BACKEND);
VCL_VOID VRT_SetChanged(VCL_BACKEND, VCL_TIME);
VCL_BACKEND VRT_AddDirector(VRT_CTX, const struct vdi_methods *,
void *, const char *, ...) v_printflike_(4, 5);
Expand Down

0 comments on commit 898f34a

Please sign in to comment.