Skip to content

Commit

Permalink
qrouting/drouting: Improve sorting algorithm code/schema
Browse files Browse the repository at this point in the history
    * the algorithms are now 'N', 'W' and 'Q' (default: 'N')
    * simplify algorithm parsing code
    * simplify sorting callback registration code
    * update DB schema docs
  • Loading branch information
liviuchircu committed Feb 14, 2020
1 parent 3e969a5 commit bbc2681
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 90 deletions.
11 changes: 6 additions & 5 deletions db/schema/dr_carriers.xml
Expand Up @@ -59,12 +59,13 @@
<column id="sorting_alg">
<name>sort_alg</name>
<type>string</type>
<default>0</default>
<default>N</default>
<size>1</size>
<description>The sorting algorithm to be employed inside the carrier
(if set) when do_routing is called. Possible values: 'O'/'o' - no sorting should be done,
'W'/'w' - weight based sorting should be done and 'Q'/'q' quality-based sorting should be done (quality
based sorting is provided by the qrouting module)
<description>The sorting algorithm to be employed for the carriers's
destinations when do_routing() is called. Possible values:
'N' (default; no sorting, preserve given order),
'W' (weight based sorting),
'Q' (quality-based sorting, provided by the qrouting module)
</description>
</column>

Expand Down
11 changes: 6 additions & 5 deletions db/schema/dr_rules.xml
Expand Up @@ -85,12 +85,13 @@
<column id="sorting_alg">
<name>sort_alg</name>
<type>string</type>
<default>0</default>
<default>N</default>
<size>1</size>
<description>The sorting algorithm to be employed inside the rule (if set)
when do_routing is called. Possible values: 'O'/'o' - no sorting should be done,
'W'/'w' - weight based sorting should be done and 'Q'/'q' quality-based sorting should be done (quality
based sorting is provided by the qrouting module). More options may be available in the future.
<description>The sorting algorithm to be employed for the rule's
destinations when do_routing() is called. Possible values:
'N' (default; no sorting, preserve given order),
'W' (weight based sorting),
'Q' (quality-based sorting, provided by the qrouting module)
</description>
</column>
<column id="sorting_profile">
Expand Down
21 changes: 7 additions & 14 deletions modules/drouting/dr_cb.c
Expand Up @@ -34,7 +34,7 @@ struct dr_head_cbl {

#define POINTER_CLOSED_MARKER ((void *)(-1))

unsigned char sort_algs[N_MAX_SORT_CBS] = {0,'O','W','Q'};
unsigned char sort_algs[N_MAX_SORT_CBS] = {'N', 'W', 'Q'};

/* the array with all the cb lists (per type) */
static struct dr_callback *dr_cbs[DRCB_MAX];
Expand Down Expand Up @@ -111,7 +111,7 @@ int insert_drcb(struct dr_head_cbl **dr_cb_list, struct dr_callback *cb,
int register_dr_cb(enum drcb_types type, dr_cb f, void *param,
dr_param_free_cb ff)
{
long int cb_sort_index = 0;
sort_cb_type alg = (sort_cb_type)param;
struct dr_callback *cb;

cb = pkg_malloc(sizeof *cb);
Expand All @@ -135,22 +135,15 @@ int register_dr_cb(enum drcb_types type, dr_cb f, void *param,
cb->next = dr_cbs[type];
dr_cbs[type] = cb;
} else {
if (!param) {
LM_ERR("no index supplied for sort callback registered at dr\n");
if (alg >= N_MAX_SORT_CBS) {
LM_ERR("invalid sorting algorithm: %u\n", alg);
goto error;
}

cb_sort_index = (long int)param;
if (cb_sort_index >= N_MAX_SORT_CBS) {
LM_ERR("Sort cbs array not large enough to accommodate cb at dr\n");
goto error;
}

if (dr_sort_cbs[cb_sort_index])
LM_WARN("sort callback at index '%ld' will be overwritten\n",
cb_sort_index);
if (dr_sort_cbs[alg])
LM_WARN("sort callback for alg %u will be overwritten\n", alg);

dr_sort_cbs[cb_sort_index] = cb;
dr_sort_cbs[alg] = cb;
}

return 0;
Expand Down
20 changes: 14 additions & 6 deletions modules/drouting/dr_cb_sorting.h
Expand Up @@ -26,22 +26,30 @@

#include "prefix_tree.h"

struct rt_info_;

/* if new callbacks are added you must increase the N_MAX_SORT_CBS
* constant accordingly, add the letter which will be provided in the db
* to the sort_algs array, add the corresponding sorting algorithm id to the
* enum an register the callback to the dr_sort_cbs in the appropriate position*/

/* The maximum number of sorting functions provided by dr */
#define N_MAX_SORT_CBS 4

typedef enum { NO_SORT = 1, WEIGHT_BASED_SORT = 2, QR_BASED_SORT = 3}
sort_cb_type;
typedef enum {
NO_SORT,
WEIGHT_BASED_SORT,
QR_BASED_SORT,

N_MAX_SORT_CBS = 3
} sort_cb_type;

/* used for mapping the db information (sort_alg = a letter) to an index
* in the sort_cb_type enum */
extern unsigned char sort_algs[N_MAX_SORT_CBS];

static inline sort_cb_type dr_get_sort_alg(char alg) {
unsigned char *p = memchr(sort_algs, alg, N_MAX_SORT_CBS);
return !p ? NO_SORT : (sort_cb_type)(p - sort_algs);
}

/* parameters needed for the registration of a gw */
struct dr_reg_param {
void *rule;
Expand All @@ -66,7 +74,7 @@ struct dr_acc_call_params {
};

struct dr_sort_params {
rt_info_t *dr_rule; /* dr_rule which contains the dst to be sorted */
struct rt_info_ *dr_rule; /* dr_rule which contains the dst to be sorted */

/* -1 for rule gwlist sort, carrier dst index for carrier gwlist sort */
unsigned short dst_idx;
Expand Down
10 changes: 6 additions & 4 deletions modules/drouting/prefix_tree.h
Expand Up @@ -30,6 +30,8 @@
#include "../../map.h"
#include "../../mem/mem_funcs.h"

#include "dr_cb_sorting.h"

#define IS_DECIMAL_DIGIT(d) \
(((d)>='0') && ((d)<= '9'))

Expand Down Expand Up @@ -105,8 +107,8 @@ struct pcr_ {
str id;
/* flags */
unsigned int flags;
/* the id of the sorting algorithm from the sort_cb_type enum */
unsigned char sort_alg;
/* gateway sorting algorithm */
sort_cb_type sort_alg;
/* array of pointers into the PSTN gw list */
pgw_list_t *pgwl;
/* length of the PSTN gw array */
Expand Down Expand Up @@ -138,8 +140,8 @@ typedef struct rt_info_ {
unsigned short ref_cnt;
/* handler used by qr for accounting (actually qr_rule_t *) */
void *qr_handler;
/* sorting algorithm for the destinations inside rule */
unsigned char sort_alg;
/* sorting algorithm for the destinations */
sort_cb_type sort_alg;
} rt_info_t;

typedef struct rt_info_wrp_ {
Expand Down
37 changes: 6 additions & 31 deletions modules/drouting/routing.c
Expand Up @@ -234,10 +234,8 @@ int add_carrier(char *id, int flags, char *sort_alg, char *gwlist, char *attrs,
int state, rt_data_t *rd,
osips_malloc_f mf, osips_free_f ff)
{
pcr_t *cr = NULL;
pcr_t *cr;
unsigned int i;
unsigned char * sort_p, n_alg;

str key;

/* allocate a new carrier structure */
Expand Down Expand Up @@ -268,20 +266,7 @@ int add_carrier(char *id, int flags, char *sort_alg, char *gwlist, char *attrs,

/* copy integer fields */
cr->flags = flags;

sort_p = memchr(sort_algs, sort_alg[0], N_MAX_SORT_CBS);
if(sort_p == NULL) {
n_alg = 1;
} else {
n_alg = (unsigned char)(sort_p - sort_algs);

if(n_alg == 0) {
n_alg = 1;
}
}

cr->sort_alg = n_alg;

cr->sort_alg = dr_get_sort_alg(sort_alg[0]);

/* set state */
if (state!=0)
Expand Down Expand Up @@ -345,8 +330,7 @@ build_rt_info(
struct dr_add_rule_params arp;
struct dr_reg_init_rule_params irp;
pgw_list_t *p = NULL;

unsigned char * sort_p, n_alg;
sort_cb_type alg;

rt = (rt_info_t*)func_malloc(mf, sizeof(rt_info_t) +
(attrs?strlen(attrs):0) + (route_idx?strlen(route_idx)+1:0) );
Expand All @@ -361,17 +345,8 @@ build_rt_info(
rt->time_rec = trec;

rt->route_idx = route_idx;
sort_p = memchr(sort_algs, sort_alg[0], N_MAX_SORT_CBS);
if(sort_p == NULL) {
n_alg = 1;
} else {
n_alg = (unsigned char)(sort_p - sort_algs);
if(n_alg == 0) {
n_alg = 1;
}
}

rt->sort_alg = n_alg;
alg = dr_get_sort_alg(sort_alg[0]);
rt->sort_alg = alg;

if (attrs && strlen(attrs)) {
rt->attrs.s = (char*)(rt+1);
Expand All @@ -390,7 +365,7 @@ build_rt_info(
}
}

if (n_alg == 3) { /* qr sorting */
if (alg == QR_BASED_SORT) {
irp.n_dst = rt->pgwa_len;
irp.r_id = id;
irp.qr_profile = qr_profile;
Expand Down
2 changes: 1 addition & 1 deletion scripts/db_berkeley/opensips/dr_carriers
Expand Up @@ -7,4 +7,4 @@ METADATA_READONLY
METADATA_LOGFLAGS
0
METADATA_DEFAULTS
NIL|NIL|NIL|0|0|0|NULL|NULL
NIL|NIL|NIL|0|'N'|0|NULL|NULL
2 changes: 1 addition & 1 deletion scripts/db_berkeley/opensips/dr_rules
Expand Up @@ -7,4 +7,4 @@ METADATA_READONLY
METADATA_LOGFLAGS
0
METADATA_DEFAULTS
NIL|NIL|NIL|NULL|0|NULL|NIL|0|0|NULL|NULL
NIL|NIL|NIL|NULL|0|NULL|NIL|'N'|0|NULL|NULL
4 changes: 2 additions & 2 deletions scripts/mysql/drouting-create.sql
Expand Up @@ -23,7 +23,7 @@ CREATE TABLE dr_rules (
priority INT(11) DEFAULT 0 NOT NULL,
routeid CHAR(255) DEFAULT NULL,
gwlist CHAR(255) NOT NULL,
sort_alg CHAR(1) DEFAULT 0 NOT NULL,
sort_alg CHAR(1) DEFAULT 'N' NOT NULL,
sort_profile INT(10) UNSIGNED DEFAULT 0 NOT NULL,
attrs CHAR(255) DEFAULT NULL,
description CHAR(128) DEFAULT NULL
Expand All @@ -35,7 +35,7 @@ CREATE TABLE dr_carriers (
carrierid CHAR(64) NOT NULL,
gwlist CHAR(255) NOT NULL,
flags INT(11) UNSIGNED DEFAULT 0 NOT NULL,
sort_alg CHAR(1) DEFAULT 0 NOT NULL,
sort_alg CHAR(1) DEFAULT 'N' NOT NULL,
state INT(11) UNSIGNED DEFAULT 0 NOT NULL,
attrs CHAR(255) DEFAULT NULL,
description CHAR(128) DEFAULT NULL,
Expand Down
10 changes: 5 additions & 5 deletions scripts/mysql/qrouting-create.sql
Expand Up @@ -2,11 +2,11 @@ INSERT INTO version (table_name, table_version) values ('qr_profiles','1');
CREATE TABLE qr_profiles (
id INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
profile_name CHAR(64) NOT NULL,
weight_asr FLOAT DEFAULT 1 NOT NULL,
weight_ccr FLOAT DEFAULT 1 NOT NULL,
weight_pdd FLOAT DEFAULT 1 NOT NULL,
weight_ast FLOAT DEFAULT 1 NOT NULL,
weight_acd FLOAT DEFAULT 1 NOT NULL,
weight_asr DOUBLE DEFAULT 1 NOT NULL,
weight_ccr DOUBLE DEFAULT 1 NOT NULL,
weight_pdd DOUBLE DEFAULT 1 NOT NULL,
weight_ast DOUBLE DEFAULT 1 NOT NULL,
weight_acd DOUBLE DEFAULT 1 NOT NULL,
warn_threshold_asr DOUBLE DEFAULT -1 NOT NULL,
warn_threshold_ccr DOUBLE DEFAULT -1 NOT NULL,
warn_threshold_pdd DOUBLE DEFAULT -1 NOT NULL,
Expand Down
4 changes: 2 additions & 2 deletions scripts/oracle/drouting-create.sql
Expand Up @@ -31,7 +31,7 @@ CREATE TABLE dr_rules (
priority NUMBER(10) DEFAULT 0 NOT NULL,
routeid VARCHAR2(255) DEFAULT NULL,
gwlist VARCHAR2(255),
sort_alg VARCHAR2(1) DEFAULT 0 NOT NULL,
sort_alg VARCHAR2(1) DEFAULT 'N',
sort_profile NUMBER(10) DEFAULT 0 NOT NULL,
attrs VARCHAR2(255) DEFAULT NULL,
description VARCHAR2(128) DEFAULT NULL
Expand All @@ -51,7 +51,7 @@ CREATE TABLE dr_carriers (
carrierid VARCHAR2(64),
gwlist VARCHAR2(255),
flags NUMBER(10) DEFAULT 0 NOT NULL,
sort_alg VARCHAR2(1) DEFAULT 0 NOT NULL,
sort_alg VARCHAR2(1) DEFAULT 'N',
state NUMBER(10) DEFAULT 0 NOT NULL,
attrs VARCHAR2(255) DEFAULT NULL,
description VARCHAR2(128) DEFAULT NULL,
Expand Down
4 changes: 2 additions & 2 deletions scripts/postgres/drouting-create.sql
Expand Up @@ -24,7 +24,7 @@ CREATE TABLE dr_rules (
priority INTEGER DEFAULT 0 NOT NULL,
routeid VARCHAR(255) DEFAULT NULL,
gwlist VARCHAR(255) NOT NULL,
sort_alg VARCHAR(1) DEFAULT 0 NOT NULL,
sort_alg VARCHAR(1) DEFAULT 'N' NOT NULL,
sort_profile INTEGER DEFAULT 0 NOT NULL,
attrs VARCHAR(255) DEFAULT NULL,
description VARCHAR(128) DEFAULT NULL
Expand All @@ -37,7 +37,7 @@ CREATE TABLE dr_carriers (
carrierid VARCHAR(64) NOT NULL,
gwlist VARCHAR(255) NOT NULL,
flags INTEGER DEFAULT 0 NOT NULL,
sort_alg VARCHAR(1) DEFAULT 0 NOT NULL,
sort_alg VARCHAR(1) DEFAULT 'N' NOT NULL,
state INTEGER DEFAULT 0 NOT NULL,
attrs VARCHAR(255) DEFAULT NULL,
description VARCHAR(128) DEFAULT NULL,
Expand Down
10 changes: 5 additions & 5 deletions scripts/postgres/qrouting-create.sql
Expand Up @@ -2,11 +2,11 @@ INSERT INTO version (table_name, table_version) values ('qr_profiles','1');
CREATE TABLE qr_profiles (
id SERIAL PRIMARY KEY NOT NULL,
profile_name VARCHAR(64) NOT NULL,
weight_asr REAL DEFAULT 1 NOT NULL,
weight_ccr REAL DEFAULT 1 NOT NULL,
weight_pdd REAL DEFAULT 1 NOT NULL,
weight_ast REAL DEFAULT 1 NOT NULL,
weight_acd REAL DEFAULT 1 NOT NULL,
weight_asr DOUBLE PRECISION DEFAULT 1 NOT NULL,
weight_ccr DOUBLE PRECISION DEFAULT 1 NOT NULL,
weight_pdd DOUBLE PRECISION DEFAULT 1 NOT NULL,
weight_ast DOUBLE PRECISION DEFAULT 1 NOT NULL,
weight_acd DOUBLE PRECISION DEFAULT 1 NOT NULL,
warn_threshold_asr DOUBLE PRECISION DEFAULT -1 NOT NULL,
warn_threshold_ccr DOUBLE PRECISION DEFAULT -1 NOT NULL,
warn_threshold_pdd DOUBLE PRECISION DEFAULT -1 NOT NULL,
Expand Down
4 changes: 2 additions & 2 deletions scripts/sqlite/drouting-create.sql
Expand Up @@ -23,7 +23,7 @@ CREATE TABLE dr_rules (
priority INTEGER DEFAULT 0 NOT NULL,
routeid CHAR(255) DEFAULT NULL,
gwlist CHAR(255) NOT NULL,
sort_alg CHAR(1) DEFAULT 0 NOT NULL,
sort_alg CHAR(1) DEFAULT 'N' NOT NULL,
sort_profile INTEGER DEFAULT 0 NOT NULL,
attrs CHAR(255) DEFAULT NULL,
description CHAR(128) DEFAULT NULL
Expand All @@ -35,7 +35,7 @@ CREATE TABLE dr_carriers (
carrierid CHAR(64) NOT NULL,
gwlist CHAR(255) NOT NULL,
flags INTEGER DEFAULT 0 NOT NULL,
sort_alg CHAR(1) DEFAULT 0 NOT NULL,
sort_alg CHAR(1) DEFAULT 'N' NOT NULL,
state INTEGER DEFAULT 0 NOT NULL,
attrs CHAR(255) DEFAULT NULL,
description CHAR(128) DEFAULT NULL,
Expand Down
10 changes: 5 additions & 5 deletions scripts/sqlite/qrouting-create.sql
Expand Up @@ -2,11 +2,11 @@ INSERT INTO version (table_name, table_version) values ('qr_profiles','1');
CREATE TABLE qr_profiles (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
profile_name CHAR(64) NOT NULL,
weight_asr FLOAT DEFAULT 1 NOT NULL,
weight_ccr FLOAT DEFAULT 1 NOT NULL,
weight_pdd FLOAT DEFAULT 1 NOT NULL,
weight_ast FLOAT DEFAULT 1 NOT NULL,
weight_acd FLOAT DEFAULT 1 NOT NULL,
weight_asr DOUBLE DEFAULT 1 NOT NULL,
weight_ccr DOUBLE DEFAULT 1 NOT NULL,
weight_pdd DOUBLE DEFAULT 1 NOT NULL,
weight_ast DOUBLE DEFAULT 1 NOT NULL,
weight_acd DOUBLE DEFAULT 1 NOT NULL,
warn_threshold_asr DOUBLE DEFAULT -1 NOT NULL,
warn_threshold_ccr DOUBLE DEFAULT -1 NOT NULL,
warn_threshold_pdd DOUBLE DEFAULT -1 NOT NULL,
Expand Down

0 comments on commit bbc2681

Please sign in to comment.