From 5df7849daba28ac403d0dd19ae46181f8b0d4383 Mon Sep 17 00:00:00 2001 From: Cerghit Ionel Date: Wed, 28 Oct 2015 16:50:17 +0200 Subject: [PATCH 1/2] dispatcher: round robin uses weights to select destination --- modules/dispatcher/dispatch.c | 17 +++++++++++++++-- modules/dispatcher/dispatch.h | 1 + 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/modules/dispatcher/dispatch.c b/modules/dispatcher/dispatch.c index 98aa60b92ee..a0aa0e190e7 100644 --- a/modules/dispatcher/dispatch.c +++ b/modules/dispatcher/dispatch.c @@ -1486,8 +1486,17 @@ int ds_select_dst(struct sip_msg *msg, ds_select_ctl_p ds_select_ctl, } break; case 4: - /* round robin */ - ds_id = (idx->last+1) % set_size; + /* round robin + Each destination is selected a number of times equal to its weight before moving + to the next destination + the count is incremented after we verify that the destination is active + */ + if( idx->dlist[idx->last].rr_count < idx->dlist[idx->last].weight) + ds_id = idx->last; + else { + idx->dlist[idx->last].rr_count = 0; + ds_id = (idx->last+1) % set_size; + } break; case 5: i = ds_hash_authusername(msg, &ds_hash); @@ -1618,6 +1627,10 @@ int ds_select_dst(struct sip_msg *msg, ds_select_ctl_p ds_select_ctl, /* remember the last used destination */ idx->last = ds_id; + /* increase chosen count in round-robin algritm, now that we know the candidate is active*/ + if(ds_select_ctl->alg == 4) + idx->dlist[ds_id].rr_count++; + /* start pushing the destinations to SIP level */ cnt = 0; rc = 1; diff --git a/modules/dispatcher/dispatch.h b/modules/dispatcher/dispatch.h index 243b8db0c46..3bfb2dc4a82 100644 --- a/modules/dispatcher/dispatch.h +++ b/modules/dispatcher/dispatch.h @@ -82,6 +82,7 @@ typedef struct _ds_dest str description; int flags; unsigned short weight; + unsigned short rr_count; /* times it was chosen in a row for weighted round-robin */ unsigned short running_weight; unsigned short active_running_weight; unsigned short priority; From 4aa72c6063ff1c08353e136b15020c56b871e85c Mon Sep 17 00:00:00 2001 From: Cerghit Ionel Date: Thu, 16 Jun 2016 18:50:33 +0300 Subject: [PATCH 2/2] dispatcher: updated documentation for round-robin change --- modules/dispatcher/README | 27 +++++++++++++-------- modules/dispatcher/doc/dispatcher_admin.xml | 6 +++-- modules/dispatcher/doc/dispatcher_faq.xml | 6 +++-- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/modules/dispatcher/README b/modules/dispatcher/README index d62a17e8a94..773a683b98e 100644 --- a/modules/dispatcher/README +++ b/modules/dispatcher/README @@ -155,7 +155,7 @@ Chapter 1. Admin Guide definition of weights for the destination. This is useful in order to get a different ratio of traffic between destinations. - Since version 1.12 the dispatcher module keeps its destination + Since version 2.1 the dispatcher module keeps its destination sets into different partitions. Each partition is described by its own db_url, table_name, dst_avp, grp_avp, cnt_avp, sock_avp, attr_avp and blacklists. Setting any of this @@ -176,9 +176,9 @@ Chapter 1. Admin Guide Remember that in order to be able to use a table from a partition, its name must be found in the "version" table belonging to the database defined in the partition's db_url. - Also, in version 1.12 the "flags" parameter has been moved to - ds_select_dst and ds_select_domain along with "force_dst" and - "use_default" flags. + Also, in version 2.1 the "flags" parameter has been moved to + ds_select_dst() and ds_select_domain() along with "force_dst" + and "use_default" flags. 1.2. Dependencies @@ -201,6 +201,11 @@ Chapter 1. Admin Guide parameter will only change the default partition's db_url. Use the partition parameter to create and alter other partitions. + NOTE: if you intend to use the default partition you have to + explicity set this default db_url, otherwise OpenSIPS will not + start (he value of global default db_url is not inherited here! + ). + Default value is “NULL”. At least one db_url should be defined for the dispatcher module to work. @@ -641,10 +646,12 @@ modparam("dispatcher", "socket_col", "my_sock") + “1” - hash over from uri. + “2” - hash over to uri. + “3” - hash over request-uri. - + “4” - round-robin (next destination). + + “4” - weighted round-robin (next destination). the + destination's weight determines how many times it is + chosen before going to the next one + “5” - hash over authorization-username (Proxy-Authorization or "normal" authorization). If no - username is found, round robin is used. + username is found, weighted round-robin is used. + “6” - random (using rand()). + “7” - hash over the content of PVs string. Note: This works only when the parameter hash_pvar is set. @@ -673,7 +680,7 @@ modparam("dispatcher", "socket_col", "my_sock") maximum of max_results will be put into the specified avp for failover. This allows having many destinations but limit the useless traffic in case of a number that is bound - to fail everywhere. Since version 1.12, the last paramater + to fail everywhere. Since version 2.1, the last paramater cand be represented by a list of flags and max_results, separated by comma. You can specify either only the flags, either only the max_results paramater, but if you want to @@ -1016,9 +1023,9 @@ Chapter 2. Frequently Asked Questions chosen(if we have two destinations with weights 1 respectively 4 than the second one is 4 times more likely to be selected than the other). The sum of all weights does not need to add up - to a specific number. It is important to understand that the - weights are not used in the round-robin algorithm at the - moment. + to a specific number. Weights are now used in the round-robin + algorithm, a destination is chosen a number of times equal to + its weight consecutively before going to the next destination. The priority field is used at ordering the destinations from a set. It does not affect the overall probability of a diff --git a/modules/dispatcher/doc/dispatcher_admin.xml b/modules/dispatcher/doc/dispatcher_admin.xml index f67d7ae6fdb..dcec2647234 100644 --- a/modules/dispatcher/doc/dispatcher_admin.xml +++ b/modules/dispatcher/doc/dispatcher_admin.xml @@ -815,14 +815,16 @@ modparam("dispatcher", "socket_col", "my_sock") - 4 - round-robin (next destination). + 4 - weighted round-robin (next destination). + the destination's weight determines how many times it is chosen + before going to the next one 5 - hash over authorization-username (Proxy-Authorization or "normal" authorization). - If no username is found, round robin is used. + If no username is found, weighted round-robin is used. diff --git a/modules/dispatcher/doc/dispatcher_faq.xml b/modules/dispatcher/doc/dispatcher_faq.xml index 33ccb9342fd..4f15c516901 100644 --- a/modules/dispatcher/doc/dispatcher_faq.xml +++ b/modules/dispatcher/doc/dispatcher_faq.xml @@ -62,8 +62,10 @@ chosen(if we have two destinations with weights 1 respectively 4 than the second one is 4 times more likely to be selected than the other). The sum of all weights does not need to add up to a specific - number. It is important to understand that the weights are not used - in the round-robin algorithm at the moment. + number. + Weights are now used in the round-robin algorithm, a destination is + chosen a number of times equal to its weight consecutively before going + to the next destination. The priority field is used at ordering the