Browse code

dispatcher: add an attribute to preset the

latency estimator

Julien Chavanton authored on 01/10/2020 15:21:37
Showing 3 changed files
... ...
@@ -319,6 +319,11 @@ int ds_set_attrs(ds_dest_t *dest, str *vattrs)
319 319
 		} else if(pit->name.len == 6
320 320
 				  && strncasecmp(pit->name.s, "weight", 6) == 0) {
321 321
 			str2sint(&pit->body, &dest->attrs.weight);
322
+		} else if(pit->name.len == 7
323
+				  && strncasecmp(pit->name.s, "latency", 7) == 0) {
324
+			int initial_latency = 0;
325
+			if (str2sint(&pit->body, &initial_latency) == 0)
326
+				latency_stats_init(&dest->latency_stats, initial_latency, 10000);
322 327
 		} else if(pit->name.len == 7
323 328
 				  && strncasecmp(pit->name.s, "maxload", 7) == 0) {
324 329
 			str2sint(&pit->body, &dest->attrs.maxload);
... ...
@@ -2582,6 +2587,16 @@ int ds_mark_dst(struct sip_msg *msg, int state)
2582 2587
 	return (ret == 0) ? 1 : -1;
2583 2588
 }
2584 2589
 
2590
+void latency_stats_init(ds_latency_stats_t *latency_stats, int latency, int count) {
2591
+	latency_stats->stdev = 0.0f;
2592
+	latency_stats->m2 = 0.0f;
2593
+	latency_stats->max = latency;
2594
+	latency_stats->min = latency;
2595
+	latency_stats->average = latency;
2596
+	latency_stats->estimate = latency;
2597
+	latency_stats->count = count;
2598
+}
2599
+
2585 2600
 static inline void latency_stats_update(ds_latency_stats_t *latency_stats, int latency) {
2586 2601
 	/* after 2^21 ~24 days at 1s interval, the average becomes a weighted average */
2587 2602
 	if (latency_stats->count < 2097152) {
... ...
@@ -2589,16 +2604,14 @@ static inline void latency_stats_update(ds_latency_stats_t *latency_stats, int l
2589 2604
 	} else { /* We adjust the sum of squares used by the oneline algorithm proportionally */
2590 2605
 		latency_stats->m2 -= latency_stats->m2/latency_stats->count;
2591 2606
 	}
2592
-	if (latency_stats->count == 1) {
2593
-		latency_stats->stdev = 0.0f;
2594
-		latency_stats->m2 = 0.0f;
2595
-		latency_stats->max = latency;
2596
-		latency_stats->min = latency;
2597
-		latency_stats->average = latency;
2598
-		latency_stats->estimate = latency;
2599
-	}
2600
-	/* train the average if stable after 10 samples */
2601
-	if (latency_stats->count > 10 && latency_stats->stdev < 0.5) latency_stats->count = 500000;
2607
+
2608
+	if (latency_stats->count == 1)
2609
+		latency_stats_init(latency_stats, latency, 1);
2610
+	/* stabilize-train the estimator if the average is stable after 10 samples */
2611
+	if (latency_stats->count > 10 && latency_stats->count < training_count
2612
+	        && latency_stats->stdev < 0.5)
2613
+		latency_stats->count = training_count;
2614
+
2602 2615
 	if (latency_stats->min > latency)
2603 2616
 		latency_stats->min = latency;
2604 2617
 	if (latency_stats->max < latency)
... ...
@@ -204,6 +204,8 @@ typedef struct _ds_latency_stats {
204 204
 	uint32_t timeout;
205 205
 } ds_latency_stats_t;
206 206
 
207
+void latency_stats_init(ds_latency_stats_t *latency_stats, int latency, int count);
208
+
207 209
 typedef struct _ds_dest {
208 210
 	str uri;          /*!< address/uri */
209 211
 	int flags;        /*!< flags */
... ...
@@ -709,6 +709,7 @@ modparam("dispatcher", "ds_probing_mode", 1)
709 709
 		<title><varname>ds_ping_latency_stats</varname> (int)</title>
710 710
 		<para>
711 711
 		Enable latency measurement when pinging nodes
712
+		The estimator can be initialized at startup and reload using the attribute latency.
712 713
 		</para>
713 714
 
714 715
 		<itemizedlist>
... ...
@@ -735,6 +736,9 @@ DEST: {
735 736
 	URI: sip:1.2.3.4
736 737
 	FLAGS: AX
737 738
 	PRIORITY: 9
739
+	ATTRS: {
740
+		BODY: latency=24
741
+	}
738 742
 	LATENCY: {
739 743
 		AVG: 24.250000 # weighted moving average for the last few weeks
740 744
 		STD: 1.035000  # standard deviation of AVG
... ...
@@ -2126,6 +2130,9 @@ kamctl rpc dispatcher.hash 4 bob server.com
2126 2130
 							<para>'obproxy' - SIP URI of outbound proxy to be used when sending
2127 2131
 								pings. It overwrites the general ds_outbound_proxy parameter.</para>
2128 2132
 						</listitem>
2133
+						<listitem>
2134
+							<para>'latency' - latency_stats initialization in ms.</para>
2135
+						</listitem>
2129 2136
 					</itemizedlist>
2130 2137
 		</para>
2131 2138
 		</section>