"Fossies" - the Fresh Open Source Software Archive

Member "haproxy-2.0.8/src/lb_fas.c" (23 Oct 2019, 9621 Bytes) of package /linux/misc/haproxy-2.0.8.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "lb_fas.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2.0.1_vs_2.0.2.

    1 /*
    2  * First Available Server load balancing algorithm.
    3  *
    4  * This file implements an algorithm which emerged during a discussion with
    5  * Steen Larsen, initially inspired from Anshul Gandhi et.al.'s work now
    6  * described as "packing" in section 3.5:
    7  *
    8  *    http://reports-archive.adm.cs.cmu.edu/anon/2012/CMU-CS-12-109.pdf
    9  *
   10  * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
   11  *
   12  * This program is free software; you can redistribute it and/or
   13  * modify it under the terms of the GNU General Public License
   14  * as published by the Free Software Foundation; either version
   15  * 2 of the License, or (at your option) any later version.
   16  *
   17  */
   18 
   19 #include <common/compat.h>
   20 #include <common/config.h>
   21 #include <common/debug.h>
   22 #include <eb32tree.h>
   23 
   24 #include <types/global.h>
   25 #include <types/server.h>
   26 
   27 #include <proto/backend.h>
   28 #include <proto/queue.h>
   29 
   30 
   31 /* Remove a server from a tree. It must have previously been dequeued. This
   32  * function is meant to be called when a server is going down or has its
   33  * weight disabled.
   34  *
   35  * The server's lock and the lbprm's lock must be held.
   36  */
   37 static inline void fas_remove_from_tree(struct server *s)
   38 {
   39     s->lb_tree = NULL;
   40 }
   41 
   42 /* simply removes a server from a tree.
   43  *
   44  * The server's lock and the lbprm's lock must be held.
   45  */
   46 static inline void fas_dequeue_srv(struct server *s)
   47 {
   48     eb32_delete(&s->lb_node);
   49 }
   50 
   51 /* Queue a server in its associated tree, assuming the weight is >0.
   52  * Servers are sorted by unique ID so that we send all connections to the first
   53  * available server in declaration order (or ID order) until its maxconn is
   54  * reached. It is important to understand that the server weight is not used
   55  * here.
   56  *
   57  * The server's lock and the lbprm's lock must be held.
   58  */
   59 static inline void fas_queue_srv(struct server *s)
   60 {
   61     s->lb_node.key = s->puid;
   62     eb32_insert(s->lb_tree, &s->lb_node);
   63 }
   64 
   65 /* Re-position the server in the FS tree after it has been assigned one
   66  * connection or after it has released one. Note that it is possible that
   67  * the server has been moved out of the tree due to failed health-checks.
   68  *
   69  * The server's lock must be held. The lbprm's lock will be used.
   70  */
   71 static void fas_srv_reposition(struct server *s)
   72 {
   73     HA_SPIN_LOCK(LBPRM_LOCK, &s->proxy->lbprm.lock);
   74     if (s->lb_tree) {
   75         fas_dequeue_srv(s);
   76         fas_queue_srv(s);
   77     }
   78     HA_SPIN_UNLOCK(LBPRM_LOCK, &s->proxy->lbprm.lock);
   79 }
   80 
   81 /* This function updates the server trees according to server <srv>'s new
   82  * state. It should be called when server <srv>'s status changes to down.
   83  * It is not important whether the server was already down or not. It is not
   84  * important either that the new state is completely down (the caller may not
   85  * know all the variables of a server's state).
   86  *
   87  * The server's lock must be held. The lbprm's lock will be used.
   88  */
   89 static void fas_set_server_status_down(struct server *srv)
   90 {
   91     struct proxy *p = srv->proxy;
   92 
   93     if (!srv_lb_status_changed(srv))
   94         return;
   95 
   96     if (srv_willbe_usable(srv))
   97         goto out_update_state;
   98 
   99     HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
  100 
  101     if (!srv_currently_usable(srv))
  102         /* server was already down */
  103         goto out_update_backend;
  104 
  105     if (srv->flags & SRV_F_BACKUP) {
  106         p->lbprm.tot_wbck -= srv->cur_eweight;
  107         p->srv_bck--;
  108 
  109         if (srv == p->lbprm.fbck) {
  110             /* we lost the first backup server in a single-backup
  111              * configuration, we must search another one.
  112              */
  113             struct server *srv2 = p->lbprm.fbck;
  114             do {
  115                 srv2 = srv2->next;
  116             } while (srv2 &&
  117                  !((srv2->flags & SRV_F_BACKUP) &&
  118                    srv_willbe_usable(srv2)));
  119             p->lbprm.fbck = srv2;
  120         }
  121     } else {
  122         p->lbprm.tot_wact -= srv->cur_eweight;
  123         p->srv_act--;
  124     }
  125 
  126     fas_dequeue_srv(srv);
  127     fas_remove_from_tree(srv);
  128 
  129  out_update_backend:
  130     /* check/update tot_used, tot_weight */
  131     update_backend_weight(p);
  132     HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
  133 
  134  out_update_state:
  135     srv_lb_commit_status(srv);
  136 }
  137 
  138 /* This function updates the server trees according to server <srv>'s new
  139  * state. It should be called when server <srv>'s status changes to up.
  140  * It is not important whether the server was already down or not. It is not
  141  * important either that the new state is completely UP (the caller may not
  142  * know all the variables of a server's state). This function will not change
  143  * the weight of a server which was already up.
  144  *
  145  * The server's lock must be held. The lbprm's lock will be used.
  146  */
  147 static void fas_set_server_status_up(struct server *srv)
  148 {
  149     struct proxy *p = srv->proxy;
  150 
  151     if (!srv_lb_status_changed(srv))
  152         return;
  153 
  154     if (!srv_willbe_usable(srv))
  155         goto out_update_state;
  156 
  157     HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
  158 
  159     if (srv_currently_usable(srv))
  160         /* server was already up */
  161         goto out_update_backend;
  162 
  163     if (srv->flags & SRV_F_BACKUP) {
  164         srv->lb_tree = &p->lbprm.fas.bck;
  165         p->lbprm.tot_wbck += srv->next_eweight;
  166         p->srv_bck++;
  167 
  168         if (!(p->options & PR_O_USE_ALL_BK)) {
  169             if (!p->lbprm.fbck) {
  170                 /* there was no backup server anymore */
  171                 p->lbprm.fbck = srv;
  172             } else {
  173                 /* we may have restored a backup server prior to fbck,
  174                  * in which case it should replace it.
  175                  */
  176                 struct server *srv2 = srv;
  177                 do {
  178                     srv2 = srv2->next;
  179                 } while (srv2 && (srv2 != p->lbprm.fbck));
  180                 if (srv2)
  181                     p->lbprm.fbck = srv;
  182             }
  183         }
  184     } else {
  185         srv->lb_tree = &p->lbprm.fas.act;
  186         p->lbprm.tot_wact += srv->next_eweight;
  187         p->srv_act++;
  188     }
  189 
  190     /* note that eweight cannot be 0 here */
  191     fas_queue_srv(srv);
  192 
  193  out_update_backend:
  194     /* check/update tot_used, tot_weight */
  195     update_backend_weight(p);
  196     HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
  197 
  198  out_update_state:
  199     srv_lb_commit_status(srv);
  200 }
  201 
  202 /* This function must be called after an update to server <srv>'s effective
  203  * weight. It may be called after a state change too.
  204  *
  205  * The server's lock must be held. The lbprm's lock will be used.
  206  */
  207 static void fas_update_server_weight(struct server *srv)
  208 {
  209     int old_state, new_state;
  210     struct proxy *p = srv->proxy;
  211 
  212     if (!srv_lb_status_changed(srv))
  213         return;
  214 
  215     /* If changing the server's weight changes its state, we simply apply
  216      * the procedures we already have for status change. If the state
  217      * remains down, the server is not in any tree, so it's as easy as
  218      * updating its values. If the state remains up with different weights,
  219      * there are some computations to perform to find a new place and
  220      * possibly a new tree for this server.
  221      */
  222      
  223     old_state = srv_currently_usable(srv);
  224     new_state = srv_willbe_usable(srv);
  225 
  226     if (!old_state && !new_state) {
  227         srv_lb_commit_status(srv);
  228         return;
  229     }
  230     else if (!old_state && new_state) {
  231         fas_set_server_status_up(srv);
  232         return;
  233     }
  234     else if (old_state && !new_state) {
  235         fas_set_server_status_down(srv);
  236         return;
  237     }
  238 
  239     HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
  240 
  241     if (srv->lb_tree)
  242         fas_dequeue_srv(srv);
  243 
  244     if (srv->flags & SRV_F_BACKUP) {
  245         p->lbprm.tot_wbck += srv->next_eweight - srv->cur_eweight;
  246         srv->lb_tree = &p->lbprm.fas.bck;
  247     } else {
  248         p->lbprm.tot_wact += srv->next_eweight - srv->cur_eweight;
  249         srv->lb_tree = &p->lbprm.fas.act;
  250     }
  251 
  252     fas_queue_srv(srv);
  253 
  254     update_backend_weight(p);
  255     HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
  256 
  257     srv_lb_commit_status(srv);
  258 }
  259 
  260 /* This function is responsible for building the trees in case of fast
  261  * weighted least-conns. It also sets p->lbprm.wdiv to the eweight to
  262  * uweight ratio. Both active and backup groups are initialized.
  263  */
  264 void fas_init_server_tree(struct proxy *p)
  265 {
  266     struct server *srv;
  267     struct eb_root init_head = EB_ROOT;
  268 
  269     p->lbprm.set_server_status_up   = fas_set_server_status_up;
  270     p->lbprm.set_server_status_down = fas_set_server_status_down;
  271     p->lbprm.update_server_eweight  = fas_update_server_weight;
  272     p->lbprm.server_take_conn = fas_srv_reposition;
  273     p->lbprm.server_drop_conn = fas_srv_reposition;
  274 
  275     p->lbprm.wdiv = BE_WEIGHT_SCALE;
  276     for (srv = p->srv; srv; srv = srv->next) {
  277         srv->next_eweight = (srv->uweight * p->lbprm.wdiv + p->lbprm.wmult - 1) / p->lbprm.wmult;
  278         srv_lb_commit_status(srv);
  279     }
  280 
  281     recount_servers(p);
  282     update_backend_weight(p);
  283 
  284     p->lbprm.fas.act = init_head;
  285     p->lbprm.fas.bck = init_head;
  286 
  287     /* queue active and backup servers in two distinct groups */
  288     for (srv = p->srv; srv; srv = srv->next) {
  289         if (!srv_currently_usable(srv))
  290             continue;
  291         srv->lb_tree = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.fas.bck : &p->lbprm.fas.act;
  292         fas_queue_srv(srv);
  293     }
  294 }
  295 
  296 /* Return next server from the FS tree in backend <p>. If the tree is empty,
  297  * return NULL. Saturated servers are skipped.
  298  *
  299  * The server's lock must be held. The lbprm's lock will be used.
  300  */
  301 struct server *fas_get_next_server(struct proxy *p, struct server *srvtoavoid)
  302 {
  303     struct server *srv, *avoided;
  304     struct eb32_node *node;
  305 
  306     srv = avoided = NULL;
  307 
  308     HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
  309     if (p->srv_act)
  310         node = eb32_first(&p->lbprm.fas.act);
  311     else if (p->lbprm.fbck) {
  312         srv = p->lbprm.fbck;
  313         goto out;
  314     }
  315     else if (p->srv_bck)
  316         node = eb32_first(&p->lbprm.fas.bck);
  317     else {
  318         srv = NULL;
  319         goto out;
  320     }
  321 
  322     while (node) {
  323         /* OK, we have a server. However, it may be saturated, in which
  324          * case we don't want to reconsider it for now, so we'll simply
  325          * skip it. Same if it's the server we try to avoid, in which
  326          * case we simply remember it for later use if needed.
  327          */
  328         struct server *s;
  329 
  330         s = eb32_entry(node, struct server, lb_node);
  331         if (!s->maxconn || (!s->nbpend && s->served < srv_dynamic_maxconn(s))) {
  332             if (s != srvtoavoid) {
  333                 srv = s;
  334                 break;
  335             }
  336             avoided = s;
  337         }
  338         node = eb32_next(node);
  339     }
  340 
  341     if (!srv)
  342         srv = avoided;
  343   out:
  344     HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
  345     return srv;
  346 }
  347 
  348 
  349 /*
  350  * Local variables:
  351  *  c-indent-level: 8
  352  *  c-basic-offset: 8
  353  * End:
  354  */