* as many times as its weight indicates it. If it's there too often, we remove
* the last occurrences. If it's not there enough, we add more occurrences. To
* remove a server from the tree, normally call this with eweight=0.
+ *
+ * The server's lock and the lbprm's lock must be held.
*/
static inline void chash_queue_dequeue_srv(struct server *s)
{
* It is not important whether the server was already down or not. It is not
* important either that the new state is completely down (the caller may not
* know all the variables of a server's state).
+ *
+ * The server's lock must be held. The lbprm lock will be used.
*/
static void chash_set_server_status_down(struct server *srv)
{
if (!srv_lb_status_changed(srv))
return;
+ HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
+
if (srv_willbe_usable(srv))
goto out_update_state;
update_backend_weight(p);
out_update_state:
srv_lb_commit_status(srv);
+
+ HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
}
/* This function updates the server trees according to server <srv>'s new
* important either that the new state is completely UP (the caller may not
* know all the variables of a server's state). This function will not change
* the weight of a server which was already up.
+ *
+ * The server's lock must be held. The lbprm lock will be used.
*/
static void chash_set_server_status_up(struct server *srv)
{
if (!srv_lb_status_changed(srv))
return;
+ HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
+
if (!srv_willbe_usable(srv))
goto out_update_state;
update_backend_weight(p);
out_update_state:
srv_lb_commit_status(srv);
+
+ HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
}
/* This function must be called after an update to server <srv>'s effective
* weight. It may be called after a state change too.
+ *
+ * The server's lock must be held. The lbprm lock may be used.
*/
static void chash_update_server_weight(struct server *srv)
{
return;
}
+ HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
+
/* only adjust the server's presence in the tree */
chash_queue_dequeue_srv(srv);
update_backend_weight(p);
srv_lb_commit_status(srv);
+
+ HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
}
/*
unsigned int dn, dp;
int loop;
+ HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
+
if (p->srv_act)
root = &p->lbprm.chash.act;
- else if (p->lbprm.fbck)
- return p->lbprm.fbck;
+ else if (p->lbprm.fbck) {
+ nsrv = p->lbprm.fbck;
+ goto out;
+ }
else if (p->srv_bck)
root = &p->lbprm.chash.bck;
- else
- return NULL;
+ else {
+ nsrv = NULL;
+ goto out;
+ }
/* find the node after and the node before */
next = eb32_lookup_ge(root, hash);
if (!next)
next = eb32_first(root);
- if (!next)
- return NULL; /* tree is empty */
+ if (!next) {
+ nsrv = NULL; /* tree is empty */
+ goto out;
+ }
prev = eb32_prev(next);
if (!prev)
nsrv = eb32_entry(next, struct tree_occ, node)->server;
}
+ out:
+ HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
return nsrv;
}
/* Remove a server from a tree. It must have previously been dequeued. This
* function is meant to be called when a server is going down or has its
* weight disabled.
+ *
+ * The server's lock and the lbprm's lock must be held.
*/
static inline void fas_remove_from_tree(struct server *s)
{
s->lb_tree = NULL;
}
-/* simply removes a server from a tree */
+/* simply removes a server from a tree.
+ *
+ * The server's lock and the lbprm's lock must be held.
+ */
static inline void fas_dequeue_srv(struct server *s)
{
eb32_delete(&s->lb_node);
* available server in declaration order (or ID order) until its maxconn is
* reached. It is important to understand that the server weight is not used
* here.
+ *
+ * The server's lock and the lbprm's lock must be held.
*/
static inline void fas_queue_srv(struct server *s)
{
/* Re-position the server in the FS tree after it has been assigned one
* connection or after it has released one. Note that it is possible that
* the server has been moved out of the tree due to failed health-checks.
+ *
+ * The server's lock must be held. The lbprm's lock will be used.
*/
static void fas_srv_reposition(struct server *s)
{
* It is not important whether the server was already down or not. It is not
* important either that the new state is completely down (the caller may not
* know all the variables of a server's state).
+ *
+ * The server's lock must be held. The lbprm's lock will be used.
*/
static void fas_set_server_status_down(struct server *srv)
{
if (srv_willbe_usable(srv))
goto out_update_state;
+ HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
+
if (!srv_currently_usable(srv))
/* server was already down */
goto out_update_backend;
out_update_backend:
/* check/update tot_used, tot_weight */
update_backend_weight(p);
+ HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
+
out_update_state:
srv_lb_commit_status(srv);
}
* important either that the new state is completely UP (the caller may not
* know all the variables of a server's state). This function will not change
* the weight of a server which was already up.
+ *
+ * The server's lock must be held. The lbprm's lock will be used.
*/
static void fas_set_server_status_up(struct server *srv)
{
if (!srv_willbe_usable(srv))
goto out_update_state;
+ HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
+
if (srv_currently_usable(srv))
/* server was already up */
goto out_update_backend;
out_update_backend:
/* check/update tot_used, tot_weight */
update_backend_weight(p);
+ HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
+
out_update_state:
srv_lb_commit_status(srv);
}
/* This function must be called after an update to server <srv>'s effective
* weight. It may be called after a state change too.
+ *
+ * The server's lock must be held. The lbprm's lock will be used.
*/
static void fas_update_server_weight(struct server *srv)
{
return;
}
+ HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
+
if (srv->lb_tree)
fas_dequeue_srv(srv);
fas_queue_srv(srv);
update_backend_weight(p);
+ HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
+
srv_lb_commit_status(srv);
}
/* Return next server from the FS tree in backend <p>. If the tree is empty,
* return NULL. Saturated servers are skipped.
+ *
+ * The server's lock must be held. The lbprm's lock will be used.
*/
struct server *fas_get_next_server(struct proxy *p, struct server *srvtoavoid)
{
/* Remove a server from a tree. It must have previously been dequeued. This
* function is meant to be called when a server is going down or has its
* weight disabled.
+ *
+ * The server's lock and the lbprm's lock must be held.
*/
static inline void fwlc_remove_from_tree(struct server *s)
{
s->lb_tree = NULL;
}
-/* simply removes a server from a tree */
+/* simply removes a server from a tree.
+ *
+ * The server's lock and the lbprm's lock must be held.
+ */
static inline void fwlc_dequeue_srv(struct server *s)
{
eb32_delete(&s->lb_node);
/* Queue a server in its associated tree, assuming the weight is >0.
* Servers are sorted by #conns/weight. To ensure maximum accuracy,
* we use #conns*SRV_EWGHT_MAX/eweight as the sorting key.
+ *
+ * The server's lock and the lbprm's lock must be held.
*/
static inline void fwlc_queue_srv(struct server *s)
{
/* Re-position the server in the FWLC tree after it has been assigned one
* connection or after it has released one. Note that it is possible that
* the server has been moved out of the tree due to failed health-checks.
+ *
+ * The server's lock must be held. The lbprm's lock will be used.
*/
static void fwlc_srv_reposition(struct server *s)
{
* It is not important whether the server was already down or not. It is not
* important either that the new state is completely down (the caller may not
* know all the variables of a server's state).
+ *
+ * The server's lock must be held. The lbprm's lock will be used.
*/
static void fwlc_set_server_status_down(struct server *srv)
{
if (srv_willbe_usable(srv))
goto out_update_state;
+ HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
+
if (!srv_currently_usable(srv))
/* server was already down */
out_update_backend:
/* check/update tot_used, tot_weight */
update_backend_weight(p);
+ HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
+
out_update_state:
srv_lb_commit_status(srv);
}
* important either that the new state is completely UP (the caller may not
* know all the variables of a server's state). This function will not change
* the weight of a server which was already up.
+ *
+ * The server's lock must be held. The lbprm's lock will be used.
*/
static void fwlc_set_server_status_up(struct server *srv)
{
if (!srv_willbe_usable(srv))
goto out_update_state;
+ HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
+
if (srv_currently_usable(srv))
/* server was already up */
goto out_update_backend;
out_update_backend:
/* check/update tot_used, tot_weight */
update_backend_weight(p);
+ HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
+
out_update_state:
srv_lb_commit_status(srv);
}
/* This function must be called after an update to server <srv>'s effective
* weight. It may be called after a state change too.
+ *
+ * The server's lock must be held. The lbprm's lock will be used.
*/
static void fwlc_update_server_weight(struct server *srv)
{
return;
}
+ HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
+
if (srv->lb_tree)
fwlc_dequeue_srv(srv);
fwlc_queue_srv(srv);
update_backend_weight(p);
+ HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
+
srv_lb_commit_status(srv);
}
/* Return next server from the FWLC tree in backend <p>. If the tree is empty,
* return NULL. Saturated servers are skipped.
+ *
+ * The server's lock must be held. The lbprm's lock will be used.
*/
struct server *fwlc_get_next_server(struct proxy *p, struct server *srvtoavoid)
{
* It is not important whether the server was already down or not. It is not
* important either that the new state is completely down (the caller may not
* know all the variables of a server's state).
+ *
+ * The server's lock must be held. The lbprm's lock will be used.
*/
static void fwrr_set_server_status_down(struct server *srv)
{
if (srv_willbe_usable(srv))
goto out_update_state;
+ HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
+
if (!srv_currently_usable(srv))
/* server was already down */
goto out_update_backend;
out_update_backend:
/* check/update tot_used, tot_weight */
update_backend_weight(p);
+ HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
+
out_update_state:
srv_lb_commit_status(srv);
}
* important either that the new state is completely UP (the caller may not
* know all the variables of a server's state). This function will not change
* the weight of a server which was already up.
+ *
+ * The server's lock must be held. The lbprm's lock will be used.
*/
static void fwrr_set_server_status_up(struct server *srv)
{
if (!srv_willbe_usable(srv))
goto out_update_state;
+ HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
+
if (srv_currently_usable(srv))
/* server was already up */
goto out_update_backend;
out_update_backend:
/* check/update tot_used, tot_weight */
update_backend_weight(p);
+ HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
+
out_update_state:
srv_lb_commit_status(srv);
}
/* This function must be called after an update to server <srv>'s effective
* weight. It may be called after a state change too.
+ *
+ * The server's lock must be held. The lbprm's lock will be used.
*/
static void fwrr_update_server_weight(struct server *srv)
{
return;
}
+ HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
+
grp = (srv->flags & SRV_F_BACKUP) ? &p->lbprm.fwrr.bck : &p->lbprm.fwrr.act;
grp->next_weight = grp->next_weight - srv->cur_eweight + srv->next_eweight;
}
update_backend_weight(p);
+ HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
+
srv_lb_commit_status(srv);
}
/* Remove a server from a tree. It must have previously been dequeued. This
* function is meant to be called when a server is going down or has its
* weight disabled.
+ *
+ * The server's lock and the lbprm's lock must be held.
*/
static inline void fwrr_remove_from_tree(struct server *s)
{
/* Queue a server in the weight tree <root>, assuming the weight is >0.
* We want to sort them by inverted weights, because we need to place
* heavy servers first in order to get a smooth distribution.
+ *
+ * The server's lock and the lbprm's lock must be held.
*/
static inline void fwrr_queue_by_weight(struct eb_root *root, struct server *s)
{
}
}
-/* simply removes a server from a weight tree */
+/* simply removes a server from a weight tree.
+ *
+ * The server's lock and the lbprm's lock must be held.
+ */
static inline void fwrr_dequeue_srv(struct server *s)
{
eb32_delete(&s->lb_node);
/* queues a server into the appropriate group and tree depending on its
* backup status, and ->npos. If the server is disabled, simply assign
* it to the NULL tree.
+ *
+ * The server's lock and the lbprm's lock must be held.
*/
static void fwrr_queue_srv(struct server *s)
{
}
}
-/* prepares a server when extracting it from the "init" tree */
+/* prepares a server when extracting it from the "init" tree.
+ *
+ * The server's lock and the lbprm's lock must be held.
+ */
static inline void fwrr_get_srv_init(struct server *s)
{
s->npos = s->rweight = 0;
}
-/* prepares a server when extracting it from the "next" tree */
+/* prepares a server when extracting it from the "next" tree.
+ *
+ * The server's lock and the lbprm's lock must be held.
+ */
static inline void fwrr_get_srv_next(struct server *s)
{
struct fwrr_group *grp = (s->flags & SRV_F_BACKUP) ?
HA_ATOMIC_ADD(&s->npos, grp->curr_weight);
}
-/* prepares a server when it was marked down */
+/* prepares a server when it was marked down.
+ *
+ * The server's lock and the lbprm's lock must be held.
+ */
static inline void fwrr_get_srv_down(struct server *s)
{
struct fwrr_group *grp = (s->flags & SRV_F_BACKUP) ?
s->npos = grp->curr_pos;
}
-/* prepares a server when extracting it from its tree */
+/* prepares a server when extracting it from its tree.
+ *
+ * The server's lock and the lbprm's lock must be held.
+ */
static void fwrr_get_srv(struct server *s)
{
struct proxy *p = s->proxy;
/* switches trees "init" and "next" for FWRR group <grp>. "init" should be empty
* when this happens, and "next" filled with servers sorted by weights.
+ *
+ * The lbprm's lock must be held.
*/
static inline void fwrr_switch_trees(struct fwrr_group *grp)
{
/* return next server from the current tree in FWRR group <grp>, or a server
* from the "init" tree if appropriate. If both trees are empty, return NULL.
+ *
+ * The lbprm's lock must be held.
*/
static struct server *fwrr_get_server_from_group(struct fwrr_group *grp)
{
/* Computes next position of server <s> in the group. It is mandatory for <s>
* to have a non-zero, positive eweight.
-*/
+ *
+ * The server's lock and the lbprm's lock must be held.
+ */
static inline void fwrr_update_position(struct fwrr_group *grp, struct server *s)
{
if (!s->npos) {
/* Return next server from the current tree in backend <p>, or a server from
* the init tree if appropriate. If both trees are empty, return NULL.
* Saturated servers are skipped and requeued.
+ *
+ * The server's lock must be held. The lbprm's lock will be used.
*/
struct server *fwrr_get_next_server(struct proxy *p, struct server *srvtoavoid)
{
#include <proto/proto_tcp.h>
#include <proto/queue.h>
-/* this function updates the map according to server <srv>'s new state */
+/* this function updates the map according to server <srv>'s new state.
+ *
+ * The server's lock must be held. The lbprm's lock will be used.
+ */
static void map_set_server_status_down(struct server *srv)
{
struct proxy *p = srv->proxy;
goto out_update_state;
/* FIXME: could be optimized since we know what changed */
+ HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
recount_servers(p);
update_backend_weight(p);
recalc_server_map(p);
+ HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
out_update_state:
srv_lb_commit_status(srv);
}
-/* This function updates the map according to server <srv>'s new state */
+/* This function updates the map according to server <srv>'s new state.
+ *
+ * The server's lock must be held. The lbprm's lock will be used.
+ */
static void map_set_server_status_up(struct server *srv)
{
struct proxy *p = srv->proxy;
goto out_update_state;
/* FIXME: could be optimized since we know what changed */
+ HA_SPIN_LOCK(LBPRM_LOCK, &p->lbprm.lock);
recount_servers(p);
update_backend_weight(p);
recalc_server_map(p);
+ HA_SPIN_UNLOCK(LBPRM_LOCK, &p->lbprm.lock);
out_update_state:
srv_lb_commit_status(srv);
}
* px->lbprm.tot_wact, tot_wbck, tot_used, tot_weight, so it must be
* called after recount_servers(). It also expects px->lbprm.map.srv
* to be allocated with the largest size needed. It updates tot_weight.
+ *
+ * The lbprm's lock must be held.
*/
void recalc_server_map(struct proxy *px)
{
* the proxy <px> following the round-robin method.
* If any server is found, it will be returned and px->lbprm.map.rr_idx will be updated
* to point to the next server. If no valid server is found, NULL is returned.
+ *
+ * The lbprm's lock will be used.
*/
struct server *map_get_server_rr(struct proxy *px, struct server *srvtoavoid)
{
* pointed to by the result of a modulo operation on <hash>. The server map may
* be recomputed if required before being looked up. If any server is found, it
* will be returned. If no valid server is found, NULL is returned.
+ *
+ * The lbprm's lock will be used.
*/
struct server *map_get_server_hash(struct proxy *px, unsigned int hash)
{
- if (px->lbprm.tot_weight == 0)
- return NULL;
- return px->lbprm.map.srv[hash % px->lbprm.tot_weight];
+ struct server *srv = NULL;
+
+ HA_SPIN_LOCK(LBPRM_LOCK, &px->lbprm.lock);
+ if (px->lbprm.tot_weight)
+ srv = px->lbprm.map.srv[hash % px->lbprm.tot_weight];
+ HA_SPIN_UNLOCK(LBPRM_LOCK, &px->lbprm.lock);
+ return srv;
}