From d803e475e58506eae56c6844c416e456dcbd5746 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20L=C3=A9caille?= Date: Thu, 25 Apr 2019 07:42:09 +0200 Subject: [PATCH] MINOR: log: Enable the log sampling and load-balancing feature. This patch implements the sampling and load-balancing of log servers configured with "sample" new keyword implemented by this commit: 'MINOR: log: Add "sample" new keyword to "log" lines'. As the list of ranges used to sample the log to balance is ordered, we only have to maintain ->curr_idx member of smp_info struct which is the index of the sample and check if it belongs or not to the current range to decide if we must send it to the log server or not. --- include/common/hathreads.h | 2 ++ include/proto/log.h | 11 +++++++++++ include/types/log.h | 2 ++ src/log.c | 25 +++++++++++++++++++++++-- 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/include/common/hathreads.h b/include/common/hathreads.h index 7cbb3495b..308bcb1f1 100644 --- a/include/common/hathreads.h +++ b/include/common/hathreads.h @@ -443,6 +443,7 @@ enum lock_label { START_LOCK, TLSKEYS_REF_LOCK, AUTH_LOCK, + LOGSRV_LOCK, OTHER_LOCK, LOCK_LABELS }; @@ -559,6 +560,7 @@ static inline const char *lock_label(enum lock_label label) case START_LOCK: return "START"; case TLSKEYS_REF_LOCK: return "TLSKEYS_REF"; case AUTH_LOCK: return "AUTH"; + case LOGSRV_LOCK: return "LOGSRV"; case OTHER_LOCK: return "OTHER"; case LOCK_LABELS: break; /* keep compiler happy */ }; diff --git a/include/proto/log.h b/include/proto/log.h index b05ab3efc..7b056c5fe 100644 --- a/include/proto/log.h +++ b/include/proto/log.h @@ -54,6 +54,17 @@ extern THREAD_LOCAL char *logline; extern THREAD_LOCAL char *logline_rfc5424; +/* + * Test if index numbered from 0 is in range with low and high + * limits of indexes numbered from 1. + */ +static inline int in_smp_log_range(struct smp_log_range *rg, unsigned int idx) +{ + if (idx + 1 <= rg->high && idx + 1 >= rg->low) + return 1; + return 0; +} + /* Initialize/Deinitialize log buffers used for syslog messages */ int init_log_buffers(); void deinit_log_buffers(); diff --git a/include/types/log.h b/include/types/log.h index 9f9912aa2..affcd92d2 100644 --- a/include/types/log.h +++ b/include/types/log.h @@ -26,6 +26,7 @@ #include #include #include +#include #include #define NB_LOG_FACILITIES 24 @@ -202,6 +203,7 @@ struct logsrv { int minlvl; int maxlen; struct logsrv *ref; + __decl_hathreads(HA_SPINLOCK_T lock); }; #endif /* _TYPES_LOG_H */ diff --git a/src/log.c b/src/log.c index cffd8f148..49de5709f 100644 --- a/src/log.c +++ b/src/log.c @@ -946,6 +946,7 @@ int parse_logsrv(char **args, struct list *logsrvs, int do_del, char **err) cur_arg += 2; } + HA_SPIN_INIT(&logsrv->lock); /* parse the facility */ logsrv->facility = get_log_facility(args[cur_arg]); if (logsrv->facility < 0) { @@ -1722,12 +1723,32 @@ void __send_log(struct proxy *p, int level, char *message, size_t size, char *sd /* Send log messages to syslog server. */ nblogger = 0; list_for_each_entry(logsrv, logsrvs, list) { + static THREAD_LOCAL int in_range = 1; + /* we can filter the level of the messages that are sent to each logger */ if (level > logsrv->level) continue; - __do_send_log(logsrv, ++nblogger, pid.area, pid.data, level, - message, size, sd, sd_size, tag->area, tag->data); + if (logsrv->lb.smp_rgs) { + struct smp_log_range *curr_rg; + + HA_SPIN_LOCK(LOGSRV_LOCK, &logsrv->lock); + curr_rg = &logsrv->lb.smp_rgs[logsrv->lb.curr_rg]; + in_range = in_smp_log_range(curr_rg, logsrv->lb.curr_idx); + if (in_range) { + /* Let's consume this range. */ + curr_rg->curr_idx = (curr_rg->curr_idx + 1) % curr_rg->sz; + if (!curr_rg->curr_idx) { + /* If consumed, let's select the next range. */ + logsrv->lb.curr_rg = (logsrv->lb.curr_rg + 1) % logsrv->lb.smp_rgs_sz; + } + } + logsrv->lb.curr_idx = (logsrv->lb.curr_idx + 1) % logsrv->lb.smp_sz; + HA_SPIN_UNLOCK(LOGSRV_LOCK, &logsrv->lock); + } + if (in_range) + __do_send_log(logsrv, ++nblogger, pid.area, pid.data, level, + message, size, sd, sd_size, tag->area, tag->data); } }