Skip to content

Commit

Permalink
vbp: Introduce a backend probe state & document fsm
Browse files Browse the repository at this point in the history
The state is not used yet other than for assertions.
  • Loading branch information
nigoroll committed Jul 8, 2024
1 parent 1cfa4e6 commit 96d9bdb
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
37 changes: 35 additions & 2 deletions bin/varnishd/cache/cache_backend_probe.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@

#include "VSC_vbe.h"

struct vbp_state {
const char *name;
};

#define VBP_STATE(n) const struct vbp_state vbp_state_ ## n [1] = {{ .name = #n }}
VBP_STATE(scheduled);
VBP_STATE(running);
VBP_STATE(cold);
VBP_STATE(deleted);
#undef VBP_STATE

/* Default averaging rate, we want something pretty responsive */
#define AVG_RATE 4

Expand Down Expand Up @@ -83,6 +94,7 @@ struct vbp_target {
double rate;

vtim_real due;
const struct vbp_state *state;
int running;
int heap_idx;
struct pool_task task[1];
Expand Down Expand Up @@ -449,15 +461,18 @@ vbp_task(struct worker *wrk, void *priv)

Lck_Lock(&vbp_mtx);
if (vt->running < 0) {
assert(vt->state == vbp_state_deleted);
assert(vt->heap_idx == VBH_NOIDX);
vbp_delete(vt);
} else {
assert(vt->state == vbp_state_running);
vt->running = 0;
if (vt->heap_idx != VBH_NOIDX) {
vt->due = VTIM_real() + vt->interval;
VBH_delete(vbp_heap, vt->heap_idx);
vbp_heap_insert(vt);
}
vt->state = vbp_state_scheduled;
}
Lck_Unlock(&vbp_mtx);
}
Expand Down Expand Up @@ -490,15 +505,22 @@ vbp_thread(struct worker *wrk, void *priv)
vt->due = now + vt->interval;
VBH_insert(vbp_heap, vt);
if (!vt->running) {
assert(vt->state == vbp_state_scheduled);
vt->state = vbp_state_running;
vt->running = 1;
vt->task->func = vbp_task;
vt->task->priv = vt;
Lck_Unlock(&vbp_mtx);
r = Pool_Task_Any(vt->task, TASK_QUEUE_REQ);
Lck_Lock(&vbp_mtx);
if (r)
if (r) {
vt->running = 0;
vt->state = vbp_state_scheduled;
}
}
else
assert(vt->state == vbp_state_running);

}
}
NEEDLESS(Lck_Unlock(&vbp_mtx));
Expand Down Expand Up @@ -658,12 +680,18 @@ VBP_Control(const struct backend *be, int enable)

Lck_Lock(&vbp_mtx);
if (enable) {
// XXX next two assertions are WRONG, see #4108 - WIP
assert(vt->state == vbp_state_cold);
assert(vt->heap_idx == VBH_NOIDX);
vt->due = VTIM_real();
vbp_heap_insert(vt);
vt->state = vbp_state_scheduled;
} else {
assert(vt->state == vbp_state_scheduled ||
vt->state == vbp_state_running);
assert(vt->heap_idx != VBH_NOIDX);
VBH_delete(vbp_heap, vt->heap_idx);
vt->state = vbp_state_cold;
}
Lck_Unlock(&vbp_mtx);
}
Expand All @@ -686,6 +714,7 @@ VBP_Insert(struct backend *b, const struct vrt_backend_probe *vp,
ALLOC_OBJ(vt, VBP_TARGET_MAGIC);
XXXAN(vt);

vt->state = vbp_state_cold;
vt->conn_pool = tp;
VCP_AddRef(vt->conn_pool);
vt->backend = b;
Expand All @@ -711,13 +740,17 @@ VBP_Remove(struct backend *be)
be->probe = NULL;
vt->backend = NULL;
if (vt->running) {
assert(vt->state == vbp_state_running);
// task scheduled, it calls vbp_delete()
vt->running = -1;
vt = NULL;
vt->state = vbp_state_deleted;
} else if (vt->heap_idx != VBH_NOIDX) {
assert(vt->state == vbp_state_scheduled);
// task done, not yet rescheduled
VBH_delete(vbp_heap, vt->heap_idx);
}
} else
assert(vt->state == vbp_state_cold);
Lck_Unlock(&vbp_mtx);
if (vt != NULL) {
assert(vt->heap_idx == VBH_NOIDX);
Expand Down
35 changes: 35 additions & 0 deletions doc/graphviz/cache_backend_probe.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# cache_backend_probe struct vbp_state

digraph cache_backend_probe {
ALLOC
scheduled
running
cold
deleted
FREE

edge [fontname=Courier]

edge [label="vbp_task()"]
deleted -> FREE
running -> scheduled

edge [label="vbp_thread()"]
scheduled -> running

edge [label="vbp_thread() error"]
scheduled -> scheduled

edge [label="VBP_Control()"]
cold -> scheduled
scheduled -> cold
running -> cold

edge [label="VBP_Insert()"]
ALLOC -> cold

edge [label="VBP_Remove()"]
running -> deleted # should not happen. we should go through some cool first
scheduled -> FREE # This should not happen. VBP_Control should have set cold
cold -> FREE
}

0 comments on commit 96d9bdb

Please sign in to comment.