Skip to content

Commit

Permalink
vcp: Poll dying connection pools asynchronously
Browse files Browse the repository at this point in the history
Before, we would wait in a blocking loop for
connection pools to be freed before deleting
them for good.

This commit transforms this blocking loop into a
CLI hook that will delete the freed connection
pools at each trigger.

Refs #4064

Better diff with the --ignore-all-space option.
  • Loading branch information
cartoush committed Oct 2, 2024
1 parent 45faed2 commit 1b9e6cc
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
1 change: 1 addition & 0 deletions bin/varnishd/cache/cache_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ cli_cb_before(const struct cli *cli)
VSL(SLT_CLI, NO_VXID, "Rd %s", VSB_data(cli->cmd));
Lck_Lock(&cli_mtx);
VCL_Poll();
VCP_RelPoll();
}

static void
Expand Down
47 changes: 36 additions & 11 deletions bin/varnishd/cache/cache_conn_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,20 @@ struct conn_pool {
};

static struct lock conn_pools_mtx;
static struct lock dead_pools_mtx;

static VRBT_HEAD(vrb, conn_pool) conn_pools = VRBT_INITIALIZER(&conn_pools);
VRBT_HEAD(vrb, conn_pool);
VRBT_GENERATE_REMOVE_COLOR(vrb, conn_pool, entry, static)
VRBT_GENERATE_REMOVE(vrb, conn_pool, entry, static)
VRBT_GENERATE_FIND(vrb, conn_pool, entry, vcp_cmp, static)
VRBT_GENERATE_INSERT_COLOR(vrb, conn_pool, entry, static)
VRBT_GENERATE_INSERT_FINISH(vrb, conn_pool, entry, static)
VRBT_GENERATE_INSERT(vrb, conn_pool, entry, vcp_cmp, static)
VRBT_GENERATE_NEXT(vrb, conn_pool, entry, static);
VRBT_GENERATE_MINMAX(vrb, conn_pool, entry, static);

static struct vrb conn_pools = VRBT_INITIALIZER(&conn_pools);
static struct vrb dead_pools = VRBT_INITIALIZER(&dying_cps);

/*--------------------------------------------------------------------
*/
Expand Down Expand Up @@ -249,17 +255,35 @@ VCP_Rel(struct conn_pool **cpp)
(void)shutdown(pfd->fd, SHUT_RDWR);
cp->n_kill++;
}
while (cp->n_kill) {
Lck_Unlock(&cp->mtx);
(void)usleep(20000);
Lck_Lock(&cp->mtx);
}

Lck_Lock(&dead_pools_mtx);
VRBT_INSERT(vrb, &dead_pools, cp);
Lck_Unlock(&dead_pools_mtx);
Lck_Unlock(&cp->mtx);
Lck_Delete(&cp->mtx);
AZ(cp->n_conn);
AZ(cp->n_kill);
free(cp->endpoint);
FREE_OBJ(cp);
}

void
VCP_RelPoll(void)
{
struct conn_pool *cp, *cp2;

ASSERT_CLI();
Lck_Lock(&dead_pools_mtx);
VRBT_FOREACH_SAFE(cp, vrb, &dead_pools, cp2) {
CHECK_OBJ_NOTNULL(cp, CONN_POOL_MAGIC);
if (cp->n_kill > 0)
continue;
VRBT_REMOVE(vrb, &dead_pools, cp);
Lck_Unlock(&dead_pools_mtx);

Lck_Delete(&cp->mtx);
AZ(cp->n_conn);
AZ(cp->n_kill);
free(cp->endpoint);
FREE_OBJ(cp);
Lck_Lock(&dead_pools_mtx);
}
Lck_Unlock(&dead_pools_mtx);
}

/*--------------------------------------------------------------------
Expand Down Expand Up @@ -583,6 +607,7 @@ void
VCP_Init(void)
{
Lck_New(&conn_pools_mtx, lck_conn_pool);
Lck_New(&dead_pools_mtx, lck_dead_pool);
}

/**********************************************************************/
Expand Down
1 change: 1 addition & 0 deletions bin/varnishd/cache/cache_varnishd.h
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ void VSL_Flush(struct vsl_log *, int overflow);
struct conn_pool;
void VCP_Init(void);
void VCP_Panic(struct vsb *, struct conn_pool *);
void VCP_RelPoll(void);

/* cache_backend_probe.c */
void VBP_Init(void);
Expand Down
1 change: 1 addition & 0 deletions include/tbl/locks.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ LOCK(pipestat)
LOCK(probe)
LOCK(sess)
LOCK(conn_pool)
LOCK(dead_pool)
LOCK(vbe)
LOCK(vcapace)
LOCK(vcl)
Expand Down

0 comments on commit 1b9e6cc

Please sign in to comment.