Skip to content

Commit

Permalink
dialog: added h_id_start and h_id_step parameters
Browse files Browse the repository at this point in the history
- allow to control how internal dialog hash id is generated to avoid
overalapping values across many instances of kamailio
  • Loading branch information
miconda committed Apr 4, 2018
1 parent dc8faaf commit 8f9b6ea
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
16 changes: 16 additions & 0 deletions src/modules/dialog/dialog.c
Expand Up @@ -63,6 +63,7 @@
#include "../../core/mem/mem.h"
#include "../../core/timer_proc.h"
#include "../../core/lvalue.h"
#include "../../core/globals.h"
#include "../../core/parser/parse_to.h"
#include "../../modules/tm/tm_load.h"
#include "../../core/rpc_lookup.h"
Expand Down Expand Up @@ -124,6 +125,9 @@ str ruri_pvar_param = str_init("$ru");
pv_elem_t * ruri_param_model = NULL;
str empty_str = STR_NULL;

int dlg_h_id_start = 0;
int dlg_h_id_step = 1;

/* statistic variables */
int dlg_enable_stats = 1;
int active_dlgs_cnt = 0;
Expand Down Expand Up @@ -315,6 +319,8 @@ static param_export_t mod_params[]={
{ "early_timeout", PARAM_INT, &dlg_early_timeout },
{ "noack_timeout", PARAM_INT, &dlg_noack_timeout },
{ "end_timeout", PARAM_INT, &dlg_end_timeout },
{ "h_id_start", PARAM_INT, &dlg_h_id_start },
{ "h_id_step", PARAM_INT, &dlg_h_id_step },
{ 0,0,0 }
};

Expand Down Expand Up @@ -470,6 +476,16 @@ static int mod_init(void)
unsigned int n;
sr_cfgenv_t *cenv = NULL;

if(dlg_h_id_start==-1) {
dlg_h_id_start = server_id;
} else if(dlg_h_id_start<0) {
dlg_h_id_start = 0;
}

if(dlg_h_id_step<1) {
dlg_h_id_step = 1;
}

if(dlg_ka_interval!=0 && dlg_ka_interval<30) {
LM_ERR("ka interval too low (%d), has to be at least 30\n",
dlg_ka_interval);
Expand Down
34 changes: 29 additions & 5 deletions src/modules/dialog/dlg_db_handler.c
Expand Up @@ -82,7 +82,8 @@ static db_func_t dialog_dbf;
extern int dlg_enable_stats;
extern int active_dlgs_cnt;
extern int early_dlgs_cnt;

extern int dlg_h_id_start;
extern int dlg_h_id_step;

#define SET_STR_VALUE(_val, _str)\
do{\
Expand Down Expand Up @@ -281,6 +282,7 @@ int load_dialog_info_from_db(int dlg_hash_size, int fetch_num_rows,
dlg_iuid_t dbuid[DLG_MAX_DB_LOAD_EXTRA];
int loaded_extra = 0;
int loaded_extra_more = 0;
dlg_cell_t *dit;

if(use_dialog_table() != 0){
return -1;
Expand Down Expand Up @@ -363,17 +365,39 @@ int load_dialog_info_from_db(int dlg_hash_size, int fetch_num_rows,

if(mode!=0) {
dlg_lock(d_table, &(d_table->entries[dlg->h_entry]));
/* loading at runtime - check dialog id conflicts */
dit = (d_table->entries)[dlg->h_entry].first;
while (dit) {
if (dit->h_id == VAL_INT(values+1)) {
break;
}
dit = dit->next;
}
if(dit) {
LM_WARN("conflicting dialog id: %u/%u - skipping\n",
dlg->h_entry, (unsigned int)VAL_INT(values+1));
dlg_unlock(d_table, &(d_table->entries[dlg->h_entry]));
shm_free(dlg);
continue;
}
}

/*link the dialog*/
link_dlg(dlg, 0, 0);

dlg->h_id = VAL_INT(values+1);
next_id = d_table->entries[dlg->h_entry].next_id;

d_table->entries[dlg->h_entry].next_id =
(next_id <= dlg->h_id) ? (dlg->h_id+1) : next_id;

if(dlg_h_id_step==1) {
d_table->entries[dlg->h_entry].next_id =
(next_id <= dlg->h_id) ? (dlg->h_id+1) : next_id;
} else {
/* update next id only if matches this instance series */
if((dlg->h_id - dlg_h_id_start) % dlg_h_id_step == 0) {
d_table->entries[dlg->h_entry].next_id =
(next_id <= dlg->h_id) ? (dlg->h_id + dlg_h_id_step)
: next_id;
}
}
if(mode!=0) {
dlg_unlock(d_table, &(d_table->entries[dlg->h_entry]));
}
Expand Down
22 changes: 19 additions & 3 deletions src/modules/dialog/dlg_hash.c
Expand Up @@ -51,6 +51,8 @@ extern int dlg_enable_dmq;
extern int dlg_early_timeout;
extern int dlg_noack_timeout;
extern int dlg_end_timeout;
extern int dlg_h_id_start;
extern int dlg_h_id_step;

/*! global dialog table */
struct dlg_table *d_table = 0;
Expand Down Expand Up @@ -308,7 +310,12 @@ int init_dlg_table(unsigned int size)
LM_ERR("failed to init lock for slot: %d\n", i);
goto error1;
}
d_table->entries[i].next_id = kam_rand() % (3*size);
if(dlg_h_id_step>1) {
d_table->entries[i].next_id = dlg_h_id_start
+ (dlg_h_id_step * ((kam_rand() % (3*size)) + 1));
} else {
d_table->entries[i].next_id = kam_rand() % (3*size);
}
}

return 0;
Expand Down Expand Up @@ -884,8 +891,17 @@ void link_dlg(struct dlg_cell *dlg, int n, int mode)
if(unlikely(mode==0)) dlg_lock( d_table, d_entry);

/* keep id 0 for special cases */
dlg->h_id = 1 + d_entry->next_id++;
if(dlg->h_id == 0) dlg->h_id = 1;
if(dlg_h_id_step>1) {
if((d_entry->next_id==0)
|| (d_entry->next_id + dlg_h_id_step < d_entry->next_id)) {
d_entry->next_id = dlg_h_id_start + dlg_h_id_step;
}
dlg->h_id = d_entry->next_id;
d_entry->next_id += dlg_h_id_step;
} else {
dlg->h_id = 1 + d_entry->next_id++;
if(dlg->h_id == 0) dlg->h_id = 1;
}
LM_DBG("linking dialog [%u:%u]\n", dlg->h_entry, dlg->h_id);
if (d_entry->first==0) {
d_entry->first = d_entry->last = dlg;
Expand Down

0 comments on commit 8f9b6ea

Please sign in to comment.