[CLEANUP] stick_table: add/clarify some comments

This commit is contained in:
Willy Tarreau 2010-06-06 11:56:36 +02:00
parent 2799e98a36
commit aea940eb23
3 changed files with 71 additions and 66 deletions

View File

@ -29,9 +29,9 @@
/* pattern in and out types */ /* pattern in and out types */
enum { enum {
PATTERN_TYPE_IP = 0, /* ipv4 type */ PATTERN_TYPE_IP = 0, /* ipv4 type */
PATTERN_TYPE_INTEGER = 1, /* unsigned 32bits integer type */ PATTERN_TYPE_INTEGER, /* unsigned 32bits integer type */
PATTERN_TYPE_STRING = 2, /* char string type */ PATTERN_TYPE_STRING, /* char string type */
PATTERN_TYPES PATTERN_TYPES /* number of types, must always be last */
}; };
/* pattern fetch direction */ /* pattern fetch direction */

View File

@ -31,14 +31,15 @@
#include <common/memory.h> #include <common/memory.h>
/* stick table key types */ /* stick table key types */
#define STKTABLE_TYPE_IP 0 /* table key is ipv4 */ enum {
#define STKTABLE_TYPE_INTEGER 1 /* table key is unsigned 32bit integer */ STKTABLE_TYPE_IP = 0, /* table key is ipv4 */
#define STKTABLE_TYPE_STRING 2 /* table key is a null terminated string */ STKTABLE_TYPE_INTEGER, /* table key is unsigned 32bit integer */
STKTABLE_TYPE_STRING, /* table key is a null terminated string */
STKTABLE_TYPES /* Number of types, must always be last */
};
#define STKTABLE_TYPES 3 /* Increase this value if you add a type */ /* stick table key type flags */
#define STK_F_CUSTOM_KEYSIZE 0x00000001 /* this table's key size is configurable */
/* stick table type flags */
#define STKTABLE_TYPEFLAG_CUSTOMKEYSIZE 0x00000001 /* this table type maxsize is configurable */
/* stick table keyword type */ /* stick table keyword type */
struct stktable_type { struct stktable_type {
@ -47,30 +48,32 @@ struct stktable_type {
size_t default_size; /* default key size */ size_t default_size; /* default key size */
}; };
/* stuck session */ /* sticky session */
struct stksess { struct stksess {
int sid; /* id of server to use for session */ int sid; /* id of server to use for this session */
unsigned int expire; /* session expiration date */ unsigned int expire; /* session expiration date */
struct eb32_node exps; /* ebtree node used to hold the session in expiration tree */ struct eb32_node exps; /* ebtree node used to hold the session in expiration tree */
struct ebmb_node keys; /* ebtree node used to hold the session in table */ struct ebmb_node keys; /* ebtree node used to hold the session in table */
/* WARNING! do not put anything after <keys>, it's used by the key */
}; };
/* stick table */ /* stick table */
struct stktable { struct stktable {
struct eb_root keys; /* head of stuck session tree */ struct eb_root keys; /* head of sticky session tree */
struct eb_root exps; /* head of stuck session expiration tree */ struct eb_root exps; /* head of sticky session expiration tree */
struct pool_head *pool; /* pool used to allocate stuck sessions */ struct pool_head *pool; /* pool used to allocate sticky sessions */
struct task *exp_task; /* expiration task */ struct task *exp_task; /* expiration task */
unsigned long type; /* type of table (determine key format) */ unsigned long type; /* type of table (determines key format) */
size_t key_size; /* size of a key, maximum size in case of string */ size_t key_size; /* size of a key, maximum size in case of string */
unsigned int size; /* maximum stuck session in table */ unsigned int size; /* maximum number of sticky sessions in table */
unsigned int current; /* number of stuck session in table */ unsigned int current; /* number of sticky sessions currently in table */
int nopurge; /* 1 never purge stuck sessions */ int nopurge; /* if non-zero, don't purge sticky sessions when full */
int exp_next; /* next epiration date */ int exp_next; /* next expiration date (ticks) */
int expire; /* duration before expiration of stuck session */ int expire; /* time to live for sticky sessions (milliseconds) */
}; };
/*** The definitions below should probably be better placed in pattern.h ***/
/* stick table key data */ /* stick table key data */
union stktable_key_data { union stktable_key_data {
struct in_addr ip; /* used to store an ip key */ struct in_addr ip; /* used to store an ip key */

View File

@ -29,8 +29,8 @@
/* /*
* Free an allocate sticked session <ts>. * Free an allocated sticky session <ts>, and decrease sticky sessions counter
* Decrease table <t> sticked session counter . * in table <t>.
*/ */
void stksess_free(struct stktable *t, struct stksess *ts) void stksess_free(struct stktable *t, struct stksess *ts)
{ {
@ -39,7 +39,8 @@ void stksess_free(struct stktable *t, struct stksess *ts)
} }
/* /*
* Init or modify <key> of th sticked session <ts> present in table <t>. * Initialize or update the key in the sticky session <ts> present in table <t>
* from the value present in <key>.
*/ */
void stksess_key(struct stktable *t, struct stksess *ts, struct stktable_key *key) void stksess_key(struct stktable *t, struct stksess *ts, struct stktable_key *key)
{ {
@ -53,7 +54,7 @@ void stksess_key(struct stktable *t, struct stksess *ts, struct stktable_key *ke
/* /*
* Init sticked session <ts> using <key>. * Init sticky session <ts> of table <t> using <key>.
*/ */
struct stksess *stksess_init(struct stktable *t, struct stksess * ts, struct stktable_key *key) struct stksess *stksess_init(struct stktable *t, struct stksess * ts, struct stktable_key *key)
{ {
@ -66,8 +67,8 @@ struct stksess *stksess_init(struct stktable *t, struct stksess * ts, struct stk
} }
/* /*
* Trash oldest <to_batch> sticked sessions from table <t> * Trash oldest <to_batch> sticky sessions from table <t>
* Returns number of trashed sticked session. * Returns number of trashed sticky sessions.
*/ */
static int stktable_trash_oldest(struct stktable *t, int to_batch) static int stktable_trash_oldest(struct stktable *t, int to_batch)
{ {
@ -109,8 +110,8 @@ static int stktable_trash_oldest(struct stktable *t, int to_batch)
continue; continue;
} }
/* session expired, trash it */
/* session expired, trash it */
ebmb_delete(&ts->keys); ebmb_delete(&ts->keys);
stksess_free(t, ts); stksess_free(t, ts);
batched++; batched++;
@ -120,11 +121,10 @@ static int stktable_trash_oldest(struct stktable *t, int to_batch)
} }
/* /*
* Allocate and initialise a new sticked session. * Allocate and initialise a new sticky session.
* The new sticked session is returned or NULL in case of lack of memory. * The new sticky session is returned or NULL in case of lack of memory.
* Sticked sessions should only be allocated this way, and must be * Sticky sessions should only be allocated this way, and must be freed using
* freed using stksess_free(). * stksess_free(). Increase table <t> sticky session counter.
* Increase table <t> sticked session counter.
*/ */
struct stksess *stksess_new(struct stktable *t, struct stktable_key *key) struct stksess *stksess_new(struct stktable *t, struct stktable_key *key)
{ {
@ -148,14 +148,13 @@ struct stksess *stksess_new(struct stktable *t, struct stktable_key *key)
} }
/* /*
* Lookup in table <t> for a sticked session identified by <key>. * Looks in table <t> for a sticky session matching <key>.
* Returns pointer on requested sticked session or NULL if no one found. * Returns pointer on requested sticky session or NULL if none was found.
*/ */
struct stksess *stktable_lookup(struct stktable *t, struct stktable_key *key) struct stksess *stktable_lookup(struct stktable *t, struct stktable_key *key)
{ {
struct ebmb_node *eb; struct ebmb_node *eb;
/* lookup on track session */
if (t->type == STKTABLE_TYPE_STRING) if (t->type == STKTABLE_TYPE_STRING)
eb = ebst_lookup_len(&t->keys, key->key, key->key_len); eb = ebst_lookup_len(&t->keys, key->key, key->key_len);
else else
@ -166,39 +165,40 @@ struct stksess *stktable_lookup(struct stktable *t, struct stktable_key *key)
return NULL; return NULL;
} }
/* Existing session, returns server id */
return ebmb_entry(eb, struct stksess, keys); return ebmb_entry(eb, struct stksess, keys);
} }
/* /* Try to store sticky session <ts> in the table. If another entry already
* Store sticked session if not present in table. * exists with the same key, its server ID is updated with <sid> and a non
* Il already present, update the existing session. * zero value is returned so that the caller knows it can release its stksess.
* If no similar entry was present, <ts> is inserted into the tree and assigned
* server ID <sid>. Zero is returned in this case, and the caller must not
* release the stksess.
*/ */
int stktable_store(struct stktable *t, struct stksess *tsess, int sid) int stktable_store(struct stktable *t, struct stksess *ts, int sid)
{ {
struct stksess *ts;
struct ebmb_node *eb; struct ebmb_node *eb;
if (t->type == STKTABLE_TYPE_STRING) if (t->type == STKTABLE_TYPE_STRING)
eb = ebst_lookup(&(t->keys), (char *)tsess->keys.key); eb = ebst_lookup(&(t->keys), (char *)ts->keys.key);
else else
eb = ebmb_lookup(&(t->keys), tsess->keys.key, t->key_size); eb = ebmb_lookup(&(t->keys), ts->keys.key, t->key_size);
if (unlikely(!eb)) { if (unlikely(!eb)) {
tsess->sid = sid; /* no existing session, insert ours */
ebmb_insert(&t->keys, &tsess->keys, t->key_size); ts->sid = sid;
ebmb_insert(&t->keys, &ts->keys, t->key_size);
tsess->exps.key = tsess->expire = tick_add(now_ms, MS_TO_TICKS(t->expire)); ts->exps.key = ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
eb32_insert(&t->exps, &tsess->exps); eb32_insert(&t->exps, &ts->exps);
if (t->expire) { if (t->expire) {
t->exp_task->expire = t->exp_next = tick_first(tsess->expire, t->exp_next); t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
task_queue(t->exp_task); task_queue(t->exp_task);
} }
return 0; return 0;
} }
/* Existing track session */
ts = ebmb_entry(eb, struct stksess, keys); ts = ebmb_entry(eb, struct stksess, keys);
if ( ts->sid != sid ) if ( ts->sid != sid )
@ -207,7 +207,8 @@ int stktable_store(struct stktable *t, struct stksess *tsess, int sid)
} }
/* /*
* Trash expired sticked sessions from table <t>. * Trash expired sticky sessions from table <t>. The next expiration date is
* returned.
*/ */
static int stktable_trash_expired(struct stktable *t) static int stktable_trash_expired(struct stktable *t)
{ {
@ -262,7 +263,8 @@ static int stktable_trash_expired(struct stktable *t)
} }
/* /*
* Task processing function to trash expired sticked sessions. * Task processing function to trash expired sticky sessions. A pointer to the
* task itself is returned since it never dies.
*/ */
static struct task *process_table_expire(struct task *task) static struct task *process_table_expire(struct task *task)
{ {
@ -272,7 +274,7 @@ static struct task *process_table_expire(struct task * task)
return task; return task;
} }
/* Perform minimal intializations, report 0 in case of error, 1 if OK. */ /* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */
int stktable_init(struct stktable *t) int stktable_init(struct stktable *t)
{ {
if (t->size) { if (t->size) {
@ -298,7 +300,7 @@ int stktable_init(struct stktable *t)
*/ */
struct stktable_type stktable_types[STKTABLE_TYPES] = { { "ip", 0, 4 } , struct stktable_type stktable_types[STKTABLE_TYPES] = { { "ip", 0, 4 } ,
{ "integer", 0, 4 }, { "integer", 0, 4 },
{ "string", STKTABLE_TYPEFLAG_CUSTOMKEYSIZE, 32 } }; { "string", STK_F_CUSTOM_KEYSIZE, 32 } };
/* /*
@ -315,7 +317,7 @@ int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *ke
*key_size = stktable_types[*type].default_size; *key_size = stktable_types[*type].default_size;
(*myidx)++; (*myidx)++;
if (stktable_types[*type].flags & STKTABLE_TYPEFLAG_CUSTOMKEYSIZE) { if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) {
if (strcmp("len", args[*myidx]) == 0) { if (strcmp("len", args[*myidx]) == 0) {
(*myidx)++; (*myidx)++;
*key_size = atol(args[*myidx]); *key_size = atol(args[*myidx]);