EnterpriseDB / barman

Barman - Backup and Recovery Manager for PostgreSQL
https://www.pgbarman.org/
GNU General Public License v3.0
2.06k stars 191 forks source link

Add support for config models #873

Closed barthisrael closed 9 months ago

barthisrael commented 10 months ago

Description

This PR adds support for config models in Barman. The main idea of this feature is to make it easier to integrate Barman with HA solutions, e.g. Patroni.

Besides the usual Barman server configuration, we now add support for Barman model configuration. The model acts as a set of overrides for configuration options for a given Barman server.

The servers configuration now have an additional configuration option:

In order to say to Barman that a section of the configuration file is a model, you need to set the configuration option model = true in such section. Also, as explained for the Barman servers, you need to specify a cluster option in the model. That is what makes it possible to apply config models to servers: they need to belong to the same cluster.

The models currently support overriding all server configuration options, except for:

Note: we did it that way because those options can be easily destructive for a Barman server, with the most concerning ones being the directories related as they specify where to store files.

Example

As an example, let's assume you have a PostgreSQL cluster composed of 3 nodes, say A (primary), B (standby), and C (standby), and assume you are taking backups from A through a Barman server named my_cluster. You could create a couple config models which would override the conninfo options of that Barman server. So, in the event of a failover in the cluster, you could ask Barman to apply a given model on top of the Barman server config, overriding the values that would reference the failed node.

Please note that you can either have no active model, or have at most one active model at a given point in time. If you have no active model, only the options from the Barman server take place. If you have an active model, the options defined in the model will override the corresponding options from the Barman server config.

In order to switch configuration models, the user can use the new CLI command barman config-switch. You need to call the command specifying the server name and the model name. That command can also be used with the --reset flag, so the active model is reset, allowing only the server options to take place.

Technical details

Previous to this PR we used to have a ServerConfig class in the code. It used to have the logic for handling the configuration of the Barman servers. While introducing the models implementation we noticed some overlap between how we would handle server and model configuration. With that in mind we created a BaseConfig class which handles the similarities, and created a couple subclasses from it:

We had to change the logic for parsing the Barman configuration, so it is now able to distinguish between sections for Barman servers and for Barman models. The same function which was used to parse and cache the servers from config files is not in charge of parsing and caching the config models. At the very end it will apply any configuration models which are active based on the hidden files that are maintained by Barman inside each server directory.

When parsing config models, Barman will log warning messages about options which are not supported in models. Also, it will deactivate Barman servers if they attempt to apply an invalid model.

References: BAR-122.

barthisrael commented 10 months ago

Thanks for the new review! I've applied most of your suggestions -- except for the one which depends on the order of merging between this PR and #868 .

I'll now work on adjusting the oversight in regards to cluster vs servers relationship -- it should be 1 to many, not 1 to 1.

barthisrael commented 10 months ago

@gcalacoci I just submitted a commit which changes the behavior of ModelConfig, so it allows overrides based on a blacklist instead of a whitelist on the server config options.

edb-sonar-app[bot] commented 10 months ago

SonarQube Quality Gate

Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 9 Code Smells

94.1% 94.1% Coverage
0.0% 0.0% Duplication