Implement event loss prevention for db output
cover Implement event loss prevention for db output
Hello,
The first three patches are cleaning that could be necessary to avoid issue with the fourth one who does the real job.
The idea is to store the queries in memory if the database goes down and to proceed to the insertion when it gets back up. To do so, a chained list of queries is attached to the database instance and queries are added to it till a memory cap is not reached. When the database came back, the queries are played in order so it should not cause any issue for any potential triggers.
This new feature is inactive by default and can be activated by setting "backlog_memcap" to a non null value in the database instance configuration.
To avoid to cause overun on the event capture by inserting too many things at once at recovery (and thus blocking capture for a certain time), a maximum of "backlog_oneshot_requests" " are inserted at a time.
Patchset statistics:
include/ulogd/db.h | 34 ++++++++++-- output/mysql/ulogd_output_MYSQL.c | 4 +- output/pgsql/ulogd_output_PGSQL.c | 4 +- output/sqlite3/ulogd_output_SQLITE3.c | 2 + ulogd.conf.in | 9 +++ util/db.c | 170 +++++++++++++++++++++++++++++++++++++++++++++++---------- 6 files changed, 188 insertions(+), 35 deletions(-)
BR,
--
Eric Leblond <[email protected]>
PATCH 1/4 postgresql: add sanity checking
Clean postgresql handler at deinit.
--- output/pgsql/ulogd_output_PGSQL.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
output/pgsql/ulogd_output_PGSQL.c +3 -1
@@ -214,7 +214,9 @@ static int close_db_pgsql(struct ulogd_pluginstance *upi) {struct pgsql_instance *pi = (struct pgsql_instance *) upi->private; -PQfinish (pi->dbh); +if (pi->dbh) +PQfinish (pi->dbh); + pi->dbh =NULL ;return 0 ; }
Signature · 1 line
1.7.10.4
PATCH 2/4 mysql: add sanity checking
In reply to Eric LeblondNullify mysql handler at deinit.
--- output/mysql/ulogd_output_MYSQL.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
output/mysql/ulogd_output_MYSQL.c +3 -1
@@ -162,7 +162,9 @@ static int get_columns_mysql(struct ulogd_pluginstance *upi)static int close_db_mysql (struct ulogd_pluginstance *upi) {struct mysql_instance *mi = (struct mysql_instance *) upi->private; -mysql_close (mi->dbh); +if (mi->dbh) +mysql_close (mi->dbh); + pi->dbh =NULL ;return 0 ; }
Signature · 1 line
1.7.10.4
PATCH 3/4 sqlite3: add sanity checking
In reply to Eric LeblondNullify sqlite3 handler at deinit.
--- output/sqlite3/ulogd_output_SQLITE3.c | 2 ++ 1 file changed, 2 insertions(+)
output/sqlite3/ulogd_output_SQLITE3.c +2 -0
@@ -431,6 +431,8 @@ sqlite3_stop(struct ulogd_pluginstance *pi)sqlite3_close (priv->dbh); + priv->dbh =NULL ; +return 0 ; }
Signature · 1 line
1.7.10.4
PATCH 4/4 db: store data in memory during database downtime
In reply to Eric LeblondThis patch is adding a mechanism to store query in a backlog build in memory. This allow to store events during downtime in memory and realize the effective insertion when the database comes back. A memory cap is used to avoid any memory flooding.
--- include/ulogd/db.h | 34 +++++++++-- ulogd.conf.in | 9 +++ util/db.c | 170 +++++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 180 insertions(+), 33 deletions(-)
include/ulogd/db.h +29 -5
@@ -20,6 +20,12 @@ struct db_driver { const char *stmt, unsigned int len); }; +struct db_stmt { +char *stmt; +int len; +struct llist_head list; +}; +struct db_instance {char *stmt;/* buffer for our insert statement */ char *stmt_val;/* pointer to the beginning of the "VALUES" part */
@@ -28,9 +34,15 @@ struct db_instance {time_t reconnect;int (*interp)(struct ulogd_pluginstance *upi);struct db_driver *driver; +unsigned int backlog_memcap; +unsigned int backlog_memusage; +unsigned int backlog_oneshot; +unsigned char backlog_full; +struct llist_head backlog; };#define TIME_ERR ((time_t)-1)/* Be paranoid */ #define RECONNECT_DEFAULT 2 +#define MAX_ONESHOT_REQUEST 10#define DB_CES \ { \
@@ -51,13 +63,25 @@ struct db_instance { .key = "procedure", \ .type = CONFIG_TYPE_STRING, \ .options = CONFIG_OPT_MANDATORY, \ + }, \ + { \ + .key = "backlog_memcap", \ + .type = CONFIG_TYPE_INT, \ + .u.value = 0, \ + }, \ + { \ + .key = "backlog_oneshot_requests", \ + .type = CONFIG_TYPE_INT, \ + .u.value = MAX_ONESHOT_REQUEST, \ } -#define DB_CE_NUM 4 -#define table_ce (x) (x->ces[0]) -#define reconnect_ce (x) (x->ces[1]) -#define timeout_ce (x) (x->ces[2]) -#define procedure_ce (x) (x->ces[3]) +#define DB_CE_NUM 6 +#define table_ce (x) (x->ces[0]) +#define reconnect_ce (x) (x->ces[1]) +#define timeout_ce (x) (x->ces[2]) +#define procedure_ce (x) (x->ces[3]) +#define backlog_memcap_ce (x) (x->ces[4]) +#define backlog_oneshot_ce (x) (x->ces[5])void ulogd_db_signal (struct ulogd_pluginstance *upi,int signal);int ulogd_db_start (struct ulogd_pluginstance *upi);
ulogd.conf.in +9 -0
@@ -207,6 +207,13 @@ user="nupik" table="ulog" pass="changeme" procedure="INSERT_PACKET_FULL" +# backlog configuration: +# set backlog_memcap to the size of memory that will be +# allocated to store events in memory if data is temporary down +# and insert them when the database came back. +#backlog_memcap=1000000 +# number of events to insert at once when backlog is not empty +#backlog_oneshot_requests=10 [mysql2] db="nulog"
@@ -224,6 +231,8 @@ table="ulog" #schema="public" pass="changeme" procedure="INSERT_PACKET_FULL" +#backlog_memcap=1000000 +#backlog_oneshot_requests=10 [pgsql2] db="nulog"
util/db.c +142 -28
205 lines not on this page · raw message
Signature · 1 line
1.7.10.4