From e043828601d8e7cd0087ab6fc40ecc23cf8fd21d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?L=C3=A9o=20Gillot-Lamure?=
 <leo.gillot-lamure-ext@socgen.com>
Date: Thu, 18 May 2023 19:09:43 +0200
Subject: [PATCH] loadbalancer: support setting nbthread with variable
 haproxy_threads
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Threads are the recommended way to scale CPU performance since HAProxy
1.8.

Official documentation says: « While "nbproc" historically used to be the only
way to use multiple processors, it also involved a number of shortcomings
related to the lack of synchronization between processes (health-checks, peers,
stick-tables, stats, ...) which do not affect threads. As such, any modern
configuration is strongly encouraged to migrate away from "nbproc" to
"nbthread". ».

While more recent versions of HAProxy automatically detect the number of
available CPU and enable threads for them, it can be useful to explicitely set
the value.

In this patch, setting cpu-map for threads is not supported.

Change-Id: Id917c70f3dbe52f24f25d9403ba8151729e8966b
---
 ansible/roles/loadbalancer/defaults/main.yml                | 2 ++
 .../loadbalancer/templates/haproxy/haproxy_main.cfg.j2      | 1 +
 releasenotes/notes/haproxy-nbthread-7680f1be54eeb365.yaml   | 6 ++++++
 3 files changed, 9 insertions(+)
 create mode 100644 releasenotes/notes/haproxy-nbthread-7680f1be54eeb365.yaml

diff --git a/ansible/roles/loadbalancer/defaults/main.yml b/ansible/roles/loadbalancer/defaults/main.yml
index ddc8c604de..7db023e017 100644
--- a/ansible/roles/loadbalancer/defaults/main.yml
+++ b/ansible/roles/loadbalancer/defaults/main.yml
@@ -51,6 +51,8 @@ keepalived_traffic_mode: "multicast"
 
 # Extended global configuration, optimization options.
 haproxy_max_connections: 40000
+haproxy_threads: 1
+# More than 1 process is deprecated by HAProxy, prefer haproxy_threads
 haproxy_processes: 1
 haproxy_process_cpu_map: "no"
 # Matches the mariadb 10000 max connections limit
diff --git a/ansible/roles/loadbalancer/templates/haproxy/haproxy_main.cfg.j2 b/ansible/roles/loadbalancer/templates/haproxy/haproxy_main.cfg.j2
index 5e4ad2c673..39a6ceed51 100644
--- a/ansible/roles/loadbalancer/templates/haproxy/haproxy_main.cfg.j2
+++ b/ansible/roles/loadbalancer/templates/haproxy/haproxy_main.cfg.j2
@@ -7,6 +7,7 @@ global
     log {{ syslog_server }}:{{ syslog_udp_port }} {{ syslog_haproxy_facility }}
     maxconn {{ haproxy_max_connections }}
     nbproc {{ haproxy_processes }}
+    nbthread {{ haproxy_threads }}
     {% if (haproxy_processes | int > 1) and (haproxy_process_cpu_map | bool) %}
         {% for cpu_idx in range(0, haproxy_processes) %}
     cpu-map {{ cpu_idx + 1 }} {{ cpu_idx }}
diff --git a/releasenotes/notes/haproxy-nbthread-7680f1be54eeb365.yaml b/releasenotes/notes/haproxy-nbthread-7680f1be54eeb365.yaml
new file mode 100644
index 0000000000..c71dd7e2e2
--- /dev/null
+++ b/releasenotes/notes/haproxy-nbthread-7680f1be54eeb365.yaml
@@ -0,0 +1,6 @@
+---
+features:
+  - |
+    HAProxy supports setting nbthread via variable haproxy_threads.
+    Threads are recommended instead of processes since HAProxy 1.8.
+    They cannot be used both at the same time.