Skip to content

Commit c2f2870

Browse files
committed
ofproto-dpif: Improve load balancing in dp_hash select groups.
ovn-kubernetes users observe uneven distribution of traffic among service backends. It gets noticeably worse when 9+ backends are configured. For example: Server connections server-6nbg7 634 server-72br8 326 <----- server-7b8rm 605 server-9tnz7 655 server-c84hw 596 server-mjskl 622 server-ptkcc 336 <----- server-rl7pk 592 server-xd2cb 634 Here we can see that out of 5000 connections total, servers '72br8' and 'ptkcc' received about 2 times less connections than any other server. With 10 backends, 4 servers will receive 2x less, etc. The situation is common, because kubernetes users frequently scale their applications with something like kubectl scale --replicas=10. Services are implemented as OVN load-balancers, which are implemented as OpenFlow select groups with a dp_hash selection method. Balancing is implemented by allocating a hash space (number of hashes is a power of 2) and distributing hashes between the group buckets using Webster's method taking into account bucket weights. This works well enough when buckets have different weights, because OVS needs to allocate larger hash space in order to accommodate the weight difference. However, in case of OVN load-balancers, all the buckets (backends) have the same weight. This leads to allocation of a smaller hash space just enough to fit all the backends (closest power of 2). For example, if we have 10 backends (buckets), then 16 hashes will be allocated. After hashes are distributed, we end up with 6 buckets having 2 hashes each and 4 buckets having 1 hash. So, each of the 6 buckets will receive on average 2x more traffic than each of the remaining 4. The problem can be mitigated by expanding the hash space and this is already done for cases with small number of buckets by limiting the hash space to have at least 16 hashes. However, it is just not large enough for 9 or 10 buckets to provide a good distribution. At the same time, blindly increasing the limit is also not a good solution as we do not want too large of a hash space for small number of buckets, simply because each hash is a separate datapath flow and having too many of them may cause performance issues. Approach taken in this change is to ensure that the hash space is at least 4 times larger than the number of buckets, but not larger than the maximum allowed (256). This provides a better distribution while not unnecessarily exploding number of datapath flows for services with not that many backends. Here is some data to demonstrate why the 4 was chosen as a coefficient: coeff. : 1 2 3 4 5 100 ------------------------------------------------------------------- AvgDiff : 43.1 % 27.1 % 18.3 % 15.1 % 13.6 % 10.9 % MaxDiff : 50.0 % 33.3 % 25.0 % 20.0 % 20.0 % 20.0 % AvgDev : 24.9 % 13.4 % 8.5 % 6.9 % 6.1 % 4.8 % MaxDev : 35.4 % 20.4 % 14.4 % 11.2 % 11.2 % 11.2 % --------+---------------------------------------------------------- 16 : 1 1 1 1 1 - 32 : 17 9 6 5 4 - 64 : 33 17 11 9 7 - 128 : 65 33 22 17 13 1 256 : 129 65 43 33 26 2 --------+---------------------------------------------------------- current proposed Table shows average and maximum load difference (Diff) between backends across groups with 1 to 64 equally weighted backends. And it shows average and maximum standard deviation (Dev) of load distribution for the same. For example, with a coefficient 2, the maximum difference between two backends will be 33% and the maximum standard deviation will be 20.4%. With the current logic (coefficient of 1) we have maximum difference as high as 50%, as shown with the example at the beginning, with the standard deviation of 35.4%. The bottom half of the table shows from how many backends we start to use a particular number of buckets. For example, with a coeff. 3 we will have 16 hashes for 1 to 5 buckets, 32 hashes for 6-10, 64 buckets for 11-21 and so on. According to the table, the number 4 is about where we achieve a good enough standard deviation for the load (11.2%) while still not creating too many hashes for cases with low number of backends. The standard deviation also doesn't go down that much with higher coefficient. The data is aggregated for up to 64 backends because with higher numbers we're getting close to the cap of 256 hashes, so deviation increases. However, the load difference with so many backends should have lower impact on a cluster in general. The change improves the cases with 64+ backends, but to get very good numbers we'll need to consider moving the upper limit for the hash space, which is a separate change to think about. Added test checks that standard deviation of the load distribution between buckets is below 12.5% for all cases up to 64 buckets. It's just a pretty number that I've chosen that should be IMO good enough. Note: when number of buckets is a power of 2, then we have an ideal distribution with zero deviation, because hashes can be evenly mapped to buckets. Note 2: The change doesn't affect distribution for 4 or less backends, since there are still minimum 16 hashes for those groups. Reported-at: https://issues.redhat.com/browse/FDP-826 Acked-by: Eelco Chaudron <[email protected]> Acked-by: Aaron Conole <[email protected]> Signed-off-by: Ilya Maximets <[email protected]>
1 parent ba4b28b commit c2f2870

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

