From 29947745b5dd1a0a45c8153f5c5b92b87cd27b74 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Wed, 28 Oct 2020 11:43:49 +0100 Subject: [PATCH] MINOR: pattern: store a generation number in the reference patterns Right now it's not possible to perform a safe reload because we don't know what patterns were recently added or were already present. This patch adds a generation counter to the reference patterns so that it is possible to know what generation of the reference they were loaded with. A reference now has two generations, the current one, used for all additions, and the next one, allocated to those wishing to update the contents. The generation wraps at 2^32 so comparisons must be made relative to the current position. The idea will be that upon full reload, the caller will first get a new generation ID, will insert all new patterns using it, will then switch the current ID to the new one, and will delete all entries older than the current ID. This has the benefit of supporting chunked updates that remain consistent and that won't block the whole process for ages like pat_ref_reload() currently does. --- include/haproxy/pattern-t.h | 3 +++ src/pattern.c | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/include/haproxy/pattern-t.h b/include/haproxy/pattern-t.h index 477b2bbc6..30ff999b5 100644 --- a/include/haproxy/pattern-t.h +++ b/include/haproxy/pattern-t.h @@ -106,6 +106,8 @@ struct pat_ref { struct list head; /* The head of the list of struct pat_ref_elt. */ struct list pat; /* The head of the list of struct pattern_expr. */ unsigned int flags; /* flags PAT_REF_*. */ + unsigned int curr_gen; /* current generation number (anything below can be removed) */ + unsigned int next_gen; /* next generation number (insertions use this one) */ int unique_id; /* Each pattern reference have unique id. */ unsigned long long revision; /* updated for each update */ __decl_thread(HA_SPINLOCK_T lock); /* Lock used to protect pat ref elements */ @@ -122,6 +124,7 @@ struct pat_ref_elt { struct list tree_head; /* all pattern_tree derived from this reference */ char *pattern; char *sample; + unsigned int gen_id; /* generation of pat_ref this was made for */ int line; }; diff --git a/src/pattern.c b/src/pattern.c index 7f4f44958..7b4d876e2 100644 --- a/src/pattern.c +++ b/src/pattern.c @@ -1756,6 +1756,8 @@ struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int f ref->reference = NULL; ref->flags = flags; + ref->curr_gen = 0; + ref->next_gen = 0; ref->unique_id = unique_id; LIST_INIT(&ref->head); LIST_INIT(&ref->pat); @@ -1767,7 +1769,8 @@ struct pat_ref *pat_ref_newid(int unique_id, const char *display, unsigned int f /* This function adds entry to . It can fail on memory error. It returns * the newly added element on success, or NULL on failure. The PATREF_LOCK on - * must be held. + * must be held. It sets the newly created pattern's generation number + * to the same value as the reference's. */ struct pat_ref_elt *pat_ref_append(struct pat_ref *ref, const char *pattern, const char *sample, int line) { @@ -1777,6 +1780,7 @@ struct pat_ref_elt *pat_ref_append(struct pat_ref *ref, const char *pattern, con if (!elt) goto fail; + elt->gen_id = ref->curr_gen; elt->line = line; elt->pattern = strdup(pattern);