# Checking multiple conditions does not work

**URL:** <https://discourse.haproxy.org/t/checking-multiple-conditions-does-not-work/5553>\
**Category:** Help!\
**Created:** [August 4, 2020, 7:29am UTC](https://discourse.haproxy.org/t/checking-multiple-conditions-does-not-work/5553 "2020-08-04T07:29:15Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![guyman](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@guyman](https://discourse.haproxy.org/u/guyman)\
**Post date:** [August 4, 2020, 7:29am UTC](https://discourse.haproxy.org/t/checking-multiple-conditions-does-not-work/5553/1 "2020-08-04T07:29:15Z")

</div>

Hi,

I want to check 2 conditions on redis, where it should be master and on other checks, at least 2 of 3 sentinel nodes should provide this redis-instance being master.

Therefore, number of servers found on another backend check, is reused and if number if found server is at least 2, it should be selected.

However, condition with use-server and nbsrv is totally ignored. ge 2 is not working and lt 1 for example, not, too.  
Any ideas?  
Best  
Ronny

backend check\_master\_redis1  
mode tcp  
option tcp-check  
tcp-check connect  
tcp-check send PING\r\n  
tcp-check expect string +PONG  
tcp-check send SENTINEL\ master\ secucore-redis-001\r\n  
tcp-check expect string 10.240.50.1  
tcp-check send QUIT\r\n  
tcp-check expect string +OK

server sentinel1 10.240.50.1:26379 check inter 2s  
server sentinel2 10.240.50.2:26379 check inter 2s  
server sentinel3 10.240.50.3:26379 check inter 2s

backend check\_master\_redis2  
mode tcp  
option tcp-check  
tcp-check connect  
tcp-check send PING\r\n  
tcp-check expect string +PONG  
tcp-check send SENTINEL\ master\ secucore-redis-001\r\n  
tcp-check expect string 10.240.50.2  
tcp-check send QUIT\r\n  
tcp-check expect string +OK

server sentinel1 10.240.50.1:26379 check inter 2s  
server sentinel2 10.240.50.2:26379 check inter 2s  
server sentinel3 10.240.50.3:26379 check inter 2s

backend check\_master\_redis3  
mode tcp  
option tcp-check  
tcp-check connect  
tcp-check send PING\r\n  
tcp-check expect string +PONG  
tcp-check send SENTINEL\ master\ secucore-redis-001\r\n  
tcp-check expect string 10.240.50.3  
tcp-check send QUIT\r\n  
tcp-check expect string +OK

server sentinel1 10.240.50.1:26379 check inter 2s  
server sentinel2 10.240.50.2:26379 check inter 2s  
server sentinel3 10.240.50.3:26379 check inter 2s

frontend ft\_redis  
bind \*:6379  
mode tcp  
use\_backend bk\_redis

backend bk\_redis  
mode tcp

option tcp-check  
tcp-check connect  
tcp-check send PING\r\n  
tcp-check expect string +PONG  
tcp-check send info\ replication\r\n  
tcp-check expect string role:master  
tcp-check send QUIT\r\n  
tcp-check expect string +OK

use-server redis1 if { srv\_is\_up(redis1) } { nbsrv(check\_master\_redis1) ge 2 }  
use-server redis2 if { srv\_is\_up(redis2) } { nbsrv(check\_master\_redis2) ge 2 }  
use-server redis3 if { srv\_is\_up(redis3) } { nbsrv(check\_master\_redis3) ge 2 }

server redis1 10.240.50.1:6379 check inter 1s  
server redis2 10.240.50.2:6379 check inter 1s  
server redis3 10.240.50.3:6379 check inter 1s

---

<div class="post-metadata">

**Author:** ![lukastribus](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@lukastribus](https://discourse.haproxy.org/u/lukastribus)\
**Post date:** [August 5, 2020, 9:31pm UTC](https://discourse.haproxy.org/t/checking-multiple-conditions-does-not-work/5553/2 "2020-08-05T21:31:22Z")

</div>

You need to set server `weight` to 0 if you want to content switch like this.

[https://cbonte.github.io/haproxy-dconv/2.0/configuration.html#4.2-use-server](https://cbonte.github.io/haproxy-dconv/2.0/configuration.html#4.2-use-server)

> The “[use-server](https://cbonte.github.io/haproxy-dconv/2.0/configuration.html#use-server)” statement works both in HTTP and TCP mode. This makes it suitable for use with content-based inspection. For instance, a server could be selected in a farm according to the TLS SNI field. And if these servers have their weight set to zero, they will not be used for other traffic.

---

<div class="post-metadata">

**Author:** ![guyman](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@guyman](https://discourse.haproxy.org/u/guyman)\
**Post date:** [August 6, 2020, 6:12am UTC](https://discourse.haproxy.org/t/checking-multiple-conditions-does-not-work/5553/3 "2020-08-06T06:12:31Z")

</div>

Thanks for your hint,  
adding or removing weight to 0 does not take any effect on the configuration,  
the condition is still not checked and ignored ☹

---

<div class="post-metadata">

**Author:** ![lukastribus](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@lukastribus](https://discourse.haproxy.org/u/lukastribus)\
**Post date:** [August 6, 2020, 6:24am UTC](https://discourse.haproxy.org/t/checking-multiple-conditions-does-not-work/5553/4 "2020-08-06T06:24:16Z")

</div>

And what does that actually mean? Is it load-balanced to a random server or not at all?

---

<div class="post-metadata">

**Author:** ![guyman](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@guyman](https://discourse.haproxy.org/u/guyman)\
**Post date:** [August 6, 2020, 7:08am UTC](https://discourse.haproxy.org/t/checking-multiple-conditions-does-not-work/5553/5 "2020-08-06T07:08:29Z")

</div>

It is loadbalanced, BUT without checking the condition which is in the config like if { srv\_is\_up(redis1) } { nbsrv(check\_master\_redis1) ge 2 }

Even if you change it to lt 1 or anything else, it is totally ignored

---

<div class="post-metadata">

**Author:** ![lukastribus](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@lukastribus](https://discourse.haproxy.org/u/lukastribus)\
**Post date:** [August 6, 2020, 11:50am UTC](https://discourse.haproxy.org/t/checking-multiple-conditions-does-not-work/5553/6 "2020-08-06T11:50:41Z")

</div>

Ok, and when you set weigth to 0, it is not load-balanced anymore, is that correct?

`ge 2` will never be true, and `lt 1` won’t be either.

You have configured backends that check which server is the master, and I don’t think there will be more than one master active at one time, right?

So `ge 2` will never be true, because the number will never be 2 or larger.  
And `lt 1` will never be true, because the number will never be less than 1.

Please read:  
[https://cbonte.github.io/haproxy-dconv/2.0/configuration.html#7.1.2](https://cbonte.github.io/haproxy-dconv/2.0/configuration.html#7.1.2)

> Available operators for integer matching are :
> 
> eq : true if the tested value equals at least one value  
> ge : true if the tested value is greater than or equal to at least one value  
> gt : true if the tested value is greater than at least one value  
> le : true if the tested value is less than or equal to at least one value  
> lt : true if the tested value is less than at least one value

Check your logs and status pages to understand the real number of backend servers available and use the operators appropriately.

---

<div class="post-metadata">

**Author:** ![guyman](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@guyman](https://discourse.haproxy.org/u/guyman)\
**Post date:** [August 6, 2020, 12:10pm UTC](https://discourse.haproxy.org/t/checking-multiple-conditions-does-not-work/5553/7 "2020-08-06T12:10:31Z")

</div>

Well, this is the Problem ;):  
It is never true the result, so there should be no server available at the backend, but it always is … because it seems to prefer the tcp check before and to ignore the condition totally in use-server

Have you understood the problem?  
If not, we can do a skype or gotomeeting session if you ´re interested and I can explain it to you.  
Best  
Ronny

---

<div class="post-metadata">

**Author:** ![lukastribus](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@lukastribus](https://discourse.haproxy.org/u/lukastribus)\
**Post date:** [August 6, 2020, 12:30pm UTC](https://discourse.haproxy.org/t/checking-multiple-conditions-does-not-work/5553/8 "2020-08-06T12:30:21Z")

</div>

Right, that’s why I asked:

> [@lukastribus](#):
>
> Ok, and when you set weigth to 0, it is not load-balanced anymore, is that correct?

---

<div class="post-metadata">

**Author:** ![guyman](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@guyman](https://discourse.haproxy.org/u/guyman)\
**Post date:** [August 6, 2020, 1:33pm UTC](https://discourse.haproxy.org/t/checking-multiple-conditions-does-not-work/5553/9 "2020-08-06T13:33:26Z")

</div>

Setting weight to 0 or adding a weight in general, results in a blue selected node, but it still seems to be ignored because I can continue to connect, what should not be correct, because one of the master check results in 3 x available servers at all (means lt 1 to fake it and let them result in false), still does not work.

I did a workaround, using 3 backends now, with then checking with nbsrv, this is working well, so nbsrv seems not to work on use\_server, but on use-backend

Maybe we can have a 10 min websession and I present exactly what I mean

---

<div class="post-metadata">

**Author:** ![lukastribus](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@lukastribus](https://discourse.haproxy.org/u/lukastribus)\
**Post date:** [August 6, 2020, 4:23pm UTC](https://discourse.haproxy.org/t/checking-multiple-conditions-does-not-work/5553/10 "2020-08-06T16:23:33Z")

</div>

> [@guyman](#):
>
> results in a blue selected node, but it still seems to be ignored because I can continue to connect

How are you checking that, EXACTLY? Do you see in the haproxy log, that a connection to a server is established?

---

<div class="post-metadata">

**Author:** ![guyman](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@guyman](https://discourse.haproxy.org/u/guyman)\
**Post date:** [August 7, 2020, 8:15am UTC](https://discourse.haproxy.org/t/checking-multiple-conditions-does-not-work/5553/11 "2020-08-07T08:15:16Z")

</div>

Lukas,  
I am confused.

I now use this config with weight0

frontend ft\_redis  
bind \*:6379  
option dontlog-normal  
mode tcp  
maxconn 6000  
tcp-request connection reject if { nbsrv(check\_master\_redis1) ge 2 } { nbsrv(check\_master\_redis2) ge 2 } { nbsrv(check\_master\_redis3) ge 2 }  
default\_backend bk\_redis

backend bk\_redis  
mode tcp  
fullconn 6000  
option tcp-check  
tcp-check connect  
tcp-check send PING\r\n  
tcp-check expect string +PONG  
tcp-check send info\ replication\r\n  
tcp-check expect string role:master  
tcp-check send QUIT\r\n  
tcp-check expect string +OK  
use-server redis1 if { srv\_is\_up(redis1) } { nbsrv(check\_master\_redis1) ge 2 }  
server redis1 10.240.50.1:6379 check inter 1s maxconn 6000 weight 0  
use-server redis2 if { srv\_is\_up(redis2) } { nbsrv(check\_master\_redis2) ge 2 }  
server redis2 10.240.50.2:6379 check inter 1s maxconn 6000 weight 0  
use-server redis3 if { srv\_is\_up(redis3) } { nbsrv(check\_master\_redis3) ge 2 }  
server redis3 10.240.50.3:6379 check inter 1s maxconn 6000 weight 0

This is now working, I checked it using:

watch ‘redis-cli -h 10.240.40.200 -p 6379 info | grep run\_id’

I swore, the last 3 times I did it, it did not work either.

A little confusing is, using “lt 1” instead of “ge 2” to test the conditions, results still in a blue bar in haproxy, on the tcp-checked server which was valid, so “master” server is always marked as blue, because I think, this is a kind of wanted behaviour, that it still is marked as “up” because the tcp-check is valid, and the condition is not a kind of relevance for it?  
Best,  
Ronny

---

<div class="post-metadata">

**Author:** ![lukastribus](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@lukastribus](https://discourse.haproxy.org/u/lukastribus)\
**Post date:** [August 7, 2020, 9:04am UTC](https://discourse.haproxy.org/t/checking-multiple-conditions-does-not-work/5553/12 "2020-08-07T09:04:40Z")

</div>

The colors on the stats page are about the health check results and health check result only.

Your `use-server` directives have nothing to do with that.

---

<div class="post-metadata">

**Author:** ![guyman](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@guyman](https://discourse.haproxy.org/u/guyman)\
**Post date:** [August 7, 2020, 9:43am UTC](https://discourse.haproxy.org/t/checking-multiple-conditions-does-not-work/5553/13 "2020-08-07T09:43:36Z")

</div>

However, weight 0 is the trick you need, otherwise, the rules are ignored. Thank you very much.

---

<div class="post-metadata">

**Author:** ![lukastribus](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@lukastribus](https://discourse.haproxy.org/u/lukastribus)\
**Post date:** [August 10, 2020, 10:25am UTC](https://discourse.haproxy.org/t/checking-multiple-conditions-does-not-work/5553/14 "2020-08-10T10:25:08Z")

</div>

The rules are not ignored with the default weight, but normal load-balancing occurs, when none of the `use-server` directives match.

`use-server` is an optional configuration, and when the conditions do not match, it won’t do anything as expected.

And with a non-zero weight that means normal load-balancing will occur in that case.

---

<div class="post-metadata">

**Author:** ![guyman](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@guyman](https://discourse.haproxy.org/u/guyman)\
**Post date:** [August 10, 2020, 10:36am UTC](https://discourse.haproxy.org/t/checking-multiple-conditions-does-not-work/5553/15 "2020-08-10T10:36:47Z")

</div>

If I do not set any weight in the config, it does ignore my check for at least 2 servers on … 😉  
It began to work, if I set it to weight 0 🙂