ofproto/ofproto-dpif.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5154,8 +5154,10 @@ group_setup_dp_hash_table(struct group_dpif *group, size_t max_hash)
51545154
min_weight, total_weight);
51555155

51565156
uint64_t min_slots = DIV_ROUND_UP(total_weight, min_weight);
5157-
uint64_t min_slots2 = ROUND_UP_POW2(min_slots);
5158-
uint64_t n_hash = MAX(16, min_slots2);
5157+
uint64_t min_slots2 =
5158+
MAX(min_slots, MIN(n_buckets * 4, MAX_SELECT_GROUP_HASH_VALUES));
5159+
uint64_t min_slots3 = ROUND_UP_POW2(min_slots2);
5160+
uint64_t n_hash = MAX(16, min_slots3);
51595161
if (n_hash > MAX_SELECT_GROUP_HASH_VALUES ||
51605162
(max_hash != 0 && n_hash > max_hash)) {
51615163
VLOG_DBG(" Too many hash values required: %"PRIu64, n_hash);

tests/ofproto-dpif.at

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,60 @@ bucket3 >= 500
12381238
OVS_VSWITCHD_STOP
12391239
AT_CLEANUP
12401240

1241+
AT_SETUP([ofproto-dpif - select group with dp_hash and equal weights])
1242+
1243+
OVS_VSWITCHD_START
1244+
add_of_ports br0 1 10
1245+
1246+
AT_CHECK([ovs-appctl vlog/set ofproto_dpif:file:dbg vconn:file:info])
1247+
1248+
AT_DATA([stddev.awk], [
1249+
{
1250+
# $1 (target) is a mean value, because all weights are the same.
1251+
# $2 (hits) is an actual number of hashes assigned to this bucket.
1252+
n_hashes += $2
1253+
n_buckets++
1254+
sum_sq_diff += ($2 - $1) * ($2 - $1)
1255+
}
1256+
END {
1257+
mean = n_hashes / n_buckets
1258+
stddev = sqrt(sum_sq_diff / n_buckets)
1259+
stddevp = stddev * 100 / mean
1260+
1261+
print "hashes:", n_hashes, "buckets:", n_buckets
1262+
print "mean:", mean, "stddev:", stddev, "(", stddevp, "% )"
1263+
1264+
# Make sure that standard deviation of load between buckets is below 12.5%.
1265+
# Note: it's not a strict requirement, but a good number that passes tests.
1266+
if (stddevp <= 12.5) { print "PASS" }
1267+
else { print "FAIL" }
1268+
}
1269+
])
1270+
1271+
m4_define([CHECK_DISTRIBUTION], [
1272+
AT_CHECK([tail -n $1 ovs-vswitchd.log | grep 'ofproto_dpif|DBG|.*Bucket' \
1273+
| sed 's/.*target=\([[0-9\.]]*\) hits=\([[0-9]]*\)/\1 \2/' \
1274+
| awk -f stddev.awk], [0], [stdout])
1275+
AT_CHECK([grep -q "buckets: $2" stdout])
1276+
AT_CHECK([grep -q 'PASS' stdout])
1277+
])
1278+
1279+
m4_define([OF_GROUP], [group_id=$1,type=select,selection_method=dp_hash])
1280+
m4_define([OFG_BUCKET], [bucket=weight=$1,output:10])
1281+
1282+
dnl Test load distribution in groups with up to 64 equally weighted buckets.
1283+
m4_define([OFG_BUCKETS], [OFG_BUCKET(100)])
1284+
m4_for([id], [1], [64], [1], [
1285+
get_log_next_line_num
1286+
AT_CHECK([ovs-ofctl -O OpenFlow15 add-group br0 \
1287+
"OF_GROUP(id),OFG_BUCKETS()"])
1288+
CHECK_DISTRIBUTION([+$LINENUM], [id])
1289+
m4_append([OFG_BUCKETS], [,OFG_BUCKET(100)])
1290+
])
1291+
1292+
OVS_VSWITCHD_STOP
1293+
AT_CLEANUP
1294+
12411295
AT_SETUP([ofproto-dpif - select group with explicit dp_hash selection method])
12421296

12431297
OVS_VSWITCHD_START

0 commit comments

Comments
 (0)