Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion modules/dialog/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ modparam("dialog", "enable_stats", 0)

The size of the hash table internally used to keep the dialogs. A
larger table is much faster but consumes more memory. The hash size
must be a power of 2 number.
must be a power of 2 number, at most 16777216.


IMPORTANT: If dialogs' information should be stored in a database,
Expand Down Expand Up @@ -1056,6 +1056,13 @@ Consult the [clusterer - Capabilities](../clusterer/README.md#capabilities-layer
chapter for more details.


Nodes without the *instance_id* parameter drop the replication packets
of the nodes that have it. While a cluster is half upgraded, the old
nodes get none of the dialogs created on upgraded nodes, and a dialog
created on an old node stays there until its timeout if it ends through
an upgraded node. Upgrade a cluster as a whole, or drain each node first.


*Default value is "0" (no replication).*


Expand All @@ -1066,6 +1073,34 @@ modparam("dialog", "dialog_replication_cluster", 1)
```


#### instance_id (int)


The instance of this OpenSIPS in the dialog ids: it is stored in the top
byte of the hash entry, so dialogs created by different instances never
get the same id. Each node replicating dialogs to the others must use a
different instance.


The value must be between 0 and 127; 0 leaves the dialog ids as they were
before. OpenSIPS refuses to start with an invalid value.


Do not downgrade to an OpenSIPS version without this parameter while
the database holds dialogs created with an instance.


*Default value is the node id in the *dialog_replication_cluster*,
if set, else "0".*


```opensips title="Set instance_id parameter"
...
modparam("dialog", "instance_id", 3)
...
```


#### profile_replication_cluster (int)


Expand Down
23 changes: 23 additions & 0 deletions modules/dialog/dialog.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ str cdb_url = {0,0};
/* dialog replication using clusterer */
int dialog_repl_cluster = 0;
int profile_repl_cluster = 0;
/* -1: the node id in dialog_replication_cluster, if set */
int dlg_instance_id = -1;
str dlg_repl_cap = str_init("dialog-dlg-repl");
str prof_repl_cap = str_init("dialog-prof-repl");

Expand Down Expand Up @@ -368,6 +370,7 @@ static const param_export_t mod_params[]={
{ "profile_timeout", INT_PARAM, &profile_timeout },
/* dialog replication through clusterer using TCP binary packets */
{ "dialog_replication_cluster", INT_PARAM, &dialog_repl_cluster },
{ "instance_id", INT_PARAM, &dlg_instance_id },
{ "profile_replication_cluster", INT_PARAM, &profile_repl_cluster },
{ "replicate_profiles_timer", INT_PARAM, &repl_prof_utimer },
{ "replicate_profiles_check", INT_PARAM, &repl_prof_timer_check },
Expand Down Expand Up @@ -998,6 +1001,26 @@ static int mod_init(void)
}
}

if (dlg_instance_id == -1) {
dlg_instance_id = dialog_repl_cluster ? clusterer_api.get_my_id() : 0;
if (dlg_instance_id < 0 || dlg_instance_id > DLG_INSTANCE_MAX) {
LM_ERR("node id %d in dialog_replication_cluster is not a valid "
"dialog instance (0-%d), set instance_id\n",
dlg_instance_id, DLG_INSTANCE_MAX);
return -1;
}
} else if (dlg_instance_id < 0 || dlg_instance_id > DLG_INSTANCE_MAX) {
LM_ERR("invalid instance_id %d, must be 0-%d\n",
dlg_instance_id, DLG_INSTANCE_MAX);
return -1;
}

if ((unsigned int)dlg_hash_size > DLG_HASH_SIZE_MAX) {
LM_ERR("hash_size %d is too big, max is %u\n",
dlg_hash_size, DLG_HASH_SIZE_MAX);
return -1;
}

if ( init_dlg_table(dlg_hash_size)<0 ) {
LM_ERR("failed to create hash table\n");
return -1;
Expand Down
41 changes: 27 additions & 14 deletions modules/dialog/dlg_db_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,13 @@ static int load_dialog_info_from_db(int dlg_hash_size)
GET_STR_VALUE(from_tag, values, 3, 1, 0);
GET_STR_VALUE(to_tag, values, 5, 1, 0);

d_entry = &d_table->entries[hash_entry];
if (dlg_h_idx(hash_entry) >= d_table->size) {
LM_ERR("dialog %u:%u does not fit hash_size %u -> skipping\n",
hash_entry, hash_id, d_table->size);
continue;
}

d_entry = &d_table->entries[dlg_h_idx(hash_entry)];
dlg_lock(d_table, d_entry);

if (get_dlg_unsafe(d_entry, &callid, &from_tag, &to_tag,
Expand Down Expand Up @@ -666,7 +672,7 @@ static int load_dialog_info_from_db(int dlg_hash_size)
goto error_unlock;
}

if(dlg->h_entry != hash_entry){
if(dlg_h_idx(dlg->h_entry) != dlg_h_idx(hash_entry)){
dlg_unlock(d_table, d_entry);
LM_ERR("inconsistent hash data in the dialog database: "
"you may have restarted opensips using a different "
Expand All @@ -681,11 +687,12 @@ static int load_dialog_info_from_db(int dlg_hash_size)
/* link the dialog */
link_dlg_unsafe(d_entry, dlg);

dlg->h_entry = hash_entry;
dlg->h_id = hash_id;

/* next_id follows the max value of all loaded ids */
if (d_table->entries[dlg->h_entry].next_id <= dlg->h_id)
d_table->entries[dlg->h_entry].next_id = dlg->h_id + 1;
if (d_table->entries[dlg_h_idx(dlg->h_entry)].next_id <= dlg->h_id)
d_table->entries[dlg_h_idx(dlg->h_entry)].next_id = dlg->h_id + 1;

GET_STR_VALUE(to_tag, values, 5, 1, 1);

Expand Down Expand Up @@ -944,7 +951,7 @@ int dlg_timer_remove_from_db(struct dlg_cell *cell)
/* from timer point of view, we are done with the dialogs */
for (i=0;i<dlg_bulk_del_no;i++) {
cell = dlg_del_holder[i];
unref_dlg_unsafe(cell,1,&(d_table->entries[cell->h_entry]));
unref_dlg_unsafe(cell,1,&(d_table->entries[dlg_h_idx(cell->h_entry)]));
}

dlg_del_curr_no = 0;
Expand Down Expand Up @@ -1034,7 +1041,7 @@ int update_dialog_timeout_info(struct dlg_cell * cell)
VAL_TYPE(values+1) = DB_INT;

/* lock the entry */
entry = (d_table->entries)[cell->h_entry];
entry = (d_table->entries)[dlg_h_idx(cell->h_entry)];
dlg_lock( d_table, &entry);

SET_BIGINT_VALUE(values, dlg_get_db_id(cell));
Expand Down Expand Up @@ -1108,7 +1115,7 @@ int update_dialog_dbinfo(struct dlg_cell * cell)
VAL_TYPE(values+18) = DB_BLOB;

/* lock the entry */
entry = (d_table->entries)[cell->h_entry];
entry = (d_table->entries)[dlg_h_idx(cell->h_entry)];
dlg_lock( d_table, &entry);

SET_BIGINT_VALUE(values, dlg_get_db_id(cell));
Expand Down Expand Up @@ -1177,7 +1184,7 @@ int update_dialog_dbinfo(struct dlg_cell * cell)
VAL_TYPE(values+18) = DB_BLOB;

/* lock the entry */
entry = (d_table->entries)[cell->h_entry];
entry = (d_table->entries)[dlg_h_idx(cell->h_entry)];
dlg_lock( d_table, &entry);

SET_BIGINT_VALUE(values, dlg_get_db_id(cell));
Expand Down Expand Up @@ -1214,7 +1221,7 @@ int update_dialog_dbinfo(struct dlg_cell * cell)
VAL_TYPE(values+21) = DB_INT;

/* lock the entry */
entry = (d_table->entries)[cell->h_entry];
entry = (d_table->entries)[dlg_h_idx(cell->h_entry)];
dlg_lock( d_table, &entry);

SET_BIGINT_VALUE(values, dlg_get_db_id(cell));
Expand Down Expand Up @@ -1934,9 +1941,14 @@ static int sync_dlg_db_mem(void)
GET_STR_VALUE(from_tag, values, 3, 1, 0);
GET_STR_VALUE(to_tag, values, 5, 1, 0);

/* TODO - check about hash resize ? maybe hash was lowered & we overflow the hash */
if (dlg_h_idx(hash_entry) >= d_table->size) {
LM_ERR("dialog %u:%u does not fit hash_size %u -> skipping\n",
hash_entry, hash_id, d_table->size);
continue;
}

known_dlg = 0;
d_entry = &(d_table->entries[hash_entry]);
d_entry = &(d_table->entries[dlg_h_idx(hash_entry)]);

/* lock the whole entry */
dlg_lock( d_table, d_entry);
Expand All @@ -1961,7 +1973,7 @@ static int sync_dlg_db_mem(void)
goto error;
}

if(dlg->h_entry != hash_entry){
if(dlg_h_idx(dlg->h_entry) != dlg_h_idx(hash_entry)){
LM_ERR("inconsistent hash data in the dialog database: "
"you may have restarted opensips using a different "
"hash_size: please erase %.*s database and restart\n",
Expand All @@ -1973,11 +1985,12 @@ static int sync_dlg_db_mem(void)
/*link the dialog*/
link_dlg(dlg, 0);

dlg->h_entry = hash_entry;
dlg->h_id = hash_id;

/* next_id follows the max value of all loaded ids */
if (d_table->entries[dlg->h_entry].next_id <= dlg->h_id)
d_table->entries[dlg->h_entry].next_id = dlg->h_id + 1;
if (d_table->entries[dlg_h_idx(dlg->h_entry)].next_id <= dlg->h_id)
d_table->entries[dlg_h_idx(dlg->h_entry)].next_id = dlg->h_id + 1;

dlg->start_ts = VAL_INT(values+6);

Expand Down
6 changes: 3 additions & 3 deletions modules/dialog/dlg_handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -2237,7 +2237,7 @@ void dlg_onroute(struct sip_msg* req, str *route_params, void *param)
ctx_dialog_set(dlg);
ctx_lastdstleg_set(dst_leg);
log_bogus_dst_leg(dlg);
d_entry = &(d_table->entries[dlg->h_entry]);
d_entry = &(d_table->entries[dlg_h_idx(dlg->h_entry)]);

/* if there was a transaction created before, store the dialog in it */
t = d_tmb.t_gett();
Expand Down Expand Up @@ -2858,7 +2858,7 @@ void dlg_ondelete(struct dlg_tl *tl)

dlg = get_dlg_del_tl_payload(tl);

d_entry = &(d_table->entries[dlg->h_entry]);
d_entry = &(d_table->entries[dlg_h_idx(dlg->h_entry)]);

dlg_lock( d_table, d_entry);

Expand Down Expand Up @@ -3288,7 +3288,7 @@ int test_and_set_dlg_flag(struct dlg_cell *dlg, unsigned int mask,
if (value)
value = mask;

d_entry = &(d_table->entries[dlg->h_entry]);
d_entry = &(d_table->entries[dlg_h_idx(dlg->h_entry)]);
dlg_lock (d_table,d_entry);

if ((dlg->user_flags & mask) == value) {
Expand Down
22 changes: 11 additions & 11 deletions modules/dialog/dlg_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ struct dlg_cell* build_new_dlg( str *callid, str *from_uri, str *to_uri,
#endif

dlg->state = DLG_STATE_UNCONFIRMED;
dlg->h_entry = dlg_hash( callid);
dlg->h_entry = dlg_h_entry(dlg_hash(callid), dlg_instance_id);

LM_DBG("new dialog %p (c=%.*s,f=%.*s,t=%.*s,ft=%.*s) on hash %u\n",
dlg, callid->len,callid->s, from_uri->len, from_uri->s,
Expand Down Expand Up @@ -779,15 +779,15 @@ struct dlg_cell* lookup_dlg( unsigned int h_entry, unsigned int h_id,
struct dlg_cell *dlg;
struct dlg_entry *d_entry;

if (h_entry>=d_table->size)
if (dlg_h_idx(h_entry)>=d_table->size)
goto not_found;

d_entry = &(d_table->entries[h_entry]);
d_entry = &(d_table->entries[dlg_h_idx(h_entry)]);

dlg_lock( d_table, d_entry);

for( dlg=d_entry->first ; dlg ; dlg=dlg->next ) {
if (dlg->h_id == h_id) {
if (dlg->h_id == h_id && dlg->h_entry == h_entry) {
if (active_only && dlg->state==DLG_STATE_DELETED) {
dlg_unlock( d_table, d_entry);
goto not_found;
Expand Down Expand Up @@ -940,17 +940,17 @@ struct dlg_cell* get_dlg_by_did(str *did, int active_only)
if (parse_dlg_did(did, &h_entry, &h_id) < 0)
return NULL;

if (h_entry>=d_table->size)
if (dlg_h_idx(h_entry)>=d_table->size)
return NULL;

LM_DBG("looking for hentry=%d hid=%d\n", h_entry, h_id);

d_entry = &(d_table->entries[h_entry]);
d_entry = &(d_table->entries[dlg_h_idx(h_entry)]);
dlg_lock( d_table, d_entry);
for( dlg = d_entry->first ; dlg ; dlg = dlg->next ) {
if (active_only && dlg->state>DLG_STATE_CONFIRMED )
continue;
if (dlg->h_id == h_id) {
if (dlg->h_id == h_id && dlg->h_entry == h_entry) {
ref_dlg_unsafe( dlg, 1);
dlg_unlock( d_table, d_entry);
return dlg;
Expand Down Expand Up @@ -992,7 +992,7 @@ void link_dlg(struct dlg_cell *dlg, int extra_refs)
{
struct dlg_entry *d_entry;

d_entry = &(d_table->entries[dlg->h_entry]);
d_entry = &(d_table->entries[dlg_h_idx(dlg->h_entry)]);

dlg_lock(d_table, d_entry);

Expand Down Expand Up @@ -1032,7 +1032,7 @@ void _ref_dlg(struct dlg_cell *dlg, unsigned int cnt)
{
struct dlg_entry *d_entry;

d_entry = &(d_table->entries[dlg->h_entry]);
d_entry = &(d_table->entries[dlg_h_idx(dlg->h_entry)]);

dlg_lock( d_table, d_entry);
ref_dlg_unsafe( dlg, cnt);
Expand All @@ -1043,7 +1043,7 @@ void _unref_dlg(struct dlg_cell *dlg, unsigned int cnt)
{
struct dlg_entry *d_entry;

d_entry = &(d_table->entries[dlg->h_entry]);
d_entry = &(d_table->entries[dlg_h_idx(dlg->h_entry)]);

dlg_lock( d_table, d_entry);
unref_dlg_unsafe( dlg, cnt, d_entry);
Expand Down Expand Up @@ -1197,7 +1197,7 @@ void next_state_dlg(struct dlg_cell *dlg, int event, int dir, int *old_state,
{
struct dlg_entry *d_entry;

d_entry = &(d_table->entries[dlg->h_entry]);
d_entry = &(d_table->entries[dlg_h_idx(dlg->h_entry)]);
*unref = 0;

dlg_lock( d_table, d_entry);
Expand Down
16 changes: 14 additions & 2 deletions modules/dialog/dlg_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,18 @@ struct dlg_cell *get_current_dialog();

#define dlg_hash(_callid) core_hash(_callid, NULL, d_table->size)

/* the top byte of h_entry holds the instance that created the dialog,
* the low bytes the hash table index */
#define DLG_INSTANCE_SHIFT 24
#define DLG_INSTANCE_MAX 127
#define DLG_HASH_SIZE_MAX (1U << DLG_INSTANCE_SHIFT)
#define dlg_h_idx(_h_entry) ((_h_entry) & (DLG_HASH_SIZE_MAX - 1))
#define dlg_h_inst(_h_entry) ((_h_entry) >> DLG_INSTANCE_SHIFT)
#define dlg_h_entry(_idx, _inst) \
((_idx) | ((unsigned int)(_inst) << DLG_INSTANCE_SHIFT))

extern int dlg_instance_id;

#define dlg_lock(_table, _entry) \
lock_set_get( (_table)->locks, (_entry)->lock_idx);
#define dlg_unlock(_table, _entry) \
Expand All @@ -247,10 +259,10 @@ struct dlg_cell *get_current_dialog();
((_dlg)->legs_no[DLG_LEGS_USED]>_leg)?(_dlg)->legs[_leg]._field.s:"NULL"

#define dlg_lock_dlg(_dlg) \
dlg_lock( d_table, &(d_table->entries[_dlg->h_entry]))
dlg_lock( d_table, &(d_table->entries[dlg_h_idx(_dlg->h_entry)]))

#define dlg_unlock_dlg(_dlg) \
dlg_unlock( d_table, &(d_table->entries[_dlg->h_entry]))
dlg_unlock( d_table, &(d_table->entries[dlg_h_idx(_dlg->h_entry)]))

static inline int ensure_leg_array(int needed_legs, struct dlg_cell *dlg)
{
Expand Down
Loading
Loading