Move the backlog fields into a separate structure along the same lines as the ring-buffer. Signed-off-by: Jeremy Sowden <jeremy@xxxxxxxxxx> --- include/ulogd/db.h | 18 +++++++++++++----- util/db.c | 44 ++++++++++++++++++++++---------------------- 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/include/ulogd/db.h b/include/ulogd/db.h index ebf4f42917c3..fc3b15ef0e0f 100644 --- a/include/ulogd/db.h +++ b/include/ulogd/db.h @@ -45,6 +45,18 @@ struct db_stmt_ring { }; +struct db_stmt_backlog { + + struct llist_head items; + + unsigned int memcap; + unsigned int memusage; + unsigned int oneshot; + + int full; + +}; + struct db_stmt { char *stmt; int len; @@ -61,11 +73,7 @@ struct db_instance { /* DB ring buffer */ struct db_stmt_ring ring; /* Backlog system */ - unsigned int backlog_memcap; - unsigned int backlog_memusage; - unsigned int backlog_oneshot; - unsigned char backlog_full; - struct llist_head backlog; + struct db_stmt_backlog backlog; }; #define RECONNECT_DEFAULT 2 diff --git a/util/db.c b/util/db.c index 8a870846332b..89c81d8d1dc5 100644 --- a/util/db.c +++ b/util/db.c @@ -259,7 +259,7 @@ _interp_db_init(struct ulogd_pluginstance *upi) if (di->reconnect && di->reconnect > time(NULL)) { /* store entry to backlog if it is active */ - if (di->backlog_memcap && !di->backlog_full) { + if (di->backlog.memcap && !di->backlog.full) { _bind_sql_stmt(upi, di->stmt); _add_to_backlog(upi, di->stmt, strlen(di->stmt)); } @@ -268,7 +268,7 @@ _interp_db_init(struct ulogd_pluginstance *upi) if (di->driver->open_db(upi) < 0) { ulogd_log(ULOGD_ERROR, "can't establish database connection\n"); - if (di->backlog_memcap && !di->backlog_full) { + if (di->backlog.memcap && !di->backlog.full) { _bind_sql_stmt(upi, di->stmt); _add_to_backlog(upi, di->stmt, strlen(di->stmt)); } @@ -302,7 +302,7 @@ _interp_db_main(struct ulogd_pluginstance *upi) _bind_sql_stmt(upi, di->stmt); /* if backup log is not empty we add current query to it */ - if (!llist_empty(&di->backlog)) { + if (!llist_empty(&di->backlog.items)) { int ret = _add_to_backlog(upi, di->stmt, strlen(di->stmt)); if (ret == 0) { if (_process_backlog(upi) < 0) @@ -623,27 +623,27 @@ _configure_backlog(struct ulogd_pluginstance *upi) { struct db_instance *di = (struct db_instance *) &upi->private; - INIT_LLIST_HEAD(&di->backlog); + INIT_LLIST_HEAD(&di->backlog.items); - di->backlog_memusage = 0; - di->backlog_memcap = backlog_memcap_ce(upi->config_kset).u.value; - di->backlog_full = 0; + di->backlog.memusage = 0; + di->backlog.memcap = backlog_memcap_ce(upi->config_kset).u.value; + di->backlog.full = 0; - if (di->backlog_memcap == 0) + if (di->backlog.memcap == 0) return 0; if (ringsize_ce(upi->config_kset).u.value) { ulogd_log(ULOGD_ERROR, "Ring buffer has precedence over backlog\n"); - di->backlog_memcap = 0; + di->backlog.memcap = 0; return 0; } - di->backlog_oneshot = backlog_oneshot_ce(upi->config_kset).u.value; - if (di->backlog_oneshot <= 2) { + di->backlog.oneshot = backlog_oneshot_ce(upi->config_kset).u.value; + if (di->backlog.oneshot <= 2) { ulogd_log(ULOGD_ERROR, "backlog_oneshot_requests must be > 2 to be effective. Setting it to 3.\n"); - di->backlog_oneshot = 3; + di->backlog.oneshot = 3; } return 0; @@ -658,17 +658,17 @@ _add_to_backlog(struct ulogd_pluginstance *upi, struct db_stmt *query; /* check if we are using backlog */ - if (di->backlog_memcap == 0) + if (di->backlog.memcap == 0) return 0; query_size = sizeof(*query) + len + 1; /* check len against backlog */ - if (query_size + di->backlog_memcap - di->backlog_memusage) { - if (di->backlog_full == 0) + if (query_size + di->backlog.memcap - di->backlog.memusage) { + if (!di->backlog.full) ulogd_log(ULOGD_ERROR, "Backlog is full starting to reject events.\n"); - di->backlog_full = 1; + di->backlog.full = 1; return -1; } @@ -684,10 +684,10 @@ _add_to_backlog(struct ulogd_pluginstance *upi, return -1; } - di->backlog_memusage += query_size; - di->backlog_full = 0; + di->backlog.memusage += query_size; + di->backlog.full = 0; - llist_add_tail(&query->list, &di->backlog); + llist_add_tail(&query->list, &di->backlog.items); return 0; } @@ -696,7 +696,7 @@ static int _process_backlog(struct ulogd_pluginstance *upi) { struct db_instance *di = (struct db_instance *) &upi->private; - int i = di->backlog_oneshot; + int i = di->backlog.oneshot; struct db_stmt *query; struct db_stmt *nquery; @@ -704,13 +704,13 @@ _process_backlog(struct ulogd_pluginstance *upi) if (di->reconnect && di->reconnect > time(NULL)) return 0; - llist_for_each_entry_safe(query, nquery, &di->backlog, list) { + llist_for_each_entry_safe(query, nquery, &di->backlog.items, list) { if (di->driver->execute(upi, query->stmt, query->len) < 0) { /* error occur, database connexion need to be closed */ di->driver->close_db(upi); return _reconnect_db(upi); } else { - di->backlog_memusage -= sizeof(*query) + query->len + 1; + di->backlog.memusage -= sizeof(*query) + query->len + 1; llist_del(&query->list); free(query->stmt); free(query); -- 2.35.1