]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: log/balance: support for the "random" lb algorithm
authorAurelien DARRAGON <adarragon@haproxy.com>
Wed, 4 Oct 2023 15:30:02 +0000 (17:30 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Fri, 13 Oct 2023 08:05:06 +0000 (10:05 +0200)
In this patch we add basic support for the random algorithm:

random algorithm picks a random server using the result of the
statistical_prng() function as if it was a hash key to then compute the
related server ID.

There is no support for the <draw> parameter (which is implemented for
tcp/http load-balancing), because we don't have the required metrics to
evaluate server's load in log backends for the moment. Plus it would add
more complexity to the __do_send_log_backend() function so we'll keep it
this way for now but this might be needed in the future.

doc/configuration.txt
src/backend.c
src/log.c

index 6c903fcd50192018bacc1181d5862961f6e29600..d0643574b464b8d87783c859758aa886db357a26 100644 (file)
@@ -8839,6 +8839,12 @@ log-balance <algorithm> [ <arguments> ]
                   goes back UP it is added at the end of the list so that the
                   sticky server doesn't change until it becomes DOWN.
 
+      random      A random number will be used as the key for the server
+                  lookup. Random log balancing can be useful with large farms
+                  or when servers are frequently added or removed from the
+                  pool of available servers as it may avoid the hammering
+                  effect that could result from roundrobin in this situation.
+
     <arguments> is an optional list of arguments which may be needed by some
                 algorithms.
 
index 8b76a9343e0d62e1536508dcdb063940a65e33f6..e0b5a58775caf96177844593e76d08c49a8b605d 100644 (file)
@@ -2849,8 +2849,12 @@ int backend_parse_log_balance(const char **args, char **err, struct proxy *curpr
                /* we use ALGO_FAS as "sticky" mode in log-balance context */
                curproxy->lbprm.algo |= BE_LB_ALGO_FAS;
        }
+       else if (strcmp(args[0], "random") == 0) {
+               curproxy->lbprm.algo &= ~BE_LB_ALGO;
+               curproxy->lbprm.algo |= BE_LB_ALGO_RND;
+       }
        else {
-               memprintf(err, "only supports 'roundrobin', 'sticky' options");
+               memprintf(err, "only supports 'roundrobin', 'sticky', 'random', options");
                return -1;
        }
        return 0;
index 6900407a41e46e6475be81fd3a3a28a1e1087df0..53011f7f7c958d0be24a38edb2a9ff1328ee650e 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -2116,6 +2116,10 @@ static inline void __do_send_log_backend(struct proxy *be, struct log_header hdr
                 */
                targetid = 0;
        }
+       else if ((be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_RND) {
+               /* random mode */
+               targetid = statistical_prng() % nb_srv;
+       }
 
  skip_lb: