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);
... ...
@@ -2587,6 +2592,16 @@ int ds_mark_dst(struct sip_msg *msg, int state)
2587 2592
 	return (ret == 0) ? 1 : -1;
2588 2593
 }
2589 2594
 
2595
+void latency_stats_init(ds_latency_stats_t *latency_stats, int latency, int count) {
2596
+	latency_stats->stdev = 0.0f;
2597
+	latency_stats->m2 = 0.0f;
2598
+	latency_stats->max = latency;
2599
+	latency_stats->min = latency;
2600
+	latency_stats->average = latency;
2601
+	latency_stats->estimate = latency;
2602
+	latency_stats->count = count;
2603
+}
2604
+
2590 2605
 static inline void latency_stats_update(ds_latency_stats_t *latency_stats, int latency) {
2591 2606
 	int training_count = 10000;
2592 2607
 
... ...
@@ -2596,18 +2611,14 @@ static inline void latency_stats_update(ds_latency_stats_t *latency_stats, int l
2596 2611
 	} else { /* We adjust the sum of squares used by the oneline algorithm proportionally */
2597 2612
 		latency_stats->m2 -= latency_stats->m2/latency_stats->count;
2598 2613
 	}
2599
-	if (latency_stats->count == 1) {
2600
-		latency_stats->stdev = 0.0f;
2601
-		latency_stats->m2 = 0.0f;
2602
-		latency_stats->max = latency;
2603
-		latency_stats->min = latency;
2604
-		latency_stats->average = latency;
2605
-		latency_stats->estimate = latency;
2606
-	}
2614
+
2615
+	if (latency_stats->count == 1)
2616
+		latency_stats_init(latency_stats, latency, 1);
2607 2617
 	/* stabilize-train the estimator if the average is stable after 10 samples */
2608 2618
 	if (latency_stats->count > 10 && latency_stats->count < training_count
2609 2619
 	        && latency_stats->stdev < 0.5)
2610 2620
 		latency_stats->count = training_count;
2621
+
2611 2622
 	if (latency_stats->min > latency)
2612 2623
 		latency_stats->min = latency;
2613 2624
 	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>