diff --git a/Database-High-Availability.md b/Database-High-Availability.md new file mode 100644 index 0000000..126cad4 --- /dev/null +++ b/Database-High-Availability.md @@ -0,0 +1,349 @@ +# Database High Availability + +This page describes how to place the Cacti database on a MariaDB Galera +cluster or a replicated primary and replica without breaking polling or +plugin processing. It records which layouts were tested, what each one did, +and what remains untested. + +The short version: Cacti must talk to exactly one database server at a time. +Use the other servers as standbys and let a proxy move Cacti to a standby when +the active server fails. Do not split reads and writes across servers. + +## Summary + +- Route every Cacti connection, including plugin connections, to one writable + server. This is called a single-writer layout. +- Do not use a read/write splitting router such as the MaxScale + `readwritesplit` router for Cacti. It sends lock functions and other + statements of one session to different servers. On Galera with the stock + tables, polling stopped updating graphs without logging an error. +- Do not let two Cacti processes connect to different cluster nodes directly. +- Decide what happens when a failed server returns. Either stay on the + standby until a planned change, or make the proxy close standby sessions + when it moves back. Otherwise two servers can hold Cacti sessions at once. +- Converting the `MEMORY` tables to InnoDB removes one cause of failure. It + does not make read/write splitting or multiple writers safe. + +## Why Cacti needs one database server + +Cacti keeps short-lived working state in the database and expects every +connection to see that state immediately. Three mechanisms depend on it. + +- **`MEMORY` tables.** The poller writes collected values to `poller_output` + and the main poller reads them back to update RRD files. Background jobs + record themselves in the `processes` table so that a second copy does not + start. Both tables use the `MEMORY` storage engine. Galera replicates InnoDB + tables only, so a row written on one node does not exist on the others. See + the Galera known limitations page listed under [References](#references). +- **Named locks.** Cacti and several plugins call `GET_LOCK()` to stop two + jobs from running together. A named lock belongs to one connection on one + server. In testing, two sessions on two Galera nodes both obtained the same + lock name. +- **Reading back what was just written.** Cacti writes a row and reads it on + the next statement, sometimes from a different process. A replica can + answer before the row arrives. In testing on a primary and replica, a count + taken through a read/write splitting router immediately after an insert + returned zero, and the next read returned one. This happened once and + replica lag was not measured. No stale InnoDB read was seen on Galera in + these tests, but Galera does not rule one out with its default settings. + +The list of `MEMORY` tables for this branch is in +[General Installing Instructions](General-Installing-Instructions.md). + +## What was tested + +The results below come from one test run on 3 December 2026 with small cloud +virtual machines on a private network. + +| Component | Version | +| --- | --- | +| Cacti | 1.2.32 from the `1.2.x` branch | +| Syslog plugin | 5.0 from the plugin's `develop` branch | +| MariaDB with Galera | 10.6.24, three nodes | +| MaxScale | 24.02.9 | +| HAProxy | 2.8 | + +Cacti 1.2.32 is the release line this manual documents. The development +branch was not run on a cluster. + +Each run used the command line installer, one device with five data sources, +forced poller cycles, and 2,000 to 30,000 generated Syslog messages processed +by two Syslog workers. That is a functional test. It says nothing about +performance at production size. + +### Results on a three-node Galera cluster + +| Route from Cacti to the cluster | Poller | Syslog processing | +| --- | --- | --- | +| MaxScale `readconnroute`, `router_options=master` | Graphs updated | All messages processed | +| HAProxy, one active server and two `backup` servers | Graphs updated | All messages processed | +| MaxScale `readwritesplit`, stock tables | No graphs updated in 5 cycles across three runs, no error logged | One master: all messages processed. Two masters: both ran | +| MaxScale `readwritesplit`, `MEMORY` tables converted to InnoDB | Graphs updated in 8 of 8 cycles | Tested with two masters only: one ran and processed all messages, and logged stale worker warnings | + +With `readwritesplit` and the stock tables, each poller cycle reported +`RRDsProcessed:0` and exited normally. The collected values stayed in +`poller_output` on the node that received the writes, and they accumulated +with each cycle. The table was empty on the other two nodes, and the routing +test below shows that reads of a `MEMORY` table went to those nodes. + +### Statement routing observed through MaxScale + +One client session through `readwritesplit` ran these statements in order. + +| Statement | Answered by | +| --- | --- | +| `SELECT @@hostname` | A replica | +| `SELECT GET_LOCK('name', 0)` | The primary | +| `SELECT CONNECTION_ID()` | A replica | +| `SELECT IS_USED_LOCK('name')` | The primary | +| `SELECT COUNT(*)` on a `MEMORY` table just written | A replica, returned 0 | +| `SELECT COUNT(*)` on an InnoDB table just written | A replica, returned 1 | + +`CONNECTION_ID()` returned 83 and `IS_USED_LOCK()` returned 87 in the same +client session, because the two statements ran on different servers. Code +that compares the two values cannot confirm that it holds its own lock. +The same session through `readconnroute` or HAProxy returned one server name +and one connection ID throughout. + +The MaxScale routing rules are described in the router references listed +under [References](#references). + +## Recommended layout + +Run three Galera nodes so the cluster keeps quorum when one node is lost. Put +a proxy between Cacti and the cluster and configure it to send all +connections to one node. Point `$database_hostname` and `$database_port` in +`include/config.php` at the proxy. If a plugin has its own database settings, +point those at the same proxy listener. + +The proxy is itself a single point of failure unless you run two of them +behind a virtual IP address. That part was not tested. + +### MaxScale + +Use the `readconnroute` router with `router_options=master`. The `galeramon` +monitor chooses one node as the master. Set `use_priority` so the choice is +predictable and `disable_master_failback` so a returning node does not take +the role back. + +```ini +[db1] +type=server +address=192.0.2.11 +port=3306 +priority=1 + +[db2] +type=server +address=192.0.2.12 +port=3306 +priority=2 + +[db3] +type=server +address=192.0.2.13 +port=3306 +priority=3 + +[galera-monitor] +type=monitor +module=galeramon +servers=db1,db2,db3 +user=maxscale_monitor +password= +monitor_interval=1s +use_priority=true +disable_master_failback=true + +[cacti-single-writer] +type=service +router=readconnroute +router_options=master +servers=db1,db2,db3 +user=maxscale_monitor +password= + +[cacti-listener] +type=listener +service=cacti-single-writer +port=3306 +``` + +Replace the addresses, account name, and password placeholders with your own +values. Check the licence terms of the MaxScale version you deploy before you +depend on it; the terms differ between release series. + +### HAProxy + +HAProxy in TCP mode gives the same single-writer behaviour with open source +software. Mark every server except one as `backup`. HAProxy needs a health +check that knows whether the node is synced, because a node that accepts TCP +connections is not necessarily usable. Run a small HTTP responder on each +node that returns status 200 only when `wsrep_local_state` is 4, for example +the `clustercheck` script distributed with Percona XtraDB Cluster. + +```haproxy +listen cacti-single-writer + bind *:3306 + mode tcp + option httpchk GET / + http-check expect status 200 + timeout client 30m + timeout server 30m + default-server port 9200 inter 1s fall 2 rise 2 on-marked-down shutdown-sessions + server db1 192.0.2.11:3306 check on-marked-up shutdown-backup-sessions + server db2 192.0.2.12:3306 check backup + server db3 192.0.2.13:3306 check backup +``` + +`option httpchk GET /` makes HAProxy send an HTTP request to the responder. +The responder must read the whole request before it replies. The first +responder used in testing replied without reading it. HAProxy logged +`Connection reset by peer` for each check, marked every node down, and +refused client connections. The tests used a short script written for the +purpose, not the Percona script. + +`on-marked-up shutdown-backup-sessions` matters. Without it, HAProxy sent new +connections back to `db1` as soon as it returned, while a session opened +during the outage stayed on `db2`. Two nodes then held client sessions until +that session ended. The test clients only read, so no conflicting writes +were made. With the option set, HAProxy closed the session on `db2` when +`db1` returned, and all connections were on one node again. + +HAProxy always returns to the first server when it recovers. If you would +rather stay on the standby until a planned change, use MaxScale with +`disable_master_failback` or manage the HAProxy server state yourself. + +## What happens during a failover + +The active node was stopped while one client held a session open and another +opened a new connection every second. + +| Event | MaxScale `readconnroute` | HAProxy with `backup` servers | +| --- | --- | --- | +| Session open on the failed node | Closed with error 2013 | Closed with error 2013 | +| New connections reach the next node | About 3 seconds | About 3 seconds | +| Failed node returns | Stayed on the new node | Returned to the first node | + +Each case was run once, with a client that sampled once per second. Both +proxies checked node health once per second; other intervals were not tested. +After the MaxScale run, MaxScale listed the returned node as synced and kept +the standby as master. + +A Cacti process that loses its connection in the middle of a poller cycle +was not tested. Expect that cycle to be incomplete and the next cycle to run +normally. After the failover in testing, the next poller cycle and Syslog +run completed against the new node with no change to Cacti. + +A `MEMORY` table is emptied when its server restarts, and its rows exist only +on the node that received them. Unprocessed `poller_output` rows on a failed +node are therefore expected to be lost, and InnoDB tables are expected to +keep them. This follows from how the engine works and was not tested. + +## Converting MEMORY tables to InnoDB + +[General Installing Instructions](General-Installing-Instructions.md) lists +the `ALTER TABLE` statements. In testing the conversion had these effects. + +- Polling through `readwritesplit` wrote graphs again in 8 of 8 cycles. +- A second Syslog master started one second after the first did not run. + This was one run through `readwritesplit` and two runs with the masters + connected to different nodes directly. +- When two Syslog masters were started at the same instant on different + nodes, both ran in 1 of 3 attempts and the registry held two rows for the + same job. In the Cacti source, `register_process_start()` reads the table + and then inserts, and the table's primary key includes the process ID, so + a second row is accepted. + +Convert the tables if a standby must take over without losing queued poller +output. Do not treat the conversion as permission to split reads from writes +or to write on several nodes. Named locks remain local to one node, and +reads can still be answered by a different server than the last write. + +These tests did not include a Cacti upgrade. Check the table engines after +each upgrade, and convert again if any have changed back: + +```sql +SELECT TABLE_NAME, ENGINE +FROM information_schema.TABLES +WHERE TABLE_SCHEMA = 'cacti' AND ENGINE = 'MEMORY'; +``` + +## Syslog plugin + +The Syslog plugin runs one master process that starts worker processes. The +master must be unique, and each worker checks that its master is still +running. + +- Development builds of the plugin from before the fix for + [plugin_syslog issue 479](https://github.com/Cacti/plugin_syslog/issues/479) + used a named lock for that check. Through `readwritesplit`, every worker + logged `the processing run that launched it no longer holds the processing + lock` and exited, and no messages were transferred. The same build worked + through a single-writer route. +- Current builds use the Cacti process registry. Through a single-writer + route a second master did not run. Through `readwritesplit` a single + master processed all messages. With two masters through `readwritesplit`, + or on different nodes, both ran at the same time while the registry was a + `MEMORY` table, and one logged `One or more transfer workers failed` + while its workers were still running. See + [plugin_syslog issue 502](https://github.com/Cacti/plugin_syslog/issues/502). + +In the runs where two masters overlapped, the number of rows added to the +`syslog` table matched the number of messages injected. That is not evidence +that overlapping masters are safe. Alerts, reports, and removal rules were +not configured in these tests. + +If the plugin uses its own database, as set in the plugin's `config.php`, +send that connection through the same single-writer listener. + +## Primary and replica replication + +Two of the test servers were reconfigured as a primary and one replica with +standard binary log replication, monitored by the MaxScale `mariadbmon` +monitor. + +- `MEMORY` table rows did reach the replica. Polling through + `readwritesplit` wrote graphs in 8 of 8 cycles. +- `GET_LOCK()`, `CONNECTION_ID()` and `IS_USED_LOCK()` were routed as on + Galera. `CONNECTION_ID()` and `IS_USED_LOCK()` ran on different servers, + and the older Syslog build failed in the same way. +- A read of an InnoDB table through `readwritesplit` immediately after an + insert returned stale data once in the routing test. +- Through `readconnroute` with `router_options=master`, polling and Syslog + processing completed normally. + +The recommendation is the same: one writer, reached through a route that +never sends Cacti statements to a replica. Automatic promotion of the replica +was configured but not exercised. + +## Limits of these tests + +- One small installation, one device, five data sources. No load testing. +- Cacti 1.2.32 only. The development branch was not run. +- MariaDB 10.6.24 and MaxScale 24.02.9 only. Other versions may route or lock + differently. +- Remote data collectors, Boost, Spine, and plugins other than Syslog were + not exercised. +- ProxySQL was not tested. +- Network partitions, loss of quorum, and state transfers during polling were + not tested. Only a clean stop and start of the active node was, once per + proxy, with a read-only test client and not with Cacti running. +- Loss of the database connection in the middle of a poller cycle was not + tested. +- Proxy redundancy was not tested. +- Sample sizes are small. The HAProxy route was polled for one cycle. The + simultaneous-start result is three attempts, not a rate. Poller cycle + counts combine several runs, and the poller also starts its own Syslog run + in the background. +- The primary and replica pair had one replica, and replication lag was not + measured. + +## References + +| Topic | Source | +| --- | --- | +| What Galera replicates | [MariaDB Galera Cluster known limitations](https://mariadb.com/docs/galera-cluster/reference/mariadb-galera-cluster-known-limitations) | +| Read/write split routing | [MaxScale readwritesplit](https://mariadb.com/docs/maxscale/reference/maxscale-routers/maxscale-readwritesplit) | +| Single-server routing | [MaxScale readconnroute](https://mariadb.com/docs/maxscale/reference/maxscale-routers/maxscale-readconnroute) | +| Health checks and `backup` servers | [HAProxy 2.8 configuration manual](https://docs.haproxy.org/2.8/configuration.html) | diff --git a/General-Installing-Instructions.md b/General-Installing-Instructions.md index e8b0c01..f4e9513 100644 --- a/General-Installing-Instructions.md +++ b/General-Installing-Instructions.md @@ -202,6 +202,9 @@ storage engine which do not get replicated among nodes, which can cause problems. If you configure Cacti to only connect to one node of your cluster and are not load balancing this does not apply to you. +For tested cluster and proxy layouts, see +[Database High Availability](Database-High-Availability.md). + If you are running multiple nodes in a load-balanced environment where you connect to a VIP, you should remove all but one node from rotation during Cacti installation or update. After the installation/update, log in to your MySQL diff --git a/README.md b/README.md index 5bcf7cb..beb2ee3 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,7 @@ PARTICULAR PURPOSE. See the GNU General Public License for more details. - [Restore Cacti on a replacement server](Restore-Cacti.md) - [Diagnose an empty or stale graph](Diagnose-Stale-Graphs.md) - [Measure and size a collector](Size-a-Collector.md) +- [Run the database on a cluster or replica](Database-High-Availability.md) - [Test the alert path end to end](Test-Alert-Delivery.md) - [Accept and report on an ISP circuit](ISP-Circuit-Acceptance.md) - [Hand over a clinic WAN incident](Hospital-WAN-Handover.md)