Browse Source

dispatcher: add an attribute to preset the

latency estimator
Julien Chavanton 5 years ago
parent
commit
8429ef3805

+ 19 - 8
src/modules/dispatcher/dispatch.c

@@ -319,6 +319,11 @@ int ds_set_attrs(ds_dest_t *dest, str *vattrs)
 		} else if(pit->name.len == 6
 				  && strncasecmp(pit->name.s, "weight", 6) == 0) {
 			str2sint(&pit->body, &dest->attrs.weight);
+		} else if(pit->name.len == 7
+				  && strncasecmp(pit->name.s, "latency", 7) == 0) {
+			int initial_latency = 0;
+			if (str2sint(&pit->body, &initial_latency) == 0)
+				latency_stats_init(&dest->latency_stats, initial_latency, 10000);
 		} else if(pit->name.len == 7
 				  && strncasecmp(pit->name.s, "maxload", 7) == 0) {
 			str2sint(&pit->body, &dest->attrs.maxload);
@@ -2587,6 +2592,16 @@ int ds_mark_dst(struct sip_msg *msg, int state)
 	return (ret == 0) ? 1 : -1;
 }
 
+void latency_stats_init(ds_latency_stats_t *latency_stats, int latency, int count) {
+	latency_stats->stdev = 0.0f;
+	latency_stats->m2 = 0.0f;
+	latency_stats->max = latency;
+	latency_stats->min = latency;
+	latency_stats->average = latency;
+	latency_stats->estimate = latency;
+	latency_stats->count = count;
+}
+
 static inline void latency_stats_update(ds_latency_stats_t *latency_stats, int latency) {
 	int training_count = 10000;
 
@@ -2596,18 +2611,14 @@ static inline void latency_stats_update(ds_latency_stats_t *latency_stats, int l
 	} else { /* We adjust the sum of squares used by the oneline algorithm proportionally */
 		latency_stats->m2 -= latency_stats->m2/latency_stats->count;
 	}
-	if (latency_stats->count == 1) {
-		latency_stats->stdev = 0.0f;
-		latency_stats->m2 = 0.0f;
-		latency_stats->max = latency;
-		latency_stats->min = latency;
-		latency_stats->average = latency;
-		latency_stats->estimate = latency;
-	}
+
+	if (latency_stats->count == 1)
+		latency_stats_init(latency_stats, latency, 1);
 	/* stabilize-train the estimator if the average is stable after 10 samples */
 	if (latency_stats->count > 10 && latency_stats->count < training_count
 	        && latency_stats->stdev < 0.5)
 		latency_stats->count = training_count;
+
 	if (latency_stats->min > latency)
 		latency_stats->min = latency;
 	if (latency_stats->max < latency)

+ 2 - 0
src/modules/dispatcher/dispatch.h

@@ -204,6 +204,8 @@ typedef struct _ds_latency_stats {
 	uint32_t timeout;
 } ds_latency_stats_t;
 
+void latency_stats_init(ds_latency_stats_t *latency_stats, int latency, int count);
+
 typedef struct _ds_dest {
 	str uri;          /*!< address/uri */
 	int flags;        /*!< flags */

+ 7 - 0
src/modules/dispatcher/doc/dispatcher_admin.xml

@@ -709,6 +709,7 @@ modparam("dispatcher", "ds_probing_mode", 1)
 		<title><varname>ds_ping_latency_stats</varname> (int)</title>
 		<para>
 		Enable latency measurement when pinging nodes
+		The estimator can be initialized at startup and reload using the attribute latency.
 		</para>
 
 		<itemizedlist>
@@ -735,6 +736,9 @@ DEST: {
 	URI: sip:1.2.3.4
 	FLAGS: AX
 	PRIORITY: 9
+	ATTRS: {
+		BODY: latency=24
+	}
 	LATENCY: {
 		AVG: 24.250000 # weighted moving average for the last few weeks
 		STD: 1.035000  # standard deviation of AVG
@@ -2126,6 +2130,9 @@ kamctl rpc dispatcher.hash 4 bob server.com
 							<para>'obproxy' - SIP URI of outbound proxy to be used when sending
 								pings. It overwrites the general ds_outbound_proxy parameter.</para>
 						</listitem>
+						<listitem>
+							<para>'latency' - latency_stats initialization in ms.</para>
+						</listitem>
 					</itemizedlist>
 		</para>
 		</section>