Skip to content

Commit

Permalink
Merge pull request #3076 from wangduanduan/ds_reload_1
Browse files Browse the repository at this point in the history
feat: add inherit_state to ds_reload
  • Loading branch information
bogdan-iancu committed May 4, 2023
2 parents 6617ecc + 4c61984 commit c465148
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ core.*
out
log
*.log
*.html
*.html
7 changes: 5 additions & 2 deletions modules/dispatcher/dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,7 @@ static ds_data_t* ds_load_data(ds_partition_t *partition, int use_state_col)
}


int ds_reload_db(ds_partition_t *partition, int initial)
int ds_reload_db(ds_partition_t *partition, int initial, int is_inherit_state)
{
ds_data_t *old_data;
ds_data_t *new_data;
Expand Down Expand Up @@ -1214,7 +1214,10 @@ int ds_reload_db(ds_partition_t *partition, int initial)
if (old_data) {
/* copy the state of the destinations from the old set
* (for the matching ids) */
ds_inherit_state( old_data, new_data);
if (is_inherit_state == INHERIT_STATE_YES) {
ds_inherit_state( old_data, new_data);
}

ds_destroy_data_set( old_data );
}

Expand Down
6 changes: 5 additions & 1 deletion modules/dispatcher/dispatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
#define MI_FULL_LISTING (1<<0)


#define INHERIT_STATE_YES 1
#define INHERIT_STATE_NO 0


extern int ds_persistent_state;

typedef struct _ds_dest
Expand Down Expand Up @@ -211,7 +215,7 @@ extern void *ds_srg;
int init_ds_db(ds_partition_t *partition);
int ds_connect_db(ds_partition_t *partition);
void ds_disconnect_db(ds_partition_t *partition);
int ds_reload_db(ds_partition_t *partition, int initial);
int ds_reload_db(ds_partition_t *partition, int initial, int is_inherit_state);

int init_ds_data(ds_partition_t *partition);
void ds_destroy_data(ds_partition_t *partition);
Expand Down
44 changes: 40 additions & 4 deletions modules/dispatcher/dispatcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,9 @@ static const mi_export_t mi_cmds[] = {
},
{ "ds_reload", 0, 0, mi_child_init, {
{ds_mi_reload, {0}},
{ds_mi_reload, {"inherit_state", 0}},
{ds_mi_reload_1, {"partition", 0}},
{ds_mi_reload_1, {"partition", "inherit_state", 0}},
{EMPTY_MI_RECIPE}}
},
{ "ds_push_script_attrs", 0, 0, 0, {
Expand Down Expand Up @@ -908,7 +910,7 @@ static int mod_init(void)
}

/* do the actual data load */
if (ds_reload_db(partition, 1)!=0) {
if (ds_reload_db(partition, 1, INHERIT_STATE_YES)!=0) {
LM_ERR("failed to load data from DB\n");
return -1;
}
Expand Down Expand Up @@ -1371,9 +1373,26 @@ mi_response_t *ds_mi_reload(const mi_params_t *params,
struct mi_handler *async_hdl)
{
ds_partition_t *part_it;
str inherit_state;
int is_inherit_state = INHERIT_STATE_YES;

if (get_mi_string_param(params, "inherit_state", &inherit_state.s, &inherit_state.len) >= 0) {
LM_DBG("inherit_state is: %s \n", inherit_state.s);

if (inherit_state.s[0] == '0' || inherit_state.s[0] == 'n' || inherit_state.s[0] == 'N') {
is_inherit_state = INHERIT_STATE_NO;
}
else if (inherit_state.s[0] == '1' || inherit_state.s[0] == 'y' || inherit_state.s[0] == 'Y') {
is_inherit_state = INHERIT_STATE_YES;
} else {
LM_WARN("inherit_state values was not recognized, ignored \n");
}
}

LM_DBG("is_inherit_state is: %d \n", is_inherit_state);

for (part_it = partitions; part_it; part_it = part_it->next)
if (ds_reload_db(part_it, 0)<0)
if (ds_reload_db(part_it, 0, is_inherit_state)<0)
return init_mi_error(500, MI_SSTR(MI_ERR_RELOAD));

if (ds_cluster_id && ds_cluster_sync() < 0)
Expand All @@ -1387,15 +1406,32 @@ mi_response_t *ds_mi_reload_1(const mi_params_t *params,
{
ds_partition_t *partition;
str partname;
str inherit_state;
int is_inherit_state = INHERIT_STATE_YES;

if (get_mi_string_param(params, "partition", &partname.s, &partname.len) < 0)
return init_mi_param_error();
return init_mi_param_error();

if (get_mi_string_param(params, "inherit_state", &inherit_state.s, &inherit_state.len) >= 0) {
LM_DBG("inherit_state is: %s \n", inherit_state.s);

if (inherit_state.s[0] == '0' || inherit_state.s[0] == 'n' || inherit_state.s[0] == 'N') {
is_inherit_state = INHERIT_STATE_NO;
}
else if (inherit_state.s[0] == '1' || inherit_state.s[0] == 'y' || inherit_state.s[0] == 'Y') {
is_inherit_state = INHERIT_STATE_YES;
} else {
LM_WARN("inherit_state values was not recognized, ignored \n");
}
}

LM_DBG("is_inherit_state is: %d \n", is_inherit_state);

partition = find_partition_by_name(&partname);

if (partition == NULL)
return init_mi_error(500, MI_SSTR(MI_UNK_PARTITION));
if (ds_reload_db(partition, 0) < 0)
if (ds_reload_db(partition, 0, is_inherit_state) < 0)
return init_mi_error(500, MI_SSTR(MI_ERR_RELOAD));

if (ds_cluster_id && ds_cluster_sync() < 0)
Expand Down
12 changes: 10 additions & 2 deletions modules/dispatcher/doc/dispatcher_admin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1700,14 +1700,23 @@ opensips-cli -x mi ds_list
<itemizedlist>
<listitem><para>
<emphasis>partition</emphasis> (optional) - name of
the partition to be reloaded.
the partition to be reloaded. default partition is "default".
</para></listitem>

<listitem><para><emphasis>inherit_state</emphasis> (optional) : whether inherit old state of the destination , default is y. </para>
<itemizedlist>
<listitem><para> <quote>n</quote>: no inherit state </para></listitem>
<listitem><para> <quote>y</quote>: inherit state </para></listitem>
</itemizedlist>
</listitem>

</itemizedlist>
<para>
MI FIFO Command Format:
</para>
<programlisting format="linespecific">
opensips-cli -x mi ds_reload
opensips-cli -x mi ds_reload inherit_state=n
</programlisting>
</section>

Expand Down Expand Up @@ -1913,4 +1922,3 @@ opensips-cli -x mi ds_reload
</section>
</section>
</chapter>

0 comments on commit c465148

Please sign in to comment.